Accelerating the pace of engineering and science

# Documentation

## Implicitly Specify a Time-Varying State-Space Model

This example shows how to specify a time-varying state-space model by passing a parameter-mapping function describing the model to ssm (i.e., implicitly specify a state-space model). Suppose that from periods 1 to 10, the state model are stationary AR(2) and MA(1) models, respectively, and the observation model is the sum of the two states. From periods 11 to 20, the state model only includes the first AR(2) model.

Create the function timeVariantParamMap.m, which specifies how the parameters in params map to the state-space model matrices, the initial state values, and the type of state. Symbolically, the models are:

• For periods 1 to 10,

$\begin{array}{c}\left[\begin{array}{c}{x}_{1t}\\ {x}_{2t}\\ {x}_{3t}\\ {x}_{4t}\end{array}\right]=\left[\begin{array}{cccc}{\varphi }_{1}& {\varphi }_{2}& 0& 0\\ 1& 0& 0& 0\\ 0& 0& 0& \theta \\ 0& 0& 0& 0\end{array}\right]\left[\begin{array}{c}{x}_{1,t-1}\\ {x}_{2,t-1}\\ {x}_{3,t-1}\\ {x}_{4,t-1}\end{array}\right]+\left[\begin{array}{cc}{\sigma }_{1}& 0\\ 0& 0\\ 0& 1\\ 0& 1\end{array}\right]\left[\begin{array}{c}{u}_{1t}\\ {u}_{2t}\end{array}\right]\\ {y}_{t}={a}_{1}\left({x}_{1t}+{x}_{3t}\right)+{\sigma }_{2}{\epsilon }_{t}.\end{array}$

• For period 11,

$\begin{array}{c}\left[\begin{array}{c}{x}_{1t}\\ {x}_{2t}\end{array}\right]=\left[\begin{array}{cccc}{\varphi }_{1}& {\varphi }_{2}& 0& 0\\ 1& 0& 0& 0\end{array}\right]\left[\begin{array}{c}{x}_{1,t-1}\\ {x}_{2,t-1}\\ {x}_{3,t-1}\\ {x}_{4,t-1}\end{array}\right]+\left[\begin{array}{c}{\sigma }_{1}\\ 0\end{array}\right]{u}_{1t}\\ {y}_{t}={a}_{2}{x}_{1t}+{\sigma }_{3}{\epsilon }_{t}.\end{array}$

• For periods 12 to 20,

$\begin{array}{c}\left[\begin{array}{c}{x}_{1t}\\ {x}_{2t}\end{array}\right]=\left[\begin{array}{cc}{\varphi }_{1}& {\varphi }_{2}\\ 1& 0\end{array}\right]\left[\begin{array}{c}{x}_{1,t-1}\\ {x}_{2,t-1}\end{array}\right]+\left[\begin{array}{c}{\sigma }_{1}\\ 0\end{array}\right]{u}_{1t}\\ {y}_{t}={a}_{2}{x}_{1t}+{\sigma }_{3}{\epsilon }_{t}.\end{array}$

```function [A,B,C,D,Mean0,Cov0,StateType] = timeVariantParamMap(params)
% Time-variant state-space model parameter mapping function example. This
% function maps the vector params to the state-space matrices (A, B, C, and
% D), the initial state value and the initial state variance (Mean0 and
% Cov0), and the type of state (StateType). From periods 1 through 10, the
% state model is an AR(2) and an MA(1) model, and the observation model is
% the sum of the two states. From periods 11 through 20, the state model is
% just the AR(2) model.
varu11 = exp(params(3));  % Positive variance constraints
vare11 = exp(params(7));
vare12 = exp(params(9));
A1 = {[params(1) params(2) 0 0; 1 0 0 0; 0 0 0 params(5); 0 0 0 0]};
B1 = {[sqrt(varu11) 0; 0 0; 0 1; 0 1]};
C1 = {params(6)*[1 0 1 0]};
D1 = {sqrt(vare11)};
Mean0 = [0.5 0.5 0 0];
Cov0 = eye(4);
StateType = [0 0 0 0];
A2 = {[params(1) params(2) 0 0; 1 0 0 0]};
B2 = {[sqrt(varu11); 0]};
A3 = {[params(1) params(2); 1 0]};
B3 = {[sqrt(varu11); 0]};
C3 = {params(8)*[1 0]};
D3 = {sqrt(vare12)};
A = [repmat(A1,10,1);A2;repmat(A3,9,1)];
B = [repmat(B1,10,1);B2;repmat(B3,9,1)];
C = [repmat(C1,10,1);repmat(C3,10,1)];
D = [repmat(D1,10,1);repmat(D3,10,1)];
end

```

Specify the state-space model by passing the function timeVariantParamMap as a function handle to ssm.

`Mdl = ssm(@timeVariantParamMap)`
```Mdl =

The state space model is implicitly defined by the function:
@timeVariantParamMap```

The software implicitly defines the state-space model. Usually, you cannot verify state-space models that you implicitly define.

Pass Mdl and response data to estimate to estimate the model parameters.