Skip to content

ENH: Add on-demand residuals to rolling regression results - #10462

Merged
bashtage merged 2 commits into
statsmodels:mainfrom
shan3520:enh-7521-rolling-resid
Oct 6, 2026
Merged

bashtage merged 2 commits into
statsmodels:mainfrom
shan3520:enh-7521-rolling-resid

Conversation

@shan3520

@shan3520 shan3520 commented Oct 6, 2026

Copy link
Copy Markdown
Contributor

This PR adds get_resid(out_of_sample=False) for RollingRegressionResults, per suggestion #7521 (comment) to address the "obvious cases" brought up by bashtage in 2022. Calculated on demand rather than saved as part of fit, which was the source of the concern in #6986.

The default calculates resid[t] = y[t] - x[t] @ params[t], where parameters correspond to window ending at t (i.e., including t). With out_of_sample=True, it calculates one-step-ahead residual resid[t] = y[t] - x[t] @ params[t-1], with parameters corresponding to the previous window.

Since it relies on the model design matrix, it works with from_formula and categoricals. For RollingWLS, it returns unweighted residuals (as WLS.resid). It retains index for pandas and returns NaN for observations before min_nobs and observations with missing values. To conserve memory, it does not return per-window residual matrix.

Tests were added that confirm that it behaves correctly comparing against WLS on the window with different combinations (pandas/numpy input, with/without constant, with missing observations, with/without weights). One additional test checks the expanding window version. Verified against external R reference (base R lm() per window on the first 30 rows of macrodata, for both OLS and WLS with weights 1/population) at relative tolerance 1e-8. R code is provided in the test comments. Finally, a test was added that shows that from_formula with categorical variable produces the same result as explicitly coded design matrix. All new tests fail on main.

I would be happy to change the API name if desired (e.g., adding resid property for the default case).

Closes #7521.
Xref #6986.

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: drafting the implementation and tests.
    I have personally read, understood, and can explain every line of this diff, and
    have verified the statistical/numerical correctness of the change.

Add RollingRegressionResults.get_resid, which computes one residual per
window when requested, y[t] - x[t] @ params[t] by default, or the
one-step ahead error y[t] - x[t] @ params[t-1] with out_of_sample=True.
The residuals use the model design matrix so they also work for models
created with from_formula. Test values are verified against R.

closes statsmodels#7521
xref statsmodels#6986
@bashtage

bashtage commented Oct 6, 2026

Copy link
Copy Markdown
Member

One comment and some additional testing is needed.

Please add a test test when missing="skip" to make sure the returned values are correct and have the correct shape for both values of out_of_sample

@bashtage bashtage left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

More testing and a small docstring fix.

Comment thread statsmodels/regression/rolling.py Outdated

Returns
-------
{ndarray, Series}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be something like residuals : ndarray or Series

{ } notation is reserved for literals, e.g, {"up", "down"}.

@shan3520

shan3520 commented Oct 6, 2026

Copy link
Copy Markdown
Contributor Author

The docstring has been updated to explicitly state that residuals should be : ndarray or Series. In addition, a test with missing="skip" as per your request has been implemented. This test checks for both values of out_of_sample for both pandas and numpy data input, making sure that the shape and pandas index are correct, that non-NaNs equal y - x @ params, and that NaNs occur at skipped windows/missing observations. In working on this test, I found that there is a bug where missing="skip" causes silently wrong parameters to be calculated in RollingWLS/RollingOLS due to an insufficient NaN check and outdated sums caused by skipped windows. These have been fixed separately in #10463. Therefore, this test uses get_resid vs. model fit parameters directly rather than vs. WLS.

@bashtage
bashtage merged commit 93f83fb into statsmodels:main Oct 6, 2026
13 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.

ENH: add a function to compute residuals for RollingOLS

2 participants