svgexpected-loss
Expected loss by decision
If we ship (or keep the control) now, how much do we expect to lose if that choice turns out wrong?
How to read it
- Read blue as the expected loss of shipping that arm, orange as that of keeping control.
- Compare with your tolerable loss.
- Ship only when the loss from shipping is below that threshold.
- Callout 1: Keeping control here risks a large loss.
- Callout 2: Shipping B risks almost nothing.
- Callout 3: Shipping D has a larger expected loss than keeping control.
Watch out
- Expected loss depends on the prior and on a loss scale that must suit the business.
Defaults
- Tolerable loss ε
- 0
- The loss you can accept if your decision turns out wrong.
- Range 0 to 1,000,000,000, step 0.0001
Pinned in the code
- Draws the run's numbers as given (computed by the Python/R engine); the chart computes nothing statistical.
- Expected loss of shipping = E[max(control − treatment, 0)] under the posterior (in the metric's units, for a higher-is-better metric); of keeping the control = E[max(treatment − control, 0)].
- The decision rule ships the treatment when its expected loss is below ε and keeps the control when that loss is below ε; otherwise keep collecting data.
# Expected loss by decision — Codesplash Chart Lab (recipe "expected-loss")
# controls: threshold=0
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("expected-loss-demo.csv", keep_default_na=False, na_values=[""]) # demo data (only empty cells are missing): Chart Lab → Code → Download demo CSV
df["arm"] = pd.Categorical(df["arm"].astype("string"), categories=["B", "C", "D"])
A = "arm"
SHIP = "loss if shipped"
KEEP = "loss if not shipped"
EPS = 0
d = df.dropna(subset=[A, SHIP, KEEP])
x = np.arange(len(d))
fig, ax = plt.subplots(figsize=(8, 4.5))
ax.bar(x - 0.2, d[SHIP], width=0.4, color=PALETTE[0], label="expected loss if shipped")
ax.bar(x + 0.2, d[KEEP], width=0.4, color=PALETTE[1], label="expected loss if not shipped")
if EPS:
ax.axhline(EPS, color="black", linestyle="--", linewidth=1.2, label=f"tolerable loss ε = {EPS:g}")
ax.set_xticks(x, d[A].astype(str))
ax.set_ylabel("expected loss")
ax.legend(fontsize=8)
plt.show()Needs pandas, numpy, matplotlib · reads expected-loss-demo.csv · app theme from the optional codesplash helper
# Expected loss by decision — Codesplash Chart Lab (recipe "expected-loss")
# controls: threshold=0
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("expected-loss-demo.csv", keep_default_na=False, na_values=[""]) # demo data (only empty cells are missing): Chart Lab → Code → Download demo CSV
df["arm"] = pd.Categorical(df["arm"].astype("string"), categories=["B", "C", "D"])
A = "arm"
SHIP = "loss if shipped"
KEEP = "loss if not shipped"
EPS = 0
d = df.dropna(subset=[A, SHIP, KEEP])
fig = go.Figure()
fig.add_bar(x=d[A].astype(str), y=d[SHIP], name="expected loss if shipped", marker_color=PALETTE[0])
fig.add_bar(x=d[A].astype(str), y=d[KEEP], name="expected loss if not shipped", marker_color=PALETTE[1])
if EPS:
fig.add_hline(y=EPS, line=dict(color="black", dash="dash"), annotation_text=f"ε = {EPS:g}")
fig.update_layout(barmode="group", yaxis_title="expected loss")
fig.show()Needs pandas, numpy, plotly · reads expected-loss-demo.csv · app theme from the optional codesplash helper
# Expected loss by decision — Codesplash Chart Lab (recipe "expected-loss")
# controls: threshold=0
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("expected-loss-demo.csv", check.names = FALSE, stringsAsFactors = FALSE, na.strings = "") # empty cells are missing; Chart Lab → Code → Download demo CSV
df[["arm"]] <- factor(df[["arm"]], levels = c("B", "C", "D"))
a_col <- "arm"
ship_col <- "loss if shipped"
keep_col <- "loss if not shipped"
eps <- 0
arms <- as.character(df[[a_col]])
d <- rbind(data.frame(arm = arms, loss = df[[ship_col]], choice = "if shipped"),
data.frame(arm = arms, loss = df[[keep_col]], choice = "if not shipped"))
d$arm <- factor(d$arm, levels = unique(arms))
d$choice <- factor(d$choice, levels = c("if shipped", "if not shipped"))
p <- ggplot(d[!is.na(d$loss), ], aes(arm, loss, fill = choice)) +
geom_col(position = position_dodge(width = 0.8), width = 0.75) +
{ if (!is.null(eps) && eps > 0) geom_hline(yintercept = eps, linetype = "dashed") } +
scale_fill_manual(values = cs_cat) + labs(x = NULL, y = "expected loss", fill = "Expected loss") + theme_cs
print(p)Needs ggplot2 · reads expected-loss-demo.csv · app theme from the optional codesplashr helper