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 with inputs and outputs , we aim to find a probability distribution that "best" describes the dataset, i.e. maximizes the likelihood of the data under some assumptions.
An additional constraint is that the pairs 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:
- 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 . 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 , our blueprint is then the joint distribution . Some of the most common generative models are Naive Bayes and Gaussian Mixture Models [2].
- Based on How You Estimate the Distribution: We assume that the distribution has a parametric form , where is the parameter vector, and fit 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 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 , making it a good starting point.
Given a dataset where , 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:
where is the parameter vector that we want to learn.
Taking the exponential of both sides, we get:
Since the labels are binary, we know that . So, we can rewrite the above equation as:
Solving for , we get:
where is the sigmoid function which maps any real value to a probability between 0 and 1.
Next, we find the that maximizes the likelihood of the data, , where the likelihood function is given by:
Substituting the sigmoid function, we get:
Maximizing the likelihood function over the data is equivalent to minimizing the negative log of the likelihood function,
To find the parameter vector 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 until the gradient is (close to) zero, where 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:
A quick note on the derivative of the sigmoid function:
Using this, the derivative for a single data point simplifies to:
So over all the data points, we have:
Because 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 where , 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 classes. We model the conditional probability of each class, and choose one class as the reference class such that . Next, we know that the sum of the probabilities of all the classes must be 1, so we have:
Now, we can rewrite the log-odds of the output as a linear combination of the input features, for all :
Exponentiating both sides, we get:
Substituting equation 2.2 into equation 2.1 and factoring out , we get:
Substituting equation 2.3 back into equation 2.2, we get:
Since we have , we can simplify the above equation to:
which is the softmax function.
Similar to logistic regression, we can use gradient descent to find the parameter vector that minimizes the negative log-likelihood of the data. However, the likelihood function is a bit more complicated:
where is the indicator function that is 1 if and 0 otherwise.
Taking the negative log of the likelihood function, we get:
Since a closed-form solution is not possible, we use gradient descent once again, with the same update rule . Again, to make this easier to work with, we take the derivative of the negative log-likelihood with respect to a single weight vector for a single example,
Using
and the fact that exactly one indicator is 1, the gradient for a single example is:
Summing over all examples, the gradient with respect to is: