Computer Lab: Assignment training exercises This lab is a variation on Lab 5. Web page: http://www.ma.hw.ac.uk/~andrewc/erm Go to the module web page and, if you have not already done so copy the following files to your directory \FRM functions10A.r functions10B.r # This is a new set of functions # some of which replace ones in functions10A.r sp500.r nasdaq.r Load each one into your R workspace, if you have not already done so: for example source("functions10A.r") source("functions10B.r") source("sp500.r") source("nasdaq.r") ### Note some graphical results with discussion will appear later ### on the module web page. x=sp500$adjclose tv=sp500$t n=length(x) tv=tv[2:n] d=diff(log(x)) nd=length(d) # Note that tv is the same length as d nv=(1:nd)[tv > 1980] # nv is a set of index numbers tv=tv[nv] # tv[nv] extracts the dates which are all after 1980 d=d[nv] # This line extracts the daily log returns after 1980 nd=length(d) # recalculate the length of d plot(tv,d,pch=20) # The next few lines take a while to run! # They output the current log-likelihood on the screen so you can watch progress # Don't worry about warning messages that appear at the end. These occur if the # optimisation algorithm steps outside the admissible region for parameters # at any stage (e.g. it tries a value for nu that gives infinite variance. res1=fit.garch11.normal(d) # Fit GARCH(1,1) assuming Z(t)~N(0,1) # This is the same as the quasi maximum likelihood # which assumes the Z(t) are normal res2=fit.garch11.t(d) # Fit GARCH(1,1); Z(t)~t_nu with E[Z]=0, Var[Z]=1 res3=fit.garch11.nct(d) # Fit GARCH(1,1); Z(t)~NCT with E[Z]=0, Var[Z]=1 #################################### # Simulation #################################### # functions10B.r contains functions that can be used for simulation # assuming normally distributed Z(t) # # Students need to develop their own code for the t and NCT n.sim=1000 # The number of independent sample paths that we want to generate x=sp500$adjclose # reset this vector in case anything has been done to it in the meantime X0=x[length(x)] # Final value of the index = starting point for simulations T1=50 # How many days forward do you want to simulate res=res1 # Use the output from the fit.garch11.normal routine Sres1=simulate.garch11.normal.R(n.sim,X0,T1,res) names(Sres1) # Check what is output in Sres1 # t in a time vector running from time 0 (index value X0) to T1 (i.e. T1+1 values) # X is an array of dimension n.sim x (T1+1) and it gives the sample paths of the index, X(t) ### The first column of X is time 0 and is therefore constant and equal to X0 ### The second column of X is time 1 and gives the simulated values of the index X(t) # d is an array of dimension n.sim x T1 and gives the corresponding daily log returns # sigma is an array of dimension n.sim x T1 and gives the corresponding daily volatilities ### The first column of sigma will be constant and equal to sigma(n+1) derived ### from the GARCH(1,1) fitting exercise # Z is an array of dimension n.sim x T1 and gives the corresponding daily i.i.d. innovations Z(t) # Thus, try e.g. dim(Sres1$X) plot(Sres1$t,Sres1$X[1,],type="l") # Plot sample path number 1 of X(t) lines(Sres1$t,Sres1$X[2,],col=2) # Add sample path number 2 # Now plot a bundle of sample paths of X(t) setaxes(0,50,1000,1400) for(i in 1:1000){ lines(Sres1$t,Sres1$X[i,],col=i) } ####################################################### # Fitting GARCH(1,1) models with time-dependent mu(t) ####################################################### res1=fit.garch11.normal(d) # Fit GARCH(1,1) assuming Z(t)~N(0,1) # and mu(t)=mu constant res1B=fit.garch11.normal2(d) # Fit GARCH(1,1) assuming Z(t)~N(0,1) # and mu(t)=mu0 + mu1.(delta(t-1) - mu0) # Compare the log-likelihoods res1$ll res1B$ll # Under the AIC would you prefer the basic model or the model with mu(t)=mu0 + mu1.(delta(t-1) - mu0)? # What about the BIC?