SHARE
TWEET

Neural network test in R

mjaniec Jul 31st, 2012 97 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # using Artificial Neural Networks to predict (unsuccessfully...) stock price changes
  2. # see more: http://reakkt.com
  3.  
  4. rm(list=ls())
  5.  
  6. library(neuralnet)
  7.  
  8. data.file <- "fw20_d.csv" # EOD data from stooq.com
  9.  
  10. # n=500, d=50, l=4 lub 3 dla t=10
  11. # n=500, d=50, l=3 dla t=5
  12.  
  13. d <- 50
  14.  
  15. n.train <- 500
  16. n.test  <- 7
  17.  
  18. layers      <- 3
  19. repetitions <- 2
  20.  
  21. n <- n.train+n.test+d
  22.  
  23. fit.n <- 100 # number of s1 date draws
  24.  
  25. market.data <- read.csv(data.file,header=TRUE)
  26.  
  27. x.market <- log(market.data[-1,"Close"]/
  28.                 market.data[-nrow(market.data),"Close"])
  29.  
  30. market.n <- length(x.market)
  31.  
  32. fit.ratio  <- numeric(fit.n)
  33. random.fit <- numeric(fit.n)
  34.  
  35. for (i in 1:fit.n) {
  36.  
  37.   s1 <- round(runif(1,n,market.n))
  38.  
  39.   x <- x.market[(s1-n+1):s1]
  40.  
  41.   x.matrix <- cbind( tail(x,n-d),
  42.                      sapply(1:(d-1), function(i) tail(head(x,n-i),n-d) ),
  43.                      head(x,n-d) )
  44.  
  45.   colnames(x.matrix) <- c("X",paste("X",1:d,sep=""))
  46.   x.df <- data.frame(x.matrix)
  47.  
  48.   x.df.train <- x.df[1:n.train,]
  49.   x.df.test  <- x.df[(n.train+1):(n.train+n.test),]
  50.  
  51.   # some other parameters to try:
  52.   # act.fct="tanh"
  53.   # err.fct="ce"
  54.  
  55.   f <- paste("X",paste("X",1:d,"+",sep="",collapse=""),sep="~")
  56.   f <- substr(f,1,nchar(f)-1)
  57.  
  58.   nn <- neuralnet(eval(parse(t=f)),data=x.df.train,
  59.                   # act.fct="tanh",
  60.                   hidden=layers,rep=repetitions,linear.output=TRUE)
  61.  
  62.   if (length(nn$weights)==0) stop("No convergence") else {
  63.  
  64.     # plot(nn)
  65.    
  66.     x.forecast <- compute(nn,x.df.test[,-1])
  67.    
  68.     matplot(cbind(x.df[(n-d-n.test+1):(n-d),1],x.forecast$net.result),type="l",lty="solid",ylab="")
  69.     points(x.df[(n-d-n.test+1):(n-d),1],col="Black",pch=16)
  70.     points(x.forecast$net.result,col="Red",pch=16)
  71.     abline(h=0,col="Orange",lty="dotted")
  72.    
  73.     # cbind(x.df.test[,1],x.forecast$net.result)
  74.    
  75.     conform.sign <- sum(sign(x.df.test[,1])==sign(x.forecast$net.result))
  76.  
  77.     # conform.diff <- sum((x.df.test[,1]-x.forecast$net.result)^2)
  78.    
  79.   }
  80.  
  81.   fit.ratio[i]  <- conform.sign/n.test
  82.  
  83.   random.fit[i] <- sum(sign(x.df.test[,1])==sapply(runif(n.test,0,1), function(i) if (i>0.5) return(1) else return(-1)))/n.test
  84.  
  85. }
  86.  
  87. fit.density     <- density(fit.ratio)
  88. random.density  <- density(random.fit)
  89.  
  90. xlim <- range(c(fit.density$x,random.density$x))
  91. ylim <- range(c(fit.density$y,random.density$y))
  92.  
  93. plot(fit.density,xlim=xlim,ylim=ylim)
  94. lines(random.density,col="Blue",lty="dotted")
  95. abline(v=0.5,col="Red",lty="dotted")
  96.  
  97. summary(random.fit)
  98. summary(fit.ratio)
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top