| Function | Distribution |
|---|---|
| [d,p,q,r]norm | Normal |
| [d,p,q,r]unif | Uniform |
| [d,p,q,r]exp | Exponential |
| [d,p,q,r]gamma | Gamma |
| [d,p,q,r]beta | Beta |
| [d,p,q,r]t | Student t |
| [d,p,q,r]chisq | Chi-squared |
Exploring What-If Scenarios
How might our plans, methods, model \(\ldots\) fail?
Simulation lets us explore scenarios we can’t observe directly.
Generate data from known processes → see how methods behave.
Simulation extends EDA beyond the data we have.
“What would happen if \(\dots\)?”
Simulation lets us explore failure modes before they occur in practice.
For each distribution, R provides four functions:
| Prefix | Function | Example |
|---|---|---|
d |
density/mass | dnorm(0) → 0.399 |
p |
cumulative probability | pnorm(1.96) → 0.975 |
q |
quantile (inverse CDF) | qnorm(0.975) → 1.96 |
r |
random generation | rnorm(10) → 10 values |
| Function | Distribution |
|---|---|
| [d,p,q,r]norm | Normal |
| [d,p,q,r]unif | Uniform |
| [d,p,q,r]exp | Exponential |
| [d,p,q,r]gamma | Gamma |
| [d,p,q,r]beta | Beta |
| [d,p,q,r]t | Student t |
| [d,p,q,r]chisq | Chi-squared |
See ?Distributions for a complete list.
| Function | Distribution |
|---|---|
| [d,p,q,r]binom | Binomial |
| [d,p,q,r]pois | Poisson |
| [d,p,q,r]geom | Geometric |
| [d,p,q,r]nbinom | Negative Binomial |
The Poisson is the “law of rare events”—the limit of Binomial\((n, p)\) as \(n \to \infty\) and \(p \to 0\) with \(np \to \lambda\).
Key insight: We specify the distribution; R generates samples.
Both estimate the center of a symmetric distribution.
Mean: \(\hat{\mu} = \frac{1}{n}\sum_{i=1}^n X_i\)
Median: \(\hat{m}\) = middle value (or average of two middle values)
Which is better? It depends on the distribution.
For normal data, theory tells us:
\[\frac{\text{Var}(\hat{m})}{\text{Var}(\hat{\mu})} \approx \frac{\pi}{2} \approx 1.57\]
The median has 57% higher variance than the mean.
Can we verify this by simulation?
Figure 1: Sampling distributions of mean and median (n=30, R=10,000)
| Estimator | Variance | Std Error |
|---|---|---|
| Mean | 0.0338 | 0.1837 |
| Median | 0.0512 | 0.2263 |
Variance ratio: 1.517 (theory: \(\pi/2 \approx\) 1.571)
Simulation confirms the theoretical result.
We verified a known result. But simulation shines when:
EDA application: “How would my estimator behave if the data were slightly different?”
Classic example: estimate \(\pi\) by random sampling.
Idea: A quarter circle of radius 1 has area \(\pi/4\).
Figure 2: Monte Carlo estimation of π
To estimate \(E[g(X)]\):
\[\hat{\theta} = \frac{1}{n}\sum_{i=1}^n g(X_i)\]
where \(X_1, \ldots, X_n\) are random samples from the distribution of \(X\).
Law of Large Numbers: \(\hat{\theta} \to E[g(X)]\) as \(n \to \infty\)
Figure 3: Monte Carlo estimate converges to true value
Standard error decreases as \(1/\sqrt{n}\).
What if we want to estimate \(P(Z > 6)\) where \(Z \sim N(0,1)\)?
About 1 in a billion. Naive simulation won’t work.
[1] 0
We’d need billions of samples to see even one event.
Key idea: Sample from a different distribution, then reweight.
Instead of sampling from \(p(x)\), sample from \(q(x)\) where rare events are common.
\[E_p[g(X)] = E_q\left[g(X) \cdot \frac{p(X)}{q(X)}\right]\]
The ratio \(w(x) = p(x)/q(x)\) is the importance weight.
Strategy: Shift the normal distribution to center it near 6.
Let \(q(x) = \phi(x - 6)\) (normal centered at 6).
| Method | Estimate |
|---|---|
| True value | 9.87e-10 |
| Importance sampling | 9.92e-10 |
| Naive (n = 10^6) | 0 |
Importance sampling makes the impossible feasible.
How precise is our estimate?
Classical approach: derive standard error formula.
Bootstrap approach: Resample from the data itself.
Figure 4: Bootstrap distribution of the sample mean
Normal theory 95% CI: [18.9, 37.7] — similar result.
The bootstrap works for almost any statistic:
EDA application: “How much would this pattern change with different data?”
Simulation connects to machine learning:
Cross-validation: Repeatedly resample to estimate generalization error.
Stochastic gradient descent: Random sampling of training batches.
Bayesian ML: MCMC and variational inference sample from posteriors.
After fitting a model:
This is the posterior predictive check—simulation for model criticism.
Simulation explores what-if scenarios before they occur
R provides d/p/q/r functions for common distributions
Monte Carlo methods estimate quantities via random sampling
Importance sampling handles rare events
Bootstrap quantifies uncertainty without formulas
| Concept | Formula |
|---|---|
| Monte Carlo estimate | \(\hat{\theta} = \frac{1}{n}\sum_{i=1}^n g(X_i)\) |
| Importance weight | \(w(x) = p(x) / q(x)\) |
| Variance ratio (mean vs. median) | \(\pi/2 \approx 1.57\) |
Verify the variance ratio for normal data:
rnorm(), compute mean and median.var(medians) / var(means). Compare to \(\pi/2 \approx 1.57\).Repeat Exercise 1, but use rcauchy() instead of rnorm().
Use the bootstrap to estimate a 95% CI for the median of mtcars$mpg:
Estimate \(P(Z > 4)\) using importance sampling. Compare to pnorm(4, lower.tail = FALSE).
When is simulation more trustworthy than mathematical derivation?
A colleague sets set.seed(42) and runs one simulation. Is this reproducible research?
How many bootstrap replications are “enough”?
?Distributions