Skip to content
svgrecursive-residual

Recursive residuals (CUSUM)

Are a regression's coefficients stable over time, or does the fit drift?

Demo data · seed 20261101 · 77 rows · default settings · 640 × 360SVG, lightSVG, darkDemo CSV

How to read it

  1. Read the line as the cumulative sum of recursive residuals in time order.
  2. A stable regression keeps it near zero, inside the bounds.
  3. Touching a bound suggests the coefficients have moved.
  1. Callout 1: Reaches the bound: coefficients are unstable.
  2. Callout 2: The upper 5% bound.
  3. Callout 3: Near zero: the fit is stable early on.

Watch out

  • It's better at detecting a coefficient shift than a change in variance, and needs rows in time order.

Defaults

No settings: the chart draws one way.

Pinned in the code

  • Draws the engine's numbers as given (precomputed by the Python or R analysis); the chart computes nothing inferential.
  • Bounds are ±a·√(n−k)·(1 + 2t/(n−k)) with a = 0.948 at 5% (Brown, Durbin & Evans 1975) when the engine publishes the CUSUM; points beyond them are marked.
# Recursive residuals (CUSUM) — Codesplash Chart Lab (recipe "recursive-residual")
# controls: defaults
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

try:
    import codesplash as cs  # optional: the app's chart theme
except ImportError:
    cs = None
if cs:
    cs.use_codesplash(dark=False)
PALETTE = cs.palette(dark=False)["categorical"] if cs else plt.rcParams["axes.prop_cycle"].by_key()["color"]

df = pd.read_csv("recursive-residual-demo.csv", keep_default_na=False, na_values=[""])  # demo data (only empty cells are missing): Chart Lab → Code → Download demo CSV

_tokens = cs.palette(dark=False) if cs else {}
SEQ = _tokens.get("sequential", ["#eff6fd", "#1a469d"])  # sequential ramp (low → high)
DIV = _tokens.get("diverging", ["#c13e2e", "#f3f5f8", "#0859ac"])  # negative, midpoint, positive
REF = _tokens.get("reference", "#4e5661")
GREY = _tokens.get("dim", "#ced1d6")
I = "quarter"
V = "CUSUM"
BL = "lower"
BH = "upper"
d = df.dropna(subset=[I, V]).sort_values(I)
fig, ax = plt.subplots(figsize=(8, 4.5))
ax.axhline(0, color=GREY, linewidth=0.8)
for b in (BL, BH):
    if b:
        ax.plot(d[I], d[b], color="#c13e2e", linestyle="--", linewidth=1)
ax.plot(d[I], d[V], color=plt.rcParams["text.color"], linewidth=1.6)
if BL and BH:
    out = d[(d[V] > d[BH]) | (d[V] < d[BL])]
    ax.scatter(out[I], out[V], s=16, color="#c13e2e", zorder=3)
ax.set_xlabel(I)
ax.set_ylabel(V)
plt.show()

Needs pandas, numpy, matplotlib · reads recursive-residual-demo.csv · app theme from the optional codesplash helper