Skip to content

BUG: Set the last predicted state cov of a converged filtered-timing filter - #11141

Draft
bashtage wants to merge 2 commits into
statsmodels:mainfrom
bashtage:wip-ss-timing-converged-cov
Draft

bashtage wants to merge 2 commits into
statsmodels:mainfrom
bashtage:wip-ss-timing-converged-cov

Conversation

@bashtage

Copy link
Copy Markdown
Member

Merges cleanly with the other open kalman_filter.py PRs. #11035 also appends tests to test_kalman.py and adds a name to its kalman_filter import list: keep both names and both blocks.

With the filtered timing convention (timing_init_filtered = True), the
Kalman filter predicts the state for the period after the sample in a last
prediction step. Once the filter has converged, the prediction step no
longer computes the predicted state covariance matrix (post_convergence
copies the converged matrix into each period's storage at the start of the
period instead), so the last column of predicted_state_cov was left at
zero. For example, in a local level model with 50 observations the last two
columns were [1.1328, 0.0], and [1.1328, 1.1328] with tolerance=0. The
predicted state itself was right, and the default (predicted) timing was not
affected. Results that start from the end of the sample, such as forecasts
from the filter output, used the zero matrix.

Changes

  • In the last prediction step, a converged filter copies the converged
    predicted state covariance matrix into the last column before the
    prediction.
  • versionchanged note on FilterResults.predicted_state_cov.

Not changed

  • All other columns, the predicted states, and filters that have not
    converged.

Tests (test_kalman.py)

  • One- and two-state models, with the multivariate and the univariate
    filter: with filtered timing the filter converges before the last period,
    and the last column of predicted_state_cov equals that of the same
    filter with tolerance=0 (no convergence) and the solution of the
    discrete algebraic Riccati equation; the last predicted state is
    unchanged.

  • tests added / passed.

  • code/documentation is well formatted.

  • properly formatted commit message. See
    NumPy's guide.

AI Disclosure

Contributions must comply with the statsmodels AI Policy.

Tick exactly one of the following. If AI tools were used, replace the <...>
placeholders in the second option with the tool name(s) and how the tool's output was
used.

  • No AI tools were used to develop this pull request.
  • AI tools were used.
    Tool(s): Claude Code (Claude Opus 5.5).
    Used for: finding the bug in a coverage audit of the compiled state space code, writing the fix and its tests, and checking them (tests failing on main, mutation check with a rebuild of the extension per mutant).
    I have personally read, understood, and can explain every line of this diff, and
    have verified the statistical/numerical correctness of the change.

🤖 Generated with Claude Code

@bashtage

bashtage commented Nov 24, 2026 •

Copy link
Copy Markdown
Member Author

Merge order. These open PRs build on this one (their branches contain its commits), so please merge this PR first:

Indented PRs build on the PR above them, so merge them after it.

Once this PR is merged, their diffs shrink to their own changes. Each of them also says "Stacked on #..." at the top of its description.

Merge-order note generated with Claude Code (updated 2026-11-25).

@bashtage
bashtage force-pushed the wip-ss-timing-converged-cov branch from a40df43 to 2023e1c Compare November 24, 2026 16:17
bashtage added a commit to bashtage/statsmodels that referenced this pull request Nov 24, 2026
FilterResults.predict computes out-of-sample forecasts and dynamic
in-sample predictions by running a filter on missing data that starts
from a known initialization at the predicted state (and covariance
matrix) of its first period. The new filter copied the timing convention
of the model. With the filtered timing (timing_init_filtered=True), it
treated that predicted state as the filtered state of the period before,
so the predictions were silently one period late: for an AR(1) observed
with noise (SARIMAX(y, order=(1, 0, 0), measurement_error=True)),
get_forecast(2).predicted_mean was [0.058, 0.029] instead of
[0.117, 0.058], with the same log-likelihood. This affects forecast,
get_forecast, predict and get_prediction with dynamic prediction or
out-of-sample periods, and the forecasts that news compares.

Stacked on statsmodels#11141, which sets the predicted state covariance matrix for
the period after the sample of a converged filter with the filtered
timing; without it, the forecast variances would still be wrong.

Changes
- The filter run by FilterResults.predict always uses the predicted timing
  convention (alternate_timing=False), which matches its initialization.
- A versionchanged note in FilterResults.predict.

Not changed
- Models with the default (predicted) timing convention.
- One-step-ahead in-sample predictions, which use the stored filter
  output.

Tests (test_kalman.py)
- For an AR(1) observed with noise with the filtered timing, the
  out-of-sample forecasts and the dynamic in-sample predictions (means
  and variances) equal those with the predicted timing and the closed
  form from the last filtered state and its variance; the first two
  predictions of the dynamic case are the one-step-ahead forecasts. Both
  cases fail without the change.
bashtage added a commit to bashtage/statsmodels that referenced this pull request Nov 24, 2026
…iming

With the filtered timing convention (timing_init_filtered=True), the
compiled Kalman filter predicted the state of period t from the filtered
state of period t-1 with the time-varying transition matrix, state
intercept and selected state covariance matrix of period t
(a_t = T_t a_{t-1|t-1} + c_t), and the period after the sample with those
of the last period again. Everything else reads column t of these
matrices as the transition from period t to t+1, as with the default
(predicted) timing and as the Representation docstring says: the smoother
(L_t = T_t - K_t Z_t), FilterResults.predict and extend (statsmodels#11152, statsmodels#11162),
and the models that fill time-varying matrices. So with time-varying
matrices and the filtered timing:
- the smoothed states did not belong to the filter: the filtered states
  equalled those of the model with the matrices shifted by one period,
  its smoothed states were up to 0.146 away in a time-varying AR(1);
- VARMAX with exog raised "array must not contain infs or NaNs" (the NaN
  last column of the state intercept entered the last in-sample period);
- VARMAX with a time trend silently used the trend one period ahead.

Stacked on statsmodels#11152 (which is stacked on statsmodels#11141): the VARMAX forecast test
with the filtered timing needs its forecasting fix.

Changes
- With the filtered timing, the filter predicts period t >= 1 with the
  transition matrix, state intercept and selected state covariance matrix
  of period t-1; period 0 is predicted from the initialization with those
  of period 0, as before. A filtered-timing model is then the
  predicted-timing model with the same matrices whose initial state is the
  initialization pushed through the period 0 transition
  (a_0 = T_0 m + c_0, P_0 = T_0 P T_0' + R_0 Q_0 R_0'), for the filter, the
  smoother and prediction. This covers the conventional, univariate and
  collapsed filters, missing data and the memory conservation options,
  which share the prediction step in _kalman_filter.pyx.in; the
  Chandrasekhar recursions and exact diffuse initialization already raise
  with the filtered timing.
- VARMAXResults._set_final_predicted_state (forecasts and simulations
  with exog) runs its temporary model with the predicted timing, since its
  initial state is the predicted state of the last period (as statsmodels#11162 does
  for extend). With measurement error, the forecasts with exog under the
  filtered timing were wrong otherwise.
- Notes and a versionchanged note in KalmanFilter.set_filter_timing.

Not changed
- The predicted timing, and the filtered timing with time-invariant
  matrices: 349 output arrays (filter and smoother output and forecasts of
  time-varying generic models with each filter, SARIMAX,
  UnobservedComponents, DynamicFactor and VARMAX with exog or trends, and
  time-invariant models with the filtered timing) are bit-identical to
  those of the base.
- The prediction of the first period from the initialization.

Tests
- test_kalman.py: a model in which every system matrix is time-varying
  (conventional, univariate and collapsed filters, missing data,
  memory_conserve) and models in which only the transition, state
  intercept, state covariance or selection matrix is time-varying. With
  the filtered timing, the loglikelihood, forecasts, predicted and
  filtered states and covariances, Kalman gain, smoothed states,
  covariances and disturbances equal those of the predicted-timing model
  with the pushed-through initialization, and each predicted state and
  covariance equals the prediction step computed from the initialization
  or the previous filtered state with the previous period's matrices. A
  time-invariant case checks the same (passes before and after). The
  time-varying AR(1) of the smoother example: filtered and smoothed states
  agree with the equivalent model.
- test_varmax.py: VARMAX(1, 0) with exog and with trend="ct" under the
  filtered timing. Without measurement error, the one-step-ahead
  predictions and the forecasts equal the VAR recursion; with measurement
  error and a known initialization, the loglikelihood, fitted values,
  smoothed states and covariances, and the forecast means and variances
  equal those of the predicted-timing model with the pushed-through
  initialization.
@bashtage
bashtage force-pushed the wip-ss-timing-converged-cov branch from 2023e1c to 3c178ba Compare November 25, 2026 12:14
bashtage added a commit to bashtage/statsmodels that referenced this pull request Nov 25, 2026
FilterResults.predict computes out-of-sample forecasts and dynamic
in-sample predictions by running a filter on missing data that starts
from a known initialization at the predicted state (and covariance
matrix) of its first period. The new filter copied the timing convention
of the model. With the filtered timing (timing_init_filtered=True), it
treated that predicted state as the filtered state of the period before,
so the predictions were silently one period late: for an AR(1) observed
with noise (SARIMAX(y, order=(1, 0, 0), measurement_error=True)),
get_forecast(2).predicted_mean was [0.058, 0.029] instead of
[0.117, 0.058], with the same log-likelihood. This affects forecast,
get_forecast, predict and get_prediction with dynamic prediction or
out-of-sample periods, and the forecasts that news compares.

Stacked on statsmodels#11141, which sets the predicted state covariance matrix for
the period after the sample of a converged filter with the filtered
timing; without it, the forecast variances would still be wrong.

Changes
- The filter run by FilterResults.predict always uses the predicted timing
  convention (alternate_timing=False), which matches its initialization.
- A versionchanged note in FilterResults.predict.

Not changed
- Models with the default (predicted) timing convention.
- One-step-ahead in-sample predictions, which use the stored filter
  output.

Tests (test_kalman.py)
- For an AR(1) observed with noise with the filtered timing, the
  out-of-sample forecasts and the dynamic in-sample predictions (means
  and variances) equal those with the predicted timing and the closed
  form from the last filtered state and its variance; the first two
  predictions of the dynamic case are the one-step-ahead forecasts. Both
  cases fail without the change.
bashtage added a commit to bashtage/statsmodels that referenced this pull request Nov 25, 2026
MLEResults.extend (and VARMAXResults.extend, which does not call it)
initializes the new model with the predicted state of the first new
period, predicted_state[..., -1], but the clone keeps the timing
convention of the model. With the filtered timing convention
(timing_init_filtered=True), the new model treated that predicted state as
the filtered state of the period before, so the extended results were one
period late: SARIMAX(y, order=(1, 0, 0), measurement_error=True,
timing_init_filtered=True).smooth([.5, 1, 1]).extend([.4, .3]).fittedvalues
gave [0.058, 0.115] instead of [0.117, 0.134], the values of the model of
the whole sample (and of the predicted timing). The log-likelihood,
filtered and smoothed states of the extension were wrong too. Same class
as the forecasting fix of statsmodels#11152.

Changes
- extend sets the predicted timing convention on the new model, in
  MLEResults.extend (used by SARIMAX, UnobservedComponents, DynamicFactor,
  DynamicFactorMQ, ...) and in VARMAXResults.extend. Nothing changes with
  the predicted timing convention, the default.
- Docstrings: the new model uses the predicted timing; versionchanged
  0.15.1.

Not changed
- append and apply refilter their whole sample with the model's
  initialization (or a copy of it), so they were right with the filtered
  timing; the new tests check them.
- With the filtered timing, a filter that converged leaves the last
  predicted state covariance at zero on main (statsmodels#11141 fixes that in the
  compiled filter), so the extension of a converged filter starts from a
  zero covariance until statsmodels#11141 is merged too. The tests use tolerance=0;
  with the converged covariance the extension matches the whole sample.

Tests
- SARIMAX with each timing: the fittedvalues, llf_obs, filtered and
  smoothed states and smoothed state covariances of extend equal the last
  periods of the model of the whole sample; append (with and without
  copy_initialization) equals the model of the whole sample and apply a
  model built on the new data. The filtered-timing case fails on main.
- VARMAX with each timing: extend equals the last periods of the model of
  the whole sample. The filtered-timing case fails on main.
bashtage added a commit to bashtage/statsmodels that referenced this pull request Nov 25, 2026
…iming

With the filtered timing convention (timing_init_filtered=True), the
compiled Kalman filter predicted the state of period t from the filtered
state of period t-1 with the time-varying transition matrix, state
intercept and selected state covariance matrix of period t
(a_t = T_t a_{t-1|t-1} + c_t), and the period after the sample with those
of the last period again. Everything else reads column t of these
matrices as the transition from period t to t+1, as with the default
(predicted) timing and as the Representation docstring says: the smoother
(L_t = T_t - K_t Z_t), FilterResults.predict and extend (statsmodels#11152, statsmodels#11162),
and the models that fill time-varying matrices. So with time-varying
matrices and the filtered timing:
- the smoothed states did not belong to the filter: the filtered states
  equalled those of the model with the matrices shifted by one period,
  its smoothed states were up to 0.146 away in a time-varying AR(1);
- VARMAX with exog raised "array must not contain infs or NaNs" (the NaN
  last column of the state intercept entered the last in-sample period);
- VARMAX with a time trend silently used the trend one period ahead.

Stacked on statsmodels#11152 (which is stacked on statsmodels#11141): the VARMAX forecast test
with the filtered timing needs its forecasting fix.

Changes
- With the filtered timing, the filter predicts period t >= 1 with the
  transition matrix, state intercept and selected state covariance matrix
  of period t-1; period 0 is predicted from the initialization with those
  of period 0, as before. A filtered-timing model is then the
  predicted-timing model with the same matrices whose initial state is the
  initialization pushed through the period 0 transition
  (a_0 = T_0 m + c_0, P_0 = T_0 P T_0' + R_0 Q_0 R_0'), for the filter, the
  smoother and prediction. This covers the conventional, univariate and
  collapsed filters, missing data and the memory conservation options,
  which share the prediction step in _kalman_filter.pyx.in; the
  Chandrasekhar recursions and exact diffuse initialization already raise
  with the filtered timing.
- VARMAXResults._set_final_predicted_state (forecasts and simulations
  with exog) runs its temporary model with the predicted timing, since its
  initial state is the predicted state of the last period (as statsmodels#11162 does
  for extend). With measurement error, the forecasts with exog under the
  filtered timing were wrong otherwise.
- Notes and a versionchanged note in KalmanFilter.set_filter_timing.

Not changed
- The predicted timing, and the filtered timing with time-invariant
  matrices: 349 output arrays (filter and smoother output and forecasts of
  time-varying generic models with each filter, SARIMAX,
  UnobservedComponents, DynamicFactor and VARMAX with exog or trends, and
  time-invariant models with the filtered timing) are bit-identical to
  those of the base.
- The prediction of the first period from the initialization.

Tests
- test_kalman.py: a model in which every system matrix is time-varying
  (conventional, univariate and collapsed filters, missing data,
  memory_conserve) and models in which only the transition, state
  intercept, state covariance or selection matrix is time-varying. With
  the filtered timing, the loglikelihood, forecasts, predicted and
  filtered states and covariances, Kalman gain, smoothed states,
  covariances and disturbances equal those of the predicted-timing model
  with the pushed-through initialization, and each predicted state and
  covariance equals the prediction step computed from the initialization
  or the previous filtered state with the previous period's matrices. A
  time-invariant case checks the same (passes before and after). The
  time-varying AR(1) of the smoother example: filtered and smoothed states
  agree with the equivalent model.
- test_varmax.py: VARMAX(1, 0) with exog and with trend="ct" under the
  filtered timing. Without measurement error, the one-step-ahead
  predictions and the forecasts equal the VAR recursion; with measurement
  error and a known initialization, the loglikelihood, fitted values,
  smoothed states and covariances, and the forecast means and variances
  equal those of the predicted-timing model with the pushed-through
  initialization.
@bashtage
bashtage force-pushed the wip-ss-timing-converged-cov branch from 3c178ba to 1d1777c Compare November 25, 2026 18:39
bashtage added a commit to bashtage/statsmodels that referenced this pull request Nov 25, 2026
FilterResults.predict computes out-of-sample forecasts and dynamic
in-sample predictions by running a filter on missing data that starts
from a known initialization at the predicted state (and covariance
matrix) of its first period. The new filter copied the timing convention
of the model. With the filtered timing (timing_init_filtered=True), it
treated that predicted state as the filtered state of the period before,
so the predictions were silently one period late: for an AR(1) observed
with noise (SARIMAX(y, order=(1, 0, 0), measurement_error=True)),
get_forecast(2).predicted_mean was [0.058, 0.029] instead of
[0.117, 0.058], with the same log-likelihood. This affects forecast,
get_forecast, predict and get_prediction with dynamic prediction or
out-of-sample periods, and the forecasts that news compares.

Stacked on statsmodels#11141, which sets the predicted state covariance matrix for
the period after the sample of a converged filter with the filtered
timing; without it, the forecast variances would still be wrong.

Changes
- The filter run by FilterResults.predict always uses the predicted timing
  convention (alternate_timing=False), which matches its initialization.
- A versionchanged note in FilterResults.predict.

Not changed
- Models with the default (predicted) timing convention.
- One-step-ahead in-sample predictions, which use the stored filter
  output.

Tests (test_kalman.py)
- For an AR(1) observed with noise with the filtered timing, the
  out-of-sample forecasts and the dynamic in-sample predictions (means
  and variances) equal those with the predicted timing and the closed
  form from the last filtered state and its variance; the first two
  predictions of the dynamic case are the one-step-ahead forecasts. Both
  cases fail without the change.
bashtage added a commit to bashtage/statsmodels that referenced this pull request Nov 25, 2026
…iming

With the filtered timing convention (timing_init_filtered=True), the
compiled Kalman filter predicted the state of period t from the filtered
state of period t-1 with the time-varying transition matrix, state
intercept and selected state covariance matrix of period t
(a_t = T_t a_{t-1|t-1} + c_t), and the period after the sample with those
of the last period again. Everything else reads column t of these
matrices as the transition from period t to t+1, as with the default
(predicted) timing and as the Representation docstring says: the smoother
(L_t = T_t - K_t Z_t), FilterResults.predict and extend (statsmodels#11152, statsmodels#11162),
and the models that fill time-varying matrices. So with time-varying
matrices and the filtered timing:
- the smoothed states did not belong to the filter: the filtered states
  equalled those of the model with the matrices shifted by one period,
  its smoothed states were up to 0.146 away in a time-varying AR(1);
- VARMAX with exog raised "array must not contain infs or NaNs" (the NaN
  last column of the state intercept entered the last in-sample period);
- VARMAX with a time trend silently used the trend one period ahead.

Stacked on statsmodels#11152 (which is stacked on statsmodels#11141): the VARMAX forecast test
with the filtered timing needs its forecasting fix.

Changes
- With the filtered timing, the filter predicts period t >= 1 with the
  transition matrix, state intercept and selected state covariance matrix
  of period t-1; period 0 is predicted from the initialization with those
  of period 0, as before. A filtered-timing model is then the
  predicted-timing model with the same matrices whose initial state is the
  initialization pushed through the period 0 transition
  (a_0 = T_0 m + c_0, P_0 = T_0 P T_0' + R_0 Q_0 R_0'), for the filter, the
  smoother and prediction. This covers the conventional, univariate and
  collapsed filters, missing data and the memory conservation options,
  which share the prediction step in _kalman_filter.pyx.in; the
  Chandrasekhar recursions and exact diffuse initialization already raise
  with the filtered timing.
- VARMAXResults._set_final_predicted_state (forecasts and simulations
  with exog) runs its temporary model with the predicted timing, since its
  initial state is the predicted state of the last period (as statsmodels#11162 does
  for extend). With measurement error, the forecasts with exog under the
  filtered timing were wrong otherwise.
- Notes and a versionchanged note in KalmanFilter.set_filter_timing.

Not changed
- The predicted timing, and the filtered timing with time-invariant
  matrices: 349 output arrays (filter and smoother output and forecasts of
  time-varying generic models with each filter, SARIMAX,
  UnobservedComponents, DynamicFactor and VARMAX with exog or trends, and
  time-invariant models with the filtered timing) are bit-identical to
  those of the base.
- The prediction of the first period from the initialization.

Tests
- test_kalman.py: a model in which every system matrix is time-varying
  (conventional, univariate and collapsed filters, missing data,
  memory_conserve) and models in which only the transition, state
  intercept, state covariance or selection matrix is time-varying. With
  the filtered timing, the loglikelihood, forecasts, predicted and
  filtered states and covariances, Kalman gain, smoothed states,
  covariances and disturbances equal those of the predicted-timing model
  with the pushed-through initialization, and each predicted state and
  covariance equals the prediction step computed from the initialization
  or the previous filtered state with the previous period's matrices. A
  time-invariant case checks the same (passes before and after). The
  time-varying AR(1) of the smoother example: filtered and smoothed states
  agree with the equivalent model.
- test_varmax.py: VARMAX(1, 0) with exog and with trend="ct" under the
  filtered timing. Without measurement error, the one-step-ahead
  predictions and the forecasts equal the VAR recursion; with measurement
  error and a known initialization, the loglikelihood, fitted values,
  smoothed states and covariances, and the forecast means and variances
  equal those of the predicted-timing model with the pushed-through
  initialization.
…filter

With the filtered timing convention (`timing_init_filtered = True`), the
Kalman filter predicts the state for the period after the sample in a last
prediction step. Once the filter has converged, the prediction step no
longer computes the predicted state covariance matrix (`post_convergence`
copies the converged matrix into each period's storage at the start of the
period instead), so the last column of `predicted_state_cov` was left at
zero. For example, in a local level model with 50 observations the last two
columns were [1.1328, 0.0], and [1.1328, 1.1328] with `tolerance=0`. The
predicted state itself was right, and the default (predicted) timing was not
affected. Results that start from the end of the sample, such as forecasts
from the filter output, used the zero matrix.

Changes
- In the last prediction step, a converged filter copies the converged
  predicted state covariance matrix into the last column before the
  prediction.
- `versionchanged` note on `FilterResults.predicted_state_cov`.

Not changed
- All other columns, the predicted states, and filters that have not
  converged.

Tests (test_kalman.py)
- One- and two-state models, with the multivariate and the univariate
  filter: with filtered timing the filter converges before the last period,
  and the last column of `predicted_state_cov` equals that of the same
  filter with `tolerance=0` (no convergence) and the solution of the
  discrete algebraic Riccati equation; the last predicted state is
  unchanged.
bashtage added a commit to bashtage/statsmodels that referenced this pull request Nov 28, 2026
MLEResults.extend (and VARMAXResults.extend, which does not call it)
initializes the new model with the predicted state of the first new
period, predicted_state[..., -1], but the clone keeps the timing
convention of the model. With the filtered timing convention
(timing_init_filtered=True), the new model treated that predicted state as
the filtered state of the period before, so the extended results were one
period late: SARIMAX(y, order=(1, 0, 0), measurement_error=True,
timing_init_filtered=True).smooth([.5, 1, 1]).extend([.4, .3]).fittedvalues
gave [0.058, 0.115] instead of [0.117, 0.134], the values of the model of
the whole sample (and of the predicted timing). The log-likelihood,
filtered and smoothed states of the extension were wrong too. Same class
as the forecasting fix of statsmodels#11152.

Changes
- extend sets the predicted timing convention on the new model, in
  MLEResults.extend (used by SARIMAX, UnobservedComponents, DynamicFactor,
  DynamicFactorMQ, ...) and in VARMAXResults.extend. Nothing changes with
  the predicted timing convention, the default.
- Docstrings: the new model uses the predicted timing; versionchanged
  0.16.0.

Not changed
- append and apply refilter their whole sample with the model's
  initialization (or a copy of it), so they were right with the filtered
  timing; the new tests check them.
- With the filtered timing, a filter that converged leaves the last
  predicted state covariance at zero on main (statsmodels#11141 fixes that in the
  compiled filter), so the extension of a converged filter starts from a
  zero covariance until statsmodels#11141 is merged too. The tests use tolerance=0;
  with the converged covariance the extension matches the whole sample.

Tests
- SARIMAX with each timing: the fittedvalues, llf_obs, filtered and
  smoothed states and smoothed state covariances of extend equal the last
  periods of the model of the whole sample; append (with and without
  copy_initialization) equals the model of the whole sample and apply a
  model built on the new data. The filtered-timing case fails on main.
- VARMAX with each timing: extend equals the last periods of the model of
  the whole sample. The filtered-timing case fails on main.
@bashtage
bashtage force-pushed the wip-ss-timing-converged-cov branch from 1d1777c to 25cc021 Compare November 28, 2026 21:36
bashtage added a commit to bashtage/statsmodels that referenced this pull request Nov 28, 2026
…iming

With the filtered timing convention (timing_init_filtered=True), the
compiled Kalman filter predicted the state of period t from the filtered
state of period t-1 with the time-varying transition matrix, state
intercept and selected state covariance matrix of period t
(a_t = T_t a_{t-1|t-1} + c_t), and the period after the sample with those
of the last period again. Everything else reads column t of these
matrices as the transition from period t to t+1, as with the default
(predicted) timing and as the Representation docstring says: the smoother
(L_t = T_t - K_t Z_t), FilterResults.predict and extend (statsmodels#11152, statsmodels#11162),
and the models that fill time-varying matrices. So with time-varying
matrices and the filtered timing:
- the smoothed states did not belong to the filter: the filtered states
  equalled those of the model with the matrices shifted by one period,
  its smoothed states were up to 0.146 away in a time-varying AR(1);
- VARMAX with exog raised "array must not contain infs or NaNs" (the NaN
  last column of the state intercept entered the last in-sample period);
- VARMAX with a time trend silently used the trend one period ahead.

Stacked on statsmodels#11152 (which is stacked on statsmodels#11141): the VARMAX forecast test
with the filtered timing needs its forecasting fix.

Changes
- With the filtered timing, the filter predicts period t >= 1 with the
  transition matrix, state intercept and selected state covariance matrix
  of period t-1; period 0 is predicted from the initialization with those
  of period 0, as before. A filtered-timing model is then the
  predicted-timing model with the same matrices whose initial state is the
  initialization pushed through the period 0 transition
  (a_0 = T_0 m + c_0, P_0 = T_0 P T_0' + R_0 Q_0 R_0'), for the filter, the
  smoother and prediction. This covers the conventional, univariate and
  collapsed filters, missing data and the memory conservation options,
  which share the prediction step in _kalman_filter.pyx.in; the
  Chandrasekhar recursions and exact diffuse initialization already raise
  with the filtered timing.
- VARMAXResults._set_final_predicted_state (forecasts and simulations
  with exog) runs its temporary model with the predicted timing, since its
  initial state is the predicted state of the last period (as statsmodels#11162 does
  for extend). With measurement error, the forecasts with exog under the
  filtered timing were wrong otherwise.
- Notes and a versionchanged note in KalmanFilter.set_filter_timing.

Not changed
- The predicted timing, and the filtered timing with time-invariant
  matrices: 349 output arrays (filter and smoother output and forecasts of
  time-varying generic models with each filter, SARIMAX,
  UnobservedComponents, DynamicFactor and VARMAX with exog or trends, and
  time-invariant models with the filtered timing) are bit-identical to
  those of the base.
- The prediction of the first period from the initialization.

Tests
- test_kalman.py: a model in which every system matrix is time-varying
  (conventional, univariate and collapsed filters, missing data,
  memory_conserve) and models in which only the transition, state
  intercept, state covariance or selection matrix is time-varying. With
  the filtered timing, the loglikelihood, forecasts, predicted and
  filtered states and covariances, Kalman gain, smoothed states,
  covariances and disturbances equal those of the predicted-timing model
  with the pushed-through initialization, and each predicted state and
  covariance equals the prediction step computed from the initialization
  or the previous filtered state with the previous period's matrices. A
  time-invariant case checks the same (passes before and after). The
  time-varying AR(1) of the smoother example: filtered and smoothed states
  agree with the equivalent model.
- test_varmax.py: VARMAX(1, 0) with exog and with trend="ct" under the
  filtered timing. Without measurement error, the one-step-ahead
  predictions and the forecasts equal the VAR recursion; with measurement
  error and a known initialization, the loglikelihood, fitted values,
  smoothed states and covariances, and the forecast means and variances
  equal those of the predicted-timing model with the pushed-through
  initialization.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant