Skip to content
svgmc-distribution

Monte Carlo distribution

Across all simulated scenarios, how likely is each outcome — and of missing the target?

Demo data · seed 20261101 · 20,000 rows · default settings · 640 × 360SVG, lightSVG, darkDemo CSV

How to read it

  1. Read the histogram as the spread of simulated outcomes.
  2. Read the curve for the cumulative chance, on the right axis.
  3. Read the shaded part as the chance of missing the target.
  1. Callout 1: The chance of missing the target.
  2. Callout 2: P5, P50 and P95.
  3. Callout 3: The cumulative chance.

Watch out

  • The result is only as good as your input distributions. Simulated precision isn't real-world precision.

Defaults

Target (null = none)
0
The outcome you want to reach; the chart shows the chance of missing it.
Range -1,000,000,000,000 to 1,000,000,000,000, step 1
Percentiles
P5 · P50 · P95
Which percentiles to mark.
Also: P10 · P50 · P90 · P1 · P50 · P99 · P25 · P50 · P75

Pinned in the code

  • Raw draws are binned on equal-width edges (Sturges × 2, 30–80 bins); percentiles are type 7 (R default) on the draws. Pre-binned input is drawn as sent, with percentiles interpolated within bins.
  • P(value < target) counts draws strictly below the target. Draws come from the shared random stream, so Python and R simulations agree draw for draw.
# Monte Carlo distribution — Codesplash Chart Lab (recipe "mc-distribution")
# controls: target=0, percentiles=5-50-95
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("mc-distribution-demo.csv", keep_default_na=False, na_values=[""])  # demo data (only empty cells are missing): Chart Lab → Code → Download demo CSV

V = "profit ($k)"
BL = None
BH = None
N = None
TARGET, PCTS = 0, [5, 50, 95]
fig, ax = plt.subplots(figsize=(8, 4.5))
if V:
    v = df[V].dropna().to_numpy()
    k = max(30, min(80, 2 * int(np.ceil(np.log2(len(v)) + 1))))
    w = np.full(len(v), 1 / len(v))
    counts, edges, _ = ax.hist(v, bins=k, weights=w, color=PALETTE[0], alpha=0.55)
    pv = np.percentile(v, PCTS)  # numpy's "linear" = type 7
else:
    b = df.dropna(subset=[BL, BH, N]).sort_values(BL)
    share = b[N] / b[N].sum()
    ax.bar(b[BL], share, width=b[BH] - b[BL], align="edge", color=PALETTE[0], alpha=0.55)
    edges, counts = np.r_[b[BL].to_numpy(), b[BH].iloc[-1]], share.to_numpy()
    cum = np.r_[0, np.cumsum(counts)]
    pv = np.interp(np.array(PCTS) / 100, cum, edges)
ax2 = ax.twinx()
ax2.plot(edges, np.r_[0, np.cumsum(counts)], color="black", linewidth=1.4)
ax2.set_ylim(0, 1.02)
ax2.set_ylabel("cumulative probability")
for p, x in zip(PCTS, pv):
    ax.axvline(x, color=PALETTE[2], linestyle="--", linewidth=1)
    ax.annotate(f"P{p}", (x, 1), xycoords=("data", "axes fraction"), ha="left", va="top", fontsize=8, color=PALETTE[2])
if TARGET is not None:
    ax.axvline(TARGET, color="#ce5342", linewidth=1.6)
ax.set_xlabel(V or "value")
ax.set_ylabel("share of draws")
plt.show()

Needs pandas, numpy, matplotlib · reads mc-distribution-demo.csv · app theme from the optional codesplash helper