Sizing and TCO

Appendix B · The Monte Carlo module, read end to end

This page quotes the files in the order they are written: sizing/mc.py from its opening docstring onwards (omitting only the title line and imports), then sizing/normal.py from its first constant to its end. The point is to show there is nothing else in either.

A run calls the pieces in a different order:

  1. rng, once, seeded from the scenario.

  2. For each uncertain input the scenario has not pinned: sample with its declared distribution, or for a measured constant, normal_ppf_scaled with its measured value and standard error.

  3. If the model declares correlations between inputs that were drawn: correlation_matrix, then correlate, once, over all the drawn inputs together.

  4. The model’s formulas, applied to whole columns of draws at once. (This step is not in sizing/mc.py.)

  5. summarise and histogram, for every node whose value varies.

The function that makes these calls is evaluate in sizing/evaluate.py. The module’s docstring from sizing/mc.py follows.

This module is small on purpose. It is quoted into ch13 and ch14 and it is meant to be read, not imported and trusted, so there is no simulation framework underneath it and no statistics package beside it — numpy for arrays, sizing.normal for one rational approximation, and nothing else. A reader who finishes ch14 should be able to delete this file and write it again.

One idea

Every distribution here is sampled the same way, and it is the only sampling idea in the book:

draw a fraction between 0 and 1 uniformly at random, and ask the distribution what value sits
at it.

That is inverse transform sampling. It is why each distribution below needs exactly one function — its percentile function, ppf — and why adding a distribution to this book is three lines rather than a new dependency. Problem 13.1 asks the reader to add one.

What is deliberately absent

No variance reduction, no quasi-random sequences, no importance sampling. Every one of them narrows an interval for the same number of draws, and every one of them also makes the interval harder to explain to the person who has to sign for the money. This book’s bottleneck is never compute; it is whether the reader believes the answer.

No fitted distributions either. Nothing here reads data and tells you which shape it is. Choosing a shape is an editorial act with provenance attached (ch03), and a function that guesses it for you produces a model whose central assumption nobody ever wrote down.

Each uncertain input in the model file declares which shape it follows, written alongside its provenance kind and source (ch03). A reviewer can then see the choice and argue with it, where a fitting function would leave the decision undeclared.

Two constants

#: The percentile the ``p10``/``p90`` form of a lognormal is pinned at, as a z-score. Named
#: because it appears in the parameter solution below and a bare 1.2816 there would be a mystery.
Z90 = float(normal_ppf(0.9))

#: How many draws a model is sampled with when its scenario does not say.
#:
#: Every scenario in the repository states its own count, so this is the fallback. ch14 measures
#: what this count buys: how much the web service model's five-year total moves from one run to
#: the next, at this count and at smaller ones. Whether it is enough depends on the precision you
#: will report the answer to. :func:`samples_needed` works out the count for a precision you choose.
DEFAULT_SAMPLES = 100_000

The seed

def rng(seed: int) -> np.random.Generator:
    """The generator, seeded.

    One per run, created here so that every result in the book can record the seed that produced
    it and be reproduced exactly. An unseeded Monte Carlo is a result nobody can repeat,
    which is the same thing this repository refuses everywhere else.

    PCG64 rather than the legacy Mersenne Twister: numpy's modern generator is what
    ``default_rng`` gives you, and pinning it by name means a numpy upgrade cannot silently
    change which stream a stamped seed refers to.
    """
    return np.random.Generator(np.random.PCG64(seed))

The enforcement happens outside this function: bench/stamp.py refuses to stamp any model result that does not record its seed. When an experiment uses many seeds, the result records the base seed and the rule that derives them. The convergence table under Reading the answer is an example: it runs the model many times at each sample count, each time with its own seed, and its result records the base seed and the rule.

The four percentile functions

# -- percentile functions ----------------------------------------------------------------
#
# Each takes a vector of fractions between 0 and 1 and returns the values at them. That is the
# whole interface. Everything else in this module is arrangement.

Uniform

def uniform_ppf(u: np.ndarray, minimum: float, maximum: float) -> np.ndarray:
    """Every value between two bounds, equally likely.

    Honest when the bounds are genuinely all you know — a contract that caps a price, a retention
    window somebody will choose from a range. Dishonest as a default, because it says the bounds
    are as likely as the middle, and almost nothing real is like that.
    """
    if not minimum <= maximum:
        raise ValueError(f"uniform needs minimum <= maximum, got {minimum}, {maximum}")
    return minimum + u * (maximum - minimum)

Triangular

def triangular_ppf(u: np.ndarray, minimum: float, likely: float, maximum: float) -> np.ndarray:
    """An expert's guess: the least it could be, the most, and the one they would bet on.

    The shape most sizing inputs arrive in, because it is the shape of the answer to "what is it,
    roughly?". Its flaw is worth stating every time it is used: it asserts that nothing outside
    the bounds can happen, and the bounds came from somebody's memory.

    The two branches meet at the mode. Below it the area grows as the square of the distance from
    the minimum, which inverts to a square root — the whole derivation, and problem 13.1 asks for
    it again for a distribution that is not here.
    """
    if not minimum <= likely <= maximum:
        raise ValueError(
            f"triangular needs min <= likely <= max, got {minimum}, {likely}, {maximum}"
        )
    if maximum == minimum:
        return np.full_like(u, minimum)
    width = maximum - minimum
    at_mode = (likely - minimum) / width
    below = minimum + np.sqrt(u * width * (likely - minimum))
    above = maximum - np.sqrt((1.0 - u) * width * (maximum - likely))
    return np.where(u < at_mode, below, above)

Lognormal

def lognormal_ppf(u: np.ndarray, p10: float, p90: float) -> np.ndarray:
    """Multiplicative uncertainty: the shape of prices, growth rates and anything compounding.

    Parameterised by two percentiles rather than by the mean and standard deviation of the
    logarithm, because nobody has an intuition for the second and everybody has one for the first.
    "I would be surprised if it were under 11 or over 19" is a sentence a person can say about a
    price, and it is exactly ``p10=11, p90=19``.

    Two properties earn it its place. It cannot go negative, and neither can a price. And a
    product of several of them is another one, which is what a chain of multiplications in a
    sizing model *is* — so the uncertainty that arrives at the end of the chain has this shape
    whether or not anybody chose it.
    """
    if not 0 < p10 < p90:
        raise ValueError(f"lognormal needs 0 < p10 < p90, got p10={p10}, p90={p90}")
    # Solve for the two parameters of the underlying normal from the two stated percentiles.
    log_median = (np.log(p10) + np.log(p90)) / 2.0
    log_spread = (np.log(p90) - np.log(p10)) / (2.0 * Z90)
    return np.exp(log_median + log_spread * normal_ppf(u))

Normal

def normal_ppf_scaled(u: np.ndarray, mean: float, sd: float) -> np.ndarray:
    """Symmetric error around a central value.

    In this book it means one thing: the measurement uncertainty of a ``measured`` node (ch03).
    A constant was measured, the measurement has a standard error, and that error is as likely to
    be high as low. It is the wrong default for a price — see :func:`lognormal_ppf` — and the
    wrong shape for anything that cannot go negative, which it happily will.
    """
    if sd < 0:
        raise ValueError(f"normal needs sd >= 0, got {sd}")
    return mean + sd * normal_ppf(u)

The registry

#: The distributions a model file may declare, by the key it declares them under. Adding one is a
#: percentile function and a line here; there is no registration machinery and no plugin system,
#: because four shapes cover every input in both reference models and a fifth should have to
#: argue for itself.
SHAPES: dict[str, Callable[..., np.ndarray]] = {
    "uniform": uniform_ppf,
    "triangular": triangular_ppf,
    "lognormal": lognormal_ppf,
    "normal": normal_ppf_scaled,
}

sample looks up shapes in SHAPES, but it is not the only function that does. The module in sizing/evaluate.py uses the same percentile functions for the point estimate of each input (its median value) and for each input’s swing in the tornado, the chart that ranks inputs by how far each moves the output. So sampling, point estimation, and the tornado all reach shapes through this single table. Appendix C covers when each shape suits an input and how each misleads.

Drawing

def sample(spec: dict, n: int, generator: np.random.Generator) -> np.ndarray:
    """Draw ``n`` values from a declared distribution.

    Two lines, and they are the chapter's whole sampling idea. The first finds the shape the
    declaration names and its parameters. The second draws ``n`` random fractions between 0 and 1
    and hands them to that shape's percentile function, which returns the value at each.
    """
    shape, parameters = one_shape(spec)
    return SHAPES[shape](generator.random(n), **parameters)

A run calls sample once for each uncertain input. A measured constant does not go through sample: it declares no distribution, only a value and a standard error, both read from its stamped measurement. For a measured constant, the run draws random fractions between 0 and 1 from the same generator and passes them directly to normal_ppf_scaled, with the measured value as the centre and the standard error as the spread. So both kinds of input are sampled the same way: draw a random fraction and look up the value at it. Only the lookup differs — the declared shape’s percentile function for an ordinary input, normal_ppf_scaled for a measured constant. The code below shows how sample pulls the one shape out of a declaration.

def one_shape(spec: dict) -> tuple[str, dict]:
    """Pull the single distribution out of a declaration, refusing ambiguity.

    A node that declares two shapes is not a node with a shape to be guessed at; it is a model
    file somebody edited without deleting the old line, and picking either one for them hides it.
    """
    declared = [key for key in spec if key in SHAPES]
    if len(declared) != 1:
        known = ", ".join(sorted(SHAPES))
        raise ValueError(
            f"a distribution declares exactly one shape, got {sorted(spec)!r}. Known shapes: {known}"
        )
    shape = declared[0]
    return shape, dict(spec[shape])

Inputs that move together

# -- inputs that move together -----------------------------------------------------------


def correlation_matrix(names: list[str], pairs: list[dict]) -> np.ndarray:
    """A full correlation matrix from the pairs a model bothered to declare.

    Everything not named is left at zero, which is an assumption and not a fact — ch14 is mostly
    about how much that assumption costs. Stating it here rather than hiding it in a default is
    the point of building the matrix explicitly.
    """
    index = {name: i for i, name in enumerate(names)}
    matrix = np.eye(len(names))
    for pair in pairs:
        a, b, rho = pair["a"], pair["b"], float(pair["rho"])
        if a not in index or b not in index:
            missing = a if a not in index else b
            raise ValueError(f"correlation names {missing!r}, which is not a sampled input")
        if not -1.0 <= rho <= 1.0:
            raise ValueError(f"correlation between {a} and {b} is {rho}, outside [-1, 1]")
        matrix[index[a], index[b]] = matrix[index[b], index[a]] = rho
    return matrix

def rank_to_score_correlation(rank_rho: np.ndarray) -> np.ndarray:
    """Work out the score correlation to ask for, so the ranks come out as declared.

    :func:`correlate` correlates normal scores and then reorders each input to follow them. The
    rank correlation that comes out is lower than the score correlation that went in, by a fixed
    formula, a result due to Karl Pearson (1907):

        rank correlation  =  (6 / pi) * arcsin(score correlation / 2)

    The gap is small and consistent: ask the scores for 0.8 and the ranks come out at about
    0.786. That is why it survives review, since nobody expects the number they typed to come
    back different. So this inverts the formula: a model file's ``rho`` means the rank
    correlation the modeller wants.
    """
    return 2.0 * np.sin(np.pi * rank_rho / 6.0)

rank_to_score_correlation is Pearson’s formula [Pearson (1907)], inverted. correlate, below, calls it on the declared matrix before it shapes the scores, not after. So the rho you declare for a pair is the rank correlation the run produces, which is why ch14’s table of declared correlations labels its column “Rank correlation”.

def correlate(
    columns: np.ndarray, target: np.ndarray, generator: np.random.Generator
) -> np.ndarray:
    """Give each input the rank correlation a model declares, without changing its distribution.

    Iman and Conover's method (1982): each column is reordered to match the target correlations.
    Every value that was in a column is still in it, so each input keeps exactly the distribution
    the modeller chose. Only which draws line up with which changes.

    Correlating the values themselves instead would change each input's distribution, and then
    the model would answer a question nobody asked. Problem 14.2 checks, among other things,
    that each input's distribution does not move, and the rank correlation comes out as declared.

    ``columns`` is (samples, inputs); ``target`` is the square matrix from
    :func:`correlation_matrix`, holding rank correlations.
    """
    n, k = columns.shape
    if target.shape != (k, k):
        raise ValueError(f"correlation matrix is {target.shape}, expected {(k, k)}")
    if k == 0 or np.allclose(target, np.eye(k)):
        return columns

    # A reference set with the right shape and no correlation: the normal scores, independently
    # shuffled per column, from the run's own generator so the whole thing reproduces from one
    # stamped seed. Working in scores rather than in the data is what makes the method
    # indifferent to the shape of each input's distribution.
    scores = normal_ppf(np.arange(1, n + 1) / (n + 1))
    reference = np.column_stack([generator.permutation(scores) for _ in range(k)])

    wanted_scores = rank_to_score_correlation(target)
    np.fill_diagonal(wanted_scores, 1.0)
    try:
        wanted = np.linalg.cholesky(wanted_scores)
    except np.linalg.LinAlgError as exc:
        raise ValueError(
            "the declared correlations are not mutually consistent — no set of inputs can have "
            "all of them at once. Check for a triangle of strong correlations that disagree."
        ) from exc
    have = np.linalg.cholesky(np.corrcoef(reference, rowvar=False))
    shaped = reference @ np.linalg.solve(have, wanted).T

    # Reorder each column to follow the shaped scores' ranking. `argsort` twice gives the rank of
    # every element; sorting the column and indexing by rank puts the largest value where the
    # largest score is, and so on down.
    out = np.empty_like(columns)
    for j in range(k):
        ranks = np.argsort(np.argsort(shaped[:, j]))
        out[:, j] = np.sort(columns[:, j])[ranks]
    return out

The method of Iman and Conover [Iman & Conover (1982)] works in three steps:

  1. Build a reference set with one column of normal scores per input, each shuffled independently using the run’s generator, so the stamped seed reproduces it.

  2. Apply the wanted score correlations using two Cholesky factors (a standard way to give a set of columns a chosen correlation), calculated from both the correlation the shuffled set has and the correlation wanted.

  3. Reorder each input’s draws to match its shaped column: largest draw where the largest score is, and so on down.

Where a set of declared correlations cannot all hold together, step 2 fails with an error directing you to look for a triangle of strong correlations that conflict. The model does not run with a revised set.

Reading the answer

# -- reading the answer ------------------------------------------------------------------

#: The percentiles every sampled node reports. Deliberately few. A table with a dozen of them
#: invites the reader to find the one that supports the decision they had already made.
PERCENTILES = (5, 25, 50, 75, 95)


def summarise(x: np.ndarray) -> dict:
    """Summarise a node's draws as percentiles, minimum, maximum, mean and standard deviation.

    The percentiles are interpolated between draws rather than read directly from them. This is
    the right choice because the draws come from a continuous distribution the model made up: no
    single draw is real, so there is no reason to prefer one.
    """
    x = np.asarray(x, dtype=float)
    values = np.percentile(x, PERCENTILES)
    return {
        "min": float(np.min(x)),
        **{f"p{p}": float(v) for p, v in zip(PERCENTILES, values, strict=True)},
        "max": float(np.max(x)),
        "mean": float(np.mean(x)),
        "sd": float(np.std(x, ddof=1)) if x.size > 1 else 0.0,
    }

def interval(x: np.ndarray, lo: float = 5, hi: float = 95) -> tuple[float, float]:
    """The range the model puts ``hi - lo`` per cent of its belief in.

    Not a confidence interval and not a guarantee. It is a statement about this model's inputs,
    and it is exactly as good as they are — which is what ch14's closing section is about.
    """
    low, high = np.percentile(np.asarray(x, dtype=float), [lo, hi])
    return float(low), float(high)


def half_width(x: np.ndarray, lo: float = 5, hi: float = 95) -> float:
    """Half the distance between the 5th and 95th percentile of the draws, by default.

    The width belongs to the distribution. More draws locate it; they do not shrink it.
    """
    low, high = interval(x, lo, hi)
    return (high - low) / 2.0

def samples_needed(observed_spread: float, at_n: int, target_spread: float) -> int:
    """How many draws until the answer moves between runs by no more than ``target_spread``.

    ``observed_spread`` is how much a figure varied between runs of ``at_n`` draws with different
    seeds. The run-to-run spread falls as one over the square root of the number of draws, so
    halving it takes four times as many draws. ch14 shows the law in a table.

    This is about sampling noise only: how much the answer moves from one run to the next because
    it was made from a finite number of draws. It says nothing about whether the model is right, and the interval
    does not narrow with more draws.
    """
    if target_spread <= 0:
        raise ValueError("the target spread must be positive")
    ratio = observed_spread / target_spread
    return int(np.ceil(at_n * ratio * ratio))

The half_width measures one run’s answer width. The run-to-run spread measures how far the answer lands from one run to the next when only the seed changes. samples_needed takes the run-to-run spread and returns how many draws make it no more than a target. Given a half_width, it returns a count that means nothing, because the half-width does not fall as draws grow. The law is that ten times smaller spread costs a hundred times the draws. The table below measures this on the web service model’s five-year total: the model run many times at each sample count, each with its own seed, and a column heading says how many runs.

Samples90% interval half-widthRun-to-run spread of p95, 32 runs eachFall in spread from the row above
100$675,024$96,864—
1,000$707,485$29,4193.29×
10,000$707,758$14,4222.04×
100,000$708,422$4,2313.41×
settlesfalls2.64× per decade from 1,000 samples up, against √10 = 3.16

Source — convergence-tco · sizing.mc

The interval settles; the run-to-run spread falls. The half-width barely moves from the row for a thousand samples down. It comes from how uncertain the model’s inputs are, and more draws converge on it rather than shrinking it. The run-to-run spread keeps falling from row to row.

The last row shows how much it fell per tenfold step from a thousand samples up, and sets this against the square root of ten, which is what the law predicts. The single-row falls wander above and below that line. Each spread in the table is estimated from a limited number of runs, so it carries its own noise; read the last row, not any single row, to see the trend. The figure measured is the 95th percentile, a tail number that follows the law but more slowly than the mean does. tests/test_mc.py holds the mean to the law closely and the 95th percentile only loosely for that reason. So read the last row as a comparison with the law, not as a test it failed.

Shipping a distribution

#: Above this ratio between the 95th percentile and the 5th, equal-width bins stop describing the
#: quantity: most of the draws fall in the first bin and the rest of the picture is empty. A queue
#: near saturation does this. Deliberately measured across the interval rather than across the
#: extremes, because one stray draw from a long tail should not change how everything is binned.
LOG_BINNING_SPAN = 100.0


def histogram(x: np.ndarray, bins: int = 64) -> dict:
    """Compress a node's draws into counts and bin edges, small enough for the browser.

    By default that is 64 counts and 65 edges instead of 100,000 draws, for every node in the
    graph. This is what lets you click any node and see a narrow input become a wide output
    further down the chain.

    Bins are equal in width, or equal in ratio if the draws span more than
    :data:`LOG_BINNING_SPAN`; the result's ``spacing`` says which. Anything that draws the
    histogram must check ``spacing``: the same counts mean different things on the two axes.
    """
    x = np.asarray(x, dtype=float)
    low, high = float(x.min()), float(x.max())
    p5, p95 = (float(v) for v in np.percentile(x, [5, 95]))
    if low > 0 and p5 > 0 and p95 / p5 >= LOG_BINNING_SPAN:
        counts, edges = np.histogram(x, bins=np.geomspace(low, high, bins + 1))
        return {"counts": counts.tolist(), "edges": edges.tolist(), "spacing": "log"}
    counts, edges = np.histogram(x, bins=bins)
    return {"counts": counts.tolist(), "edges": edges.tolist(), "spacing": "linear"}

Every node whose value varies ships its distribution as a count per bin and the bin edges, computed by calling histogram with its default bins. The model viewer that the chapters embed uses these to draw whichever node you click, whether input, intermediate step or output, in its details panel.

The bins are equal in width unless the quantity spans orders of magnitude, in which case they are equal in ratio and spacing says which. To see a ratio-spaced histogram, open the model in ch06 and click requests in the system, which is a queue near saturation. The viewer labels such histograms to show the bars are equal in ratio. The book’s static figures read spacing too, and draw a logarithmic axis when it says so.

The inverse normal

Everything in sizing/mc.py that needs the normal distribution’s percentile function calls normal_ppf from sizing/normal.py: the lognormal and normal shapes, the normal scores in correlate, and the constant Z90. There is no closed form for it. sizing/normal.py implements Acklam’s rational approximation [Acklam (2003)]: two rational functions, one for the middle and one for the tails, switching at _TAIL; the coefficients are Acklam’s and the code is this repository’s. Python’s standard library includes statistics.NormalDist().inv_cdf, which is accurate but takes one number at a time, whereas the sampler works on arrays, so the approximation is used instead. tests/test_mc.py uses the standard library as its reference, checking they agree across the range, both tails included:

#: Where the central rational approximation stops being the accurate one. Below this and above
#: its mirror, the approximation works on ``sqrt(-2 ln p)`` instead — which is the tail's natural
#: variable, and the reason the accuracy holds out to one part in a billion rather than degrading
#: where the interesting percentiles are.
_TAIL = 0.02425

_CENTRAL_NUM = (
    -3.969683028665376e01,
    2.209460984245205e02,
    -2.759285104469687e02,
    1.383577518672690e02,
    -3.066479806614716e01,
    2.506628277459239e00,
)
_CENTRAL_DEN = (
    -5.447609879822406e01,
    1.615858368580409e02,
    -1.556989798598866e02,
    6.680131188771972e01,
    -1.328068155288572e01,
)
_TAIL_NUM = (
    -7.784894002430293e-03,
    -3.223964580411365e-01,
    -2.400758277161838e00,
    -2.549732539343734e00,
    4.374664141464968e00,
    2.938163982698783e00,
)
_TAIL_DEN = (
    7.784695709041462e-03,
    3.224671290700398e-01,
    2.445134137142996e00,
    3.754408661907416e00,
)


def _poly(coefficients: tuple[float, ...], x: np.ndarray) -> np.ndarray:
    """Horner's rule. ``_poly((a, b, c), x)`` is ``a*x**2 + b*x + c``."""
    out = np.zeros_like(x)
    for coefficient in coefficients:
        out = out * x + coefficient
    return out


def normal_ppf(u: np.ndarray | float) -> np.ndarray:
    """The value below which a fraction ``u`` of a standard normal distribution lies.

    ``normal_ppf(0.5)`` is 0, ``normal_ppf(0.9)`` is about 1.28, and ``normal_ppf(0.975)`` is the
    1.96 that turns up in every textbook margin of error.

    Raises on 0 and 1 rather than returning an infinity. numpy's generator draws from 0 up to but
    not including 1, so a draw of exactly 0 is possible, though vanishingly rare, and would raise
    here. An endpoint arriving here almost always means a percentile was computed rather than
    drawn: a real bug. Returning an infinity silently would turn the model's output into ``nan``
    several steps later.
    """
    u = np.asarray(u, dtype=float)
    if np.any((u <= 0.0) | (u >= 1.0)):
        raise ValueError("normal_ppf needs percentiles strictly between 0 and 1")

    out = np.empty_like(u)

    lower = u < _TAIL
    upper = u > 1.0 - _TAIL
    central = ~(lower | upper)

    # The middle 95%, as a rational function of the distance from the median.
    q = u[central] - 0.5
    r = q * q
    out[central] = _poly(_CENTRAL_NUM, r) * q / (_poly(_CENTRAL_DEN, r) * r + 1.0)

    # Both tails, in the variable that makes them well behaved. The upper tail is the lower one
    # reflected, which is worth doing explicitly: it is the only reason the function is accurate
    # at 0.999 as well as at 0.001, and a reader checking the code should be able to see that.
    for mask, tail_u, sign in ((lower, u[lower], 1.0), (upper, 1.0 - u[upper], -1.0)):
        q = np.sqrt(-2.0 * np.log(tail_u))
        out[mask] = sign * _poly(_TAIL_NUM, q) / (_poly(_TAIL_DEN, q) * q + 1.0)

    return out

Running it

python3 -m pytest tests/test_mc.py           # the sampler, against properties it must have
python3 -m bench.run_uncertainty             # the correlation and convergence experiments
By Chris Snow, in collaboration with Claude (Anthropic) · Prose and figures: CC BY-NC 4.0 · Code and models: Apache 2.0