Linear Regression and Maximum Likelihood Estimation

Imagination is an outcome of what you learned. If you can imagine the world, that means you have learned what the world is about.

Actually we don't know how we see, at lease it's really hard to know, so we can't program to tell a machine to see.

One of the most important part in machine learning is to introspect how our brain learn by subconscious. If we can't introspect, it can be fairly hard to replicate a brain.

Linear Models

Supervised learning of linear models can be divided into 2 phases:

  • Training:

    1. Read training data points with labels {x1:n,y1:n}, where xiR1×d, yiR1×c;
    2. Estimate model parameters θ^ by certain learning Algorithms.
      Note: The parameters are the information the model learned from data.
  • Prediction:

    1. Read a new data point without label xn+1 (typically has never seen before);
    2. Along with parameter θ^, estimate unknown label y^n+1.

1-D example:
First of all, we create a linear model:

y^i=θ0+θ1xi

Both x and y are scalars in this case.
Slides by Nando de Freitas from CPSC540, UBC

Then we, for example, take SSE (Sum of Squared Error) as our objective / loss / cost / energy / error function:

J(θ)=i=1n(y^iyi)2

Linear Prediction Model

In general, each data point xi should have d dimensions, and the corresponding number of parameters should be (d+1).

The mathematical form of linear model is:

y^i=j=0dθjxij

The matrix form of linear model is:

[y^1y^2y^n]=[1x11x12x1d1x21x22x2d1xn1xn2xnd][θ0θ1θ2θd]

Or in a more compact way:

y^=Xθ

Note that the matrix form is widely used not only because it's a concise way to represent the model, but is also straightforward for coding in MatLab or Python (Numpy).

Optimization Approach

In order to optimize the model prediction, we need to minimize the quadratic cost:

J(θ)=i=1n(y^iyi)2=(yXθ)T(yXθ)

by setting the derivatives w.r.t vector θ to zero since the cost function is strictly convex and the domain of θ is convex.

Slides by Nando de Freitas from CPSC540, UBC

J(θ)θ=θ(yXθ)T(yXθ)=θ(yTy+θTXTXθ2yTXθ)=0+2(XTX)Tθ2(yTX)T=2(XTX)θ2(XTy)0

So we get θ^ as an analytical solution:

θ^=(XTX)1(XTy)

After passing by these procedures, we can see that learning is just about to adjust model parameters so as to minimize the objective function.
Thus, the prediction function can be rewrite as:

y^=Xθ^=X(XTX)1XTyHy

where H refers to hat matrix because it added hat to y

Multidimensional Label yi

So far we have been assuming yi to be a scalar. But what if the model have multiple outputs (e.g. c outputs)? Simply align with c parameters:

[y11y1cy21y2cyn1ync]=[1x11x12x1d1x21x22x2d1xn1xn2xnd][θ01θ0cθ11θ1cθ21θ2cθd1θdc]

Linear Regression with Maximum Likelihood

If we assume that each label yi is Gaussian distributed with mean xiTθ and variance σ2:

yiN(xiTθ,σ2)=(2πσ2)1/2e(yixiTθ)22σ2

Likelihood

With a reasonable i.i.d. assumption over y, we can decompose the joint distribution of likelihood:

p(y|X,θ,σ2)=i=1np(yi|xi,θ,σ2)=i=1n(2πσ2)1/2e(yixiTθ)22σ2=(2πσ2)n/2ei=1n(yixiTθ)22σ2=(2πσ2)n/2e(yXθ)T(yXθ)2σ2

Maximum Likelihood Estimation

Then our goal is to maximize the probability of the label in our Gaussian linear regression model w.r.t. θ and σ.
Slides by Nando de Freitas from CPSC540, UBC
Instead of minimizing the cost function SSE (length of blue lines), this time we maximize likelihood (length of green lines) to optimize the model parameters.

Since log function is monotonic and can simplify exponent function, here we utilize log-likelihood:

logp(y|X,θ,σ2)=n2log(2πσ2)(yXθ)T(yXθ)2σ2

MLE of θ:

logp(y|X,θ,σ2)θ=θ[n2log(2πσ2)(yXθ)T(yXθ)2σ2]=012σ2(yXθ)T(yXθ)θ=12σ2(yTy+θTXTXθ2yTXθ)θ=12σ2[0+2(XTX)Tθ2(yTX)T]=12σ2[2XTXθ2XTy]0

There's no surprise that the estimation of maximum likelihood is identical to that of least-square method.

θ^MLE=(XTX)1XTy

Besides where the "line" is, using MLE with Gaussian will give us the uncertainty, or confidence as another parameter, of the prediction y^
MLE of σ2:

logp(y|X,θ,σ2)σ=σ[n2log(2πσ2)(yXθ)T(yXθ)2σ2]=n212πσ24πσ+2(yXθ)T(yXθ)2σ3=nσ+(yXθ)T(yXθ)σ30

Thus, we get:

σ^MLE2=1n(yXθ)T(yXθ)=1ni=1n(yixiTθ)2

which is the standard estimate of variance, or mean squared error (MSE).
However, this uncertainty estimator does not work very well. We'll see another uncertainty estimator later that is very powerful.

Again, we analytically obtain the optimal parameters for the model to describe labeled data points.

Prediction

Since we have had the optimal parameters (θMLE,σMLE2) of our linear regression model, making prediction is simply get the mean of the Gaussian given different test data point x:

y^=xTθMLE

with uncertainty σMLE2.

Frequentist Learning

Maximum Likelihood Learning is part of frequentist learning.

Frequentist learning assumes there is a truth (true model) of parameter θtruth that if we had adequate data, we would be able to recover that truth. The core of learning in this case is to guess / estimate / learn the parameter θ^ w.r.t. the true model given finite number of training data.

Maximum likelihood is essentially trying to approximate model parameter θtruth by maximizing likelihood (joint probability of data given parameter), i.e.

Given n data points X=[x1,,xn] with corresponding labels y=[y1,,yn], we choose the value of model parameter θ that is most probable to generate such data points.
Slides by Nando de Freitas from CPSC540, UBC

Also note that frequentist learning relies on Law of Large Numbers.

KL Divergence and MLE

Given i.i.d assumption on data X from distribution p(X|θtrue):

p(X|θtrue)=i=1np(xi|θtrue)θMLE=argmaxθi=1np(xi|θ)=argmaxθi=1nlogp(xi|θ)

Then we add a constant value i=1nlogp(xi|θtrue) onto the equation and then divide by the constant number n:

θMLE=argmaxθ1ni=1nlogp(xi|θ)1ni=1nlogp(xi|θtrue)=argmaxθ1nlogp(xi|θ)p(xi|θtrue)

Recall Law of Large Numbers that is: as n,

1ni=1nxixp(x)dx=E[x]

where xi is simulated from p(x)

Again, we know from frequentist learning that data point xip(x|θ). Hence, as n goes , the MLE of θ becomes

θMLE=argmaxθxlogp(x|θ)p(x|θtrue)p(x|θtrue)dx=argminθxlogp(x|θtrue)p(x|θ)p(x|θtrue)dx=argminθ Ep(x|θtrue)[logp(x|θtrue)p(x|θ)]=argminθ KL[p(x|θtrue) || p(x|θ)]

Therefore, maximizing likelihood is equivalent to minimizing KL divergence.

Entropy and MLE

In the last part, we get

θMLE=argminθxlogp(x|θtrue)p(x|θ)p(x|θtrue)dx=argminθxlogp(x|θtrue)p(x|θtrue)dxxlogp(x|θ)p(x|θtrue)dx

The first integral in the equation above is negative entropy w.r.t. true parameter θtrue, i.e. information in the world , while the second integral is negative cross entropy w.r.t. model parameter θ and true parameter θtrue., i.e. information from model. The equation says, if the information in the world matches information from model, then the model has learned!

Statistical Quantities of Frequentist Learning

There are 2 quantities that frequentist often estimate:

  • bias
  • variance

Refer: CPSC540, UBC
Written with StackEdit.


  1. SSE is known by everyone but works poorly under certain circumstances e.g. if the training data contains some noise (outliers) then the model will be distorted seriously by outliers. ↩︎

  2. See one of some interesting explanations here ↩︎

posted @   LexLuc  阅读(748)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)
点击右上角即可分享
微信分享提示