When the Formula Runs Out
Most quantities worth knowing have no formula. The move that rescues them is to write the quantity as an average and then produce that average by running the thing many times.
A queue has customers arriving at random times and being served for random durations. How long does somebody wait on average?
There is a formula for the simplest version of this and it stops applying the moment anything realistic is added: two kinds of customer, a server that slows down when busy, a queue people abandon. The question is still perfectly definite and the algebra has run out.
The answer is to run the queue. Not once, but a hundred thousand times with different random arrivals, and take the average wait.
| a closed form exists? | can be written as an average? | can be simulated? | |
|---|---|---|---|
| the average of a thousand measured values | 1 | 1 | 1 |
| the area under a smooth curve on an interval | 0 | 1 | 1 |
| the average wait in a queue with abandonment | 0 | 1 | 1 |
| the chance a portfolio loses a fifth of its value | 0 | 1 | 1 |
- the quantity wanted, which has no formula
- a random draw of the kind the problem involves, such as one random arrival pattern
- whatever is computed from one draw, such as the wait that pattern produces
- the average value over all possible draws, weighted by how likely each is
- the ith actual draw made by the program
- the estimate, which is the ordinary average of the n computed values
The rewriting
A probability fits this shape without modification. Let f be one when the event happens and zero when it does not, and its average is the proportion of draws where it happened, which is the probability. Counting hits out of trials is not a different technique; it is this one with a particular f.
| step | the question | the random draw | what is computed from one draw | the average of it |
|---|---|---|---|---|
| 1 | what is the area under a curve between 0 and 1 | a point x drawn evenly in the interval | the height of the curve at x | the area, since the width is one |
| 2 | what is the chance five dice show at least three sixes | one roll of five dice | one if at least three are sixes, else zero | the probability wanted |
| 3 | how long does a customer wait on average | one random day of arrivals and service times | the mean wait on that simulated day | the long run average wait |
| 4 | what does a policy cost in the worst five per cent of years | one random year of demand | the cost in that year, kept only if it is in the worst five per cent | the conditional average, needing more care |
| 5 | the common structure | something drawable | something computable from one draw | the answer |
import numpy as np
rng = np.random.default_rng(0)
x = rng.random(100_000) # points drawn evenly in [0, 1]
print(np.mean(x ** 2)) # area under x squared: about 0.3333
rolls = rng.integers(1, 7, size=(100_000, 5))
hits = (rolls == 6).sum(axis=1) >= 3 # one or zero per trial
print(np.mean(hits)) # chance of three or more sixes: about 0.0355
rng2 = np.random.default_rng(1)
print(np.mean(rng2.random(100_000) ** 2)) # a different run, a different answerWhat is being traded
The answer is approximate and it changes from run to run. That is the whole cost, and it is acceptable because the size of the variation can be estimated from the same samples that produced the answer, which the lesson on error bars does.
The rule that follows is worth adopting immediately. A number produced this way is quoted with the uncertainty attached, as an estimate plus or minus something, and never as a bare figure. A bare figure invites everybody downstream to treat a noisy estimate as a measurement, and the third decimal place of a simulation is usually noise.
What comes next
Two things are needed before any of this works. The draws have to come from the right distribution, which is the next lesson, and the average has to be known to approach the quantity, which is the one after.
Recap
- A quantity with no closed form can still be computed if it can be written as the average of something you are able to draw at random. That rewriting is the whole technique, and everything else in the course is about doing it well.
- A probability is the average of a quantity that is one when the event happens and zero when it does not, which is why counting hits out of trials estimates it. Probabilities and averages are therefore the same problem.
- What is traded away is exactness. The answer arrives with an error that shrinks as more samples are drawn, so a simulated answer is incomplete until the size of that error is stated alongside it.
This is the reading half
Starting the course gives you your own copy of it. Every idea on every page has problems standing under it, marked with a reason rather than a tick, and any sentence you do not believe can be opened and argued with. None of that can happen on a page nobody owns.
The contents