Repository navigation
BUG: Fix pickling of the KFS simulation smoother - #11026
Merged
Merged
Conversation
This was referenced Nov 23, 2026
| rng = np.random.default_rng(20261122) | ||
| endog = rng.standard_normal(15) + 2 | ||
| if missing: | ||
| endog[[3, 9]] = np.nan |
4 of 5 tasks
Pickling the Kalman filter and smoother (KFS) simulation smoother of any
state space model, for example `pickle.dumps(mod.simulation_smoother())`,
raised "AttributeError: '..._representation.dStatespace' object has no
attribute 'filter_method'". `{prefix}SimulationSmoother.__reduce__` read
the filter settings from the state space model (`self.model`), which does
not have them. Its argument list did not match `__init__` either: it
included `self.pretransformed_variates`, which is not an attribute, and
always passed the number of observations, which `__init__` takes as a
request for a simulate-only object (and refuses for a simulation
smoother).
Changes
- `__reduce__` takes the filter method, inversion and stability methods,
memory conservation, filter timing, tolerance and loglikelihood burn-in
from the simulated Kalman filter and the smoother output from the
simulated smoother, and passes the number of observations only for a
simulate-only object (a simulator, created with `nobs`).
- The pickled state keeps how the stored variates are interpreted
(pretransformed shocks or initial state, fixed initial state), and
`__setstate__` restores it.
- `__setstate__` points the work-array pointers at the restored arrays.
`_reinitialize_temp_pointers` existed for this but was never called, so
an unpickled object would have written the Cholesky factors to freed
memory.
Not changed
- Simulation smoothing of objects that are not pickled.
- The CFA simulation smoother, which already pickled correctly.
Tests (test_pickle.py)
- A simulation smoother of an AR(1) with an intercept and measurement
error, with and without missing data: after a round trip the stored
draws are unchanged, a new simulation with the same variates gives the
draws of the original object, and with zero variates the draws equal
the smoothed state and disturbances of the Kalman smoother.
- Non-default filter settings and output flags survive the round trip,
and the unpickled object computes the Cholesky factors of the initial
state, measurement and state variances (closed form) in its own work
arrays.
- A pickled simulator (`ssm.simulator(nobs)`) generates the closed-form
AR(1) recursion from given variates.
- The compiled object keeps the pretransformed and fixed initial state
flags: `simulate()` after a round trip matches the original for each
setting.
bashtage
force-pushed
the
wip-ss-simsmoother-pickle
branch
from
November 24, 2026 11:48
d765279 to
d7caf5b
Compare
bashtage
marked this pull request as ready for review
November 27, 2026 21:44
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.
Pickling the Kalman filter and smoother (KFS) simulation smoother of any
state space model, for example
pickle.dumps(mod.simulation_smoother()),raised "AttributeError: '..._representation.dStatespace' object has no
attribute 'filter_method'".
{prefix}SimulationSmoother.__reduce__readthe filter settings from the state space model (
self.model), which doesnot have them. Its argument list did not match
__init__either: itincluded
self.pretransformed_variates, which is not an attribute, andalways passed the number of observations, which
__init__takes as arequest for a simulate-only object (and refuses for a simulation
smoother).
Changes
__reduce__takes the filter method, inversion and stability methods,memory conservation, filter timing, tolerance and loglikelihood burn-in
from the simulated Kalman filter and the smoother output from the
simulated smoother, and passes the number of observations only for a
simulate-only object (a simulator, created with
nobs).(pretransformed shocks or initial state, fixed initial state), and
__setstate__restores it.__setstate__points the work-array pointers at the restored arrays._reinitialize_temp_pointersexisted for this but was never called, soan unpickled object would have written the Cholesky factors to freed
memory.
Not changed
Tests (test_pickle.py)
A simulation smoother of an AR(1) with an intercept and measurement
error, with and without missing data: after a round trip the stored
draws are unchanged, a new simulation with the same variates gives the
draws of the original object, and with zero variates the draws equal
the smoothed state and disturbances of the Kalman smoother.
Non-default filter settings and output flags survive the round trip,
and the unpickled object computes the Cholesky factors of the initial
state, measurement and state variances (closed form) in its own work
arrays.
A pickled simulator (
ssm.simulator(nobs)) generates the closed-formAR(1) recursion from given variates.
The compiled object keeps the pretransformed and fixed initial state
flags:
simulate()after a round trip matches the original for eachsetting.
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 contract audit, writing the fix and its tests, and checking them (tests failing on main, 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