Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Lecture 12: Variability (Recap)

Open In Colab

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

Random Variables

A random variable (RV) XX 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,…0, 1, 2, \dots). It is described by its probability mass function (pmf) P(X=x)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/6P(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)f(x) gives probabilities of intervals, P(a≤X≤b)=∫abf(x) dxP(a \le X \le b) = \int_a^b f(x)\,dx. Where the density is high, outcomes are more likely.

Both types have a cumulative distribution function (cdf) F(x)=P(X≤x)F(x) = P(X \le x). It is non-decreasing, goes from 0 to 1, and is a step function for a discrete RV. For XX uniform on [a,b][a, b], every subinterval of the same length is equally likely and

F(x)=x−ab−afor a≤x≤b.F(x) = \frac{x - a}{b - a} \quad \text{for } a \le x \le b.

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.

Common Distributions

The table lists the distributions used in Lectures 12 and 13, with their expectation and variance (defined in the next section).

distributiontypeexpectationvariancetypical use
Bernoulli(pp): 1 with probability pp, else 0discreteppp(1−p)p(1-p)a yes/no outcome
binomial(nn, pp): number of 1s in nn independent Bernoulli(pp) trialsdiscretenpnpnp(1−p)np(1-p)number of successes
Poisson(λ\lambda)discreteλ\lambdaλ\lambdanumber of arrivals in a period
uniform[aa, bb]continuous(a+b)/2(a+b)/2(b−a)2/12(b-a)^2/12a value somewhere between aa and bb
exponential with rate λ\lambda: F(x)=1−e−λxF(x) = 1 - e^{-\lambda x}, x≥0x \ge 0continuous1/λ1/\lambda1/λ21/\lambda^2time between arrivals
normal N(μ,σ2)N(\mu, \sigma^2)continuousμ\muσ2\sigma^2sums and averages (see the CLT below)
lognormal(μ\mu, σ\sigma): eYe^Y with Y∼N(μ,σ2)Y \sim N(\mu, \sigma^2)continuouseμ+σ2/2e^{\mu + \sigma^2/2}(eσ2−1)e2μ+σ2(e^{\sigma^2} - 1)e^{2\mu + \sigma^2}durations: positive and skewed to the right

Watch the parameters, because they are not the same everywhere. N(3,4)N(3, 4) has variance 4 and standard deviation 2. An exponential distribution is described by its rate λ\lambda or by its mean 1/λ1/\lambda. The parameters μ\mu and σ\sigma 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.48e^{1.5} \approx 4.48.

Expectation and Variance

The expectation EXEX is the average outcome, weighted with the probabilities:

EX=∑xx P(X=x)(discrete),EX=∫xf(x) dx(continuous).EX = \sum_x x \, P(X = x) \quad \text{(discrete)}, \qquad EX = \int x f(x)\,dx \quad \text{(continuous)}.

For a die, EX=16(1+2+⋯+6)=3.5EX = \frac16 (1 + 2 + \dots + 6) = 3.5. For a function rr of XX, the expectation E[r(X)]E[r(X)] weights the values r(x)r(x) in the same way, E[r(X)]=∑xr(x)P(X=x)E[r(X)] = \sum_x r(x) P(X = x) (or ∫r(x)f(x) dx\int r(x) f(x)\,dx).

The variance σ2(X)=E[(X−EX)2]\sigma^2(X) = E[(X - EX)^2] measures the spread of XX around EXEX: the expected squared distance to the expectation. For a die, σ2(X)=16((1−3.5)2+⋯+(6−3.5)2)=35/12≈2.92\sigma^2(X) = \frac16 \big((1-3.5)^2 + \dots + (6-3.5)^2\big) = 35/12 \approx 2.92. The standard deviation (SD) σ(X)=σ2(X)\sigma(X) = \sqrt{\sigma^2(X)} has the same unit as XX, which makes it easier to interpret; the variance has easier rules, which follow now.

Sums and Averages

For RVs XX and YY and a constant aa:

E(X+Y)=EX+EY,E(aX)=aEX,E(X + Y) = EX + EY, \qquad E(aX) = aEX,

whether or not XX and YY are independent, and

σ2(X+Y)=σ2(X)+σ2(Y) if X and Y are independent,σ2(aX)=a2σ2(X).\sigma^2(X + Y) = \sigma^2(X) + \sigma^2(Y) \text{ if } X \text{ and } Y \text{ are independent}, \qquad \sigma^2(aX) = a^2 \sigma^2(X).

Note that the variances add up, not the SDs.

Now let X1,…,XnX_1, \dots, X_n be independent with the same distribution as XX, for example the outputs of nn simulation runs, and let Xˉ=(X1+⋯+Xn)/n\bar X = (X_1 + \dots + X_n)/n be their average. The rules above give

EXˉ=EX1+⋯+EXnn=EX,σ2(Xˉ)=σ2(X1)+⋯+σ2(Xn)n2=σ2(X)n,E \bar X = \frac{EX_1 + \dots + EX_n}{n} = EX, \qquad \sigma^2(\bar X) = \frac{\sigma^2(X_1) + \dots + \sigma^2(X_n)}{n^2} = \frac{\sigma^2(X)}{n},

so σ(Xˉ)=σ(X)/n\sigma(\bar X) = \sigma(X)/\sqrt n.

Law of Large Numbers

The average Xˉ\bar X is centered on EXEX, and its variance σ2(X)/n\sigma^2(X)/n goes to 0 as nn grows. So for large nn 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\sqrt n, which grows slowly: to make the SD of Xˉ\bar X twice as small, we need four times as many observations.

Central Limit Theorem

The central limit theorem (CLT) adds to the LLN what the distribution of the average looks like: for large nn, Xˉ\bar X is approximately normal,

Xˉ≈N(EX,σ2(X)n),\bar X \approx N\left(EX, \frac{\sigma^2(X)}{n}\right),

whatever the distribution of XX itself (as long as its variance is finite). A rule of thumb is that n≥30n \ge 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.

Loading...

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ˉ\bar X lies within 2σ(X)/n2\sigma(X)/\sqrt n of EXEX. Do not apply this rule to the data itself, whose distribution need not be normal; it is the average that is approximately normal.

Confidence Intervals

The CLT tells us how far the average Xˉ\bar X of nn observations is likely to be from the unknown EXEX. The SD σ(X)\sigma(X) is usually unknown too, so we estimate it with the sample standard deviation

S=∑i=1n(Xi−Xˉ)2n−1.S = \sqrt{\frac{\sum_{i=1}^n (X_i - \bar X)^2}{n - 1}}.

Dividing by n−1n - 1 instead of nn makes S2S^2 an unbiased estimator of σ2(X)\sigma^2(X). A 95% confidence interval (CI) for EXEX is then

[Xˉ−2Sn, Xˉ+2Sn].\left[\bar X - \frac{2S}{\sqrt n}, \ \bar X + \frac{2S}{\sqrt n}\right].

The 2 is a rounded 1.96. Strictly speaking, using SS instead of σ(X)\sigma(X) calls for Student’s tt-distribution with n−1n - 1 degrees of freedom, but for nn of 30 or more the difference is negligible. The width of the CI shrinks with n\sqrt 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][178 - 2 \times 5/\sqrt{100}, \ 178 + 2 \times 5/\sqrt{100}] = [177, 179].

Hypothesis Tests

A hypothesis test checks whether the data give enough evidence against a null hypothesis H0H_0, for example that two expectations are equal. We compute the p-value: the probability, if H0H_0 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 H0H_0. If it is not, we cannot conclude anything: not rejecting H0H_0 is not evidence that H0H_0 is true.

A test is two-sided if deviations in both directions count against H0H_0 (for example H1:EX≠EYH_1: EX \ne EY) and one-sided if only one direction counts (H1:EX>EYH_1: EX > EY). For a test statistic TT that is approximately N(0,1)N(0, 1) under H0H_0, a two-sided test at 5% rejects when ∣T∣>1.96|T| > 1.96 and a one-sided test rejects when T>1.64T > 1.64.

To compare two expectations EXEX and EYEY, for example the expected profits of two decisions, we use nn observations of each. There are two settings.

Independent samples. The observations of XX and YY are independent. With averages Xˉ\bar X, Yˉ\bar Y and sample SDs SXS_X, SYS_Y, the difference Xˉ−Yˉ\bar X - \bar Y has SD σ2(X)/n+σ2(Y)/n\sqrt{\sigma^2(X)/n + \sigma^2(Y)/n}, and

T=Xˉ−YˉSX2/n+SY2/nT = \frac{\bar X - \bar Y}{\sqrt{S_X^2/n + S_Y^2/n}}

is approximately N(0,1)N(0, 1) if EX=EYEX = EY. Equivalently: we reject EX=EYEX = EY (two-sided, at 5%) exactly when the 95% CI Xˉ−Yˉ±2SX2/n+SY2/n\bar X - \bar Y \pm 2\sqrt{S_X^2/n + S_Y^2/n} for EX−EYEX - EY does not contain 0.

Matched pairs. The observations come in pairs (Xi,Yi)(X_i, Y_i) that belong together, such as the incomes of two partners in one household. Then we work with the differences Di=Xi−YiD_i = X_i - Y_i and make a CI for ED=EX−EYED = EX - EY from them, exactly as in the previous section. If XiX_i and YiY_i are positively correlated, the differences vary less than in the independent setting, which gives a narrower CI.

Exercises

These exercises can be done on paper, with a calculator.

References

  • Koole, G. (2019). An Introduction to Business Analytics. Chapter 3, “Variability” (recap).

  • Ross, S.M. (2002). A First Course in Probability, 6th ed. Prentice Hall.

  • Triola, M.F. (2017). Elementary Statistics, 13th ed. Pearson.