svgsprt-path
SPRT log-likelihood ratio walk
After each observation (or event), has the evidence crossed the boundary for H₁ or for H₀?
How to read it
- Read the black path as the evidence after each observation.
- Crossing the upper line: reject H₀; the lower: accept H₀.
- Stop at the first crossing.
- Callout 1: The first crossing: stop here.
- Callout 2: The reject-H₀ boundary.
- Callout 3: The accept-H₀ boundary.
Watch out
- An SPRT plans for specific hypotheses; a true effect between them can take a long time to decide.
Defaults
No settings: the chart draws one way.
Pinned in the code
- Draws the run's numbers as given (computed by the Python/R engine); the chart computes nothing statistical.
- Series named like reject / log A / critical value are the upper boundary, accept / log B the lower one; any other series is the observed walk.
- The first crossing is the first observed point at or beyond a boundary interpolated at that x; the engine's decision is authoritative (Wald's truncation rule may decide earlier).
# SPRT log-likelihood ratio walk — Codesplash Chart Lab (recipe "sprt-path")
# 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("sprt-path-demo.csv", keep_default_na=False, na_values=[""]) # demo data (only empty cells are missing): Chart Lab → Code → Download demo CSV
df["series"] = pd.Categorical(df["series"].astype("string"), categories=["reject H₀ (log A)", "accept H₀ (log B)", "observed log Λ"])
def levels_of(s):
"""Declared level order (categoricals), else order of first appearance."""
if isinstance(s.dtype, pd.CategoricalDtype):
return [v for v in s.cat.categories if (s == v).any()]
return list(dict.fromkeys(s.dropna().astype(str)))
X = "observation"
L = "log Λ"
S = "series"
groups = levels_of(df[S]) if S else [None]
series = []
for name in groups:
d = df if name is None else df[df[S].astype(str) == str(name)]
series.append((name if name is not None else "", d.dropna(subset=[X]).sort_values(X)))
fig, ax = plt.subplots(figsize=(8, 4.5))
ax.axhline(0, color="grey", linewidth=0.8)
for name, d in series:
low = str(name).lower()
kind = "upper" if any(w in low for w in ("reject", "log a", "critical", "upper", "signal")) else "lower" if any(w in low for w in ("accept", "log b", "lower")) else "path"
col = {"upper": PALETTE[0], "lower": PALETTE[1], "path": "black"}[kind]
ax.plot(d[X], d[L], color=col, linestyle="-" if kind == "path" else "--", marker="o" if kind == "path" else None, ms=3, linewidth=2 if kind == "path" else 1.5, label=name)
ax.set_xlabel(X)
ax.set_ylabel(L)
ax.legend(fontsize=8)
plt.show()Needs pandas, numpy, matplotlib · reads sprt-path-demo.csv · app theme from the optional codesplash helper
# SPRT log-likelihood ratio walk — Codesplash Chart Lab (recipe "sprt-path")
# 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("sprt-path-demo.csv", keep_default_na=False, na_values=[""]) # demo data (only empty cells are missing): Chart Lab → Code → Download demo CSV
df["series"] = pd.Categorical(df["series"].astype("string"), categories=["reject H₀ (log A)", "accept H₀ (log B)", "observed log Λ"])
def levels_of(s):
"""Declared level order (categoricals), else order of first appearance."""
if isinstance(s.dtype, pd.CategoricalDtype):
return [v for v in s.cat.categories if (s == v).any()]
return list(dict.fromkeys(s.dropna().astype(str)))
X = "observation"
L = "log Λ"
S = "series"
groups = levels_of(df[S]) if S else [None]
series = []
for name in groups:
d = df if name is None else df[df[S].astype(str) == str(name)]
series.append((name if name is not None else "", d.dropna(subset=[X]).sort_values(X)))
fig = go.Figure()
for i, (name, d) in enumerate(series):
low = str(name).lower()
path = not any(w in low for w in ("reject", "log a", "critical", "upper", "signal", "accept", "log b", "lower"))
fig.add_scatter(x=d[X], y=d[L], mode="lines+markers" if path else "lines", name=name,
line=dict(color="black" if path else PALETTE[i % len(PALETTE)], dash="solid" if path else "dash", width=2.5 if path else 1.5))
fig.add_hline(y=0, line=dict(color="grey", width=1))
fig.update_layout(xaxis_title=X, yaxis_title=L)
fig.show()Needs pandas, numpy, plotly · reads sprt-path-demo.csv · app theme from the optional codesplash helper
# SPRT log-likelihood ratio walk — Codesplash Chart Lab (recipe "sprt-path")
# 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("sprt-path-demo.csv", check.names = FALSE, stringsAsFactors = FALSE, na.strings = "") # empty cells are missing; Chart Lab → Code → Download demo CSV
df[["series"]] <- factor(df[["series"]], levels = c("reject H₀ (log A)", "accept H₀ (log B)", "observed log Λ"))
levels_of <- function(v) if (is.factor(v)) levels(droplevels(v)) else unique(as.character(v[!is.na(v)]))
x_col <- "observation"
l_col <- "log Λ"
s_col <- "series"
d <- data.frame(x = df[[x_col]], llr = df[[l_col]], s = factor(as.character(df[[s_col]]), levels = levels_of(df[[s_col]])))
d <- d[!is.na(d$x) & !is.na(d$llr), ]
d <- d[order(d$s, d$x), ]
d$path <- !grepl("reject|log a|critical|upper|signal|accept|log b|lower", tolower(d$s))
p <- ggplot(d, aes(x, llr, colour = s, group = s)) +
geom_hline(yintercept = 0, colour = "grey50") +
geom_line(aes(linetype = path, linewidth = path)) + geom_point(data = d[d$path, ], size = 1.6) +
scale_linetype_manual(values = c("dashed", "solid"), guide = "none") + scale_linewidth_manual(values = c(0.6, 1), guide = "none") +
scale_colour_manual(values = c(cs_cat[1:2], "black", cs_cat[-(1:2)])) +
labs(x = x_col, y = l_col, colour = NULL) + theme_cs
print(p)Needs ggplot2 · reads sprt-path-demo.csv · app theme from the optional codesplashr helper