In the beginning of our article series, we already talk about how to derive polynomial regression using LSE (Linear Square Estimation) here. During this post, we will try to discuss linear regression from Bayesian point of view. **Note that linear and polynomial regression here are similar in derivation, the difference is only in design matrix**. You may check again our couple previous articles here and here.

I let you know in the beginning that the final result of deriving regression using LSE is equal to the result of deriving linear regression using MLE (Maximal Likelihood Estimation) in Bayesian method. Furthermore, the result of deriving regression using LSE with regularization is equal to the result of deriving using MAP (Maximum A Posteriori) in Bayesian method. During this post, we will try to prove it. And we will proceed to derive the posterior update formula for online learning using Conjugate prior.

## (1) Regression using LSE = MLE Bayesian?

See picture below.

Given data (green dots), we model in pink line using Gaussian distribution, and our regression prediction is the mean of it (), depicted using red line. To do regression toward given data, we can make formula below.

In this case, we can think that is noise/bias error of our given data, so that we should consider it in our model. Thus, we will form a distribution (using Gaussian) for vertically like shown with the pink line in the picture above. It will have higher value when closer to the red line or given data, and smaller value when further to the read line or given data. And our goal is to maximize the probability of meaning that we encourage the prediction to fit the given data.

We will model distribution of with Gaussian distribution (note that precision ). We can think that the distribution mean is , which is located in the point whose Gaussian probability is maximal (remember: Gaussian distribution has max value in the mean axis). Thus, by maximizing the probability, we encourage our prediction to fit the given data. Note that the mean is dependent variable of , meaning that it will has its own value for different input . Later, when we talk about “predictive distribution”, we will see that actually is our output prediction. As for variance , we can think it is the variance of our bias in our data. To sum up, probability in **univariate **Gaussian distribution that has different distribution in each certain point of axis x, where is the prediction of , and is a function of .

Given pair data training , we will do MLE to maximize our likelihood . Our likelihood will be:

As usual, we will bring to log form.

To maximize the likelihood, we will take first differential *w.r.t * and make it equals to zero.

Do you feel similar the last equation form? Yes, it is similar with what we do in linear regression using LSE here. In the linear regression with LSE, we maximize our cost function . When maximizing the function, we will make the first differential equals to zero. Thus, the constant will disappear in this case, whereas our corresponds to . Finally, we can prove these two equations to be maximized are same.

Up to this point, we successfully prove that linear regression using LSE equals to MLE in Bayesian method.

## (2) Regression using LSE with Regularization= MAP Bayesian?

Let’s try to do MAP Bayesian maximizing the posterior probability in linear regression. Given pair data training , we will try to estimate our regression parameter using Bayesian formula.

For likelihood, again, it is **univariate **Gaussian just like when we did in MLE above. As for prior, it is **multivariate** Gaussian, and we will use isotropic form (covariance matrix = scalar x identity matrix). We use *precision scalar * and *precision matrix* (precision is equal to ) instead of variance or covariance matrix to make the derivation easier. Our prior in this case is our prior knowledge of how is distributed. Ok, let’s proceed to the derivation. We will ignore marginal distribution since it’s only constant normalization.

Again, we will bring to log form.

To get the regression parameter that maximizes posterior probability, we will take first differential *w.r.t * and make it equals to zero. Here we go.

The last line of equation above is similar with our cost function in regression using LSE with regularization here, which is . Ok, let’s break down for more clear comparison.

Divide both side by , we get:

Since when maximizing we take first differential and make it equals to zero, thus, the constant value will disappear. And finally, we have:

where , and in this case corresponds to , which is the regression parameter. is just the matrix form of , and corresponds to , which our regularization weighting value. Thus, we prove that regression using LSE with regularization is equal to MAP in Bayesian regression.

## (3) Deriving posterior probability for Bayesian regression

After we can prove two things above, let’s proceed to derive the posterior for online learning. The last result, we have:

For one pair input data in every iteration in online learning, we will have single likelihood probability. Or in another word, the summation in the likelihood above will disappear. In this case, our posterior becomes as follows.

To get update parameters mean and variance, we can do “completing the square” technique like what we already did in deriving Gaussian online learning here, since product of two Gaussian will produce another new Gaussian. Let our new Gaussian .

Then, let’s do “completing the square” for the power term of both equations above by collecting those terms regarding coefficient of . Here we go.

For equation above, in coefficient, we can find our new precision matrix .

From our coefficient , we can derive .

For the clarity, let’s put the matrix form each of them. Just for example, when the polynomial order = 3. Thus:

Form above is for one pair data . For m-pairs data, we can use design matrix with . Thus, the updating parameters become:

* Congratulation!* Up to this point, we already get the parameters update of new mean and new precision matrix. By doing all derivations in this post, we also get the intuition how Bayesian method works, which is an important and potential framework in machine learning. FYI, some scientists are proud and believe that Bayesian framework will become powerful framework in machine learning that nowadays is dominated by deep learning. Maybe couple years later Bayesian will beat and replace deep learning? Or complement it? Let’s see 🙂

## (4) Frequentist vs Bayesian

In probability, generally, there are two types of reasoning approaches : frequentist and Bayesian. Frequentist has reasoning based on likelihood by doing MLE (Maximum Likelihood Estimation), whereas Bayesian has reasoning based on posterior by doing MAP (Maximum A Posteriori). Re-write again Bayesian formula, we can clearly see the difference mathematically.

Frequentist only maximizes likelihood to make prediction, whereas Bayesian maximizes posterior which uses prior knowledge . A simple example, if we have tossing coin : sequence 1 , sequence 2 , frequentist will argue that probability of T (tails) in sequence 2 , which only consider in that sequence, without considering prior (previous trials). Whereas, Bayesian will argue that the probability of T in sequence 2 , which also consider prior knowledge (previous trials).

In regression, frequentist will suffer overfitting. Because like we already prove above, frequentist which only use likelihood is equal to LSE regression without regularization. Whereas, Bayesian is less prone to be overvitting because it use prior knowledge, or in LSE regression point of view it uses regularization to avoid overfitting.

nice work…it reallty helped alot but finding it hard to run this on R-software

Thank you very much for the post. I have a question regarding the distribution of Y. How can we prove that Y is normally distributed? Because Y is not directly dependent on “e” and “x” is random and “Y” is directly dependent on “x” which might not be normally distributed.

Could you please explain?

Sincere regards,

Ganesh S K