Skip to content

BUG: Fix missing="skip" in RollingWLS and RollingOLS - #10463

Merged
bashtage merged 2 commits into
statsmodels:mainfrom
shan3520:bug-rolling-skip
Oct 6, 2026
Merged

bashtage merged 2 commits into
statsmodels:mainfrom
shan3520:bug-rolling-skip

Conversation

@shan3520

@shan3520 shan3520 commented Oct 6, 2026

Copy link
Copy Markdown
Contributor

This PR addresses two major issues in RollingWLS and RollingOLS models, which resulted in silently returning incorrect parameter estimates when missing="skip".

The Bugs

Incomplete NaN check: In order to detect missing values, _find_nans calculates a cumulative sum. However, it did it by subtracting the value window - 1 steps back instead of window steps back. This way it examined only the last window - 1 observations of the window. Thus, if a window had NaN strictly in its first observation, the skip logic was not applied and the window was estimated.

Stale sums after skipped windows: The rolling fit loop iteratively builds the $X'X$ and $X'Y$ matrices by dropping the oldest row and adding the newest. When a window was skipped, the loop advanced without updating those sums. The first valid window after a skipped window calculated its parameters based on outdated sums of previous block. That resulted in silently wrong parameters that would persist till the next reset (fit.reset option, which by default happens only at the last observation).

Impact

The current default missing="drop" mode is unaffected. However, if a user chose missing="skip", then the model returned wrong parameter estimates without throwing any errors or warnings. I have verified 1,108 windows using WLS fitted on each window separately (tested both fixed/expanding windows and weighted/unweighted data); precisely 554 windows were wrong on main, and 0 windows are wrong with this fix.

The Fix

The missing value counter properly calculates the full window by subtracting window steps back now. Also, after a skipped window, the fit loop recalculates the sums from scratch for the next valid window.

Tests and Context

The issue was discovered while I was working on the missing="skip" test requested by bashtage in #10462; currently, there is no related issue for this. The test suite did not detect the problem as with 0% missing data no window had NaN, and with ~10% missing data all windows had one, so the case of estimating windows after skipped ones was never tested.

I have added two new tests (9 test cases with the parametrization), which all fail on main and pass with this fix:

  • test_has_nan_first_obs_in_window: Makes sure that windows are correctly considered as missing if there is a NaN in the first observation.

  • test_skip_against_wls: Makes sure that parameters match per-window WLS and that NaN is returned for missing windows in various setups.

  • closes #xxxx

  • 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: drafting the fix 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.

The check for missing values in each window omitted the first
observation of the window, so a window was not skipped when only its
first observation was missing. The fit loop did not update the cross
products while windows were skipped, so the windows after a skipped
block were estimated using the wrong observations until the next
reset. Count the missing values in the full window and recompute the
cross products after skipped windows.
@bashtage

bashtage commented Oct 6, 2026

Copy link
Copy Markdown
Member

Thanks.,

@bashtage
bashtage merged commit 7af4ed7 into statsmodels:main Oct 6, 2026
8 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