# 6.2 回归任务

线性回归的任务：

* **输入**：N个<mark style="color:orange;">**独立同分布（i.i.d）**</mark>的训练样本$$(\mathbf{x}^i,y^i)\in X\times R$$，$$i=1,2,\dots,N$$
* **目标函数**：$$f\in \mathcal{F}$$
* **损失函数**：$$L(f;x,y) = (f(x)-y)^2$$
* **期望风险**：$$\int (f(x)-y)^2dP(x,y)$$

## 6.2.1 最小均方误差（LMS）

{% hint style="warning" %}
最小均方误差在分类上属于判别函数
{% endhint %}

当$$f$$是线性函数，则最优化问题为：

$$
\min\_\limits{\mathbf{w}} J(\mathbf{w}) = \sum\_{i=1}^N(\mathbf{w}^T\mathbf{x}^i - y^i)^2
$$

也就是最小化<mark style="color:purple;">**经验风险**</mark>，在这里即为<mark style="color:orange;">**最小二乘/均方误差**</mark>

### 批梯度下降

对于上述最优化问题，采用梯度下降法进行更新，梯度为

$$
\frac{\partial J(\mathbf{w})}{\partial w\_j} = 2\sum\_{i=1}^Nx\_j^i(\mathbf{w}^T\mathbf{x}^i - y^i)
$$

对于<mark style="color:purple;">**批梯度下降法（BGD）**</mark>，更新规则为：

$$
w\_j = w\_j - 2\alpha\sum\_{i=1}^Nx\_j^i(\mathbf{w}^T\mathbf{x}^i - y^i),\ \alpha>0
$$

这里$$\alpha$$为<mark style="color:orange;">**学习率**</mark>

* **优点**
  * 一次迭代是对所有样本进行计算，此时利用矩阵进行操作，实现了并行
  * 由全数据集确定的方向能够更好地代表样本总体，从而更准确地朝向极值所在的方向。当目标函数为凸函数时，BGD**一定能够得到全局最优**
* **缺点**
  * 当样本数目N很大时，每迭代一步都需要对所有样本计算，训练过程会很慢

### 随机梯度下降

对于批梯度下降的缺点，随机梯度下降采用了不同的更新规则：

$$
w\_j = w\_j - 2\alpha (\mathbf{w}^T\mathbf{x}^i - y^i)x\_j^i,\ \alpha>0
$$

也可以写作：

$$
\mathbf{w} = \mathbf{w} - 2\alpha \mathbf{X}^T\mathbf{b} \ \mathbf{b}=(b\_1, b\_2,\dots,b\_N)^T\ \text{where}\ b\_i = \mathbf{w}^T\mathbf{x}^i - y^i
$$

区别在于，<mark style="color:purple;">**随机梯度下降（SGD）**</mark>每次迭代仅针对一个样本进行，而不像BGD每次对所有样本进行训练

## 6.2.2 广义线性回归

利用非线性基进行线性回归的思路就是对非线性基进行线性组合：

$$
f(\mathbf{w},\mathbf{x}) = w\_0+ \sum\_{j=1}^Kw\_j\phi\_j(\mathbf{x}) \ 其中\ \Phi=(1,\phi\_1,\dots,\phi\_K)
$$

### 常见的非线性基函数

* 多项式基函数

$$
\phi(\mathbf{x}) = (1,x,x^2,\dots,x^K)
$$

* 高斯函数

$$
\phi\_j(\mathbf{x}) = \exp\left(-\frac{(x-\mu\_j)^2}{2s^2}\right)
$$

* Sigmoid函数

$$
\phi\_j(\mathbf{x}) = \sigma\left(\frac{x-\mu\_j}{s}\right) \ \sigma(a) = \frac{1}{1+\exp(-a)}
$$

### 广义线性回归的闭式解

**最优化问题**：

$$
\min\limits\_w J(\mathbf{w}) = \sum\_{i=1}^N(\mathbf{w}^T\phi(\mathbf{x}^i) - y^i)^2
$$

**梯度**：

$$
\frac{\partial J(\mathbf{w})}{\partial w\_j} = 2\sum\_{i=1}^N\phi\_j(\mathbf{x}^i)(\mathbf{w}^T\phi(\mathbf{w^i})-y^i)
$$

**闭式解**：

$$
\mathbf{w}^\* = (\Phi^T\Phi)^{-1}\Phi^T\mathbf{y}
$$

其中，

$$
\begin{align} \Phi &= \begin{pmatrix} \phi\_0(\mathbf{x}^1) & \dots & \phi\_k(\mathbf{x}^1) \ \vdots & \vdots & \vdots \ \phi\_0(\mathbf{x}^N) & \dots & \phi\_k(\mathbf{x}^N) \end{pmatrix} \nonumber \ \ \mathbf{y} &= (y^1,\dots,y^N)^T \nonumber \end{align}
$$

## 6.2.3 最大似然估计（MLE）

{% hint style="warning" %}
最大似然估计在分类上属于判别式模型
{% endhint %}

假设y是具有加性高斯噪声的确定函数$$f$$给出的标量，即$$y=f(\mathbf{x},\mathbf{w})+\varepsilon$$，$$\varepsilon$$是均值为0，方差为$$\beta^{-1}$$的高斯噪声

![](/files/9QyHdiueggnOFSjAbTqC)

**训练数据**：$$(\mathbf{x}^i,y^i)$$，$$i=1,2,\dots,N$$

<mark style="color:purple;">**似然函数**</mark>：

$$
\begin{align} p(y\vert \mathbf{x},\mathbf{w},\beta^{-1}) &= \mathcal{N}(y\vert f(\mathbf{x},\mathbf{w}),\beta^{-1}) \nonumber \ &= \prod\_{i=1}^N \mathcal{N}(y^i\vert \mathbf{w}^T\mathbf{x}^i,\beta^{-1}) \end{align}
$$

<mark style="color:purple;">**对数似然函数**</mark>：

$$
\sum\_{i=1}^N\ln \mathcal{N}(y^i\vert \mathbf{w}^T\mathbf{x}^i,\beta^{-1}) = \frac{N}{2}\ln\beta-\frac{N}{2}\ln2\pi-\frac{1}{2}\beta J(\mathbf{w})
$$

其中，$$J(\mathbf{w}) = \sum\limits\_{i=1}^N(\mathbf{w}^T\mathbf{x}^i - y^i)^2$$

{% hint style="success" %}
**结论**：在高斯噪声模型下，<mark style="color:red;">**最大化似然相当于最小化平方误差之和**</mark>
{% endhint %}

最小二乘法实际上是在假设误差项满足高斯分布且独立同分布情况下，使似然性最大化。

## 6.2.4 最大化后验概率（MAP）

{% hint style="warning" %}
最大化后验概率在分类上属于生成式模型
{% endhint %}

* 采用<mark style="color:orange;">**正则项**</mark>的LMS问题：

$$
\min\limits\_w \sum\_{i=1}^N(\mathbf{w}^T\mathbf{x}^i-y^i)^2+\lambda\mathbf{w}^T\mathbf{w}
$$

* **闭式解**

$$
\mathbf{w}^\* = (\Phi^T\Phi+\lambda \mathbf{I})^{-1}\Phi^T\mathbf{y}
$$

* **似然函数**

$$
p(\mathbf{y}\vert\mathbf{X},\mathbf{w},\beta)=\prod\_{i=1}^N\mathcal{N}(y^i\vert \mathbf{w}^T\mathbf{x}^i,\beta^{-1})
$$

接下来假设参数的<mark style="color:purple;">**先验概率**</mark>为<mark style="color:orange;">**多变量高斯分布**</mark>：

$$
p(\mathbf{w}) = \mathcal{N}(0,\mathbf{\alpha}^{-1}\mathbf{I})
$$

这是因为根据贝叶斯公式，需要求似然与先验的联合分布，因此先验必须与似然同分布才能继续求解，则根据贝叶斯公式：

$$
p(\mathbf{w}\vert\mathbf{y})=\frac{p(\mathbf{y}\vert\mathbf{X},\mathbf{w},\beta)p(\mathbf{w})}{p(\mathbf{y})}
$$

后验概率依然是高斯分布，对其取对数得：

$$
\ln(p(\mathbf{w}\vert\mathbf{y}))=-\beta\sum\_{i=1}^N(y^i-\mathbf{w}^T\mathbf{x}^i)^2-\lambda\mathbf{w}^T\mathbf{w} + C
$$

因此，<mark style="color:red;">**最大化后验等同于最小化带有正则项的平方和误差**</mark>

## 6.2.5 MLE与MAP的比较

* MLE是判别式模型，其先验为一常数

$$
\hat\theta\_{MLE} = \arg \max\limits\_\theta P(D\vert\theta)
$$

* MAP是产生式模型

$$
\begin{align} \hat\theta\_{MAP} &= \arg\max\limits\_\theta P(\theta\vert D) \ &= \arg\max\limits\_\theta P(D\vert \theta)P(\theta) \end{align}
$$

* MLE是频率学派的想法，MAP是贝叶斯学派的想法
* 更多的数据会使得MLE拟合更好，但容易出现过拟合


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://aye10032.gitbook.io/prml-note/di-liu-zhang-you-jian-du-xue-xi/6.2-hui-gui-ren-wu.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
