How to choose the input distributions a simulation feeds on: assess sample independence, hypothesize a distribution family, estimate its parameters by maximum likelihood, and judge the fit with chi-square and K-S tests.
flowchart LR
RNG[RNG] -->|"U(0,1)"| T[transformations]
F(["f / F (input distribution)"]) --> T
T -->|RVs| P((simulation program))
Chapter 3 built the RNG and Chapter 4 the transformations. This chapter is about the remaining box: which input distribution f / F to use.
E.g. for the M/M/1 queue we simply assumed exponential interarrival and service times. Q:
How to select input distributions?
Is a specified input distribution a good approximation of the actual process?
flowchart LR
D[Data] --> I[input distributions]
In practice, an input distribution is specified based on data collected from the actual process by observing an input r.v. of interest.
Three approaches
Use the actual data directly (e.g. customer service times): store the data in a “lookup” table and read the values in order during the simulation (trace-driven simulation).
Disadvantage: this approach is very expensive and time-consuming (a large amount of data must be stored and read).
Disadvantage: it can only reproduce what has happened historically.
Use case: typically used in failure analysis, and to validate a model of an existing system (feed the model the same historical inputs and compare its output with the real system’s).
Use the data to fit an empirical distribution (frequency distribution, histogram), and then sample from the empirical distribution during the simulation.
Disadvantage: it is not possible to generate values outside the range of the observed data X1,…,Xn, i.e. outside [miniXi,maxiXi].
Use the data to fit a theoretical distribution (a parameterized distribution such as Exp(λ), N(μ,σ2), U(a,b)); use statistical methods to determine the quality of fit.
Advantage: compact representation of the data (a few parameters instead of n numbers).
Advantage: flexible → “what-if” questions (e.g. what if service becomes 10% faster? Just change a parameter).
Advantage: it can generate values outside the observed range, and it smooths out the irregularities of a small sample.
Use the third approach whenever possible, i.e. whenever some theoretical distribution fits the data well.
Assumption: to fit a distribution, the data should be i.i.d. observations, i.e. X1,X2,…,Xn are i.i.d. from an underlying distribution. (§5.1 checks the independence part.)
5.1. Techniques for assessing sample independence
Given X1,X2,…,Xn, in the order they were observed.
Correlation plot
The correlation between two observations j apart (lag j) is
and plot ρ^j against j=1,2,…: this is the correlation plot. Even for independent data ρ^j is never exactly 0, but it should stay close to 0 (a common rule of thumb: within ±2/n). Values clearly away from 0 indicate dependence. Since a correlation of 0 does not prove independence (§2.3), the plot can only detect dependence, not prove independence.
Scatter diagram
Group the observations into pairs (Xi,Xi+1), i=1,2,…,n−1, and plot them as points.
If Xi and Xi+1 are positively correlated, then the points will approximately lie along a line with a positive slope (negatively correlated: negative slope). If they are independent, the points are scattered with no pattern.
Runs test
The runs-up test of Chapter 3 (§3.4.3). It uses only the order of the observations (which one is larger than which), so it works for any continuous data, not just U(0,1) numbers.
Three steps to fit a theoretical distribution
Hypothesize a family of distributions (§5.4).
Estimate the parameters of the distribution (maximum likelihood estimator, MLE).
Determine the quality of fit (validation).
5.2. Review of distributions and their parameters
The types of parameters:
Location parameterγ
It determines the location of the distribution (where it sits on the x-axis). Changing γ shifts the distribution but does not change its shape.
If X has location parameter 0, then Y=X+γ has location parameter γ. E.g. for N(μ,σ2), μ is the location parameter: if X∼N(0,σ2), then X+μ∼N(μ,σ2).
Scale parameterβ
It determines the scale / statistical dispersion of the distribution. Changing β expands / compresses the distribution, but the distribution maintains its basic shape. If X has scale parameter 1, then Y=βX has scale parameter β.
E.g.
N(μ,σ2)
Exp(λ)
U(a,b)
σ is the scale parameter: if X∼N(0,1), then σX∼N(0,σ2). (μ is the location parameter.)
Var(X)=1/λ2. The scale parameter is β=1/λ, the mean: if X∼Exp(1), then βX is exponential with mean β. λ itself is a rate, so a large λ compresses the distribution.
f(x)=b−a1. Let L=b−a and reparameterize U(a,b) by (a,L): a is the location and L the scale parameter, since a+LU∼U(a,a+L) for U∼U(0,1). The density height is 1/L.
Shape parameterα
It determines the basic form or shape of the distribution, not just its position and spread.
Ex. Weibull distribution (α: shape, β: scale):
f(x)=αβ−αxα−1e−(x/β)α,x>0.
α<1: decreasing density, with f(x)→∞ as x→0. α=1: the exponential distribution with mean β. α>1: a single hump, with f(0)=0. (The normal and exponential families have no shape parameter: every normal density has the same bell shape.)
5.3. Fit an empirical distribution
Used when there is enough data and theoretical distributions do not work well.
Given n observations X1,…,Xn, sort them: X(1)<X(2)<⋯<X(n) (order statistics). F(X(i))=i/n.
Empirical c.d.f.:
F(x)=nnumber of Xi’s≤x.
For continuous data, use the interpolated (piecewise linear) version:
F~(X(i))=n−1i−1,F(X(i))=nnumber of Xk’s≤X(i)=ni.
Drawbacks
The mean of F~=Xˉ. F~ spreads probability n−11 uniformly over each interval [X(i),X(i+1)], so its mean is n−11∑i=1n−12X(i)+X(i+1), which in general differs from Xˉ. E.g. for the data 1.0,2.2,2.8,4.5,5.6: mean of F~=3.20, Xˉ=3.22.
Random values generated from F~ can never be smaller than X(1) or larger than X(n).
Sampling from F~ (inverse transform, §4.2.1): generate u∼U(0,1), let P=(n−1)u and I=⌊P⌋+1, and return X=X(I)+(P−I+1)(X(I+1)−X(I)).
5.4. Step I: Hypothesize a distribution family
Prior knowledge of the random variable of interest
Service times should never be fitted using a normal distribution: a normal r.v. can be negative, a service time cannot (and service times are usually right-skewed, while the normal is symmetric).
Customers arrive at a bank one at a time (independently, at a roughly constant rate) ⇒ the arrival process is a Poisson process, so the interarrival times are exponential (§4.6).
Other heuristics
Summary statistics: mean, variance, min, max, mode. Also the median (mean ≈ median suggests a symmetric distribution) and the coefficient of variation σ/μ (equal to 1 for the exponential).
Histogram (choose an appropriate number of subintervals: too few hides the shape, too many makes it ragged; a common starting point is k≈1+log2n, then try a few others). The shape of the histogram estimates the shape of the density.
Quantile summaries and boxplot.
Given q∈(0,1), the q-quantile is the value xq such that P(X≤xq)≥q and P(X≥xq)≥1−q. (For continuous F: F(xq)=q, i.e. xq=F−1(q). x0.5 is the median; x0.25 and x0.75 are the quartiles.)
For a given sample X1,X2,…,Xn with X(1)≤X(2)≤⋯≤X(n), the sample 100q% quantile is
x^q=⎩⎨⎧X(⌈nq⌉),2X(nq)+X(nq+1),if nq is not an integer,if nq is an integer.
Ex. n=10 service times, sorted: 0.2,0.5,0.7,1.1,1.4,1.8,2.3,3.0,4.2,6.5.
q=0.25: nq=2.5 is not an integer ⇒ x^0.25=X(3)=0.7.
q=0.5: nq=5 is an integer ⇒ x^0.5=2X(5)+X(6)=21.4+1.8=1.6.
q=0.75: nq=7.5 ⇒ x^0.75=X(8)=3.0.
Boxplot
Draws the five numbers xmin,x^0.25,x^0.5,x^0.75,xmax: a box from x^0.25 to x^0.75 with a line at the median, and whiskers out to the min and the max.
If the distribution is symmetric, the median sits in the middle of the box and the two whiskers have about the same length. Here the right whisker is much longer and the mean (2.17) exceeds the median (1.6): the sample is right-skewed, which, together with the values being nonnegative, already rules out the normal. (Some software stops the whiskers at 1.5 box-lengths and plots the points beyond them individually, as outliers.)
5.5. Step II: Estimate the Parameters
Maximum Likelihood Estimator (MLE)
Estimator: a statistic, i.e. a function of the random observations, whose value serves to estimate some unknown parameter of interest. If the parameter is θ, then the estimator is denoted by θ^.
Ex. estimate μ (and σ2) from X1,X2,…,Xn:
μ^=nX1+⋯+Xn≈μ,S2=n−1∑i=1n(Xi−μ^)2≈σ2.
The estimator is a function (of random variables, so it is itself a random variable). The estimate is the value of the function: once the observed values x1,…,xn are plugged in, it becomes a number.
Construct the likelihood function L(θ), which represents the probability (likelihood) that the sequence of observations x1,x2,…,xn came from the distribution with parameter θ. The data are fixed and L is a function of θ. Since the Xi’s are i.i.d. (§5.1), the joint probability is a product:
Let l(θ)=lnL(θ) → the log-likelihood function. Taking ln does not move the maximum (ln is increasing), and it turns the product into a sum, which is much easier to differentiate. In smooth cases, solve dθdl(θ)=0 and check dθ2d2l(θ)<0.
Ex: Exp(θ)
f(x)=θ1e−x/θ,x≥0,E[X]=θ
(the exponential parameterized by its mean: θ=β=1/λ of §5.2). Given x1,x2,…,xn:
L(θ)=i=1∏nθ1e−xi/θ=θn1exp(−θ1i=1∑nxi),
l(θ)=lnL(θ)=−nlnθ−θ1i=1∑nxi,
dθdl(θ)=−θn+θ21i=1∑nxi=0⇒θ^=n1i=1∑nxi=xˉ.
(Check: dθ2d2l=θ2n−θ32∑xi, which at θ=xˉ equals −xˉ2n<0: a maximum.)
The meaning: θ is the mean of the distribution, and its MLE is the sample mean, the most natural estimate.
Ex: Geo(θ) (modified)
p(x)=(1−θ)xθ,x=0,1,2,…
x = number of trials before the first success, i.e. the number of failures (each trial is a success w.p. θ). The geometric of §4.4 counts the trials up to and including the first success, P(X=k)=(1−θ)k−1θ, k=1,2,…; here the exponent is x, not x−1, because the modified X = (usual X) −1. E[X]=θ1−θ.
(dθ2d2l=−θ2n−(1−θ)2∑xi<0, so it is a maximum.)
The meaning: on average there are xˉ failures before each success, i.e. one success in every 1+xˉ trials, so the estimated success probability is 1/(1+xˉ). Equivalently, E[X]=θ1−θ⟺θ=1+E[X]1, and the MLE simply replaces E[X] by xˉ.
Ex: uniform distribution U(a,b)
Given x1,x2,…,xn; the parameter is θ=(a,b):
L(a,b)=i=1∏nb−a1=(b−a)n1if a≤xi≤b for all i,
and L(a,b)=0 otherwise (if some xi lies outside [a,b], its density is 0). “All xi∈[a,b]” means a≤x(1) and b≥x(n).
Here setting the derivative to 0 does not work: 1/(b−a)n has no stationary point, it just keeps increasing as b−a shrinks. So make b−a as small as the constraint allows, i.e. push a up to x(1) and b down to x(n):
a^=x(1)=iminxi,b^=x(n)=imaxxi.
Properties of MLEs
Unique for many distribution families.
Asymptotically unbiased: limn→∞E[θ^]=θ. For finite n an MLE may be biased. E.g. for U(a,b), b^=X(n)≤b always, and E[X(n)]=a+(b−a)n+1n<b, but it tends to b as n→∞.
Invariant: if instead of estimating θ we want to estimate ϕ=h(θ), then the MLE is ϕ^=h(θ^). E.g. the rate of the exponential is λ=1/θ, so λ^=1/xˉ, without redoing the maximization.
Strongly consistent: limn→∞θ^=θ w.p. 1 (as in the SLLN, §2.4).
What it says. For large n, θ^ is approximately normal, centred at the true θ, with variance δ(θ)/n: θ^≈N(θ,δ(θ)/n). Standardizing (subtract the mean, divide by the standard deviation) gives the Z above.
Same steps as CI Case 1 (§2.6.1), with Xˉ(n) replaced by θ^ and σ2/n by δ(θ)/n. Rearranging −zα/2≤δ(θ)/nθ^−θ≤zα/2 gives θ^−zα/2δ(θ)/n≤θ≤θ^+zα/2δ(θ)/n.
Why δ(θ^) in the CI.δ(θ) depends on the unknown θ, so we plug in the estimate θ^. For large n this is fine because θ^→θ (property 4).
Where δ comes from.δ(θ)/n=−1/E[l′′(θ)], and l′′(θ) is the curvature of the log-likelihood. A sharply peaked l (large ∣l′′∣) means the data strongly favour values near θ^: small variance, short CI. A flat l means many values of θ explain the data almost equally well: large variance, wide CI. Since l is a sum of n terms, E[l′′] is proportional to n, so δ(θ) does not depend on n, and the CI shrinks like 1/n.
Check with the exponential. From the example above, l′′(θ)=θ2n−θ32∑xi, and E[Xi]=θ, so E[l′′(θ)]=θ2n−θ32nθ=−θ2n and δ(θ)=θ2=Var(X). Since θ^=Xˉ, property 5 is just the CLT for Xˉ (§2.4), and the CI is xˉ±zα/2xˉ/n: Case 1 of §2.6.1 with σ replaced by its MLE θ^=xˉ.
The log-likelihood of the exponential example with xˉ=2, for n=10 and n=50, and the 90% CIs xˉ±1.645xˉ/n: the sharper the peak, the shorter the interval.
Ex: construct a 90% CI for θ in the modified geometric distribution Geo(θ).
Step I: hypothesize an exponential distribution. Step II: the MLE of its mean is θ^=xˉ=27.46/20=1.373, so F^(x)=1−e−x/1.373. Step III asks how well this F^ fits. (The numbers were simulated from an exponential distribution, so the exponential family is in fact correct here.)
Graphical methods
Q–Q plot
Let F^ be the fitted theoretical distribution and F~ the empirical distribution based on x1,…,xn. A Q–Q plot is a graph of the qi-quantile of F^ vs the qi-quantile of F~.
F(x)=nnumber of xi’s≤x,F~(x)=F(x)−n0.5,qi=ni−0.5,i=1,…,n.
(This F~, the empirical c.d.f. shifted down by 0.5/n, is not the interpolated F~ of §5.3.)
F~(x(1))=n1−n0.5=n1−0.5=q1, and in general F~(x(i))=ni−0.5=qi ⇒ the qi-quantile of F~ is just x(i).
Why 0.5/n? Without this term, qn=n/n=1, and we would need the 1-quantile of F^. For a distribution with infinite range, like the normal or the exponential, F^−1(1)=+∞: not possible. With the 0.5/n, every qi lies strictly inside (0,1), and symmetrically: q1=n0.5, qn=1−n0.5.
Plot xqiM=F^−1(qi) (model quantile) as a function of xqiS=x(i) (sample quantile), i=1,…,n. If F^ fits well, the points lie close to the 45° line y=x.
Left: at height qi, read the sample quantile x(i) off the staircase F~ and the model quantile xM=F^−1(qi) off the curve F^. The Q–Q plot compares these two x-values (a horizontal gap). Right: the Q–Q plot of the running example, with F^−1(q)=−1.373ln(1−q). The points follow the line except at the upper end: the two largest observations, 2.55 and 2.95, are much smaller than the model quantiles 3.56 and 5.06, i.e. the sample has a shorter right tail than the fitted exponential.
Used for continuous distributions (it needs the quantile function F^−1; for a discrete F^ many qi share the same quantile, and the plot shows little).
P–P plot
A P–P plot is a graph of the model probability F^(x(i)) vs the sample probability F~(x(i))=qi=ni−0.5, for i=1,…,n. Used for both continuous and discrete data sets. If F^ fits well, the points lie close to the line y=x in [0,1]2.
Left: at x(i), read the probability on the curve F^ and on the staircase F~. The P–P plot compares these two probabilities (a vertical gap). Right: the P–P plot of the running example. The largest deviations are in the middle, e.g. F^(x(6))=F^(0.90)=0.48 vs q6=0.275 (the sample has no observations between 0.41 and 0.90). At the upper end the points are much closer to the line: the short right tail that stands out in the Q–Q plot is barely visible here.
Q–Q or P–P?
A small change in the tail causes a large gap in the Q–Q plot. In the tails F^ is flat (the density is small), so a tiny difference in probability corresponds to a large difference in x; the Q–Q plot compares x-values, so differences in the tails are magnified. In the middle F^ is steep, so a small difference in x corresponds to a large difference in probability; the P–P plot compares probabilities, so differences in the middle are magnified. (In the tails both F^ and F~ are close to 0 or 1, so the P–P points crowd into the corners and tail differences are hidden.)
If you want to know whether F^ provides a good approximation of F~ in the tails → Q–Q plot.
If you want to check the middle (the bulk) of the distribution → P–P plot. It is also the one of the two that works for discrete data.
The running example shows both effects on the same data: the Q–Q plot’s biggest gap is in the right tail, the P–P plot’s in the middle.
Goodness-of-fit tests (hypothesis tests)
They test the null hypothesis
H0:the Xi’s are i.i.d. with distribution F^.
As with the RNG tests (§3.4), “do not reject H0” does not prove that F^ is correct, only that the data show no evidence against it. The sample size matters: with little data almost nothing is rejected, and with a lot of data almost every F^ is rejected, since no theoretical distribution fits real data exactly.
Chi-square test
Split the range of F^ (not the range of x1,…,xn) into k adjacent intervals [a0,a1),[a1,a2),…,[ak−1,ak). It is possible that a0=−∞ and/or ak=+∞. (Every value that F^ can produce must fall in some interval, so that the pj in step 3 add up to 1.)
Count the number of xi’s in each interval: fj = number of xi’s in the j-th interval, ∑j=1kfj=n.
Assume H0 is true; compute the expected proportion pj of xi’s that would fall in the j-th interval:
where in the discrete case the sum runs over the possible values x of the fitted distribution (not over the data). The expected number in interval j is npj.
4. Test statistic
χ2=j=1∑knpj(fj−npj)2.
Reject H0 at significance level α if χ2>χk−1,α2 (see the χ2 table).
Remarks.
It is the same statistic as the RNG uniformity test of §3.4.1, which is the special case F^=U(0,1) with k equal intervals, pj=1/k.
Choosing the intervals: a common choice is equiprobable intervals, pj=1/k, i.e. aj=F^−1(j/k), with npj≥5 (as in §3.4.1).
Degrees of freedom. If m parameters of F^ were estimated from the same data (e.g. by MLE), the distribution of χ2 under H0 lies between χk−m−12 and χk−12. Using χk−1,α2, as in step 4, is conservative: the actual probability of a type I error is at most α. So if χ2≤χk−m−1,α2, do not reject; if χ2>χk−1,α2, reject; in between, the decision depends on which critical value is used.
Example (running example, F^ = exponential with θ^=1.373). Take k=4 equiprobable intervals, so npj=20/4=5, with boundaries aj=F^−1(j/4)=−1.373ln(1−j/4):
j
interval
fj
npj
(fj−npj)2/npj
1
[0,0.395)
4
5
0.2
2
[0.395,0.952)
2
5
1.8
3
[0.952,1.903)
7
5
0.8
4
[1.903,∞)
7
5
0.8
total
20
20
χ2=3.6
m=1 parameter was estimated, so the critical value at α=0.05 is between χ2,0.052=5.991 and χ3,0.052=7.815. Since χ2=3.6<5.991, do not reject H0 either way: no evidence against the exponential fit.
Note what the test cannot see: interval 4 only counts how many observations exceed 1.903 (7, close to the expected 5), not how far out they go, so the short right tail visible in the Q–Q plot does not affect χ2. With n=20 that short tail is a chance feature of the sample anyway, since the data do come from an exponential.