DPredict · Guide test 238
MOSUM parameter stability test
Tests regression stability using moving sums of residuals or estimating functions.
Do it in CodeSplash
In the app, set it up in Analyze with this method:
- Structural change in a regression — Did the relationship between the outcome and its predictors change over time — at a known date, at an unknown date, or gradually?
In Structural change in a regression, choose Test = OLS-MOSUM (moving sums).
Do it in CodeSplash Run this test on your own data, in Python or R.
Bench
Change the design, drag the data, move α. The statistic, its reference distribution and the verdict follow.
Bench · OLS-MOSUM test
Adds curve × (x − 5)².
Error SD grows like exp(h · (x − 5)/5).
Errors in collection order carry φ × the previous error.
Your data
Simulated with a known ρIf H₀ were true
- The reference: how M varies across samples when H₀ is true, simulated with these x: 499 sets of N(0, 1) errors (Monte Carlo with the regressors held fixed (499 draws on the shared stream)).
- Your statistic, from the data above.
- The p-value: the area at least as extreme as yours (p = .058).
- The hatched rejection region at α = .050: statistics landing here reject H₀.
The formula
With your numbers
- OLS residuals of the full-sample fit, in time order
- the window as a fraction of the sample
The verdict
M = 1.14, p = .058
p > α = .050: compatible with H₀
A moving window reacts to local departures, such as a temporary shift, that cumulative sums average away; the window h is a choice to state.
- Statistic
- 1.143
- p-value
- 0.0580
OLS-MOSUM test on 40 (x, y) pairs: M = 1.14, p = .058. The p-value is above α = .050, so the data are compatible with H₀.
Question and hypotheses
H0: regression parameters remain stable.
Assumptions
Suitable regression residual process and weak-dependence conditions; a prespecified window fraction h and valid boundary calibration.
Formula
Parameters and notation
h controls window width; OLS and recursive MOSUM versions use different residuals and limit processes.
Structure of the method
Compute residual or score sums in a moving window along the ordered sample, standardize, and evaluate a global functional such as the largest absolute moving sum. Window overlap creates strong dependence among plotted values, so their number is not the number of independent tests.
Estimated and nuisance parameters
Estimate the regression and relevant residual covariance/scale; the moving-window process is derived from those fitted quantities. The window width is not a learned coefficient. OLS, recursive, and score-based variants use different fitted inputs and reference processes even when their plots have similar shapes.
Settings and design choices
Choose window fraction/length, ordering, process type, tested parameter directions, and covariance treatment. Short windows emphasize brief changes but can be noisy; long windows smooth and may dilute temporary changes. The h=.2 demonstration is one configuration, not a universally optimal detection bandwidth.
Fitting and adaptation
Adapt to a different time resolution by redefining a substantively meaningful window and recalibrating that exact process. A bootstrap should preserve the null dependence and refit the regression. If window width is selected from several choices, include that selection in the calibration rather than reporting the most favorable unadjusted p value.
Sensitivity and robustness
Check endpoint coverage, transient shifts, persistent drift, heteroskedasticity, serial correlation, and leverage. Apparent multiple peaks often arise from overlapping windows around one change. Nonrejection at one bandwidth does not rule out instability at another timescale, but exploratory sensitivity should be separated from confirmatory testing.
How to use and calibrate
Use a simultaneous maximum moving-sum reference matching h and residual type.
Example application
Detect a local or temporary model change that a cumulative sum might dilute.
Interpretation and pitfalls
Watch out
Trying many window widths and selecting the smallest p value requires adjustment.
What changed in edition 2
- New sections: structure of the method, estimated and nuisance parameters, settings and design choices, fitting and adaptation, sensitivity and robustness.
Python and R
Chow fixes a breakpoint at observation 50; supF searches the trimmed range and uses search-adjusted calibration. CUSUM/MOSUM/score fluctuation tests assess parameter constancy in observation order; MOSUM h is window fraction.
Not run — Executed end to end through Rscript · needs Python standard library + Rscript; R packages: strucchange
# Python launcher for the complete R implementation below.
# Requires Rscript on PATH and the listed R packages.
import shutil
import subprocess
rscript = shutil.which("Rscript")
if rscript is None:
raise RuntimeError("Install R and make Rscript available on PATH.")
r_code = r"""
set.seed(2026)
n <- 100; x <- rnorm(n); z <- rnorm(n); y <- 1+.5*x+.3*z+rnorm(n)
d <- data.frame(y,x,z); fit <- lm(y~x+z,data=d); reduced <- lm(y~x,data=d)
print(strucchange::sctest(strucchange::efp(y~x,data=d,type="OLS-MOSUM",h=.2)))
"""
# No shell interpolation. A failed R execution raises an error in Python.
result = subprocess.run([rscript, "--vanilla", "-"], input=r_code,
text=True, capture_output=True)
print(result.stdout)
if result.stderr:
print(result.stderr)
result.check_returncode()
Validated — Executed successfully in the validation environment · needs strucchange
set.seed(2026)
n <- 100; x <- rnorm(n); z <- rnorm(n); y <- 1+.5*x+.3*z+rnorm(n)
d <- data.frame(y,x,z); fit <- lm(y~x+z,data=d); reduced <- lm(y~x,data=d)
print(strucchange::sctest(strucchange::efp(y~x,data=d,type="OLS-MOSUM",h=.2)))
Output
OLS-based MOSUM test data: strucchange::efp(y ~ x, data = d, type = "OLS-MOSUM", h = 0.2) M0 = 0.51309, p-value = 0.6206
Sources
- strucchange — strucchange · efpAuthor documentation/source reviewed: strucchange 1.6-0.search.r-project.org
- efpsearch.r-project.org
- sctestsearch.r-project.org
- Statistics guide v4 (historical):
- Python: Executed end to end through Rscript · R: Executed successfully in the validation environment
- Reference's record:
- Edition 2 review: Python: skip · R: not executed (Rscript unavailable); why not run: Python: Python-to-R bridge; Rscript is unavailable. R code was not executed. Python: Not run in the reference's review: Python-to-R bridge; Rscript is unavailable. R code was not executed. R: Reviewed but not run: R wasn't available to the reference's authors.
From the CodeSplash statistics guide (v4), entry 238, updated from the reference's edition 2 (reviewed 14 November 2026) as STAT238.