Main Content

ssm class

Superclasses:

Create state-space model

Description

ssmcreates a standard, linear,state-space modelobject with independent Gaussian state disturbances and observation innovations.

You can:

  • Specify atime-invariantortime-varyingmodel.

  • Specify whether states are stationary,static, or nonstationary.

  • Specify the state-transition, state-disturbance-loading, measurement-sensitivity, or observation-innovation matrices:

    • Explicitly by providing the matrices

    • Implicitly by providing a function that maps the parameters to the matrices, that is, a parameter-to-matrix mapping function

Once you have specified a model:

  • If it contains unknown parameters, then pass the model and data toestimate, which estimates the parameters.

  • If the state and observation matrices do not contain unknown parameters (for example, an estimatedssmmodel), then you can pass it to:

    • filterto implement forward recursion and obtain filtered estimates

    • forecastto obtain forecasted states and observations

    • smoothto implement backward recursion and obtain smoothed estimates

    • simulateto simulate states and observations from the state-space model

  • ssmsupports regression of exogenous predictors. To include a regression component that deflates the observations, seeestimate,filter,forecast, andsmooth.

Construction

Mdl= ssm(A,B,C)creates astate-space model(Mdl) using state-transition matrixA, state-disturbance-loading matrixB, and measurement-sensitivity matrixC.

Mdl= ssm(A,B,C,D)creates a state-space model using state-transition matrixA, state-disturbance-loading matrixB, measurement-sensitivity matrixC, and observation-innovation matrixD.

Mdl= ssm(___,Name,Value)uses any of the input arguments in the previous syntaxes and additional options that you specify by one or moreName,Valuepair arguments.

Namecan also be a property name andValueis the corresponding value.Namemust appear inside single quotes (''). You can specify several name-value pair arguments in any order asName1,Value1,...,NameN,ValueN.

Mdl= ssm(ParamMap)creates a state-space model using a parameter-to-matrix mapping function (ParamMap) that you write. The function maps a vector of parameters to the matricesA,B, andC. Optionally,ParamMapcan map parameters toD,Mean0, orCov0. To specify the types of states, the function can returnStateType. To accommodate a regression component in the observation equation,ParamMapcan also return deflated observation data.

Mdl= ssm(DSSMMdl)converts a diffuse state-space model object (DSSMMdl) to a state-space model object (Mdl).ssmsets all initial variances of diffuse states inSSMMdl.Cov0to1e07.

Input Arguments

expand all

State-transition coefficient matrix for explicit state-space model creation, specified as a matrix or cell vector of matrices.

The state-transition coefficient matrix,At, specifies how the states,xt, are expected to transition from periodt– 1 tot, for allt= 1,...,T. That is, the expected state-transition equation at periodtisE(xt|xt–1) =Atxt–1.

For time-invariant state-space models, specifyAas anm-by-mmatrix, wheremis the number of states per period.

For time-varying state-space models, specifyAas aT-dimensional cell array, whereA{t}contains anmt-by-mt– 1state-transition coefficient matrix. If the number of states changes from periodt– 1 tot, thenmtmt– 1.

NaNvalues in any coefficient matrix indicate unique, unknown parameters in the state-space model.Acontributes:

  • sum(isnan(A(:)))未知参数定常状态odels. In other words, if the state-space model is time invariant, then the software uses the same unknown parameters defined inAat each period.

  • numParamsAunknown parameters to time-varying state-space models, wherenumParamsA = sum(cell2mat(cellfun(@(x)sum(sum(isnan(x))),A,'UniformOutput',0))). In other words, if the state-space model is time varying, then the software assigns a new set of parameters for each matrix inA.

You cannot specifyAandParamMapsimultaneously.

Data Types:double|cell

State-disturbance-loading系数矩阵for explicit state-space model creation, specified as a matrix or cell vector of matrices.

The state disturbances,ut, are independent Gaussian random variables with mean 0 and standard deviation 1. The state-disturbance-loading coefficient matrix,Bt, specifies the additive error structure in the state-transition equation from periodt– 1 tot, for allt= 1,...,T. That is, the state-transition equation at periodtisxt=Atxt–1+Btut.

For time-invariant state-space models, specifyBas anm-by-kmatrix, wheremis the number of states andkis the number of state disturbances per period.B*B'is the state-disturbance covariance matrix for all periods.

For time-varying state-space models, specifyBas aT-dimensional cell array, whereB{t}contains anmt-by-ktstate-disturbance-loading coefficient matrix. If the number of states or state disturbances changes at periodt, then the matrix dimensions betweenB{t-1}andB{t}vary.B{t}*B{t}'is the state-disturbance covariance matrix for periodt.

NaNvalues in any coefficient matrix indicate unique, unknown parameters in the state-space model.Bcontributes:

  • sum(isnan(B(:)))未知参数定常状态odels. In other words, if the state-space model is time invariant, then the software uses the same unknown parameters defined inBat each period.

  • numParamsBunknown parameters to time-varying state-space models, wherenumParamsB = sum(cell2mat(cellfun(@(x)sum(sum(isnan(x))),B,'UniformOutput',0))). In other words, if the state-space model is time varying, then the software assigns a new set of parameters for each matrix inB.

You cannot specifyBandParamMapsimultaneously.

Data Types:double|cell

Measurement-sensitivity coefficient matrix for explicit state-space model creation, specified as a matrix or cell vector of matrices.

The measurement-sensitivity coefficient matrix,Ct, specifies how the states are expected to linearly combine at periodtto form the observations,yt, for allt= 1,...,T. That is, the expected observation equation at periodtisE(yt|xt) =Ctxt.

For time-invariant state-space models, specifyCas ann-by-mmatrix, wherenis the number of observations andmis the number of states per period.

For time-varying state-space models, specifyCas aT-dimensional cell array, whereC{t}contains annt-by-mtmeasurement-sensitivity coefficient matrix. If the number of states or observations changes at periodt, then the matrix dimensions betweenC{t-1}andC{t}vary.

NaNvalues in any coefficient matrix indicate unique, unknown parameters in the state-space model.Ccontributes:

  • sum(isnan(C(:)))未知参数定常状态odels. In other words, if the state-space model is time invariant, then the software uses the same unknown parameters defined inCat each period.

  • numParamsCunknown parameters to time-varying state-space models, wherenumParamsC = sum(cell2mat(cellfun(@(x)sum(sum(isnan(x))),C,'UniformOutput',0))). In other words, if the state-space model is time varying, then the software assigns a new set of parameters for each matrix inC.

You cannot specifyCandParamMapsimultaneously.

Data Types:double|cell

Observation-innovation coefficient matrix for explicit state-space model creation, specified as a matrix or cell vector of matrices.

The observation innovations,εt, are independent Gaussian random variables with mean 0 and standard deviation 1. The observation-innovation coefficient matrix,Dt, specifies the additive error structure in the observation equation at periodt, for allt= 1,...,T. That is, the observation equation at periodtisyt=Ctxt+Dtεt.

For time-invariant state-space models, specifyDas ann-by-hmatrix, wherenis the number of observations andhis the number of observation innovations per period.D*D'is the observation-innovation covariance matrix for all periods.

For time-varying state-space models, specifyDas aT-dimensional cell array, whereD{t}contains annt-by-htmatrix. If the number of observations or observation innovations changes at periodt, then the matrix dimensions betweenD{t-1}andD{t}vary.D{t}*D{t}'is the observation-innovation covariance matrix for periodt.

NaNvalues in any coefficient matrix indicate unique, unknown parameters in the state-space model.Dcontributes:

  • sum(isnan(D(:)))未知参数定常状态odels. In other words, if the state-space model is time invariant, then the software uses the same unknown parameters defined inDat each period.

  • numParamsDunknown parameters to time-varying state-space models, wherenumParamsD = sum(cell2mat(cellfun(@(x)sum(sum(isnan(x))),D,'UniformOutput',0))). In other words, if the state-space model is time varying, then the software assigns a new set of parameters for each matrix inD.

By default,Dis an empty matrix indicating no observation innovations in the state-space model.

You cannot specifyDandParamMapsimultaneously.

Data Types:double|cell

Parameter-to-matrix mapping function for implicit state-space model creation, specified as a function handle.

ParamMapmust be a function that takes at least one input argument and returns at least three output arguments. The requisite input argument is a vector of unknown parameters, and the requisite output arguments correspond to the coefficient matricesA,B, andC, respectively. If your parameter-to-mapping function requires the input parameter vector argument only, then implicitly create a state-space model by entering the following:

Mdl = ssm(@ParamMap)

In general, you can write an intermediate function, for example,ParamFun, using this syntax:

function [A,B,C,D,Mean0,Cov0,StateType,DeflateY] = ... ParamFun(params,...otherInputArgs...)

In this general case, create the state-space model by entering

Mdl =舰导弹(@ (params) ParamMap (params,……otherInputArgs...))

However:

  • Follow the order of the output arguments.

  • paramsis a vector, and each element corresponds to an unknown parameter.

  • ParamFunmust returnA,B, andC, which correspond to the state-transition, state-disturbance-loading, and measurement-sensitivity coefficient matrices, respectively.

  • If you specify more input arguments than the parameter vector (params), such as observed responses and predictors, then implicitly create the state-space model using the syntax pattern

    Mdl = ssm(@(params)ParamFun(params,y,z))

  • For the optional output argumentsD,Mean0,Cov0,StateType, andDeflateY:

    • The optional output arguments correspond to the observation-innovation coefficient matrixDand the name-value pair argumentsMean0,Cov0, andStateType.

    • To skip specifying an optional output argument, set the argument to[]in the function body. For example, to skip specifyingD, then setD = [];in the function.

    • DeflateYis the deflated-observation data, which accommodates a regression component in the observation equation. For example, in this function, which has a linear regression component,Yis the vector of observed responses andZis the vector of predictor data.

      function [A,B,C,D,Mean0,Cov0,StateType,DeflateY] = ParamFun(params,Y,Z) ... DeflateY = Y - params(9) - params(10)*Z; ... end

    • For the default values ofMean0,Cov0, andStateType, seeAlgorithms.

  • It is best practice to:

    • Load the data to the MATLAB®工作区之前指定的model.

    • Create the parameter-to-matrix mapping function as its own file.

If you specifyParamMap, then you cannot specify any name-value pair arguments or any other input arguments.

Data Types:function_handle

Diffuse state-space model to convert to a state-space model, specified as adssmmodel object.

ssmsets all initial variances of diffuse states inDSSMMdl.Cov0fromInfto1e7. Any diffuse states with variance other thanInfretain their values.

To apply the standard Kalman filter instead of the diffuse Kalman filter for filtering, smoothing, and parameter estimation, convert a diffuse state-space model to a state-space model.

Name-Value Arguments

Specify optional pairs of arguments asName1=Value1,...,NameN=ValueN, whereNameis the argument name andValueis the corresponding value. Name-value arguments must appear after other arguments, but the order of the pairs does not matter.

Before R2021a, use commas to separate each name and value, and encloseNamein quotes.

Initial state mean for explicit state-space model creation, specified as the comma-separated pair consisting of'Mean0'and a numeric vector with length equal to the number of initial states. For the default values, seeAlgorithms.

If you specifyParamMap, then you cannot specifyMean0. Instead, specify the initial state mean in the parameter-to-matrix mapping function.

Data Types:double

Initial state covariance matrix for explicit state-space model creation, specified as the comma-separated pair consisting of'Cov0'and a square matrix with dimensions equal to the number of initial states. For the default values, seeAlgorithms.

If you specifyParamMap, then you cannot specifyCov0. Instead, specify the initial state covariance in the parameter-to-matrix mapping function.

Data Types:double

Initial state distribution indicator for explicit state-space model creation, specified as the comma-separated pair consisting of'StateType'and a numeric vector with length equal to the number of initial states. This table summarizes the available types of initial state distributions.

Value 初始状态分布类型
0 Stationary (for example, ARMA models)
1 The constant 1 (that is, the state is 1 with probability 1)
2 Diffuse or nonstationary (for example, random walk model, seasonal linear time series) orstatic state

For example, suppose that the state equation has two state variables: The first state variable is an AR(1) process, and the second state variable is a random walk. Specify the initial distribution types by setting'StateType',[0; 2].

If you specifyParamMap, then you cannot specifyMean0. Instead, specify the initial state distribution indicator in the parameter-to-matrix mapping function.

For the default values, seeAlgorithms.

Data Types:double

Properties

expand all

State-transition coefficient matrix for explicitly created state-space models, specified as a matrix, a cell vector of matrices, or an empty array ([]). For implicitly created state-space models and before estimation,Ais[]and read only.

The state-transition coefficient matrix,At, specifies how the states,xt, are expected to transition from periodt– 1 tot, for allt= 1,...,T. That is, the expected state-transition equation at periodtisE(xt|xt–1) =Atxt–1.

For time-invariant state-space models,Ais anm-by-mmatrix, wheremis the number of states per period.

For time-varying state-space models,Ais aT-dimensional cell array, whereA{t}contains anmt-by-mt– 1state-transition coefficient matrix. If the number of states changes from periodt– 1 tot, thenmtmt– 1.

NaNvalues in any coefficient matrix indicate unknown parameters in the state-space model.Acontributes:

  • sum(isnan(A(:)))未知参数定常状态odels. In other words, if the state-space model is time invariant, then the software uses the same unknown parameters defined inAat each period.

  • numParamsAunknown parameters to time-varying state-space models, wherenumParamsA = sum(cell2mat(cellfun(@(x)sum(sum(isnan(x))),A,'UniformOutput',0))). In other words, if the state-space model is time varying, then the software assigns a new set of parameters for each matrix inA.

Data Types:double|cell

State-disturbance-loading系数矩阵for explicitly created state-space models, specified as a matrix, a cell vector of matrices, or an empty array ([]). For implicitly created state-space models and before estimation,Bis[]and read only.

The state disturbances,ut, are independent Gaussian random variables with mean 0 and standard deviation 1. The state-disturbance-loading coefficient matrix,Bt, specifies the additive error structure in the state-transition equation from periodt– 1 tot, for allt= 1,...,T. That is, the state-transition equation at periodtisxt=Atxt–1+Btut.

For time-invariant state-space models,Bis anm-by-kmatrix, wheremis the number of states andkis the number of state disturbances per period.B*B'is the state-disturbance covariance matrix for all periods.

For time-varying state-space models,Bis aT-dimensional cell array, whereB{t}contains anmt-by-ktstate-disturbance-loading coefficient matrix. If the number of states or state disturbances changes at periodt, then the matrix dimensions betweenB{t-1}andB{t}vary.B{t}*B{t}'is the state-disturbance covariance matrix for periodt.

NaNvalues in any coefficient matrix indicate unknown parameters in the state-space model.Bcontributes:

  • sum(isnan(B(:)))未知参数定常状态odels. In other words, if the state-space model is time invariant, then the software uses the same unknown parameters defined inBat each period.

  • numParamsBunknown parameters to time-varying state-space models, wherenumParamsB = sum(cell2mat(cellfun(@(x)sum(sum(isnan(x))),B,'UniformOutput',0))). In other words, if the state-space model is time varying, then the software assigns a new set of parameters for each matrix inB.

Data Types:double|cell

Measurement-sensitivity coefficient matrix for explicitly created state-space models, specified as a matrix, a cell vector of matrices, or an empty array ([]). For implicitly created state-space models and before estimation,Cis[]and read only.

The measurement-sensitivity coefficient matrix,Ct, specifies how the states are expected to combine linearly at periodtto form the observations,yt, for allt= 1,...,T. That is, the expected observation equation at periodtisE(yt|xt) =Ctxt.

For time-invariant state-space models,Cis ann-by-mmatrix, wherenis the number of observations andmis the number of states per period.

For time-varying state-space models,Cis aT-dimensional cell array, whereC{t}contains annt-by-mtmeasurement-sensitivity coefficient matrix. If the number of states or observations changes at periodt, then the matrix dimensions betweenC{t-1}andC{t}vary.

NaNvalues in any coefficient matrix indicate unknown parameters in the state-space model.Ccontributes:

  • sum(isnan(C(:)))未知参数定常状态odels. In other words, if the state-space model is time invariant, then the software uses the same unknown parameters defined inCat each period.

  • numParamsCunknown parameters to time-varying state-space models, wherenumParamsC = sum(cell2mat(cellfun(@(x)sum(sum(isnan(x))),C,'UniformOutput',0))). In other words, if the state-space model is time varying, then the software assigns a new set of parameters for each matrix inC.

Data Types:double|cell

Observation-innovation coefficient matrix for explicitly created state-space models, specified as a matrix, a cell vector of matrices, or an empty array ([]). For implicitly created state-space models and before estimation,Dis[]and read only.

The observation innovations,εt, are independent Gaussian random variables with mean 0 and standard deviation 1. The observation-innovation coefficient matrix,Dt, specifies the additive error structure in the observation equation at periodt, for allt= 1,...,T. That is, the observation equation at periodtisyt=Ctxt+Dtεt.

For time-invariant state-space models,Dis ann-by-hmatrix, wherenis the number of observations andhis the number of observation innovations per period.D*D'is the observation-innovation covariance matrix for all periods.

For time-varying state-space models,Dis aT-dimensional cell array, whereD{t}contains annt-by-htmatrix. If the number of observations or observation innovations changes at periodt, then the matrix dimensions betweenD{t-1}andD{t}vary.D{t}*D{t}'is the state-disturbance covariance matrix for periodt.

NaNvalues in any coefficient matrix indicate unknown parameters in the state-space model.Dcontributes:

  • sum(isnan(D(:)))未知参数定常状态odels. In other words, if the state-space model is time invariant, then the software uses the same unknown parameters defined inDat each period.

  • numParamsDunknown parameters to time-varying state-space models, wherenumParamsD = sum(cell2mat(cellfun(@(x)sum(sum(isnan(x))),D,'UniformOutput',0))). In other words, if the state-space model is time varying, then the software assigns a new set of parameters for each matrix inD.

Data Types:double|cell

Initial state mean, specified as a numeric vector or an empty array ([]).Mean0has length equal to the number of initial states (size(A,1)orsize(A{1},1)).

Mean0is the mean of the Gaussian distribution of the states at period 0.

For implicitly created state-space models and before estimation,Mean0is[]and read only. However,estimatespecifiesMean0after estimation.

Data Types:double

Initial state covariance matrix, specified as a square matrix or an empty array ([]).Cov0has dimensions equal to the number of initial states (size(A,1)orsize(A{1},1)).

Cov0is the covariance of the Gaussian distribution of the states at period 0.

For implicitly created state-space models and before estimation,Cov0is[]and read only. However,estimatespecifiesCov0after estimation.

Data Types:double

Initial state distribution indicator, specified as a numeric vector or empty array ([]).StateTypehas length equal to the number of initial states.

For implicitly created state-space models or models with unknown parameters,StateTypeis[]and read only.

This table summarizes the available types of initial state distributions.

Value 初始状态分布类型
0 Stationary (e.g., ARMA models)
1 The constant 1 (that is, the state is 1 with probability 1)
2 Nonstationary (e.g., random walk model, seasonal linear time series) orstatic state

For example, suppose that the state equation has two state variables: The first state variable is an AR(1) process, and the second state variable is a random walk. Then,StateTypeis[0; 2].

For nonstationary states,ssmsetsCov0to1e7by default. Subsequently, the software implements the Kalman filter for filtering, smoothing, and parameter estimation. This specification imposes relatively weak knowledge on the initial state values of diffuse states, and uses initial state covariance terms between all states.

Data Types:double

Parameter-to-matrix mapping function, specified as a function handle or an empty array ([]).ParamMapcompletely specifies the structure of the state-space model. That is,ParamMapdefinesA,B,C,D, and, optionally,Mean0,Cov0, andStateType. For explicitly created state-space models,ParamMapis[]and read only.

Data Types:function_handle

Object Functions

expand all

estimate Maximum likelihood parameter estimation of state-space models
disp Display summary information for state-space model
refine Refine initial parameters to aid state-space model estimation
filter Forward recursion of state-space models
smooth Backward recursion of state-space models
update Real-time state update by state-space model Kalman filtering
irf Impulse response function (IRF) of state-space model
irfplot Plot impulse response function (IRF) of state-space model
simulate Monte Carlo simulation of state-space models
simsmooth State-space model simulation smoother
forecast Forecast states and observations of state-space models

Copy Semantics

Value. To learn how value classes affect copy operations, seeCopying Objects.

Examples

collapse all

Create a standard state-space model containing two independent, autoregressive states, and the observations are the deterministic sum of the two states. Symbolically, the system of equations is

[ x t , 1 x t , 2 ] = [ ϕ 1 0 0 ϕ 2 ] [ x t - 1 , 1 x t - 1 , 2 ] + [ σ 1 0 0 σ 2 ] [ u t , 1 u t , 2 ]

y t = [ 1 1 ] [ x t , 1 x t , 2 ] .

Specify the state-transition matrix.

A = [NaN 0; 0 NaN];

Specify the state-disturbance-loading matrix.

B = [NaN 0; 0 NaN];

Specify the measurement-sensitivity matrix.

C = [1 1];

Define the state-space model usingssm.

Mdl =舰导弹(A, B, C)
Mdl =类型:状态空间模型的地对地导弹状态向量与兰gth: 2 Observation vector length: 1 State disturbance vector length: 2 Observation innovation vector length: 0 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) + (c3)u1(t) x2(t) = (c2)x2(t-1) + (c4)u2(t) Observation equation: y1(t) = x1(t) + x2(t) Initial state distribution: Initial state means are not specified. Initial state covariance matrix is not specified. State types are not specified.

Mdlis anssmmodel containing unknown parameters. A detailed summary ofMdlprints to the Command Window.

这是很好的实践的e to verify that the state and observation equations are correct. If the equations are not correct, then it might help to expand the state-space equation manually.

Create a state-space model containing two independent, autoregressive states, and the observations are the sum of the two states, plus Gaussian error. Symbolically, the equation is

[ x t , 1 x t , 2 ] = [ ϕ 1 0 0 ϕ 2 ] [ x t - 1 , 1 x t - 1 , 2 ] + [ σ 1 0 0 σ 2 ] [ u t , 1 u t , 2 ]

y t = [ 1 1 ] [ x t , 1 x t , 2 ] + σ 3 ε t .

Define the state-transition matrix.

A = [NaN 0; 0 NaN];

Define the state-disturbance-loading matrix.

B = [NaN 0; 0 NaN];

Define the measurement-sensitivity matrix.

C = [1 1];

Define the observation-innovation matrix.

D = NaN;

Create the state-space model usingssm.

Mdl = ssm(A,B,C,D)
Mdl =类型:状态空间模型的地对地导弹状态向量与兰gth: 2 Observation vector length: 1 State disturbance vector length: 2 Observation innovation vector length: 1 Sample size supported by model: Unlimited Unknown parameters for estimation: 5 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) + (c3)u1(t) x2(t) = (c2)x2(t-1) + (c4)u2(t) Observation equation: y1(t) = x1(t) + x2(t) + (c5)e1(t) Initial state distribution: Initial state means are not specified. Initial state covariance matrix is not specified. State types are not specified.

Mdlis anssmmodel containing unknown parameters. A detailed summary ofMdlprints to the Command Window.

这是很好的实践的e to verify that the state and observations equations are correct. If the equations are not correct, then it might help to expand the state-space equation manually.

Pass the data andMdltoestimateto estimate the parameters.

Create a state-space model, where the state equation is an AR(2) model. The state disturbances are mean zero Gaussian random variables with standard deviation of 0.3. The observation equation is the difference between the current and previous state plus a mean zero Gaussian observation innovation with a standard deviation of 0.1. Symbolically, the state-space model is

[ x 1 , t x 2 , t x 3 , t ] = [ 0 . 6 0 . 2 0 . 5 1 0 0 0 0 1 ] [ x 1 , t - 1 x 2 , t - 1 x 3 , t - 1 ] + [ 0 . 3 0 0 ] u 1 , t

y t = [ 1 - 1 0 ] [ x 1 , t x 2 , t x 3 , t ] + 0 . 1 ε t .

There are three states: x 1 , t is the AR(2) process, x 2 , t represents x 1 , t - 1 , and x 3 , t is the AR(2) model constant.

Define the state-transition matrix.

A = [0.6 0.2 0.5; 1 0 0; 0 0 1];

Define the state-disturbance-loading matrix.

B = [0.3; 0; 0];

Define the measurement-sensitivity matrix.

C = [1 -1 0];

Define the observation-innovation matrix.

D = 0.1;

Usessmto create the state-space model. Set the initial-state mean (Mean0) and covariance matrix (Cov0). Identify the type of initial state distributions (StateType) by noting the following:

  • x 1 , t is a stationary, AR(2) process.

  • x 2 , t is also a stationary, AR(2) process.

  • x 3 , t is the constant 1 for all periods.

Mean0 = [0; 0; 1];% The mean of the AR(2)varAR2 = 0.3*(1 - 0.2)/((1 + 0.2)*((1 - 0.2)^2 - 0.6^2));% The variance of the AR(2)Cov1AR2 = 0.6*0.3/((1 + 0.2)*((1 - 0.2)^2) - 0.6^2);% The covariance of the AR(2)Cov0 = zeros(3); Cov0(1:2,1:2) = varAR2*eye(2) + Cov1AR2*flip(eye(2)); StateType = [0; 0; 1]; Mdl = ssm(A,B,C,D,'Mean0',Mean0,'Cov0',Cov0,'StateType',StateType)
Mdl =类型:状态空间模型的地对地导弹状态向量与兰gth: 3 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) = (0.60)x1(t-1) + (0.20)x2(t-1) + (0.50)x3(t-1) + (0.30)u1(t) x2(t) = x1(t-1) x3(t) = x3(t-1) Observation equation: y1(t) = x1(t) - x2(t) + (0.10)e1(t) Initial state distribution: Initial state means x1 x2 x3 0 0 1 Initial state covariance matrix x1 x2 x3 x1 0.71 0.44 0 x2 0.44 0.71 0 x3 0 0 0 State types x1 x2 x3 Stationary Stationary Constant

Mdlis anssmmodel.

You can display properties ofMdlusing dot notation. For example, display the initial state covariance matrix.

Mdl.Cov0
ans =3×30.7143 0.4412 0 0.4412 0.7143 0 0 0 0

Use a parameter mapping function to create a time-invariant state-space model, where the state model is AR(1) model. The states are observed with bias, but without random error. Set the initial state mean and variance, and specify that the state is stationary.

Write a function that specifies how the parameters inparamsmap to the state-space model matrices, the initial state values, and the type of state. Symbolically, the model is

$$\begin{array}{*{20}{c}}
{{x_t} = \phi {x_{t - 1}} + \sigma {u_t}}\\
{{y_t} = a{x_t}}
\end{array}.$$

% Copyright 2015 The MathWorks, Inc.function[A,B,C,D,Mean0,Cov0,StateType] = timeInvariantParamMap(params)% Time-invariant 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). The state model is AR(1)% without observation error.varu1 = exp(params(2));% Positive variance constraintA = params(1); B = sqrt(varu1); C = params(3); D = []; Mean0 = 0.5; Cov0 = 100; StateType = 0;end

Save this code as a file namedtimeInvariantParamMap.mto a folder on your MATLAB® path.

Create the state-space model by passing the functiontimeInvariantParamMapas a function handle tossm.

Mdl = ssm(@timeInvariantParamMap);

ssmimplicitly creates the state-space model. Usually, you cannot verify implicitly defined state-space models.

If you estimate, filter, or smooth a diffuse state-space model containing at least one diffuse state, then the software uses the diffuse Kalman filter. To use the standard Kalman filter instead, convert the diffuse state-space model to a standard state-space model.ssmattributes a large initial state variance (1e7) for diffuse states. A standard state-space model treatment results in an approximation to the results of the diffuse Kalman filter. However,estimateuses all of the data to fit the model, andfilterandsmoothreturn filtered and smoothed estimates for all periods, respectively.

Explicitly create a one-dimensional diffuse state-space model. Specify that the first state equation is x t = x t - 1 + u t , and that the observation model is y t = x t + ε t .

A = 1; B = 1; C = 1; D = 1; MdlDSSM = dssm(A,B,C,D)
MdlDSSM = State-space model type: dssm State vector length: 1 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 equation: x1(t) = x1(t-1) + u1(t) Observation equation: y1(t) = x1(t) + e1(t) Initial state distribution: Initial state means x1 0 Initial state covariance matrix x1 x1 Inf State types x1 Diffuse

MdlDSSMis adssmmodel object. Because the model does not contain any unknown parameters,dssminfers the initial state distribution and its parameters. In particular, the initial state variance isInfbecause the nonstationary state has a diffuse distribution by default.

Convert MdlDSSM to a standard state-space model.

Mdl = ssm(MdlDSSM)
Mdl =类型:状态空间模型的地对地导弹状态向量与兰gth: 1 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 equation: x1(t) = x1(t-1) + u1(t) Observation equation: y1(t) = x1(t) + e1(t) Initial state distribution: Initial state means x1 0 Initial state covariance matrix x1 x1 1e+07 State types x1 Diffuse

Mdlis anssmmodel object. The structures ofMdlandMdlDSSMare equivalent, except that the initial state variance of the state inMdlis1e7.

To see the difference between the two models, simulate 10 periods of data from a state-space model that is similar toMdl, except it has known initial state mean of 5 and variance 2.

Mdl0 = ssm(A,B,C,D,'Mean0',5,'Cov0',2,'StateType',2); T = 10; rng(1);% For reproducibilityy = simulate(Mdl0,T);

Obtain filtered and smoothed states fromMdlandMdlDSSMusing the simulated data.

fY = filter(Mdl,y); fDY = filter(MdlDSSM,y); sY = smooth(Mdl,y); sDY = smooth(MdlDSSM,y);

Plot the filtered and smoothed states.

figure; plot(1:T,y,'-o',1:T,fY,'-d',1:T,fDY,'-*'); legend('Simulated Data','Filtered States -- Mdl','Filtered States -- MdlDSSM');

Figure contains an axes object. The axes object contains 3 objects of type line. These objects represent Simulated Data, Filtered States -- Mdl, Filtered States -- MdlDSSM.

figure; plot(1:T,y,'-o',1:T,sY,'-d',1:T,sDY,'-*'); legend('Simulated Data','Smoothed States -- Mdl','Smoothed States -- MdlDSSM');

Figure contains an axes object. The axes object contains 3 objects of type line. These objects represent Simulated Data, Smoothed States -- Mdl, Smoothed States -- MdlDSSM.

Besides apparent transient behavior, the filtered and smoothed states between the standard and diffuse state-space models appear nearly equivalent. The slight difference occurs becausefilterandsmoothset all diffuse state estimates in the diffuse state-space model to 0 while they implement the diffuse Kalman filter. Once the covariance matrices of the smoothed states attain full rank,filterandsmooth切换到使用the standard Kalman filter. In this case, the switching time occurs after the first period.

More About

expand all

Tips

SpecifyParamMapin a more general or complex setting, where, for example:

  • The initial state values are parameters.

  • In time-varying models, you want to use the same parameters for more than one period.

  • You want to impose parameter constraints.

Algorithms

  • Default values forMean0andCov0:

    • If you explicitly specify the state-space model (that is, you provide the coefficient matricesA,B,C, and optionallyD), then:

      • For stationary states, the software generates the initial value using the stationary distribution. If you provide all values in the coefficient matrices (that is, your model has no unknown parameters), thenssmgenerates the initial values. Otherwise, the software generates the initial values during estimation.

      • For states that are always the constant 1,ssmsetsMean0to 1 andCov0to0.

      • For diffuse states, the software setsMean0to 0 andCov0to1e7by default.

    • If you implicitly create the state-space model (that is, you provide the parameter vector to the coefficient-matrices-mapping functionParamMap), then the software generates any initial values during estimation.

  • For static states that do not equal 1 throughout the sample, the software cannot assign a value to the degenerate, initial state distribution. Therefore, set static states to2using the name-value pair argumentStateType. Subsequently, the software treats static states as nonstationary and assigns the static state a diffuse initial distribution.

  • It is best practice to setStateTypefor each state. By default, the software generatesStateType, but this behavior might not be accurate. For example, the software cannot distinguish between a constant 1 state and a static state.

  • The software cannot inferStateTypefrom data because the data theoretically comes from the observation equation. The realizations of the state equation are unobservable.

  • ssmmodels do not store observed responses or predictor data. Supply the data wherever necessary using the appropriate input or name-value pair arguments.

  • Suppose that you want to create a state-space model using a parameter-to-matrix mapping function with this signature:

    [A,B,C,D,Mean0,Cov0,StateType,DeflateY] = paramMap(params,Y,Z)
    and you specify the model using an anonymous function
    Mdl = ssm(@(params)paramMap(params,Y,Z))
    The observed responsesYand predictor dataZare not input arguments in the anonymous function. IfYandZexist in the MATLAB Workspace before you createMdl, then the software establishes a link to them. Otherwise, if you passMdltoestimate, the software throws an error.

    The link to the data established by the anonymous function overrides all other corresponding input argument values ofestimate. This distinction is important particularly when conducting a rolling window analysis. For details, seeRolling-Window Analysis of Time-Series Models.

Alternatives

  • If the states are observable, and the state equation resembles any of the following models, use the associated function instead.

    • ARIMA model, then you can specify anarimamodel instead.

    • Regression model with ARIMA errors, then you can specify aregARIMAmodel instead.

    • Conditional variance model, then you can specify agarch,egarch, orgjrmodel instead.

    • VAR model, then you can estimate such a model usingvarmandestimate.

  • To impose no prior knowledge on the initial state values of diffuse states, and to implement the diffuse Kalman filter, create adssmmodel object instead of anssmmodel object.

  • For a Bayesian view of a standard state-space model, usebssm.

References

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