Learn R Programming

astsa (version 1.16)

ffbs: Forward Filtering Backward Sampling

Description

FFBS algorithm for state space models

Usage

ffbs(y, A, mu0, Sigma0, Phi, Ups, Gam, sQ, sR, input)

Value

xs

A p-dimensional matrix of sampled states

x0n

The sampled initial state (because R is 1-based)

Arguments

y

Data matrix, vector or time series.

A

Observation matrix. Can be constant or an array with dim=c(q,p,n) if time varying.

mu0

Initial state mean.

Sigma0

Initial state covariance matrix.

Phi

State transition matrix.

Ups

State input matrix; use Ups = 0 if not needed.

Gam

Observation input matrix; use Gam = 0 if not needed.

sQ

State error covariance matrix is Q = sQ%*%t(sQ) -- see details below. In the univariate case, it is the standard deviation.

sR

Observation error covariance matrix is R = sR%*%t(sR) -- see details below. In the univariate case, it is the standard deviation.

input

matrix or vector of inputs having the same row dimension as y; use input = 0 if not needed

Author

D.S. Stoffer

Details

Refer to Section 6.12 of edition 4 text. For a linear state space model, the FFBS algorithm provides a way to sample a state sequence \(x_{0:n}\) from the posterior \(\pi(x_{0:n} \mid \Theta, y_{1:n})\) with parameters \(\Theta\) and data \(y_{1:n}\) as described in Procedure 6.1.

The general model is $$x_t = \Phi x_{t-1} + \Upsilon u_{t} + sQ\, w_t \quad w_t \sim iid\ N(0,I)$$ $$y_t = A_t x_{t-1} + \Gamma u_{t} + sR\, v_t \quad v_t \sim iid\ N(0,I)$$ where \(w_t \perp v_t\). Consequently the state noise covariance matrix is \(Q = sQ\, sQ'\) and the observation noise covariance matrix is \(R = sR\, sR'\) and \(sQ, sR\) do not have to be square as long as everything is conformable.

\(x_t\) is p-dimensional, \(y_t\) is q-dimensional, and \(u_t\) is r-dimensional. Note that \(sQ\, w_t\) has to be p-dimensional, but \(w_t\) does not, and \(sR\, v_t\) has to be q-dimensional, but \(v_t\) does not.

References

You can find demonstrations of astsa capabilities at FUN WITH ASTSA.

The most recent version of the package can be found at https://github.com/nickpoison/astsa/.

In addition, the News and ChangeLog files are at https://github.com/nickpoison/astsa/blob/master/NEWS.md.

The webpages for the texts are https://www.stat.pitt.edu/stoffer/tsa4/ and https://www.stat.pitt.edu/stoffer/tsda/.

Examples

Run this code
if (FALSE) {

## -- this is just one pass - see FUN WITH ASTSA for the real fun --##
# generate some data
 set.seed(1)
 sQ  = 1; sR = 3; n = 100  
 mu0 = 0; Sigma0 = 10; x0 = rnorm(1,mu0,Sigma0)
 w = rnorm(n); v = rnorm(n)
 x = c(x0 + sQ*w[1]);  y = c(x[1] + sR*v[1])   # initialize
for (t in 2:n){
  x[t] = x[t-1] + sQ*w[t]
  y[t] = x[t] + sR*v[t]   
  }
## run one pass of FFBS, plot data, states and sampled states  
run = ffbs(y,A=1,mu0=0,Sigma0=10,Phi=1,Ups=0,Gam=0,sQ=1,sR=3,input=0)
tsplot(cbind(y,run$xs), spaghetti=TRUE, type='o', col=c(8,4), pch=c(1,NA))
legend('topleft', legend=c("y(t)","xs(t)"), lty=1, col=c(8,4), bty="n", pch=c(1,NA))
}

Run the code above in your browser using DataLab