svgsurv-brier-curve
Survival Brier score
How far are predicted survival probabilities from what happened, at each time?
How to read it
- Read each point as the squared error of the predicted survival probability at that time; lower is better.
- Compare with the dashed line at 0.25: the score of a forecast that always says 50%.
- Read the number at the top left, the integrated Brier score: the average of this curve across the times shown.
- Callout 1: Early on few events have happened, so errors are small for any model.
- Callout 2: Reference: a 50/50 forecast.
- Callout 3: Integrated Brier score.
Watch out
- The curve tends to rise with time for any model as outcomes become less certain. Compare models at the same times, not early against late.
- A low Brier score at a time when nearly everyone is alive can be earned by predicting survival for all.
Defaults
No settings: the chart draws one way.
Pinned in the code
- Draws the run's numbers as given (computed by the host's cs-ml evaluation or the Python/R engine); the chart computes nothing statistical.
- Rows with an event by t count (S(t))² over the chance of remaining uncensored until their event; rows still at risk count (1 − S(t))² over the same chance at t. The integrated score is the trapezoid mean over the times shown.
# Survival Brier score — Codesplash Chart Lab (recipe "surv-brier-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("surv-brier-curve-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")
def trapezoid_mean(t, v):
"""The integrated Brier score: trapezoid area over the span of the times."""
t, v = np.asarray(t, float), np.asarray(v, float)
return float(np.sum(np.diff(t) * (v[1:] + v[:-1]) / 2) / (t[-1] - t[0])) if len(t) > 1 else float(v[0])
T = "Days since diagnosis"
B = "Brier score"
d = df.dropna(subset=[T, B]).sort_values(T)
fig, ax = plt.subplots(figsize=(7, 4))
ax.axhline(0.25, color=REF, linestyle="--", linewidth=1, label="0.25: a 50/50 forecast")
ax.plot(d[T], d[B], marker="o", markersize=3, color=PALETTE[0], label=f"integrated {trapezoid_mean(d[T], d[B]):.3f}")
ax.set_ylim(0, max(0.3, d[B].max()))
ax.set_xlabel(T)
ax.set_ylabel(B)
ax.legend(frameon=False)
plt.show()Needs pandas, numpy, matplotlib · reads surv-brier-curve-demo.csv · app theme from the optional codesplash helper
# Survival Brier score — Codesplash Chart Lab (recipe "surv-brier-curve")
# 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("surv-brier-curve-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")
def trapezoid_mean(t, v):
"""The integrated Brier score: trapezoid area over the span of the times."""
t, v = np.asarray(t, float), np.asarray(v, float)
return float(np.sum(np.diff(t) * (v[1:] + v[:-1]) / 2) / (t[-1] - t[0])) if len(t) > 1 else float(v[0])
T = "Days since diagnosis"
B = "Brier score"
d = df.dropna(subset=[T, B]).sort_values(T)
fig = go.Figure(go.Scatter(x=d[T], y=d[B], mode="lines+markers", line_color=PALETTE[0], name=f"integrated {trapezoid_mean(d[T], d[B]):.3f}"))
fig.add_hline(y=0.25, line_dash="dash", line_color=REF, annotation_text="0.25: a 50/50 forecast")
fig.update_layout(xaxis_title=T, yaxis_title=B, yaxis_range=[0, max(0.3, d[B].max())], showlegend=True)
fig.show()Needs pandas, numpy, plotly · reads surv-brier-curve-demo.csv · app theme from the optional codesplash helper
# Survival Brier score — Codesplash Chart Lab (recipe "surv-brier-curve")
# 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("surv-brier-curve-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
trapezoid_mean <- function(t, v) {
n <- length(t)
if (n < 2L) return(v[1])
sum(diff(t) * (v[-1] + v[-n]) / 2) / (t[n] - t[1])
}
T <- "Days since diagnosis"
B <- "Brier score"
d <- data.frame(time = df[[T]], brier = df[[B]])
d <- d[is.finite(d$time) & is.finite(d$brier), ]
d <- d[order(d$time), ]
p <- ggplot(d, aes(time, brier)) +
geom_hline(yintercept = 0.25, linetype = "dashed", colour = ref_col) +
geom_line(colour = cs_cat[[1]], linewidth = 0.8) +
geom_point(colour = cs_cat[[1]], size = 1.5) +
coord_cartesian(ylim = c(0, max(0.3, d$brier))) +
labs(x = T, y = B, subtitle = sprintf("integrated %.3f", trapezoid_mean(d$time, d$brier))) + theme_cs
print(p)Needs ggplot2 · reads surv-brier-curve-demo.csv · app theme from the optional codesplashr helper