在R

时间:2016-06-12 13:49:38

标签: r kalman-filter

我一直试图弄清楚如何定义方程式和参数来运行带有4个变量的卡尔曼滤波器(fkf包)。我正在使用的模型是扩展的资本资产定价模型,变量是因变量:Rpt(超额投资组合收益)和三个独立变量:Rmt(excelss月度收益),SMB(规模因子)和ILLIQ(非流动性)因子)。该数据包括5年运行的22种股票的月度数据。该链接提供了数据集外观的概念,以便于参考。 Portfolio returns。这是前三个股票的数据集:3 first stocks

加载上面的数据集(完整),我已经能够运行只有一个变量Rpt的卡尔曼滤波器。但是,如何向模型添加更多变量?我正在努力定义正确的论证和方程式。有人能帮忙吗?下面是我到目前为止的代码。

namibia1 <- read.delim(file.choose(), header=T)
attach(namibia1)
namibia1_ts <- ts(as.numeric(namibia1[,1]), frequency=12, start=c(2011,1), end=c(2015,12))
namibia1_ts <- ts(as.numeric(namibia1[,1]), frequency=12, start=c(2011,1), end=c(2015,12))
y <- namibia1_ts
dt <- ct <- matrix(0)
Tt <- matrix(1)
a0 <- y[1]
P0 <- matrix(1)
Zt <- matrix(c(1), ncol=60)
fit.fkf <- optim(c(HHt = var(y, na.rm=TRUE)*.5, 
                   GGt = var(y, na.rm=TRUE)*.5), 
                   fn  = function(par, ...){ -fkf(HHt = matrix(par[1]) }, 
                   GGt = matrix(par[2]), ...)$logLik, 
                   yt  = rbind(y), 
                   a0 = a0, P0 = P0, dt = dt, ct = ct, Zt = Zt, Tt = Tt, 
                  check.input = FALSE)

我更准确的是如何将过渡和测量方程定义为矩阵,如果是,如何定义?这似乎是一件非常容易的事,但到目前为止,它对我没用。

1 个答案:

答案 0 :(得分:3)

当解释变量的数量为1时,测量和转移方程为: enter image description here 在此图像中,[1 Xt]为Zt,νt的错误为GGtdiag(2)是Tt。在图像上方,我假设没有拦截,因此ct = matrix(0)dt <- matrix(0, nrow=2, ncol=1)。预计c(μ0, β0)a0,其差异为P0

# using data
namibia1 <- data.frame(Rpt = c(-7.9466, 7.0845, -6.4460, 5.0913, -0.1614, 10.4113, 11.5786, 
                              -6.6402, -6.9760, -0.7926, 7.2900, 16.1156, -12.1467),
                      Rmt = c(-2.9151, -2.6468, -3.5493, -2.9928, -1.8305, -1.8491, -3.2975,
                              -0.9582, -4.1794, -5.0553, 1.8858, -1.4464, -2.9151),
                      SMB = c(-2.5471, -4.3690, 4.2772, 12.1632, 5.1860, -5.3839, -1.4868, 
                              12.1463, 10.8383, 8.4175, -11.1109, -32.2698, -0.5691),
                      ILLIQ = c(-1.7851, 18.3652, -3.2890, 4.9808, -13.8678, -2.9312, 18.5644,
                                -2.6254, -23.1361, 3.2620, -8.3979, 37.6330, 14.7067),
                      Year = c(rep(2011, 12), 2012),
                      Month = c("January", "February", "March", "April", "May", "June", "Juli", "August",
                                "September", "October", "November", "December", "Janualy"))

# In your equations, m = 4, d = 1, n = nrow(namibia1)
y <- namibia1$Rpt
dt <- matrix(0, nrow=4, ncol=1)
ct <- matrix(0)
Tt <- diag(4)
Zt <- array(t(cbind(rep(1,nrow(namibia1)), namibia1[,2:4])), dim=c(1, 4, nrow(namibia1)))
a0 <- c(1, 1, 1, 1)
P0 <- matrix(100, nrow=4, ncol=4)

fit.fkf <- optim(c(1, 1, 1, 1, 1),
                 fn = function(par, ...) -fkf(HHt = diag(4) * par[1:4], GGt = matrix(par[5]), ...)$logLik,
                 yt = rbind(y), a0 = a0, P0 = P0, dt = dt, ct = ct,
                 Zt = Zt, Tt = Tt, check.input = T)
sqrt(fit.fkf$par) # estimated sd
fkf.obj <- fkf(a0, P0, dt, ct, Tt, Zt, HHt = diag(4) * fit.fkf$par[1:4],
               GGt = matrix(fit.fkf$par[5]), yt = rbind(y))
# the value of a0, P0 and optim's par mean nothing special.


编辑(对不起,我忘了斜坡。)

estimatedLevel <- fkf.obj$att[1,] + namibia1[,2] * fkf.obj$att[2,] + namibia1[,3] * fkf.obj$att[3,] + namibia1[,4] * fkf.obj$att[4,]

plot(y)
lines(estimatedLevel, col = "blue")

enter image description here

相关问题