Hello, world! My starting this blog has been long overdue. I thought I’d ease myself into it with something easy and uncontroversial, so here’s my take on linear regression. We start with the standard (frequentist) maximum likelihood approach, and show that Gaussian noise induces the familiar least-squares result, then show the correspondence between isotropic Gaussian priors and regularizers, before playing with some Bayesian updating using Mathematica. Note that these results are all well-known and discussed in detail in Bishop [1]; my motivation for reproducing them here is that I hadn’t encountered a concise and complete online write-up that I found satisfying.


  • 2016-03-30: Edits for readability, and added a section on Bayesian regression.
  • 2016-11-28: Some more minor edits, and moved the code to its own GitHub repo.


In the standard regression task we are given labelled data

with each of the , and drawn i.i.d. from some unknown stochastic process . Our objective is to learn a linear model of the form

where are our model weights (parameters), and the basis functions are known and fixed. Now, let’s assume the data contains additive zero-mean Gaussian noise, so that

and so the probability density of the targets conditioned on the inputs is

The Gaussian noise assumption is motivated by the central limit theorem, and it helps in the Bayesian setting, since the Gaussian is its own conjugate prior. Note here that we make the conditional dependence on and explicit, and that we assume that the precision is known, and that the basis functions are well-chosen in some sense.

Maximum Likelihood Solution

Given that our dataset is drawn i.i.d., the likelihood is given by

Our objective now is to learn the weights that maximise this likelihood, taking the variance of the noise to be fixed. Notice that since the don’t depend on the parameters \(w\), we can neglect them in our objective function. The maximum likelihood (ML) solution for is

Now, is a monotonically increasing concave function, so

This shows the correspondence between maximizing the likelihood under Gaussian noise with minimizing the square error. Minimizing the sum of square error has a nice geometric interpration, since it corresponds to finding the point in the subspace spanned by the basis which minimizes the Euclidean distance to .

This is the standard frequentist result. The objective is a quadratic program, is convex, and has the well-known closed-form solution

where is the vector of targets, and we have defined the design matrix so that its row is given by .

Maximum likelihood is notorious for overfitting, since by definition it finds the model for which our particular dataset is most probable; without tweaks, such a model will not generalize well to unseen data. The tweak that is typically introduced in the frequentist framework is an regularizer on the weights; this will tend to bias us towards simpler models with small coefficients. Note that there are numerous other ways in which we can penalize complex models; for example, we can use the MDL principle, or try to make sparse by using an regularizer instead.

MAP Inference

Interestingly, a certain class of prior over induces an regularizer when we calculate the posterior. This gives a stronger intuition for introducing this term into the objective function. Consider the isotropic Gaussian prior with hyperparameter

where is the identity matrix. Because a Gaussian is its own conjugate prior, we know that the posterior is a Gaussian. We can now easily compute the weights that maximize this posterior, conditioned on seeing some data . This is the maximum a posteriori (MAP) point estimate of . Using the prior above, we can easily see that

where the constant is dependent only on the hyperparameters and . The second term in our objective is now clearly the regularizer we’ve been looking for. This objective is also convex, and has a simple closed-form solution analogous to the simple ML case.

Full Bayes

With MAP inference we start with a relatively ignorant prior over our model parameters, learn from a big batch of data, and end up with a point estimate. This clearly is not useful if we want to learn online with a sequential learning scheme, in which we update our belief with each new datum we receive. For each new data point that we receive, we compute the posterior from Bayes rule, using as a prior the current state of our belief:

where in our case . Using Gaussian priors and likelihoods, we ensure that our posterior is also Gaussian, and so we can write down the posterior directly:

where, as it turns out (Equations (3.53) and (3.54) in Bishop),

We now have the ingredients we need to implement a Bayesian updating scheme. The Mathematica notebook can be found here.

$$\ \dots\ $$

Figure: Contour plots of the distribution . (a) Isotropic prior. (b) Posterior after updating on one data point. (c) Posterior after updating on 20 data points. The white represents the ground truth.

Predictive distribution

We can use the predictive distribution, representing our uncertainty in the value of , given some and a bunch of experience . Note that we marginalize out the parameter , using .

Of course, the central issue with the Bayesian scheme (neglecting the computational/analytic difficulties arising from maintaining and updating a large distribution) is choosing the prior in a sensible and principled way. Note that the prior we used in the polynomial fitting example above was essentially chosen for convenience; though it is simple and thus reasonable according to Ockham’s principle, it is still chosen arbitrarily. Enter Solomonoff’s universal prior, which I’ll discuss more in a later post.


[1] C. M. Bishop. Pattern Recognition and Machine Learning. Springer, 2006