svgorigin-strip
Backtest origin strip
Which values does each backtest origin train on and forecast?
How to read it
- Read each strip as one backtest origin.
- The first part is what it trains on, the second what it forecasts and is scored on.
- Check that no forecast window comes before its training data.
Watch out
- Origins that overlap heavily give fewer independent tests than their number suggests.
Defaults
No settings: the chart draws one way.
Pinned in the code
- Positions come from the host's BacktestPlan (cs-ml backtest.rs): offsets from the end of the longest series.
- A model never sees values after its origin; a sliding training window shows as a strip that starts later.
- Origins closer together than the horizon forecast overlapping windows, so their errors are not independent.
# Backtest origin strip — Codesplash Chart Lab (recipe "origin-strip")
# 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("origin-strip-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")
ORIGIN = "origin"
START = "train_start"
END = "train_end"
TEST = "test_end"
d = df.dropna(subset=[ORIGIN, END, TEST]).reset_index(drop=True)
start = d[START].fillna(0) if START else pd.Series(0, index=d.index)
y = np.arange(len(d))[::-1]
fig, ax = plt.subplots(figsize=(9, 0.5 * len(d) + 1.4))
ax.barh(y, d[END] - start, left=start, height=0.6, color=PALETTE[0], label="training")
ax.barh(y, d[TEST] - d[END], left=d[END], height=0.6, color=PALETTE[1], label="forecast and scored")
ax.set_yticks(y, d[ORIGIN])
ax.set_xlabel("position in the series")
ax.legend(fontsize=8, ncol=2, loc="upper center", bbox_to_anchor=(0.5, -0.18))
plt.show()Needs pandas, numpy, matplotlib · reads origin-strip-demo.csv · app theme from the optional codesplash helper
# Backtest origin strip — Codesplash Chart Lab (recipe "origin-strip")
# 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("origin-strip-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")
ORIGIN = "origin"
START = "train_start"
END = "train_end"
TEST = "test_end"
d = df.dropna(subset=[ORIGIN, END, TEST]).reset_index(drop=True)
start = d[START].fillna(0) if START else pd.Series(0, index=d.index)
fig = go.Figure()
fig.add_bar(y=d[ORIGIN], x=d[END] - start, base=start, orientation="h", marker_color=PALETTE[0], name="training")
fig.add_bar(y=d[ORIGIN], x=d[TEST] - d[END], base=d[END], orientation="h", marker_color=PALETTE[1], name="forecast and scored")
fig.update_layout(barmode="overlay", xaxis_title="position in the series", yaxis=dict(autorange="reversed"))
fig.show()Needs pandas, numpy, plotly · reads origin-strip-demo.csv · app theme from the optional codesplash helper
# Backtest origin strip — Codesplash Chart Lab (recipe "origin-strip")
# 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("origin-strip-demo.csv", check.names = FALSE, stringsAsFactors = FALSE, na.strings = "") # empty cells are missing; Chart Lab → Code → Download demo CSV
df[["origin"]] <- factor(df[["origin"]])
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
ORIGIN <- "origin"
START <- "train_start"
END <- "train_end"
TEST <- "test_end"
d <- df[!is.na(df[[ORIGIN]]) & !is.na(df[[END]]) & !is.na(df[[TEST]]), , drop = FALSE]
d$.y <- factor(d[[ORIGIN]], levels = rev(unique(d[[ORIGIN]])))
d$.start <- if (!is.null(START)) ifelse(is.na(d[[START]]), 0, d[[START]]) else 0
p <- ggplot(d, aes(y = .y)) +
geom_linerange(aes(xmin = .start, xmax = .data[[END]], colour = "training"), linewidth = 6) +
geom_linerange(aes(xmin = .data[[END]], xmax = .data[[TEST]], colour = "forecast and scored"), linewidth = 6) +
scale_colour_manual(values = c(training = cs_cat[1], "forecast and scored" = cs_cat[2]), name = NULL) +
labs(x = "position in the series", y = NULL) +
theme(legend.position = "bottom")
print(p)Needs ggplot2 · reads origin-strip-demo.csv · app theme from the optional codesplashr helper