We can go a long way in Machine Learning without having to deal with the scary probabilistic expressions. But, in order to be able to read the bleeding edge research as soon as it is published, and not from a blog, months later, we need to able to speak the language of probability. Probability is a great way to express the relationship between the data and the model, concisely. We are familiar with how Linear Regression works from Andrew Ng’s course. We know the loss function just tries to minimize the quadratic distance between data points and the model (line). In this post, we will revisit Linear Regression from a probabilistic perspective, using a method known as the Maximum Likelihood estimation. We could apply this knowledge to any Neural Network based architecture.

The principle of Maximum Likelihood is at the heart of Machine Learning. It guides us to find the best model in a search space of all models. In simple terms, Maximum Likelihood Estimation or MLE lets us choose a model (parameters) that explains the data (training set) better than all other models. For any given neural network architecture, the objective function can be derived based on the principle of Maximum Likelihood.

MLE is a tool based on probability. There are a few concepts in probability, that should be understood before diving into MLE. Probability is a framework for meauring and managing uncertainty. In machine learning, every inference we make, has some degree of uncertainty associated with it. It is essential for us to quantify this uncertainty. Let us start with the Gaussian Distribution.

## Gaussian Distribution

A probability distribution is a function that provides us the probabilities of all possible outcomes of a stochastic process. It can be thought of, as a description of the stochastic process, in terms of the probabilities of events. The most commonly occurring distribution is the Gaussian Distribution or the Normal Distribution.

The gaussian distribution is a means to measure the uncertainty of a variable that is continuous between and . The distribution is centered at mean, . The width depends on the parameter , the standard deviation (variance, ). Naturally, area under the curve equals 1.

Let us take the example of coin toss, to understand the normal distribution. We take an unbiased coin and flip it ‘n’ times. We calculate the probability of occurance of 1 to ‘n’ heads for each ‘n’ value. In the animation below, each frame is an experiment and the number on right top corner denotes the number of flips in that experiment. Each experiment involves flipping the coin ‘n’ times. We observe the probability of getting 1, 2,…‘n’ heads for each experiment, and plot it.

As you can see, with increase in number of flips (experiments), the curve of probability distribution tends to assume the shape of a normal distribution, represented by the equation above. In each of the experiments, the peak probability happens at half the number of heads and the probability density tends to decay on both sides. This is basically due to the fact that, there are more possible ways for the results to be close to half heads and half tails, compared to number of heads dominating the number of tail or vice versa.

That was fun to watch but how is this relevant to linear regression or machine learning? The data points in the training set, do not accurately represent the original data generating distribution or process. Hence we consider the process stochastic and build our model to accomodate a certain level of uncertainty. Every data point can be considered a random variable sampled from the data generating distribution which we assume to be gaussian. By that logic, learning or training is basically recreating the original distribution, that generated the training data.

### Random Sampling

The process of sampling from a normal distribution is expressed as, . ‘x’ is a random variable sampled or generated or simulated from the guassian distribution. As we sample from this distribution, most samples will fall around the center, near the mean, because of higher probability density in the center.

### Multivariate Gaussian

The 2D gaussian distribution or bivariate distribution, consists of 2 random variables and . It can have many different shapes. The shape depends on the covariance matrix, . The Multivariate Gaussian Distribution is a generalization of bivariate distribution, for ‘n’ dimensions.

In the above expression, **y** and are vectors of ‘n’ dimensions, and is a matrix of shape ‘nxn’.

The figure below presents the top view of bivariate gaussian. The smaller circles denote the data points sampled from the distribution. Notice the variation in the shape of the gaussian with . The mean, given by and ( and ) determine the center of the gaussian, while determines the shape.

## Maximum Likelihood

I am borrowing this amazing toy example from Nando de Fretais’s lecture, to illustrate the principle of maximum likelihood. Consider 3 data points, , which are independent and drawn from a guassian with unknown mean and variance 1. Let’s say we have two choices for : {1, 2.5}. Which would you choose? Which model () would explain the data better?

In general, any data point drawn from a gaussian with mean and and variance 1, can be written as,

, the mean, shifts the center of the standard normal distribution ( and ).

The likelihood of data () having been drawn from , can be defined as,

as are independent.

Now, we have two normal distributions defined by = 1 and = 2.5. Let us draw both and plot the data points. In the figure below, notice the dotted lines that connect the bell curve to the data points. Consider the point in the first distribution(). The length of the dotted line gives the probability of the being drawn from .

The likelihood of data () having been drawn from , is given by,

The individual probabilities in the equation above, are equal to the heights of corresponding dotted lines in the figure. We see that the likelihood, given by the product of individual probabilities of data points given model, is basically the product of lengths of dotted lines. It is obvious that the likelihood of model is higher. We choose the model (), that maximizes the likelihood.

## Linear Regression

We are ready to apply MLE to linear regression, where the objective is to find the best line that fits the data points. But first, let us make some assumptions. We assume each label, , is gaussian distributed with mean, and variance, , given by

where each is a vector of form ().

The mean, represents the best fit line. The data points will vary about the line, and the second term, captures this variance, (see figure below).

### Learning

If we assume that each point is gaussian distributed, the process of learning becomes the process of maximizing the product of the individual probabilities, which is equivalent to maximizing the log likelihood. We switch to log space, as it is more convenient and it removes the exponential in the gaussian distribution.

As the data points are independent, we can write the joint probability distribution of as,

rewriting in vector form,

Log likelihood,

The first term is a constant and the second term is a parabola, the peak (maxima) of which can be found by equating the derivative of to zero. Equating first derivative to zero, we get,

we get,

Finally, we reach our goal of finding the best model for linear regression. This equation is commonly known as the normal equation. The same equation can be derived using the least squares method (perhaps in another post).

Similarly, we can get the maximum likelihood of variance, , by differentiating log likelihood with respect to and equating to zero.

This gives us the standard estimate of variance in the training data.

## Inference

So, we have learned the parameters of our model, and . Based on these learned parameters, we can make inferences for new unseen data. For a new data point , our best estimate is given by,

The degree of uncertainty in our estimate, is given by the variance, .

## Reference

- Andrew Ng’s Machine Learning course
- Nando de Freitas’s Machine Learning course
- Maximum Likelihood and Linear Regression
- Coin Toss Experiment
- Bivariate Gaussian in matplotlib

I hope to do a follow up post very soon, applying maximum likelihood to more complex neural networks, and also introduce the concept of Bayesian Learning and compare it with the Frequentist approach.

Feel free to drop a comment.