SZ1, SZ2 - Simulating random variables

SZ1, SZ2 - Simulating random variables

Turn spreadsheet random numbers into discrete uniform, continuous uniform and Normal variables. You will build repeated trials, estimate probabilities and distributions from their output, and distinguish simulation variation from a faulty probability model.

From random decimals to equally likely integers

A spreadsheet's random-number function returns a decimal between 00 and 11. How can it roll a fair die when its output is almost never a whole-number die score?

Split its interval into six equal pieces, and give every piece a die score. The lengths of the pieces, rather than the number of decimal places displayed, determine the probabilities.

You already know

A discrete uniform variable gives equal probability to each of its specified values. A continuous uniform variable gives probabilities proportional to interval lengths. Independent trials require fresh independent random inputs.

Write UU for a modelled continuous uniform random number on [0,1][0,1]. A spreadsheet's RAND⁡()\operatorname{RAND}() provides a finite-precision version with 0≤U<10\le U<1. The missing upper endpoint has probability zero in the ideal continuous model.

[DIAGRAM: asset_name: Equal intervals generate fair die scores; asset_slug: uniform_integer_mapping; caption: Six equal input intervals give six equally likely scores; description: A horizontal interval from 0 to 1 is divided at exact sixths into six equal bands labelled die scores 1 to 6. A clay marker at U=0.79 falls in the fifth band, so its simulated die score is 5. Boundary labels use half-open intervals, with each boundary assigned to the band on its right.]
Six equal input intervals give six equally likely scores

Discrete uniform mapping

For integers L≤HL\le H, let m=H−L+1m=H-L+1. Then

D=L+⌊mU⌋,0≤U<1D=L+\lfloor mU\rfloor,\qquad 0\le U<1

is uniform on {L,L+1,…,H}\{L,L+1,\ldots,H\}. The floor ⌊z⌋\lfloor z\rfloor is the greatest integer not exceeding zz. For non-negative inputs, the spreadsheet function INT⁡\operatorname{INT} performs this step.

Not in the formula booklet: learn it.

For a die, the intervals are [0,1/6),[1/6,2/6),…,[5/6,1)[0,1/6),[1/6,2/6),\ldots,[5/6,1). Each has probability 1/61/6. In a spreadsheet, put =RAND⁡()=\operatorname{RAND}() in cell A2\text{A2}, then =1+INT⁡(6∗A2)=1+\operatorname{INT}(6*\text{A2}) in B2\text{B2}, and fill down. Within a spreadsheet formula, ∗* means multiplication.

Worked example 1: simulate a fair die

Question. Use U=0.08,0.36,0.79,0.99U=0.08,0.36,0.79,0.99 to simulate four scores on a fair six-sided die.

Plan. Multiply by six, take the integer part, then add one.

6U=0.48, 2.16, 4.74, 5.94scale the interval⌊6U⌋=0, 2, 4, 5integer partsD=1+⌊6U⌋=1, 3, 5, 6die labels\begin{aligned} 6U&=0.48,\ 2.16,\ 4.74,\ 5.94 && \text{scale the interval}\\ \lfloor6U\rfloor&=0,\ 2,\ 4,\ 5 && \text{integer parts}\\ D=1+\lfloor6U\rfloor&=1,\ 3,\ 5,\ 6 && \text{die labels} \end{aligned}

Why this step? The integer parts run from 00 to 55; adding one gives the required labels.

Answer. Scores 1,3,5,61,3,5,6.

Check. The largest input still gives 66, never 77, because U<1U<1.

Change only the number of possible scores from six to four, using the same inputs.

Worked example 2: four equally likely labels

Question. Simulate values uniform on {1,2,3,4}\{1,2,3,4\} from the same four random numbers.

Plan. Replace the scale factor by four while retaining the add-one step.

4U=0.32, 1.44, 3.16, 3.96four equal intervals⌊4U⌋=0, 1, 3, 3integer partsD=1+⌊4U⌋=1, 2, 4, 4required labels\begin{aligned} 4U&=0.32,\ 1.44,\ 3.16,\ 3.96 && \text{four equal intervals}\\ \lfloor4U\rfloor&=0,\ 1,\ 3,\ 3 && \text{integer parts}\\ D=1+\lfloor4U\rfloor&=1,\ 2,\ 4,\ 4 && \text{required labels} \end{aligned}

Why this step? Repeated simulated values are allowed; equal probabilities do not force every label to appear once.

Answer. 1,2,4,41,2,4,4.

Check. Each label still corresponds to an input interval of length 1/41/4.

The distribution is uniform even though this short sample does not contain equal counts of all four values. For non-consecutive labels, use the integer as an index: assigning indices 1,2,3,41,2,3,4 to labels 2,7,11,202,7,11,20 makes those four labels equally likely too.

Watch out

Wrong: round 6U6U to the nearest integer for a die. This can give 00, and the endpoint intervals have different lengths. Right: use 1+⌊6U⌋1+\lfloor6U\rfloor.

Try a new number of labels and inputs, including an exact interval boundary.

Stretch and shift for a continuous uniform variable

A continuous uniform variable needs no rounding to integer labels. Stretch the input interval to the required width, then shift its left endpoint.

Continuous uniform mapping

For a<ba<b,

X=a+(b−a)UX=a+(b-a)U

has a continuous uniform distribution on [a,b][a,b]. Equal-length output intervals come from equal-length input intervals. In finite-precision simulation with U<1U<1, the output is below bb; this endpoint distinction does not change the ideal continuous probabilities.

Not in the formula booklet: learn it.

The width is b−ab-a, not bb. For a service time uniform on [3,11][3,11] minutes, a spreadsheet cell can use =3+8∗RAND⁡()=3+8*\operatorname{RAND}().

Worked example 3: a bounded service time

Question. Simulate three service times uniform on [3,11][3,11] minutes using U=0.15,0.52,0.91U=0.15,0.52,0.91.

Plan. Multiply each input by the width eight, then add the minimum time three.

X=3+8Uwidth and offset8U=1.20, 4.16, 7.28stretchX=4.20, 7.16, 10.28shift\begin{aligned} X&=3+8U && \text{width and offset}\\ 8U&=1.20,\ 4.16,\ 7.28 && \text{stretch}\\ X&=4.20,\ 7.16,\ 10.28 && \text{shift} \end{aligned}

Why this step? Multiplying by eleven would create the wrong width, even if you then added three.

Answer. 4.20,7.16,10.284.20,7.16,10.28 minutes.

Check. Every value lies within the required interval; U=0U=0 would give exactly 33.

Change only the upper endpoint from eleven to fifteen minutes.

Worked example 4: a wider interval

Question. Use the same inputs for service times uniform on [3,15][3,15] minutes.

Plan. Keep the offset three and change the width to twelve.

X=3+12Unew interval width12U=1.80, 6.24, 10.92same random inputsX=4.80, 9.24, 13.92add the minimum\begin{aligned} X&=3+12U && \text{new interval width}\\ 12U&=1.80,\ 6.24,\ 10.92 && \text{same random inputs}\\ X&=4.80,\ 9.24,\ 13.92 && \text{add the minimum} \end{aligned}

Why this step? The input's relative position within the interval stays the same while the interval becomes wider.

Answer. 4.80,9.24,13.924.80,9.24,13.92 minutes.

Check. At U=1/2U=1/2, the mapping gives the midpoint 99, as a uniform transformation should.

A random output is not automatically uniform: a nonlinear transformation such as U2U^2 puts more output values near zero.

Watch out

Wrong: use 3+15U3+15U for [3,15][3,15]. Right: use 3+(15−3)U3+(15-3)U; the proposed wrong expression reaches towards 1818.

Retain the minimum time and change only the maximum again.

Use a Normal inverse function

A Normal variable needs more values near its mean and fewer in its tails. An affine transformation of a uniform input remains uniform, so it cannot produce that shape.

Let Φ\Phi be the standard Normal cumulative distribution function. Its inverse takes an accumulated probability and returns the corresponding standard Normal value.

Normal simulation

For 0<U<10<U<1,

Z=Φ−1(U),X=μ+σZZ=\Phi^{-1}(U),\qquad X=\mu+\sigma Z

simulates X∼N(μ,σ2)X\sim N(\mu,\sigma^2), where σ>0\sigma>0. A spreadsheet inverse Normal function can do both steps: NORM.INV⁡(U,μ,σ)\operatorname{NORM.INV}(U,\mu,\sigma).

Not in the formula booklet: learn it.

The inverse works because Φ\Phi is increasing:

P(Z≤z)=P(U≤Φ(z))=Φ(z)P(Z\le z)=P(U\le\Phi(z))=\Phi(z)

Thus the output has the required standard Normal cumulative probabilities.

Put =RAND⁡()=\operatorname{RAND}() in A2\text{A2} and, for N(60,25)N(60,25), put =NORM.INV⁡(A2,60,5)=\operatorname{NORM.INV}(\text{A2},60,5) in B2\text{B2}. The final argument is the standard deviation, not the variance. Regenerate an input if it is an endpoint 00 or 11, where a finite Normal quantile is unavailable.

Worked example 5: one Normal observation

Question. Simulate an observation from N(60,25)N(60,25) using U=0.9U=0.9. Give it to three decimal places.

Plan. Convert the probability to a standard Normal quantile, then use standard deviation five.

σ=25=5variance to standard deviationz=Φ−1(0.9)=1.281551…inverse Normalx=60+5z=66.407757…shift and scale\begin{aligned} \sigma&=\sqrt{25}=5 && \text{variance to standard deviation}\\ z=\Phi^{-1}(0.9)&=1.281551\ldots && \text{inverse Normal}\\ x&=60+5z=66.407757\ldots && \text{shift and scale} \end{aligned}

Why this step? 0.90.9 is a cumulative probability, not a standard Normal observation.

Answer. 66.40866.408.

Check. The value exceeds the mean, and its standardised value has cumulative probability 0.90.9.

Change only the mean from sixty to ninety, retaining the same variance and random input.

Worked example 6: shift a Normal model

Question. Simulate from N(90,25)N(90,25) using U=0.9U=0.9.

Plan. Keep the same quantile and standard deviation; change the location.

z=1.281551…same input probabilityx=90+5z=96.407757…new meanx=96.408three decimal places\begin{aligned} z&=1.281551\ldots && \text{same input probability}\\ x&=90+5z=96.407757\ldots && \text{new mean}\\ x&=96.408 && \text{three decimal places} \end{aligned}

Why this step? Changing the mean translates all corresponding simulated values; it does not change their spread.

Answer. 96.40896.408.

Check. The result is exactly thirty greater before rounding than the first example's result.

Using the same input deliberately makes these two examples comparable. To generate independent variables in one trial, use separate random inputs instead.

Watch out

Wrong: enter 2525 as the standard deviation for N(90,25)N(90,25). Right: enter 55, because the second Normal parameter is variance.

Change the variance and use a lower-tail input.

Turn complete trials into probabilities

Suppose two independent waiting times are each uniform on [0,8][0,8] minutes. One trial must generate both waits, then calculate their total.

Use separate random calls for XX and YY. Reusing one value would make the waits equal rather than independent.

CellFormulaMeaning
A2\text{A2}=8∗RAND⁡()=8*\operatorname{RAND}()First wait XX
B2\text{B2}=8∗RAND⁡()=8*\operatorname{RAND}()Independent second wait YY
C2\text{C2}=A2+B2=\text{A2}+\text{B2}Total T=X+YT=X+Y
D2\text{D2}=IF⁡(C2>10,1,0)=\operatorname{IF}(\text{C2}>10,1,0)Whether this total exceeds ten

Fill the row down for repeated independent trials. Sum the indicator column to count successes. Store a run's values if you need to retain them while the spreadsheet recalculates.

Simulation estimate

With rr successes in NN independent trials,

p^=rN\widehat p=\frac rN

estimates the model probability of the event. For a distribution, divide each value's or class interval's frequency by NN. All classes together must include every trial exactly once.

Not in the formula booklet: learn it.

Here is a small demonstration run using two-decimal random inputs; use the displayed values as the trial values.

TrialXXYYT=X+YT=X+Y
116.646.641.441.448.088.08
222.562.564.004.006.566.56
333.523.526.806.8010.3210.32
440.400.402.482.482.882.88
552.562.561.841.844.404.40
666.326.326.486.4812.8012.80
772.082.082.482.484.564.56
883.123.125.925.929.049.04
997.367.367.687.6815.0415.04
10104.004.000.640.644.644.64
11115.845.842.402.408.248.24
12124.004.006.086.0810.0810.08

Continue this lesson.

You’re previewing NovaLearn without an account. Create a free account to read the rest.

Create free account