Skip to content
svgdecision-curve

Decision curve

Across the range of risk thresholds a clinician might use, does acting on the model do more good than treating everyone or no one?

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

How to read it

  1. Read x as the risk threshold you'd act on.
  2. Read y as net benefit: true positives minus false positives weighted by the threshold.
  3. A model is useful where it's above treat-all and treat-none.
  1. Callout 1: The model above the treat-all line.
  2. Callout 2: Treat none: zero benefit.
  3. Callout 3: Treat all: benefit falls as the threshold rises.

Watch out

  • Only thresholds you'd really use matter, so ignore the rest of the curve.

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.
  • Net benefit = TP/n − FP/n · pₜ/(1 − pₜ). Strategies named all/treat all and none/treat none are drawn dashed; the y axis is clipped at −5% of the maximum so the treat-all line does not dominate (as in dcurves).
# Decision curve — Codesplash Chart Lab (recipe "decision-curve")
# 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("decision-curve-demo.csv", keep_default_na=False, na_values=[""])  # demo data (only empty cells are missing): Chart Lab → Code → Download demo CSV
df["strategy"] = pd.Categorical(df["strategy"].astype("string"), categories=["Full model", "Clinical score", "Treat all", "Treat none"])

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)))
T = "threshold probability"
NB = "net benefit"
S = "strategy"
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=[T]).sort_values(T)))
top = df[NB].max()
fig, ax = plt.subplots(figsize=(8, 4.5))
ax.axhline(0, color="grey", linewidth=0.8)
k = 0
for name, d in series:
    low = str(name).lower()
    if "all" in low or "none" in low:
        ax.plot(d[T], d[NB], color="grey" if "all" in low else "black", linestyle="--", linewidth=1.2, label=name)
    else:
        ax.plot(d[T], d[NB], color=PALETTE[k % len(PALETTE)], linewidth=2, label=name)
        k += 1
ax.set_ylim(-0.05 * top, top * 1.08)  # clip the treat-all line, as dcurves does
ax.set_xlabel(T)
ax.set_ylabel(NB)
ax.legend(fontsize=8)
plt.show()

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