
Probability and Bayesian Inference
Department of Biostatistics and Computational Biology, University of Rochester Medical Center
In the 10,000-door Monty Hall problem, you chose door \(i\) and the host left one other door, \(j\), closed.
\[ P(H_n\mid Y=j) \propto P(Y=j\mid H_n)P(H_n). \]
The host’s rule supplies the likelihood. Today we build the probability machinery behind this update, then use the same machinery in a graphical model.
Probability lives on a triple \((\Omega, \mathcal F, P)\). The first two pieces are sets; the third is a function.
Roll a die once.
| outcome | \(\omega = 3\) |
| sample space | \(\Omega = \{1,2,3,4,5,6\}\) |
| event “even” | \(A = \{2,4,6\}\) |
| event “at least 5” | \(B = \{5,6\}\) |
| \(A\) AND \(B\) | \(A \cap B = \{6\}\) |
| NOT \(A\) | \(A^c = \{1,3,5\}\) |
An outcome is a point. An event is a set of points. A single outcome \(\{3\}\) is also an event, but a set with one element, not the element itself.
\(P : \mathcal F \to [0,1]\) assigns a number to every event, not to outcomes directly, subject to
\[ P(\Omega) = 1, \qquad P\Big(\bigcup_{k} A_k\Big) = \sum_k P(A_k) \;\text{ for disjoint } A_1, A_2, \ldots \]
Notation for the whole course. \(P(\cdot)\) is a probability measure, always applied to an event. \(p(\cdot)\) is a mass function or a density, a function of a value. We write \(P(X = x)\) and \(p(x)\) for the same number when \(X\) is discrete.
Let \(\omega\) denote an outcome and \(A\) an event. Once \(\omega\) is known, the statement “\(A\) occurred” is either true or false:
\[ 1_A(\omega) = \begin{cases} 1, & \omega\in A,\\ 0, & \omega\notin A. \end{cases} \]
Before \(\omega\) is known, \(P(A)\in[0,1]\) represents our uncertainty. A truth value and a probability are different objects.
Probability theory expresses uncertainty about events and provides rules for combining those uncertainties.
Probability extends logic to situations where truth is uncertain.
For events \(A, B\),
\[ P(A\cap B) = P(A\mid B)P(B), \]
and
\[ P(A\cup B) = P(A)+P(B)-P(A\cap B). \]
If \(A\) and \(B\) are independent, \(P(A\mid B)=P(A)\) and therefore \(P(A\cap B)=P(A)P(B)\).

For \(P(B)>0\), conditioning restricts attention to outcomes in \(B\) and renormalizes:
\[P(A \mid B) = \frac{P(A\cap B)}{P(B)}.\]

Events \(A\) and \(B\) are conditionally independent given \(C\) if,
\[P(A\cap B \mid C) = P(A\mid C)P(B\mid C),\]
we write \(A \bot B | C\).
Once \(C\) is known, learning \(A\) provides no additional information about \(B\).

A random variable represents an uncertain quantity that can take values in some set \(\mathcal{X}\).
\[ X \in \mathcal{X}. \]
A random variable is a (measurable) function \(X : \Omega \to \mathcal{X}\) that maps an outcome \(\omega\) to a value \(x = X(\omega)\).
A probability distribution describes our uncertainty about the possible values of a random variable.
For a discrete random variable,
\[ p(x) = P(X = x), \]
and probabilities sum to \(1\):
\[ \sum_{x \in \mathcal X} p(x) = 1. \]
For a continuous real-valued random variable, a density satisfies
\[ p(x)\geq 0, \qquad \int_{\mathcal X} p(x)\,dx = 1. \]
Note
A density is not a probability at a point and may exceed \(1\). Probabilities come from integrating density over sets.
If \(\mathcal{X} \subseteq \mathbb R\), then
\[ F(x) = P(X \leq x). \]
If \(X\) has density \(p\), then
\[ F(x) = \int_{-\infty}^{x} p(y) dy. \]
If \(X\) is discrete, then
\[ F(x) = \sum_{y \leq x} p(y). \]
The CDF requires an ordered support; a density does not.
For random variables \(X,Y,Z\) and possible values \(x,y,z\):
\[p(x_{1:N}) = p(x_1) \prod_{n=2}^{N} p(x_n \mid x_{1:n-1}).\]
The product rule can be written in either direction:
\[p(x,y)=p(y\mid x)p(x)=p(x\mid y)p(y).\]
Therefore,
\[ \boxed{p(x\mid y)=\frac{p(y\mid x)p(x)}{p(y)}} \qquad\text{where}\qquad p(y)=\sum_x p(y\mid x)p(x), \]
with the sum replaced by an integral for continuous \(X\).
Expectation is a probability-weighted average. For a function \(g\),
\[ \mathbb E[g(X)] = \begin{cases} \displaystyle \sum_{x\in\mathcal X} g(x)p(x), & X \text{ discrete},\\[1em] \displaystyle \int_{\mathcal X} g(x)p(x)\,dx, & X \text{ continuous with density }p. \end{cases} \]
This identity turns integration into averaging, which is the basis of Monte Carlo methods in Lab 2.
An indicator converts an event into a numerical random variable:
\[\begin{aligned} 1_A(x) = \left\{ \begin{array}{ll} 0 & \text{ if } x \notin A\\ 1 & \text{ if } x \in A. \end{array} \right. \end{aligned}\]Its expectation is the probability of the event:
\[\mathbb{E}[1_A(X)] = P(X \in A).\]
Our goal is to represent data and hidden causes with a joint distribution.
\[p(x_O, x_H) = p(x_O \mid x_H)\, p(x_H).\]
In Week 1’s letters: \(x_O = (x, y)\), the inputs and outputs, and \(x_H = (z, \theta)\), the latent variables and the parameters. The subscripts let one formula cover a single parameter, a latent label per observation, or the hidden nodes of a graph. We use whichever is clearer in the moment.
Both start from a probability model for the data, \(p(y \mid \theta)\). They differ in what the probability statement is about.
For the coin: the frequentist reports \(\hat\theta = 0.65\) with standard error \(\sqrt{\hat\theta(1-\hat\theta)/N} \approx 0.05\), which describes how \(\hat\theta\) would scatter across other sets of \(100\) flips. The Bayesian reports \(\text{Beta}(66, 36)\), which describes where \(\theta\) is given these \(100\) flips.
The Bayesian answer is the more direct one: it is about the quantity we asked about.
Prior specifies initial belief on the possible values of the hidden variables, expressed via a distribution.
The posterior serves to update the belief after having seen the data.
In the coin flip that follows, \(x_H = \theta\) and \(x_O = y_{1:N}\). In Part II, \(x_H\) is the set of hidden nodes of a graph. The formula does not change.
Let \(Y_n=1\) denote heads and \(Y_n=0\) tails. Conditional on the probability of heads \(\theta\),
\[ Y_n\mid\theta \sim \operatorname{Bernoulli}(\theta). \]
Because \(\theta\in[0,1]\), we use a Beta prior:
\[ \theta\sim\operatorname{Beta}(a,b). \]
Choosing \(a=b=1\) gives a uniform prior over \(\theta\). A fair coin is the particular value \(\theta=0.5\); the uniform prior does not assume the coin is fair.


N = 100, heads = 65, tails = 35

Compute the posterior:
\[ p(\theta \mid y_{1:N}) = \frac{\left[\prod_{n=1}^{N}p(y_n\mid\theta)\right]p(\theta)} {p(y_{1:N})}. \]
How?
Let \(s=\sum_{n=1}^{N}y_n\) be the number of heads. The numerator is
\[ \begin{aligned} p(y_{1:N}\mid\theta)p(\theta) &= \left[\prod_{n=1}^{N}p(y_n\mid\theta)\right]p(\theta) \\ &= \theta^{s}(1-\theta)^{N-s} \frac{\theta^{a-1}(1-\theta)^{b-1}}{B(a,b)} \\ &= \frac{1}{B(a,b)} \theta^{a+s-1}(1-\theta)^{b+N-s-1}. \end{aligned} \]
This is the kernel of a Beta distribution with parameters \(\tilde a=a+s\) and \(\tilde b=b+N-s\).
Denominator (marginal likelihood):
\[ \begin{aligned} p(y_{1:N}) &= \int_0^1 p(y_{1:N}\mid\theta)p(\theta)\,d\theta \\ &= \frac{1}{B(a,b)} \int_0^1 \theta^{a+s-1}(1-\theta)^{b+N-s-1}\,d\theta. \end{aligned} \]
So the posterior is,
\[ \begin{aligned} p(\theta\mid y_{1:N}) &= \frac{\theta^{a+s-1}(1-\theta)^{b+N-s-1}} {\int_0^1 \theta^{a+s-1}(1-\theta)^{b+N-s-1}\,d\theta}. \end{aligned} \]
\(B(a, b)\) cancels.
The denominator is a constant with respect to \(\theta\), but what is it?
Beta function:
\[ B(a, b) = \int_{0}^{1} \theta^{a-1} (1 - \theta)^{b-1} d\theta. \]
The denominator:
\[ \int_0^1 \theta^{a+s-1}(1-\theta)^{b+N-s-1}\,d\theta, \]
so the denominator is \(B(a+s,b+N-s)\). Therefore,
\[ \boxed{\theta\mid y_{1:N}\sim\operatorname{Beta}(a+s,b+N-s)} \]
and
\[ p(y_{1:N})=\frac{B(a+s,b+N-s)}{B(a,b)}. \]
Note
This is the probability of the observed sequence \(y_{1:N}\). If the observation were only the count \(S=s\), its probability would also include the binomial coefficient \(\binom Ns\).
Posterior parameters: alpha = 66, beta = 36

Week 1 separated inference from prediction. The posterior answers the inference question; averaging over it answers the predictive question:
\[ \begin{aligned} P(Y_{\mathrm{new}}=1\mid y_{1:N}) &= \int_0^1 P(Y_{\mathrm{new}}=1\mid\theta) p(\theta\mid y_{1:N})\,d\theta \\ &= \mathbb E[\theta\mid y_{1:N}] = \frac{a+s}{a+b+N}. \end{aligned} \]
For the seeded data, this is \(66/102\approx0.647\). It predicts the next observation while accounting for uncertainty in \(\theta\), rather than simply plugging in an assumed value.
Posterior parameters: alpha = 784, beta = 318

Is it true that the prior and posterior are always in the same family?
What do we do if the closed form for marginal is not available?
For the coin, uncertainty about \(\theta\) is epistemic; variation in the next flip conditional on \(\theta\) is aleatoric. Both are probabilities conditional on information, so this distinction does not require claiming that randomness is an intrinsic property of the coin.
With \(N=10{,}000\), you chose door \(i=1\) and the host left door \(j=387\) closed. Let \(H_n\) mean that the car is behind door \(n\), and let \(Y\) be the other door left closed.
\[ P(H_n\mid Y=j) = \frac{P(Y=j\mid H_n)P(H_n)}{P(Y=j)}, \qquad P(H_n)=\frac1N. \]
The host knows where the car is. If it is behind your door, the host chooses uniformly which other door to leave closed; if it is behind another door, the host must leave that door closed.
What are the three possible values of \(P(Y=j\mid H_n)\)?
Therefore, the posterior is,
\[ \begin{aligned} P(H_i | Y = j) &\propto \frac{1}{N-1} \frac{1}{N} \\ P(H_j | Y = j) &\propto \frac{1}{N} \\ P(H_n | Y = j) &= 0 \text{ for } n \notin \{i, j\}. \end{aligned} \]
The denominator sums over all \(N\) hypotheses, but only two contribute:
\[ P(Y = j) = \sum_{n=1}^{N} P(Y = j \mid H_n) \, P(H_n) = \underbrace{\frac{1}{N-1} \cdot \frac{1}{N}}_{n \, = \, i} \; + \; \underbrace{1 \cdot \frac{1}{N}}_{n \, = \, j} = \frac{1}{N}\left(\frac{1}{N-1} + 1\right) = \frac{1}{N-1}. \]
Therefore,
\[ \begin{aligned} P(H_i | Y = j) &= \frac{1}{N} = 1/10{,}000 \\ P(H_j | Y = j) &= \frac{N-1}{N} = 9{,}999/10{,}000 \\ P(H_n | Y = j) &= 0 \text{ for } n \notin \{i, j\}. \end{aligned} \]

The host’s 9,998 choices were not free. Almost all of that constrained information flows into one door.
Part I’s examples had one hidden variable. Part II moves to a graph of them, and two ideas carry the day. Read ahead in Murphy:
Your grass is wet this morning. Did it rain?

Read the picture as four local statements, one per node:
The arrows carry no numbers yet. The graph alone is a set of independence claims; the numbers come next.
Each node gets a table: a prior for the root, a conditional probability table (CPT) for everything else.
| \(P(C{=}1)\) |
|---|
| 0.5 |
Cloudy is a coin flip. The root tilts nothing, and the marginal probability of rain also comes out to \(0.5\).
| \(C\) | \(P(S{=}1 \mid C)\) | \(P(R{=}1 \mid C)\) |
|---|---|---|
| 0 | 0.5 | 0.2 |
| 1 | 0.1 | 0.8 |
Cloud makes rain four times more likely and the sprinkler five times less likely. Through \(C\), sprinkler and rain are negatively associated.
| \(S\) | \(R\) | \(P(W{=}1 \mid S,R)\) |
|---|---|---|
| 0 | 0 | 0.00 |
| 0 | 1 | 0.90 |
| 1 | 0 | 0.90 |
| 1 | 1 | 0.99 |
A noisy-OR: each cause wets the grass independently with probability \(0.9\), so both give \(1 - 0.1^2\). Grass never gets wet on its own, so \(W = 1\) means at least one cause fired.
Each variable depends only on its parents:
\[ P(C, S, R, W) = \underbrace{P(C)}_{1} \; \underbrace{P(S \mid C)}_{2} \; \underbrace{P(R \mid C)}_{2} \; \underbrace{P(W \mid S, R)}_{4} \]
Four binary variables, so sixteen rows.
C S R W P
0 0 0 0 0.20000
0 0 0 1 0.00000
0 0 1 0 0.00500
0 0 1 1 0.04500
0 1 0 0 0.02000
0 1 0 1 0.18000
0 1 1 0 0.00050
0 1 1 1 0.04950
1 0 0 0 0.09000
1 0 0 1 0.00000
1 0 1 0 0.03600
1 0 1 1 0.32400
1 1 0 0 0.00100
1 1 0 1 0.00900
1 1 1 0 0.00040
1 1 1 1 0.03960
sums to 1.000000
P(R=1) = 0.5000 <- prior
P(W=1) = 0.6471
P(R=1 | W=1) = 0.7079 <- the grass is wet
Wet grass raises our belief in rain from \(0.50\) to \(0.71\). That is Bayes’ theorem, applied to a table.
P(R=1) = 0.5000
P(R=1 | W=1) = 0.7079
P(R=1 | W=1, S=1) = 0.3204 <- sprinkler was on
Belief in rain climbs to \(0.71\), then collapses to \(0.32\) – below the prior – once we learn the sprinkler was on. Two things pushed it down: the sprinkler already accounts for the wet grass, and a running sprinkler is itself evidence of a clear sky.

The dashed line is the prior. Evidence can move a belief below where it started.
common cause -- C makes S and R dependent:
P(S=1) = 0.3000
P(S=1 | R=1) = 0.1800 <- differs, so dependent
P(S=1 | C=1) = 0.1000
P(S=1 | R=1, C=1) = 0.1000 <- equal, so independent given C
common effect -- W makes S and R dependent again:
P(R=1 | C=1) = 0.8000
P(R=1 | C=1, S=1) = 0.8000 <- still equal
P(R=1 | C=1, W=1) = 0.9758
P(R=1 | C=1, W=1, S=1) = 0.8148 <- now differs

Important
Conditioning does not simply “add information” – depending on where a variable sits in the graph, observing it can either break a dependence or manufacture one. The general rule is d-separation, and it lets us read independence off a picture instead of grinding through the algebra. The next few slides make that rule mechanical.

Every path through a directed graph is built from these three. The chain and the common cause behave alike: the middle node carries information until it is observed. The common effect is the mirror image. Two other names you will meet: a common-cause node \(X \leftarrow Z \rightarrow Y\) is a fork, and a common-effect node \(X \rightarrow Z \leftarrow Y\) is a collider, because two arrowheads meet at it. We use the names interchangeably.
Definition
\(X \perp Y \mid Z\) holds in the graph if every path between \(X\) and \(Y\) is blocked given \(Z\). A path is any sequence of edges joining \(X\) and \(Y\), arrow directions ignored; it is blocked given \(Z\) when it passes through
d-separation is a statement about the arrows alone. It never looks at the numbers.
Checking every path by hand is error-prone. Bayes ball turns the definition into a mechanical procedure.

Shade the nodes in \(Z\). Drop a ball at \(X\) and let it travel by these four rules, applied at each node \(v\) it arrives at. If the ball never reaches \(Y\), then \(X \perp Y \mid Z\).
Add one node. \(G\): the path is slippery, a child of \(W\) only.

Run the ball in your head. Which of these hold?
parents2 = {**parents, "G": ["W"]}
cpt2 = {**cpt, "G": {(0,): 0.05, (1,): 0.7}}
tbl2 = make_joint(parents2, cpt2)
print(f"{'claim':<16}{'Bayes ball':<14}{'joint table'}")
for X, Y, g in [("S", "R", ("G",)), ("C", "G", ("W",)), ("S", "R", ("C", "G"))]:
print(f"{fmt(X, Y, g):<16}{str(d_separated(parents2, X, Y, g)):<14}{independent_in_table(tbl2, X, Y, g)}")claim Bayes ball joint table
S ⊥ R | G False False
C ⊥ G | W True True
S ⊥ R | C, G False False
A collider is a dead end for a ball coming from above, unless something below it is observed, in which case the ball can turn around and come back up.
For now, note what we did: a prior, a likelihood, and a renormalization. Everything else is scale.
Next week, the unobserved quantity is a mixture label \(z_i\). Its posterior probability becomes an EM responsibility—the same belief update, repeated for every observation.
Next week the unknown is a cluster label \(z_n\) per observation, and the posterior over it is computed by the same update as the coin. Read ahead in Murphy:
As you read, watch for the responsibility \(r_{nk} = p(z_n = k \mid y_n, \theta)\). It is a posterior, computed by Bayes’ theorem exactly as today, and every step of EM is built from it.
Bayesian inference means computing the posterior \(p(x_H \mid x_O)\). Why is this difficult?
\[\begin{aligned} p(x_H \mid x_O) = \frac{p(x_O \mid x_H)\, p(x_H)}{p(x_O)}, \qquad p(x_O) = \int p(x_O \mid x_H)\, p(x_H)\, dx_H. \end{aligned}\]Conjugacy makes normalization and posterior expectations analytically tractable. It is a property of a prior–likelihood pairing, not of either distribution alone.
For a regular exponential-family likelihood,
\[ p(y \mid \eta) = h(y)\exp\left(\eta^\top T(y) - A(\eta)\right). \]
Next: the definition, the Bernoulli written in this form, and the general recipe that turns any such likelihood into a conjugate update. The Beta–Bernoulli from Part I falls out as a special case.
A family of distributions is an exponential family when it can be written
\[ p(y \mid \eta) = h(y)\exp\!\left(\eta^\top T(y)-A(\eta)\right). \]
\(Y \sim \text{Bernoulli}(\theta)\):
\[ \begin{aligned} p(y | \theta) &= \theta^{y} (1 - \theta)^{1-y} \\ &= \exp(y \log(\theta) + (1 - y) \log(1 - \theta)) \\ &= \exp(T(y)^T \eta), \end{aligned} \]
where
\[ \begin{aligned} T(y) &= (1[y = 1], 1[y = 0]) \\ \eta &= (\log(\theta), \log(1 - \theta)). \end{aligned} \]
An exponential-family representation is minimal when there is no nonzero vector \(c\) for which \(c^\top T(y)\) is constant for every \(y\).
Here,
\[ (1,1)^\top T(y)=1[y=1]+1[y=0]=1, \]
so one statistic is redundant. Removing the redundancy gives a single sufficient statistic and a single unconstrained natural parameter.
Minimal representation for the Bernoulli:
\[ \begin{aligned} p(y | \theta) &= \exp\left[y \log\left(\frac{\theta}{1 - \theta}\right) + \log(1 - \theta)\right] \\ &= \exp\left(y \eta - \log(1+e^{\eta})\right), \end{aligned} \]
using \(1 - \theta = 1/(1 + e^{\eta})\), so that \(\log(1-\theta) = -\log(1 + e^{\eta})\).
For \(N\) independent draws from an exponential family, the sufficient statistics add:
\[ \prod_{n=1}^N p(y_n \mid \eta) = \Big[\prod_n h(y_n)\Big] \exp\!\Big(\eta^\top \underbrace{\textstyle\sum_n T(y_n)}_{\text{all the data enters here}} - N A(\eta)\Big). \]
Choose a prior with the same shape in \(\eta\), with two hyperparameters \(\tau\) and \(\nu\):
\[ p(\eta \mid \tau, \nu) \propto \exp\!\big(\eta^\top \tau - \nu A(\eta)\big). \]
Multiply, and the posterior has the same shape with \[ \tau \;\to\; \tau + \sum_n T(y_n), \qquad \nu \;\to\; \nu + N. \]
Read \(\tau\) as sufficient statistics from \(\nu\) imaginary prior observations. Inference is bookkeeping: add the data’s statistics, add the sample size.
Minimal Bernoulli: \(\eta = \log\frac{\theta}{1-\theta}\), \(T(y) = y\), \(A(\eta) = \log(1 + e^\eta)\).
The recipe’s prior, written back in terms of \(\theta\) (with the change of variables \(d\eta = d\theta / (\theta(1-\theta))\)):
\[ \exp(\eta\tau - \nu\log(1+e^\eta)) = \frac{(e^\eta)^\tau}{(1+e^\eta)^\nu} = \theta^{\tau}(1-\theta)^{\nu - \tau} \;\;\Longrightarrow\;\; p(\theta) \propto \theta^{\tau - 1}(1-\theta)^{\nu-\tau-1}. \]
That is \(\text{Beta}(a, b)\) with \(a = \tau\) and \(b = \nu - \tau\).
The update \(\tau \to \tau + s\), \(\nu \to \nu + N\) becomes \((a, b) \to (a + s,\; b + N - s)\): exactly the derivation from Part I, with no Beta-function integral in sight. The prior’s \(a + b = \nu\) is its weight in imaginary coin flips.
Number of events per unit, \(y_n \in \{0, 1, 2, \ldots\}\) (reads per gene, admissions per day, mutations per sample):
\[ y_n \mid \lambda \sim \text{Poisson}(\lambda), \qquad p(y \mid \lambda) = \frac{\lambda^y e^{-\lambda}}{y!} = \frac{1}{y!}\exp\!\big(y \log\lambda - \lambda\big). \]
So \(\eta = \log\lambda\), \(T(y) = y\), \(A(\eta) = e^\eta = \lambda\). The recipe’s prior in terms of \(\lambda\) (Jacobian \(d\eta = d\lambda/\lambda\)):
\[ p(\lambda) \propto \lambda^{\tau - 1} e^{-\nu\lambda} \qquad\text{i.e. } \lambda \sim \text{Gamma}(\text{shape } a = \tau,\; \text{rate } b = \nu). \]
Posterior: \(\lambda \mid y_{1:N} \sim \text{Gamma}\big(a + \sum_n y_n,\; b + N\big)\). The prior is worth \(b\) imaginary units of observation carrying \(a\) events in total; the posterior mean \(\frac{a + \sum y}{b + N}\) is a weighted average of the prior mean \(a/b\) and the sample mean.

Same picture as the coin: a broad prior, a data-dominated posterior, and the update was two additions. Nothing was integrated. The normalizing constant is the Gamma function, and we never had to look at it.
Discrete
Continuous
Multivariate
Nearly every row of the table of conjugate pairs is one instance of the recipe on the previous slides; the few exceptions, such as uniform–Pareto, are conjugate without being exponential families. Not in the family: Student-\(t\), mixtures, and anything whose support depends on the parameter, such as the uniform on \([0,\theta]\).