Basis Lab

Answering With Random Samples

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.

FIG 1Four questions, and what is available for each
a closed form exists?can be written as an avecan be simulated?
the average of a thousan111
the area under a smooth 011
the average wait in a qu011
the chance a portfolio l011
The first row needs no simulation, since the answer is a direct computation. The marked cells are the ordinary situation: most integrals have no elementary antiderivative, and most systems with two or more random parts interacting have no closed form for anything about them. Note that the second column is true in every row, which is the claim this course rests on: an enormous range of questions can be written as the average of something drawable, and that rewriting is what makes them answerable at all.
FIG 2The quantity, written as an average
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 left expression is a definition of the quantity and gives no way to compute it. The right one is a loop. Everything this course does is in the gap between them: why the right side approaches the left, how far apart they are after n draws, and how to make each draw worth more. Note that f can be anything computable, including a whole simulation of a system, which is why the method reaches problems that have no mathematical description at all beyond the program that defines them.

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.

FIG 3Turning two questions into averages
stepthe questionthe random drawwhat is computed from one drawthe average of it
1what is the area under a curve between 0 and 1a point x drawn evenly in the intervalthe height of the curve at xthe area, since the width is one
2what is the chance five dice show at least three sixesone roll of five diceone if at least three are sixes, else zerothe probability wanted
3how long does a customer wait on averageone random day of arrivals and service timesthe mean wait on that simulated daythe long run average wait
4what does a policy cost in the worst five per cent of yearsone random year of demandthe cost in that year, kept only if it is in the worst five per centthe conditional average, needing more care
5the common structuresomething drawablesomething computable from one drawthe answer
5 steps
The first three are direct, and the fourth is marked as needing care for a reason worth knowing now: an average taken over only the worst five per cent of draws throws away ninety five per cent of the work, so its error is far larger than the sample count suggests. That is the problem the lesson on making each sample count exists to solve. The last row is the shape to look for in any new problem: name the draw, name what is computed from one draw, and check that the average of it is what was asked.
FIG 4Two estimates, each a loop and an average
python
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 answer
The first estimate is an area, computed as the average height of a curve at random points. The exact value is one third, and a hundred thousand samples lands within a few thousandths of it. The second is a probability, computed as the average of a quantity that is one on success and zero otherwise, which is why the same two lines serve both. The last line is the thing to notice: run the whole file again with a different seed and both numbers change in the third decimal place, which is the error this course is about measuring.

What 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

NextMaking a Random Draw →

The rest of this course

  1. 01When the Formula Runs Outyou are here
  2. 02Making a Random Drawopening only
  3. 03The Average Is the Answeropening only
  4. 04How Far Off Is Itopening only
  5. 05Making Each Sample Countopening only
  6. 06Running the Whole Processopening only
  7. 07Using the Data You Have Twiceopening only
  8. 08When a Simulation Liesopening only

Read alongside