Main Content

simsmooth

Bayesian nonlinear non-Gaussian state-space model simulation smoother

Since R2024a

Description

simsmooth provides random paths of states drawn from the posterior smoothed state distribution, which is the distribution of the states conditioned on model parameters Θ and the full-sample response data, of a Bayesian nonlinear non-Gaussian state-space model (bnlssm).

To draw state paths from the posterior smoothed state distribution, simsmooth uses the nonlinear forward-filtering, backward-sampling method (FFBS), in which it implements sequential Monte Carlo (SMC) to perform forward filtering, and then it resamples and reweights particles (weighted random samples) generated by SMC to perform backward sampling.

X = simsmooth(Mdl,Y,params) returns a randomly drawn path of states, simulated from the posterior smoothed state distribution, by applying the simulation smoother to the input Bayesian nonlinear non-Gaussian state-space model and responses data. simsmooth uses FFBS to obtain the random path from the posterior smoothed state distribution. simsmooth evaluates the parameter map Mdl.ParamMap by using the input vector of parameter values.

example

X = simsmooth(Mdl,Y,params,Name=Value) specifies additional options using one or more name-value arguments. For example, simsmooth(Mdl,Y,params,NumPaths=1e4,Resample="residual") specifies generating 1e4 random paths and to resample residuals.

example

[X,OutputFilter,x0] = simsmooth(___) additionally returns the following quantities using any of the input-argument combinations in the previous syntaxes:

  • OutputFilter — SMC forward filtering results by sampling time containing the following quantities:

    • Approximate loglikelihood values associated with the input data, input parameters, particles, and posterior state filtering distribution

    • Filter estimate of state-distribution means

    • Filter estimate of state-distribution covariance

    • State particles and corresponding weights that approximate the filtering distribution

    • Effective sample size

    • Flags indicating which data the software used to filter

    • Flags indicating resampling

  • x0 — Simulation-smoothed initial state, computed only when you request the this output

example

Examples

collapse all

This example draws a random path from the approximate posterior smoothed state distribution of the Bayesian nonlinear state-space model in equation. The state-space model contains two independent, stationary, autoregressive states each with a model constant. The observations are a nonlinear function of the states with Gaussian noise. The prior distribution of the parameters is flat. Symbolically, the system of equations is

[x1,tx2,tx3,tx4,t]=[θ1θ200010000θ3θ40001][x1,t-1x2,t-1x3,t-1x4,t-1]+[θ50000θ600][u1,tu3,t]

yt=log(exp(x1,t-μ1)+exp(x3,t-μ3))+θ7εt.

μ1 and μ3 are the unconditional means of the corresponding states. The initial distribution moments of each state are their unconditional mean and covariance.

Create a Bayesian nonlinear state-space model characterized by the system. The observation equation is in equation form, that is, the function composing the states is nonlinear and the innovation series εt is additive, linear, and Gaussian. The Local Functions section contains two functions required to specify the Bayesian nonlinear state-space model: the state-space model parameter mapping function and the prior distribution of the parameters. You can use the functions only within this script.

Mdl = bnlssm(@paramMap,@priorDistribution)
Mdl = 
  bnlssm with properties:

             ParamMap: @paramMap
    ParamDistribution: @priorDistribution
      ObservationForm: "equation"
           Multipoint: [1x0 string]

Mdl is a bnlssm model specifying the state-space model structure and prior distribution of the state-space model parameters. Because Mdl contains unknown values, it serves as a template for posterior analysis with observations.

Simulate a series of 100 observations from the following stationary 2-D VAR process.

x1,t=1+0.9x1,t-1+0.3u1,tx3,t=-1-0.75x3,t-1+0.2u3,t,

where the disturbance series uj,t are standard Gaussian random variables.

rng(1,"twister")    % For reproducibility
T = 100;
thetatrue = [0.9; 1; -0.75; -1; 0.3; 0.2; 0.1];
MdlSim = varm(AR={diag(thetatrue([1 3]))},Covariance=diag(thetatrue(5:6).^2), ...
    Constant=thetatrue([2 4]));
XSim = simulate(MdlSim,T);

Compose simulated observations using the following equation.

yt=log(exp(x1,t-x1)+exp(x3,t-x3))+0.1εt,

where the innovation series εt is a standard Gaussian random variable.

ysim = log(sum(exp(XSim - mean(XSim)),2)) + thetatrue(7)*randn(T,1);

To draw from the approximate posterior smoothed state distribution, the simsmooth function requires response data and a model with known state-space model parameters. Choose a random set with the following constraints:

  • θ1 and θ3 are within the unit circle. Use U(-1,1) to generate values.

  • θ2 and θ4 are real numbers. Use the N(0,32) distribution to generate values.

  • θ5, θ6, and θ7 are positive real numbers. Use the χ12 distribution to generate values.

theta13 = (-1+(1-(-1)).*rand(2,1));
theta24 = 3*randn(2,1);
theta567 = chi2rnd(1,3,1);
theta = [theta13(1); theta24(1); theta13(2); theta24(2); theta567];

Draw a random path from the approximate smoothed state posterior distribution by passing the Bayesian nonlinear model, simulated data, and parameter values to simsmooth.

SmoothX = simsmooth(Mdl,ysim,theta);
size(SmoothX)
ans = 1×2

   100     4

SmoothX is a 100-by-4 matrix containing one path drawn from the approximate posterior smoothed state distribution, with rows corresponding to periods in the sample and columns corresponding to the state variables. The simsmooth function uses the FFBS method (SMC and a bootstrap to resample particles and weights) to obtain draws from the posterior smoothed state distribution.

Draw another path, but specify Θ used to simulate the data.

SmoothXSim = simsmooth(Mdl,ysim,thetatrue);

Plot the two paths with the true state values.

figure
tiledlayout(2,1)
nexttile
plot([SmoothX(:,1) SmoothXSim(:,1) XSim(:,1)])
title("x_{t,1}")
legend("Smoothed State, random \theta","Smoothed State, true \theta","XSim")
nexttile
plot([SmoothX(:,3) SmoothXSim(:,3) XSim(:,2)])
title("x_{t,3}")
legend("Smoothed State, random \theta","Smoothed State, true \theta","XSim")

Figure contains 2 axes objects. Axes object 1 with title x indexOf t, 1 baseline contains 3 objects of type line. These objects represent Smoothed State, random \theta, Smoothed State, true \theta, XSim. Axes object 2 with title x indexOf t, 3 baseline contains 3 objects of type line. These objects represent Smoothed State, random \theta, Smoothed State, true \theta, XSim.

The paths using the true value of Θ and the simulated state paths are close. The paths generated from the random value of Θ is far from the simulated state paths.

Local Functions

These functions specify the state-space model parameter mappings, in equation form, and log prior distribution of the parameters.

function [A,B,C,D,Mean0,Cov0,StateType] = paramMap(theta)
    A = @(x)blkdiag([theta(1) theta(2); 0 1],[theta(3) theta(4); 0 1])*x; 
    B = [theta(5) 0; 0 0; 0 theta(6); 0 0];
    C = @(x)log(exp(x(1)-theta(2)/(1-theta(1))) + ...
        exp(x(3)-theta(4)/(1-theta(3))));
    D = theta(7);
    Mean0 = [theta(2)/(1-theta(1)); 1; theta(4)/(1-theta(3)); 1];         
    Cov0 = diag([theta(5)^2/(1-theta(1)^2) 0 theta(6)^2/(1-theta(3)^2) 0]);          
    StateType = [0; 1; 0; 1];     % Stationary state and constant 1 processes
end

function logprior = priorDistribution(theta)
    paramconstraints = [(abs(theta([1 3])) >= 1); (theta(5:7) <= 0)];
    if(sum(paramconstraints))
        logprior = -Inf;
    else 
        logprior = 0;   % Prior density is proportional to 1 for all values
                        % in the parameter space.
    end
end

This example shows how to draw from the posterior distribution of smoothed states and model parameters by using a Gibbs sampler. Consider this nonlinear state-space model

xt=ϕxt-1+σutytPoisson(λext),

where the parameters in Θ={ϕ,σ,λ} have the following priors:

  • ϕN(-1,1)(m0,v02), that is, a truncated normal distribution with ϕ[-1,1].

  • σ2IG(a0,b0), that is, an inverse gamma distribution with shape a0 and scale b0.

  • λGamma(α0,β0), that is, a gamma distribution with shape α0 and scale β0.

Simulate Series

Consider this data-generating process (DGP).

xt=0.7xt-1+0.2utytPoisson(3ext),

where the series ut is a standard Gaussian series of random variables.

Simulate a series of 200 observations from the process.

rng(500,"twister")    % For reproducibility
T = 200;
thetaDGP = [0.7; 0.2; 3];

numparams = numel(thetaDGP);

MdlXSim = arima(AR=thetaDGP(1),Variance=thetaDGP(2), ...
    Constant=0);
xsim = simulate(MdlXSim,T);
y = random("poisson",thetaDGP(3)*exp(xsim));

figure
plot(y)

Figure contains an axes object. The axes object contains an object of type line.

Create Bayesian Nonlinear Model

The Local Functions section contains the functions paramMap and logPrior required to specify the Bayesian nonlinear state-space model. The paramMap function specifies the state-space model structure and initial state moments. The priorDistribution function returns the log of the joint prior distribution of the state-space model parameters. You can use the functions only within this script.

Create a Bayesian nonlinear state-space model for the DGP. Indicate that the state-space model observation equation is expressed as a distribution. To speed up computations, the arguments A and LogY of the paramMap function are written to enable simultaneous evaluation of the transition and observation densities of multiple particles. Specify this characteristic by using the Multipoint name-value argument.

Mdl = bnlssm(@paramMap,@priorDistribution,ObservationForm="distribution", ...
    Multipoint=["A" "LogY"]);

Perform Gibbs Sampling

A Gibbs sampler is an Markov chain Monte Carlo method for drawing a sample from the joint posterior distribution of parameters. It successively draws from the full conditional distributions of the states and model parameters, one at a time; results of previous draws are substituted, which enables the Markov chains to explore the parameter space.

The full conditional distribution of the states is the posterior of the smoothed state distribution, which simsmooth computes. The remaining full conditionals are:

  • π(ϕ|x,y,σ2,λ)N(-1,1)(mϕ,vϕ2), where mϕ=v02t=2Txtxt-1+σ2m0v02t=2Txt-12+σ2 andvϕ2=σ2v02v02t=2Txt-12+σ2.

  • π(σ2|x,y,ϕ,λ)IG(aσ2,bσ2), where aσ2=a0+T-12 and bσ2=b01+0.5b0t=2T(xt-ϕxt-1)2.

  • π(λ|x,y,ϕ,σ2)Gamma(αλ,βλ), where αλ=α0+t=1Tyt and βλ=β01+β0t=1Text.

You can view the joint posterior distribution π(ϕ,σ2|x,y,λ) as the semiconjugate Bayesian linear regression model zt=ϕwt+vt, where the response series zt={x2,...,xT}, predictor series wt={x1,...,xT-1}, and the error series vtN(0,σ2). If you view the problem this way, you can speed up sampler. During sampling, you can reject any ϕ draws outside its support.

The Local Functions section contains the following functions:

  • phiFC — Draws from π(ϕ|x,y,σ2,λ)

  • sigma2FC — Draws from π(σ2|x,y,ϕ,λ)

  • lambdaFC — Draws from π(λ|x,y,ϕ,σ2)

Conduct the Gibbs sampler. Draw a sample of 2000 from the full conditional distributions. Draw 1500 particles for each call of simsmooth. Perform rejection sampling at a maximum of 50 iterations. This example chooses the initial conditions arbitrarily.

nGibbs = 2000;
Gibbs = zeros(T+numparams,nGibbs); % Preallocate for state and parameter draws
numparticles = 1500;
maxiterations = 50;

% Initial values
% theta
theta0 = [0.5; 0.1; 2]; 

% pi(phi,sigma2) hyperparameters
m0 = 0;                 
v02 = 1;
a0 = 1;
b0 = 1;
MdlBLM = semiconjugateblm(1,Intercept=0,Mu=m0,V=v02, ...
    A=a0,B=b0);

% lambda hyperparameters
alpha0 = 3;
beta0 = 1;

hyperparams = [m0 v02 a0 b0 alpha0 beta0];

% Prepare wait bar dialog box
wb = waitbar(0,"1",Name="Running Gibbs Sampler ...", ...
    CreateCancelBtn="setappdata(gcbf,Canceling=true)");
setappdata(wb,Canceling=false);

% Gibbs sampler
theta = theta0;
for j = 1:nGibbs
    % Press Cancel in the dialog to break.
    if getappdata(wb,"Canceling")
        fprintf("Gibbs sampler canceled")
        break
    end
    waitbar(j/nGibbs,wb,sprintf("Draw %d of %d",j,nGibbs));
    Gibbs(1:T,j) = simsmooth(Mdl,y,theta,NumParticles=numparticles, ...
        MaxIterations=maxiterations);
    [Gibbs(T+1,j),Gibbs(T+2,j)] = blmFC(Gibbs(1:T,j),MdlBLM,theta);
    Gibbs(T+3,j) = lambdaFC(Gibbs(1:T,j),y,hyperparams);
    theta = Gibbs(T+(1:3),j);
end
delete(wb)

Describe π(Θ|y)

To reduce the influence of initial conditions on the sample, remove the first 50 draws from the Gibbs sample. To reduce the influence of serial correlation in the sample, thin the sample by keeping every fourth draw.

burnin = 50;
thin = 4;
pphi = Gibbs(T+1,burnin:thin:end);
psigma2 = Gibbs(T+2,burnin:thin:end);
plambda = Gibbs(T+3,burnin:thin:end);

Plot trace plots of the Gibbs sampler.

figure
h = tiledlayout(3,1);
nexttile
plot(pphi)
title("\pi(\phi|y)")
nexttile
plot(psigma2)
title("\pi(\sigma^2|y)")
nexttile
plot(plambda)
title("\pi(\lambda|y)")
title(h,"Trace Plots")

Figure contains 3 axes objects. Axes object 1 with title pi ( phi |y) contains an object of type line. Axes object 2 with title pi ( sigma Squared baseline |y) contains an object of type line. Axes object 3 with title pi ( lambda |y) contains an object of type line.

The trace plots show that the Markov chains are mixing adequately.

Summarize the posterior distributions by computing sample medians and 95% percentile intervals of the processed posterior draws. Plot histograms of the posterior distributions of the model parameters.

mphi = median(pphi);
ciphi = quantile(pphi,[0.025 0.975]);
msigma2 = median(psigma2);
cisigma2 = quantile(psigma2,[0.025 0.975]);
mlambda = median(plambda);
cilambda = quantile(plambda,[0.025 0.975]);

figure
h = tiledlayout(3,1);
nexttile
histogram(pphi)
title(sprintf("\\pi(\\phi|y), median=%.3f, ci=(%.3f, %.3f)",mphi,ciphi))
nexttile
histogram(psigma2)
title(sprintf("\\pi(\\sigma^2|y), median=%.3f, ci=(%.3f, %.3f)",msigma2,cisigma2))
nexttile
histogram(plambda)
title(sprintf("\\pi(\\lambda|y), median=%.3f, ci=(%.3f, %.3f)",mlambda,cilambda))
title(h,"Posterior Histograms")

Figure contains 3 axes objects. Axes object 1 with title pi ( phi |y), blank median= 0 . 709 , blank ci=( 0 . 555 , blank 0 . 856 ) contains an object of type histogram. Axes object 2 with title pi ( sigma Squared baseline |y), blank median= 0 . 209 , blank ci=( 0 . 133 , blank 0 . 310 ) contains an object of type histogram. Axes object 3 with title pi ( lambda |y), blank median= 3 . 348 , blank ci=( 2 . 615 , blank 4 . 183 ) contains an object of type histogram.

The posterior medians are close to their DGP counterparts.

Local Functions

These functions specify the state-space model parameter mappings, in distribution form, the log prior distribution of the parameters, and random draws from full conditional distribution of each parameter.

function [A,B,LogY,Mean0,Cov0,StateType] = paramMap(theta)
    A = theta(1); 
    B = sqrt(theta(2));
    LogY = @(y,x)y.*x - exp(x).*theta(3);
    Mean0 = 0;
    Cov0 = 2;
    StateType = 0;     % Stationary state process
end

function logprior = priorDistribution(theta,hyperparams)
    % Prior of phi
    m0 = hyperparams(1);
    v20 = hyperparams(2);
    pphi = makedist("normal",mu=m0,sigma=sqrt(v20));
    pphi = truncate(pphi,-1,1);
    lpphi = log(pdf(pphi,theta(1)));

    % Prior of sigma2
    a0 = hyperparams(3);
    b0 = hyperparams(4);
    lpsigma2 = -a0*log(b0) - log(gamma(a0)) + (-a0-1)*log(theta(2)) - ...
        1./(b0*theta(2));

    % Prior of lambda
    alpha0 = hyperparams(5);
    beta0 = hyperparams(6);
    plambda = makedist("gamma",alpha0,beta0);
    lplambda = log(pdf(plambda,theta(3))); 

    logprior = lpphi + lpsigma2 + lplambda;
end

function [phi,sigma2] = blmFC(x,mdl,theta)
    % Reject sampled phi when it is outside the unit circle
    while true
        phi = simulate(mdl,x(1:end-1),x(2:end),Sigma2=theta(2));
        if abs(phi) < 1
            break
        end
    end
    [~,sigma2] = simulate(mdl,x(1:(end-1)),x(2:end),Beta=theta(1));
end

function [lambda,alpha,beta] = lambdaFC(x,y,hyperparams)
   alpha0 = hyperparams(5);
   beta0 = hyperparams(6);
   alpha = sum(y) + alpha0;
   beta = beta0./(beta0*sum(exp(x))+1);
   lambda = gamrnd(alpha,beta,1);
end

simsmooth runs SMC to forward filter the state-space model, which includes resampling particles. To assess the quality of the sample, including whether any posterior filtered state distribution is close to degenerate, you can monitor these algorithms by returning the third output of smooth.

Consider this nonlinear state-space model.

[xt,1xt,2xt,3xt,4]=[θ1θ200010000θ3θ40001][xt-1,1xt-1,2xt-1,3xt-1,4]+[θ50000θ600][ut,1ut,3]

yt=log(exp(xt,1-μ1)+exp(xt,3-μ3))+θ7εt.

μ1 and μ3 are the unconditional means of the corresponding states. The initial distribution moments of each state are their unconditional mean and covariance.

Simulate a series of 100 observations from the following stationary 2-D VAR process.

xt,1=1+0.9xt-1,1+0.3ut,1xt,3=-1+-0.75xt-1,3+0.2ut,3yt=log(exp(xt,1-x1)+exp(xt,3-x3))+0.1εt,

where the disturbance series ut,j and εt are standard Gaussian random variables.

rng(100,"twister")    % For reproducibility
T = 100;
thetatrue = [0.9; 1; -0.75; -1; 0.3; 0.2; 0.1];
MdlSim = varm(AR={diag(thetatrue([1 3]))},Covariance=diag(thetatrue(5:6).^2), ...
    Constant=thetatrue([2 4]));
XSim = simulate(MdlSim,T);
y = log(sum(exp(XSim - mean(XSim)),2)) + thetatrue(7)*randn(T,1);

Create a Bayesian nonlinear state-space model. The Local Functions section contains the required functions specifying the Bayesian nonlinear state-space model structure and joint prior distribution.

Mdl = bnlssm(@paramMap,@priorDistribution);

Approximate the posterior smoothed state distribution of the state-space model. As in the Draw Path from Posterior Smoothed State Distribution example, choose a random set of initial parameter values. Specify the resampling residuals for the SMC. Return the forward-filtering results and the approximate initial smoothed state x0.

theta13 = (-1+(1-(-1)).*rand(2,1));
theta24 = 3*randn(2,1);
theta567 = chi2rnd(1,3,1);
theta = [theta13(1); theta24(1); theta13(2); theta24(2); theta567];

[~,OutputFilter,x0] = simsmooth(Mdl,y,theta,Resample="residual");

Output is a 100-by-1 structure array containing several fields, one set of fields for each observation, including:

  • FilteredStatesCov — Approximate posterior filtered state distribution covariance for the states at each sampling time

  • DataUsed — Whether the forward-filtering algorithm used an observation for posterior estimation

  • Resample — Whether the forward-filtering algorithm resampled the particles associated with an observation

Plot the determinant of the approximate posterior filtered state covariance matrices for states that are not constant.

filteredstatecov = cellfun(@(x)det(x([1 3],[1 3])),{OutputFilter.FilteredStatesCov});

figure
plot(filteredstatecov)
title("Approx. Post. Filtered State Covariances")

Figure contains an axes object. The axes object with title Approx. Post. Filtered State Covariances contains an object of type line.

Any covariance determinant that is close to 0 indicates a close-to-degenerate distribution. No covariance determinants in the analysis are close to 0.

Determine whether the forward-filtering algorithm omitted any observations from posterior estimation.

anyObsOmitted = sum([OutputFilter.DataUsed]) ~= T
anyObsOmitted = logical
   0

anyObsOmitted = 0 indicates that the algorithm used all observations.

Determine whether filter resampled any particles associated with observations.

whichResampled = numel(find([OutputFilter.Resampled] == true))
whichResampled = 
75

The forward-filtering algorithm resampled particles associated with observations the 75 observations listed in whichResampled.

Local Functions

These functions specify the state-space model parameter mappings, in equation form, and log prior distribution of the parameters.

function [A,B,C,D,Mean0,Cov0,StateType] = paramMap(theta)
    A = @(x)blkdiag([theta(1) theta(2); 0 1],[theta(3) theta(4); 0 1])*x; 
    B = [theta(5) 0; 0 0; 0 theta(6); 0 0];
    C = @(x)log(exp(x(1)-theta(2)/(1-theta(1))) + ...
        exp(x(3)-theta(4)/(1-theta(3))));
    D = theta(7);
    Mean0 = [theta(2)/(1-theta(1)); 1; theta(4)/(1-theta(3)); 1];         
    Cov0 = diag([theta(5)^2/(1-theta(1)^2) 0 theta(6)^2/(1-theta(3)^2) 0]);          
    StateType = [0; 1; 0; 1];     % Stationary state and constant 1 processes
end

function logprior = priorDistribution(theta)
    paramconstraints = [(abs(theta([1 3])) >= 1) (theta(5:7) <= 0)];
    if(sum(paramconstraints))
        logprior = -Inf;
    else 
        logprior = 0;   % Prior density is proportional to 1 for all values
                        % in the parameter space.
    end
end

Input Arguments

collapse all

Bayesian nonlinear state-space model, specified as a bnlssm model object created by the bnlssm function.

The function handles of the properties Mdl.ParamDistribution and Mdl.ParamMap determine the prior and the data likelihood, respectively. simsmooth evaluates Mdl.ParamMap at the input params.

Observed response data, specified as a numeric matrix or a cell vector of numeric vectors.

  • If Mdl is time invariant with respect to the observation equation, Y is a T-by-n matrix. Each row of the matrix corresponds to a period and each column corresponds to a particular observation in the model. T is the sample size and n is the number of observations per period. The last row of Y contains the latest observations.

  • If Mdl is time varying with respect to the observation equation, Y is a T-by-1 cell vector. Y{t} contains an nt-dimensional vector of observations for period t, where t = 1, ..., T. For linear observation models, the corresponding dimensions of the coefficient matrices, outputs of Mdl.ParamMap, C{t}, and D{t} must be consistent with the matrix in Y{t} for all periods. For nonlinear observation models, the dimensions of the inputs and outputs associated with the observations must be consistent. Regardless of model type, the last cell of Y contains the latest observations.

NaN elements indicate missing observations. For details on how simsmooth accommodates missing observations, see Algorithms.

Data Types: double | cell

State-space model parameters Θ to evaluate the parameter mapping Mdl.ParamMap, specified as a numparams-by-1 numeric vector. Elements of params0 must correspond to the elements of the first input arguments of Mdl.ParamMap and Mdl.ParamDistribution.

Data Types: double

Name-Value Arguments

Specify optional pairs of arguments as Name1=Value1,...,NameN=ValueN, where Name is the argument name and Value is the corresponding value. Name-value arguments must appear after other arguments, but the order of the pairs does not matter.

Example: simsmooth(Mdl,Y,params,NumParticles=1e4,Resample="residual") specifies generating 1e4 random paths and to use the residual-resampling SMC method.

Number of particles for SMC, specified as a positive integer.

Example: NumParticles=1e4

Data Types: double

Number of sample state paths to draw from the posterior smoothed state distribution, specified as a positive integer.

Example: NumPaths=1e4

Data Types: double

Maximum number of rejection sampling iterations for posterior sampling using the simulation smoother, specified as a nonnegative integer. simsmooth conducts rejection sampling before it conducts the computationally intensive importance sampling algorithm.

Example: MaxIterations=100

Data Types: double

SMC resampling method, specified as a value in this table.

ValueDescription
"multinomial"At time t, the set of previously generated particles (parent set) follows a standard multinomial distribution, with probabilities proportional to their weights. An offspring set is resampled with replacement from the parent set [1].
"residual"Residual sampling, a modified version of multinomial resampling that can produce an estimator with lower variance than the multinomial resampling method [6].
"systematic"Systematic sampling, which produces an estimator with lower variance than the multinomial resampling method [4].

Resampling methods downsample insignificant particles to achieve a smaller estimator variance than if no resampling is performed and to avoid sampling from a degenerate proposal [4].

Example: Resample="residual"

Data Types: char | string

Effective sample size threshold, below which simsmooth resamples particles, specified as a nonnegative scalar. For more details, see [4], Ch. 12.3.3.

Tip

  • To resample during every period, set Cutoff=numparticles, where numparticles is the value of the NumParticles name-value argument.

  • To avoid resampling, set Cutoff=0.

Example: Cutoff=0.75*numparticles

Data Types: double

Flag for sorting particles before resampling, specified as a value in this table.

ValueDescription
truesimsmooth sorts the generated particles before resampling them.
falsesimsmooth does not sort the generated particles.

When SortPartiles=true, simsmooth uses Hilbert sorting during the SMC routine to sort the particles. This action can reduce Monte Carlo variation, which is useful when you compare loglikelihoods resulting from evaluating several params arguments that are close to each other [3]. However, the sorting routine requires more computation resources, and can slow down computations, particularly in problems with a high-dimensional state variable.

Example: SortParticles=true

Data Types: logical

Previously generated normal random numbers, as returned by filter, to reproduce simsmooth results, specified as the RND output, a structure array, of previous filter call. Specify RND to control the random number generator.

The default is an empty structure array, which causes simsmooth to generate new random numbers.

Data Types: struct

Output Arguments

collapse all

Simulated paths of smoothed states, drawn from the posterior smoothed state distribution p(xT,…,x0|yT,…,y1,Θ), for t = 1,…,T, returned as a T-by-m numeric matrix for one simulated path, T-by-m-by-NumPaths 3-D numeric array for NumPaths simulated paths, or a T-by-1 cell vector of numeric matrices.

Each row corresponds to a time point in the sample. The last row contains the latest simulated smoothed states.

If Mdl is a time-invariant model with respect to the states, each column of X corresponds to a state in the model and each page corresponds to a sample path.

If Mdl is a time-varying model with respect to the states, then, for each t = 1,…,T, cell X(t) contains an mt-by-NumPaths matrix of simulated smoothed states. Each row corresponds to a state variable for the corresponding time and each column corresponds to a simulated path. Each path across all cells correspond.

SMC forward filtering results by period, returned as a T-by-1 structure array with fields in this table, and where cell t corresponds to the filtering result for time t.

FieldDescriptionEstimate/Approximation of
LogLikelihoodScalar approximate loglikelihood objective function value log p(yt|y1,…,yt)
FilteredStatesmt-by-1 vector of approximate filtered state estimatesE(xt|y1,...,yt)
FilteredStatesCovmt-by-mt variance-covariance matrix of filtered statesVar(xt|y1,...,yt)
CustomStatistics(mt + 1)-by-NumParticles simulated particles and corresponding weights that approximate the filtering distributionN/A
EffectiveSampleSizeEffective sample size for importance sampling, a scalar in [0,NumParticles]N/A
DataUsedht-by-1 flag indicating whether the software filters using a particular observation. For example, if observation j at time t is a NaN, element j in DataUsed at time t is 0.N/A
ResampledFlag indicating whether simsmooth resampled particlesN/A

Simulated paths of the smoothed initial state vector, drawn from the posterior smoothed state distribution p(xT,…,x0|yT,…,y1,Θ), for t = 1,…,T, returned as an m0-by-1 numeric vector for one simulated path or an m0-by-NumPaths numeric matrix for NumPaths simulated paths.

Each row of x0 corresponds to a state in the model and each column corresponds to a sample path.

If you do not request to return x0, simsmooth does not compute it.

Tips

  • Smoothing has several advantages over filtering.

    • The smoothed state estimator is more accurate than the online filter state estimator because it is based on the full-sample data, rather than only observations up to the estimated sampling time.

    • A stable approximation to the gradient of the loglikelihood function, which is important for numerical optimization, is available from the smoothed state samples of the simulation smoother (finite differences of the approximated loglikelihood computed from the filter state estimates is numerically unstable).

    • You can use the simulation smoother to perform Bayesian estimation of the nonlinear state-space model via the Metropolis-within-Gibbs sampler.

  • Unless you set Cutoff=0, simsmooth resamples particles according to the specified resampling method Resample. Although resampling particles with high weights improves the results of the SMC, you should also allow the sampler traverse the proposal distribution to obtain novel, high-weight particles. To do this, experiment with Cutoff.

  • Avoid an arbitrary choice of the initial state distribution. bnlssm functions generate the initial particles from the specified initial state distribution, which impacts the performance of the nonlinear filter. If the initial state specification is bad enough, importance weights concentrate on a small number of particles in the first SMC iteration, which might produce unreasonable filtering results. This vulnerability of the nonlinear model behavior contrasts with the stability of the Kalman filter for the linear model, in which the initial state distribution usually has little impact on the filter because the prior is washed out as it processes data.

Algorithms

simsmooth accommodates missing data by not updating filtered state estimates corresponding to missing observations. In other words, suppose there is a missing observation at period t. Then, the state forecast for period t based on the previous t – 1 observations and filtered state for period t are equivalent.

References

[1] Andrieu, Christophe, Arnaud Doucet, and Roman Holenstein. "Particle Markov Chain Monte Carlo Methods." Journal of the Royal Statistical Society Series B: Statistical Methodology 72 (June 2010): 269–342. https://doi.org/10.1111/j.1467-9868.2009.00736.x.

[2] Andrieu, Christophe, and Gareth O. Roberts. "The Pseudo-Marginal Approach for Efficient Monte Carlo Computations." Ann. Statist. 37 (April 2009): 697–725. https://dx.doi.org/10.1214/07-AOS574.

[3] Deligiannidis, George, Arnaud Doucet, and Michael Pitt. "The Correlated Pseudo-Marginal Method." Journal of the Royal Statistical Society, Series B: Statistical Methodology 80 (June 2018): 839–870. https://doi.org/10.1111/rssb.12280.

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

[5] Fernández-Villaverde, Jesús, and Juan F. Rubio-Ramírez. "Estimating Macroeconomic Models: A Likelihood Approach." Review of Economic Studies 70(October 2007): 1059–1087. https://doi.org/10.1111/j.1467-937X.2007.00437.x.

[6] Liu, Jun, and Rong Chen. "Sequential Monte Carlo Methods for Dynamic Systems." Journal of the American Statistical Association 93 (September 1998): 1032–1044. https://dx.doi.org/10.1080/01621459.1998.10473765.

Version History

Introduced in R2024a

See Also

Objects

Functions