Regression
Bayesian Linear Regression
2023-12-14
The usual methodology for linear regression is to find the regression coefficients that minimize the distance between the regression equation and the samples. However, this approach can lead to overfitting when the number of samples is small, and it does not provide a measure of uncertainty for the obtained regression coefficients. On the other hand, Bayesian regression is a method that finds the coefficients by maximizing the likelihood from the prior knowledge (prior) and the likelihood from the sample data. It provides statistical values such as the mean and variance for the coefficients. Mathematically, both methodologies essentially discuss the same concept from different perspectives.
First, let's take a brief look at Bayes' theorem. In the figure below, the probability of (A ∩ B) is the conditional probability of B given A, which is equal to the conditional probability of A given B.

P(A ∩ B)=P(B ∩ A)
P(A ∩ B)=P(B|A)/P(A)
P(B ∩ A)=P(A|B)/P(B)
From the above two equations, we can get the following two conditional probability equations:
P(A|B) = P(B|A) P(A)/P(B)
P(B|A) = P(A|B) P(B)/P(A).
That is, Bayes' theorem can be summarized as follows: the probability of A under the condition B (posterior probability) can be expressed as the product of the probability of A itself (prior probability) and the probability of B occurring given the condition A (likelihood)."
P(A|B) = P(B|A) * P(A) / P(B)
Posterior probability = likelihood * Prior probability/ marginal likelihood
P(B) here is the sum of the conditional probabilities of B occurring for all possible conditions of A as expressed below:
P(B) = P(B|A) P(A) + P(B|A') P(A').
Here, P(A') = 1-P(A)
The probability of event A occurring and the probability of event B occurring are independent, so they are proportional to each other, expressed as:
P(A|B) ∝ P(B|A) * P(A)
From the perspective of regression, we still need to find the coefficients w that satisfy the equation f(X) = Xw. To do this, the Bayesian formula is expressed as follows.
\(p(w|y,X)=\dfrac{p(y|X,w)p(w)}{p(y|X)}\)
First, let's define the error, ε, included in the measurement y as y = f(X) + ε. Assuming that ε is the Gaussian noise follows a Gaussian distribution with mean 0 and variance σ2, denoted as ε ~ N(0,σ2)
(1) The probability p(w|y,X) represents the posterior probability of w given the data y and X, assuming a normal distribution ~ N(μ,Λ).
(2) The p(y|X,w) represents the likelihood of the predicted value y given the inputs X and parameters w. It is calculated as the product of the probabilities for each data point's value. In other words,
\(p(y|X,w)=\prod _i^{\ }p(y_i|X_i,w)=p(y_1|X_1,w)p(y_2|X_2,w)p(y_3|X_3,w)\dots p(y_n,X_n,w)\)
\(=\dfrac{1}{(\sqrt{2\pi }\sigma)^n}\exp \dfrac{-(y_1-X_1w)^2}{2\sigma^2}\exp \dfrac{-(y_2-X_2w)^2}{2\sigma^2}\dots \exp \dfrac{-(y_n-X_nw)^2}{2\sigma^2}\)
\(=\dfrac{1}{(2\pi \sigma^2)^{n/2}}\exp \dfrac{-(y-Xw)^T(y-Xw)}{2\sigma^2}=N(Xw,\sigma^2I)\)
(3) p(w) is the prior probability for w, and its actual value is unknown, so it is assumed to follow a normal distribution with mean 0 and variance ∑, denoted as w ~ N(0,∑). Here, ∑ is a diagonal matrix representing the dispersion for each coefficient.
\(p(w)=\dfrac{1}{\sqrt{2\pi }\Sigma}\exp (\dfrac{-w^T\Sigma^{-1}w}{2})\)
(4) \(p(y|X) = \int p(y|X,w)p(w)dw\) is a term for normalization by the sum of the probabilities possible for all w.
Discerning that p(w|y,X) is proportional to p(y|X,w)p(w) ,
p(w|y,X) ∝ p(y|X,w)p(w)
the w which maximizes the following form of p(w|y,X) is
\(p(w|y,X)\propto \exp -\dfrac{(y-Xw)^T(y-Xw)}{2\sigma^2}\exp -\dfrac{w^T\Sigma^{-1}w}{2}\)
\(\propto \exp -\dfrac{(y-Xw)^T(y-Xw)/\sigma^2+w^T\Sigma^{-1}w}{2}\)
identical with the one which maximizes the log likelihood of p(w|y,X)
\(\max _w^{ }\log p(w|y,X)=\max _w^{ }\ -\dfrac{(y-Xw)^T(y-Xw)/\sigma^2+w^T\Sigma^{-1}w}{2}\)
and again same with the one who minimizes the negative log likelihood.
\(\min _w^{ } \left\{(y-Xw)^T(y-Xw)+\sigma^2\Sigma^{-1}w^Tw\right\}\)
At the ends, it results in the same as Ridge regression that uses L2 regulation to avoid any overfitting.
Rearrange the likelihood to find the distribution of w as follows:
\(p(w|y,X)\propto \exp -\dfrac{(y-Xw)^T(y-Xw)/\sigma^2+w^T\Sigma^{-1}w}{2}\)
\(=\exp -\dfrac{y^Ty}{2\sigma^2}\exp -\left\{\ \dfrac{-w^TX^Ty}{\sigma^2}+\dfrac{w^TX^TXw}{2\sigma^2}+\dfrac{w^T\Sigma^{-1}w}{2}\right\}\)
\(\propto \exp -\left\{\dfrac{w^T(X^TX/\sigma^2+\Sigma^{-1})w}{2}-\dfrac{w^TX^Ty}{\sigma^2}\right\}\)
\(\Rightarrow \exp -\left\{\dfrac{w^TAw}{2}-w^Tb\right\}\)
Here, we define A and B as follows:
\(A\equiv X^TX/\sigma^2+\Sigma^{-1}\)
\(b\equiv X^Ty/\sigma^2\)
If we assume that the posterior distribution of the probability w is a multivariate normal distribution N(μ,Λ) (which is in tensor form since it has more than one coefficient), then the likelihood p(w|y,X) becomes
\(p(w|y,X)=\dfrac{1}{(2\pi )^{k/2}|\Lambda|^{1/2}}\exp -\dfrac{(w-\mu)^T\Lambda^{-1}(w-\mu)}{2}\)
\(\propto \exp -\dfrac{(w-\mu)^T\Lambda^{-1}(w-\mu)}{2}=\exp -\left\{\dfrac{w^T\Lambda^{-1}w}{2}-w^T\Lambda^{-1}\mu+\dfrac{\mu^T\Lambda^{-1}\mu}{2}\right\}\)
\(\propto \exp -\left\{\dfrac{w^T\Lambda^{-1}w}{2}-w^T\Lambda^{-1}\mu\right\}\exp -\dfrac{\mu^T\Lambda^{-1}\mu}{2}\).
Since the last term is a constant independent from w, the following relationship holds
\(p(w|y,X) \propto \exp -\left\{\dfrac{w^T\Lambda^{-1}w}{2}-w^T\Lambda^{-1}\mu\right\}\).
Now we can define A and b by comparing above two equations as follows:
\(\Lambda^{-1}=A=X^TX/\sigma^2+\Sigma^{-1}\Rightarrow \Lambda=A^{-1}=\sigma^2(X^TX+\sigma^2\Sigma^{-1})^{-1}\)
\(\Lambda^{-1}\mu=b=X^Ty/\sigma^2\Rightarrow \mu=A^{-1}X^Ty/\sigma^2=(X^TX+\sigma^2\Sigma^{-1})^{-1}X^Ty\)
The posterior probability distribution p(w|y,X) ultimately becomes a Gaussian distribution with a mean of A-1XTy/σ2 and a variance of A-1,
\(p(w|y,X)\sim N(A^{-1}X^Ty/\sigma^2,A^{-1})\)
The mean value of the posterior distribution of w can be seen to be the result of minimizing the objective function above.
\(\dfrac{\partial(\log p(w|y,X))}{\partial w}=0=-2X^Ty/\sigma^2+2X^TXw/\sigma^2+2\Sigma^{-1}w\)
\(w^*=X^Ty/\sigma^2(X^TX/\sigma^2+\Sigma^{-1})^{-1}=(X^TX+\sigma^2\Sigma^{-1})^{-1}X^Ty\equiv \mu\)
In order to get an inference to a new data \(x^*\), we use the conditional distribution and posterior distribution of w as below:
\(p(f^{\ast }|x^{\ast },X,y)=p(f^{\ast }|x^{\ast },w)p(w|X,y)dw\)
\(p(y^*|X^*,X,y)=\int _{\ }^{\ }p(y^*|X^*,w)p(w|X,y)dw=\int _{\ }^{\ }N(f(X^*),\sigma^2)N(\mu,\Lambda)dw\)
\(=\int _{\ }^{\ }\dfrac{1}{\sqrt{2\pi \sigma^2}}\dfrac{1}{\sqrt{2\pi \Lambda}}\exp -\left\{\dfrac{(y^*-X^*w)^T(y^*-X^*w)/\sigma^2+(w-\mu)^T\Lambda^{-1}(w-\mu)}{2}\right\}dw\)
\(\equiv N(y^*|X^{*T}\mu,X^{*T}A^{-1}X^*)\)
and the inference results in a distribution with the mean of X*Tμ, the variance of X*TA-1X* .
For the illustration of the Bayesian Linear Regression in an Excel worksheet, we use the following multivariate 3 pairs of sample data with the below assumptions for the Gaussian noisy, ε, included in the measurement, y, and a prior distribution of w:
\(\epsilon \sim N(0,\sigma^2=1)\)
\(w \sim N(0,\Sigma=\begin{bmatrix} 1 & 0 \\ 0 & 1\end{bmatrix})\).

Following are the excel functions to find the posterior distribution of w ~ N(Wbar, Σ) and the distribution of the inference \(Y^* \sim N(\overline {Y^*},\sigma_{Y*}^2)\)
XTX = MMULT(TRANSPOSE(X),X) [2x2]
XTY = MMULT(TRANSPOSE(X),Y) [2x1]
A = XTX/σ2+Σ-1 [2x2]
A-1 = MINVERSE(A) [2x2]
Wbar=MMULT(XTY,A-1)/σ2 [2x1]

\(\overline {Y^*}\) = MMULT(X*,Wbar)
σY* = SQRT(MMULT(X*,MMULT(A-1,TRANSPOSE(X*)))
The table and plot below show +/- 2sigma "Credible Interval" for the new dataset.

