Repository navigation
Conversation
MLEResults.simulate(..., anchor=..., initial_state=z, pretransformed_initial_state=False) transformed the standard Normal draws z with the mean and covariance matrix of the initialization of the model, also when the anchor was after the start of the sample. Without initial_state, the initial state is drawn from the predicted state mean and covariance matrix at the anchor, so the standardized draws did not give draws from that distribution: for an AR(1) observed with noise (SARIMAX(y, order=(1, 0, 0), measurement_error=True)), res.simulate(2, anchor="end", initial_state=[0], pretransformed_initial_state=False) with zero shocks gave [0, 0] instead of the forecasts [0.081, 0.041]. The docstring said only that the draws "must be transformed using the `initial_state_cov` covariance matrix", and that initial_state must be given for an anchor other than "start" or 0, although an unspecified one is drawn from the predicted state. Changes - With an anchor after the start of the sample, MLEResults.simulate transforms the draws into mean + L z, with the predicted state mean and covariance matrix at the anchor and L the lower triangular Cholesky factor (the factor the simulator uses for the initialization), and passes the result on as a pretransformed initial state. A pivot that is not positive gives a zero column, so that singular covariance matrices, common at an anchor (states that are known exactly, such as the lagged values of an AR(2) or the level of an ARIMA(p, 1, q) without simple differencing), give a factor with L L' equal to the matrix. Draws with one column per repetition are transformed column by column. - Docstrings of MLEResults.simulate: initial_state is the state of the first simulated period, may have one column per repetition, and an unspecified one is drawn from the predicted state at the anchor; pretransformed_initial_state says which mean and covariance matrix are used (versionchanged 0.16.0). MLEModel.simulate: pretransformed_initial_state uses the initialization of the model, also with a later anchor. Not changed - anchor="start" (or 0): the draws are transformed with the initialization of the model, as before (with the filtered timing this is not the predicted state of the first period). - MLEModel.simulate, the model method, which has no predicted states, keeps using the initialization of the model for any anchor; its docstring now says so. - Pretransformed initial states, and initial_state=None. Tests (test_simulate.py) - AR(2) observed with noise, anchors "end", 1 and -3: with zero draws and zero shocks the simulation is the path from the predicted state at the anchor (and the forecasts for "end"); with draws z it is the path from the predicted mean plus the Cholesky factor (numpy) of the predicted covariance matrix times z, also for three repetitions with a column of draws each; a pretransformed initial state is used as it is. Fail on main. - anchor "start" and 0, with the predicted and the filtered timing and a known initialization, and the model method with anchor="end": the path from the initialization mean plus its Cholesky factor times z (unchanged). - A model whose first state is always 1: the predicted state covariance matrix at the end is diag(0, 2), so the draw of the first state is ignored and that of the second is scaled by sqrt(2). Fails on main.
The tests of the standardized initial state at the anchor and at the start of the sample use an AR(3) observed with noise instead of an AR(2), so that the Cholesky factor of the predicted state covariance matrix has a column below the diagonal that depends on the previous columns (with two states, its computation was not checked).
bashtage
force-pushed
the
wip-ss-simulate-anchor-initial-state
branch
from
November 28, 2026 21:36
0a5c209 to
a81e27b
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.
Implements decision 77 / 77b (i): with pretransformed_initial_state=False and an anchor after the start of the sample, MLEResults.simulate transforms the standardized initial_state with the predicted state mean and covariance matrix at the anchor (the moments that initial_state=None draws from), so that zero draws and zero shocks give the forecasts; anchor="start" and MLEModel.simulate (no predicted states) keep the initialization of the model, now documented. The Cholesky factor gives a zero column for a non-positive pivot, since the predicted covariance matrix at an anchor is often singular. mlemodel.py merges cleanly with every open PR that changes it (#10940, #11029, #11043, #11055-#11064, #11104, #11114, #11115, #11130, #11147, #11156, #11162, #11169, #11185); test_simulate.py conflicts with #11138 only where both append tests at the end of the file: keep both blocks.
BUG: Transform a standardized initial state at the simulation anchor
MLEResults.simulate(..., anchor=..., initial_state=z,
pretransformed_initial_state=False) transformed the standard Normal draws
z with the mean and covariance matrix of the initialization of the model,
also when the anchor was after the start of the sample. Without
initial_state, the initial state is drawn from the predicted state mean
and covariance matrix at the anchor, so the standardized draws did not
give draws from that distribution: for an AR(1) observed with noise
(SARIMAX(y, order=(1, 0, 0), measurement_error=True)),
res.simulate(2, anchor="end", initial_state=[0],
pretransformed_initial_state=False) with zero shocks gave [0, 0] instead
of the forecasts [0.081, 0.041]. The docstring said only that the draws
"must be transformed using the
initial_state_covcovariance matrix",and that initial_state must be given for an anchor other than "start" or
0, although an unspecified one is drawn from the predicted state.
Changes
transforms the draws into mean + L z, with the predicted state mean and
covariance matrix at the anchor and L the lower triangular Cholesky
factor (the factor the simulator uses for the initialization), and
passes the result on as a pretransformed initial state. A pivot that is
not positive gives a zero column, so that singular covariance matrices,
common at an anchor (states that are known exactly, such as the lagged
values of an AR(2) or the level of an ARIMA(p, 1, q) without simple
differencing), give a factor with L L' equal to the matrix. Draws with
one column per repetition are transformed column by column.
first simulated period, may have one column per repetition, and an
unspecified one is drawn from the predicted state at the anchor;
pretransformed_initial_state says which mean and covariance matrix are
used (versionchanged 0.16.0). MLEModel.simulate:
pretransformed_initial_state uses the initialization of the model, also
with a later anchor.
Not changed
initialization of the model, as before (with the filtered timing this
is not the predicted state of the first period).
keeps using the initialization of the model for any anchor; its
docstring now says so.
Tests (test_simulate.py)
zero shocks the simulation is the path from the predicted state at the
anchor (and the forecasts for "end"); with draws z it is the path from
the predicted mean plus the Cholesky factor (numpy) of the predicted
covariance matrix times z, also for three repetitions with a column of
draws each; a pretransformed initial state is used as it is. Fail on
main.
known initialization, and the model method with anchor="end": the path
from the initialization mean plus its Cholesky factor times z
(unchanged).
matrix at the end is diag(0, 2), so the draw of the first state is
ignored and that of the second is scaled by sqrt(2). Fails on main.
TST: Simulate from a standardized initial state with three states
The tests of the standardized initial state at the anchor and at the
start of the sample use an AR(3) observed with noise instead of an AR(2),
so that the Cholesky factor of the predicted state covariance matrix has
a column below the diagonal that depends on the previous columns (with
two states, its computation was not checked).
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: implementing the maintainer's decision, writing the tests against independent values (state space recursions from the transformed initial state, forecasts), 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