# General Linear Model 3

先有点废话：

1. 当你有了模型以后，你该如何调整模型，这就是regularization，
2. 最后就是模型的cross validation

那如何保证不overfitting呢，因为这是最需要小心的，那很自然我们就会用到regularization

1. 理解model error 中bias 与variance的关系
2. 知道regularization方法试图解决的问题是什么
3. 了解hyper-parameter的概念，以及优化计算hyperparameter的方法——cross validation

## Model Evaluation

### 1. Overfitting

当你太关心一些细节，那反而在未知的情况下表现的不好。就像你考gre花大量时间在一道极难的问题上,会出现什么情况呢？

<figure><img src="https://1687157657-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FKLjYi16C9OsVCRdrTcrH%2Fuploads%2F6yfRBWHpTuc8OOdvUd3y%2FScreen%20Shot%202021-03-13%20at%209.11.36%20PM.png?alt=media&#x26;token=6494624b-c794-4c2b-aa06-09bc0fe87e0b" alt="" width="375"><figcaption></figcaption></figure>

* **机器学习的目的是为了构建一个模型，让这个模型尽可能的在未知数据上表现的好。**
* 表现好或者差的定义=$$\vert$$预测值-真实值$$\vert^2$$（差值的平方和），所以就是square error或者是mean squre error
* **overfitting的定义**：如果模型在训练数据上表现很好，反而在未知的数据表现的不太好，这就出于overfitting（这里是连接training和）
* 什么情况出现overfitting？

1. Q1: Is $$F(x)$$ the more complicated the better?

   When your model is too complicated, it may get overfitting.
2. Q2: In classification problems. are our features the more the better?（features多余你的data）
   1. 比如说你只有两个人的data，但是你用了一堆的feature，这就像你解方程的时候，未知数多余方程数
3. Fundamental causes of overfitting:

   Complicated Model; Limited learning data.

Loss 里的error：（手段）

* 针对**训练数据**：观察training data里面的$y$和模型预测的值之间的差别

预测error（**Model Error**）

* 针对未来的**未知数据**：未来的$y$和模型之间的差别

感想：

1. 同为一样，但是目的不同，前面是为了找parameter，后面是为了看对未知数据的效果；
2. 换句话说，这就是理想和现实的差距

### 2. Model Error

**Model error 的定义**

定义：是指未来未知的数据（test数据）上的error

$$
MSE=\frac{1}{n}\sum\_{i=1}^{n}(y\_i-f(x\_i))^2
$$

一点比较：model error 很像loss，但是

1. loss function 是用来构造模型；
2. model error 是用来检查模型效果的

**Model的部分（这个是关于你的training data）**

Expected error of regression models(bias+variance的推导)

$$
E(Error)=bias^2+variance+Irreducible
$$

Error的期望组成：（是model 的error）

1. **Bias**： measures how far off **in general** the models predictions are from the correct value
2. **Variance**: is how much the predictions for **a given point** vary between different realization of the model
3. (不重要)Irreducible error：the part that can not be reduced by optmizing the model

一个例子：

1. 当你数据不同的时候，loss function的parameter就会有些不同，所以bias是平均你的parameter

**一个类比（这个是training data???不是bias和variance中的$\hat{f}$和training有关系）**

* 学生的考试能力 = 你的平均水平（与这一次没有关系）+这一次的发挥来说对你多正常
* 模型的精准性bias：
  * 第一层次的理解：模型输出结果与真实值之间的差距。错误！！！
  * 第二层次的理解：这个**model在训练数据**有变化下的平均输出结果与真实值相比，得到的平均准确性
* 模型的稳定性variance：
  * 第一层次的理解：模型输出结果的稳定性
  * 第二层次的理解：’**某一次model的数据结果**与这次model的平均水平的差距‘的平方的期望

<figure><img src="https://1687157657-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FKLjYi16C9OsVCRdrTcrH%2Fuploads%2FSZoe7tF8lt2yxa7jYfrf%2FScreen%20Shot%202021-03-13%20at%209.47.02%20PM.png?alt=media&#x26;token=6722d241-25c6-41ee-9d93-91189cc3c54e" alt="" width="375"><figcaption></figcaption></figure>

Error due to Variance:

* The error due to variance is taken as the variability of a model prediction for **a given data point**
* Again, imagine you can repeat the entire model building process multiple times. The variance is how much the predictions for a given point vary between different realizations of the model.

Q：那你怎么从一个training的bias 和variance去看model本身的bias和variance呢？

Ans：这里的模型空间，会随着模型复杂度变大，很自然，你的variance增大，你的bias减小

重要事情：

<figure><img src="https://1687157657-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FKLjYi16C9OsVCRdrTcrH%2Fuploads%2FI5DFmnpxyxoBCr9pBhhp%2FScreen%20Shot%202021-03-13%20at%209.55.08%20PM.png?alt=media&#x26;token=82b62399-b76a-4050-8e83-dc55790c6840" alt="" width="375"><figcaption></figcaption></figure>

要回答overfitting和underfitting可以用上面这个图

* 第一部分，是指在testing data上的表现？就是上面的图？对的，注意纵坐标是testing error；

2. 第二部分，是symptoms是提到

要回答bias和variance可以用下面这个图（**这个是对training data的**， 就是上面的表格？对的因为在看你的model的参数，就是参数a和b）

例子：有个点（X，Y）它对应的下面四张图代表了四个模型，请判断他们的bias 和variance什么状态

​ 1. 这是你不同的model，不同的loss的parameter不一样，因为training data 不一样

<figure><img src="https://1687157657-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FKLjYi16C9OsVCRdrTcrH%2Fuploads%2F0KlDNXaMQg8FjzzCsb2H%2FScreen%20Shot%202021-03-13%20at%209.58.41%20PM.png?alt=media&#x26;token=84bc2e11-0584-4726-82a6-413aa82fb128" alt="" width="375"><figcaption></figcaption></figure>

### 3. How to solve the overfitting problem？

1. Increase training data size
2. avoid over-training your dataset:
   1. Filter out features, e.g. feature reduction

      Principal component analysis(PCA)
   2. **Regularization**

      **Ridge regression, Least absolute shrinkage and selction operation(LASSO)**

      **Logistic Regression-L2, Logistic Regression-L1**
   3. Ensemble Learning

## Regularization

情况：在你的testing data 上出现了overfitting的情况（直接在你的loss function上来治疗你的问题，所以这是在testing上增加的？）

所以我们要对overfitting做点事情

当$$f(x\_i)=ax\_i+b$$时：

$$
Loss= \sum\_{i=1}^{n}(y\_i-ax\_i-b)^2
$$

Loss特别小的时候，model error 会很大，因为什么？ var 大(相当于之前只和training有关系，但是我们需要和testing建立连接)

$$
Loss= \sum\_{i=1}^{n}(y\_i-ax\_i-b)^2+testing \ variance
$$

可以吗？不行。testing data 不知道，testing var 不知道

Model training process

$$
Loss= \sum\_{i=1}^{n}(y\_i-ax\_i-b)^2+间接描述\ testing \ \ variance
$$

例子：y=ax+b,a 和b已知 a=0.5, b= 85，相当于能连接training 和test的，间接描述testing的，只有这个parameter的函数了

$$
Loss= \sum\_{i=1}^{n}\[(y\_i-ax\_i-b)^2+w(a,b)]
$$

## Ridge Regression: L2 penalty in the loss function

**Ridge Regression** improves the loss function definition of Linear Regression by introducing variance into the formula

**Regularization** is nothing but adding a penalty term to the objective function and controlling the model complexity using that penalty term

**Training stage:**

$$
Loss=\sum\_{i=1}^{n}(y\_i-\beta\_0-\sum\_{j=1}^n \beta\_j x\_{ij})^2+\lambda\sum\_{j=1}^p\beta\_j^2
$$

$$MSE=\frac{1}{m}\sum\_{i=1}^{n}(y\_i-\beta\_0-\sum\_{j=1}^n \beta\_j x\_{ij})^2$$

Ridge regression 就是linear regression加上平方项的penalty term

1. 画出来像等高线
2. 后来可以推广到logistics, 所以就是一个是L1，另外一个就是L2

### LASSO: L1 penalty in the loss function

Lasso(Least absolute shrinkage and selection operator)

$$
Loss=\sum\_{i=1}^{n}(y\_i-\beta\_0-\sum\_{j=1}^n \beta\_j x\_{ij})^2+\lambda\sum\_{j=1}^p \vert \beta\_j \vert
$$

### Hyperparameter Optimization

这样的penalty公式中的$$\lambda$$ 就是hyperparameter：

1. $$\lambda$$可以让方差和偏差达到平衡：$$\lambda$$增大，模型方差（variance）减少，偏差增大（bias）
2. 相当于你在用$$\lambda$$来控制你的参数$$a$$和$$b$$

总结：

1. L1:：feature selection regularization
2. L2：是correlation的情况处理的比较好
3. model error 不存在training上，mse是用来测试机器好不好，所以不是不用Regularization
4. L1和L2一般就用在linear 和logistics上现在，不过regulizaration这个概念在其他地方都有

建模分两个部分，

实验室

1. 骨架：选择你的大的框架
2. 肉：就是你里面的parameter，min loss function就是填肉

面对世界

1. 做出来这个机器人到底行不行，
2. 这时候就要regularization（要解决overfitting（low bias/high variance），虽然已经在testing data上了，但是还是要回到training data 里面），所以是骨架，还是肉呢？
3. 用regularization的时候只是在training data里，目的是为了调整parameter
4. 从而当用于testing data 时候，我们就取消这个regularization的这个项对么？

Q：对应logistics

bias has a negative first-order derivative in response to model complexity while variance has a positive slope这个图的注解是这样的

Reference:

* <http://scott.fortmann-roe.com/docs/BiasVariance.html>
* <https://www.zhihu.com/question/27068705>
* book: pattern recognition？

## SVM

1.refer to your publications

$$E((w^Tx+b,0)\_{\max})$$?

2. Maximum margin classifier

   $$\max \frac{1}{\vert \vert w \vert \vert},\qquad s.t. y\_i(w^Tx\_i+b)\geq 0,\qquad i=1,\cdots,n$$
3. $$\min \frac{1}{2}\vert\vert w \vert\vert^2,\qquad s.t. y\_i(w^Tx\_i+b)\geq 1,\qquad i=1,\cdots,n$$

   Dual $$\mathcal{L}(w,b,a)=\frac{1}{2}\vert \vert w\vert \vert^2-\sum\_{i=1}^n \alpha\_i(y\_i(w^Tx\_i+b)-1)$$变形后

   $$\mathcal{L}(w,b,a)=\sum\_{i=1}^n\alpha\_i-\frac{1}{2}\alpha\_i\alpha\_jy\_i y\_jx\_i^T x\_j$$And $$w=\sum\_{i=1}^n\alpha\_iy\_ix\_i$$

   dual problem
4. 换成核估计

   $$f(x)=\sum\_{i=1}^Nw\_i\phi\_i(x)+b$$ 转换成为 $$f(x)=\sum\_{i=1}^l\alpha\_i y\_i \langle \phi(x\_i), \phi(x) \rangle+b$$

$$\alpha$$ 可以由dual 来求

$$\max\_{\alpha}\sum\_{i=1}^n \alpha\_i-\frac{1}{2}\sum\_{i,j=1}^n\alpha\_i\alpha\_jy\_iy\_j\langle \phi(x\_i)\phi(x\_j)\rangle \qquad s.t. \alpha\_i \geq 0,i=1,\cdots, n; \sum\_{i=1}^n\alpha\_iy\_i=0$$

$$\max\_{\alpha}\sum\_{i=1}^n \alpha\_i-\frac{1}{2}\sum\_{i,j=1}^n\alpha\_i\alpha\_jy\_iy\_jx\_ix\_j \qquad s.t. \alpha\_i \geq 0,i=1,\cdots, n; \sum\_{i=1}^n\alpha\_iy\_i=0$$
