Skip to content

BUG: Fix pickling of the KFS simulation smoother - #11026

Merged
bashtage merged 3 commits into
statsmodels:mainfrom
bashtage:wip-ss-simsmoother-pickle
Nov 27, 2026
Merged

bashtage merged 3 commits into
statsmodels:mainfrom
bashtage:wip-ss-simsmoother-pickle

Conversation

@bashtage

Copy link
Copy Markdown
Member

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.

  • 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 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

rng = np.random.default_rng(20261122)
endog = rng.standard_normal(15) + 2
if missing:
endog[[3, 9]] = np.nan
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
bashtage force-pushed the wip-ss-simsmoother-pickle branch from d765279 to d7caf5b Compare November 24, 2026 11:48
@bashtage
bashtage marked this pull request as ready for review November 27, 2026 21:44
@bashtage
bashtage merged commit 8619b0c into statsmodels:main Nov 27, 2026
10 of 11 checks passed
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.

2 participants