Probabilistic Classification


Probabilistic Classification

Michael
Welcome to probabilistic classification. So far, we have focused on classifiers that receive a record and return one prediction for the class of the target variable. In this section, we extend that idea: instead of making only one guess, a classifier can express uncertainty in richer ways.

Anna
The simplest kind is a point classifier, or point predictor. It returns a single value, which we might call v-hat. In other words, it makes one specific guess about which class the target belongs to.

Michael
A more general classifier might return an ordered list of guesses. Imagine a camera mounted on a car looking at something in the street. It might say the object is most likely a pedestrian, the second choice is someone riding a scooter, and the third choice is someone riding a bicycle.

Anna
So the classifier knows enough to rule out possibilities such as a car, but it cannot confidently identify one exact class. The list can also have a fixed size, such as the top three or top ten, or a variable size depending on how uncertain the classifier is.

Michael
The most informative output is often a probability distribution over the possible target classes. For the street example, the classifier might assign probability 0.9 to pedestrian, 0.05 to scooter, and 0.05 to bicycle.

Anna
Weather forecasts give us a familiar example. Rather than simply saying rain or no rain, a weather app might say that the probability of rain today is 15 percent and the probability of no rain is 85 percent.

Michael
Let's first examine list classifiers. Suppose the predictions are called v-top, v-second, and v-third. We would be happiest if the true class were v-top, somewhat less happy if it were v-second, and less happy again if it were v-third.

Anna
Recommendation systems commonly produce this kind of output. For example, Amazon can use browsing history and recent purchases to create a ranked list of the products a person is most likely to be interested in or buy next.

Michael
One way to build a list classifier is with an embedding and nearest neighbors. If a predictor produces an embedded prediction, we could return not only the closest representative in the embedded space, but the three closest representatives. The closest is the top guess, the next closest is the second guess, and so on.

Anna
Now let's turn to probability more formally. A probability distribution on the target set V assigns a real number to every possible class. Every assigned number must be non-negative, and all the numbers must add up to one.

Michael
If p is the distribution, then p of v means the probability assigned to class v. For a target set containing rain and shine, we could have p of rain equal to 0.15 and p of shine equal to 0.85.

Anna
If the classes are labeled v-one through v-k, we can represent the distribution as a k-dimensional vector. Its entries are p-one through p-k, where p-i is the probability of class v-i.

Michael
In vector notation, p is greater than or equal to zero, interpreted element by element, because every probability must be non-negative. The condition that the probabilities sum to one can be written as one-transpose p equals one, where the vector one contains all ones.

Anna
So there are two equivalent ways to think about a probability distribution. It can be a function that maps each class in V to a real number, or it can be a vector with one coordinate for each of the k classes. We will use both viewpoints.

Michael
A probabilistic classifier takes an input u and returns a predicted probability distribution on V. We can write that prediction as p-hat equals capital G of u. The hat indicates that this is a predicted distribution rather than an underlying true one.

Anna
There is a small but important notational point here. Capital G is a function whose output is itself a function, namely the probability distribution p-hat. Once we have p-hat, we can evaluate it at a class v-i to obtain the predicted probability of that class.

Michael
So p-hat of v-i is the probability the classifier assigns to class v-i for the particular input u. We could write this in one expression as capital G of u evaluated at v-i.

Anna
Point and probabilistic classifiers are related. A point prediction v-hat can be converted into a probability distribution that assigns probability one to v-hat and probability zero to every other class.

Michael
That conversion is technically probabilistic, but it does not add information. It simply expresses complete certainty in the original point prediction. The more useful direction is often to start with a probability distribution and turn it into a single actionable choice.

Anna
To do that, choose the class with the highest predicted probability. Formally, select the class that maximizes p-hat of v. This method is called maximum likelihood.

Michael
For example, if a weather predictor gives probabilities for rain and shine, one simple action rule is to carry an umbrella when rain has the higher probability and leave it behind when shine has the higher probability. There are many possible ways to turn probabilities into actions, but maximum likelihood is one straightforward method.

Anna
We can also turn a probabilistic classifier into a list classifier. Simply sort the classes by their predicted probabilities. The class with the largest probability is first, the class with the next-largest probability is second, and so forth.

Michael
Several familiar classifier types can be extended in this way. A decision tree can have probability distributions at its leaves rather than single point predictions. A nearest-neighbor classifier can also produce probabilities by examining the labels of nearby training examples.

Anna
Suppose a query point has k nearest neighbors. We look at the distribution of their target classes. If three of eight neighbors are in one class and five are in another, the classifier assigns probabilities three-eighths and five-eighths to those classes.

Michael
For a two-dimensional illustration, imagine red and blue labeled points in the plane and a query point. If we use the eight nearest neighbors, we count how many are red and how many are blue. The resulting empirical distribution determines the predicted probabilities at that query point.

Anna
The prediction can be visualized by color. A point whose neighborhood is entirely blue receives a probability of 100 percent blue and 0 percent red, while a point surrounded entirely by red receives the reverse. Intermediate mixtures produce intermediate colors.

Michael
Now we need ways to evaluate these richer predictions. For a list classifier, one option is to measure accuracy as a function of rank. On a test set, we can ask how often the true class was the top guess, how often it appeared among the top two, and how often it appeared among the top three.

Anna
For example, perhaps the top guess is correct for 68 percent of test cases, the true class is among the top two for 79 percent, and it is among the top three for 85 percent. If being in the top three is what matters, then 85 percent is the relevant accuracy.

Michael
Another approach assigns points according to rank. A correct top guess might receive three points, a correct second guess two points, and a correct third guess one point. Adding those scores over the test set gives a measure that rewards better rankings.

Anna
Evaluating a probabilistic classifier is subtler. For every test example, we have the true class v and a predicted distribution p-hat. A good prediction should assign a high probability to the class that actually occurs.

Michael
For instance, if we predict the chance of rain for each day, then on days when it actually rains, we want the predicted probability of rain to be large. On days when it does not rain, we want that probability to be small.

Anna
The standard formal measure is based on likelihood. Suppose the inputs are u-one through u-n, the observed classes are v-one through v-n, and the prediction for input u-i is p-hat-i. The probability assigned to the observed outcome v-i is p-hat-i evaluated at v-i.

Michael
If we assume the data points are independent, the probability of observing the entire sequence of classes is the product of those individual probabilities: the product, over i, of p-hat-i evaluated at v-i.

Anna
We can view that product in two ways. As a probability, it describes how likely the observed outcomes are under the predicted distributions. As a likelihood, it measures how plausible the predicted distributions are given the observed outcomes.

Michael
A larger likelihood indicates a better match between the predictions and the data. Therefore, two probabilistic classifiers can be compared by comparing their likelihoods, with the larger likelihood being preferred.

Anna
In practice, it is more convenient to use logarithms. The logarithm turns the product into a sum. We usually take the negative log likelihood, which is negative the sum over i of the log of p-hat-i evaluated at v-i.

Michael
Because of the negative sign, a better classifier has a smaller negative log likelihood. Assigning a high probability to an outcome makes its negative log contribution small, while assigning a very low probability makes the contribution large.

Anna
The raw negative log likelihood also grows as the number of test examples grows, so we normalize by the number of examples. The average negative log likelihood is negative one over n times the sum of the logs of the probabilities assigned to the observed classes.

Michael
This average gives us a useful performance metric that can be compared across data sets of different sizes. It is the probabilistic analogue of evaluating a prediction rule using an average loss.

Anna
There is a helpful question we can ask about this metric: if the classifier is forced to make the same distributional prediction for every example, what is the best constant prediction?

Michael
Suppose the observed target values are v-one through v-n, and we choose one fixed distribution p-hat that does not depend on the inputs. The distribution must still be non-negative and sum to one. We want the p-hat that minimizes the average negative log likelihood.

Anna
The answer is the empirical distribution of the observed classes. For each class, count how often it appears in the data and divide by the total number of observations. Those frequencies form the best constant probabilistic classifier.

Michael
This parallels earlier results. The best constant predictor under squared loss is the mean, and the best constant predictor under absolute loss is the median. Under average negative log likelihood, the best constant probabilistic predictor is the empirical distribution.

Anna
To see the structure of the objective, group the data by class. Instead of summing one term for every observation, we can sum over the k classes. The contribution of class v-j is its empirical frequency q-j multiplied by the logarithm of the predicted probability p-hat-j, with an overall negative sign.

Michael
This quantity is called the cross entropy of p-hat relative to q. In vector notation, it is negative the sum from j equals one to k of q-j times log p-hat-j. Here q is the empirical, or underlying observed, distribution, and p-hat is the distribution produced by the classifier.

Anna
When the two distributions are the same, the corresponding quantity is called entropy. The entropy of p is negative the sum of p-j times log p-j. Cross entropy and entropy are important in information theory, coding theory, and machine learning.

Michael
A closely related quantity is the Kullback-Leibler divergence, or KL divergence. The KL divergence from q to p is the cross entropy of q relative to p minus the entropy of q.

Anna
Subtracting the entropy does not change which p minimizes the objective, because the entropy of q is constant with respect to p. The KL divergence is useful because it is always non-negative and equals zero when the two distributions are the same.

Michael
So we can interpret KL divergence as a measure of how different two probability distributions are. The fact that it is non-negative follows from the logarithm inequality log x less than or equal to x minus one, together with the fact that both probability distributions sum to one.

Anna
Therefore, the cross entropy is minimized when the predicted distribution p-hat equals the empirical distribution q. At that point, the KL divergence is zero, and the cross entropy reduces to the entropy of q.

Michael
Let's close with the main picture. A point classifier makes one specific guess about the target class. A list classifier ranks several possible guesses. A probabilistic classifier assigns a full probability distribution to all possible classes.

Anna
Probabilistic predictions can be converted into point predictions by choosing the most likely class, or into ranked lists by sorting classes by probability. To evaluate them, we use average negative log likelihood on test data, which rewards assigning high probability to what actually occurs.

Michael
And when the classifier is constant, the best distribution under this metric is simply the empirical distribution of the target classes. That connects probabilistic classification to cross entropy, entropy, and KL divergence.

Ask anything
We've read this whole study set, so questions about it get the best answers.