8 Estimation II

8.1 Multimodality

To build generative AI, we rely on Maximum Likelihood Estimation (MLE). However, MLE has a prerequisite: you must first define the mathematical shape of the probability distribution before you can find its parameters.
The problem is that real-world data is very complex to model. For example, in image generation there are many ways to arrange the pixels, and in text generation, there isn’t just one correct next word.
There are multiple, highly probable correct answers. In statistics, this means the data has multiple peaks (modes). Standard statistical models (like Gaussian distribution) are too simple for real-world data (one have one peak).
Hence, we need a way to represent complex, multimodal datasets.

Simple vs Complex Data Distributions

8.2 Latent Variable Models

8.2.1 Definition and Efficient Representation

Because real-world data cannot be effectively modelled with simple probability distributions, we use latent variables. We assume that the observable data is generated by a smaller set of underlying, unobservable factors.
Notations:

  1. Observation, xx: The raw, complex data (like pixels of an image or words in a sentence).

  2. Latent variable, zz: hidden, unobserved factor that acts as an essential, compressed representation of the observation xx.

  3. Joint distribution, p(x,z)=p(x|z)p(z)p(x,z) = p(x|z)p(z).

    • The prior p(z)p(z): he distribution of the latent factors themselves. Before generating an image, the model randomly selects a set of traits from this distribution. For example, it might sample a zz-vector that mathematically translates to "highly tilted" and "very thick".

    • Conditional distribution p(x|z)p(x|z): Once the hidden traits (zz) are chosen, this distribution determines the actual probability of the raw pixels (xx) given those specific traits.

    • Data generation: To generate a new data point xx, the model performs a two-step sampling process: first, sample z∼p(z)z \sim p(z), and then sample the image x∼p(x|z)x \sim p(x|z).

For example, each image (one digit) in the MNIST database is 28×2828\times 28 pixels. The raw observation xx exists in 784-dimensional space, which is computationally massive and inefficient.
Out of all combinations of 784 pixels, actual handwritten digits occupy a very small fraction. The latent variable zz captures just the necessary semantic features needed to draw the digit in a much lower dimension.
Some zz factors might be "which digit is it?"; "How slanted is it?"; "How thick is the stroke?".
Arithmetic operations in the latent space can admit meaningful semantic interpretations, in contrast to pixel space, where direct image addition lacks semantic meaning.

Isolation of specific semantic features into individual dimensions of the latent variable.
Points in latent space mapped to high-dimensional data space, via x∼p(x|z)x \sim p(x|z)

Here, p(x|z)p(x|z) serves as the decoder of the encoder-decoder model. We feed a low-dimensional vector of numbers (zz), and the network outputs a high-dimensional object (xx). In contrast, the encoder would be represented by the posterior distribution p(z|x)p(z|x).

8.2.2 The Manifold Hypothesis

A lower-dimensional space manifold within a higher dimensional space

The Manifold Hypothesis says that high-dimensional, real-world data (like images or text) often lies on or near a lower-dimensional, structured manifold embedded within that high-dimensional space.
In the MNIST example, in the 84-dimensional space of a 28x28 pixel image, useful, structured data (like actual digits) tend to cluster along some lower-dimensional manifold within that space. Everywhere else in the higher-dimensional space, it is usually noise.
The implication is that we don’t need to waste computing power trying to learn the rules for the entire 784-dimensional space. Instead, we only need to learn the latent variable coordinates that map onto that manifold.

8.2.3 Gaussian Mixture Models (GMM)

A 1-dimensional 2-Gaussian mixture

A Gaussian Mixture Model (GMM) is a probabilistic model assuming all data points are generated from a mixture of a finite number of Gaussian distributions with unknown parameters. It helps to solve our problem of modelling multiple peaks.
To create a GMM with KK different Gaussian distributions on a dd-dimensional space (ℝd\mathbb{R}^d), the overall PDF is written as: p(x|μ,Σ)=∑t=1Kπtp(x|μt,Σt)p(x|\mu,\Sigma)=\sum_{t=1}^{K}\pi_{t}p(x|\mu_{t},\Sigma_{t}) (dd refers to the shape of the data, in the MNIST example, d=784d=784)
The parameters:

The hyperparameter: The number of mixes, KK. We have to decide how many clusters to look for before training the model.

The GMM is a specific type of latent variable model. We can translate the GMM equation into the p(x,z)p(x, z) framework.
Here, the latent variable, zz, answers the question: "Which specific Gaussian cluster did this data point come from?". Mathematically, z=(ζt)t=1K∈{0,1}K,∑t=1Kζt=1,ζt={1if category t0otherwisez=(\zeta_{t})_{t=1}^{K} \in \{0,1\}^K,\quad \sum_{t=1}^{K}\zeta_{t}=1, \quad \zeta_t = \begin{cases} 1 & \text{if category } t \\ 0 & \text{otherwise} \end{cases} This means zz is a one-hot vector of KK numbers where exactly one number is a ’1’ and the rest are ’0’s. For example, if we have K=3K=3 clusters, and a data point was generated by the second Gaussian, then z=(0,1,0)z = (0, 1, 0).

We can represent the generation process p(x,z)=p(x|z)p(z)p(x, z) = p(x|z)p(z) using this zz:

8.3 Expectation-Maximization (EM) Algorithm

We want to run MLE on the latent variable model to get the best values for the parameters μ\mu (means), Σ\Sigma (covariances) and π\pi (mixing weights), so we have a decoder.

8.3.1 Intractability of Direct MLE

8.3.1.1 Why do we need to use the latent variable zz?

Recall the MLE equation: Total Log-Likelihood=∑i=1nlog⁡pθ(Xi)\text{Total Log-Likelihood} = \sum_{i=1}^{n} \log p_{\theta}(X_i) If we apply MLE on the GMM equation by only looking at xx, we get Total Log-Likelihood=∑i=1nlog⁡(∑t=1Kπtp(x|μt,Σt))\text{Total Log-Likelihood} = \sum_{i=1}^{n} \log \left( \sum_{t=1}^{K} \pi_t p(x|\mu_t, \Sigma_t) \right) Taking the derivative is algebraically intractable. In other words, it is hard to simplify this as the summation is in the log.
What if know zz for every data point?
If we consider the joint probability p(x,z)p(x, z), we have Total Log-Likelihood=∑i=1nlog⁡(p(xi,zi))=∑i=1nlog⁡(∏t=1K[πt⋅p(xi|μt,Σt)]ζit)\begin{align*} \text{Total Log-Likelihood} &= \sum_{i=1}^n \log (p(x_i, z_i)) \\ &= \sum_{i=1}^n \log \left( \prod_{t=1}^K \left[ \pi_t \cdot p(x_i | \mu_t, \Sigma_t) \right]^{\zeta_{it}} \right) \end{align*} This equation is easier to simplify as the product is in the log, so by properties of logarithms, the summation will be outside of the log.

8.3.2 The EM Framework (E-step and M-step)

However, to use p(x,z)p(x, z) (to simplify the MLE equation above), we need to know the value of ζt\zeta_t (and by extension, zz).
The EM algorithm is a way to find a MLE for problems where we have latent variables, i.e. p(x|θ)=∑zp(x,z|θ)p(x|\theta) = \sum_z p(x,z|\theta).
The overarching flow is to iteratively improve our guess of the parameters θ\theta. We start with a completely random guess θ(1)\theta^{(1)}, then use EM algorithm to find a better guess. θ(1)→θ(2)…→θ(k)→θ(k+1)…\theta^{(1)} \rightarrow \theta^{(2)} \dots \rightarrow \theta^{(k)} \rightarrow \theta^{(k+1)} \dots We have i.i.d. observations x1:n=(xi)i=1nx_{1:n} = (x_i)^n_{i=1}, and note that p(z1:n|x1:n,θ)=∏ip(zi|xi,θ)p(z_{1:n}|x_{1:n},\theta) = \prod_i p(z_i|x_i,\theta).
Recall that we wanted to maximise the true log-likelihood of our data: log⁡p(x1:n|θ)=log⁡(∑z1:np(x1:n,z1:n|θ))\log p(x_{1:n}|\theta) = \log \left( \sum_{z_{1:n}} p(x_{1:n}, z_{1:n}|\theta) \right) But the problem is that there is a summation inside the log, making this algebraically intractable.

8.3.2.1 The E-step

We cannot directly optimize the likelihood because we do not know the hidden variables zz. In this step, we use our current, locked-in parameter guesses θ(k)\theta^{(k)} to evaluate the probabilities of the hidden variables. Mathematically, this involves constructing a surrogate lower-bound function (which will become the Q-function) that approximates the true log-likelihood.
Jensen’s Inequality: For random variable XX and concave function ff (∩\cap frown shape function), f(𝔼[X])≥𝔼[f(X)]f(\mathbb{E}[X]) \ge \mathbb{E}[f(X)] log⁡p(x1:n|θ)=log⁡(∑z1:np(z1:n|x1:n,θ(k))p(x1:n,z1:n|θ)p(z1:n|x1:n,θ(k)))[Multiply and divide by posterior]≥∑z1:np(z1:n|x1:n,θ(k))log⁡p(x1:n,z1:n|θ)p(z1:n|x1:n,θ(k))[Jensen’s inequality; f=log⁡, 𝔼=∑]=L(θ,θ(k))[Define a surrogate function]\begin{align*} \log p(x_{1:n}|\theta) &= \log \left( \sum_{z_{1:n}} p(z_{1:n}|x_{1:n}, \theta^{(k)}) \frac{p(x_{1:n}, z_{1:n}|\theta)}{p(z_{1:n}|x_{1:n}, \theta^{(k)})} \right) && \text{[Multiply and divide by posterior]} \\ &\ge \sum_{\textstyle z_{1:n}} p(z_{1:n}|x_{1:n}, \theta^{(k)}) \log \frac{p(x_{1:n}, z_{1:n}|\theta)}{p(z_{1:n}|x_{1:n}, \theta^{(k)})} && \text{[Jensen's inequality; $f$=$\log$, $\mathbb{E}$=$\textstyle\sum$]} \\ &= L(\theta, \theta^{(k)}) && \text{[Define a surrogate function]} \end{align*} Note that we defined a surrogate function L(θ,θ(k))L(\theta, \theta^{(k)}). L(θ,θ(k))=∑z1:np(z1:n|x1:n,θ(k))⏟Part 1log⁡p(x1:n,z1:n|θ)⏞Part 2p(z1:n|x1:n,θ(k))⏟Part 3L(\theta, \theta^{(k)}) = \sum_{z_{1:n}} \underbrace{p(z_{1:n}|x_{1:n}, \theta^{(k)})}_{\text{Part 1}} \log \frac{\overbrace{p(x_{1:n}, z_{1:n}|\theta)}^{\text{Part 2}}}{\underbrace{p(z_{1:n}|x_{1:n}, \theta^{(k)})}_{\text{Part 3}}}

2 properties of L(θ,θ(k))L(\theta, \theta^{(k)}):

  1. L(θ,θ(k))≤log⁡p(x1:n|θ)L(\theta, \theta^{(k)}) \le \log p(x_{1:n}|\theta). LL is guaranteed to be a lower bound that always sits underneath the true log-likelihood.

  2. L(θ(k),θ(k))=log⁡p(x1:n|θ(k))L(\theta^{(k)},\theta^{(k)}) = \log p(x_{1:n|\theta^{(k)}}). When θ=θ(k)\theta = \theta^{(k)}, LL perfectly equals the true log-likelihood.
    Derivation: L(θ(k),θ(k))=∑z1:np(z1:n|x1:n,θ(k))log⁡p(x1:n,z1:n|θ(k))p(z1:n|x1:n,θ(k))=∑z1:np(z1:n|x1:n,θ(k))log⁡p(x1:n|θ(k))p(z1:n|x1:n,θ(k))p(z1:n|x1:n,θ(k))[p(x,z)=p(x)p(z|x)]=∑z1:np(z1:n|x1:n,θ(k))log⁡p(x1:n|θ(k))=1×log⁡p(x1:n|θ(k))[log⁡p(x1:n|θ(k)) is a constant]\begin{align*} L(\theta^{(k)}, \theta^{(k)}) &= \sum_{z_{1:n}} p(z_{1:n}|x_{1:n}, \theta^{(k)}) \log \frac{p(x_{1:n}, z_{1:n}|\theta^{(k)})}{p(z_{1:n}|x_{1:n}, \theta^{(k)})} \\ &= \sum_{z_{1:n}} p(z_{1:n}|x_{1:n}, \theta^{(k)}) \log \frac{p(x_{1:n}|\theta^{(k)}) p(z_{1:n}|x_{1:n}, \theta^{(k)})}{p(z_{1:n}|x_{1:n}, \theta^{(k)})} && \text{[$p(x, z) = p(x)p(z|x)$]}\\ &= \sum_{z_{1:n}} p(z_{1:n}|x_{1:n}, \theta^{(k)}) \log p(x_{1:n}|\theta^{(k)}) \\ &= 1 \times \log p(x_{1:n}|\theta^{(k)}) &&\text{[$\log p(x_{1:n}|\theta^{(k)})$ is a constant]} \end{align*}

Getting the Q-function: L(θ(k),θ(k))=∑z1:np(z1:n|x1:n,θ(k))log⁡p(x1:n,z1:n|θ)p(z1:n|x1:n,θ(k))=∑zp(z1:n|x1:n,θ(k))[log⁡p(x1:n,z1:n|θ)−log⁡p(z1:n|x1:n,θ(k))]=∑zp(z|x,θ(k))log⁡p(x,z|θ)⏟Term 1−∑zp(z1:n|x1:n,θ(k))log⁡p(z1:n|x1:n,θ(k))⏟Term 2=∑zp(z1:n|x1:n,θ(k))log⁡p(x1:n,z1:n|θ)=Q(θ,θ(k))\begin{align*} L(\theta^{(k)}, \theta^{(k)}) &= \sum_{\textstyle z_{1:n}} p(z_{1:n}|x_{1:n}, \theta^{(k)}) \log \frac{p(x_{1:n}, z_{1:n}|\theta)}{p(z_{1:n}|x_{1:n}, \theta^{(k)})} \\ &= \sum_z p(z_{1:n}|x_{1:n}, \theta^{(k)}) \Big[ \log p(x_{1:n}, z_{1:n}|\theta) - \log p(z_{1:n}|x_{1:n}, \theta^{(k)}) \Big] \\ &= \underbrace{\sum_z p(z|x, \theta^{(k)}) \log p(x, z|\theta)}_{\text{Term 1}} - \underbrace{\sum_z p(z_{1:n}|x_{1:n}, \theta^{(k)}) \log p(z_{1:n}|x_{1:n}, \theta^{(k)})}_{\text{Term 2}} \\ &= \sum_{z} p(z_{1:n}|x_{1:n}, \theta^{(k)}) \log p(x_{1:n}, z_{1:n}|\theta) \\ &= Q(\theta, \theta^{(k)}) \end{align*}

8.3.2.2 The M-step

Now we have a surrogate function built using our expectations of the hidden variables. The goal of the M-step is to find the brand new parameters (θ\theta) that maximize the Q function. We take the derivative of the Q-function with respect to our parameters θ\theta and solve for zero to find the brand new, updated parameters θ(k+1)\theta^{(k+1)}.

Note that Term 2 does not contain θ\theta, it only contains θ(k)\theta^{(k)} (our old, locked-in guesses), so we can discard it from the Q-function.
A more tractable form of the Q-function is: Q(θ,θ(k))=∑z1:np(z1:n|x1:n,θ(k))log⁡(∏i=1np(xi,zi|θ))=∑z1:np(z1:n|x1:n,θ(k))∑i=1nlog⁡p(xi,zi|θ)=∑i=1n∑zip(zi|xi,θ(k))log⁡p(xi,zi|θ)\begin{align*} Q(\theta, \theta^{(k)}) &= \sum_{z_{1:n}} p(z_{1:n}|x_{1:n}, \theta^{(k)}) \log \left( \prod_{i=1}^n p(x_i, z_i|\theta) \right) \\ &= \sum_{z_{1:n}} p(z_{1:n}|x_{1:n}, \theta^{(k)}) \sum_{i=1}^n \log p(x_i, z_i|\theta) \\ &= \sum_{i=1}^n \sum_{z_i} p(z_i|x_i, \theta^{(k)}) \log p(x_i, z_i|\theta) \end{align*}

8.3.3 Application to Gaussian Mixtures

The Q-function is Q(θ,θ(k))=∑i=1n∑zip(zi|xi,θ(k))log⁡p(xi,zi|θ)Q(\theta,\theta^{(k)})=\sum_{i=1}^{n}\sum_{z_{i}}p(z_{i}|x_{i},\theta^{(k)})\log p(x_{i},z_{i}|\theta) For a GMM, the parameters θ\theta consist of the mixing weights (π\pi), the means (μ\mu), and the covariance matrices (Σ\Sigma) for all KK clusters. Therefore, θ=(π,μ,Σ)\theta = (\pi, \mu, \Sigma) and for a specific cluster tt, θt=(πt,μt,Σt)\theta_t = (\pi_t, \mu_t, \Sigma_t).

8.3.3.1 The E-step

In this step, the goal is to use our current best guess of the parameters, denoted as θ(k)\theta^{(k)}, to compute the probability that a specific data point xix_i came from a specific cluster tt.
This probability is called the responsibility, denoted as γt,i(k)\gamma_{t,i}^{(k)}. We calculate it using Bayes’ Theorem: γt,i(k)=πt(k)p(xi|θt(k))∑s=1Kπs(k)p(xi|θs(k))\gamma_{t,i}^{(k)} = \frac{\pi_t^{(k)} p(x_i | \theta_t^{(k)})}{\sum_{s=1}^K \pi_s^{(k)} p(x_i | \theta_s^{(k)})}

8.3.3.2 The M-step

We plug these responsibilities into a surrogate function called the Q-function. The Q-function acts as a lower bound to our true, intractable log-likelihood. By maximizing the Q-function, we push the true likelihood up.
The Q-function for a GMM looks like this: Q(θ,θ(k))=∑i=1n∑t=1Kγt,i(k){log⁡πt+log⁡p(xi|θt)}Q(\theta, \theta^{(k)}) = \sum_{i=1}^n \sum_{t=1}^K \gamma_{t,i}^{(k)} \{\log \pi_t + \log p(x_i | \theta_t)\} We now take the derivatives of the Q-function with respect to our parameters (πt,μt,Σt\pi_t, \mu_t, \Sigma_t), set them to zero, and solve. This gives us the exact formulas to update our clusters to fit the data better, treating the responsibilities γt,i(k)\gamma_{t,i}^{(k)} as fixed weights.
First, we calculate the "effective number of points" assigned to cluster tt by summing up all the responsibilities for that cluster: nt(k+1)=∑i=1nγt,i(k)n_t^{(k+1)} = \sum_{i=1}^n \gamma_{t,i}^{(k)} Then, we use nt(k+1)n_t^{(k+1)} to calculate our brand new parameters, θ(k+1)\theta^{(k+1)}:

  1. Update the Mixing Weights: πt(k+1)=nt(k+1)n\pi_t^{(k+1)} = \frac{n_t^{(k+1)}}{n} The new weight is simply the effective number of points in cluster tt divided by the total number of points nn.

  2. Update the Means: μt(k+1)=1nt(k+1)∑i=1nγt,i(k)xi\mu_t^{(k+1)} = \frac{1}{n_t^{(k+1)}} \sum_{i=1}^n \gamma_{t,i}^{(k)} x_i The new center is a weighted average of all data points. Points with a high responsibility γ\gamma for this cluster pull the mean closer to themselves.

  3. Update the Covariance Matrices: Σt(k+1)=1nt(k+1)∑i=1nγt,i(k)(xi−μt(k+1))(xi−μt(k+1))⊤\Sigma_t^{(k+1)} = \frac{1}{n_t^{(k+1)}} \sum_{i=1}^n \gamma_{t,i}^{(k)} (x_i - \mu_t^{(k+1)})(x_i - \mu_t^{(k+1)})^\top The new shape is a weighted covariance calculation. Notice the use of the outer product (xi−μ)(xi−μ)⊤(x_i - \mu)(x_i - \mu)^\top which gives us the required matrix structure, scaled by how much each point belongs to the cluster.

We now have a new set of parameters θ(k+1)\theta^{(k+1)}. These are fed back into the E-step to calculate fresh responsibilities, which are then fed again into the M-step to get even better parameters.
This is repeated until the parameters stop changing significantly, at which point the algorithm has converged and the best fitting Gaussian distribution has been found.