%% Convert Standard to Diffuse State-Space Model
% By default, ssm assigns a large scalar (1e7) to the initial state variance of all diffuse states in a standard state-space model. Using this specification, the software subsequently estimates, filters, and smooths a standard state-space model using the standard Kalman filter. A standard state-space model treatment is an approximation to results from an analysis that treats diffuse states using infinite variance. To implement the diffuse Kalman filter instead, convert the standard state-space model to a diffuse state-space model. This conversion attributes infinite variance to all diffuse states.
% Explicitly create a two-dimensional standard state-space model. Specify that the first state equation is x_{1,t} = x_{1,t-1} + u_{1,t} and that the second state equation is x_{2,t} = 0.2x_{2,t-1} + u_{2,t}. Specify that the first observation equation is y_{1,t} = x_{1,t} + \varepsilon_{1,t} and that the second observation equation is y_{2,t} = x_{2,t} + \varepsilon_{2,t}. Specify that the states are diffuse and nonstationary, respectively.

A = [1 0; 0 0.2];   
B = [1 0; 0 1]; 
C = [1 0;0 1]; 
D = [1 0; 0 1];
StateType = [2 0];
MdlSSM = ssm(A,B,C,D,'StateType',StateType)

% MdlSSM is an ssm model object. In some cases, ssm can detect the state type, but it is good practice to specify whether the state is stationary, diffuse, or the constant 1. Because the model does not contain any unknown parameters, ssm infers the initial state distributions.
%
% Convert MdlSSM to a diffuse state-space model.

Mdl = dssm(MdlSSM)

% Mdl is a dssm model object. The structures of Mdl and MdlSSM are equivalent, except that the initial state variance of the state in Mdl is Inf rather than 1e7.
%
% To see the difference between the two models, simulate 10 periods of data from a state-space model that is similar to MdlSSM. Set the initial state covariance matrix to I_2.

Mdl0 = MdlSSM;
Mdl0.Cov0 = eye(2);
T = 10;
rng(1); % For reproducibility
y = simulate(Mdl0,T);

%
% Obtain filtered and smoothed states from Mdl and MdlSSM using the simulated data.

fY = filter(MdlSSM,y);
fYD = filter(Mdl,y);
sY = smooth(MdlSSM,y);
sYD = smooth(Mdl,y);

%
% Plot the filtered and smoothed states.

figure;
subplot(2,1,1)
plot(1:T,y(:,1),'-o',1:T,fY(:,1),'-d',1:T,fYD(:,1),'-*');
title('Filtered States for x_{1,t}')
legend('Simulated Data','Filtered States -- MdlSSM','Filtered States -- Mdl');
subplot(2,1,2)
plot(1:T,y(:,1),'-o',1:T,sY(:,1),'-d',1:T,sYD(:,1),'-*');
title('Smoothed States for x_{1,t}')
legend('Simulated Data','Smoothed States -- MdlSSM','Smoothed States -- Mdl');

figure;
subplot(2,1,1)
plot(1:T,y(:,2),'-o',1:T,fY(:,2),'-d',1:T,fYD(:,2),'-*');
title('Filtered States for x_{2,t}')
legend('Simulated Data','Filtered States -- MdlSSM','Filtered States -- Mdl');
subplot(2,1,2)
plot(1:T,y(:,2),'-o',1:T,sY(:,2),'-d',1:T,sYD(:,2),'-*');
title('Smoothed States for x_{2,t}')
legend('Simulated Data','Smoothed States -- MdlSSM','Smoothed States -- Mdl');

% In addition to apparent transient behavior in the random walk, the filtered and smoothed states between the standard and diffuse state-space models appear nearly equivalent. The slight difference occurs because filter and smooth set 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, filter and smooth switch to using the standard Kalman filter. In this case, the switching time occurs after the first period.
% Copyright 2015 The MathWorks, Inc.