Repository navigation
Conversation
This was referenced Nov 24, 2026
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
force-pushed
the
wip-mlemodel-extend-filtered-timing
branch
from
November 25, 2026 12:14
675bb96 to
aeea3f5
Compare
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
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.
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
force-pushed
the
wip-mlemodel-extend-filtered-timing
branch
from
November 28, 2026 21:36
aeea3f5 to
31c06a3
Compare
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.
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.
Same class as #11152 (forecasts with the filtered timing), and independent of it. With the filtered timing, a filter that converged leaves its last predicted state covariance at zero on main (#11141), and extend starts from it: the extension of a converged filter is right only with #11141 as well (checked with the converged covariance). mlemodel.py, varmax.py and test_forecasting.py merge cleanly with every open PR that changes them (#10940, #11029, #11043, #11055-#11064, #11083, #11085-#11089, #11103, #11104, #11114, #11115, #11121, #11130, #11133, #11147, #11156, #11157).
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 #11152.
Changes
MLEResults.extend (used by SARIMAX, UnobservedComponents, DynamicFactor,
DynamicFactorMQ, ...) and in VARMAXResults.extend. Nothing changes with
the predicted timing convention, the default.
0.16.0.
Not changed
initialization (or a copy of it), so they were right with the filtered
timing; the new tests check them.
predicted state covariance at zero on main (BUG: Set the last predicted state cov of a converged filtered-timing filter #11141 fixes that in the
compiled filter), so the extension of a converged filter starts from a
zero covariance until BUG: Set the last predicted state cov of a converged filtered-timing filter #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.
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: finding the bug in a coverage audit of the time series code, writing the fix and its tests, and checking them (tests failing on the base, mutation check).
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