svgcoverage-by-horizon
Coverage by horizon
Do prediction intervals keep their promise further ahead?
How to read it
- Read each line as an interval's actual coverage at each horizon.
- Compare with its dashed nominal level.
- Lines falling below their target mean the intervals are too narrow that far ahead.
Watch out
- Long horizons have fewer backtest values, so their coverage is more uncertain.
Defaults
No settings: the chart draws one way.
Pinned in the code
- Coverage is scored on the backtest's forecasts by the host (cs-ml backtest.rs).
- Each point uses only the forecasts at that horizon, so few origins make it coarse: with 5 origins, each point moves in steps of 20%.
# Coverage by horizon — Codesplash Chart Lab (recipe "coverage-by-horizon")
# 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("coverage-by-horizon-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")
H = "h"
LEVEL = "level"
COV = "coverage"
d = df.dropna(subset=[H, LEVEL, COV])
fig, ax = plt.subplots(figsize=(8, 4.2))
for i, (level, s) in enumerate(sorted(d.groupby(LEVEL), key=lambda kv: -kv[0])):
s = s.sort_values(H)
colour = PALETTE[i % len(PALETTE)]
ax.plot(s[H], s[COV], marker="o", color=colour, label=f"{level:g}%")
ax.axhline(level / 100, color=colour, alpha=0.45, linestyle="--")
ax.set_ylim(0, 1)
ax.set_xlabel("steps ahead")
ax.set_ylabel("share of actuals inside the interval")
ax.legend(fontsize=8)
plt.show()Needs pandas, numpy, matplotlib · reads coverage-by-horizon-demo.csv · app theme from the optional codesplash helper
# Coverage by horizon — Codesplash Chart Lab (recipe "coverage-by-horizon")
# 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("coverage-by-horizon-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")
H = "h"
LEVEL = "level"
COV = "coverage"
d = df.dropna(subset=[H, LEVEL, COV])
fig = go.Figure()
for i, (level, s) in enumerate(sorted(d.groupby(LEVEL), key=lambda kv: -kv[0])):
s = s.sort_values(H)
colour = PALETTE[i % len(PALETTE)]
fig.add_scatter(x=s[H], y=s[COV], mode="lines+markers", line_color=colour, name=f"{level:g}%")
fig.add_hline(y=level / 100, line_dash="dash", line_color=colour, opacity=0.45)
fig.update_layout(xaxis_title="steps ahead", yaxis_title="share of actuals inside the interval", yaxis_range=[0, 1])
fig.show()Needs pandas, numpy, plotly · reads coverage-by-horizon-demo.csv · app theme from the optional codesplash helper
# Coverage by horizon — Codesplash Chart Lab (recipe "coverage-by-horizon")
# 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("coverage-by-horizon-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
H <- "h"
LEVEL <- "level"
COV <- "coverage"
d <- df[!is.na(df[[H]]) & !is.na(df[[LEVEL]]) & !is.na(df[[COV]]), , drop = FALSE]
levels_found <- sort(unique(d[[LEVEL]]), decreasing = TRUE)
d$.level <- factor(paste0(d[[LEVEL]], "%"), levels = paste0(levels_found, "%"))
targets <- data.frame(.level = factor(paste0(levels_found, "%"), levels = paste0(levels_found, "%")), target = levels_found / 100)
p <- ggplot(d, aes(.data[[H]], .data[[COV]], colour = .level)) +
geom_hline(data = targets, aes(yintercept = target, colour = .level), linetype = "dashed", alpha = 0.45) +
geom_line() + geom_point() +
scale_colour_manual(values = cs_cat, name = NULL) +
coord_cartesian(ylim = c(0, 1)) +
labs(x = "steps ahead", y = "share of actuals inside the interval")
print(p)Needs ggplot2 · reads coverage-by-horizon-demo.csv · app theme from the optional codesplashr helper