## Create Continuous State-Space Models for Economic Data Analysis

Econometrics Toolbox™ has a variety of Markovian, discrete-time, state-space models that you can use to analyze multivariate economic time series data. Its continuous state-space functionalities are best suited for economic time series modelling and analysis, but you can use them for other applications. If you have a specialized application, consider these implementations in other MathWorks® products:

For discrete state-space functionalities, which are best suited to model regime shifts, such as threshold- or Markov-switching models, see Regime-Switching Models.

### Types of Models

The continuous state-space models in Econometrics Toolbox are dynamic systems that consist of the following types of stochastic equations:

• State equations, xt = ft(xt–1;Θ) — Describe how latent processes transition from times t – 1 to t, for t = 1 through T. The left-hand-side mt-by-1 vector of mt states xt contains unique measurements, lags of other states allowing for lags greater than one on the right-hand side, or constants.

• Observation (measurement) equations, yt = gt(xt;Θ) — Describe how an observer measures the latent process at each time t. The left-hand-side yt is an nt-by-1 vector of nt observations.

The parameters, state variables, or observation variables can be time invariant or time varying. For a time-varying model, the subscript t of ft and gt indicates the possibility of a change during the sample period when:

• A parameter changes to a different parameter in Θ, which accommodates models with a structural change or seasonality.

• The dimensions of xt or yt change, or what they represent changes.

Without the subscript, the model is time invariant—the parameters and variables are the same throughout the sample period.

Regardless of your application, a goal of state-space modeling is to estimate and analyze the latent states xt and model parameters Θ. With Econometrics Toolbox you can model linear and nonlinear (with respect to parameters), Gaussian and non-Gaussian, and frequentist and Bayesian state-space models. The toolbox has these functions for creating an object that represents a state-space model.

FunctionDescriptionState EquationObservation EquationCreation Method
`ssm`Standard state-space model with finite initial state covariancesLinear model and additive Gaussian noiseLinear model and additive Gaussian noiseExplicit and implicit
`dssm`Diffuse state-space model, standard state-space model that has at least one state with an infinite initial varianceLinear model and additive Gaussian noiseLinear model and additive Gaussian noiseExplicit and implicit
`bssm`Bayesian linear state-space modelLinear model and additive Gaussian or Student's t noiseLinear model and additive noise with several distribution options. The supported distributions range in levels of skewness and kurtosis.Only implicit
`bnlssm`Bayesian nonlinear non-Gaussian state-space modelNonlinear model and additive Gaussian noiseCustom distributionOnly implicit

Similar to other Econometrics Toolbox model objects, a state-space model object completely specifies the structure and stochastic properties of the model. The properties (accessible by using dot notation) correspond to the parameter values and model characteristics, either explicitly (where you directly specify coefficient matrices) or implicitly (where you write a function that describes the model and returns model parameters and characteristics). You must supply enough information for MATLAB® to infer the values of structural parameters of the model, such as the state and observation dimensionalities. However, model coefficients and some error distribution parameters are estimable. If you plan to fit the model to data, you do not specify their values when you create a model object.

### Basic Steps

To create a state-space model:

1. Decide on the model structure and assumptions for your problem.

2. If the model is time invariant and linear, express it in matrix form.

3. Choose the creation function for your problem.

4. In MATLAB, create the inputs that specify the state-space model structure and assumptions.

• Explicitly specify the model — If you can express the model in matrix form, create and specify each coefficient matrix. All coefficients are estimable. Set unknown coefficients that you want to fit to data to `NaN`. You can use optional name-value arguments to specify model characteristics, such as initial state distribution moments.

• Implicitly specify the model — Regardless of whether you can explicitly specify the model, you can write a custom function that accepts a vector of state-space model parameters Θ, describes the model structure by mapping the parameters to the coefficient matrices, and returns the matrices and model characteristics. All coefficients are estimable; MATLAB prepares variable elements in the output coefficients for estimation. Bayesian models support only implicit model specification.

• For a Bayesian model, specify the log prior distribution of the state-space model parameters π(Θ). Write a custom function that accepts a vector of state-space model parameters and returns the real scalar log prior distribution at the input parameters.

5. Create the model object representing your state-space model by passing the required structural inputs, and setting any optional inputs, to the appropriate creation function.

### Decide on Model Structure

Before creating a state-space model object in MATLAB, you must decide the model structure and which assumptions apply to your problem. Specifically, you determine the following characteristics:

• The latent variables xt, their dynamic form, and stochastic properties.

• How the latent variables combine to form the observations yt, the stochastic properties of the observations, and whether the observations need to be deflated by exogenous predictors Zt.

• Whether the parametric form of the model varies with time, for example, if a state is introduced midway through sampling or the value of at least one parameter changes midway through sampling due to a structural change.

• In a time-invariant state-space model:

• The coefficient matrices, more generally, model parameters, are constant throughout the sample.

• The number of states, state disturbances, observations, and observation innovations are the same for all periods.

For example, for all t, the following system of equations represents a time-invariant state-space model.

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

• In a time-varying state-space model:

• The coefficient matrices might change from period to period.

• The number of states, state disturbances, observations, and observation innovations might change from period to period (a dimension-varying model). For example, one state drops out of the model or an observation becomes immeasurable during the sampling time frame.

For an example of a time-varying model, suppose, for t = 1,..,10

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

for t = 11

`$\begin{array}{c}{x}_{1,t}=\left[\begin{array}{cc}{\varphi }_{4}& 0\end{array}\right]\left[\begin{array}{c}{x}_{1,t-1}\\ {x}_{2,t-1}\end{array}\right]+0.5{u}_{1,t}\\ {y}_{t}={\varphi }_{5}{x}_{1,t}+0.2{\epsilon }_{t}\end{array},$`

and for t = 12,..,T

`$\begin{array}{c}{x}_{1,t}={\varphi }_{4}+0.5{u}_{1,t}\\ {y}_{t}={\varphi }_{5}{x}_{1,t}+0.2{\epsilon }_{t}\end{array}.$`

Three sets of state transition matrices are in the model, but the model contains only two sets of coefficient matrices.

Deciding on model structure is application dependent and can require separate analysis or iteration. Regardless, a best practice is to write the system so that all parameters are identifiable. A completely identifiable model is particularly important for a non-Bayesian problem.

For the model-creation workflow in this topic, consider the time-invariant, linear, Gaussian standard state-space model

`$\begin{array}{l}{w}_{t}=a+\varphi {w}_{t-1}+b{u}_{t}\\ {y}_{t}={w}_{t}+d{\epsilon }_{t}.\end{array}$`

wt is a latent time series variable, ut and εt are mutually independent, iid standard Gaussian series, and yt is a time series of measurements. All other variables are model parameters.

### Express Model in Matrix Form

Expressing your state-space model in matrix form in MATLAB means that Econometrics Toolbox can operate on it using fewer function calls, which can speed up computations.

When your state-space model is time-invariant, linear, and Gaussian, you can express it in the following matrix form:

`$\begin{array}{l}{x}_{t}=A{x}_{t-1}+B{u}_{t}\\ {y}_{t}=C{x}_{t}+D{\epsilon }_{t}.\end{array}$`

The elements of the matrices A, B, C, and D are known constants or unknown state-space model parameters in Θ.

To write a model in matrix form, first group the parameters:

• ΘA — Factors of xt–1, any constants in the state equation, and elements that facilitate moving average terms (lags of ut)

• ΘB — Factors of ut in the state equation

• ΘC — Factors of xt

• ΘD — Factors of εt in the observation equation

Consider the example system

`$\begin{array}{l}{w}_{t}=a+\varphi {w}_{t-1}+b{u}_{t}\\ {y}_{t}={w}_{t}+d{\epsilon }_{t}.\end{array}$`

In the system,

• Θ = {a,ϕ,b,c,d}.

• ΘA = {a,ϕ}.

• ΘB = {b}.

• ΘC = {c}.

• ΘD = {d}.

Next, form the state equation coefficient matrices A and B. A is a linear map from xt–1 to E(xt|xt–1), with vectors xt and xt–1 consisting of the state variables of interest and abstract variables that facilitate other linear terms associated with ΘA. Specifically, common elements of xt include:

• State variables of interest.

• State variables that equal 1 for all t. Associate this state with constant parameters in the state equation.

• Placeholders that facilitate including state or state disturbance variables with lags greater than 1.

Typically, B is the square root of the covariance of the state disturbances, with the vector ut containing only state disturbance variables.

In the example system, xt = [wt vt]′, where vt = 1 for all t, is a placeholder associated with a. Although two states exist, vt has no associated disturbance. Therefore, ut is univariate and appears only in the equation of ft.

Given the options for elements of xt and ut, write A and B as matrices that map xt–1 and ut to xt. In the example system,

`$A=\left[\begin{array}{cc}\varphi & a\\ 0& 1\end{array}\right]$`

and

`$B=\left[\begin{array}{c}b\\ 0\end{array}\right].$`

Rewriting the state equation in matrix form,

`$\begin{array}{c}\left[\begin{array}{c}{w}_{t}\\ {v}_{t}\end{array}\right]=\left[\begin{array}{cc}\varphi & a\\ 0& 1\end{array}\right]\left[\begin{array}{c}{w}_{t-1}\\ {v}_{t-1}\end{array}\right]+\left[\begin{array}{c}b\\ 0\end{array}\right]{u}_{t},\\ =\begin{array}{l}\varphi {w}_{t-1}+a{v}_{t-1}+b{u}_{t}\hfill \\ {v}_{t-1}\hfill \end{array}.\end{array}$`

The second state equation, vt = vt–1, facilitates the creation of the constant term in the equation of wt. When you create the model in MATLAB, you can specify characteristics of each state, such as which ones are stationary or equal one.

Consider a state that includes lags of state disturbances (moving average terms):

`${w}_{t}=a+\varphi {w}_{t-1}+\theta {u}_{t-1}+{u}_{t}.$`

The expanded state equation in matrix form for this example is

`$\left[\begin{array}{c}{w}_{t}\\ {v}_{1,t}\\ {v}_{2,t}\end{array}\right]=\left[\begin{array}{ccc}\varphi & a& \theta \\ 0& 1& 0\\ 0& 0& 0\end{array}\right]\left[\begin{array}{c}{w}_{t-1}\\ {v}_{1,t-1}\\ {v}_{2,t-1}\end{array}\right]+\left[\begin{array}{c}b\\ 0\\ 1\end{array}\right]{u}_{t}.$`

The placeholder variable v2,t = ut and the third term in the equation of wt is θv2,t–1 = θut–1.

After writing the state equation in matrix form, you write the observation equation in matrix form. The forms that C and D take are usually more straightforward than A and B because A and B specify the dynamics of the system, which can be more complex than how the system is measured or observed. Elements of C determine how the states xt linearly combine to form possibly perturbed observations yt, and elements of D specify the scale of the perturbations.

In the example system, the expanded observation equation is

`${y}_{t}=\left[\begin{array}{cc}c& 0\end{array}\right]\left[\begin{array}{c}{w}_{t}\\ {v}_{t}\end{array}\right]+d{\epsilon }_{t}.$`

### Choose Appropriate Model

The structure of your state-space model and your assumptions suggest which creation function to use.

#### Standard State-Space Model

A standard state-space model, or a linear, Gaussian state-space model, is linear with respect to the model parameters Θt and it has additive, Gaussian state and measurement errors. The `ssm` function creates a standard state-space model object, `ssm`.

Symbolically, a standard state-space model, in its most general form, is the following system of equations. The system is time varying. For t = 1,...,T:

`$\begin{array}{l}{x}_{t}={A}_{t}{x}_{t-1}+{B}_{t}{u}_{t}\\ {y}_{t}-{\left({Z}_{t}\beta \right)}^{\prime }={C}_{t}{x}_{t}+{D}_{t}{\epsilon }_{t},\end{array}$`

where:

• ${x}_{t}=\left[{x}_{1,t},...,{x}_{{m}_{t},t}\right]\prime$ is an mt-dimensional state vector describing the dynamics of some, possibly unobservable, phenomenon at period t. The initial state distribution (x0) is Gaussian with mean μ0 and covariance matrix Σ0. Elements of xt can represent lags of other elements or constants.

• ${y}_{t}=\left[{y}_{1,t},...,{y}_{{n}_{t},t}\right]\prime$ is an nt-dimensional observation vector describing how the states are measured by observers at period t.

• At is the mt-by-mt – 1 state-transition matrix describing how the states at time t transition to the states at period t – 1.

• Bt is the mt-by-kt state-disturbance-loading matrix describing how the states at period t combine with the innovations at period t, with elements specifying the intensity of the noise.

• Ct is the nt-by-mt measurement-sensitivity matrix describing how the observations at period t relate to the states at period t.

• Dt is the nt-by-ht observation-innovation matrix describing how the observations at period t combine with the observation errors at period t, with elements specifying the intensity of the noise. If the combination of the states is observed without error, Dt = 0 for all t.

• The matrices At, Bt, Ct, and Dt are coefficient matrices. Let θJ be the set of all parameters in coefficient matrix Jt, Jt = {At,Bt,Ct,Dt} and t = 1,…,T. Θ = {θA, θB, θC, θD} is the collection of all parameters in the coefficient matrices. The matrices are estimable, that is, they can contain unknown parameters for fitting to data. Also, you can specify values for some or all of the elements for experimentation or constrained estimation. The subscript t indicates that the coefficient matrices are time varying; you can omit the subscript for simpler time-invariant coefficients or models.

• ${u}_{t}=\left[{u}_{1,t},...,{u}_{{k}_{t},t}\right]\prime$ is a kt-dimensional, Gaussian, white-noise, unit-variance vector of state disturbances at period t.

• ${\epsilon }_{t}=\left[{\epsilon }_{1,t},...,{\epsilon }_{{h}_{t},t}\right]\prime$ is an ht-dimensional, Gaussian, white-noise, unit-variance vector of observation innovations at period t.

• εt and ut are uncorrelated.

• For time-invariant models,

• ${Z}_{t}=\left[\begin{array}{cccc}{z}_{t1}& {z}_{t2}& \cdots & {z}_{td}\end{array}\right]$ is row t of a T-by-d matrix of predictors Z. Each column of Z corresponds to a predictor, and each successive row to a successive period. If the observations are multivariate, all predictors deflate each observation.

• β is a d-by-n matrix of regression coefficients for Zt.

• Default initial state moments are as follows. You can adjust the defaults by setting the `Mean0` and `Cov0` properties when you create the model or by using dot notation.

• For stationary states (`StateType` property is `0`), the initial means, variances, and covariances are finite, and the software infers them.

• For states that are the constant one for all periods (`StateType` property is `1`), the initial state means are 1 and covariances are 0.

• For nonstationary states (`StateType` property is `2`), the initial state means are 0 and initial state variance is `1e7`.

Not expressed in the system are the m1-by-1 initial state mean vector μ0 and the m1-by-m1 initial state covariance matrix Σ0. Although they are estimable for non-Bayesian models, typically, each are set to known constants (either by you or by defaults).

#### Diffuse State-Space Model

A diffuse state-space model is a standard state-space model that contains at least one state with an infinite initial variance (`Inf`), called a diffuse state. In addition to having an infinite initial variance, all diffuse states are uncorrelated with all other states in the model. The `dssm` function creates a diffuse state-space model object, `dssm`.

You can use a diffuse state-space model when:

• Starting points of some nonstationary systems, such as random walk process, lead to initial distribution variances that approach infinity.

• An infinite variance specification for an initial state distribution indicates complete ignorance, or no prior knowledge, of the diffuse states. The advantage of this specification is that the analysis of these states is more objective. In other words, the observations, rather than additional distribution assumptions, aid in understanding the diffuse states. The disadvantage is that posterior distributions of the states might be improper, and the likelihood function is unbounded. However, with enough data and an identifiable, Gaussian state-space model, the filtered and smoothed states, and a likelihood based on them, can be computed using the diffuse Kalman filter.

• You want to represent a static, initial state as an unknown parameter by attributing to it an infinite variance.

#### Bayesian Linear State-Space Model

A Bayesian linear state-space model is a Bayesian view of the standard state-space model in which the vector of model parameters Θ are treated as random variables with a joint prior distribution Π(Θ) and a posterior distribution Π(Θ|yt) composed of the joint prior and data likelihood computed by the standard Kalman filter. The `bssm` function creates a Bayesian linear state-space model object, `bssm`.

For filtering, smoothing, and parameter estimation, frequentist analyses rely on closed-form expectations and maximum likelihood estimation. Bayesian analyses typically require numerical integration by Markov chain Monte Carlo (MCMC) sampling. As a result, Bayesian models can be richer, allowing for more complexity, than standard frequentist models. Standard and Bayesian linear state-space models have these notable differences:

DifferenceStandard State-Space ModelBayesian State-Space Model
State disturbance series ut distribution optionsOnly multivariate GaussianMultivariate Gaussian or Student's t
Observation innovation series εt distribution optionsOnly multivariate Gaussian

Linear observation innovation series εt can follow one of these distributions, specified by the `ObservationDistribution` name-value argument:

• `"Gaussian"` — Multivariate Gaussian

• `"t"` — Multivariate Student's t

• `"mixture"` — Univariate finite Gaussian mixture

• `"Laplace"` — Univariate Laplace

• `"skewnormal"` — Univariate skew normal

Θ
• Vector of state-space model parameters treated as fixed and possibly unknown

• Estimated by maximum likelihood

• Vector of state-space model parameters treated as a random vector with a prior distribution specified as a custom function

• Posterior distribution approximated by the Metropolis-Hasting MCMC sampler

Model specificationExplicit or implicitImplicit only

#### Bayesian Nonlinear Non-Gaussian State-Space Model

Like a Bayesian linear state-space model, a Bayesian nonlinear non-Gaussian state-space model, or simply a Bayesian nonlinear model, takes a Bayesian view of the problem, but the state dynamical behavior is nonlinear and the observations follow a custom distribution. This form enables the model to capture complex nuances in the data or more closely match the sampling scheme. The `bnlssm` function creates a Bayesian nonlinear state-space model object, `bnlssm`.

In general, a linear, multivariate, time-varying, Gaussian state-space model can be one of these systems of equation, where At through Dt are possibly nonlinear functions:

• Equation form — Observations have linear Gaussian error, explicitly

`$\begin{array}{l}{x}_{t}={A}_{t}\left({x}_{t-1};{\theta }_{A}\right)+{B}_{t}\left({\theta }_{B}\right){u}_{t}\\ {y}_{t}={C}_{t}\left({x}_{t};{\theta }_{C}\right)+{D}_{t}\left({\theta }_{D}\right){\epsilon }_{t}.\end{array}$`

• Distribution form — Observations follow a custom distribution p(yt|xt;Θ,Zt), explicitly

`$\begin{array}{l}{x}_{t}={A}_{t}\left({x}_{t-1};\Theta \right)+{B}_{t}\left(\Theta \right){u}_{t}\\ {y}_{t}~\mathrm{log}p\left({y}_{t}|{x}_{t};\Theta ,{Z}_{t}\right).\end{array}$`

Bayesian linear and nonlinear state-space models have these notable differences:

DifferenceBayesian Linear State-Space ModelBayesian Nonlinear State-Space Models
Mappings At and Ct Linear, specified as coefficient matrices
• For linear dynamics, specified as coefficient matrices or cell vectors of coefficient matrices

• For nonlinear dynamics, a function handle to a function that describes the nonlinear dynamics

State disturbance series ut distribution optionsMultivariate Gaussian or Student's tOnly multivariate Gaussian
Observation distribution options

Linear observation innovation series εt can follow one of these distributions, specified by the `ObservationDistribution` name-value argument:

• `"Gaussian"` — Multivariate Gaussian

• `"t"` — Multivariate Student's t

• `"mixture"` — Univariate finite Gaussian mixture

• `"Laplace"` — Univariate Laplace

• `"skewnormal"` — Univariate skew normal

The `ObservationForm` name-value argument specifies the form that the observation equation takes:

• `"equation"` (default) — Observations have linear Gaussian innovations, with measurement-sensitivity coefficient matrix or cell vectors of coefficient matrices.

• `"distribution"` — Observations have a custom distribution.

### Create Structural Inputs and Model Object

After you choose a state-space model for your problem, create the inputs that specify its structure.

#### Create Standard State-Space Model

To represent a standard state-space model in MATLAB by using the `ssm` function, consider creating the model

`$\begin{array}{l}{w}_{t}=a+\varphi {w}_{t-1}+b{u}_{t}\\ {y}_{t}={w}_{t}+d{\epsilon }_{t}.\end{array}$`

The model is partially specified, which means its structure is completely specified, but some parameters are unknown. (A fully specified model contains no unknown parameters.) You can express the model in matrix form

`$\begin{array}{l}\left[\begin{array}{c}{w}_{t}\\ {v}_{t}\end{array}\right]=\left[\begin{array}{cc}\varphi & a\\ 0& 1\end{array}\right]\left[\begin{array}{c}{w}_{t-1}\\ {v}_{t-1}\end{array}\right]+\left[\begin{array}{c}b\\ 0\end{array}\right]{u}_{t}\\ {y}_{t}=\left[\begin{array}{cc}1& 0\end{array}\right]\left[\begin{array}{c}{w}_{t}\\ {v}_{t}\end{array}\right]+d{\epsilon }_{t}.\end{array}$`

Create a matrix for each coefficient matrix. Set elements that represent unknown parameters to `NaN`.

```A = [NaN NaN; 0 1]; B = [NaN; 0]; C = [1 0]; D = NaN;```

Pass the coefficient matrices to `ssm`. Specify that the first state variable is stationary (type `0`) and the second state variable is the constant 1 (type `1`).

```stateType = [0; 1]; Mdl = ssm(A,B,C,D,StateType=stateType) ```
```Mdl = State-space model type: ssm State vector length: 2 Observation vector length: 1 State disturbance vector length: 1 Observation innovation vector length: 1 Sample size supported by model: Unlimited Unknown parameters for estimation: 4 State variables: x1, x2,... State disturbances: u1, u2,... Observation series: y1, y2,... Observation innovations: e1, e2,... Unknown parameters: c1, c2,... State equations: x1(t) = (c1)x1(t-1) + (c2)x2(t-1) + (c3)u1(t) x2(t) = x2(t-1) Observation equation: y1(t) = x1(t) + (c4)e1(t) Initial state distribution: Initial state means are not specified. Initial state covariance matrix is not specified. State types x1 x2 Stationary Constant ```

The output describes the state-space model, including the model form, which you can use to verify whether you have specified the model correctly. The variables `cn`, where `n` is a natural number, represent unknown, estimable parameters.

Alternatively, you can specify the structure of a state-space model by using a custom parameter-to-matrix mapping function, regardless of whether the model can be expressed in matrix form. Using a custom function, you can create more complex models or reparameterize the model to enforce restrictions on the parameters. The function requires an input vector of parameters Θ with other optional inputs and outputs the coefficient matrices and other model characteristics. For more details, see `ssm`.

Create the following parameter-to-matrix mapping function, which specifies the structure of the example state-space model, and save it to your working directory or create it within a script. Restrict the error scales to be positive.

``` function [A,B,C,D,Mean0,Cov0,StateType] = exampleStandardSSM(theta) phi = theta(1); a = theta(2); varU = exp(theta(3)); b = sqrt(varU); varEPS = exp(theta(4)); d = sqrt(varEPS); A = [phi a; 0 1]; B = [b; 0]; C = [1 0]; D = d; Mean0 = []; % Defer to default Cov0 = []; % Defer to default StateType = [0; 1]; end```

Create the state-space model represented in the function by passing `exampleStandardSSM` as a function handle to `ssm`.

`Mdl = ssm(@exampleStandardSSM)`
```Mdl = State-space model type: ssm The state-space model is implicitly defined by the function: @exampleStandardSSM```
At the time you create an implicitly specified model, MATLAB cannot determine its structure.

#### Create Diffuse State-Space Model

To represent a diffuse state-space model in MATLAB by using the `dssm` function, consider creating this fully specified and observed random walk with drift model

`$\begin{array}{l}{w}_{t}=2+{w}_{t-1}+0.5{u}_{t}\\ {y}_{t}={w}_{t}.\end{array}$`

Suppose Var(w0) is infinite. You can express the model in matrix form

`$\begin{array}{l}\left[\begin{array}{c}{w}_{t}\\ {v}_{t}\end{array}\right]=\left[\begin{array}{cc}1& 2\\ 0& 1\end{array}\right]\left[\begin{array}{c}{w}_{t-1}\\ {v}_{t-1}\end{array}\right]+\left[\begin{array}{c}0.5\\ 0\end{array}\right]{u}_{t}\\ {y}_{t}=\left[\begin{array}{cc}1& 0\end{array}\right]\left[\begin{array}{c}{w}_{t}\\ {v}_{t}\end{array}\right].\end{array}$`

Create a matrix for each coefficient matrix.

```A = [1 2; 0 1]; B = [0.5; 0]; C = [1 0]; D = 0;```

Pass the coefficient matrices to `dssm`. Specify that the first state variable is stationary (type `0`) and the second state variable is the constant 1 (type `1`).

```stateType = [0; 1]; Mdl = dssm(A,B,C,D,StateType=stateType) ```
```Mdl = State-space model type: dssm State vector length: 2 Observation vector length: 1 State disturbance vector length: 1 Observation innovation vector length: 1 Sample size supported by model: Unlimited State variables: x1, x2,... State disturbances: u1, u2,... Observation series: y1, y2,... Observation innovations: e1, e2,... State equations: x1(t) = x1(t-1) + (2)x2(t-1) + (0.50)u1(t) x2(t) = x2(t-1) Observation equation: y1(t) = x1(t) Initial state distribution: Initial state means x1 x2 0 1 Initial state covariance matrix x1 x2 x1 Inf 0 x2 0 0 State types x1 x2 Stationary Constant ```

The output indicates that all parameters are known and the initial variance of wt is infinite.

#### Create Bayesian Linear State-Space Model

To represent a Bayesian linear state-space model in MATLAB by using the `bssm` function, consider creating the model

`$\begin{array}{l}{w}_{t}=a+\varphi {w}_{t-1}+b{u}_{t}\\ {y}_{t}={w}_{t}+d{\epsilon }_{t}.\end{array}$`

Despite the fact that you can write the model in matrix form, you must implicitly specify a Bayesian model by describing its structure in a custom parameter-to-matrix mapping function.

Create the following parameter-to-matrix mapping function, which specifies the structure of the example state-space model, and save it to your working directory or write the function in your script. For details on how to write a parameter-to-matrix mapping function, see `bssm`.

``` function [A,B,C,D,Mean0,Cov0,StateType] = exampleBayesianSSM(theta) phi = theta(1); a = theta(2); b = theta(3); d = theta(4); A = [phi a; 0 1]; B = [b; 0]; C = [1 0]; D = d; Mean0 = []; % Defer to default Cov0 = []; % Defer to default StateType = [0; 1]; end```
Although you can write the function `exampleBayesianSSM` to impose constraints on parameters, you can impose constraints in the log prior distribution function more easily.

Assuming the prior distribution π(Θ) is flat, create the log prior distribution function representing log π(Θ). Include the following parameter constraints by setting the probability outside the boundary to 0 (or `–Inf` for the log probability):

• ϕ is in the unit circle (that is, enforce a stationary AR(1) state).

• Error standard deviations b > 0 and d > 0.

```function logprior = exampleFlatPriorBSSM(theta) phigte1 = theta(1) >= 1; nonposb = theta(3) <= 0; nonposd = theta(4) <= 0; paramconstraints = [phigte1 nonposb nonposd]; if sum(paramconstraints) > 0 logprior = -Inf; else logprior = 0; % Prior density is proportional to 1 % for all values in the parameter space. end end```

Create the Bayesian linear state-space model represented in the functions by passing the functions as function handles to `bssm`.

`Mdl = bssm(@exampleBayesianSSM,@exampleFlatPriorBSSM)`
```Mdl = Mapping that defines a state-space model: @exampleBayesianSSM Log density of parameter prior distribution: @exampleFlatPriorBSSM```

#### Create Bayesian Nonlinear Non-Gaussian State-Space Model

To represent a Bayesian nonlinear, non-Gaussian state-space model in MATLAB by using the `bnlssm` function, consider a state-space model that has these characteristics:

• The state equation has two independent, nonstationary states.

• The states combine to form the probability of success for a series of Bernoulli observations.

• The two states contain white Gaussian noise.

• The prior distribution of the parameters is flat.

Symbolically, the system of equations, in distribution form, is

`$\begin{array}{l}\left[\begin{array}{c}{x}_{1,t}\\ {x}_{2,t}\end{array}\right]=\left[\begin{array}{cc}{\theta }_{1}& 0\\ 0& {\theta }_{2}\end{array}\right]\left[\begin{array}{c}{x}_{1,t-1}\\ {x}_{2,t-1}\end{array}\right]+\left[\begin{array}{cc}{\theta }_{3}& 0\\ 0& {\theta }_{4}\end{array}\right]\left[\begin{array}{c}{u}_{1,t}\\ {u}_{2,t}\end{array}\right]\\ {y}_{t}~p\left({y}_{t};{x}_{t}\right)=\frac{\mathrm{exp}\left({x}_{1,t}\right)}{\mathrm{exp}\left({x}_{1,t}\right)+\mathrm{exp}\left({x}_{2,t}\right)}.\end{array}$`

Implicitly specify the model by describing its structure in a custom parameter-to-matrix mapping function. Save the function to your working directory or write the function in your script. Because the equation is in distribution form, return the log of the joint observation density log p(yt;xt). For details on how to write a parameter-to-matrix mapping function, see `bnlssm`.

```function [A,B,LogY,Mean0,Cov0,StateType] = exampleBayesianNLSSM(theta) A = [theta(1) 0; 0 theta(2)]; B = [theta(3) 0; 0 theta(4)]; LogY = @(y,x)sum(log(binopdf(y,1,exp(x(1))/(exp(x(2))+exp(x(1)))))); Mean0 = [0.5; 0.5]; Cov0 = eye(2); StateType = [2; 2]; % Nonstationary state processes end```

Assuming the prior distribution π(Θ) is flat, create the log prior distribution function representing log π(Θ). Constrain the error standard deviations θ3 > 0 and θ4 > 0 by setting the prior probability outside the boundary to 0 (or `–Inf` for the log probability).

```function logprior = exampleFlatPriorBNLSSM(theta) paramconstraints = (theta(3:4) <= 0); if(sum(paramconstraints)) logprior = -Inf; else logprior = 0; % Prior density is proportional to 1 % for all values in the parameter space. end end```

Create the Bayesian nonlinear non-Gaussian state-space model represented in the functions by passing the functions as function handles to `bnlssm`. Specify that the observation equation is in distribution form.

```Mdl = bnlssm(@exampleBayesianNLSSM,@exampleFlatPriorBNLSSM, ... ObservationForm="distribution")```
```Mdl = bnlssm with properties: ParamMap: @exampleBayesianNLSSM ParamDistribution: @exampleFlatPriorBNLSSM ObservationForm: "distribution" Multipoint: [1×0 string]```

## References

[1] Durbin J., and S. J. Koopman. Time Series Analysis by State Space Methods. 2nd ed. Oxford: Oxford University Press, 2012.