# 曲线拟合

曲线拟合是构造一条曲线或一个数学函数，使之最佳地拟合一系列数据点，通常的做法是调整所选模型函数的参数，使曲线与数据之间某种偏差度量——最常见的是残差平方和——尽可能小。做这件事的主流方法是最小二乘法，由阿德里安-马里·勒让德（Adrien-Marie Legendre）于 1805 年首次发表；如今曲线拟合已涵盖线性回归与非线性回归、加权拟合、平滑样条，以及能抵抗离群点的稳健变体。<sup>[1](https://ekonometria.org/en/podstawy/mnk-gauss/)</sup>

| 关键事实 | 说明 |
|---|---|
| 核心准则 | 最小二乘法使观测值与模型之间的误差平方和最小；平方运算消去了误差的正负号，并使大偏差受到超过比例的惩罚<sup>[2](https://atlasofengineering.com/engineering-physics/gauss-legendre-and-the-history-of-least-squares/)</sup> |
| 首次发表 | 勒让德《确定彗星轨道的新方法》（*Nouvelles méthodes pour la détermination des orbites des comètes*）一书的附录《论最小二乘方法》（"Sur la méthode des moindres quarrés"），巴黎，1805 年<sup>[1](https://ekonometria.org/en/podstawy/mnk-gauss/)</sup> |
| 优先权之争 | 高斯于 1809 年声明，他自 1795 年起就已使用这一原理，但首先发表的是勒让德；1820 年勒让德就优先权问题攻击高斯<sup>[3](https://en.wikisource.org/wiki/A_Source_Book_in_Mathematics/Legendre_on_Least_Squares)</sup><sup> • </sup><sup>[4](https://www.sciencedirect.com/science/article/pii/0315086077900325)</sup> |
| 线性与非线性 | 线性最小二乘估计总可以解析地求得；非线性模型则需要带初值的迭代优化<sup>[5](https://www.itl.nist.gov/div898/handbook/pmd/section1/pmd142.htm)</sup> |
| 加权拟合 | 当误差方差不恒定时，取与各水平上的方差成反比的权重，可得到最精确的参数估计<sup>[6](https://www.itl.nist.gov/div898/handbook/pmd/section1/pmd143.htm)</sup> |
| 验证 | 高 R² 并不保证拟合良好；图解残差分析是检验模型是否充分的主要工具<sup>[7](https://itl.nist.gov/div898/handbook/pmd/section4/pmd44.htm)</sup> |
| 稳健替代方法 | Scikit-learn 提供三种稳健回归估计器——RANSAC、Theil Sen 和 HuberRegressor——用于处理被离群点污染的数据<sup>[8](https://scikit-learn.org/stable/modules/linear_model.html)</sup> |

## 最小二乘准则

设模型函数为 \( f(\vec{x};\vec{\beta}) \)，其中参数 \( \beta_0, \beta_1, \ldots \) 未知，曲线拟合要找出使观测响应与模型之间的偏差最小的那组参数取值。其基本形式把数据描述为

\[ y = f(\vec{x};\vec{\beta}) + \varepsilon, \]

其中 \( \varepsilon \) 表示随机误差。对误差取平方一举两得：既消去了正负号，使同等幅度的正误差与负误差权重相同；又使大偏差受到超过比例的惩罚，从而拟合不至于过于明目张胆地抛弃任何一个观测点。<sup>[2](https://atlasofengineering.com/engineering-physics/gauss-legendre-and-the-history-of-least-squares/)</sup>

**加权拟合。** 最小二乘法的通常假定——每个数据点提供同等精确的信息——并非在一切应用中都成立；例如，随着被测量的量增大，测量精度可能下降。加权最小二乘法把每个观测的一个非负权重 \( w_i \) 纳入准则

\[ Q = \sum_{i=1}^{n} w_i \left[ y_i - f(\vec{x}_i;\hat{\vec{\beta}}) \right]^2, \]

而当权重与解释变量各水平上的误差方差成反比时，参数估计达到可能的最精确程度。该理论假定权重是精确已知的，而实际情况几乎从来不是如此；由少量重复观测估计出来的权重，会对分析产生严重而难以预料的影响。<sup>[6](https://www.itl.nist.gov/div898/handbook/pmd/section1/pmd143.htm)</sup><sup> • </sup><sup>[9](https://www.itl.nist.gov/div898/handbook/pmd/section4/pmd432.htm)</sup>

## 线性拟合与非线性拟合

**线性模型。** 当模型对未知参数呈线性时，最小二乘估计总可以解析地求得。对于正交设计，例如 \( 2^3 \) 全因子设计，随着新项的加入，先前已纳入各项的估计值不会改变——而一般的最小二乘拟合并不具备这一性质。<sup>[5](https://www.itl.nist.gov/div898/handbook/pmd/section1/pmd142.htm)</sup><sup> • </sup><sup>[10](https://itl.nist.gov/div898/handbook/eda/section3/eda35i1.htm)</sup>

**非线性模型。** 非线性最小二乘把这一方法推广到范围大得多的一类函数；几乎任何能写成闭式的函数都可以纳入，对参数以何种方式出现也少有约束。例子包括有理函数，如 \( f(x;\vec{\beta}) = (\beta_0 + \beta_1 x)/(1 + \beta_2 x) \)，幂律 \( \beta_1 x^{\beta_2} \)，以及指数衰减 \( \beta_0 + \beta_1 \exp(-\beta_2 x) \)。非线性模型能描述线性函数无法描述的渐近行为，例如混凝土养护过程中的强度增长：先迅速上升，随后趋于平缓。<sup>[5](https://www.itl.nist.gov/div898/handbook/pmd/section1/pmd142.htm)</sup>

代价在于计算。非线性拟合需要迭代优化，初值必须与未知的估计值相当接近，否则过程可能不收敛；糟糕的初值还可能使迭代收敛到局部极小值，而不是定义最小二乘估计的那个全局极小值。高斯-牛顿迭代及其莱文贝格-马夸特变体是这类计算的标准算法。<sup>[5](https://www.itl.nist.gov/div898/handbook/pmd/section1/pmd142.htm)</sup><sup> • </sup><sup>[11](https://www.nist.gov/publications/fitting-natures-basic-functions-part-iii-exponentials-sinusoids-and-nonlinear-least)</sup> 求解非线性最小二乘本质上就很困难，几乎任何算法无论多么稳健，都可能被某个精心构造的数据集难倒；由于这类方法并不保证能达到平方和的全局极小值，收敛到另一个局部极小值并不算程序错误。<sup>[12](https://itl.nist.gov/div898/strd/nls/nls_info.shtml)</sup>

**软件实践。** 通用库直接封装了这些算法。SciPy 的 `scipy.optimize.curve_fit` 用非线性最小二乘把函数 f 拟合到数据，假定 `ydata = f(xdata, *params) + eps`；它接受参数初值猜测、可选的边界，以及用于描述 ydata 不确定度的 `sigma` 参数，后者可以是一维的标准差数组，也可以是误差的二维协方差矩阵。<sup>[13](https://cache-docs.scipy.org/doc/scipy/reference/generated/scipy.optimize.curve_fit.html)</sup> 它的 `absolute_sigma` 选项控制参数不确定度的报告方式：取默认值 `False` 时，只有 sigma 值的相对大小有意义，sigma 会被缩放，使最优参数对应的约化卡方等于 1；取 `True` 时，返回的协方差反映 sigma 的绝对值。<sup>[13](https://cache-docs.scipy.org/doc/scipy/reference/generated/scipy.optimize.curve_fit.html)</sup>

## 拟合的验证

**残差分析。** 拟合模型的残差是观测响应与回归函数预测值之差，即 \( e_i = y_i - f(\vec{x}_i;\hat{\vec{\beta}}) \)。若模型正确，残差就近似于随机误差，应当表现出随机性；残差中出现非随机的结构，则明确表明模型对数据拟合不佳。图解残差分析——把残差分别对预测变量、拟合值和运行顺序作图——是多数过程建模应用中的主要验证工具，而 R² 之类的数值方法只起确认作用。<sup>[7](https://itl.nist.gov/div898/handbook/pmd/section4/pmd44.htm)</sup>

**残差图形揭示的问题。** 特定的结构指向特定的缺陷。若残差云呈漏斗形，散布随拟合值增大而扩大，则提示应对响应变量作变换，例如对其对数或平方根建模。残差对某个预测变量的图形若呈皱眉状或微笑状，说明存在弯曲，模型需要在该预测变量上加入高阶项。把残差对时间顺序作图，则可以揭示过程的漂移。<sup>[14](https://itl.nist.gov/div898/handbook/pri/section2/pri245.htm)</sup>

**仅有 R² 还不够。** R² 统计量衡量模型所解释的响应总变异所占的比例；在实践中，报出这个数字往往就是模型验证的全部内容，但 R² 高并不保证模型对数据拟合得好，而拟合不好的模型无法可靠地回答背后的科学问题。<sup>[7](https://itl.nist.gov/div898/handbook/pmd/section4/pmd44.htm)</sup>

## 离群点与稳健拟合

无论线性还是非线性，最小二乘拟合都对离群点高度敏感：一两个异常点就能严重影响结果，而非线性回归可用于检出它们的模型验证工具又比线性回归少。<sup>[5](https://www.itl.nist.gov/div898/handbook/pmd/section1/pmd142.htm)</sup> 稳健回归处理的是被离群点或模型误差污染的数据。其中一个关键概念是崩溃点，即拟合开始偏离非离群数据之前，数据中可以成为离群点的比例。Scikit-learn 提供三种各有取舍的稳健估计器：RANSAC 比 Theil Sen 更快，随样本量的扩展性更好，且能很好地处理 y 方向上的大离群点；Theil Sen 更善于应对 X 方向上的中等大小离群点，但这一性质在高维情形下会消失；HuberRegressor 并不忽略离群点，而是赋予它们较小的权重，建议取 `epsilon = 1.35` 以达到 95% 的统计效率。<sup>[8](https://scikit-learn.org/stable/modules/linear_model.html)</sup>

## 非参数平滑

并非每个拟合问题都从参数模型出发。平滑样条直接从数据估计未知的平滑函数：R 的 `smooth.spline` 拟合三次平滑样条，其拟合在残差误差与对曲线二阶导数平方积分的惩罚之间取得平衡；若用户未指定平滑参数，则用普通交叉验证或广义交叉验证来选取。<sup>[15](https://search.r-project.org/R/refmans/stats/html/smooth.spline.html)</sup> 这类方法以可解释性换取灵活性，因为结果是一条曲线，而不是一小组参数。

## 历史

最小二乘法诞生于十八世纪的天文学与大地测量学，当时必须用不完美的观测支撑对一条轨道或一个位置的单一估计。首次发表的是勒让德 1805 年的附录，其中概述了用系数未知的线性方程拟合观测的方法；高斯出于同样的天文学问题，据他自己所述在此前就已应用这一原理，并在 1809 年关于天体运动的著作中给出了该方法的概率推导。两人之间的优先权之争成为科学史上最著名的争论之一，其高潮是勒让德于 1820 年发表文章攻击高斯。<sup>[1](https://ekonometria.org/en/podstawy/mnk-gauss/)</sup><sup> • </sup><sup>[3](https://en.wikisource.org/wiki/A_Source_Book_in_Mathematics/Legendre_on_Least_Squares)</sup><sup> • </sup><sup>[4](https://www.sciencedirect.com/science/article/pii/0315086077900325)</sup><sup> • </sup><sup>[16](https://ideas.repec.org/a/jns/jbstat/v219y1999i3-4p458-467n1014.html)</sup> 在勒让德清晰而实用的阐述之后二十年之内，最小二乘法已被各门测量科学普遍采用。<sup>[2](https://atlasofengineering.com/engineering-physics/gauss-legendre-and-the-history-of-least-squares/)</sup>

## References

1. [The Method of Least Squares: History, Theory and Proofs, Ekonometria.org](https://ekonometria.org/en/podstawy/mnk-gauss/)
2. [Gauss, Legendre and the History of Least Squares, Atlas of Engineering](https://atlasofengineering.com/engineering-physics/gauss-legendre-and-the-history-of-least-squares/)
3. [A Source Book in Mathematics: Legendre on Least Squares, Wikisource](https://en.wikisource.org/wiki/A_Source_Book_in_Mathematics/Legendre_on_Least_Squares)
4. [An attack on Gauss, published by Legendre in 1820, ScienceDirect](https://www.sciencedirect.com/science/article/pii/0315086077900325)
5. [4.1.4.2. Nonlinear Least Squares Regression, NIST Engineering Statistics Handbook](https://www.itl.nist.gov/div898/handbook/pmd/section1/pmd142.htm)
6. [4.1.4.3. Weighted Least Squares Regression, NIST Engineering Statistics Handbook](https://www.itl.nist.gov/div898/handbook/pmd/section1/pmd143.htm)
7. [4.4.4. How can I tell if a model fits my data?, NIST Engineering Statistics Handbook](https://itl.nist.gov/div898/handbook/pmd/section4/pmd44.htm)
8. [1.1. Linear Models, scikit-learn documentation](https://scikit-learn.org/stable/modules/linear_model.html)
9. [4.4.3.2. Weighted Least Squares, NIST Engineering Statistics Handbook](https://www.itl.nist.gov/div898/handbook/pmd/section4/pmd432.htm)
10. [1.3.5.18.1. Defining Models and Prediction Equations, NIST Engineering Statistics Handbook](https://itl.nist.gov/div898/handbook/eda/section3/eda35i1.htm)
11. [Fitting Nature's Basic Functions Part III: Exponentials, Sinusoids and Nonlinear Least Squares, NIST](https://www.nist.gov/publications/fitting-natures-basic-functions-part-iii-exponentials-sinusoids-and-nonlinear-least)
12. [Nonlinear Least Squares test information, NIST Statistical Reference Datasets](https://itl.nist.gov/div898/strd/nls/nls_info.shtml)
13. [curve_fit, SciPy v1.18.0 Manual](https://cache-docs.scipy.org/doc/scipy/reference/generated/scipy.optimize.curve_fit.html)
14. [5.2.4.5. Check of assumptions, NIST Engineering Statistics Handbook](https://itl.nist.gov/div898/handbook/pri/section2/pri245.htm)
15. [R: Fit a Smoothing Spline, R Documentation](https://search.r-project.org/R/refmans/stats/html/smooth.spline.html)
16. [Gauss and the Method of the Least Squares, Jahrbücher für Nationalökonomie und Statistik](https://ideas.repec.org/a/jns/jbstat/v219y1999i3-4p458-467n1014.html)

---
*Initially written Sep 17, 2026 · Reviewed: Oct 15, 2026 · Edited: Oct 15, 2026 · Last review: Oct 15, 2026*

Topic: 物质世界与数学 › 数学与统计学 › 统计学与概率论 › Statistical inference, estimation, sampling, and testing › Regression analysis

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/curve-fitting

English original: https://www.edgepedia.org/curve-fitting
