The Probabilistic View of Machine Learning

March 19, 2025

This post is a collection of the mathematical concepts that I have come across in the past few months. The probabilistic way of thinking about machine learning is elegant and answers a lot of the whys of machine learning.

Problem Setup

I like to think of machine learning as pattern matching, where we find a function that maps input to an output. The probabilistic interpretation generalizes this notion to a probability distribution over the output given the input.

Given a dataset D={(x1,y1),(x2,y2),...,(xn,yn)}D = \{(x_{1}, y_{1}), (x_{2}, y_{2}), ..., (x_{n}, y_{n})\} with inputs xx and outputs yy, we aim to find a probability distribution p(x,y)p(x, y) that "best" describes the dataset, i.e. maximizes the likelihood of the data under some assumptions.

An additional constraint is that the pairs (xi,yi)∈D(x_{i}, y_{i}) \in D are independent and identically distributed, i.e. the dataset we are training on is a good (random) sample from the underlying distribution, which intuitively makes sense since we can't really "learn" anything otherwise. Also, we want our model to be able to generalize and predict on new data [1].

The "best" probabilistic model depends on our modeling goals and assumptions about the data. We can classify the methods in two ways:

  1. Based on What You Model: A discriminative approach, as the name suggests, discriminates between classes, which makes it a good tool for classification tasks. We model the conditional distribution p(y∣x)p(y \mid x). This is the most common approach to machine learning in practice, and some examples include Logistic Regression, Neural Networks, and Decision Trees. A generative approach is more nuanced, as we are trying to create a "blueprint" of our data. Building on the "blueprint" analogy, if the final result of the "blueprint" is the observed sample (x,y)(x, y), our blueprint is then the joint distribution p(x,y)p(x, y). Some of the most common generative models are Naive Bayes and Gaussian Mixture Models [2].
  2. Based on How You Estimate the Distribution: We assume that the distribution has a parametric form p(x,y;θ)p(x, y; \theta), where θ\theta is the parameter vector, and fit θ\theta via Maximum Likelihood Estimation (MLE) or Bayesian inference. I like to think of this as a "knobs" approach, where we are looking for the best θ\theta to fit the data. Examples include Logistic Regression, Neural Networks, and Naive Bayes. There are other ways to estimate a distribution (non-parametric methods like Decision Trees, for example), but the main focus here is on parametric likelihood. So, with a high-level understanding of the problem setup, let's dive deeper into the details of some of the most common machine learning algorithms.

Logistic Regression

We start with the classic logistic regression algorithm which is a classification model (seriously the real hard problem in machine learning is naming stuff). Logistic regression is a discriminative model that uses a parametric approach to model the conditional distribution p(y∣x;θ)p(y \mid x; \theta), making it a good starting point.

Given a dataset D={(x1,y1),(x2,y2),...,(xn,yn)}D = \{(x_{1}, y_{1}), (x_{2}, y_{2}), ..., (x_{n}, y_{n})\} where yi∈{0,1}y_i \in \{0, 1\}, the logistic regression algorithm models the log odds of the output as a linear combination of the input features.

Following from the definition above, we have:

log⁡p(y=1∣x;θ)p(y=0∣x;θ)=θTx \log \frac{p(y = 1 \mid x; \theta)}{p(y = 0 \mid x; \theta)} = \theta^T x

where θ\theta is the parameter vector that we want to learn.

Taking the exponential of both sides, we get:

p(y=1∣x;θ)p(y=0∣x;θ)=eθTx \frac{p(y = 1 \mid x; \theta)}{p(y = 0 \mid x; \theta)} = e^{\theta^T x}

Since the labels are binary, we know that p(y=1∣x;θ)=1−p(y=0∣x;θ)p(y = 1 \mid x; \theta) = 1- p(y = 0 \mid x; \theta). So, we can rewrite the above equation as:

p(y=1∣x;θ)1−p(y=1∣x;θ)=eθTx \frac{p(y = 1 \mid x; \theta)}{1 - p(y = 1 \mid x; \theta)} = e^{\theta^T x}

Solving for p(y=1∣x;θ)p(y = 1 \mid x; \theta), we get:

p(y=1∣x;θ)=11+e−θTx=σ(θ⊤x)p(y = 1 \mid x; \theta) = \frac{1}{1 + e^{-\theta^T x}} = \sigma(\theta^{\top}x)

where σ(z)=11+e−z\sigma(z) = \frac{1}{1 + e^{-z}} is the sigmoid function which maps any real value to a probability between 0 and 1.

Next, we find the θ\theta that maximizes the likelihood of the data, θ∗=arg max⁡θL(θ)\theta^* = \argmax_{\theta} \mathcal L(\theta), where the likelihood function is given by:

L(θ)=∏i=1np(yi∣xi;θ) \mathcal L( \theta) = \prod_{i=1}^{n} p(y_{i} \mid x_{i}; \theta)

Substituting the sigmoid function, we get:

L(θ)=∏i=1nσ(θ⊤xi)yi(1−σ(θ⊤xi))1−yi.\mathcal L( \theta) = \prod_{i=1}^{n} \sigma(\theta^{\top}x_i)^{y_{i}} \left( 1 - \sigma(\theta^{\top}x_i) \right)^{1 - y_{i}}.

Maximizing the likelihood function over the data is equivalent to minimizing the negative log of the likelihood function,

ℓ(θ)=−log⁡L(θ)=−log⁡∏i=1n(σ(θ⊤xi))yi(1−σ(θ⊤xi))1−yi\ell(\theta) = - \log \mathcal L(\theta) = - \log \prod_{i=1}^{n} \left( \sigma(\theta^{\top}x_i) \right)^{y_{i}} \left( 1 - \sigma(\theta^{\top}x_i) \right)^{1 - y_{i}}
ℓ(θ)=−∑i=1n(yilog⁡σ(θ⊤xi)+(1−yi)log⁡(1−σ(θ⊤xi))).\ell(\theta) = -\sum_{i=1}^{n} \left( y_{i} \log \sigma (\theta^{\top}x_i) + (1 - y_{i}) \log (1 - \sigma(\theta^{\top}x_i)) \right).

To find the parameter vector θ\theta that minimizes the negative log-likelihood of the data, we use gradient descent because no closed-form solution exists. Intuitively, gradient descent looks for the direction in which the loss decreases fastest and takes a small step in that direction. The gradient points in the direction of steepest ascent, so we step along the negative gradient, repeating the update rule θ←θ−η∇ℓ(θ)\theta \leftarrow \theta - \eta \nabla \ell(\theta) until the gradient is (close to) zero, where η\eta is the learning rate.

To make this easier to work with, we start by taking the derivative of the negative log-likelihood for a single data point:

∂ℓi(θ)∂θ=−∂∂θ(yilog⁡σ(θ⊤xi)+(1−yi)log⁡(1−σ(θ⊤xi))). \frac{\partial \ell_i(\theta)}{\partial \theta} = - \frac{\partial }{\partial \theta} \left( y_{i} \log \sigma (\theta^{\top}x_i) + (1 - y_{i}) \log (1 - \sigma(\theta^{\top}x_i)) \right).

A quick note on the derivative of the sigmoid function:

∂σ(θ⊤xi)∂θ=σ(θ⊤xi)(1−σ(θ⊤xi)) xi\frac{\partial \sigma(\theta^{\top}x_i)}{\partial \theta} = \sigma(\theta^{\top}x_i) (1 - \sigma(\theta^{\top}x_i)) \, x_i

Using this, the derivative for a single data point simplifies to:

∂ℓi(θ)∂θ=−(yi(1−σ(θ⊤xi))−(1−yi)σ(θ⊤xi))xi=(σ(θ⊤xi)−yi)xi.\frac{\partial \ell_i( \theta)}{\partial \theta} = -\left( y_{i}(1 - \sigma(\theta^{\top}x_i)) - (1 - y_{i}) \sigma(\theta^{\top}x_i) \right) x_i = \left( \sigma(\theta^{\top}x_i) - y_i \right) x_i.

So over all the data points, we have:

∇ℓ(θ)=∑i=1n(σ(θ⊤xi)−yi)xi.\boxed{\nabla \ell( \theta) = \sum_{i=1}^{n} \left( \sigma(\theta^{\top}x_i) - y_i \right) x_i.}

Because ℓ(θ)\ell(\theta) is convex, gradient descent with a suitable learning rate converges to a global minimum, with no local minima to get stuck in.[3]

Multinomial Regression

With logistic regression we focus on binary classification, now we generalize the idea to multi-class classification.

Given a dataset D={(x1,y1),(x2,y2),...,(xn,yn)}D = \{(x_{1}, y_{1}), (x_{2}, y_{2}), ..., (x_{n}, y_{n})\} where yi∈{1,2,...,k}y_i \in \{1, 2, ..., k\}, similar to the binary case, we model the log-odds of the output as a linear combination of the input features, however, this time we have kk classes. We model the conditional probability of each class, and choose one class CC as the reference class such that θC=0\theta_C = 0. Next, we know that the sum of the probabilities of all the classes must be 1, so we have:

p(y=C∣x;θ)+∑c′≠Cp(y=c′∣x;θ)=1(2.1)p(y = C \mid x; \theta) + \sum_{c' \neq C} p(y = c' \mid x; \theta) = 1 \tag{2.1}

Now, we can rewrite the log-odds of the output as a linear combination of the input features, for all c≠Cc \neq C:

log⁡p(y=c∣x;θ)p(y=C∣x;θ)=θc⊤x\log \frac{p(y = c \mid x; \theta)}{p(y = C \mid x; \theta)} = \theta_c^{\top}x

Exponentiating both sides, we get:

p(y=c∣x;θ)p(y=C∣x;θ)=eθc⊤x(2.2)\frac{p(y = c \mid x; \theta)}{p(y = C \mid x; \theta)} = e^{\theta_c^{\top}x} \tag{2.2}

Substituting equation 2.2 into equation 2.1 and factoring out p(y=C∣x;θ)p(y = C \mid x; \theta), we get:

p(y=C∣x;θ)[1+∑c′≠Ceθc′⊤x]=1p(y = C \mid x; \theta)\left [1 + \sum_{c' \neq C} e^{\theta_{c'}^{\top}x} \right ] = 1
p(y=C∣x;θ)=11+∑c′≠Ceθc′⊤x(2.3)p(y = C \mid x; \theta) = \frac{1}{1 + \sum_{c' \neq C} e^{\theta_{c'}^{\top}x}} \tag{2.3}

Substituting equation 2.3 back into equation 2.2, we get:

p(y=c∣x;θ)=p(y=C∣x;θ)eθc⊤xp(y = c \mid x; \theta) = p(y = C \mid x; \theta) e^{\theta_c^{\top}x}
p(y=c∣x;θ)=eθc⊤x1+∑c′≠Ceθc′⊤xp(y = c \mid x; \theta) = \frac{e^{\theta_c^{\top}x}}{ 1 + \sum_{c' \neq C} e^{\theta_{c'}^{\top}x}}

Since we have θC=0\theta_C = 0, we can simplify the above equation to:

p(y=c∣x;θ)=eθc⊤x∑c′=1keθc′⊤x(2.4)p(y = c \mid x; \theta) = \frac{e^{\theta_c^{\top}x}}{ \sum_{c' = 1}^{k} e^{\theta_{c'}^{\top}x}} \tag{2.4}

which is the softmax function.

Similar to logistic regression, we can use gradient descent to find the parameter vector θ\theta that minimizes the negative log-likelihood of the data. However, the likelihood function is a bit more complicated:

L(θ)=∏i=1n∏c=1kp(yi=c∣xi;θ)1{yi=c}\mathcal L(\theta) =\prod_{i=1}^{n} \prod_{c=1}^{k} p(y_i = c \mid x_i; \theta)^{ 1 \{y_i = c\}}

where 1{yi=c}1\{y_i = c\} is the indicator function that is 1 if yi=cy_i = c and 0 otherwise.

Taking the negative log of the likelihood function, we get:

ℓ(θ)=−log⁡L(θ)=−log⁡(∏i=1n∏c=1kp(yi=c∣xi;θ)1{yi=c})\ell(\theta) = - \log \mathcal L(\theta) = - \log \left( \prod_{i=1}^{n} \prod_{c=1}^{k} p(y_i = c \mid x_i; \theta)^{ 1 \{y_i = c\}} \right )
ℓ(θ)=−∑i=1n∑c=1k1{yi=c}log⁡p(yi=c∣xi;θ)\ell(\theta) = - \sum_{i=1}^{n} \sum_{c=1}^{k} 1\{y_i = c\} \log p(y_i = c \mid x_i; \theta)
ℓ(θ)=−∑i=1n∑c=1k1{yi=c}log⁡eθc⊤xi∑c′=1keθc′⊤xi\ell(\theta) = - \sum_{i=1}^{n} \sum_{c=1}^{k} 1\{y_i = c\} \log \frac{e^{\theta_c^{\top}x_i}}{\sum_{c' = 1}^{k} e^{\theta_{c'}^{\top}x_i}}
ℓ(θ)=−∑i=1n∑c=1k1{yi=c}(θc⊤xi−log⁡∑c′=1keθc′⊤xi)(2.5)\ell(\theta) = - \sum_{i=1}^{n} \sum_{c=1}^{k} 1\{y_i = c\} \left( \theta_c^{\top}x_i - \log \sum_{c' = 1}^{k} e^{\theta_{c'}^{\top}x_i} \right) \tag{2.5}

Since a closed-form solution is not possible, we use gradient descent once again, with the same update rule θ←θ−η∇ℓ(θ)\theta \leftarrow \theta - \eta \nabla \ell(\theta). Again, to make this easier to work with, we take the derivative of the negative log-likelihood with respect to a single weight vector θc\theta_c for a single example,

∂ℓi(θ)∂θc=−∂∂θc∑j=1k1{yi=j}(θj⊤xi−log⁡∑c′=1keθc′⊤xi)\frac{\partial \ell_i(\theta)}{\partial \theta_c} = - \frac{\partial }{\partial \theta_c} \sum_{j=1}^{k} 1 \{y_i = j\} \left( \theta_j^{\top}x_i - \log \sum_{c' = 1}^{k} e^{\theta_{c'}^{\top}x_i} \right)

Using

∂eθc′⊤xi∂θc={eθc⊤xiif c′=c0if c′≠c\frac{\partial e^{\theta_{c'}^{\top} x_i}}{\partial \theta_c} = \begin{cases} e^{\theta_c^{\top} x_i} & \text{if } c' = c \\ 0 & \text{if } c' \neq c \end{cases}

and the fact that exactly one indicator is 1, the gradient for a single example is:

∂ℓi(θ)∂θc=(eθc⊤xi∑c′=1keθc′⊤xi−1{yi=c})xi\frac{\partial \ell_i(\theta)}{\partial \theta_c} = \left(\frac{e^{\theta_c^{\top}x_i}}{\sum_{c' = 1}^{k} e^{\theta_{c'}^{\top}x_i}} - 1 \{y_i = c\}\right)x_i

Summing over all examples, the gradient with respect to θc\theta_c is:

∇θcℓ(θ)=∑i=1n(eθc⊤xi∑c′=1keθc′⊤xi−1{yi=c})xi.\boxed{\nabla_{\theta_c} \ell( \theta) = \sum_{i=1}^{n} \left( \frac{e^{\theta_c^{\top}x_i}}{\sum_{c' = 1}^{k} e^{\theta_{c'}^{\top}x_i}} - 1 \{y_i = c\}\right)x_i.}

References