Repository navigation
BUG: Fix missing="skip" in RollingWLS and RollingOLS - #10463
Merged
Merged
Conversation
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.
5 of 6 tasks
Member
|
Thanks., |
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.
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.
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.