R Programming / Analysis interfaces and table integrity / Lesson 10
Probability, randomness, and simulation
Distinguish exact enumeration, distribution calculations, and Monte Carlo evidence.
On this page
Lesson goals
- Define a probability model before sampling.
- Use seed and generator configuration deliberately.
- Interpret simulation error and dependence correctly.
Randomness needs an explicit model
A simulation samples from a declared mathematical distribution; it does not learn whether that distribution describes a real process. Specify outcomes, probabilities, dependence, and the quantity to estimate. Independent fair binary trials imply four equally likely pairs, but changing fairness or dependence changes those probabilities. The first fixture enumerates every pair and tabulates total successes, giving an exact distribution for its stated model. Exact enumeration avoids sampling error here because the state space is tiny. It should not be described as empirical evidence about coins, machines, people, or any external population.
Distribution functions have distinct prefixes
R distribution families commonly provide d for density or probability mass, p for cumulative probabilities, q for quantiles, and r for random generation. A density is not a point probability for a continuous variable. Specify tail direction, log-probability options, and parameter meanings when using these interfaces. A normal standard deviation is not its variance; a binomial size counts trials rather than the number of simulated rows. Check small known cases before trusting a complex calculation. The enumerated binary fixture provides one independent reference for the corresponding two-trial binomial probabilities.
outcomes <- expand.grid(first = 0:1, second = 0:1)
counts <- table(factor(rowSums(outcomes), levels = 0:2))
probabilities <- as.numeric(counts)/nrow(outcomes)
cat("counts=", paste(as.integer(counts), collapse = ","), "\n", sep = "")
cat("probabilities=", paste(probabilities, collapse = ","), "\n", sep = "")
cat("expected successes=", sum((0:2)*probabilities), "\n", sep = "")
counts=1,2,1
probabilities=0.25,0.5,0.25
expected successes=1
Enumeration is exact only because the four declared binary pairs are assigned equal probability under the independent fair-trial fixture.
A seed identifies a random-state starting point
The second fixture resets a seed before two identical sampling calls and compares the results within one session. It does not print a claimed universal sequence of sampled faces. Reproducibility also depends on generator kind, sampling algorithm, language version, and call order. Save the relevant configuration with an experiment and use documented generator controls instead of manually editing random-state integers. Resetting the same seed inside every loop iteration can unintentionally repeat the same draw. Decide whether common random numbers are a deliberate comparison technique or an accidental loss of independent replications.
set.seed(17)
first <- sample.int(6L, size = 5L, replace = TRUE)
set.seed(17)
second <- sample.int(6L, size = 5L, replace = TRUE)
cat("same within session=", identical(first, second), "\n", sep = "")
cat("draw count=", length(first), "\n", sep = "")
cat("valid faces=", all(first >= 1L & first <= 6L), "\n", sep = "")
same within session=TRUE
draw count=5
valid faces=TRUE
Resetting the seed repeats this draw under the same generator configuration; exact sampled values and cross-version equality are deliberately not asserted.
Monte Carlo estimates have simulation error
An average over simulated replications approximates an expectation under the supplied model. It varies because only finitely many draws were taken, even when the model parameters are fixed. Under appropriate independence and finite-variance assumptions, the standard error of a sample mean decreases roughly with the square root of the replication count. This is not a universal guarantee for dependent chains, heavy tails, or rare-event estimates. Track valid replication counts and any rejected outcomes. Distinguish simulation error from uncertainty about the model itself, which more replications of an incorrect model cannot remove.
Make simulation comparisons auditable
Write a function for one replication with explicit inputs and returned summaries, then use a controlled traversal. Preserve the target quantity and failure policy rather than selecting only favorable draws. Compare an estimate with an exact tiny case or an analytically known invariant where possible. For interval or test simulations, define the repeated-data experiment before counting coverage or rejection frequency. This course uses tiny synthetic fixtures and does not run R, parallel random streams, or external data. Expected outputs are based on enumeration and within-session generator invariants; no sampled sequence or Monte Carlo accuracy was measured during review.
Common errors and fixes
Every replication returns the same random sample
- Why it happens
- The seed was reset identically inside each replication.
- How to fix it
- Seed once at the intended experiment boundary or define a deliberate stream policy.
- Prevent it next time
- Test whether independent replications actually advance state.
A continuous density is reported as a point probability
- Why it happens
- The d and p interfaces were interpreted interchangeably.
- How to fix it
- Use intervals and the appropriate cumulative calculation.
- Prevent it next time
- State whether the variable is discrete or continuous.
More simulations are claimed to validate a real-world model
- Why it happens
- Monte Carlo error was confused with model validity.
- How to fix it
- Assess assumptions and external evidence separately.
- Prevent it next time
- Label every simulation result by its supplied probability model.
Check your understanding
1. Under two independent fair binary trials, what is the probability of exactly one success?
Show worked solution
The pairs (0,1) and (1,0) are two of four equally likely outcomes, so the probability is one half. If the trials are dependent or have unequal success probabilities, this counting argument must use the changed joint probabilities rather than retain one half automatically.
2. Why does quadrupling independent replications often halve the standard error of a mean?
Show worked solution
Under the stated finite-variance independent model, the standard error is proportional to 1/sqrt(n). Replacing n by 4n multiplies it by one half. This concerns simulation variability of that mean and does not establish correctness of the assumed distribution or independence.
Key takeaways
- Define the model before drawing random values.
- Seed, generator, and call order define repeatability.
- Simulation precision and model validity are different questions.