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.
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 are worked through slowly, with examples and no math.
Core concepts (the conceptual scaffolding)
1. Probability axioms
- 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)$, which means that 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 that 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.
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
Shorter than both 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$.
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. The empirical cumulative distribution function is just the fraction of your draws at or below each value, so it climbs from 0 to 1 and shows the whole distribution without you having to pick a bin width. Base R gives it with
ecdf(y); in ggplot addstat_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
# A tibble is the Tidyverse version of a data frame: same idea, tidier printing.
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)
)
# pivot_longer stacks the four model columns into two: a "model" label column
# and a "y" value column. That long shape is what group_by() needs, and it
# keeps one row per model, one column per summary, so all four stay on screen.
# (Summarising the four columns side by side instead builds 16 columns, and
# the console hides most of them behind "i 9 more variables".)
sims |> pivot_longer(everything(), names_to="model", values_to="y") |>
group_by(model) |>
summarise(mean = mean(y),
var = var(y),
p0 = mean(y == 0),
pgt20 = mean(y > 20))
sims |> pivot_longer(everything()) |>
ggplot(aes(value, fill=name)) +
geom_histogram(binwidth=1) + facet_wrap(~name, scales="free_y") +
theme_minimal()
Reveal the theoretical table and discussion
Theoretical values. Every entry in this table is a formula evaluated exactly, none of it is simulation output. You can fill the whole table in before you draw a single number, and doing so first is the point:
| 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 |
Now compare your run, and mind the gap. Your 1000 draws will not reproduce this table. With set.seed(2025) the NB($k=0.5$) row comes back with a sample variance of $244.63$ against the theoretical $210$, and $P(Y=0)=0.202$ against $0.218$; the Poisson row’s sample variance, by contrast, lands almost on top of its theoretical variance ($9.49$ against $10$). Nothing has gone wrong. A sample variance is itself a random quantity, and for a heavy right tail it is a very noisy one. A handful of extreme transects shift it by tens of units, so 1000 draws pin it down only loosely. The rule to carry forward is that the more clumped the model, the more data you need before an empirical summary can be trusted. When a real dataset's variance sits this far from what your model predicts, the first question is “do I have enough data to tell?”, not “is the model wrong?”
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), so 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)$. Each marten either survives or does not, independently and with the same probability, so each one is a Bernoulli($p$) trial. The number of survivors is the sum of five independent Bernoulli trials, and a sum of independent Bernoulli trials is exactly what Binomial means:
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)?
- The chance of catching at least one case in $n$ samples is the complement of your answer to (d). Write that expression, then use it to work out how many deer you would need to sample to be 95% sure of detecting the disease at $p = 0.12$.
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 A2, just bigger numbers: $P(Y=3)=\binom{24}{3}(0.12)^3(0.88)^{21}=2024\times0.001728\times0.0683=0.239$.
(c) Cumulative rather than point: $P(Y \le 4) = \sum_{k=0}^{4} P(Y=k)$, which R adds up for you: pbinom(4, 24, 0.12) $= 0.847$. So at 12% prevalence, "four or fewer infected in a sample of 24" is the ordinary result. It happens about 85% of the time.
(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":
(e) 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$, the inequality rearranges to $n \ge \log(0.05)/\log(0.88) = 23.44$, so $n=24$ is the smallest sample that clears the bar, and it clears it barely: $P(Y\ge1)=1-0.0465=0.9535$, against $0.9471$ at $n=23$. 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
dmultinom(c(16, 36, 20, 8), prob=p) # the modal flight: 0.001832
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, because there are thousands of plausible flights, and the probability has to be shared out among all of them. Search the whole support and the modal flight turns out to be $(16, 36, 20, 8)$, exactly $n\mathbf{p}$, each category at its own expected count, and even that single most likely outcome carries only $0.001832$, under $0.2\%$ of the probability. Our $(18,38,18,6)$, at $0.00117$, is a perfectly ordinary flight.
(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. That is not special to the multinomial: any Binomial$(n,p)$ has $\mathrm{Var}/\mathrm{mean} = 1-p$, which is below 1 for every $p>0$, so a Binomial is always under-dispersed relative to a Poisson of the same mean. A multinomial margin inherits it because that margin is a Binomial. The reason is the fixed total. With $n$ pinned at 80, a category cannot run away the way an unbounded count can.)
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 A4): 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.
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, so 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.