Tietokoneharjoitus 6

Tehtävä 5

Sovita AR(p) malli SP500 tuottojen itseisarvoihin.

file<-"http://cc.oulu.fi/~jklemela/timeseries/sp500.csv"
data<-read.csv(file=file)
sp500<-data[,7]                # otetaan kunkin paivan lopetuskurssi
sp500<-sp500[length(sp500):1]  # aloitetaan aikasarja vanhimmasta havainnosta

# piiretaan aikasarja
plot(sp500,type="l")

# tulostetaan tuotot
y<-sp500[2:length(sp500)]-sp500[1:(length(sp500)-1)]
r<-y/sp500[1:(length(sp500)-1)]
plot(r,type="l")

plot(abs(r),type="l")

library(tseries)

# sovitetaan AR-malli  ->  arma

p<-20
order<-c(p,0,0)
ari<-arma(abs(r),order=order)
ari
summary(ari)

Call:
arma(x = abs(r), order = order)

Coefficient(s):
      ar1        ar2        ar3        ar4        ar5        ar6        ar7  
 0.060794   0.100058   0.064395   0.055392   0.109019   0.048397   0.044250  
      ar8        ar9       ar10       ar11       ar12       ar13       ar14  
 0.036990   0.038166   0.030609   0.052837   0.027897   0.015074   0.002718  
     ar15       ar16       ar17       ar18       ar19       ar20  intercept  
 0.020151   0.021892   0.020400   0.036885   0.015948   0.022011   0.001161  


Call:
arma(x = abs(r), order = order)

Model:
ARMA(20,0)

Residuals:
      Min        1Q    Median        3Q       Max 
-0.040189 -0.003684 -0.001267  0.002467  0.190536 

Coefficient(s):
           Estimate  Std. Error  t value Pr(>|t|)    
ar1       0.0607937   0.0078922    7.703 1.33e-14 ***
ar2       0.1000583   0.0079068   12.655  < 2e-16 ***
ar3       0.0643954   0.0079410    8.109 4.44e-16 ***
ar4       0.0553917   0.0079557    6.963 3.34e-12 ***
ar5       0.1090188   0.0079659   13.686  < 2e-16 ***
ar6       0.0483974   0.0080105    6.042 1.52e-09 ***
ar7       0.0442496   0.0080195    5.518 3.43e-08 ***
ar8       0.0369903   0.0080264    4.609 4.05e-06 ***
ar9       0.0381662   0.0080284    4.754 2.00e-06 ***
ar10      0.0306089   0.0080231    3.815 0.000136 ***
ar11      0.0528370   0.0080231    6.586 4.53e-11 ***
ar12      0.0278970   0.0080282    3.475 0.000511 ***
ar13      0.0150737   0.0080260    1.878 0.060366 .  
ar14      0.0027177   0.0080180    0.339 0.734647    
ar15      0.0201508   0.0080088    2.516 0.011867 *  
ar16      0.0218917   0.0079643    2.749 0.005983 ** 
ar17      0.0204001   0.0079543    2.565 0.010328 *  
ar18      0.0368855   0.0079398    4.646 3.39e-06 ***
ar19      0.0159482   0.0079061    2.017 0.043674 *  
ar20      0.0220106   0.0078922    2.789 0.005289 ** 
intercept 0.0011609   0.0001012   11.467  < 2e-16 ***
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Fit:
sigma^2 estimated as 4.114e-05,  Conditional Sum-of-Squares = 0.66,  AIC = -116470.9



# residuaalit ################################

resi<-residuals(ari)
plot(resi)

acf(resi,na.action=na.pass)


# AIC  ->   arima0 ###############################

p<-30
aics<-matrix(0,p,1)
for (i in 1:p){
  order<-c(i,0,0)
  aics[i]<-arima0(r,order=order)$aic
}
plot(aics)

aics<-c(
-103145.1,
-103174.5,
-103172.5,
-103171.3,
-103173.5,
-103172.1,
-103177.2,
-103176.7,
-103176.1,
-103176.6,
-103178.5,
-103189.1,
-103187.1,
-103185.1,
-103185.1,
-103200.1,
-103199.0,
-103204.3,
-103202.4,
-103201.3,
-103207.2,
-103205.2,
-103203.3,
-103202.2,
-103202.4,
-103207.7,
-103211.9,
-103210.0,
-103217.6,
-103216.5
)
plot(aics,type="b")


p<-30
aics<-matrix(0,p,1)
for (i in 1:p){
  order<-c(i,0,0)
  aics[i]<-arima0(abs(r),order=order)$aic
}
plot(aics)
aics

aics<-c(
-113887.8,
-114749.5,
-115170.9,
-115444.7,
-115876.5,
-116013.9,
-116117.5,
-116188.3,
-116253.2,
-116295.5,
-116371.9,
-116398.2,
-116409.6,
-116411.0,
-116424.4,
-116436.7,
-116445.6,
-116469.6,
-116472.4,
-116478.0,
-116482.8,
-116481.0,
-116488.0,
-116494.3,
-116494.0,
-116492.2,
-116522.7,
-116523.7,
-116532.8
)
plot(aics)

 
# Sum of squared prediction errors ####################

p<-10
sspe<-matrix(0,p,1)
t0<-10000
T<-length(r)
for (i in 1:p){
  order<-c(i,0)
  errs<-matrix(0,T-t0,1)
  for (t in t0:(T-1)){
     x<-r[1:t]
     coef<-arma(x,order=order)$coef
     rhat<-sum(as.vector(coef[1:i])*r[t:(t-i+1)])
     errs[t-t0+1]<-r[t+1]-rhat
  }
  sspe[i]<-sqrt(sum(errs^2))
}
plot(sspe)


sspe<-c(
0.9058649,
0.9058457,
0.9060230,
0.9061900,
0.9063012,
0.9066239,
0.9064767,
0.9065603,
0.9068165,
0.9068918
)
plot(sspe,type="b")