Module 1 β Bayesian Concepts
Two meanings of probability Β· Bayesβ rule Β· a whole posterior from a discrete prior Β· summarising a posterior Β· posterior predictive distributions Β· confidence vs credible intervals
Every Bayesian analysis in this course is the same machine: prior Γ likelihood β posterior, and then everything you want to know (a best guess, an interval, a probability, a prediction, a decision) is read off the posterior. Module 1 shows that machine in the simplest possible setting, where the parameter can only take a list of values and Bayesβ rule is just multiplication followed by division by a sum. No integrals and no MCMC yet.
The module opens with the philosophical difference that drives everything else: what is a probability? The classical (frequentist) definition only allows probabilities for repeatable events, so a fixed but unknown parameter cannot have one. The Bayesian definition treats probability as a measure of uncertainty, so a parameter, a unique event or a future observation can all have a probability distribution.
That difference has practical consequences. It is why a credible interval can say βthe parameter is in here with 95% probabilityβ and a confidence interval cannot, and why Bayesian models can predict new data directly. The examples on this page are made up for this app: a thyroid screening test, a treatment decision and a count of fish in a pond.
The page has two parts. Part A (concepts, no code needed) covers the ideas; mark each section as you go. Part B runs small R examples in your browser.
Course notes: Introduction and Module 1 βBayesian conceptsβ (up to p. 52). Reading deadline: Seminar 1, Monday 7 September 2026.
Optional textbook: McElreath, Statistical Rethinking (2nd ed.), chapters 1β3 cover the same ground (grid approximation, sampling from the posterior).
Examples on this page (screening, treatment threshold, captureβrecapture) are written for this app. The course notes use other examples; work through those in the notes.
Conditional probability. \(P(A \mid B) = P(A \cap B) / P(B)\). βProbability of A given B.β In general \(P(A \mid B) \neq P(B \mid A)\): the probability of a positive test given disease is not the probability of disease given a positive test.
Law of total probability. If \(B\) takes the values \(b_1, \dots, b_k\), then \(P(A) = \sum_k P(A \mid b_k) P(b_k)\). This is the denominator of Bayesβ rule.
Likelihood. For fixed data \(y\), the likelihood is \(p(y \mid \theta)\) viewed as a function of the parameter \(\theta\). It is not a probability distribution over \(\theta\) (it need not sum or integrate to 1). Maximum likelihood (STA110, STA140) picks the \(\theta\) that makes the observed data most probable.
Part A Β· Concepts
1.1 Two meanings of probability
| Frequentist (classical) | Bayesian | |
|---|---|---|
| Probability is⦠| the long-run relative frequency of an event in infinitely many repetitions under identical conditions | a number between 0 and 1 that measures uncertainty (partial knowledge) about anything unknown |
| Can have a probability | repeatable events: the next coin toss, a random sample | also unique events and fixed unknown quantities: βthis patient has the diseaseβ, βthe true mean is above 5β |
| Parameters are⦠| fixed constants; randomness is only in the data | unknown, so described by a distribution |
| Typical output | estimator, standard error, p-value, confidence interval | posterior distribution, credible interval, posterior predictive distribution |
Two problems with the frequency definition: βidentical conditionsβ is vague (the same coin, the same person tossing it, the same air pressure?) and, more seriously, many events cannot be repeated even in principle. Whether it rained in Gothenburg on 1 September 2026, or whether this particular patient responds to treatment, has no long-run frequency. Yet weather services publish a probability of rain every day, and clinicians talk about a patientβs probability of disease. The Bayesian view makes that everyday usage formal.
Every analysis contains choices: which data to collect, which model to fit, which variables to adjust for, which results to report. The prior is one more choice, and it is written down explicitly where others can inspect and vary it. The course notes deliberately put the prior last in the list of error sources: data errors, software errors and model misspecification are usually more dangerous and harder to detect.
1.2 Why use probability for beliefs?
The notes give three justifications. None is a proof; each makes the Bayesian approach reasonable.
- Analogy. We already describe physical randomness with probability. Using the same language for uncertainty due to incomplete knowledge is natural.
- Decision theory. Decisions under uncertainty should minimise expected loss. If you want to act rationally on uncertain information, you need numbers that behave like probabilities.
- Coherence of bets. If the prices you would pay for bets on events do not obey the rules of probability, a bookmaker can arrange bets so that you lose money whatever happens (a βDutch bookβ).
Worked example: when to treat. Suppose treating a healthy patient costs 1 unit (side effects, cost), and not treating a sick patient costs 9 units. Let \(p\) be the probability that the patient is sick.
| Decision | Expected loss |
|---|---|
| Treat | \((1-p) \times 1\) |
| Do not treat | \(p \times 9\) |
Treat when \((1-p) < 9p\), that is when \(p > 1/10\). In general, treat when
\[p > \frac{\text{cost of a false positive}}{\text{cost of a false positive} + \text{cost of a false negative}}.\]
This threshold is the same for every patient. What differs is \(p\), and \(p\) is specific to this patient with these symptoms. A single long-run frequency cannot provide it; a probability for the individual case can.
A decision that minimises expected loss can still turn out badly for a particular patient. Decision theory only promises the best average over repeated decisions made with the information available at the time.
1.3 Bayesβ rule and the base-rate fallacy
\[p(B \mid A) = \frac{p(A \mid B)\, p(B)}{p(A)} = \frac{p(A \mid B)\, p(B)}{\sum_b p(A \mid B = b)\, p(B = b)}\]
Bayesβ rule follows directly from the definition of conditional probability. Nobody disputes it. The debate between schools of statistics is about which quantities may have probability distributions, not about the rule itself.
Worked example: screening for hypothyroidism. In a screening population 2% have the condition. The test has sensitivity 0.95 (positive when sick) and specificity 0.90 (negative when healthy). A person tests positive. What is the probability that they have the condition?
Think in natural frequencies, 10,000 people:
| Test positive | Test negative | Total | |
|---|---|---|---|
| Condition | 190 | 10 | 200 |
| No condition | 980 | 8,820 | 9,800 |
| Total | 1,170 | 8,830 | 10,000 |
\[P(\text{condition} \mid +) = \frac{0.95 \times 0.02}{0.95 \times 0.02 + 0.10 \times 0.98} = \frac{190}{1170} \approx 0.16\]
Only 16%, despite a test that is right 95% and 90% of the time. There are more false positives (980) than true positives (190) because healthy people vastly outnumber sick people. Ignoring the prevalence (the prior) is the base-rate fallacy. Using Bayesβ rule is uncontroversial here: the fallacy comes from not using it.
Vocabulary that maps directly onto Bayesian terms:
| Diagnostic testing | Bayesian inference |
|---|---|
| Prevalence, pre-test probability | Prior \(p(\theta)\) |
| Sensitivity and specificity | Likelihood \(p(y \mid \theta)\) |
| Positive predictive value, post-test probability | Posterior \(p(\theta \mid y)\) |
A p-value is (roughly) \(P(\text{data this extreme} \mid H_0)\). Reading it as \(P(H_0 \mid \text{data})\) swaps the condition, the same mistake as reading sensitivity as the positive predictive value. Getting from one to the other requires Bayesβ rule, and therefore a prior probability for \(H_0\).
1.4 A whole posterior from a discrete prior
Screening had one number as the answer. Usually the unknown can take many values and we want its whole posterior distribution. With a discrete parameter this still only needs multiplication and one sum.
Worked example: how many fish are in the pond? You catch, mark and release 30 fish. A week later you catch 25 fish and 5 of them carry a mark. Let \(N\) be the number of fish in the pond.
Likelihood. If the pond has \(N\) fish and fish mix freely, the number of marked fish in a catch of 25 follows a hypergeometric distribution:
\[p(y = 5 \mid N) = \frac{\binom{30}{5}\binom{N-30}{20}}{\binom{N}{25}}\]
It is zero for \(N < 50\): you have already seen \(30 + 20 = 50\) different fish. The maximum likelihood estimate is about \(30 \times 25 / 5 = 150\) (the classical LincolnβPetersen estimate).
Prior. A local angler says βa few hundred, probably not more than a thousandβ. A log-normal distribution with median 300 and log-scale SD 0.6 puts about 95% of its mass between roughly 90 and 1000. Restrict it to \(N = 50, \dots, 3000\) and rescale so it sums to one.
Bayesβ rule on a grid. For every candidate \(N\): multiply prior Γ likelihood, then divide by the sum over all \(N\).
The posterior sits between the prior (median 300) and the likelihood (peak near 150): posterior mode 165, median 195, mean 213, and a 90% credible interval from 115 to 372. The prior pulls the estimate up because five recaptures out of 25 is little data. The likelihoodβs long right tail (large ponds are hard to rule out with few recaptures) is tamed by the priorβs belief that ponds of 2,000 fish are unusual.
Two conditions for this to work, from the notesβ abbreviated workflow:
- the likelihood must be defined for every value the prior allows (it may be zero for some), and
- at least some values with non-zero prior must give the observed data non-zero probability. If the prior only allowed \(N < 50\), no posterior would exist.
1.5 Summarising a posterior
The posterior is the answer. Summaries are chosen for the reader and the decision at hand.
| Summary | Definition | Best under loss⦠|
|---|---|---|
| Posterior mean | \(E[\theta \mid y]\) | squared error |
| Posterior median | 50th percentile | absolute error |
| Posterior mode (MAP) | \(\arg\max_\theta p(\theta \mid y)\) | βall or nothingβ (0β1 loss) |
| Posterior SD | SD of the posterior | describes uncertainty about \(\theta\) |
| Equal-tailed 95% credible interval | 2.5th to 97.5th percentile | easy, works with transformations |
| 95% highest posterior density (HPD) interval | shortest region containing 95% | can be several pieces if the posterior has several modes |
A 95% credible interval means what people usually want a confidence interval to mean: given the model, the prior and the data, \(\theta\) lies in the interval with probability 0.95.
Transformations. Because a monotone transformation keeps the order of values, interval endpoints transform directly: if \([-0.40, 0.10]\) is a 95% credible interval for a log relative risk, then \([e^{-0.40}, e^{0.10}] = [0.67, 1.11]\) is a 95% credible interval for the relative risk. Means do not: \(E[e^{\theta}] \neq e^{E[\theta]}\). The median transforms directly because it is a percentile.
With a sample of draws (from Module 3 on, MCMC gives you draws from the posterior), every summary becomes an average or a percentile over draws. A derived quantity, such as a ratio of two parameters, is computed draw by draw, which keeps the correlation between parameters intact.
Data SD (\(\sigma\)): how much individual observations vary. Posterior SD of a mean: how uncertain you are about the mean; it shrinks as \(n\) grows, like a standard error. Posterior predictive SD: how uncertain you are about one new observation; it never shrinks below \(\sigma\). A larger \(\sigma\) usually means larger posterior and predictive SDs too, but they answer different questions.
1.6 Posterior predictive distributions
Bayesian models are generative: given parameter values, you can simulate new data. Integrating over the posterior gives the posterior predictive distribution:
\[p(\tilde y \mid y) = \int p(\tilde y \mid \theta)\, p(\theta \mid y)\, d\theta\]
It is easiest to compute by Monte Carlo:
Posterior predictive by Monte Carlo
- Repeat many times: (a) draw \(\theta^{(s)}\) from the posterior; (b) draw a new \(\tilde y^{(s)}\) from \(p(y \mid \theta^{(s)})\).
- Summarise the \(\tilde y^{(s)}\): a mean, a probability such as \(P(\tilde y \geq 10)\), or an expected loss.
Fish example: next week you catch 40 fish. Each posterior draw of \(N\) gives one simulated number of marked fish. Because \(N\) is uncertain, the predictive distribution is wider than a hypergeometric distribution with \(N\) fixed at its posterior mean. Plugging in a single best guess underestimates uncertainty.
To check whether you ran enough Monte Carlo draws, rerun the simulation and look at how much the answer changes: that variation is the Monte Carlo error. More draws shrink it at the rate \(1/\sqrt{\text{number of draws}}\).
Conditionally generative models. A regression model predicts \(y\) given \(X\); it does not model the covariates themselves. That is fine when you choose \(X\) (a dose, a treatment). Not every method is generative: a permutation test gives a p-value but cannot say what the next observation will look like.
1.7 The Bayesian workflow, abbreviated
The notes give a four-step version here and expand it in Module 6:
- Full probability model for everything: observed data, unknown parameters, future or missing data. Requires knowing the research question and how the data were collected.
- Prior: express the available information about the unknowns as a joint prior distribution.
- Condition on the data: compute the posterior (and posterior predictive) of the quantities of interest.
- Evaluate: does the model fit the data? Are the conclusions reasonable? How sensitive are they to the model and the prior?
Step 4 usually sends you back to step 1 or 2: the workflow is a loop, not a line. Modules 3 and 4 give the tools for step 4.
1.8 Confidence intervals are not credible intervals
Both are abbreviated βCIβ and with large samples they often agree numerically. They answer different questions.
95% confidence interval
A property of the procedure: if the experiment were repeated infinitely often, 95% of the intervals produced would contain the true value. Says nothing about this interval.
95% credible interval
A property of this posterior: given the model, prior and data, the parameter lies in the interval with probability 0.95.
Why a confidence interval cannot be read as a credible interval (the notesβ argument, with made-up numbers):
- Coverage is about the procedure, not the interval. βFlip a coin: heads, report \((-\infty, \infty)\); tails, report the single point \(\{0\}\)β is a valid 50% confidence procedure for any non-zero parameter. It never uses the data. Its intervals are either useless or almost certainly wrong, yet the procedure has exact 50% coverage. So the width of a confidence interval does not by itself measure precision.
- Several valid procedures give different intervals for the same data. With 4 successes out of 20:
| Interval | Lower | Upper |
|---|---|---|
| Wald 95% CI | 0.025 | 0.375 |
| Wilson 95% CI | 0.081 | 0.416 |
| Exact (ClopperβPearson) 95% CI | 0.057 | 0.437 |
| 95% credible interval, Beta(1, 1) prior | 0.082 | 0.419 |
If every valid 95% confidence interval were a 95% probability statement about \(\theta\), the region between the Wilson and exact limits would have to contain both 0% and a positive share of the probability. That is a contradiction. So confidence intervals are not automatically probability statements; at most one procedure could coincide with some posterior, and not because it is a confidence procedure.
- A credible interval has no such contradiction: one prior + one model + one dataset gives exactly one posterior. Two people with different priors get different posteriors, just as two people with different data or models would.
Do credible intervals have 95% coverage in repeated experiments? Not in general. Coverage depends on how the prior relates to the true value: a prior that puts zero probability on the true value gives zero coverage. Module 2, Section 2.8 shows this in a simulation.
The true value is fixed and your interval is fixed: it either contains the value or not. The 95% refers to the long run of hypothetical repetitions. The sentence βthere is a 95% probability that the effect lies between A and Bβ is a credible-interval statement and needs a prior.
1.9 Hypothesis tests and Bayes factors
You can compute \(P(\theta > 0 \mid y)\) from a posterior. Two cautions from the notes:
- It depends on the prior probability that \(\theta > 0\). If you then dichotomise at 0.05, two reasonable priors can give 0.049 and 0.051 and opposite decisions.
- For a continuous parameter \(P(\theta = 0 \mid y) = 0\): a point null always has zero posterior probability.
The Bayes factor compares whole models instead:
\[\underbrace{\frac{p(m_1 \mid y)}{p(m_2 \mid y)}}_{\text{posterior odds}} = \underbrace{\frac{p(y \mid m_1)}{p(y \mid m_2)}}_{\text{Bayes factor}} \times \underbrace{\frac{p(m_1)}{p(m_2)}}_{\text{prior odds}}\]
BF > 1 favours \(m_1\). The Bayes factor does not depend on the prior odds of the models, but it does depend on the priors for the parameters inside each model: a vague prior for an effect in \(m_1\) spreads prior mass over huge effects and lowers \(p(y \mid m_1)\). You must specify what the alternative hypothesis looks like. The course notes do not use hypothesis testing after Module 1. Later modules compare models by predictive performance instead (Module 4).
When does the Bayesian answer differ most?
- Small samples and strong prior information: the prior visibly moves the answer (the fish example).
- Unique events and decisions: a probability for this patient, this trial, or an expected loss.
- Parameters near a boundary (proportions near 0 or 1, variances near 0), where classical intervals can include impossible values.
- With large samples and weak priors the likelihood dominates and Bayesian and classical results usually agree numerically, though their interpretations still differ.
Limitations
- The answer depends on the prior and the model. That dependence is visible and can be checked (sensitivity analysis), but it cannot be removed.
- Translating vague knowledge (βprobably smallβ) into a specific distribution is hard, and βI have no ideaβ cannot be expressed exactly (see flat priors in Module 2).
- A grid over all parameter values only works for one or two parameters. Beyond that, computation needs MCMC (Module 3).
Part B Β· Practice in the browser
Cells marked Run Code run R in your browser. The first run on a page takes a few seconds while R loads.
Exercise 1 Β· Screening. The function computes the positive predictive value.
- What is the PPV in a general population where the prevalence is 0.2%?
- Which prevalence gives a PPV of 0.5? Try values, or solve with
uniroot(). - Keep prevalence at 2%. Which matters more for PPV here: raising sensitivity to 0.99 or raising specificity to 0.99?
ppv(0.002, 0.95, 0.90)β 0.019. Fewer than 2 in 100 positives have the condition.uniroot(function(p) ppv(p, 0.95, 0.90) - 0.5, c(0.001, 0.5))$rootβ 0.095, about 9.5%.- Specificity:
ppv(0.02, 0.99, 0.90)β 0.17 butppv(0.02, 0.95, 0.99)β 0.66. With a rare condition, false positives come from the large healthy group, so specificity dominates.
Exercise 2 Β· The fish posterior on a grid.
- Replace the prior with a flat one (
prior <- rep(1, length(pond_size)), then normalise). How do the mean and the 90% interval change? Why does the upper limit depend on the arbitrary cut-off at 3000? - Suppose
recapturedhad been 15. Rerun with the log-normal prior. Does the prior still matter much? - What is the posterior probability that the pond holds more than 400 fish?
- The mean rises (about 230) and the 90% interval widens to about 106β477. With a flat prior the long right tail of the likelihood is not tamed; with too few recaptures, very large ponds stay somewhat plausible. Change the grid to
50:10000and the mean moves again: a βflatβ prior on a bounded grid is still a choice. - With 15 recaptures the likelihood is concentrated near 50 fish, and prior and flat-prior posteriors almost agree. More data means less prior influence.
sum(posterior[pond_size > 400])β 0.035.
Exercise 3 Β· Posterior predictive by Monte Carlo. Run after Exercise 2 (it uses posterior).
- Why is
sd_fulllarger thansd_plugin? - Run the cell three times. How much does
prob_10_or_morechange? What does that tell you about the Monte Carlo error with 20,000 draws?
sd_fullincludes uncertainty about \(N\) as well as the randomness of the catch; the plug-in version pretends \(N\) is known.- It changes in the third decimal. That variation is the Monte Carlo error; it is small compared with the uncertainty the probability expresses, so 20,000 draws is enough here.
Why-questions
Bayesβ rule is a theorem of probability. The debate is about whether unknown fixed quantities (parameters, hypotheses, unique events) may have probability distributions, and so whether a prior may be used at all.
The likelihood is a product over observations, so it becomes more and more concentrated while the prior stays the same. Eventually the likelihood dominates the product prior Γ likelihood. The exception: values with zero prior probability stay impossible whatever the data say.
A single estimate ignores the uncertainty about the parameter, so predictions are too narrow and rare events look rarer than they are. Tail events (extreme values, rare adverse outcomes) are where this matters most.