Professional Documents
Culture Documents
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
(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
xt xt
xt+1 (feedback)
At , (MLE)
xt Kalman filter
smoothingfiltering
forecastingKalman filter filtering
Kalman filter
Kalman filter
0.2
dlm
(6)
t = t1 + w,t
w,t N (0, 2 )
(7)
t = t1 + w,t
N (0, 2 )
(8)
w,t
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
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
(11)
profit
n
Y
prof it =
(tradesi + 1)
(12)
i=1
Figure: (3)SP500
2007-01012009-01-01 SP500364.5096%
7
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