Probability, randomness, and the discrete distributions that show up everywhere in ecology
A guided walk from "what is a probability" to "which distribution describes my data" — using whales, martens, salmon, deer, and elk as the running examples.
What's in this lab and what to hand in
This page is your in-class practice problem set — worked examples with visible answers. Your graded work is a separate problem set: the morning session's Probability problem set is worth 20 points and the afternoon session's Discrete distributions problem set is worth 20 points, for 40 points across Day 1. Both live on the same page (the graded problem set); write up your answers in the R Markdown template for each session and submit on Canvas.
Need help with R Markdown? See the R Markdown tutorial. Other docs for this day: plain-language summary · practice answer key.
Why we start with probability
Every statistical model you will fit this week — t-tests, GLMs, mixed models, MLE, Bayesian models — is, underneath, a probability statement about how the data were generated. The model says: "if nature behaves like this, with these unknown parameters, then the observations should look like that." Inference is then about working backward from the observations to the parameters.
If you understand probability deeply, every model in the rest of the course feels like a natural extension. If you don't, the rest of the week will feel like memorizing R function names. So today is the most important day of the workshop.
Three habits we want you to start today and use all week:
- Name the random variable. What is varying? Over what sample space?
- Name the data-generating process. What story produced the data?
- Name the distribution that matches that story. Bernoulli? Poisson? Beta-binomial? The story should pick the distribution, not convenience.
Plain-language overview
Probability is the math of "how often would this happen if we ran the world many times." A random variable is a number that comes out of one of those runs (e.g., the count of martens detected on a camera, or whether a deer is infected). A distribution is the full menu of what that number could be and how often each value would show up.
This morning we build the math of probability from first principles. This afternoon we meet the four discrete distributions that cover ~80% of count data in wildlife science: Bernoulli, Binomial, Poisson, Negative Binomial, plus the Multinomial for multi-category data.
You will leave today able to look at a dataset and say, "the story here is X, so the natural distribution is Y." That sentence is most of statistical modeling.
Read the full plain-language summary for Day 1 → — the same ideas worked through slowly, with examples and no math.
Core concepts (the conceptual scaffolding)
1. Probability axioms (Kolmogorov, in plain English)
- Non-negativity: $P(A) \ge 0$ for every event $A$. Probabilities cannot be negative.
- Normalization: $P(\Omega) = 1$. Something in the sample space happens.
- Additivity: If $A$ and $B$ are mutually exclusive, $P(A \cup B) = P(A) + P(B)$.
2. Conditional probability and independence
Events are independent when $P(A \mid B) = P(A)$ — knowing $B$ tells you nothing about $A$. Almost nothing in ecology is fully independent; recognizing that drives the whole rest of the course (mixed models, autocorrelation, beta-binomial...).
3. Bayes' rule (preview of Day 4–5)
Read it as "reverse a conditional." If you know how often the test is positive given disease, you can flip that around to get the probability of disease given a positive test — once you account for how common the disease is to begin with.
4. Random variables, PMF, CDF
A random variable $X$ is a function that assigns a number to each possible outcome. Its probability mass function $p(x) = P(X = x)$ tells you the probability of each specific value (discrete case). Its cumulative distribution function $F(x) = P(X \le x)$ adds up the probabilities up to and including $x$.
5. Expected value and variance
Two algebra rules you will use constantly:
- $E[aX + b] = aE[X] + b$
- $\mathrm{Var}(aX + b) = a^2 \mathrm{Var}(X)$ — note the constant disappears, and $a$ is squared.
6. The discrete distribution decision tree
| Your data look like… | Use… | Parameters |
|---|---|---|
| A single yes/no | Bernoulli | $p$ |
| Number of successes in $n$ independent trials, fixed $p$ | Binomial | $n, p$ |
| Counts of rare independent events in a unit of time/space | Poisson | $\lambda$ |
| Counts that are over-dispersed (variance > mean) | Negative Binomial | $\mu, k$ (or $r, p$) |
| Counts across $k$ mutually exclusive categories, fixed $n$ total | Multinomial | $n, (p_1, \dots, p_k)$ |
Morning lab · Probability puzzles
Work each problem with pencil and paper first. Then check your numerical answer in R. Each problem comes with a hint, a short worked solution you can reveal, and a "why this matters" note linking the puzzle back to ecology.
The boy-or-girl paradox
A family has two children. Assume independent 50/50 sexes and equal birth-order probabilities.
- You are told at least one child is a girl. What is the probability both are girls?
- You are told the older child is a girl. What is the probability both are girls?
- Why are the answers different? Write one or two sentences in plain English.
Reveal worked solution
Sample space: the four equally likely birth orders are {BB, BG, GB, GG}, each with probability 1/4.
(a) "At least one girl" rules out BB. We are left with {BG, GB, GG}, three equally likely outcomes. Only GG is "both girls". So $P(\text{both} \mid \text{at least one girl}) = \tfrac{1}{3}$.
(b) "Older child is a girl" rules out BB and GB. We are left with {BG, GG}, two equally likely outcomes. So $P(\text{both} \mid \text{older is girl}) = \tfrac{1}{2}$.
(c) Knowing which child is a girl (a more specific piece of information) shrinks the sample space more than just knowing one of them is.
Check in R:
# Monte Carlo check
set.seed(1)
fams <- matrix(sample(c("B","G"), 2*1e6, replace=TRUE), ncol=2)
mean(rowSums(fams=="G")==2 & rowSums(fams=="G")>=1) /
mean(rowSums(fams=="G")>=1) # ~ 1/3
mean(fams[,1]=="G" & fams[,2]=="G") /
mean(fams[,1]=="G") # ~ 1/2
Taal's taxi (Bayes for camera traps)
A city has 1 blue taxi and 99 green taxis (100 total). A witness identifies a hit-and-run taxi as blue. In a controlled vision test, the witness correctly identifies blue taxis 99% of the time and green taxis 95% of the time. Given the witness called it blue, what is the probability the taxi really was blue?
Reveal worked solution
Let $B$ = "taxi is blue", $W_B$ = "witness says blue".
- $P(B) = 0.01$, $P(\bar B) = 0.99$
- $P(W_B \mid B) = 0.99$
- $P(W_B \mid \bar B) = 0.05$ (the 5% misclassification of green)
Total probability of "says blue":
Bayes:
So only ~17% — even though the witness is "99% reliable" on blue taxis.
Where the small answer really comes from — the odds form. Don't stop at the plug-in; the same problem read in odds shows exactly why a "99% reliable" witness still loses. Divide Bayes' rule for $B$ by Bayes' rule for $\bar B$ — the shared denominator $P(W_B)$ cancels — and you are left with a rule worth memorizing:
Posterior odds = prior odds × likelihood ratio: the evidence only ever multiplies the odds you started with. Here the prior odds are $1:99$ and the likelihood ratio is $0.99/0.05 = 19.8$, so the posterior odds are $19.8/99 = 0.2$, i.e. $1:5$, which is $P = \tfrac{1}{1+5} = \tfrac{1}{6} \approx 0.167$ — the same answer, now with the mechanism exposed. To reach even money (odds $1:1$) the test would need a likelihood ratio near 99 — it would have to be about five times sharper than it is — just to overcome a base rate that starts you 99-to-1 in the hole. That is the general lesson: against a rare event, the base rate sets the odds you must climb out of, and even a very accurate test barely dents it.
p_B <- 0.01; p_W_B <- 0.99; p_W_notB <- 0.05
(p_W_B * p_B) / (p_W_B * p_B + p_W_notB * (1 - p_B)) # 0.1667
Tallest among neighbors
$n$ people of distinct heights sit at a round table in random order. What is the expected number of people who are shorter than both of their immediate neighbors?
Reveal worked solution
Let $X_i$ be 1 if person $i$ is shorter than both neighbors, else 0. The total count is $X = \sum X_i$. By linearity of expectation, $E[X] = \sum E[X_i] = n \cdot P(\text{person 1 shorter than both neighbors})$.
Consider person $i$ together with their two neighbors. Because everyone is seated in random order and all heights are distinct, each of the $3! = 6$ height orderings of these three people is equally likely. Person $i$ is shorter than both neighbors exactly when $i$ is the smallest of the three. Fixing $i$ as the smallest leaves the two neighbors free to be arranged in $2! = 2$ ways, so 2 of the 6 orderings qualify. Hence $P(\text{person } i \text{ shorter than both neighbors}) = 2/6 = 1/3$.
Therefore $E[X] = n/3$.
n <- 50; reps <- 1e5
sapply(1:reps, function(i){
h <- sample(n) # distinct heights at table positions
sum(h < c(h[n], h[-n]) & h < c(h[-1], h[1]))
}) |> mean() # ~ 16.67 = 50/3
Cystic fibrosis (addition & multiplication rules)
Cystic fibrosis is autosomal recessive. Both parents are heterozygous carriers (Cc). A child is unaffected if at least one C allele is inherited.
- What is the probability that any one child is unaffected? (Use the addition rule on the Punnett square.)
- If they have four children, what is the probability all four are unaffected? (Multiplication rule.)
Reveal worked solution
Punnett: CC : Cc : Cc : cc = 1:1:1:1, each with probability 1/4. Unaffected = CC, Cc, or Cc.
(a) $P(\text{unaffected}) = 1/4 + 1/4 + 1/4 = 3/4$. (Or just $1 - P(cc) = 1 - 1/4 = 3/4$.)
(b) Children are independent: $(3/4)^4 = 81/256 \approx 0.316$.
(3/4)^4 # 0.3164
dbinom(0, size=4, prob=1/4) # same thing
Afternoon lab · Discrete distributions in R
Each of the named distributions below is a story about how data are generated. The afternoon work pairs the story with R code so you start to associate the function name (dpois, dnbinom, dmultinom) with a generative process, not just a formula.
Whale transect counts: Poisson vs. Negative Binomial
A humpback whale survey expects $\lambda = 10$ whales per transect. We will draw 1000 transects under four models: Poisson($\lambda=10$), and Negative Binomial with mean 10 and dispersion parameter (R's size) $k = 5,\, 1,\, 0.5$. Recall that for NB, $\mathrm{Var}(Y) = \mu + \mu^2/k$, so smaller $k$ = more variance.
For each of the four distributions:
- Draw 1000 values. Compute the empirical mean and variance.
- Plot a histogram and the ECDF.
- Compute $P(Y = 0)$ and $P(Y > 20)$.
- Which model would best describe a sample where most transects have zero whales but a few have huge counts?
library(ggplot2); library(dplyr); library(tidyr)
set.seed(2025)
n <- 1000; lambda <- 10
sims <- tibble(
pois = rpois(n, lambda),
nb_k5 = rnbinom(n, mu=lambda, size=5),
nb_k1 = rnbinom(n, mu=lambda, size=1),
nb_kp5= rnbinom(n, mu=lambda, size=0.5)
)
sims |> summarise(across(everything(), list(mean=mean, var=var, p0=~mean(.==0), pgt20=~mean(.>20))))
sims |> pivot_longer(everything()) |>
ggplot(aes(value, fill=name)) +
geom_histogram(binwidth=1) + facet_wrap(~name, scales="free_y") +
theme_minimal()
Reveal expected output and discussion
Approximate values (one seed) — yours will be close:
| Distribution | Mean | Variance | P(Y=0) | P(Y>20) |
|---|---|---|---|---|
| Pois(10) | 10.0 | 10 | 0.000 | 0.002 |
| NB(μ=10, k=5) | 10.0 | 30 | 0.004 | 0.046 |
| NB(μ=10, k=1) | 10.0 | 110 | 0.091 | 0.135 |
| NB(μ=10, k=0.5) | 10.0 | 210 | 0.218 | 0.155 |
Those numbers are the theoretical values the simulation fluctuates around, and none of them is arbitrary — each column is one formula evaluated, so you can predict the table before you run a single draw.
The variance column. Every row has mean 10 by construction; the story is entirely in the variance. Start from what the NB literally is — a Poisson whose rate is itself random (gamma-distributed) across transects, the mathematical version of "animals clump." That mixing gives the mean-variance law
Read it as two pieces: the $\mu$ is the irreducible Poisson noise, and the $\mu^2/k$ is the extra variance clumping adds. That second term is the whole point — it is what a plain Poisson cannot produce. The general move is to see $k$ as a clumping dial: as $k \to \infty$ the extra term vanishes and you recover $\mathrm{Var}=\mu$ (the Poisson is just NB with the clumping turned off), and as $k$ shrinks the extra term explodes. Only now plug in, with $\mu=10$:
Halving $k$ from 1 to 0.5 doubles the excess term (100 → 200) — clumping bites hardest at small $k$.
The $P(Y=0)$ column. A zero transect means "not one whale," and the NB pmf evaluated at zero collapses to a clean closed form,
so the same clumping dial controls how common empty transects are. Plug in $\mu=10$: $k=5$ gives $(5/15)^5=(1/3)^5=1/243\approx 0.004$; $k=1$ gives $(1/11)^1\approx 0.091$; $k=0.5$ gives $(0.5/10.5)^{0.5}=\sqrt{1/21}\approx 0.218$. The Poisson is the limit again — $(k/(k+\mu))^k \to e^{-\mu}$ as $k\to\infty$, and $e^{-10}\approx 0.0000454$, which rounds to the $0.000$ in the table. So a Poisson essentially never gives an empty transect, while NB($k=0.5$) gives one more than a fifth of the time.
(d) Small $k$ inflates both tails at once — the $P(Y=0)$ and $P(Y>20)$ columns rise together — which is exactly the "many zeros, a few huge counts" signature. The dataset described matches NB with small $k$; most ecological count data are like this.
Conceptual takeaway: the Poisson is a one-parameter distribution and is forced to have variance equal to its mean. Real ecological counts almost never satisfy that. Use the negative binomial unless you have a specific reason not to.
Humboldt marten survival
Five GPS-collared Humboldt martens each have probability $p = 0.7$ of surviving to the next month, independently.
- What is the probability that exactly $k$ of them survive, for $k = 0, 1, 2, 3, 4, 5$?
- Sketch the resulting bar plot of probabilities.
- What is the name of this distribution?
Reveal worked solution
k <- 0:5
probs <- dbinom(k, size=5, prob=0.7)
round(probs, 3)
# 0.002 0.028 0.132 0.309 0.360 0.168
barplot(probs, names.arg=k, ylab="P(survivors)")
This is the Binomial distribution, $X \sim \text{Bin}(n=5, p=0.7)$. dbinom is not a black box — it evaluates the binomial pmf, and it is worth seeing where one entry comes from so you could reproduce any of them by hand:
Two factors, pulling in opposite directions. The $p^{k}(1-p)^{n-k}$ is the probability of one specific survivor/casualty pattern; the $\binom{n}{k}$ counts how many distinct patterns give exactly $k$ survivors. Take the peak value, $k=4$:
Now the mode makes sense. Each extra survivor multiplies the probability by $\tfrac{p}{1-p}=\tfrac{0.7}{0.3}=2.33$ but by a shrinking combinatorial factor; the two forces balance and the pmf turns over at $k=\lfloor(n+1)p\rfloor=\lfloor 6\times0.7\rfloor=\lfloor 4.2\rfloor=\mathbf{4}$ — so even though each marten is more likely to live than die, "all five survive" ($p=0.168$) is less likely than "four of five" ($p=0.360$).
Chronic wasting disease in mule deer
A herd has a true CWD prevalence of $p = 0.12$. You sample $n = 24$ deer independently.
- Write the data-generating model in mathematical notation.
- What is the probability that exactly 3 of the 24 are infected?
- What is the probability that 4 or fewer are infected?
- What is the probability that no infections are detected (and what does that say about surveillance design)?
Reveal worked solution
(a) $Y \sim \text{Bin}(n=24, p=0.12)$.
dbinom(3, 24, 0.12) # P(Y=3) = 0.2387
pbinom(4, 24, 0.12) # P(Y<=4) = 0.8471
dbinom(0, 24, 0.12) # P(Y=0) = 0.0465
(b) Same pmf as Exercise 6, just bigger numbers — $P(Y=3)=\binom{24}{3}(0.12)^3(0.88)^{21}=2024\times0.001728\times0.0683=0.239$.
(d) The zero-case probability is the one you can do in your head, because at $k=0$ the combinatorial factor is $\binom{24}{0}=1$ and the whole pmf collapses to "every single deer must dodge infection":
That is the load-bearing quantity for surveillance, so make it general. The chance of catching at least one case in $n$ samples is its complement,
and flipping it around tells a program how many deer to sample: to be $95\%$ sure of detecting disease at prevalence $p$, solve $1-(1-p)^n \ge 0.95$ for $n$. Here, with $p=0.12$ and $n=24$, $P(Y\ge1)=1-0.0465=0.95$ — so $n=24$ is almost exactly the sample size that buys you 95% detection power at 12% prevalence. Even at that prevalence, a smaller sample would leave a real chance of a clean-but-wrong "no cases found." This is precisely the arithmetic behind "freedom from disease" sample-size calculations.
Elk age-sex classification
In Rocky Mountain National Park, the population mix is roughly: bulls 0.20, cows 0.45, calves 0.25, yearlings 0.10. A classification flight observes $n = 80$ elk and records each one's class.
- What is the probability of observing exactly (18, 38, 18, 6)?
- Simulate 5000 flights and plot the marginal distribution of "calves observed".
- What distribution is each marginal from a multinomial?
Reveal worked solution
p <- c(0.20, 0.45, 0.25, 0.10)
dmultinom(c(18, 38, 18, 6), prob=p) # ~0.0011
set.seed(1)
flights <- rmultinom(5000, size=80, prob=p)
hist(flights[3,], main="Calves out of 80", xlab="count")
(a) Where the 0.0011 comes from. The multinomial pmf is the binomial's "many boxes" generalization: one multinomial coefficient counting the orderings, times each category's probability raised to its observed count,
With $\mathbf{x}=(18,38,18,6)$, $n=80$, that is $\dfrac{80!}{18!\,38!\,18!\,6!}\,(0.20)^{18}(0.45)^{38}(0.25)^{18}(0.10)^{6}\approx 0.0011$. It reads small, but every specific outcome is small — there are thousands of plausible flights, and even the single most likely one carries only about 1% of the probability.
(c) Why each margin is Binomial — the collapse trick. Don't take it on faith; derive it, because it is the move that lets you reason about one category at a time. Fix your attention on calves and lump the other three classes into a single "not-calf" box. Each of the 80 elk is now just calf (prob $0.25$) or not-calf (prob $0.20+0.45+0.10=0.75$), independently — and "count of successes in $n$ independent yes/no trials with fixed $p$" is the definition of the Binomial. So
The general rule: any single category of a Multinomial$(n,\mathbf{p})$ is Binomial$(n,p_i)$ — collapse the rest into "other" and the multi-box problem becomes the two-box problem you already know. (Note $\mathrm{Var}=15$ is below the mean 20: unlike the whales, a multinomial margin is under-dispersed, because a fixed total $n$ caps how large any one category can run.)
Likert-scale survey simulation
You survey $n=80$ commercial fishers about a new regulation, with prior expected responses: strongly oppose 0.30, oppose 0.20, neutral 0.15, support 0.20, strongly support 0.15.
- Simulate one survey.
- Repeat 500 times and look at the variability of "strongly support."
Reveal worked solution
p <- c(.30, .20, .15, .20, .15)
rmultinom(1, 80, p)
sims <- rmultinom(500, 80, p)
summary(sims[5,])
Same collapse trick as the elk (Exercise 8): lump the other four responses into "not strongly-support" and the marginal is $\text{Bin}(80, 0.15)$, with $E=np=12$ and $\mathrm{Var}=np(1-p)=80\cdot0.15\cdot0.85=10.2$, so $\mathrm{SD}=\sqrt{10.2}\approx 3.2$. That is the punchline made concrete: a typical single survey lands anywhere from about 6 to 18 "strongly support" votes (12 $\pm$ two SDs) even though the underlying rate never moved — the sample-to-sample range is large compared to the point estimate, which is exactly why one survey is weak evidence.
Cystic fibrosis generalized (back to Punnett, with R)
Two Cc parents will have $n$ children. Affected = cc.
- Write the data-generating model.
- What is the probability that at least one child of $n=5$ is affected?
- For what number of children does that probability cross 50%?
Reveal worked solution
(a) $K \sim \text{Bin}(n, 0.25)$.
1 - dbinom(0, 5, 0.25) # 0.7627
# smallest n with P(>=1) >= 0.5
which(1 - dbinom(0, 1:10, 0.25) >= 0.5)[1] # 3
(b) The "at least one" complement. Never sum $P(1)+P(2)+\dots+P(5)$ — go through the one outcome that's a single term. "At least one affected" is the complement of "none affected," and "none affected" needs every child to independently escape, so the messy sum collapses to
At $n=5$ that is $1-(0.75)^5 = 1-0.2373 = 0.763$.
(c) Where 3 comes from — solve it, don't scan it. The which() above brute-forces a list, but the threshold has a closed form. You want the smallest $n$ with $1-(0.75)^n \ge 0.5$, i.e. $(0.75)^n \le 0.5$. Take logs (the log is negative, so the inequality flips):
You can see the crossing by hand: $(0.75)^2 = 0.5625$ (still more than half a chance of no affected child) but $(0.75)^3 = 0.422$ (now under half), so the coin flips between the second and third child — 3 children. The same $\ln 0.5 / \ln(1-p)$ formula gives the "how many trials until an even chance of at least one hit" threshold for any $p$: rare detections need many trials, common ones only a few.
Stretch problems (optional)
Connecting Bernoulli and Binomial via expectation algebra
Let $Y_1, \dots, Y_n$ be independent Bernoulli($p$). Let $X = \sum_{i=1}^n Y_i$. Using only $E[Y_i] = p$, $\mathrm{Var}(Y_i) = p(1-p)$, and the linearity rules, prove:
- $E[X] = np$.
- $\mathrm{Var}(X) = np(1-p)$.
- Why does independence matter for (b) but not (a)?
Reveal solution
(a) $E[X] = E[\sum Y_i] = \sum E[Y_i] = np$. Linearity of expectation holds even without independence.
(b) $\mathrm{Var}(X) = \mathrm{Var}(\sum Y_i)$. Under independence, variances add: $= \sum \mathrm{Var}(Y_i) = np(1-p)$.
(c) If the $Y_i$ were correlated, you would pick up covariance terms: $\mathrm{Var}(\sum Y_i) = \sum \mathrm{Var}(Y_i) + 2\sum_{i<j}\mathrm{Cov}(Y_i,Y_j)$. Positive correlation (the rule in ecology — birds in flocks, deer in herds) inflates the variance — this is exactly why over-dispersed count distributions exist.
Poisson as a limit of the Binomial
Show that if $X \sim \text{Bin}(n, p)$ with $n \to \infty$ and $p \to 0$ in such a way that $np \to \lambda$, then $P(X = k) \to e^{-\lambda} \lambda^k / k!$.
Then use simulation to demonstrate this with $\lambda = 4$: draw 10,000 values from each of Bin(20, 0.2), Bin(100, 0.04), Bin(1000, 0.004), and Pois(4). Compare histograms and means/variances.
Reveal sketch
Math. Start from the binomial pmf and substitute $p=\lambda/n$ (this is what "$np\to\lambda$" means — hold the rate fixed while $n$ grows):
The trick is to regroup into a $\lambda^k/k!$ that will survive, times three pieces that each have a clean limit. Expand $\binom{n}{k}=\dfrac{n!}{k!\,(n-k)!}=\dfrac{n(n-1)\cdots(n-k+1)}{k!}$ and split the last factor's exponent:
Now send $n\to\infty$ with $k$ and $\lambda$ fixed, and take the three pieces in turn:
- (1) is a product of $k$ fixed factors $\tfrac{n}{n}\cdot\tfrac{n-1}{n}\cdots\tfrac{n-k+1}{n} = 1\cdot(1-\tfrac1n)\cdots(1-\tfrac{k-1}{n})$, each $\to 1$, so the whole thing $\to 1$.
- (2) is the definition of the exponential: $\left(1-\tfrac{\lambda}{n}\right)^{n}\to e^{-\lambda}$.
- (3) has a fixed exponent $-k$ and a base $\to 1$, so it $\to 1^{-k}=1$.
The three pieces collapse to $1\cdot e^{-\lambda}\cdot 1$, leaving
the Poisson pmf. Each piece carried one idea: (1) says "with $n$ huge, choosing $k$ of them barely dents the pool," (2) manufactures the $e^{-\lambda}$, and (3) is the correction that vanishes. Notice the mean-variance signature falls out for free: the binomial variance $np(1-p)=\lambda(1-\lambda/n)\to\lambda$, matching its mean — the equidispersion the Poisson is famous for is just the $p\to0$ limit of $np(1-p)$.
Conceptual takeaway. Poisson is the natural distribution for many independent rare events occurring in a fixed window of time/space, whose rate aggregates to $\lambda$. Whale sightings per transect, salmon redds per km, parasites per host (if independent) — all natural Poisson stories.
for(params in list(c(20,0.2), c(100,0.04), c(1000,0.004))) {
x <- rbinom(1e4, params[1], params[2])
cat("n=", params[1], " mean=", mean(x), " var=", var(x), "\n")
}
y <- rpois(1e4, 4); cat("Pois mean=", mean(y), " var=", var(y))
The Monty Hall problem (for camera-trap analogy)
Three trail cameras: a marten was photographed by exactly one of them, with equal prior probability (1/3 each). You guess Camera A. The technician (who knows which camera holds the marten) opens Camera C, which shows nothing. Should you switch to Camera B?
- Reason out the answer using conditional probability.
- Simulate 100,000 trials to verify.
Reveal solution
Switch. The intuition is that your initial guess (A) was right with probability 1/3, and opening C collapses the remaining 2/3 onto B — but "collapses" is doing a lot of work, so derive the 2/3 with Bayes instead of asserting it. Let the marten's true camera be $A$, $B$, or $C$ (prior 1/3 each), and condition on the event "technician opens C." The whole subtlety lives in the likelihoods — how the technician behaves given each truth:
- $P(\text{opens C}\mid \text{marten at A}) = \tfrac12$ — your guess A is safe and the marten's A is safe, so the technician is free to open B or C, and picks C half the time.
- $P(\text{opens C}\mid \text{marten at B}) = 1$ — he cannot open A (your guess) or B (the marten), so he is forced to open C.
- $P(\text{opens C}\mid \text{marten at C}) = 0$ — he never opens the marten's own camera.
Total probability of the reveal:
Now Bayes for the two cameras still in play:
There is the 2/3, and now you can see exactly where it comes from: B's likelihood is twice A's ($1$ vs $\tfrac12$), because the technician's forced move when the marten is at B pumps evidence toward B that your own guess A never receives. Switching doubles your odds.
To verify that by simulation you have to model the technician and the switch explicitly — draw the truth and your guess, have the technician open a camera that is neither, then move to whatever remains:
set.seed(1)
trials <- 1e5
truth <- sample(1:3, trials, replace=TRUE) # camera holding the marten
guess <- sample(1:3, trials, replace=TRUE) # your initial pick
# Technician opens a camera that is neither your pick nor the marten's
opened <- vapply(seq_len(trials), function(i) {
choices <- setdiff(1:3, c(guess[i], truth[i]))
if (length(choices) == 1) choices else sample(choices, 1)
}, numeric(1))
# You switch to the one remaining unopened camera
switched <- vapply(seq_len(trials), function(i) {
setdiff(1:3, c(guess[i], opened[i]))
}, numeric(1))
mean(guess == truth) # staying wins: 0.33257
mean(switched == truth) # switching wins: 0.66743
Over 100,000 trials, staying won 0.33257 and switching won 0.66743 — matching the analytic 1/3 and 2/3.
guess <- 1 and report mean(truth != guess). That returns 2/3, but it is only P(your initial guess was wrong) — true by construction, with no technician and no switch anywhere in the code. It restates the claim rather than testing it. The simulation only has teeth once the technician is constrained to open a losing camera you did not pick.R cheat sheet (distribution functions)
| Distribution | Density / PMF | CDF | Quantile | Random draw |
|---|---|---|---|---|
| Bernoulli/Binomial | dbinom | pbinom | qbinom | rbinom |
| Poisson | dpois | ppois | qpois | rpois |
| Negative Binomial | dnbinom | pnbinom | qnbinom | rnbinom |
| Geometric | dgeom | pgeom | qgeom | rgeom |
| Multinomial | dmultinom | — | — | rmultinom |
Two common parameterization gotchas:
rnbinom(n, mu=, size=)uses the ecological "mean-and-dispersion" form: variance is $\mu + \mu^2/\text{size}$. Smallersize→ more clumping.rbinom(n, size, prob)takes size = number of trials, but JAGS/Nimble writedbinom(prob, size). Always check argument order when switching between R, JAGS, and Nimble — this single mismatch causes more bugs than any other.