svgrecursive-residual
Recursive residuals (CUSUM)
Are a regression's coefficients stable over time, or does the fit drift?
How to read it
- Read the line as the cumulative sum of recursive residuals in time order.
- A stable regression keeps it near zero, inside the bounds.
- Touching a bound suggests the coefficients have moved.
- Callout 1: Reaches the bound: coefficients are unstable.
- Callout 2: The upper 5% bound.
- 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
# Recursive residuals (CUSUM) — Codesplash Chart Lab (recipe "recursive-residual")
# controls: defaults
import numpy as np
import pandas as pd
import plotly.express as px
import plotly.graph_objects as go
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 px.colors.qualitative.Plotly
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 = go.Figure()
for b in (BL, BH):
if b:
fig.add_scatter(x=d[I], y=d[b], mode="lines", line=dict(color="#c13e2e", dash="dash"), name="5% bound")
fig.add_scatter(x=d[I], y=d[V], mode="lines", line=dict(color=REF, width=2), name=V)
fig.update_layout(xaxis_title=I, yaxis_title=V)
fig.show()Needs pandas, numpy, plotly · reads recursive-residual-demo.csv · app theme from the optional codesplash helper
# Recursive residuals (CUSUM) — Codesplash Chart Lab (recipe "recursive-residual")
# controls: defaults
library(ggplot2)
has_cs <- isTRUE(requireNamespace("codesplashr", quietly = TRUE)) # optional: the app's chart theme
theme_cs <- if (has_cs) codesplashr::theme_codesplash(dark = FALSE) else theme_minimal()
cs_cat <- if (has_cs) codesplashr::cs_palette(dark = FALSE)$categorical else grDevices::palette.colors(8, "Okabe-Ito")
if (has_cs) update_geom_defaults("text", list(colour = codesplashr::cs_palette(dark = FALSE)$foreground))
df <- read.csv("recursive-residual-demo.csv", check.names = FALSE, stringsAsFactors = FALSE, na.strings = "") # empty cells are missing; Chart Lab → Code → Download demo CSV
tok <- if (has_cs) codesplashr::cs_palette(dark = FALSE) else list()
seq_ramp <- if (is.null(tok$sequential)) c("#eff6fd", "#1a469d") else tok$sequential # low → high
div_ramp <- if (is.null(tok$diverging)) c("#c13e2e", "#f3f5f8", "#0859ac") else tok$diverging # −, 0, +
ref_col <- if (is.null(tok$reference)) "#4e5661" else tok$reference
grey_col <- if (is.null(tok$dim)) "#ced1d6" else tok$dim
i_col <- "quarter"
v_col <- "CUSUM"
bl_col <- "lower"
bh_col <- "upper"
d <- df[!is.na(df[[i_col]]) & !is.na(df[[v_col]]), ]
d <- data.frame(i = d[[i_col]], v = d[[v_col]], lo = if (is.null(bl_col)) NA_real_ else d[[bl_col]], hi = if (is.null(bh_col)) NA_real_ else d[[bh_col]])
d$out <- (!is.na(d$hi) & d$v > d$hi) | (!is.na(d$lo) & d$v < d$lo)
p <- ggplot(d, aes(i, v)) + geom_hline(yintercept = 0, colour = grey_col) +
geom_line(aes(y = lo), colour = "#c13e2e", linetype = "dashed", na.rm = TRUE) + geom_line(aes(y = hi), colour = "#c13e2e", linetype = "dashed", na.rm = TRUE) +
geom_line() + geom_point(data = d[d$out, ], colour = "#c13e2e", size = 1.6) + labs(x = i_col, y = v_col) + theme_cs
print(p)Needs ggplot2 · reads recursive-residual-demo.csv · app theme from the optional codesplashr helper