svgpit-histogram
PIT histogram
Do the actual values look like draws from the forecast distributions?
How to read it
- Read each bar as how often actuals fell in that part of their forecast distribution.
- Flat means calibrated.
- A U means intervals are too narrow, a hump too wide, and a slope means bias.
Watch out
- A flat histogram is necessary, not sufficient: a forecast can be calibrated and still be uninformative.
Defaults
No settings: the chart draws one way.
Pinned in the code
- PIT values come from the quantiles the engine reports as interval bounds, with the point forecast as the median, joined linearly (cs-ml backtest.rs). More interval levels make them closer to the true transform.
- Actuals outside the widest interval count halfway to 0 or 1, since the engines report nothing further out.
- Bins are equal width over 0–1; the dashed line is the share a calibrated forecast would put in each.
- Forecasts from the same origin share errors, so bars vary more than independent draws would.
# PIT histogram — Codesplash Chart Lab (recipe "pit-histogram")
# 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("pit-histogram-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")
SHARE = "share"
share = df[SHARE].fillna(0).to_numpy()
edges = np.linspace(0, 1, len(share) + 1)
fig, ax = plt.subplots(figsize=(7, 4))
ax.bar(edges[:-1], share, width=np.diff(edges) * 0.96, align="edge", color=PALETTE[0])
ax.axhline(1 / len(share), color="#6b7280", linestyle="--", label="well calibrated")
ax.set_xlabel("where the actual fell in its forecast distribution")
ax.set_ylabel("share of forecasts")
ax.legend(fontsize=8)
plt.show()Needs pandas, numpy, matplotlib · reads pit-histogram-demo.csv · app theme from the optional codesplash helper
# PIT histogram — Codesplash Chart Lab (recipe "pit-histogram")
# 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("pit-histogram-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")
SHARE = "share"
share = df[SHARE].fillna(0).to_numpy()
edges = np.linspace(0, 1, len(share) + 1)
fig = go.Figure(go.Bar(x=(edges[:-1] + edges[1:]) / 2, y=share, width=np.diff(edges) * 0.96, marker_color=PALETTE[0], name="share in the bin"))
fig.add_hline(y=1 / len(share), line_dash="dash", line_color="#6b7280", annotation_text="well calibrated")
fig.update_layout(xaxis_title="where the actual fell in its forecast distribution", yaxis_title="share of forecasts")
fig.show()Needs pandas, numpy, plotly · reads pit-histogram-demo.csv · app theme from the optional codesplash helper
# PIT histogram — Codesplash Chart Lab (recipe "pit-histogram")
# 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("pit-histogram-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
SHARE <- "share"
share <- ifelse(is.na(df[[SHARE]]), 0, df[[SHARE]])
d <- data.frame(mid = (seq_along(share) - 0.5) / length(share), share = share)
p <- ggplot(d, aes(mid, share)) +
geom_col(width = 0.96 / length(share), fill = cs_cat[1]) +
geom_hline(yintercept = 1 / length(share), linetype = "dashed", colour = "#6b7280") +
labs(x = "where the actual fell in its forecast distribution", y = "share of forecasts")
print(p)Needs ggplot2 · reads pit-histogram-demo.csv · app theme from the optional codesplashr helper