# 最小二乘直线拟合

2022-04-14 14:31:56  阅读：55  来源： 互联网

## 推导过程

### 问题

$S_\epsilon^2=\sum_{i=1}^{n}(y_i-y)^2 = \sum_{i=1}^{n}(y_i - (ax_i+b))^2$

### 多元函数的极值与最值问题

\begin{aligned} \frac{\partial }{\partial a} S_\epsilon^2 = \frac{\partial }{\partial a} \sum_{i=1}^{n}(y_i - (ax_i+b))^2 = 0 \newline \frac{\partial }{\partial b} S_\epsilon^2 = \frac{\partial }{\partial b} \sum_{i=1}^{n}(y_i - (ax_i+b))^2 = 0 \end{aligned}

### 求偏导过程

#### 加减规则

$\frac{\partial }{\partial b} \sum_{i=1}^{n}(y_i - (ax_i+b))^2 = \frac{\partial }{\partial b}[(y_0 - (ax_0+b))^2 ] + \frac{\partial }{\partial b}[(y_1 - (ax_1+b))^2 ] + ... + \frac{\partial }{\partial b} [(y_n - (ax_n+b))^2]$

#### 乘法规则

$\frac{\partial }{\partial b} [(y_i - (ax_i+b))^2] = \frac{\partial [y_i - (ax_i+b)]}{\partial b} *(y_i - (ax_i+b)) + (y_i - (ax_i+b)) * \frac{\partial (y_i - (ax_i+b))}{\partial b} = 2 (y_i - (ax_i+b)) * \frac{\partial {(y_i - (ax_i+b))}}{\partial b}$

#### 常量导数是0，一元函数导数是斜率

$\frac{\partial {(y_i - (ax_i+b))}}{\partial b} = \frac{\partial y_i}{\partial b} - \frac{\partial ax_i}{\partial b} - \frac{\partial b}{\partial b} = 0 - 0 - 1 = -1$

$\frac{\partial {(y_i - (ax_i+b))}}{\partial a} = \frac{\partial y_i}{\partial a} - \frac{\partial ax_i}{\partial a} - \frac{\partial b}{\partial a} = 0 - x_i - 0 = -x_i$

#### 基于上面的推理，我们可以得出

\begin{aligned} \frac{\partial }{\partial a} S_\epsilon^2 = \sum_{i=1}^{n}(y_i - (ax_i+b))*(-x_i) = 0 \newline \frac{\partial }{\partial b} S_\epsilon^2 = \sum_{i=1}^{n}(y_i - (ax_i+b))*(-1) = 0 \end{aligned}

## 求 $$a,b$$ 的值

\begin{aligned} (\sum_{i=1}^{n}x_i^2)* a + (\sum_{i=1}^{n}x_i)*b = \sum_{i=1}^{n}x_i*y_i \newline (\sum_{i=1}^{n}x_i)* a + n*b = \sum_{i=1}^{n}y_i \end{aligned}

\begin{aligned} a = \frac {n*(\sum x_iy_i) - (\sum x_i)*(\sum y_i) } {n * (\sum x_i^2) - (\sum x_i)^2} \newline b = \frac {(\sum x_i^2)*(\sum y_i) - (\sum x_i)*(\sum x_i y_i) } {n * (\sum x_i^2) - (\sum x_i)^2} \end{aligned}

\begin{aligned} t1 = \sum x_i \newline t2 = \sum y_i \newline t3 = \sum x_iy_i \newline t4 = \sum x_i^2 \newline a = \frac { n*t3 - t1*t2} {n*t4 - t1^2} \newline b = \frac { t4*t2 - t1* t3 } {n*t4 - t1^2} \end{aligned}