Repository navigation
Conversation
4 of 5 tasks
…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
force-pushed
the
wip-ss-simulator-obs-bounds
branch
from
November 28, 2026 22:06
22d39c7 to
1fb4f4e
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.
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 throughssm.simulator(n)and
ssm.simulate(n)whenndiffers from the number of observations.n * k_endogvalues of themodel's observations into a second simulated model (used only for
simulation smoothing). For
n > nobs, which a time-invariant modelallows, this read past the end of the observations: on a model with 10
observations, one of them NaN,
ssm.simulator(10**6)crashed theinterpreter (access violation on Windows; 6 of 6 runs) and smaller
lengths read garbage that was not used.
n < nobsin a time-varying model, the simulated models were builtfrom the full-length system matrices, so
ssm.simulator(n)andssm.simulate(n)raised "Invalid time-varying dimension for designmatrix: requires 1 or n, got nobs", although
KalmanFilter.simulatedocuments that a time-varying model can simulate up to
nobsperiods.MLEModel.simulatewas not affected (it builds a model of thesimulation length with
ssm.extend).Changes
min(n, nobs)periods of the observations into the secondsimulated model.
nperiods of thetime-varying matrices (views of the model's matrices, as before; a
time-invariant matrix keeps its single period).
Not changed
nequal to the number of observations) and thegenerated 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 thesimulation 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, sothat 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.
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