Guide To AI Logo
Unit 16

Probabilistic Models for Machine Learning

modeling data distributions, inference techniques, and generative learning frameworks

Core Concepts Covered

  • MLE, MAP, conjugate Beta priors, and posterior summaries
  • Bayesian networks, conditional inference, latent variables, KL divergence, and the ELBO
  • GMM soft assignments, EM parameter updates, covariance structures, and BIC

1. MLE, MAP, and Prior Information

A probabilistic model assigns a likelihood p(Dθ)p(D\mid\theta) to observed data DD under parameters θ\theta. For independent observations, the likelihood is a product, and the log-likelihood turns that product into a sum: (θ)=logp(Dθ)=i=1nlogp(xiθ).\ell(\theta)=\log p(D\mid\theta)=\sum_{i=1}^{n}\log p(x_i\mid\theta).

Maximum Likelihood Estimation (MLE) chooses the parameter that best explains the observed data: θ^MLE=argmaxθp(Dθ).\hat\theta_{\mathrm{MLE}}=\arg\max_{\theta}p(D\mid\theta). For hh heads and tt tails from a Bernoulli coin, p(Dθ)=θh(1θ)tp(D\mid\theta)=\theta^h(1-\theta)^t and θ^MLE=h/(h+t)\hat\theta_{\mathrm{MLE}}=h/(h+t).

Maximum A Posteriori (MAP) combines the likelihood with a prior p(θ)p(\theta). Bayes' rule gives p(θD)=p(Dθ)p(θ)p(D),p(\theta\mid D)=\frac{p(D\mid\theta)p(\theta)}{p(D)}, and the evidence p(D)p(D) can be ignored when optimizing with respect to θ\theta: θ^MAP=argmaxθ[logp(Dθ)+logp(θ)].\hat\theta_{\mathrm{MAP}}=\arg\max_{\theta}\left[\log p(D\mid\theta)+\log p(\theta)\right].

A Beta prior is conjugate to a Bernoulli likelihood. If θBeta(α,β)\theta\sim\operatorname{Beta}(\alpha,\beta), then observing hh heads and tt tails gives θDBeta(α+h,β+t).\theta\mid D\sim\operatorname{Beta}(\alpha+h,\beta+t). The posterior mean is α/(α+β)\alpha'/(\alpha'+\beta'), while the interior posterior mode is (α1)/(α+β2)(\alpha'-1)/(\alpha'+\beta'-2).

A Gaussian prior centered at zero adds a quadratic penalty to the negative log-posterior. This is the probabilistic reason MAP estimation often looks like L2L_2 regularization: the data term pulls toward a good fit, while the prior shrinks implausibly large parameters.

Worked Example 1

Update a Coin Probability with a Beta Prior

Problem

A coin produces 88 heads and 22 tails. Compare MLE with a Beta(2,2)\operatorname{Beta}(2,2) prior's posterior mean and MAP estimate.

Step-by-step solution

1.MLE uses only the observations: θ^MLE=8/(8+2)=0.8\hat\theta_{\mathrm{MLE}}=8/(8+2)=0.8.

2.The posterior is Beta(2+8,2+2)=Beta(10,4)\operatorname{Beta}(2+8,2+2)=\operatorname{Beta}(10,4).

Final answer and interpretation

Its mean is 10/(10+4)0.71410/(10+4)\approx0.714, and its MAP estimate is (101)/(10+42)=9/12=0.75(10-1)/(10+4-2)=9/12=0.75.

The prior pulls both posterior summaries away from the extreme data-only estimate, while the influence of that prior weakens as more observations arrive.

Worked Example 2

See Gaussian-Prior Shrinkage Directly

Problem

Without a prior, minimize 12(θ3)2\frac{1}{2}(\theta-3)^2. Then add the Gaussian-prior penalty 12θ2\frac{1}{2}\theta^2 and find the MAP estimate.

Step-by-step solution

1.The data-only derivative is θ3\theta-3, so the MLE is θ=3\theta=3.

2.The MAP objective is 12(θ3)2+12θ2\frac{1}{2}(\theta-3)^2+\frac{1}{2}\theta^2.

Final answer and interpretation

Its derivative is (θ3)+θ=2θ3(\theta-3)+\theta=2\theta-3. Setting it to zero gives θ^MAP=1.5\hat\theta_{\mathrm{MAP}}=1.5.

The prior contributes a real term to the objective, so MAP trades some data fit for a parameter value closer to the prior center.

2. Bayesian Networks and Conditional Inference

A Bayesian Network is a directed acyclic graph whose nodes are random variables and whose arrows encode direct conditional dependence. The graph factorizes a large joint distribution into smaller local pieces: p(x1,,xn)=i=1np ⁣(xiParents(xi)).p(x_1,\ldots,x_n)=\prod_{i=1}^{n}p\!\left(x_i\mid\operatorname{Parents}(x_i)\right).

The missing arrows carry information too. Once a node's parents are known, the node is conditionally independent of its non-descendants. These independencies reduce how many probabilities the model must store and which terms an inference calculation must inspect.

Inference reverses or fills in parts of the graph. For a hypothesis HH and evidence EE, normalize the joint probability over all competing hypotheses: p(HE)=p(EH)p(H)hp(Eh)p(h).p(H\mid E)=\frac{p(E\mid H)p(H)}{\sum_h p(E\mid h)p(h)}.

Worked Example 1

Interpret a Positive Diagnostic Result

Problem

A disease affects 1%1\% of a population. A test has 95%95\% sensitivity and a 5%5\% false-positive rate. What is P(D+)P(D\mid +)?

Step-by-step solution

1.The true-positive joint probability is P(+D)=0.95(0.01)=0.0095P(+\cap D)=0.95(0.01)=0.0095.

2.The false-positive joint probability is P(+¬D)=0.05(0.99)=0.0495P(+\cap\neg D)=0.05(0.99)=0.0495, so P(+)=0.0095+0.0495=0.059P(+)=0.0095+0.0495=0.059.

Final answer and interpretation

P(D+)=0.0095/0.0590.161P(D\mid +)=0.0095/0.059\approx0.161, or about 16.1%16.1\%.

A positive result raises the probability sharply, but the low prior prevalence means false positives still outnumber true positives.

3. Latent Variables and Variational Inference

A latent variable zz is not observed directly but helps explain an observed variable xx. To obtain the probability of xx, sum over a discrete latent state or integrate over a continuous one: p(x)=zp(x,z)orp(x)=p(x,z)dz.p(x)=\sum_z p(x,z)\qquad\text{or}\qquad p(x)=\int p(x,z)\,dz.

Exact Bayesian inference asks for p(zx)=p(x,z)/p(x)p(z\mid x)=p(x,z)/p(x). The denominator can require an enormous sum or integral, so Variational Inference (VI) chooses a tractable approximation qϕ(zx)q_\phi(z\mid x) and fits it to the true posterior.

Unit 12 introduced KL divergence as a measure of how one distribution differs from another. Here it becomes an optimization target: DKL ⁣(qϕ(zx)p(zx))=Eqϕ ⁣[logqϕ(zx)logp(zx)].D_{\mathrm{KL}}\!\left(q_\phi(z\mid x)\parallel p(z\mid x)\right)=\mathbb{E}_{q_\phi}\!\left[\log q_\phi(z\mid x)-\log p(z\mid x)\right].

Because the true posterior contains the difficult evidence p(x)p(x), VI maximizes the Evidence Lower Bound (ELBO) instead: L(q)=Eq(z)[logp(x,z)]Eq(z)[logq(z)].\mathcal{L}(q)=\mathbb{E}_{q(z)}[\log p(x,z)]-\mathbb{E}_{q(z)}[\log q(z)]. The identity logp(x)=L(q)+DKL(q(z)p(zx))\log p(x)=\mathcal{L}(q)+D_{\mathrm{KL}}(q(z)\parallel p(z\mid x)) shows that maximizing the ELBO closes the non-negative KL gap.

Unit 21 applies this machinery to Variational Autoencoders, where an encoder supplies qϕ(zx)q_\phi(z\mid x) and a decoder supplies pθ(xz)p_\theta(x\mid z). This unit focuses on the inference calculation underneath that architecture.

Worked Example 1

Marginalize a Two-State Latent Variable

Problem

For one observation xx, suppose p(x,z1)=0.12p(x,z_1)=0.12 and p(x,z2)=0.08p(x,z_2)=0.08. Find p(x)p(x) and the posterior probabilities of the two latent states.

Step-by-step solution

1.Marginalize the hidden state: p(x)=0.12+0.08=0.20p(x)=0.12+0.08=0.20.

2.p(z1x)=0.12/0.20=0.60p(z_1\mid x)=0.12/0.20=0.60.

Final answer and interpretation

p(z2x)=0.08/0.20=0.40p(z_2\mid x)=0.08/0.20=0.40.

Posterior probabilities are normalized joint probabilities, and marginalization supplies the normalizing evidence.

Worked Example 2

Calculate an ELBO and Its KL Gap

Problem

Suppose Eq[logp(x,z)]=1.4\mathbb{E}_q[\log p(x,z)]=-1.4, Eq[logq(z)]=0.5\mathbb{E}_q[\log q(z)]=-0.5, and logp(x)=0.7\log p(x)=-0.7. Find the ELBO and DKL(qp)D_{\mathrm{KL}}(q\parallel p).

Step-by-step solution

1.L(q)=1.4(0.5)=0.9\mathcal{L}(q)=-1.4-(-0.5)=-0.9.

2.Using logp(x)=L(q)+DKL\log p(x)=\mathcal{L}(q)+D_{\mathrm{KL}}, the gap is 0.7(0.9)=0.2-0.7-(-0.9)=0.2.

Final answer and interpretation

Thus DKL(qp)=0.2D_{\mathrm{KL}}(q\parallel p)=0.2, and the ELBO sits 0.20.2 below the log evidence.

A tighter approximation has a smaller KL gap and an ELBO closer to logp(x)\log p(x).

4. Gaussian Mixture Models and Expectation-Maximization

Unit 14 introduced Gaussian Mixture Models (GMMs) as an unsupervised-learning method. K-Means gives each point one hard cluster label. A GMM instead models a probability density and assigns soft membership across KK Gaussian components: p(xi)=k=1KπkN(xiμk,Σk),k=1Kπk=1.p(x_i)=\sum_{k=1}^{K}\pi_k\mathcal{N}(x_i\mid\mu_k,\Sigma_k),\qquad \sum_{k=1}^{K}\pi_k=1.

The unobserved component label ziz_i is a latent variable. Given current parameters, the E-step computes its posterior probability, called a responsibility: γik=p(zi=kxi)=πkN(xiμk,Σk)j=1KπjN(xiμj,Σj).\gamma_{ik}=p(z_i=k\mid x_i)=\frac{\pi_k\mathcal{N}(x_i\mid\mu_k,\Sigma_k)}{\sum_{j=1}^{K}\pi_j\mathcal{N}(x_i\mid\mu_j,\Sigma_j)}.

The M-step treats responsibilities as fractional counts. With Nk=iγikN_k=\sum_i\gamma_{ik}, the updates are πknew=Nkn,μknew=1Nkiγikxi,\pi_k^{\mathrm{new}}=\frac{N_k}{n},\qquad \mu_k^{\mathrm{new}}=\frac{1}{N_k}\sum_i\gamma_{ik}x_i, Σknew=1Nkiγik(xiμknew)(xiμknew)T.\Sigma_k^{\mathrm{new}}=\frac{1}{N_k}\sum_i\gamma_{ik}(x_i-\mu_k^{\mathrm{new}})(x_i-\mu_k^{\mathrm{new}})^T.

The covariance structure controls each component's geometry. full learns a complete covariance matrix per component, tied shares one complete matrix, diag learns one variance per feature without correlations, and spherical learns one variance per component. Flexible choices need more data and can produce nearly singular covariance matrices, so use regularization and inspect component counts.

Each EM iteration does not decrease the observed-data log-likelihood (θ)=i=1nlog[k=1KπkN(xiμk,Σk)],\ell(\theta)=\sum_{i=1}^{n}\log\left[\sum_{k=1}^{K}\pi_k\mathcal{N}(x_i\mid\mu_k,\Sigma_k)\right], but EM can settle at a local optimum. Multiple initializations, covariance regularization, and checks for nearly empty components reduce common failures.

More components almost always improve training likelihood, so model selection must penalize complexity. The Bayesian Information Criterion is BIC=plogn2(θ^),\operatorname{BIC}=p\log n-2\ell(\hat\theta), where pp is the number of fitted parameters. Lower BIC is preferred among models fitted to the same dataset.

Worked Example 1

Compute a GMM E-Step Responsibility

Problem

At x=1x=1, component 1 has π1=0.6\pi_1=0.6, μ1=0\mu_1=0, σ1=1\sigma_1=1, and density 0.24200.2420. Component 2 has π2=0.4\pi_2=0.4, μ2=3\mu_2=3, σ2=1\sigma_2=1, and density 0.05400.0540. Find both responsibilities.

Step-by-step solution

1.The weighted densities are 0.6(0.2420)=0.14520.6(0.2420)=0.1452 and 0.4(0.0540)=0.02160.4(0.0540)=0.0216.

2.Their sum is 0.16680.1668, so γ1=0.1452/0.16680.871\gamma_1=0.1452/0.1668\approx0.871.

Final answer and interpretation

γ2=0.0216/0.16680.129\gamma_2=0.0216/0.1668\approx0.129, and the responsibilities sum to 11.

A responsibility combines component fit with the component's prior mixture weight.

Worked Example 2

Carry Out a One-Component M-Step

Problem

For observations x=(0,2,5)x=(0,2,5), component 1 has responsibilities γ1=(0.9,0.6,0.1)\gamma_1=(0.9,0.6,0.1). Update its effective count, mixture weight, mean, and one-dimensional variance.

Step-by-step solution

1.N1=0.9+0.6+0.1=1.6N_1=0.9+0.6+0.1=1.6, so with n=3n=3, π1new=1.6/30.533\pi_1^{\mathrm{new}}=1.6/3\approx0.533.

2.μ1new=[0.9(0)+0.6(2)+0.1(5)]/1.6=1.7/1.6=1.0625\mu_1^{\mathrm{new}}=[0.9(0)+0.6(2)+0.1(5)]/1.6=1.7/1.6=1.0625.

Final answer and interpretation

σ12new=[0.9(01.0625)2+0.6(21.0625)2+0.1(51.0625)2]/1.61.934\sigma_1^{2\,\mathrm{new}}=[0.9(0-1.0625)^2+0.6(2-1.0625)^2+0.1(5-1.0625)^2]/1.6\approx1.934.

Responsibilities act like fractional observations when EM recomputes each component.

Worked Example 3

Choose Between Two GMMs with BIC

Problem

Two models fit n=100n=100 observations. Model A has p=5p=5 parameters and =120\ell=-120; Model B has p=8p=8 and =114\ell=-114. Calculate both BIC values.

Step-by-step solution

1.BICA=5log(100)2(120)5(4.605)+240=263.03\operatorname{BIC}_A=5\log(100)-2(-120)\approx5(4.605)+240=263.03.

2.BICB=8log(100)2(114)8(4.605)+228=264.84\operatorname{BIC}_B=8\log(100)-2(-114)\approx8(4.605)+228=264.84.

Final answer and interpretation

Model A is preferred because 263.03<264.84263.03<264.84, even though Model B has the better raw likelihood.

BIC accepts extra parameters only when their likelihood improvement is large enough to offset the complexity penalty.

Interactive Practice Quiz

Test your understanding with instant feedback

QUESTION 01

What is the primary difference between Maximum Likelihood Estimation (MLE) and Maximum A Posteriori (MAP)?

QUESTION 02

In a Bayesian Network, how do we factorize the joint probability of variables P(x1,x2,...,xn)P(x_1, x_2, ..., x_n) compactly?

QUESTION 03

What is a 'Latent Variable' in probabilistic modeling?

QUESTION 04

Which of the following describes the objective of the 'Expectation' (E-step) inside the EM algorithm?

QUESTION 05

What is the role of the 'Maximization' (M-step) inside the EM algorithm?

QUESTION 06

After observing 88 heads and 22 tails with a Beta(2,2)\operatorname{Beta}(2,2) prior, what is the posterior distribution?

QUESTION 07

A disease has prevalence 0.010.01. A test has sensitivity 0.950.95 and false-positive rate 0.050.05. Approximately what is P(D+)P(D\mid +)?

QUESTION 08

If logp(x)=0.7\log p(x)=-0.7 and the ELBO is 0.9-0.9, what is DKL(q(z)p(zx))D_{\mathrm{KL}}(q(z)\parallel p(z\mid x))?

QUESTION 09

For one GMM observation, the two latent component states have joint probabilities p(x,z1)=0.06p(x,z_1)=0.06 and p(x,z2)=0.02p(x,z_2)=0.02. After marginalizing zz, what are their responsibilities?

QUESTION 10

Model A has BIC=263.03\operatorname{BIC}=263.03 and Model B has BIC=264.84\operatorname{BIC}=264.84. Which model does BIC select?