Back to trackLesson 10 of 25

Startup Runway 5: Is My Simulation Right

Uses Only Free-Tier SpiceGrinder

Problem statement

Every number in the last four lessons came with a check against exact theory, and every check passed. That was possible because the runway is a small problem with a known answer. Most simulations have no exact theory to check against, and a model can pass every check we have used so far and still be a poor picture of the business. Two different questions hide in “is my simulation right”:

  • Verification: does the model do what we meant? Is the code (here, the model file) a correct version of the idea?
  • Validation: does the idea match reality? Do the numbers the model makes look like the numbers the business makes?

Verification is a question about us, and it can be answered completely: the model either does what we meant or it does not. Validation is a question about the world, and it can only be answered partly, with the data there is. The lesson does both for the runway, and ends on the practical question: when is a simulation good enough to base a decision on?

The founder now has 24 months of books: the net cash flow of each month in $k. We made them up for this lesson. Their mean is -9.25 and their standard deviation 13.98, near the model’s -8 and 12. The questions:

  • Does the lesson 2.2 model compute the runway it was meant to compute?
  • Does a model of the monthly cash flow agree with the 24 months of books?
  • If it agrees, does that show the model is right? If it does not, how sure can we be that it is wrong?
  • What can 24 months not tell us?

Verification: recompute it another way

The cheapest independent check is to compute the same quantity a second way, with different code, and compare. The runway model of lesson 2.2 (samples/simulation/cash-runway.xml) makes 2,000 years of 12 balances, the lowest balance and the ruin flag. Its inputs are the monthly flows, and Grind --node NetFlow prints those flows exactly as the model drew them: every node has its own random stream, so asking for one node’s output gives the same numbers the whole model used. That is all we need. A few lines of Python can rebuild the rest from the flows:

import subprocess

def grind(*args):
    out = subprocess.run(["grind", *args, "samples/simulation/cash-runway.xml"],
                         capture_output=True, text=True, check=True).stdout
    return [[float(v) for v in line.split(",")] for line in out.splitlines()]

rows  = grind("--count", "2000")                        # the model's output: 14 numbers per path
flows = grind("--count", "24000", "--node", "NetFlow")  # the 24,000 monthly flows it drew

worst = bad = 0
for r, row in enumerate(rows):
    balance, low = 120.0, float("inf")
    for m in range(12):
        balance += flows[12 * r + m][0]
        low = min(low, balance)
        worst = max(worst, abs(balance - row[m]))
    if low != row[12] or (low < 0) != (row[13] == 1):
        bad += 1
print(worst, bad)

This uses the sample’s own seed, 42. It prints 0 0. The largest difference between Python’s running balance and the model’s over all 24,000 numbers is exactly zero, and in none of the 2,000 paths do the low point or the ruin flag disagree. Of the 2,000 paths, 604 were ruined (30.2%, standard error 1.0 points; lesson 2.2’s exact value is 30.0%).

Here is the first path, with the model’s own row beside it:

Month123456789101112
Flow-16.53-14.38-16.00-3.1417.39-19.79-9.342.53-9.99-13.87-6.22-7.89
Balance103.4789.0973.0869.9487.3367.5458.2060.7350.7436.8730.6522.76

The lowest balance is 22.76 (the last month), above zero, so no ruin; the model’s row says the same. The first ruined path in the run is the seventh (row 6). Its flows are -6.56 -7.53 0.16 -17.02 -16.74 -7.65 -14.87 -7.61 -6.66 -23.85 -6.97 -22.90, its balances fall from 113.44 to 4.70 in month 11 and to -18.20 in month 12; the lowest balance is -18.20, ruin 1, and the model’s row agrees.

Exact agreement is a strong result, and it is also all verification can give. It shows that the model’s balance, low point and ruin flag are what we described. It says nothing about whether a founder’s cash flow is a Normal with mean -8 and standard deviation 12. Verification has other forms, all used in this module:

  • Theory checks: the Normal’s ruin probability against a numerical solution of the same random walk (lessons 2.2 to 2.4), the sum of 12 months against its exact mean and spread (lesson 2.1).
  • Internal consistency: the low point is the minimum of the 12 balances; ruin is 1 exactly when it is below zero.
  • Running it twice: the same seed gives the same file; the golden-output tests in the repository enforce it.
  • Reading the output: a few rows by hand, like the table above.

None of these needs data from the business. That is their strength and their limit.

The model for validation

Validation needs a model of something we have data for. The data is the monthly net cash flow, so the model is the monthly flow on its own, without the balance and the year. It is two nodes:

<dataset columns="netFlow" seed="205">
  <root node="NetFlow"/>
  <nodes>
    <Normal name="RawFlow" mean="-8" sigma="12"/>
    <ToInteger name="NetFlow" mode="round">
      <input name="RawFlow"/>
    </ToInteger>
  </nodes>
</dataset>

RawFlow is lesson 2.1’s monthly flow, Normal(-8, 12). NetFlow rounds it to whole $k, because the founder’s books are in whole $k and the comparison tool checks the number of decimal places. The model is samples/simulation/cash-runway-flow-model.xml; one row is one month. The first ten rows with its seed, 205:

Row0123456789
Flow ($k)-48-26-289-37-3914-199

The founder’s history is in samples/simulation/cash-runway-founder-history.csv: a header and 24 values. It is data to compare with, not an output of any model.

Comparing a model with data

ModelCompareApp (the compare command) generates rows from a model, takes the data file, and compares them column by column: the shape of the distribution (Kolmogorov-Smirnov distance, KS for short, defined below), the tail percentiles, how much of the output falls outside the data’s range, repeated values, formats. Each difference it finds gets a severity, and the exit status is 0 when there is no high-severity finding and 1 when there is at least one.

compare --seed 1 --count 24 cash-runway-flow-model.xml cash-runway-founder-history.csv
=== SpiceGrinder Sample Comparison ===
sample rows: 24, generated rows: 24, columns: 1
findings: 0 high, 2 medium, 0 low

  medium net_cash_flow_k          distribution differs (KS distance 0.125)
  medium net_cash_flow_k          4.2% of generated values fall outside the sample's range [-36, 19]

Columns:
  net_cash_flow_k (numeric): KS 0.125, Wasserstein 0.199 IQR, p50 -8.5 -> -7.5, p99 17.85 -> 17.47; e.g. "-12" -> "20"

The KS distance is the largest vertical gap between two cumulative curves: for each value on the horizontal axis, the share of months at or below it in the data and in the model, and the biggest difference over all values. It is 0 when the curves coincide and 1 when they never meet. The tool calls it medium above 0.1 and high above 0.2.

By default compare generates as many rows as the data has, here 24, so the model’s side is as noisy as the data’s. --count 24000 gives the model a large sample and removes its noise, which is what we want when the question is whether the data looks like the model. Here are four runs, with the right model and with a wrong copy whose sigma is 6 instead of 12 (the same file with that one number changed, in a scratch directory), seed 1:

ModelGenerated rowsExitFindingsKS distanceWhat it says
sigma 122400 high, 2 medium0.125distribution differs; 4.2% of the output outside the data’s range
sigma 1224,00000 high, 1 medium0.0961.9% of the output outside the data’s range [-36, 19]
sigma 62411 high, 1 medium0.250“distribution differs a lot”; the 1st percentile is -19.85 against -35.31 in the data
sigma 624,00011 high, 1 medium0.214“distribution differs a lot”; 1st percentile -22 against -35.31 (3.6 standard errors apart)

The founder's 24 monthly cash flows as a staircase of cumulative shares, with the smooth curves of the right model and of the model with sigma 6; the right model's curve follows the staircase closely, the sigma 6 curve is much steeper and has a clearly larger gap

The picture is the comparison. The right model’s curve stays near the staircase of the 24 months, the sigma 6 curve is steeper than the data and leaves it by 0.21. The right model is not “clean”, though: it gets one or two medium findings. We come back to that below, because it matters more than the result of any one run.

Does a pass mean the model is right?

Look at what the comparison above did. The right model passed (exit 0) and the wrong one failed (exit 1). It would be tempting to say the check works. The measurement that matters is how often it does so, and the founder’s 24 months are one history among very many. To find out, we need histories whose source we know. So we draw them: 200 histories from the true model, each from its own seed (1001 to 1200), at 24, 48, 96, 192, 384 and 768 months, and compare each with the right model and with the sigma 6 one. In the loop, using a version of the model without the rounding (so that two months almost never have the same value; we come back to this), 24 months, one history per seed:

for s in $(seq 1001 1200); do
  (echo net_cash_flow_k; grind --seed $s --count 24 flow-exact.xml) > h$s.csv
  compare --seed 1 --count 5000 flow-exact.xml h$s.csv > /dev/null; echo $?
done | sort | uniq -c

For the true model this prints 100 zeros and 100 ones. Half of the 24-month histories drawn from the model itself get a high finding. (Each comparison uses 5,000 generated rows, which is plenty to take the model’s side out of the noise; 24,000 would cost five times as much.) The full picture, for 200 histories at each length, shares by the worst finding:

Months of historyHigh finding (exit 1)Medium onlyCleanMedian KS
2450.0%50.0%0.0%0.159
4826.5%72.0%1.5%0.110
966.0%75.0%19.0%0.081
1922.0%45.0%53.0%0.060
3840.0%17.0%83.0%0.043
7680.5%5.0%94.5%0.033

This is the right model on data it made itself. Every high finding in this table is a false alarm. The main reason is the one the KS column shows: a history of 24 months is a small sample, and the largest gap between a small sample’s cumulative curve and the curve it came from is typically 0.16, well above the tool’s 0.1 limit for “medium”, and above its 0.2 limit for “high” one time in four. The tool’s thresholds are right for thousands of rows. At 24 they are tighter than the noise. The tail percentiles make it worse: the 1st and 99th percentile of 24 values are close to the smallest and the largest value, and the tool underestimates their sampling error, so the tails raise a high finding in 77 of the 200 histories, the distribution check in 45.

KS distance against months of history, median and middle 80% of 200 histories for the right model and for the sigma 6 model, with the 0.1 and 0.2 thresholds; the right model's band falls from above 0.1 at 24 months to 0.03 at 768, the sigma 6 band stays near 0.17 to 0.26

How many months does the right model need before it stops raising medium findings? The “Clean” column answers it: at 96 months about one history in five is clean, at 192 one in two, at 384 five in six, and at 768 months (64 years) 94.5% are clean. The medium findings that remain at 192 and 384 months are mostly that more than 1% of the output falls outside the history’s range, which is a statement about the extremes of a finite sample. The founder, with 24, would never see a clean report from the right model.

The founder’s own history was friendly: its KS distance to the right model is 0.096 (with 24,000 generated rows), and only 4% of the 200 histories of 24 months came as close, and its 24 values are all different. The next section says why that matters too.

What a short history can reject

The other half of the question is whether the check catches a model that is wrong. Take the same 200 histories, still drawn from the true model, and compare them with the sigma 6 model:

Months of historySigma 6 model: high findingRight model: high finding
2483.0%50.0%
4872.0%26.5%
9671.0%6.0%
19296.5%2.0%
384100%0.0%
768100%0.5%

At 24 months the wrong model is flagged in 83% of the histories (and the other 17% get medium findings only), the right model in 50%. A single run at this length cannot tell which is which, and the founder’s was a lucky one for the right model. With 96 months the right model is almost never flagged, but the wrong one is still missed three times in ten. The dip is real: the largest possible gap between the sigma 6 curve and the sigma 12 curve is 0.161, less than the “high” limit of 0.2, so the KS check is no longer enough; what flags the wrong model at 192 months and more is the tail percentiles (193 of 200 histories at 192 months, all 200 at 384).

So the answer to “does a pass mean the model is right?” is no, and it is also no for a fail: 24 months can reject a gross error, with some luck, but it cannot confirm a model. The sigma 6 model is a big error (the spread is half what it should be), and still 17% of the time the check lets it through with medium findings, which the right model also gets.

Share of 200 histories that get the finding named, for each length of history, for several combinations of history and model; the false alarms of the right model fall from 50% at 24 months to 0.5% at 768, while the sigma 6 model and the Mix model on a Normal history are caught most of the time

One more thing, which has nothing to do with the model. The tool also looks at repeated values: if one value is 5% or more of the data and the model makes it far less often, that is a high finding (it is built for data in which one amount, one price or one code repeats). The sample model rounds to whole $k, like the founder’s books, and rounding makes months tie. With 24 months in whole $k, the chance that all 24 are different is 0.056%. The founder’s history is that rare case, so it passed the repeated-values check. In 200 histories of 24 and of 48 whole-$k months drawn from the right model, every one got a high finding for a repeated value, and at 96 months 175 of 200 still did (shown as the dotted line in the figure above). Rounding is what the books do, but a short history in whole numbers is full of ties that mean nothing. For the repeated runs above we used the model without the rounding, so that they measure the shape of the distribution, not the ties.

What the check does not see

ModelCompareApp looks at each month’s value and at nothing else. Lesson 2.3 built the Mix (stress months at random) and lesson 2.4 the regimes (stress months in stretches), and they have the same mean, the same spread and the same month-by-month distribution, which differs from the Normal’s in shape. Two measurements show what the check can and cannot tell about them.

First, can it tell the shape? The Normal model against 200 histories drawn from the Mix, and from the regimes:

Months of historyNormal model on Mix histories: highNormal model on regime histories: high
2499.0%99.5%
4899.0%98.0%
96100%98.0%
192 and more100%100%

Yes: the Mix’s real distance from the Normal is 0.210 (it has a sharp peak at the calm months and a long tail of stressed ones), and the check sees it. At 24 months, though, this is a detection rate of 99% against a false-alarm rate of 50%, so it tells little; from 96 months on it is 100% against 6%.

Second, can it tell the order of the months? Take the Mix model and test it against Mix histories and against regime histories, which have exactly the same month-by-month distribution:

Months of historyMix model on Mix histories: highMix model on regime histories: high
2450.5%72.5%
4820.0%36.5%
963.5%15.5%
1920.0%8.5%
3840.0%3.5%
7680.0%1.0%

No. The Mix model is just as acceptable on regime histories as on the Mix’s own, and the check has no way to say that the months in the regime histories come in stretches. The regime histories are flagged more often. That is a trace of the dependence: months in the same stretch move together, so a window of the regimes behaves like a sample with fewer independent months, and the tool’s error bars, which assume independent months, are too narrow. It is a sign that something is off, and it does not say what.

The founder’s 24 months, checked against the lesson 2.3 and 2.4 models (rounded to whole $k, --count 24000, seed 1), give exit status 1 for both: KS distances of 0.249 for the Mix and 0.246 for the regimes, and a first percentile of -61 against -35 in the data (the stressed months can be much worse than anything in the books). That points towards the Normal. But since the right model gets a high finding in half of 24-month histories, a high finding on 24 months is weak evidence about a model, and the data cannot say whether the founder’s business has stress months at all. The books show no month worse than -36 in two years. A stress month in the Mix is a one-in-ten chance, so two years would show two or three of them on average; a lucky history without any is not impossible, and the checks have no way to find out.

When is a simulation fit for a decision?

A simulation is not right or wrong in general. It is right enough for a decision, or not. The two kinds of check give the two halves of that judgement:

  • Verification tells you the model does what you said. It must be complete: if it is not, there is a bug, and nothing else is worth discussing.
  • Validation tells you which differences from reality the data can rule out. Everything it cannot rule out is still a possible truth, and the model has to be good enough for all of them.

A simulation is fit for a decision when the decision does not change inside the range the checks cannot rule out. The runway shows what that looks like. All three models have the same monthly mean and spread. The founder’s 24 months lean towards the Normal, but as we saw that is weak evidence at this length, and nothing the check can do separates the Mix from the regimes. The chance of ruin within the year from a start of $120k:

ModelChance of ruin at $120kAt $200k
Normal (lesson 2.2)30.0%0.65%
Mix (lesson 2.3)25.5%1.9%
Regimes (lesson 2.4)23.4%8.3%

At $120k the three answers are 23% to 30%: between one in four and three in ten. A founder who would raise money whenever the chance of ruin is above 15% takes the same decision under all three, so for that decision the unresolved question does not matter, and the simulation is fit for it. At $200k the same three models give 0.65%, 1.9% and 8.3%. A founder who would raise money above 5% takes opposite decisions under the Normal and under the regimes, and the one thing the checks cannot see, the order of the months, decides which. For that decision the simulation is not fit yet: what is needed is not more verification or a longer history of monthly values but evidence about whether bad months come in stretches, such as the dates of the months, the reasons for the worst ones, and what happened in similar businesses. (The 5% and 15% are invented for the example; the point is that a threshold exists, and where it falls relative to the spread of answers.)

Check: do the numbers match the math?

The verification. The only check that can be exact: 0 difference over 24,000 numbers, 0 disagreeing rows out of 2,000.

The exit status and the findings. Exit 0 for the right model in both runs and 1 for the sigma 6 model in both, as the table of findings says. The KS distances in the tool’s report (0.125, 0.096, 0.250, 0.214) are the largest gaps between the cumulative curves, and recomputing them outside the tool, from the 24 values and the generated rows, gives the same four numbers to three decimals (the gap is at -17 for three of them and at -31 for the 0.096 one).

The noise of a short history. The KS distance of a history from the model that made it follows a known distribution (Kolmogorov’s) that does not depend on the model. A simulation of that statistic, 20,000 histories for each of 24, 48 and 96 months and 4,000 for the longer ones, gives these shares above the tool’s 0.1 limit: 95.0% at 24 months, 68.5% at 48, 27.4% at 96, 4.2% at 192 and none beyond. The comparison runs measured 95.5%, 60.0%, 25.5%, 4.0% and none. Above 0.2: theory 25.6%, 3.6%, 0.1% at 24, 48 and 96 months; measured 22.5%, 2.5%, 0.0%. The medians, 0.162, 0.116 and 0.083 against 0.159, 0.110 and 0.081 measured. With 200 histories the measured shares have a standard error of about 3 points, so these agree within about that, except the 48-month share above 0.1, where the measured 60.0% is 2.6 standard errors below the 68.5% of theory; the tool’s KS is a two-sample distance against 5,000 generated values, not the one-sample distance of the simulation, and the difference shows most where the share is changing fastest. The 50% of high findings in 24 months is therefore the tool’s thresholds against honest sampling noise, not a fault in the runs: the KS part is 22%, and the tails make up the rest.

Reproducing it. Each history of N months from seed S is what grind --seed S --count N prints for the model, and the comparison uses a fixed seed; running the script for this lesson twice gives the same tables.

What the model leaves out

The monthly flow model is the Normal of lesson 2.1: it has no memory, no trend and no seasonality, and it does not know what the founder will do. The history is 24 invented numbers; a real one would have its own ties, its own trend (revenue growth moves the mean) and its own bad months. The comparison looks at one column. A model with several columns, such as the support desk queue to come, would also be compared on the relationships between them, which the tool does for pairs of columns only. And a history is never the future: a model that matches the past perfectly can still fail when the business changes.

What this gives you: the difference between verification (does the model do what you meant, answered exactly, here by recomputing 2,000 paths and finding no difference) and validation (does it match the world, answered only partly); a way to compare a model with data with compare, and what its exit status and findings mean; the lesson that a short history can reject a gross error but cannot confirm a model, with the numbers behind it (the right model gets a high finding in half of 24-month histories, 26.5% at 48 months and 6% at 96, and needs about 768 months before 19 of 20 histories are clean; the sigma 6 model is caught in 83% of 24-month histories); what the check does not see (the order of the months); and a way to judge fitness for a decision: whether the decision changes inside the range the checks cannot rule out. The companion page lets you pick where the history comes from, which model is tested and how many months there are, and shows the shares of 200 histories that come out clean, medium or high.

That closes the module. Lessons 2.1 to 2.4 built a year of months from independent draws, and then, in 2.4, forced memory into a model by holding a state for a random number of rows. It worked, but it took 14 nodes, a Pool and two Switches, and the memory lived in the way the rows were laid out. Module 3 is about the other way to give a model memory: rows that remember the previous row. A running balance, a backlog or a queue is a state that each row updates from the last one, and Recur does exactly that. Lesson 3.1 starts by rebuilding the runway of lesson 2.1 with one Recur term in place of the 78-term formula, and shows that it produces the very same numbers.