KFAS
-
Upload
shishirkashyap -
Category
Documents
-
view
95 -
download
0
Transcript of KFAS
![Page 1: KFAS](https://reader034.fdocuments.us/reader034/viewer/2022051110/55149db84a7959b1478b495d/html5/thumbnails/1.jpg)
Package ‘KFAS’February 23, 2011
Version 0.6.1
Date 2011-02-23
Title Kalman filter and smoothers for exponential family state space models.
Author Jouni Helske <[email protected]>
Maintainer Jouni Helske <[email protected]>
Depends R (>= 2.8.0)
Description Package KFAS provides functions for Kalman filtering,state, disturbance and simula-tion smoothing, forecasting and simulation of state space models. All functions can use exactdiffuse initialisation when distributions of some or allelements of initial state vector are unknown. Filtering, statesmoothing and simulation functions use sequential processingalgorithm, which is faster than standard approach, and it alsoallows singularity of prediction error variance matrix. KFASalso contains function for computing the likelihood ofexponential family state space models and function for statesmoothing of exponential family state space models.
License GPL (>= 2)
Repository CRAN
Date/Publication 2011-02-23 16:18:39
R topics documented:distsmoother . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2eflik . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2eflik0 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5efsmoother . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7forecast . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9GlobalTemp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10kf . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11ks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16simsmoother . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
1
![Page 2: KFAS](https://reader034.fdocuments.us/reader034/viewer/2022051110/55149db84a7959b1478b495d/html5/thumbnails/2.jpg)
2 eflik
Index 21
distsmoother Disturbance smoother
Description
Computes smoothed values of disturbance terms eps_t and eta_t, and their variances.
Usage
distsmoother(out)
Arguments
out Output of function ’ks’. Optcal must be (TRUE,TRUE,TRUE,TRUE/FALSE).
Value
A list with the output elements from Kalman smoother and following new elements:
epshat p*n array of E(eps_t | y_1, ..., y_n).
epshatvar p*p*n array of var(eps_t | y_1, ..., y_n).
etahat r*n array of E(eta_t | y_1, ..., y_n).
etahatvar r*r*n array of var(eta_t | y_1, ..., y_n).
References
Koopman, S.J. and Durbin J. (2001). Time Series Analysis by State Space Methods. Oxford: Ox-ford University Press
eflik Log-likelihood of exponential family state-space model.
Description
Function eflik computes log-likelihood function of univariate exponential family state-space modelvia simulation.
Usage
eflik(yt, Zt, Tt, Rt, Qt, a1, P1, P1inf, dist=c("Poisson","Binomial", "Negative binomial"), offset=1, nsim=1000, maxiter=50)
![Page 3: KFAS](https://reader034.fdocuments.us/reader034/viewer/2022051110/55149db84a7959b1478b495d/html5/thumbnails/3.jpg)
eflik 3
Arguments
yt Matrix, array or vector of observations. Note that yt is univariate.
Zt System matrix or array of observation equation.
Tt System matrix or array of transition equation.
Rt System matrix or array of transition equation.
Qt Variance matrix or array of disturbance terms eta_t.
a1 Initial state vector.
P1 Variance matrix of a1. In diffuse case P1star, the non-diffuse part of P1.
P1inf Diffuse part of P1.
dist Distribution of yt.
offset Vector of length n. See details.
nsim Number of simulations.
maxiter Maximum number of iterations used in linearisation.
Details
Function computes log-likelihood of exponential family state-space model. It is very recommendedto use estimates gained from function lik0 as initial values.
logL_p(psi) = logL_g(psi) + logE*_g(w|y),
where logL_g(psi) is log-likelihood of approximate gaussian distribution and logE*_g(w|y) is aMonte-Carlo approximation of E_g[p(y|theta)/g(y|theta) | y]. For details, see J. Durbin and S.J.Koopman (1997).
The general state space model for exponential family is given by
p(y_t|theta_t) = exp[theta’_t * y_t - b_t(theta_t) + f_t(y_t)] (observation equation)
alpha_t+1 = T_t * alpha_t + R_t * eta_t (transition equation)
where theta_t = Z_t * alpha_t and eta_t ~ N(0,Q_t).
Approximating gaussian model is given by
y*_t = Z_t * alpha_t + eps_t (observation equation)
alpha_t+1 = T_t * alpha_t + R_t * eta_t (transition equation)
where eps_t ~ N(0,H*_t) and eta_t ~ N(0,Q_t).
If yt is Poisson distributed, parameter of Poisson distribution is offset*lambda and theta = log(lambda).
If yt is from binomial distribution, offset is a vector specifying number of trials at times 1,...,n, andtheta = log[pi_t/(1-pi_t)], where pi_t is the probability of success at time t.
In case of negative binomial distribution, offset is vector of specified number of successes wantedat times 1,...,n, and theta = log(1-pi_t).
Note that this function works only for univariate observations.
![Page 4: KFAS](https://reader034.fdocuments.us/reader034/viewer/2022051110/55149db84a7959b1478b495d/html5/thumbnails/4.jpg)
4 eflik
Value
List with output from Kalman smoother, when model is approximated with gaussian distributiong(y|theta). Note that Ht is H*_t. List also contains following items:
ytilde y* of approximating gaussian model.
theta Z_t * alpha_t of approximating gaussian model.
likp Value of logL_p.
offset
dist Distribution of yt.
lik Value of log-likelihood function.
References
Durbin J. and Koopman, S.J. (1997). Monte Carlo Maximum Likelihood Estimation for Non-Gaussian State Space Models, Biometrica, Vol. 84, No. 3.Koopman, S.J. and Durbin J. (2001). Time Series Analysis by State Space Methods. Oxford: Ox-ford University Press
Examples
kk <- c(396, 399, 403, 434, 340, 519, 558, 566, 591, 566, 574, 646, 644,646, 665, 693, 773, 834, 910, 1035, 1002, 1161, 1056, 1097, 1094, 1042,1194, 1316, 1246, 1503, 1428, 1477, 1490, 1465, 1560, 1860, 2008, 2020, 2167)
vlk <- c(4623785, 4606307, 4612124, 4639657, 4666081, 4690574, 4711440,4725664, 4738902, 4752528, 4764690, 4779535, 4799964, 4826933, 4855787,4881803, 4902206, 4918154, 4932123, 4946481, 4964371, 4986431, 5013740,5041992, 5066447, 5088333, 5107790, 5124573, 5139835, 5153498, 5165474,5176209, 5188008, 5200598, 5213014, 5228172, 5246096, 5266268, 5288720)
n<-39Zt<-array(c(1,0),c(1,2))Tt<-diag(c(1,1))Tt[1,2]<-1Rt <- diag(2)a1 <- array(0,dim=2)P1 <- diag(0,2)P1inf <-diag(1,2)
likfn<-function(pars,yt,Zt,Tt,Rt,a1,P1,P1inf,dist,offset=1){Qt<-diag(exp(pars))lik<-eflik0(yt,Zt,Tt,Rt,Qt,a1,P1,P1inf,dist,offset)$lik0lik}
opt <- optim(par=c(1,1), yt=array(kk,dim=c(1,39)), Zt=Zt, Tt=Tt, Rt=Rt,
![Page 5: KFAS](https://reader034.fdocuments.us/reader034/viewer/2022051110/55149db84a7959b1478b495d/html5/thumbnails/5.jpg)
eflik0 5
a1=a1, P1=P1, P1inf=P1inf, offset=vlk, dist="Poisson", fn=likfn,method="BFGS", control=list(fnscale=-1,trace=1,REPORT=1))
Qt<-diag(exp(opt$par))out<-eflik0(yt=kk,Zt,Tt,Rt,Qt,a1,P1,P1inf,offset=vlk)out$Qt*10000
likfn2<-function(pars,yt,Zt,Tt,Rt,a1,P1,P1inf,dist,offset=1){Qt<-diag(exp(pars))lik<-eflik(yt,Zt,Tt,Rt,Qt,a1,P1,P1inf,dist,offset,nsim=1000)$likplik}
opt2 <- optim(par=opt$par, yt=array(kk,dim=c(1, 39)), Zt=Zt, Tt=Tt,Rt=Rt, a1=a1, P1=P1, P1inf=P1inf, offset=vlk, dist="Poisson", fn=likfn2,method="BFGS", control=list(fnscale=-1, trace=1, REPORT=1))
Qt<-diag(exp(opt2$par))out<-eflik(yt=kk, Zt, Tt, Rt, Qt, a1, P1, P1inf, offset=vlk)out$Qt*10000
eflik0 Approximate log-likelihood and gaussian density of exponential familystate-space model.
Description
Function expflik computes approximate log-likelihood and approximate gaussian density of uni-variate exponential family state-space model, based on Durbin and Koopman (1997, 2001).
Usage
eflik0(yt, Zt, Tt, Rt, Qt, a1, P1, P1inf, dist=c("Poisson","Binomial", "Negative binomial"), offset=1, maxiter=50)
Arguments
yt Matrix, array or vector of observations. Note that yt is univariate.
Zt System matrix or array of observation equation.
Tt System matrix or array of transition equation.
Rt System matrix or array of transition equation.
Qt Variance matrix or array of disturbance terms eta_t.
a1 Initial state vector.
P1 Variance matrix of a1. In diffuse case P1star, the non-diffuse part of P1.
P1inf Diffuse part of P1.
![Page 6: KFAS](https://reader034.fdocuments.us/reader034/viewer/2022051110/55149db84a7959b1478b495d/html5/thumbnails/6.jpg)
6 eflik0
dist Distribution of yt.offset Vector of length n. See details.maxiter Maximum number of iterations used in linearisation.
Details
Function approximates p(alpha|y) with gaussian g(alpha|y) which has same conditional mode (al-pha,alpha_n+1|y) as p(alpha|y), and computes approximate log-likelihood
logL_0(psi) = logL_g(psi) + logE_g(w_T|y),
where logL_g(psi) is log-likelihood of approximate gaussian distribution and logE_g(w_T|y) is aTaylor-approximation of E_g[p(y|theta)/g(y|theta) | y]. For details, see J. Durbin and S.J. Koopman(1997).
The general state space model for exponential family is given by
p(y_t|theta_t) = exp[theta’_t * y_t - b_t(theta_t) + f_t(y_t)] (observation equation)
alpha_t+1 = T_t * alpha_t + R_t * eta_t (transition equation)
where theta_t = Z_t * alpha_t and eta_t ~ N(0,Q_t).
Approximating gaussian model is given by
y*_t = Z_t * alpha_t + eps_t (observation equation)
alpha_t+1 = T_t * alpha_t + R_t * eta_t (transition equation)
where eps_t ~ N(0,H*_t) and eta_t ~ N(0,Q_t).
If yt is Poisson distributed, parameter of Poisson distribution is offset*lambda and theta = log(lambda).
If yt is from binomial distribution, offset is a vector specifying number of trials at times 1,...,n, andtheta = log[pi_t/(1-pi_t)], where pi_t is the probability of success at time t.
In case of negative binomial distribution, offset is vector of specified number of successes wantedat times 1,...,n, and theta = log(1-pi_t).
Note that this function works only for univariate observations.
Value
List with output from Kalman smoother and distribution smoother, when model is approximatedwith gaussian distribution g(y|theta). Note that Ht is H*_t. List also contains following items:
ytilde y* of approximating gaussian model.theta Z_t * alpha_t of approximating gaussian model.lik0 Value of logL_0.offset
dist Distribution of yt.
References
Durbin J. and Koopman, S.J. (1997). Monte Carlo Maximum Likelihood Estimation for Non-Gaussian State Space Models, Biometrica, Vol. 84, No. 3.Koopman, S.J. and Durbin J. (2001). Time Series Analysis by State Space Methods. Oxford: Ox-ford University Press.
![Page 7: KFAS](https://reader034.fdocuments.us/reader034/viewer/2022051110/55149db84a7959b1478b495d/html5/thumbnails/7.jpg)
efsmoother 7
Examples
kk <- c(396, 399, 403, 434, 340, 519, 558, 566, 591, 566, 574, 646, 644,646, 665, 693, 773, 834, 910,1035, 1002, 1161, 1056, 1097, 1094, 1042,1194, 1316, 1246, 1503, 1428, 1477, 1490, 1465, 1560, 1860, 2008, 2020,2167)
vlk <- c(4623785, 4606307, 4612124, 4639657, 4666081, 4690574, 4711440,4725664, 4738902, 4752528, 4764690, 4779535, 4799964, 4826933, 4855787,4881803, 4902206, 4918154, 4932123, 4946481, 4964371, 4986431, 5013740,5041992, 5066447, 5088333, 5107790, 5124573, 5139835, 5153498, 5165474,5176209, 5188008, 5200598, 5213014, 5228172, 5246096, 5266268, 5288720)
n<-39Zt<-array(c(1,0),c(1,2))Tt<-diag(c(1,1))Tt[1,2]<-1Rt <- diag(2)a1 <- array(0,dim=2)P1 <- diag(0,2)P1inf <-diag(1,2)
likfn<-function(pars,yt,Zt,Tt,Rt,a1,P1,P1inf,dist,offset=1){Qt<-diag(exp(pars))lik<-eflik0(yt,Zt,Tt,Rt,Qt,a1,P1,P1inf,dist,offset)$lik0lik}
opt <- optim(par=c(1, 1), yt=array(kk,dim=c(1, 39)), Zt=Zt, Tt=Tt,Rt=Rt, a1=a1, P1=P1, P1inf=P1inf, offset=vlk, dist="Poisson",fn=likfn, method="BFGS", control=list(fnscale=-1, trace=1, REPORT=1))
Qt<-diag(exp(opt$par))out<-eflik0(yt=kk, Zt, Tt, Rt, Qt, a1, P1, P1inf, offset=vlk)out$Qt*10000
efsmoother Kalman Smoother for exponential family models
Description
Performs Kalman smoothing for exponential family models via simulation.
Usage
efsmoother(out,nsim)
![Page 8: KFAS](https://reader034.fdocuments.us/reader034/viewer/2022051110/55149db84a7959b1478b495d/html5/thumbnails/8.jpg)
8 efsmoother
Arguments
out Output from function ’expflik’.
nsim Number of simulations used for smoothing.
Details
Function efsmoother performs Kalman smoothing of univariate exponential family state spacemodel using the simulation algorithms by Koopman and Durbin (2001, 2002).
Value
A list with the output elements from expflik and following new elements:
nsim Number of simulations used for smoothing.
ahat m*n array of E(alphat | y_1, y_2, ... , y_n).
that m*n array of Zt*ahat.
ahatvar m*m*n array of Var(ahat|y_1, y_2, ... , y_n).
thatvar 1*1*n array of Zt*Var(ahat|y_1, y_2, ... , y_n)*Zt’.
References
Koopman, S.J. and Durbin J. (2001). Time Series Analysis by State Space Methods. Oxford: Ox-ford University Press.
Durbin J. and Koopman, S.J. (2002). A simple and efficient simulation smoother for state spacetime series analysis, Biometrika, Volume 89, Issue 3.
Examples
library(KFAS)
kk <- c(396, 399, 403, 434, 340, 519, 558, 566, 591, 566, 574, 646, 644,646, 665, 693, 773, 834, 910,1035, 1002, 1161, 1056, 1097, 1094, 1042,1194, 1316, 1246, 1503, 1428, 1477, 1490, 1465, 1560, 1860, 2008, 2020,2167)
vlk <- c(4623785, 4606307, 4612124, 4639657, 4666081, 4690574, 4711440,4725664, 4738902, 4752528, 4764690, 4779535, 4799964, 4826933, 4855787,4881803, 4902206, 4918154, 4932123, 4946481, 4964371, 4986431, 5013740,5041992, 5066447, 5088333, 5107790, 5124573, 5139835, 5153498, 5165474,5176209, 5188008, 5200598, 5213014, 5228172, 5246096, 5266268, 5288720)
#Model
n <- 39m <- 2
![Page 9: KFAS](https://reader034.fdocuments.us/reader034/viewer/2022051110/55149db84a7959b1478b495d/html5/thumbnails/9.jpg)
forecast 9
r <- 2
yt <- array(kk,dim=c(1,n))
offset <- array(vlk,dim=c(1,n))
Zt <- array(c(1,0),dim=c(1,m))
Tt <- array(c(1,0,1,1),dim=c(m,m))
Rt <- array(c(1,0,0,1),dim=c(m,r))
psi_hat <- c(-5.356458, -16.348376)
Qt <- array(c(exp(psi_hat[1]), 0, 0, exp(psi_hat[2])), dim=c(r, r))
a1 <- matrix(0,nrow=m,ncol=1)
P1 <- matrix(0,nrow=m,ncol=m)
P1inf<-diag(1,2)
dist <- "Poisson"
eflikout <- eflik0(yt, Zt, Tt, Rt, Qt, a1, P1, P1inf, dist, offset)
nsim <- 1000
smef <- efsmoother(eflikout, nsim)
forecast Forecast state space model
Description
Performs forecasting using output from function ’kf’ (Kalman filter).
Usage
forecast(out, fc=1, Zt.fc=NULL, Tt.fc=NULL, Rt.fc=NULL,Ht.fc=NULL, Qt.fc=NULL)
Arguments
out Output from function ’kf’.
fc Integer which states how many observations is forecasted.
Zt.fc In case where matrix Z is not time-invariant, p*m*fc array of matrix Zt, t=n+1,...,n+fc.
![Page 10: KFAS](https://reader034.fdocuments.us/reader034/viewer/2022051110/55149db84a7959b1478b495d/html5/thumbnails/10.jpg)
10 GlobalTemp
Tt.fc In case where matrix T is not time-invariant, m*m*fc array of matrix Tt, t=n+1,...,n+fc.
Rt.fc In case where matrix R is not time-invariant, m*r*fc array of matrix Rt, t=n+1,...,n+fc.
Ht.fc In case where matrix H is not time-invariant, p*p*fc array of matrix Ht, t=n+1,...,n+fc.
Qt.fc In case where matrix Q is not time-invariant, r*r*fc array of matrix Qt, t=n+1,...,n+fc.
Details
The state space model is given by
y_t = Z_t * alpha_t + eps_t (observation equation)
alpha_t+1 = T_t * alpha_t + R_t * eta_t(transition equation)
where eps_t ~ N(0,H_t) and eta_t ~ N(0,Q_t).
Dimensions of variables are:’yt’ p*n’Zt’ p*m or p*m*n’Tt’ m*m or m*m*n’Rt’ m*r or m*r*n’Ht’ p*p or p*p*n’Qt’ r*r or r*r*n
Value
A list with the following elements:
yt.fc p*fc array of forecasts of observations.
Ft.fc p*p*fc array of mean square error matrix.
at.fc m*(fc+1) array of E(alpha_t | y_1, y_2, ... , y_n).
Pt.fc m*m*(fc+1) array of Var(alpha_t | y_1, y_2, ... , y_n).
GlobalTemp Two series of average global temperature deviations for years 1880-1987
Description
This data set contains two series of average global temperature deviations for years 1880-1987.These series are same as used in Shumway and Stoffer (2006), where they are known as HL andFolland series. For more details, see Shumway and Stoffer (2006, p. 327).
Usage
GlobalTemp
![Page 11: KFAS](https://reader034.fdocuments.us/reader034/viewer/2022051110/55149db84a7959b1478b495d/html5/thumbnails/11.jpg)
kf 11
Format
An 2 times 108 array containing 216 observations.
Source
http://lib.stat.cmu.edu/general/stoffer/tsa2/
References
Shumway, Robert H. and Stoffer, David S. (2006). Time Series Analysis and Its Applications: WithR examples.
kf Kalman Filter With Exact Diffuse Initialisation
Description
Performs Kalman filtering with exact diffuse initialisation using univariate approach (also knownas sequential processing). Written in Fortran, uses subroutines from BLAS and LAPACK. Seefunction ’ks’ for smoothing.
Usage
kf(yt, Zt, Tt, Rt, Ht, Qt, a1, P1, P1inf=0, optcal=c(TRUE,TRUE, TRUE, TRUE), tol=1e-7)
Arguments
yt Matrix or array of observations.
Zt System matrix or array of observation equation.
Tt System matrix or array of transition equation.
Rt System matrix or array of transition equation.
Ht Variance matrix or array of disturbance terms eps_t of observation equation.
Qt Variance matrix or array of disturbance terms eta_t.
a1 Initial state vector.
P1 Variance matrix of a1. In diffuse case P1star, the non-diffuse part of P1.
P1inf Diffuse part of P1. If non-zero, filtering starts with exact diffuse initialisation.
optcal Vector of length 4. Calculate multivariate vt, Ft, Kt, Lt and their diffuse counter-parts Finf, Fstar, Kinf, Kstar, Linf and Lstar. Default is c(TRUE,TRUE,TRUE,TRUE)which calculates all. Note that Kt cannot be calculated without Ft and Lt cannotbe calculated without Kt, so even if optcal=c(TRUE,FALSE,TRUE,TRUE), Ktand Lt are not calculated.
tol Tolerance parameter. Smallest covariance/variance value not counted for zero indiffuse phase. Default is 1e-7.
![Page 12: KFAS](https://reader034.fdocuments.us/reader034/viewer/2022051110/55149db84a7959b1478b495d/html5/thumbnails/12.jpg)
12 kf
Details
Function kf performs Kalman filtering of gaussian multivariate state space model using the uni-variate approach from Koopman and Durbin (2000, 2001). Univariate approach is also known assequential processing, see Anderson and Moore (1979). In case where the distributions of someor all of the elements of initial state vector are not known, kf uses exact diffuse initialisation usingunivariate approach by Koopman and Durbin (2000, 2003). Note that in univariate approach theprediction error variance matrices Ft, Finf and Fstar does not need to be non-singular, as there is nomatrix inversions in univariate approach algorithm. This provides faster and more general filteringthan normal multivariate Kalman filter algorithm.
Filter can deal partially or totally missing observation vectors. If y_t,i is NA, it is interpreted asmissing value, and the dimensions of vtuni and Ftuni (or Fstaruni and Finfuni) are decreased andthe corresponding elements of vt are marked as NA.
The state space model is given by
y_t = Z_t * alpha_t + eps_t (observation equation)
alpha_t+1 = T_t * alpha_t + R_t * eta_t(transition equation)
where eps_t ~ N(0,H_t) and eta_t ~ N(0,Q_t). Note that error terms eps_t and eta_t are assumed tobe uncorrelated.
When P1inf is non-zero, exact diffuse initialisation is used. Matrices Pt, Kt, Ft and Lt are decom-posed to Pinf, Pstar, Kinf, Kstar, Finf, Fstar, Linf and Lstar. Diffuse phase is continued until Pinfbecomes zero-matrix. See Koopman and Durbin (2000, 2001, 2003) for details for exact diffuseand non-diffuse filtering.
Notice that vtuni, Ftuni, Fstaruni, Finfuni, Ktuni, Kinfuni and Kstaruni are usually not the sameas those calculated in usual multivariate Kalman filter. Also the Lt, Linf and Lstar are not cal-culated explicitly. If usual vt, Ft, Finf, Fstar, Kt, Kinf, Kstar, Lt, Linf and Lstar are needed, useoptcal=c(TRUE, TRUE, TRUE, TRUE). When estimating parameters, it is suggested to use opt-cal=c(FALSE,FALSE,FALSE,FALSE) for maximum speed.
Dimensions of variables are:’yt’ p*n’Zt’ p*m or p*m*n’Tt’ m*m or m*m*n’Rt’ m*r or m*r*n’Ht’ p*p or p*p*n’Qt’ r*r or r*r*n’a1’ m*1’P1’ m*m’P1inf’ m*m
where p is dimension of observation vector, m is dimension of state vector and n is number ofobservations.
Value
A list with the following elements:
yt p*n matrix or array of observations.
![Page 13: KFAS](https://reader034.fdocuments.us/reader034/viewer/2022051110/55149db84a7959b1478b495d/html5/thumbnails/13.jpg)
kf 13
ydimt array of length n which has dimensions of observation vector at times t=1,...,n.
tv array of length 4 where tv[i]=0 if i is time-invariant and otherwise tv[i]=1, i isdt, Tt, Rt, Qt.
Zt system matrix or array of observation equation.
Tt system matrix or array of transition equation.
Rt system matrix or array of transition equation.
Ht variance matrix or array of disturbance terms eps_t of observation equation.
Qt variance matrix or array of disturbance terms eta_t.
a1 initial state vector.
P1 variance matrix of a1. In diffuse case P1star, the non-diffuse part of P1 .
at m*(n+1) array of E(alpha_t | y_1, y_2, ... , y_t-1).
Pt m*m*(n+1) array of Var(alpha_t | y_1, y_2, ... , y_t-1).
vtuni p*1*n array of vt of univariate approach.
Ftuni Ft of univariate approach, Var(vtuni).
Ktuni m*p*n array of Kalman gain of univariate approach.
Pinf, Pstar p*p*d+1 arrays of diffuse phase decomposition of Pt.Finfuni, Fstaruni
p*p*d (p*d arrays of diffuse phase decomposition of Ftuni.Kinfuni, Kstaruni
m*p*d arrays of diffuse phase decomposition of Ktuni.
d the last index of diffuse phase, ie. the non-diffuse phase began from time d+1.
j the index of last y_t,i of diffuse phase.
p the dimension of observation vector.
m the dimension of state vector.
r the dimension of variance matrix Qt.
n the number of observations.
lik Value of the log-likelihood function. If NaN, Ftuni_i,t was zero at some t=d+1,...,nor Finfuni_i,t and Fstaruni_i,t was zero at some t=1,...,d.
optcal
info if info[1]=1, could not diagonalize Ht. If info[i]=1, i=2,3,4, Finf, Fstar or Ft wassingular.
vt p*1*n array of vt = yt - Zt * at.
Ft p*p*n array of Ft = Var(vt) of Kalman filter.
Kt m*p*n array of Kalman gain: Kt = Tt * Pt * Zt’ * Ft^-1.
Lt the m*m*n array, Lt = Tt - Kt * Zt.
Finf, Fstar p*p*d arrays of diffuse phase decomposition of Ft.
Kinf, Kstar m*p*d arrays of diffuse phase decomposition of Kt.
Linf, Lstar m*m*d arrays of diffuse phase decomposition of Lt.
tol Tolerance parameter.
![Page 14: KFAS](https://reader034.fdocuments.us/reader034/viewer/2022051110/55149db84a7959b1478b495d/html5/thumbnails/14.jpg)
14 kf
References
Koopman, S.J. and Durbin J. (2000). Fast filtering and smoothing for non-stationary time seriesmodels, Journal of American Statistical Assosiation, 92, 1630-38.
Koopman, S.J. and Durbin J. (2001). Time Series Analysis by State Space Methods. Oxford: Ox-ford University Press.
Koopman, S.J. and Durbin J. (2003). Filtering and smoothing of state vector for diffuse state spacemodels, Journal of Time Series Analysis, Vol. 24, No. 1.
Shumway, Robert H. and Stoffer, David S. (2006). Time Series Analysis and Its Applications: WithR examples.
Examples
library(KFAS)
#Example of local level model#Using the Nile observationsdata(Nile)
yt<-t(data.matrix(Nile))s2_eps<-15099s2_eta<-1469.1
f.out<-kf(yt = yt, Zt = 1, Tt=1, Rt=1, Ht= s2_eps, Qt=s2_eta, a1 =0, P1=1e7)
#a1 and P1 are not really estimated,#should actually use exact diffuse initialisation:
fd.out<-kf(yt = yt, Zt = 1, Tt=1, Rt=1, Ht=s2_eps, Qt=s2_eta, a1 =0, P1=0, P1inf=1)
#No stationary elements, P1=0, P1inf=1
#Plotting observations, non-diffuse and diffuse at, not plotting the a1=0:
ts.plot(Nile, ts(f.out$at[1,2:length(Nile)], start=1872, end=1970),ts(fd.out$at[1,2:length(Nile)], start=1872, end=1970), col=c(1,2,3))
#Looks identical. Actually start of series differs little bit:
f.out$at[1,1:20]fd.out$at[1,1:20]
#Example of multivariate local level model
![Page 15: KFAS](https://reader034.fdocuments.us/reader034/viewer/2022051110/55149db84a7959b1478b495d/html5/thumbnails/15.jpg)
kf 15
#Two series of average global temperature deviations for years 1880-1987#See Shumway and Stoffer (2006), p. 327 for details
data(GlobalTemp)yt<-array(GlobalTemp,c(2,108))
#Estimating the variance parameters
likfn<-function(par, yt, a1, P1, P1inf) #Function to optim{L<-matrix(c(par[1],par[2],0,par[3]),ncol=2)H<-L%*%t(L)q11<-exp(par[4])lik<-kf(yt = yt, Zt = matrix(1,nrow=2), Tt=1, Rt=1, Ht=H, Qt=q11, a1 =a1, P1=P1, P1inf=P1inf, optcal=c(FALSE,FALSE,FALSE,FALSE))lik$lik}
#Diffuse initialisation#Notice that diffuse initialisation without univariate approach does not#work here because Finf is non-singular and non-zero
est<-optim(par=c(.1,0,.1,.1), likfn, method="BFGS",control=list(fnscale=-1), hessian=TRUE, yt=yt, a1=0, P1=0, P1inf=1)
pars<-est$parL<-matrix(c(pars[1],pars[2],0,pars[3]),ncol=2)H<-L%*%t(L)q11<-exp(pars[4])
kfd<-kf(yt = yt, Zt = matrix(1,nrow=2), Tt=1, Rt=1,Ht=H, Qt=q11, a1 = 0, P1=0, P1inf=1)
#Same as non-diffuse, initial values from Shumway and Stoffer (2006):
est<-optim(par=c(.1,0,.1,.1), likfn, method="BFGS",control=list(fnscale=-1), hessian=TRUE, yt=yt, a1=-0.35, P1=0.01, P1inf=0)
pars<-est$parL<-matrix(c(pars[1],pars[2],0,pars[3]),ncol=2)H<-L%*%t(L)q11<-exp(pars[4])
kfnd<-kf(yt = yt, Zt = matrix(1,nrow=2), Tt=1, Rt=1, Ht=H, Qt=q11,a1 = -0.35, P1=0.01, P1inf=0)
kfd$Qtkfnd$Qtkfd$Htkfnd$Ht#Estimated Qt and Ht differs
![Page 16: KFAS](https://reader034.fdocuments.us/reader034/viewer/2022051110/55149db84a7959b1478b495d/html5/thumbnails/16.jpg)
16 ks
ts.plot(ts(yt[1,],start=1880),ts(yt[2,],start=1880),ts(kfd$at[1,],start=1880),ts(kfnd$at[1,],start=1880),col=c(1,2,3,4))#differs at start
#Example of stationary ARMA(1,1)
n<-1000ar1<-0.8ma1<-0.3sigma<-0.5yt<-arima.sim(model=list(ar=ar1,ma=ma1), n=n, sd=sigma)
dt <- matrix(0, nrow = 2)Zt<-matrix(c(1,0),ncol=2)Tt<-matrix(c(ar1,0,1,0),ncol=2)Rt<-matrix(c(1,ma1),ncol=1)a1<-matrix(c(0,0),ncol=1)P1<-matrix(0,ncol=2,nrow=2)P1[1,1]<-1/(1-ar1^2)*(1+ma1^2+2*ar1*ma1)P1[1,2]<-ma1P1[2,1]<-ma1P1[2,2]<-ma1^2f.out<-kf(yt = array(yt,dim=c(1,n)), Zt = Zt, Tt=Tt, Rt=Rt, Ht= 0,Qt=sigma^2, a1 = a1, P1=P1)
ks Kalman Smoother With Exact Diffuse Initialisation
Description
Performs Kalman smoothing with exact diffuse phase using univariate approach. Programmed withFortran, uses subroutines from BLAS and LAPACK. See function ’kf’ for smoothing.
Usage
ks(out)
Arguments
out Output from function ’kf’
Details
Function ks performs Kalman smoothing of gaussian (multivariate) state space model using theunivariate approach by Koopman and Durbin (2000, 2001, 2003). In case where the distributions ofsome or all elements of initial state vector are unknown, ks uses exact diffuse phase using univariateapproach by Koopman and Durbin (2000).
![Page 17: KFAS](https://reader034.fdocuments.us/reader034/viewer/2022051110/55149db84a7959b1478b495d/html5/thumbnails/17.jpg)
ks 17
Value
A list with the output elements from Kalman filter and following new elements:
ahat m*n array of E(alphat | y_1, y_2, ... , y_n).
Vt m*m*n array of Var(alphat|y_1, y_2, ... , y_n).
rt m*n+1 array of weighted sums of innovations vj, j=t+1,...n. Notice that in lit-erature t in rt goes from 0,..n. Here t=1,...n+1. Same applies to all r and Nvariables.
rt0, rt1 m*d+1 arrays of diffuse phase decomposition of rt.
Nt m*m*n+1 array of of Var(rt).Nt0, Nt1, Nt2
m*m*d+1 arrays of diffuse phase decomposition of Nt.
References
Koopman, S.J. and Durbin J. (2000). Fast filtering and smoothing for non-stationary time seriesmodels, Journal of American Statistical Assosiation, 92, 1630-38.
Koopman, S.J. and Durbin J. (2001). Time Series Analysis by State Space Methods. Oxford: Ox-ford University Press.
Koopman, S.J. and Durbin J. (2003). Filtering and smoothing of state vector for diffuse state spacemodels, Journal of Time Series Analysis, Vol. 24, No. 1.
Shumway, Robert H. and Stoffer, David S. (2006). Time Series Analysis and Its Applications: WithR examples.
Examples
library(KFAS)
#Example of multivariate local level model#Two series of average global temperature deviations for years 1880-1987#See Shumway and Stoffer (2006), p. 327 for details
data(GlobalTemp)yt<-array(GlobalTemp,c(2,108))
#Estimating the variance parameters
likfn<-function(par, yt, a1, P1, P1inf) #Function to optim{L<-matrix(c(par[1],par[2],0,par[3]),ncol=2)H<-L%*%t(L)q11<-exp(par[4])lik<-kf(yt = yt, Zt = matrix(1,nrow=2), Tt=1, Rt=1, Ht=H, Qt=q11, a1 =
![Page 18: KFAS](https://reader034.fdocuments.us/reader034/viewer/2022051110/55149db84a7959b1478b495d/html5/thumbnails/18.jpg)
18 simsmoother
a1, P1=P1, P1inf=P1inf, optcal=c(FALSE,FALSE,FALSE,FALSE))lik$lik}
est<-optim(par=c(.1,0,.1,.1), likfn, method="BFGS",control=list(fnscale=-1), hessian=TRUE, yt=yt, a1=0, P1=0, P1inf=1)#Diffuse initialisation
pars<-est$parL<-matrix(c(pars[1],pars[2],0,pars[3]),ncol=2)H<-L%*%t(L)q11<-exp(pars[4])
kfd<-kf(yt = yt, Zt = matrix(1,nrow=2), Tt=1, Rt=1,Ht=H, Qt=q11, a1 = 0, P1=0, P1inf=1)
ksd<-ks(kfd)
#Same as non-diffuse, initial values from Shumway and Stoffer (2006):
est<-optim(par=c(.1,0,.1,.1), likfn, method="BFGS",control=list(fnscale=-1), hessian=TRUE, yt=yt, a1=-0.35, P1=0.01, P1inf=0 )
pars<-est$parL<-matrix(c(pars[1],pars[2],0,pars[3]),ncol=2)H<-L%*%t(L)q11<-exp(pars[4])
kfnd<-kf(yt = yt, Zt = matrix(1,nrow=2), Tt=1, Rt=1, Ht=H, Qt=q11, a1 =-0.35, P1=0.01, P1inf=0)
ksnd<-ks(kfnd)
kfd$Qtkfnd$Qtkfd$Htkfnd$Ht#Estimated Qt and Ht differs
#smoothed values:ts.plot(ts(yt[1,],start=1880),ts(yt[2,],start=1880),ts(ksd$ahat[1,],start=1880),ts(ksnd$ahat[1,],start=1880),col=c(1,2,3,4))
simsmoother Simulation smoother
Description
Generates random samples of state vector alpha from conditional density p(alpha | y).
![Page 19: KFAS](https://reader034.fdocuments.us/reader034/viewer/2022051110/55149db84a7959b1478b495d/html5/thumbnails/19.jpg)
simsmoother 19
Usage
simsmoother(yt, Zt, Tt, Rt, Ht, Qt, a1, P1, P1inf=0, nsim=1, tol=1e-7)
Arguments
yt Matrix or array of observations.
Zt System matrix or array of observation equation.
Tt System matrix or array of transition equation.
Rt System matrix or array of transition equation.
Ht Variance matrix or array of disturbance terms eps_t of observation equation.
Qt Variance matrix or array of disturbance terms eta_t.
a1 Initial state vector.
P1 Variance matrix of a1. In diffuse case P1star, the non-diffuse part of P1.
P1inf Diffuse part of P1.
nsim Number of samples. Default is 1.
tol Tolerance parameter. Smallest covariance/variance value not counted for zero indiffuse phase. Default is 1e-7.
Details
Function simsmoother generates random samples of state vector alpha = (alpha_1, ..., alpha_n) fromconditional density p(alpha | y).
The state space model is given by
y_t = Z_t * alpha_t + eps_t (observation equation)
alpha_t+1 = T_t * alpha_t + R_t * eta_t(transition equation)
where eps_t ~ N(0,H_t) and eta_t ~ N(0,Q_t)
Simulation smoother algorithm is from article by J. Durbin and S.J. Koopman (2002).
Value
m*n*nsim array of simulated state vectors alpha.
References
Durbin J. and Koopman, S.J. (2002). A simple and efficient simulation smoother for state spacetime series analysis, Biometrika, Volume 89, Issue 3
![Page 20: KFAS](https://reader034.fdocuments.us/reader034/viewer/2022051110/55149db84a7959b1478b495d/html5/thumbnails/20.jpg)
20 simsmoother
Examples
library(KFAS)
#Example of multivariate local level model#Two temperature datas from David S. Stoffer's webpage
y1<-scan("http://www.stat.pitt.edu/stoffer/tsa2/data/HL.dat")y2<-scan("http://www.stat.pitt.edu/stoffer/tsa2/data/folland.dat")yt<-rbind(y1,y2)
#Estimating the variance parameters
likfn<-function(par, yt, a1, P1, P1inf) #Function to optim{L<-matrix(c(par[1],par[2],0,par[3]),ncol=2)H<-L%*%t(L)q11<-exp(par[4])lik<-kf(yt = yt, Zt = matrix(1,nrow=2), Tt=1, Rt=1,Ht=H, Qt=q11, a1 = a1, P1=P1, P1inf=P1inf, optcal=c(FALSE,FALSE,FALSE,FALSE))lik$lik}
est<-optim(par=c(.1,0,.1,.1), likfn, method="BFGS", control=list(fnscale=-1),hessian=TRUE, yt=yt, a1=0, P1=0, P1inf=1) #Diffuse initialisation
pars<-est$parL<-matrix(c(pars[1],pars[2],0,pars[3]),ncol=2)H<-L%*%t(L)q11<-exp(pars[4])
kfd<-kf(yt = yt, Zt = matrix(1,nrow=2), Tt=1, Rt=1,Ht=H, Qt=q11, a1 = 0, P1=0, P1inf=1)
ksd<-ks(kfd)
alpha<-simsmoother(yt = yt, Zt = matrix(1,nrow=2), Tt=1, Rt=1,Ht=H, Qt=q11, a1 = 0, P1=0, P1inf=1, nsim=1000)
ahat<-NULLfor(i in 1:108) ahat[i]<-mean(alpha[1,i,])
ts.plot(ts(ahat),ts(ksd$ahat[1,]),col=c(1,2))
![Page 21: KFAS](https://reader034.fdocuments.us/reader034/viewer/2022051110/55149db84a7959b1478b495d/html5/thumbnails/21.jpg)
Index
∗Topic datasetsGlobalTemp, 10
distsmoother, 2
eflik, 2eflik0, 5efsmoother, 7
filter (kf), 11forecast, 9
GlobalTemp, 10
Kalman (kf), 11kf, 11ks, 16
simsmoother, 18
21