You are on page 1of 11

4.

/state space model

Q261821669
July 15, 2016

0.1

Tsay book11y1 x1

y1 = a1 x1 + v1 .

(1)

a1
v1
normal error
the hidden state
y1 y2 y3
x1 x2
x3
y1 = a1 x1 + v1

(2)

y2 = a2 x2 + v2
y3 = a3 x3 + v3

y1 = a1,1 x1 + a1,2 x2 + a1,3 x3 + v1

(3)

y2 = a2,1 x2 + a2,2 x2 + a2,3 x3 + v2


y3 = a3,1 x3 + a3,2 x2 + a3,3 x3 + v3 .
Equation(3)
Equation(3)

y = Ax + v,
y{y1 , y2 , y3 }x{x1 , x2 , x3 }A{aij }v{v1 , v2 , v3 }
normal errors
portfoliotyt
xt

yt = At xt + vt
xt = xt1 + wt

measurement/observation equation
transition/state equation
1

(4)
(5)


twt {w1 , w2 , w3 }
normal errors

the hidden statesxt


xt1 AR(1)
(Markov Process)
(state space models)
yt yt measurement
measurement

xt xt
xt+1 (feedback)
At , (MLE)
xt Kalman filter
smoothingfiltering
forecastingKalman filter filtering
Kalman filter
Kalman filter

0.2

dlm

Rdlm(dynamic linear models)


state space ARMAXstate space ARIMAstate space Stochastic
VolatilityLinear regression model
time varying parameters(tvp)state space
linear regression
yt = t + t xt + vt , vt N (0, t2 )

(6)

t = t1 + w,t

w,t N (0, 2 )

(7)

t = t1 + w,t

N (0, 2 )

(8)

w,t

t , t the hidden states t =


{t , t }0 vt yt normal
errorw,t t normal errorw,t t normal errorstate
space linear regression

yt = (1 xt )t + vt

 

  
w,t
t1
t
1 0
+
=
0 1
w,t
t1
t

(9)
(10)



1 0
At (1 xt )

0 1
Equation(9,10)dlm
XOMJPM filtering
smoothingforecastingXOM yt JMPxt Figure (1)
Figure (2)

require(quantmod)
require(dlm)
require(PerformanceAnalytics)
symbols <- c("XOM","JPM") # portfolio
symbolData <- new.env()
getSymbols(symbols, env = symbolData, src = "yahoo",
from="2012-01-01", to="2012-12-31")
ClosePrices <- do.call(cbind,eapply(symbolData, Cl))
xom <- ClosePrices[,2]
jpm <- ClosePrices[,1]
buildTVP <- function(parm, x.mat){
parm <- exp(parm)
return( dlmModReg(X=x.mat, dV=parm[1],
dW=c(parm[2], parm[3])) )
}
start.vals = c(0,0,0)
names(start.vals) = c("lns2v", "lns2a", "lns2b")
TVP.mle = dlmMLE(y=xom, parm=start.vals, x.mat=jpm,
build=buildTVP, hessian=T)
se2 <- sqrt(exp(TVP.mle$par))
names(se2) = c("sv", "sa", "sb")
# fitted ss model
3

Figure 1: Linear Regression

Figure 2: Linear Regression

TVP.dlm <- buildTVP(TVP.mle$par, jpm)


# filtering
TVP.f <- dlmFilter(xom, TVP.dlm)
# smoothing
TVP.s <- dlmSmooth(TVP.f)
# extract smoothed states - intercept and slope coefs
alpha.s = xts(TVP.s$s[-1,1,drop=FALSE],
as.Date(rownames(TVP.s$s[-1,])))
beta.s = xts(TVP.s$s[-1,2,drop=FALSE],
as.Date(rownames(TVP.s$s[-1,])))
colnames(alpha.s) = "alpha"
colnames(beta.s) = "beta"
# extract std errors - dlmSvd2var gives list of MSE matrices
mse.list = dlmSvd2var(TVP.s$U.S, TVP.s$D.S)
se.mat = t(sapply(mse.list, FUN=function(x) sqrt(diag(x))))
se.xts = xts(se.mat[-1, ], index(beta.s))
colnames(se.xts) = c("alpha", "beta")
a.u = alpha.s + 1.96*se.xts[,"alpha"]
a.l = alpha.s - 1.96*se.xts[, "alpha"]
b.u = beta.s + 1.96*se.xts[,"beta"]
b.l = beta.s - 1.96*se.xts[, "beta"]
layout(1:2)
chart.TimeSeries(cbind(alpha.s, a.l, a.u),
main="Smoothed estimates of alpha",
colorset=c(1,2,2), lty=c(1,2,2),ylab=expression(alpha),xlab="")
chart.TimeSeries(cbind(beta.s, b.l, b.u),
main="Smoothed estimates of beta",
colorset=c(1,2,2), lty=c(1,2,2),ylab=expression(beta),xlab="")
# forecasting using dlmFilter
# add 10 missing values to end of sample
new.xts = xts(rep(NA, 10),
seq.Date(from=end(xom), by="months", length.out=11)[-1])
xom.ext = merge(xom, new.xts)[,1]
TVP.ext.f = dlmFilter(xom.ext, TVP.dlm)
# extract h-step ahead forecasts of state vector
TVP.ext.f$m[as.character(index(new.xts)),]

0.3

Exponential Smoothing

Equation(4, 5)
MLE
R
Exponential Smoothing,
forecastets()(error trend seasons)

AIC BICtrend
trendseasonseasonerror
trend seasonsmicro structures
30days=30

regime3
rolling30ets()
forecast()
1-1
Rrollapply
zooregime()rollapply()
rrregime()rollapplyR
rregime()
1
.31 .3 = .3
-1
-.3
1 (.3) = .3
regime()rollapplyside
SP500next.r

trades = side next.r

(11)

profit

n
Y
prof it =
(tradesi + 1)
(12)
i=1

Figure: (3)SP500
2007-01012009-01-01 SP500364.5096%
7

Figure 3: Exponetial Smoothing SP500

require(quantmod)
library(forecast)
require(zoo)
symbol<-c("^GSPC")
symbolData <- new.env()
getSymbols(symbol, env = symbolData, src = "yahoo",
from="2007-01-01", to="2009-01-01")
sp500 <- do.call(cbind,eapply(symbolData, Cl))

regime<-function(rolling){
fit
<- ets(rolling,damped=FALSE)
fcast <- forecast(fit, h=1,level=95)
if(tail(rolling,1)<fcast$mean){1} else{-1}
}
days
<- 30
r
<- na.omit(ROC(sp500,1,"discrete"))
TS
<- zoo(r)
side
<- rollapply(TS, width = days, by = 1, FUN = regime,
by.column=FALSE, align = "right")
side
<- as.numeric(head(side,-1))
next.r <- tail(r,-days)
trades <- side*next.r
layout(1:2)
profit
<- cumprod(trades+1)
sp500.plot <- tail(sp500,-(days+1))
plot(sp500.plot, main = "SP500 Prices")
plot(profit)
length(sp500.plot)
length(profit)

0.4

1. R
2. R
9

3. R
4. R
5. R
6. R
7. R
8. R

10

You might also like