Skip to content

BUG: Fix simulator overread with missing data and short time-varying runs - #11138

Draft
bashtage wants to merge 3 commits into
statsmodels:mainfrom
bashtage:wip-ss-simulator-obs-bounds
Draft

bashtage wants to merge 3 commits into
statsmodels:mainfrom
bashtage:wip-ss-simulator-obs-bounds

Conversation

@bashtage

Copy link
Copy Markdown
Member

Changes the constructor of the compiled simulation smoother; merges cleanly with #11026, which changes its pickling in the same template. Other open statespace PRs also append tests at the end of the same test files: keep both appended blocks.

Two bugs in the constructor of the compiled simulation smoother
(_simulation_smoother.pyx.in), both reached through ssm.simulator(n)
and ssm.simulate(n) when n differs from the number of observations.

  • With missing data, the constructor copied n * k_endog values of the
    model's observations into a second simulated model (used only for
    simulation smoothing). For n > nobs, which a time-invariant model
    allows, this read past the end of the observations: on a model with 10
    observations, one of them NaN, ssm.simulator(10**6) crashed the
    interpreter (access violation on Windows; 6 of 6 runs) and smaller
    lengths read garbage that was not used.
  • For n < nobs in a time-varying model, the simulated models were built
    from the full-length system matrices, so ssm.simulator(n) and
    ssm.simulate(n) raised "Invalid time-varying dimension for design
    matrix: requires 1 or n, got nobs", although KalmanFilter.simulate
    documents that a time-varying model can simulate up to nobs periods.
    MLEModel.simulate was not affected (it builds a model of the
    simulation length with ssm.extend).

Changes

  • Copy only min(n, nobs) periods of the observations into the second
    simulated model.
  • Build both simulated models from the first n periods of the
    time-varying matrices (views of the model's matrices, as before; a
    time-invariant matrix keeps its single period).

Not changed

  • Simulation smoothing (n equal to the number of observations) and the
    generated series, which never used the copied observations.

Tests (test_simulate.py)

  • A time-varying model (TVSS, every system matrix time-varying), with and
    without missing data: ssm.simulate(5) on 10 periods equals the
    simulation of a model built on the first 5 periods and the closed-form
    recursion, for given shocks and initial state.

  • A time-invariant model with and without missing data: ssm.simulate(25)
    on 10 periods equals the simulation of the model extended to 25 periods
    (ssm.extend) and the closed-form recursion.

  • ssm.simulate(10**6) on 10 periods with a NaN runs in a subprocess, so
    that the crash fails the test instead of stopping pytest.

  • 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

…runs

Two bugs in the constructor of the compiled simulation smoother
(`_simulation_smoother.pyx.in`), both reached through `ssm.simulator(n)`
and `ssm.simulate(n)` when `n` differs from the number of observations.

- With missing data, the constructor copied `n * k_endog` values of the
  model's observations into a second simulated model (used only for
  simulation smoothing). For `n > nobs`, which a time-invariant model
  allows, this read past the end of the observations: on a model with 10
  observations, one of them NaN, `ssm.simulator(10**6)` crashed the
  interpreter (access violation on Windows; 6 of 6 runs) and smaller
  lengths read garbage that was not used.
- For `n < nobs` in a time-varying model, the simulated models were built
  from the full-length system matrices, so `ssm.simulator(n)` and
  `ssm.simulate(n)` raised "Invalid time-varying dimension for design
  matrix: requires 1 or n, got nobs", although `KalmanFilter.simulate`
  documents that a time-varying model can simulate up to `nobs` periods.
  `MLEModel.simulate` was not affected (it builds a model of the
  simulation length with `ssm.extend`).

Changes
- Copy only `min(n, nobs)` periods of the observations into the second
  simulated model.
- Build both simulated models from the first `n` periods of the
  time-varying matrices (views of the model's matrices, as before; a
  time-invariant matrix keeps its single period).

Not changed
- Simulation smoothing (`n` equal to the number of observations) and the
  generated series, which never used the copied observations.

Tests (test_simulate.py)
- A time-varying model (TVSS, every system matrix time-varying), with and
  without missing data: `ssm.simulate(5)` on 10 periods equals the
  simulation of a model built on the first 5 periods and the closed-form
  recursion, for given shocks and initial state.
- A time-invariant model with and without missing data: `ssm.simulate(25)`
  on 10 periods equals the simulation of the model extended to 25 periods
  (`ssm.extend`) and the closed-form recursion.
- `ssm.simulate(10**6)` on 10 periods with a NaN runs in a subprocess, so
  that the crash fails the test instead of stopping pytest.
Cython 3.3.1 (the CI version) refuses _simulation_smoother.pyx with "C
struct/union member cannot be a memory view": the list comprehension
iterated over a tuple of the model's memoryview attributes, which Cython
3.3 types as a C tuple. Every job that builds statsmodels failed (wheels,
docs, Pyodide, API coverage). Convert each matrix with np.asarray in a
list literal instead, the same matrices in the same order, which Cython
3.2.9 and 3.3.1 both compile.
@bashtage
bashtage force-pushed the wip-ss-simulator-obs-bounds branch from 22d39c7 to 1fb4f4e Compare November 28, 2026 22:06
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