Repository navigation
Conversation
4 of 5 tasks
bashtage
force-pushed
the
wip-ss-filtered-timing-tv-matrices
branch
3 times, most recently
from
November 25, 2026 18:41
9304ff2 to
b7db43a
Compare
…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.
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.
…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
force-pushed
the
wip-ss-filtered-timing-tv-matrices
branch
from
November 28, 2026 21:36
b7db43a to
547847f
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Stacked on #11152, which is stacked on #11141: merge those first (this PR adds one commit): the VARMAX forecast test with the filtered timing needs its forecasting fix. Implements decision 79 (a): with the filtered timing, the compiled Kalman filter predicts period t >= 1 with the time-varying matrices of period t - 1 (period 0 with those of period 0), so that filter, smoother, prediction and the models (VARMAX) agree; VARMAX's temporary model for the last predicted state uses the predicted timing, as #11162 does for extend. Time-invariant models and the predicted timing are unchanged (349 output arrays bit-identical). The source files merge cleanly with every open PR that changes them; test_varmax.py conflicts with #11083, #11085-#11089, #11103, #11121, #11133, #11157, #11167 and #11169, and test_kalman.py with #11035 (as #11141 does), only where both append tests at the end of the file: keep both blocks.
BUG: Set the last predicted state cov of a converged filtered-timing filter
With the filtered timing convention (
timing_init_filtered = True), theKalman 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_convergencecopies the converged matrix into each period's storage at the start of the
period instead), so the last column of
predicted_state_covwas left atzero. 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. Thepredicted 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
predicted state covariance matrix into the last column before the
prediction.
versionchangednote onFilterResults.predicted_state_cov.Not changed
converged.
Tests (test_kalman.py)
filter: with filtered timing the filter converges before the last period,
and the last column of
predicted_state_covequals that of the samefilter with
tolerance=0(no convergence) and the solution of thediscrete algebraic Riccati equation; the last predicted state is
unchanged.
BUG: Predict with the predicted timing from a predicted state
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 #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
convention (alternate_timing=False), which matches its initialization.
Not changed
output.
Tests (test_kalman.py)
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.
BUG: Predict with the previous period's matrices under the filtered timing
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 (#11152, #11162),
and the models that fill time-varying matrices. So with time-varying
matrices and the filtered timing:
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);
last column of the state intercept entered the last in-sample period);
Stacked on #11152 (which is stacked on #11141): the VARMAX forecast test
with the filtered timing needs its forecasting fix.
Changes
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.
with exog) runs its temporary model with the predicted timing, since its
initial state is the predicted state of the last period (as BUG: Extend state space results with the predicted timing #11162 does
for extend). With measurement error, the forecasts with exog under the
filtered timing were wrong otherwise.
Not changed
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.
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.
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.
Tool(s): Claude Code (Claude Opus 5.5).
Used for: implementing the maintainer's decision in the compiled filter, writing the tests against an equivalent model and closed forms, and checking them (tests failing on the base, mutation check with a rebuild per mutant, bit-identical output for the unchanged cases).
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