Simulation is about variability: the inputs of a model are random, so its output is random too. This notebook recaps the probability and statistics from the statistics part of the course that Lectures 12 and 13 use. It is a reference, not new material, so skim what you already know; Triola (2017) and Ross (2002) treat these topics in more detail. The simulation material starts in Why Simulate?. How to sample random numbers and compute these quantities in Python follows in Sampling a Random Variable and Monte Carlo Simulation.
Learning outcomes
On completion of this notebook, you will be able to:
distinguish discrete and continuous random variables and describe them with a pmf, pdf or cdf
compute the expectation and variance of a random variable, and of sums and averages of random variables
explain the law of large numbers and the central limit theorem
construct and interpret a confidence interval for an expectation
explain how a hypothesis test compares two expectations
A random variable (RV) X models an uncertain outcome, such as the duration of an activity or the number of customers that arrive in an hour. Random variables come in two types.
A discrete RV takes a finite or countable number of values, for example the sum of two dice or a number of arrivals (0,1,2,…). It is described by its probability mass function (pmf) P(X=x). For the sum of two dice, 6 of the 36 equally likely outcomes give 7, so P(X=7)=6/36=1/6.
A continuous RV can take any value in a range, for example a duration. Every single value then has probability 0: nobody is exactly 1.80 m tall when measured to ten decimals. Instead, a probability density function (pdf) f(x) gives probabilities of intervals, P(a≤X≤b)=∫abf(x)dx. Where the density is high, outcomes are more likely.
Both types have a cumulative distribution function (cdf) F(x)=P(X≤x). It is non-decreasing, goes from 0 to 1, and is a step function for a discrete RV. For X uniform on [a,b], every subinterval of the same length is equally likely and
Two RVs are independent if the outcome of one does not change the probabilities of the other. Repeated experiments, such as simulation runs, are usually independent; activities that depend on the same weather or the same interest rate are not.
import plotly.graph_objects as go
TEXT_COLOR = "#111827"
CDF_COLOR = "#1f77b4"
jumps = [0, 1, 2, 3]
levels = [8 / 27, 20 / 27, 26 / 27, 1]
cdf_fig = go.Figure()
# horizontal steps: F is constant between two jumps
step_starts = [-1] + jumps
step_ends = jumps + [4]
for start, end, level in zip(step_starts, step_ends, [0] + levels):
cdf_fig.add_trace(
go.Scatter(
x=[start, end],
y=[level, level],
mode="lines",
line={"color": CDF_COLOR, "width": 3},
)
)
# dotted jumps, an open dot below and a closed dot on top: F(x) is the
# upper value at a jump
for jump, low, high in zip(jumps, [0] + levels[:-1], levels):
cdf_fig.add_trace(
go.Scatter(
x=[jump, jump],
y=[low, high],
mode="lines",
line={"color": CDF_COLOR, "width": 2, "dash": "dot"},
)
)
cdf_fig.add_trace(
go.Scatter(
x=jumps,
y=[0] + levels[:-1],
mode="markers",
marker={
"symbol": "circle-open",
"size": 10,
"color": CDF_COLOR,
"line": {"width": 2},
},
)
)
cdf_fig.add_trace(
go.Scatter(
x=jumps,
y=levels,
mode="markers",
marker={"size": 10, "color": CDF_COLOR},
)
)
cdf_fig.update_layout(
paper_bgcolor="rgba(0,0,0,0)",
plot_bgcolor="rgba(0,0,0,0)",
font={"color": TEXT_COLOR, "size": 13},
margin={"l": 50, "r": 20, "t": 20, "b": 50},
height=320,
showlegend=False,
)
cdf_fig.update_xaxes(
title="x", range=[-1, 4], dtick=1, gridcolor="rgba(128,128,128,0.25)"
)
cdf_fig.update_yaxes(
title="F(x)", range=[-0.03, 1.05], gridcolor="rgba(128,128,128,0.25)"
)
cdf_fig.show(config={"displayModeBar": False, "staticPlot": True})
The table lists the distributions used in Lectures 12 and 13, with their expectation and variance (defined in the next section).
distribution
type
expectation
variance
typical use
Bernoulli(p): 1 with probability p, else 0
discrete
p
p(1−p)
a yes/no outcome
binomial(n, p): number of 1s in n independent Bernoulli(p) trials
discrete
np
np(1−p)
number of successes
Poisson(λ)
discrete
λ
λ
number of arrivals in a period
uniform[a, b]
continuous
(a+b)/2
(b−a)2/12
a value somewhere between a and b
exponential with rate λ: F(x)=1−e−λx, x≥0
continuous
1/λ
1/λ2
time between arrivals
normal N(μ,σ2)
continuous
μ
σ2
sums and averages (see the CLT below)
lognormal(μ, σ): eY with Y∼N(μ,σ2)
continuous
eμ+σ2/2
(eσ2−1)e2μ+σ2
durations: positive and skewed to the right
Watch the parameters, because they are not the same everywhere. N(3,4) has variance 4 and standard deviation 2. An exponential distribution is described by its rate λ or by its mean 1/λ. The parameters μ and σ of a lognormal distribution are those of the underlying normal distribution, not its own mean and standard deviation: lognormal(1, 1) has expectation e1.5≈4.48.
For a die, EX=61(1+2+⋯+6)=3.5. For a function r of X, the expectation E[r(X)] weights the values r(x) in the same way, E[r(X)]=∑xr(x)P(X=x) (or ∫r(x)f(x)dx).
The varianceσ2(X)=E[(X−EX)2] measures the spread of X around EX: the expected squared distance to the expectation. For a die, σ2(X)=61((1−3.5)2+⋯+(6−3.5)2)=35/12≈2.92. The standard deviation (SD) σ(X)=σ2(X) has the same unit as X, which makes it easier to interpret; the variance has easier rules, which follow now.
Now let X1,…,Xn be independent with the same distribution as X, for example the outputs of n simulation runs, and let Xˉ=(X1+⋯+Xn)/n be their average. The rules above give
The average Xˉ is centered on EX, and its variance σ2(X)/n goes to 0 as n grows. So for large n the average is close to the expectation, with high probability. This is the law of large numbers (LLN).
The SD of the average decreases with n, which grows slowly: to make the SD of Xˉ twice as small, we need four times as many observations.
whatever the distribution of X itself (as long as its variance is finite). A rule of thumb is that n≥30 is large enough. Figure 2 shows the CLT at work for averages of uniform[0, 1] samples: the average of a single sample is uniform, but the average of 16 samples already has the bell shape of a normal density.
Figure 2:Histograms of 1000 averages of 1, 2, 4 and 16 uniform[0, 1] samples.
For a normal distribution, about 68% of the probability lies within one SD of the expectation and about 95% within two SDs (more precisely, 1.96). So with probability about 95%, the average Xˉ lies within 2σ(X)/n of EX. Do not apply this rule to the data itself, whose distribution need not be normal; it is the average that is approximately normal.
The CLT tells us how far the average Xˉ of n observations is likely to be from the unknown EX. The SD σ(X) is usually unknown too, so we estimate it with the sample standard deviation
The 2 is a rounded 1.96. Strictly speaking, using S instead of σ(X) calls for Student’s t-distribution with n−1 degrees of freedom, but for n of 30 or more the difference is negligible. The width of the CI shrinks with n: to halve it, we need four times as many observations.
For example, 100 measurements with average 178 and sample SD 5 give the 95% CI [178−2×5/100,178+2×5/100]=[177,179].
A hypothesis test checks whether the data give enough evidence against a null hypothesisH0, for example that two expectations are equal. We compute the p-value: the probability, if H0 were true, of an outcome at least as extreme as the one observed. If the p-value is below the significance level, usually 5%, we reject H0. If it is not, we cannot conclude anything: not rejecting H0 is not evidence that H0 is true.
A test is two-sided if deviations in both directions count against H0 (for example H1:EX=EY) and one-sided if only one direction counts (H1:EX>EY). For a test statistic T that is approximately N(0,1) under H0, a two-sided test at 5% rejects when ∣T∣>1.96 and a one-sided test rejects when T>1.64.
To compare two expectations EX and EY, for example the expected profits of two decisions, we use n observations of each. There are two settings.
Independent samples. The observations of X and Y are independent. With averages Xˉ, Yˉ and sample SDs SX, SY, the difference Xˉ−Yˉ has SD σ2(X)/n+σ2(Y)/n, and
is approximately N(0,1) if EX=EY. Equivalently: we reject EX=EY (two-sided, at 5%) exactly when the 95% CI Xˉ−Yˉ±2SX2/n+SY2/n for EX−EY does not contain 0.
Matched pairs. The observations come in pairs (Xi,Yi) that belong together, such as the incomes of two partners in one household. Then we work with the differences Di=Xi−Yi and make a CI for ED=EX−EY from them, exactly as in the previous section. If Xi and Yi are positively correlated, the differences vary less than in the independent setting, which gives a narrower CI.