update
Syntax
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.
[
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
nextState
,NextStateCov
] = update(Mdl
,Y
)Mdl
given T observed responses
Y
. nextState
and
NextStateCov
are the mean and covariance, respectively, of the
updated state distribution.
[
initializes the Kalman filter at the current state distribution with mean
nextState
,NextStateCov
] = update(Mdl
,Y
,currentState
,CurrentStateCov
)currentState
and covariance matrix
CurrentStateCov
.
[
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,
nextState
,NextStateCov
] = update(___,Name,Value
)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.
[
also returns the loglikelihoods computed for each observation in
nextState
,NextStateCov
,logL
] = update(___)Y
.
Examples
Compute Only Final State Distribution from Kalman Filter
Suppose that a latent process is an AR(1). The state equation is
where is Gaussian with mean 0 and standard deviation 1.
Generate a random series of 100 observations from , 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
where 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
.
Filter States in Real Time
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.
Create variables that store the initial state distribution moments.
Filter the incoming observation through the model specifying the current initial state distribution moments.
Overwrite the current state distribution moments with the new state distribution moments.
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"])
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
.
Nowcast State-Space Model Containing Regression Component
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
where:
is the ARMA error series in the regression model.
is a dummy state for the MA(1) effect.
is the observed change in the unemployment rate being deflated by the growth rate of nGNP ().
is a Gaussian series of disturbances having mean 0 and standard deviation 1.
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:
Remove the leading missing observations.
Convert the nGNP series to a return series by using
price2ret
.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:
Compute the current state distribution moments by filtering all in-sample observations through the estimated model.
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 argumentBeta
.Set the current state distribution state moments to the nowcasts.
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')
Efficiently Obtain Observation Contributions to Full Data Likelihood
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
Mdl
— Standard state-space model
ssm
model object
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.
Y
— Observed response data
numeric matrix | cell vector of numeric vectors
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, thenY
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 ofY
contains the latest observations.If
Mdl
is time varying with respect to the observation equation, thenY
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 inMdl.C{t}
andMdl.D{t}
must be consistent with the matrix inY{t}
for all periods. The last cell ofY
contains the latest observations.
NaN
elements indicate missing observations. For details on how the
Kalman filter accommodates missing observations, see Algorithms.
currentState
— Current mean of state distribution
Mdl.Mean0
(default) | numeric vector
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
CurrentStateCov
— Current covariance matrix of state distribution
Mdl.Cov0
(default) | numeric matrix
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.
Params
— Estimates of unknown parameters
numeric vector
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 NaN
s), 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 ofParams
to correspond to hits of a column-wise search ofNaN
s in the state-space model coefficient matrices, initial state mean vector, and covariance matrix.If
Mdl
is time invariant, the order isA
,B
,C
,D
,Mean0
, andCov0
.If
Mdl
is time varying, the order isA{1}
throughA{end}
,B{1}
throughB{end}
,C{1}
throughC{end}
,D{1}
throughD{end}
,Mean0
, andCov0
.
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 ofParams
.
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 NaN
s, 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
Univariate
— Flag for applying univariate treatment of multivariate series
false
(default) | true
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 modelThe matrix
D
in a time-invariant state-space model
Example: Univariate=true
Data Types: logical
SquareRoot
— Flag for applying square-root Kalman filter variant
false
(default) | true
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
Tolerance
— Forecast uncertainty threshold
0
(default) | nonnegative scalar
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
Predictors
— Predictor variables in state-space model observation equation
[]
(default) | numeric matrix
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
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
Beta
— Regression coefficients
[]
(default) | numeric matrix
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
nextState
— State mean after update
applies Kalman filter
numeric vector
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
).
NextStateCov
— State covariance matrix after update
applies Kalman filter
numeric matrix
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
).
logL
— Loglikelihood for each observation
numeric vector
Loglikelihood for each observation in Y
, returned as an
T-by-1 numeric vector.
More About
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
The Kalman filter proceeds as follows for each period t:
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
where:
the state forecast for time t.
the forecasted response for time t.
the state forecast covariance.
the forecasted response covariance.
the state and response forecast covariance.
Filter observation t through the model to obtain the updated state distribution:
where:
the state filter estimator.
the state covariance filter estimator.
When is the current state mean and Pt−1|t−1 is the current state covariance, 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
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list:
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)