Main Content

update

Real-time state update by state-space model Kalman filtering

Since R2021b

Description

update efficiently updates the state distribution in real time by applying one recursion of the Kalman filter to compute state-distribution moments for the final period of the specified response data.

To compute state-distribution moments by recursive application of the Kalman filter for each period in the specified response data, use filter instead.

[nextState,NextStateCov] = update(Mdl,Y) returns the updated state-distribution moments at the final time T, conditioned on the current state distribution, by applying one recursion of the Kalman filter to the fully specified, standard state-space model Mdl given T observed responses Y. nextState and NextStateCov are the mean and covariance, respectively, of the updated state distribution.

example

[nextState,NextStateCov] = update(Mdl,Y,currentState,CurrentStateCov) initializes the Kalman filter at the current state distribution with mean currentState and covariance matrix CurrentStateCov.

example

[nextState,NextStateCov] = update(___,Name,Value) uses additional options specified by one or more name-value arguments, and uses any of the input-argument combinations in the previous syntaxes. For example, update(Mdl,Y,Params=params,SquareRoot=true) sets unknown parameters in the partially specified model Mdl to the values in params, and specifies use of the square-root Kalman filter variant for numerical stability.

example

[nextState,NextStateCov,logL] = update(___) also returns the loglikelihoods computed for each observation in Y.

example

Examples

collapse all

Suppose that a latent process is an AR(1). The state equation is

xt=0.5xt-1+ut,

where ut is Gaussian with mean 0 and standard deviation 1.

Generate a random series of 100 observations from xt, assuming that the series starts at 1.5.

T = 100;
ARMdl = arima(AR=0.5,Constant=0,Variance=1);
x0 = 1.5;
rng(1); % For reproducibility
x = simulate(ARMdl,T,Y0=x0);

Suppose further that the latent process is subject to additive measurement error. The observation equation is

yt=xt+εt,

where εt is Gaussian with mean 0 and standard deviation 0.75. Together, the latent process and observation equations compose a state-space model.

Use the random latent state process (x) and the observation equation to generate observations.

y = x + 0.75*randn(T,1);

Specify the four coefficient matrices.

A = 0.5;
B = 1;
C = 1;
D = 0.75;

Specify the state-space model using the coefficient matrices.

Mdl = ssm(A,B,C,D)
Mdl = 
State-space model type: ssm

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) = (0.50)x1(t-1) + u1(t)

Observation equation:
y1(t) = x1(t) + (0.75)e1(t)

Initial state distribution:

Initial state means
 x1 
  0 

Initial state covariance matrix
     x1   
 x1  1.33 

State types
     x1     
 Stationary 

Mdl is an ssm model. Verify that the model is correctly specified using the display in the Command Window. The software infers that the state process is stationary. Subsequently, the software sets the initial state mean and covariance to the mean and variance of the stationary distribution of an AR(1) model.

Filter the observations through the state-space model, in real time, to obtain the state distribution for period 100.

[rtfX100,rtfXVar100] = update(Mdl,y)
rtfX100 = 
1.2073
rtfXVar100 = 
0.3714

update applies the Kalman filter to all observations in y, and returns the state estimate of only period 100.

Compare the result to the results of filter.

[fX,~,output] = filter(Mdl,y);
size(fX)
ans = 1×2

   100     1

fX100 = fX(100)
fX100 = 
1.2073
fXVar100 = output(end).FilteredStatesCov
fXVar100 = 
0.3714
tol = 1e-10;
discrepencyMeans = fX100 - rtfX100;
discrepencyVars = fXVar100 - rtfXVar100;
areMeansEqual = norm(discrepencyMeans) < tol
areMeansEqual = logical
   1

areVarsEqual = norm(discrepencyVars) < tol
areVarsEqual = logical
   1

Like update, the filter function filters the observations through the model, but it returns all intermediate state estimates. Because update returns only the final state estimate, it is more suited to real-time calculations than filter.

Consider the simulated data and state-space model in Compute Only Final State Distribution from Kalman Filter.

T = 100;
ARMdl = arima(AR=0.5,Constant=0,Variance=1);
x0 = 1.5;
rng(1); % For reproducibility

x = simulate(ARMdl,T,Y0=x0);

y = x + 0.75*randn(T,1);

A = 0.5;
B = 1;
C = 1;
D = 0.75;
Mdl = ssm(A,B,C,D);

Suppose observations are available sequentially, and consider obtaining the updated state distribution by filtering each new observation as it is available.

Simulate the following procedure using a loop.

  1. Create variables that store the initial state distribution moments.

  2. Filter the incoming observation through the model specifying the current initial state distribution moments.

  3. Overwrite the current state distribution moments with the new state distribution moments.

  4. Repeat steps 2 and 3 as new observations are available.

currentState = Mdl.Mean0;
currentStateCov = Mdl.Cov0;
newState = zeros(T,1);
newStateCov = zeros(T,1);

for j = 1:T
    [newState(j),newStateCov(j)] = update(Mdl,y(j),currentState,currentStateCov);
    currentState = newState(j);
    currentStateCov = newStateCov(j);
end

Plot the observations, true state values, and new state means of each period.

figure
plot(1:T,x,'-k',1:T,y,'*g',1:T,newState,':r','LineWidth',2)
xlabel("Period")
legend(["True state values" "Observations" "New state values"])

Figure contains an axes object. The axes object with xlabel Period contains 3 objects of type line. One or more of the lines displays its values using only markers These objects represent True state values, Observations, New state values.

Compare the results to the results of filter.

tol = 1e-10;
[fX,~,output] = filter(Mdl,y);
discrepencyMeans = fX - newState;
discrepencyVars = [output.FilteredStatesCov]' - newStateCov;
areMeansEqual = norm(discrepencyMeans) < tol
areMeansEqual = logical
   1

areVarsEqual = norm(discrepencyVars) < tol
areVarsEqual = logical
   1

The real-time filter update, applied to the entire data set sequentially, returns the same state distributions as filter.

Consider that the linear relationship between the change in the unemployment rate and the nominal gross national product (nGNP) growth rate is of interest. Suppose the innovations of a mismeasured regression of the first difference of the unemployment rate onto the nGNP growth rate is an ARMA(1,1) series with Gaussian disturbances (that is, a regression model with ARMA(1,1) errors and measurement error). Symbolically, and in state-space form, the model is

[x1,tx2,t]=[ϕθ00][x1,t-1x2,t-1]+[11]utyt-βZt=x1,t+σεt,

where:

  • x1,t is the ARMA error series in the regression model.

  • x2,t is a dummy state for the MA(1) effect.

  • y1,t is the observed change in the unemployment rate being deflated by the growth rate of nGNP (Zt).

  • ut is a Gaussian series of disturbances having mean 0 and standard deviation 1.

  • εt is a Gaussian series of measurement errors with scale σ.

Load the Nelson-Plosser data set, which contains the unemployment rate and nGNP series, among other measurements.

load Data_NelsonPlosser

Preprocess the data by following this procedure:

  1. Remove the leading missing observations.

  2. Convert the nGNP series to a return series by using price2ret.

  3. Apply the first difference to the unemployment rate series.

vars = ["GNPN" "UR"];
DT = rmmissing(DataTable(:,vars));
T = size(DT,1) - 1; % Sample size after differencing

Z = [ones(T,1) price2ret(DT.GNPN)];
y = diff(DT.UR);

Though this example removes missing values, the Kalman filter accommodates series containing missing values.

Specify the coefficient matrices.

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

Specify the state-space model using ssm.

Mdl = ssm(A,B,C,D);

Fit the model to all observations except for the final 10 observations (a holdout sample). Use a random set of initial parameter values for optimization. Specify the regression component and its initial value for optimization using the 'Predictors' and 'Beta0' name-value arguments, respectively. Restrict the estimate of σ to all positive, real numbers.

fh = 10;
params0 = [0.3 0.2 0.2];
[EstMdl,estParams] = estimate(Mdl,y(1:T-fh),params0,'Predictors',Z(1:T-fh,:), ...
    'Beta0',[0.1 0.2],'lb',[-Inf,-Inf,0,-Inf,-Inf]);
Method: Maximum likelihood (fmincon)
Sample size: 51
Logarithmic  likelihood:     -87.2409
Akaike   info criterion:      184.482
Bayesian info criterion:      194.141
           |      Coeff       Std Err    t Stat     Prob  
----------------------------------------------------------
 c(1)      |  -0.31780       0.37357    -0.85071  0.39494 
 c(2)      |   1.21242       0.82223     1.47455  0.14034 
 c(3)      |   0.45583       1.32970     0.34281  0.73174 
 y <- z(1) |   1.32407       0.26525     4.99179   0      
 y <- z(2) | -24.48733       1.89161   -12.94520   0      
           |                                              
           |    Final State   Std Dev     t Stat    Prob  
 x(1)      |  -0.38117       0.42842    -0.88971  0.37363 
 x(2)      |   0.23402       0.66222     0.35339  0.72380 

EstMdl is an ssm model.

Nowcast the unemployment rate into the forecast horizon. Simulate this procedure using a loop:

  1. Compute the current state distribution moments by filtering all in-sample observations through the estimated model.

  2. When an observation is available in the forecast horizon, filter it through the model. EstMdl does not store the regression coefficients, so you must pass them in using the name-value argument Beta.

  3. Set the current state distribution state moments to the nowcasts.

  4. Repeat steps 2 and 3 when new observations are available.

[currentState,currentStateCov] = update(EstMdl,y(1:T-fh), ...
    Predictors=Z(1:T-fh,:),Beta=estParams(end-1:end));
unrateF = zeros(fh,2);
unrateCovF = cell(fh,1);

for j = 1:fh
    [unrateF(j,:),unrateCovF{j}] = update(EstMdl,y(T-fh+j),currentState,currentStateCov, ...
        Predictors=Z(T-fh+j,:),Beta=estParams(end-1:end));
    currentState = unrateF(j,:)';
    currentStateCov = unrateCovF{j};
end

Plot the estimated, filtered states. Recall that the first state is the change in the unemployment rate, and the second state helps build the first.

figure
plot(dates((end-fh+1):end),[unrateF(:,1) y((end-fh+1):end)]);
xlabel('Period')
ylabel('Change in the unemployment rate')
title('Filtered Change in the Unemployment Rate')

Figure contains an axes object. The axes object with title Filtered Change in the Unemployment Rate, xlabel Period, ylabel Change in the unemployment rate contains 2 objects of type line.

The filter function returns only the sum of the loglikelihoods for specified observations. To efficiently compute the loglikelihood of each observation, which can be convenient for custom estimation techniques, use update instead.

Consider the simulated data and state-space model in Compute Only Final State Distribution from Kalman Filter.

T = 100;
ARMdl = arima(AR=0.5,Constant=0,Variance=1);
x0 = 1.5;
rng(1); % For reproducibility

x = simulate(ARMdl,T,Y0=x0);

y = x + 0.75*randn(T,1);

A = 0.5;
B = 1;
C = 1;
D = 0.75;
Mdl = ssm(A,B,C,D);

Evaluate the likelihood function for each observation.

[~,~,logLj] = update(Mdl,y);

logL is a 100-by-1 vector; logL(j) is the loglikelihood evaluated at observation j.

Use filter to evaluate the likelihood for the entire data set.

[~,logL] = filter(Mdl,y);

logL is a scalar representing the full data likelihood.

Because the software assumes the sample is randomly drawn, the likelihood for all observations is the sum of the individual loglikelihood values. Confirm this fact.

tol = 1e-10;
discrepency = logL - sum(logLj);
areEqual = discrepency < tol
areEqual = logical
   1

Input Arguments

collapse all

Standard state-space model, specified as an ssm model object returned by ssm or estimate.

If Mdl is partially specified (that is, it contains unknown parameters), specify estimates of the unknown parameters by using the 'Params' name-value argument. Otherwise, update issues an error.

Mdl does not store observed responses or predictor data. Supply the data wherever necessary using the appropriate input or name-value arguments.

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, then Y is a T-by-n matrix, where each row corresponds to a period and each column corresponds to a particular observation in the model. T is the sample size and m 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, then Y is a T-by-1 cell vector. Each element of the cell vector corresponds to a period and contains an nt-dimensional vector of observations for that period. The corresponding dimensions of the coefficient matrices in Mdl.C{t} and Mdl.D{t} must be consistent with the matrix in Y{t} for all periods. The last cell of Y contains the latest observations.

NaN elements indicate missing observations. For details on how the Kalman filter accommodates missing observations, see Algorithms.

The current mean of the state distribution (in other words, the mean at time 1 before the Kalman filter processes the specified observations Y), specified as an m-by-1 numeric vector. m is the number of states.

Data Types: double

The current covariance matrix of the state distribution (in other words, the covariance matrix at time 1 before the Kalman filter processes the specified observations Y), specified as an m-by-m symmetric, positive semi-definite numeric matrix.

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.

Before R2021a, use commas to separate each name and value, and enclose Name in quotes.

Example: update(Mdl,Y,Params=params,SquareRoot=true) sets unknown parameters in the partially specified model Mdl to the values in params, and specifies use of the square-root Kalman filter variant for numerical stability.

Estimates of the unknown parameters in the partially specified state-space model Mdl, specified as a numeric vector.

If Mdl is partially specified (contains unknown parameters specified by NaNs), you must specify Params. The estimate function returns parameter estimates of Mdl in the appropriate form. However, you can supply custom estimates by arranging the elements of Params as follows:

  • If Mdl is an explicitly created model (Mdl.ParamMap is empty []), arrange the elements of Params to correspond to hits of a column-wise search of NaNs in the state-space model coefficient matrices, initial state mean vector, and covariance matrix.

    • If Mdl is time invariant, the order is A, B, C, D, Mean0, and Cov0.

    • If Mdl is time varying, the order is A{1} through A{end}, B{1} through B{end}, C{1} through C{end}, D{1} through D{end}, Mean0, and Cov0.

  • If Mdl is an implicitly created model (Mdl.ParamMap is a function handle), the first input argument of the parameter-to-matrix mapping function determines the order of the elements of Params.

If Mdl is fully specified, update ignores Params.

Example: Consider the state-space model Mdl with A = B = [NaN 0; 0 NaN] , C = [1; 1], D = 0, and initial state means of 0 with covariance eye(2). Mdl is partially specified and explicitly created. Because the model parameters contain a total of four NaNs, Params must be a 4-by-1 vector, where Params(1) is the estimate of A(1,1), Params(2) is the estimate of A(2,2), Params(3) is the estimate of B(1,1), and Params(4) is the estimate of B(2,2).

Data Types: double

Flag for applying the univariate treatment of a multivariate series (also known as sequential filtering), specified as true or false. A value of true applies the univariate treatment.

The univariate treatment can accelerate and improve numerical stability of the Kalman filter. However, all observation innovations must be uncorrelated. That is, DtDt' must be diagonal, where Dt, t = 1,...,T, is one of the following:

  • The matrix D{t} in a time-varying state-space model

  • The matrix D in a time-invariant state-space model

Example: Univariate=true

Data Types: logical

Flag for applying the square-root Kalman filter variant, specified as true or false. A value of true applies the square-root filter when update implements the Kalman filter.

If you suspect that the eigenvalues of the filtered state or forecasted observation covariance matrices are close to zero, set SquareRoot=true. The square-root filter is robust to numerical issues arising from the finite precision of calculations, but it requires more computational resources.

Example: SquareRoot=true

Data Types: logical

Forecast uncertainty threshold, specified as a nonnegative scalar.

If the forecast uncertainty for a particular observation is less than Tolerance during numerical estimation, then the software removes the uncertainty corresponding to the observation from the forecast covariance matrix before its inversion.

It is best practice to set Tolerance to a small number, for example, le-15, to overcome numerical obstacles during estimation.

Example: Tolerance=le-15

Data Types: double

Predictor variables in the state-space model observation equation, specified as a T-by-d numeric matrix, where d is the number of predictor variables. Row t corresponds to the observed predictors at period t (Zt). The expanded observation equation is

ytZtβ=Cxt+Dut.

That is, update deflates the observations using the regression component. β is the time-invariant vector of regression coefficients that the software estimates with all other parameters.

If there are n observations per period, then the software regresses all predictor series onto each observation.

If you specify Predictors, then Mdl must be time invariant. Otherwise, the software returns an error.

By default, the software excludes a regression component from the state-space model.

Data Types: double

Regression coefficients corresponding to predictor variables, specified as a d-by-n numeric matrix. d is the number of predictor variables (see Predictors).

If Mdl is an estimated state-space model, specify the estimated regression coefficients stored in estParams.

Output Arguments

collapse all

State mean after update applies the Kalman filter, returned as an m-by-1 numeric vector. Elements correspond to the order of the states defined in Mdl (either by the rows of A or as determined by Mdl.ParamMap).

State covariance matrix after update applies the Kalman filter, returned as an m-by-m numeric matrix. Rows and columns correspond to the order of the states defined in Mdl (either by the rows of A or as determined by Mdl.ParamMap).

Loglikelihood for each observation in Y, returned as an T-by-1 numeric vector.

More About

collapse all

Real-Time State-Distribution Update

The real-time state-distribution update applies one recursion of the Kalman filter to a standard state-space model given a length T response series and the state distribution at time T - 1, to compute the state distribution at time T.

Consider a state-space model expressed in compact form

[xtyt]=[AtAtCt]xt1+[Bt0BtCtDt][utεt].

The Kalman filter proceeds as follows for each period t:

  1. Obtain the forecast distributions for each period in the data by recursively applying the conditional expectation to the state-space equation, given initial state distribution moments x0|0 and P0|0, and all observations up to time t − 1 (Yt−11). The resulting conditional distribution is

    [xtyt]|Y1t1~Ν([x^t|t1y^t|t1],[Pt|t1Lt|t1Lt|tlVt|t1]),

    where:

    • x^t|t1=Atx^t1|t1, the state forecast for time t.

    • y^t|t1=Ctx^t|t1, the forecasted response for time t.

    • Pt|t1=AtPt1|t1At+BtBt, the state forecast covariance.

    • Vt|t1=CtPt1|t1Ct+DtDt, the forecasted response covariance.

    • Lt|t1=Pt1|t1Ct, the state and response forecast covariance.

  2. Filter observation t through the model to obtain the updated state distribution:

    xt|Y1t~Ν(x^t|t,Pt|t),

    where:

    • x^t|t=x^t|t1+Lt|t1Vt|t11(yty^t|t1), the state filter estimator.

    • Pt|t=Pt|t1Lt|t1Vt|t11Lt|t1, the state covariance filter estimator.

When x^t1|t1 is the current state mean and Pt−1|t−1 is the current state covariance, x^t|t is the new state mean and Pt|t is the new state covariance.

Algorithms

  • The Kalman filter 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.

  • For explicitly defined state-space models, update applies all predictors to each response series. However, each response series has its own set of regression coefficients.

  • For efficiency, update does minimal input validation.

  • In theory, the state covariance matrix must be symmetric and positive semi-definite. update forces symmetry of the covariance matrix before it applies the Kalman filter, but it does not check whether the matrix is positive semi-definite.

Alternative Functionality

To obtain filtered states for each period in the response data, call the filter function instead. Unlike update, filter performs comprehensive input validation.

References

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

Version History

Introduced in R2021b