# Tietokoneharjoitus 5

## Tehtävä 5

Sovita ARMA(p,q) malli SP500 tuottoihin.

```file<-"http://cc.oulu.fi/~jklemela/timeseries/sp500.csv"
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")

acf(r)
pacf(r)

library(tseries)

# estimoidaan ARMA(1,1)-malli  ######################

order<-c(1,1)
ar<-arma(r,order=order)
ar

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

Coefficient(s):
ar1         ma1   intercept
-0.4651911   0.5037691   0.0004926

# summary

summary(ar)

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

Model:
ARMA(1,1)

Residuals:
Min         1Q     Median         3Q        Max
-0.2030770 -0.0044682  0.0001331  0.0046357  0.1143614

Coefficient(s):
Estimate  Std. Error  t value Pr(>|t|)
ar1       -0.4651911   0.0825093   -5.638 1.72e-08 ***
ma1        0.5037691   0.0805997    6.250 4.10e-10 ***
intercept  0.0004926   0.0001187    4.148 3.35e-05 ***
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Fit:
sigma^2 estimated as 9.45e-05,  Conditional Sum-of-Squares = 1.52,  AIC = -103161.1

###### kokeillaan arima0-funktiota  #############################

order<-c(1,0,1)
ari<-arima0(r,order=order)
ari

Call:
arima0(x = r, order = order)

Coefficients:
ar1     ma1  intercept
-0.4892  0.5273      3e-04
s.e.   0.0072  0.1103      1e-04

sigma^2 estimated as 9.449e-05:  log likelihood = 51584.44,  aic = -103160.9

#### estimoidaan AR(p)-malli tuottojen nelioille ##################

order<-c(10,0)
ar<-arma(r^2,order=order)
ar

summary(ar)

Call:
arma(x = r^2, order = order)

Model:
ARMA(10,0)

Residuals:
Min         1Q     Median         3Q        Max
-2.754e-03 -6.229e-05 -4.120e-05  1.113e-07  4.143e-02

Coefficient(s):
Estimate  Std. Error  t value Pr(>|t|)
ar1        9.103e-02   7.894e-03   11.532  < 2e-16 ***
ar2        1.973e-01   7.911e-03   24.943  < 2e-16 ***
ar3        2.068e-02   8.058e-03    2.567   0.0103 *
ar4        5.542e-03   8.059e-03    0.688   0.4917
ar5        1.480e-01   8.055e-03   18.370  < 2e-16 ***
ar6        3.558e-02   8.055e-03    4.417 1.00e-05 ***
ar7       -3.907e-03   8.059e-03   -0.485   0.6278
ar8        3.567e-02   8.058e-03    4.427 9.55e-06 ***
ar9        6.257e-02   7.911e-03    7.910 2.66e-15 ***
ar10       1.216e-02   7.894e-03    1.541   0.1233
intercept  3.752e-05   3.655e-06   10.264  < 2e-16 ***
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Fit:
sigma^2 estimated as 1.829e-07,  Conditional Sum-of-Squares = 0,  AIC = -203400.5

#################

order<-c(5,0,0)
ari<-arima0(r^2,order=order)
ari

Call:
arima0(x = r^2, order = order)

Coefficients:
ar1     ar2     ar3     ar4     ar5  intercept
0.1034  0.2036  0.0314  0.0263  0.1596      1e-04
s.e.  0.0078  0.0078  0.0080  0.0078  0.0078      0e+00

sigma^2 estimated as 1.843e-07:  log likelihood = 101647.5,  aic = -203281

ari\$aic

[1] -203281

```
```#######################################

order<-c(5,0,0)
arima0(abs(r),order=order)

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[1:29])

[,1]
[1,] -113887.8
[2,] -114749.5
[3,] -115170.9
[4,] -115444.7
[5,] -115876.5
[6,] -116013.9
[7,] -116117.5
[8,] -116188.3
[9,] -116253.2
[10,] -116295.5
[11,] -116371.9
[12,] -116398.2
[13,] -116409.6
[14,] -116411.0
[15,] -116424.4
[16,] -116436.7
[17,] -116445.6
[18,] -116469.6
[19,] -116472.4
[20,] -116478.0
[21,] -116482.8
[22,] -116481.0
[23,] -116488.0
[24,] -116494.3
[25,] -116494.0
[26,] -116492.2
[27,] -116522.7
[28,] -116523.7
[29,] -116532.8
[30,]       0.0
>

```