http://multidimenstat.weblahko.sk/lecture_time_series1.pdf
http://multidimenstat.weblahko.sk/lesson_time_series_2.pdf
http://multidimenstat.weblahko.sk/lecture_var.pdf
|
Simulation of the action of fundamental trader
and consequent formation of the time series of prices
pf<-1 # fundamental price, intrinsic value (price)
p<-(pf+0.01) # initial price setting
vp<-p # initial vector setting for plot
Ns<-2000 # number of steps
time=c(0:Ns)
for(i in 1:Ns){
if(p>pf) p<-(p * 0.99) # deterministic reduction
if(p<pf) p<-(p * 1.01) # deterministic increase
p<-(p*exp(rnorm(1,mean=0,sd=0.01))) # randomization
vp<-cbind(vp,p)
}
vp<-as.vector(vp)
plot(vp,type="l")
a) acf(vp)
b) pacf(vp)
c) library(tseries)
adf.test(vp)
> adf.test(vp)
Augmented Dickey-Fuller Test
data: vp
Dickey-Fuller = -11.5254, Lag order = 12, p-value = 0.01
alternative hypothesis: stationary
Warning message:
In adf.test(vp) : p-value smaller than printed p-value
(reject null: unit root)
d) kpss.test(vp)
> kpss.test(vp)
KPSS Test for Level Stationarity
data: vp
KPSS Level = 0.0899, Truncation lag parameter = 10, p-value = 0.1
Warning message:
In kpss.test(vp) : p-value greater than printed p-value
(not enough to reject null: i.e. stationary)
Samuelson theory of business cycles - simulation
b3<-1.04
b2<-0.2
b1<-1.01
Y1<-1.0
Y2<-1.04
Y0<-1
Nt<-500
vY<-rep(0,Nt)
for(i in 1:Nt){
Y2<-Y1
Y1<-Y0
Y0<-(b1*(Y1-Y2)+b2*Y1+b3) + 1.0*rnorm(1,mean=0,sd=0.3)
vY[i]<-Y0
}
plot(vY,type="l")
a1) repeat for b1<-0.1 (there is no more "explosive" behaviour)
Linear Filters
plot(UKDriverDeaths)
fi1<-filter(UKDriverDeaths,c(1/2,rep(1,11),1/2)/12)
fi2<-filter(UKDriverDeaths,c(1/2,rep(1,5),1/2)/6)
lines(fi1,col=2)
lines(fi2,col=3)
a1) construct the comprehensive plot including action of
various filters, as wel as generate jpg file as an output
datU<-UKDriverDeaths
f1<-filter(datU,c(1/2,rep(1,5),1/2)/6)
f2<-filter(datU,c(1/2,rep(1,11),1/2)/12)
f3<-filter(datU,c(1/2,rep(1,17),1/2)/18)
f4<-filter(datU,c(1/2,rep(1,23),1/2)/24)
jpeg(filename = "filter4plot.jpg")
par(mfrow=c(2,2))
plot(datU)
lines(f1,col=1)
plot(datU)
lines(f2,col=2)
plot(datU)
lines(f3,col=3)
plot(datU)
lines(f4,col=4)
dev.off()
Decompose
a0)
d1<- decompose(log(UKDriverDeaths))
plot(d1)
a1) plot(co2)
a2)
d2< -decompose(log(co2))
plot(d2)
c-I)
plot(d2$random)
c-II)
d2r<-d2$random
d2s<-d2$seasonal
check: plot(d2r), plot(d2s)
c-III)
calculate acf from d2r and d2s and compare, but there will be some problems
(here are reported problems with NAs when applied adf.test())
d2rr<-d2r[10:450]
d2ss<-d2s[10:450]
> adf.test(d2rr)
Augmented Dickey-Fuller Test
data: d2rr
Dickey-Fuller = -11.7102, Lag order = 7, p-value = 0.01
alternative hypothesis: stationary
Warning message:
In adf.test(d2rr) : p-value smaller than printed p-value
>
> kpss.test(d2rr)
KPSS Test for Level Stationarity
data: d2r
KPSS Level = 0.0067, Truncation lag parameter = 4, p-value = 0.1
Warning message:
In kpss.test(d2rr) : p-value greater than printed p-value
> acf(d2rr)
( still 6 months "memory" exists in random component)
ARIMA MODEL SIMULATION
(find best model, see example from fundamental price generation)
pfun<-1 # "fundamental" price
p<-(pfun+0.01) # initial price setting
vp<-p # auxilliary initial vector setting for plot
Ns<-1000 # number of steps
time=c(0:Ns) # vector of times
for(i in 1:Ns){
if(p>pfun) p<-(p * 0.99) # deterministic reduction
if(p<pfun) p<-(p * 1.01) # deterministic increase
p<-(p*exp(rnorm(1,mean=0,sd=0.01))) #randomization
vp<-cbind(vp,p)
}
# ( to check data by means of plot(time,vp,type="l") )
vp<-as.vector(vp)
a1=arima(vp,order=c(0,0,0))
a2=arima(vp,order=c(0,0,1))
a3=arima(vp,order=c(0,1,0))
a4=arima(vp,order=c(1,0,0))
a5=arima(vp,order=c(1,1,0))
a6=arima(vp,order=c(0,1,1))
a7=arima(vp,order=c(1,0,1))
a8=arima(vp,order=c(1,1,1))
a8=arima(vp,order=c(0,0,2))
a9=arima(vp,order=c(0,2,0))
a10=arima(vp,order=c(2,0,0))
veca=as.vector(cbind(a1$aic,a2$aic,a3$aic,a4$aic,
a5$aic,a6$aic,a7$aic,a8$aic,a9$aic,a10$aic))
which.min(veca)
> which.min(veca)
[1] 4
>
# Output of a4 model ....
> veca[4]
[1] -6234.531
> a4
Call:
arima(x = vp, order = c(1, 0, 0))
Coefficients:
ar1 intercept
0.4163 1.0054
s.e. 0.0287 0.0006
sigma^2 estimated as 0.0001148: log likelihood = 3120.27, aic = -6234.53
Sub-task - perform search for optimal arima using Monte Carlo method
a1) change in one parameter, task - create ormax dimensional vector
of aic values
d1<-decompose(log(UKDriverDeaths))
d1r<-d1$random
d1rr<-d1r[10:180]
ordmax<-20
aric<-rep(0,ordmax)
for(i1 in 0:ordmax){
ar<-arima(d1r,order=c(i1,0,0))
aric[i1]<-ar$aic
}
plot(aric)
ARIMA simulator
# compare and estimate role of autoregression
a) try to investigate influence of autoregressive term
ars<-arima.sim(n = 1000, list(ar = c(0.6897, -0.4858),
ma = c(-0.2279, 0.2488)),sd = sqrt(0.1796))
plot(acf(ars))
# change strength of autoregression term
ars1<-arima.sim(n = 1000, list(ar = c(0.9897, -0.4858), ma = c(-0.2279, 0.2488)),sd = sqrt(0.1796))
acf0<-acf(ars)
acf1<-acf(ars1)
plot(acf0[[1]],col=4, type="l")
lines(acf1[[1]], col=2)
b) demonstrate influence of positive autoregressive term
ars1<-arima.sim(n = 2000, list(ar = c(0.1, -0.4858), ma = c(-0.2279, 0.2488)),sd = sqrt(0.1796))
ars2<-arima.sim(n = 2000, list(ar = c(0.3, -0.4858), ma = c(-0.2279, 0.2488)),sd = sqrt(0.1796))
ars3<-arima.sim(n = 2000, list(ar = c(0.5, -0.4858), ma = c(-0.2279, 0.2488)),sd = sqrt(0.1796))
ars4<-arima.sim(n = 2000, list(ar = c(0.9, -0.4858), ma = c(-0.2279, 0.2488)),sd = sqrt(0.1796))
jpeg(filename = "acf14.jpg")
par(mfrow=c(2,2))
acf(ars1)
acf(ars2)
acf(ars3)
acf(ars4)
dev.off()
VAR model
library(vars)
data(Canada)
var.2c <- VAR(Canada, p = 2, type = "const")
> var.2c
VAR Estimation Results:
=======================
Estimated coefficients for equation e:
======================================
Call:
e = e.l1 + prod.l1 + rw.l1 + U.l1 + e.l2 + prod.l2 + rw.l2 + U.l2 + const
e.l1 prod.l1 rw.l1 U.l1 e.l2
1.637821e+00 1.672717e-01 -6.311863e-02 2.655848e-01 -4.971338e-01
prod.l2 rw.l2 U.l2 const
-1.016501e-01 3.844492e-03 1.326893e-01 -1.369984e+02
causality(var.2c, cause = "e")
> causality(var.2c, cause = "e")
$Granger
Granger causality H0: e do not Granger-cause prod rw U
data: VAR object var.2c
F-Test = 6.2768, df1 = 6, df2 = 292, p-value = 3.206e-06
$Instant
H0: No instantaneous causality between: e and prod rw U
data: VAR object var.2c
Chi-squared = 26.0685, df = 3, p-value = 9.228e-06
# use a robust HC variance-covariance matrix for the Granger test:
causality(var.2c, cause = "e", vcov.=vcovHC(var.2c))