svgprior-posterior
Prior and posterior
How did the data move belief about the effect, and how much more (or less) plausible is the null value now?
How to read it
- Compare the dashed prior with the solid posterior: the data moved belief from one to the other.
- Compare heights at the test value: if the posterior is lower there, the data favour an effect.
- Read the shaded credible interval: where the effect probably lies, given the prior.
- Callout 1: The prior's height at the test value.
- Callout 2: The posterior's height at the test value.
- Callout 3: The posterior: belief about the effect after the data.
Watch out
- The result depends on the prior. Check robustness across prior widths.
Defaults
- Test value (H₀)
- 0
- The effect value to test, usually 0; the chart compares prior and posterior there.
- Range -1,000,000,000 to 1,000,000,000, step 0.01
- Credible interval lower (equal bounds = none)
- 0
- The lower bound of the credible interval to shade.
- Range -1,000,000,000 to 1,000,000,000, step 0.001
- Credible interval upper
- 0
- The upper bound of the credible interval to shade.
- Range -1,000,000,000 to 1,000,000,000, step 0.001
- Credible level
- 0.95
- The share of posterior probability inside the interval.
- Range 0.5 to 0.999, step 0.01
- Bayes factor BF₁₀ to label (0 = none)
- 0
- Print this Bayes factor on the chart.
- Range 0 to 1,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000, step 0.01
Pinned in the code
- Draws the run's numbers as given (computed by the Python/R engine); the chart computes nothing statistical.
- Prior dashed, posterior filled; the dots mark both heights at the test value. For a point null nested in the alternative, posterior height ÷ prior height = BF₀₁ (Savage–Dickey density ratio), the engines' identity check.
- The credible interval is the engine's equal-tailed interval (quantiles of the posterior).
# Prior and posterior — Codesplash Chart Lab (recipe "prior-posterior")
# controls: point=0, ci_low=0, ci_high=0, cred=0.95, bf10=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("prior-posterior-demo.csv", keep_default_na=False, na_values=[""]) # demo data (only empty cells are missing): Chart Lab → Code → Download demo CSV
df["curve"] = pd.Categorical(df["curve"].astype("string"), categories=["Prior", "Posterior"])
_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")
X = "δ (standardized effect)"
D = "density"
K = "curve"
POINT = 0 # the test value
is_prior = df[K].astype(str).str.contains("prior", case=False)
prior, post = df[is_prior].sort_values(X), df[~is_prior].sort_values(X)
fig, ax = plt.subplots(figsize=(7, 4.5))
ax.plot(prior[X], prior[D], color=REF, linestyle="--", label="prior")
ax.plot(post[X], post[D], color=PALETTE[0], linewidth=2, label="posterior")
ax.axvline(POINT, color=REF, linewidth=0.8, linestyle=":")
ax.scatter([POINT], [np.interp(POINT, prior[X], prior[D])], s=30, facecolor="white", edgecolor=REF, zorder=3)
ax.scatter([POINT], [np.interp(POINT, post[X], post[D])], s=30, color=PALETTE[0], zorder=3)
ax.set_xlabel(X)
ax.set_ylabel("density")
ax.legend(fontsize=8)
plt.show()Needs pandas, numpy, matplotlib · reads prior-posterior-demo.csv · app theme from the optional codesplash helper
# Prior and posterior — Codesplash Chart Lab (recipe "prior-posterior")
# controls: point=0, ci_low=0, ci_high=0, cred=0.95, bf10=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("prior-posterior-demo.csv", keep_default_na=False, na_values=[""]) # demo data (only empty cells are missing): Chart Lab → Code → Download demo CSV
df["curve"] = pd.Categorical(df["curve"].astype("string"), categories=["Prior", "Posterior"])
_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")
X = "δ (standardized effect)"
D = "density"
K = "curve"
POINT = 0 # the test value
is_prior = df[K].astype(str).str.contains("prior", case=False)
prior, post = df[is_prior].sort_values(X), df[~is_prior].sort_values(X)
fig = go.Figure()
fig.add_scatter(x=prior[X], y=prior[D], mode="lines", line=dict(color=REF, dash="dash"), name="prior")
fig.add_scatter(x=post[X], y=post[D], mode="lines", line=dict(color=PALETTE[0], width=2.5), name="posterior")
fig.add_vline(x=POINT, line_dash="dot", line_color=REF)
fig.update_layout(xaxis_title=X, yaxis_title="density")
fig.show()Needs pandas, numpy, plotly · reads prior-posterior-demo.csv · app theme from the optional codesplash helper
# Prior and posterior — Codesplash Chart Lab (recipe "prior-posterior")
# controls: point=0, ci_low=0, ci_high=0, cred=0.95, bf10=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("prior-posterior-demo.csv", check.names = FALSE, stringsAsFactors = FALSE, na.strings = "") # empty cells are missing; Chart Lab → Code → Download demo CSV
df[["curve"]] <- factor(df[["curve"]], levels = c("Prior", "Posterior"))
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
X <- "δ (standardized effect)"
D <- "density"
K <- "curve"
POINT <- 0 # the test value
d <- data.frame(x = df[[X]], y = df[[D]], curve = ifelse(grepl("prior", as.character(df[[K]]), ignore.case = TRUE), "prior", "posterior"))
p <- ggplot(d, aes(x, y, colour = curve, linetype = curve)) +
geom_line(linewidth = 0.8) +
geom_vline(xintercept = POINT, colour = ref_col, linetype = "dotted") +
scale_colour_manual(values = c(posterior = cs_cat[[1]], prior = ref_col), name = NULL) +
scale_linetype_manual(values = c(posterior = "solid", prior = "dashed"), name = NULL) +
labs(x = X, y = "density") + theme_cs
print(p)Needs ggplot2 · reads prior-posterior-demo.csv · app theme from the optional codesplashr helper