#Chapter 4 ## TAR example: n=200 #nr observations shocks=rnorm(n) next.obs=function(x,a){ifelse(x<0,-1.5*x+a,0.5*x+a)} x=0 for (i in 1:n) {x=c(x,next.obs(x[i],shocks[i]))} plot(x, type="l") abline(0,0) ## Neural Network stuff: # You need to install this library: library(nnet) #We use daily intel data intel.daily=read.csv("DailyINTCfrom2000.csv",header=T) intel.prices=intel.daily$Adj.Close[length(intel.daily$Adj.Close):1] library(fBasics) intel.returns=returns(intel.prices)[-1] #Separate data: length(intel.returns) ## The last 60 observations are from April 30 to present -we will use these for testing (because of the crazy last year) ## I am going to use the first 2340 observations for training and the rest for testing. pacf(intel.returns[1:2340]) #We will only use the 3 lags as inputs: intel.x=cbind(intel.returns[1:2337],intel.returns[2:2338],intel.returns[3:2339]) y=intel.returns[4:2340] # the output # Now we need to build the neural network. # The size of the hidden layer is not specified. We will use 10 just because it is a round number. # We want to predict a single number so we need only one node for the output #In general any neural network algorithm is very slow but the one build in R #is actually using C routines and it is surprisingly fast intel.nnet=nnet(intel.x,y, size=10, linout=T, decay=0.01, maxit=1000) ## intel.x is the matrix containing the values for the input nodes as lines ## y is the output node values also as lines ## size specifies the hidden layer size ## linout=T tells R to use linear functions for the second activation function. ## linout=F means use the logistic function ## decay= 0.001 is the step used when learning the weights ## maxit sets up a maximum number of iterations for the convergence of the algorithm summary(intel.nnet) ## this shows the nodes and the weights ## We can look at the predicted values versus the true values: plot(y,predict(intel.nnet,intel.x), main="Neural Net Results",xlab="True",ylab="NN predictions") abline(h=0,v=0); abline(0,1,lty=2) ## the later plots the 45 degree line. The points should have been close to this line #In general it is better to center the variables before using them in neural networks, however these are adjusted returns and their mean is pretty close to zero. #How about the sum of squares for the predicted values: intel.p=cbind(intel.returns[2338:2401],intel.returns[2339:2402],intel.returns[2340:2403]) y.pred=predict(intel.nnet,intel.p) y.obs=intel.returns[2340:2403] #observed values # sum of squares: sum((y.pred-y.obs)^2) ## What about using more inputs (recall the pacf showed evidence of lag 13 and lag 16 being significant too ## What about using more or less nodes in the hidden layer ## What about using a logistic function for the second activation function ## How does this compare with the ARMA models? ## actually about the last question: intel.ar=ar(intel.returns[1:2339]) intel.ar.pred= predict(intel.ar,n.ahead=64)$pred sum((y.obs-intel.ar.pred)^2) ## this is actually better