如何进行线性回归并考虑误差条?

13

我正在为某个有限大小的物理系统进行计算机模拟,之后会对无穷大进行外推(热力学极限)。一些理论认为数据应该随着系统大小呈线性缩放,因此我正在进行线性回归。

我拥有的数据存在噪声,但对于每个数据点,我可以估算误差条。例如,数据点看起来像:

x_list = [0.3333333333333333, 0.2886751345948129, 0.25, 0.23570226039551587, 0.22360679774997896, 0.20412414523193154, 0.2, 0.16666666666666666]
y_list = [0.13250359351851854, 0.12098339583333334, 0.12398501145833334, 0.09152715, 0.11167239583333334, 0.10876248333333333, 0.09814170444444444, 0.08560799305555555]
y_err = [0.003306749165349316, 0.003818446389148108, 0.0056036878203831785, 0.0036635292592592595, 0.0037034897788415424, 0.007576672222222223, 0.002981084130692832, 0.0034913019065973983]

假设我想在Python中实现此操作。

  1. 我知道的第一种方法是:

    m, c, r_value, p_value, std_err = scipy.stats.linregress(x_list, y_list)
    

    我理解这给出了结果的误差条,但是这并没有考虑到初始数据的误差条。

  2. 我知道的第二种方式是:

  3. m, c = numpy.polynomial.polynomial.polyfit(x_list, y_list, 1, w = [1.0 / ty for ty in y_err], full=False)
    
    在这里,我们使用每个点的误差条的倒数作为权重,用于最小二乘逼近。因此,如果某个点并不是非常可靠,它将不会对结果产生很大影响,这是合理的。
    但我不知道如何得到同时结合这两种方法的东西。
    我真正想要的是第二种方法所做的事情,即在每个点以不同权重影响结果时使用回归。但与此同时,我想知道我的结果有多准确,也就是说,我想知道结果系数的误差条是什么。
    我该怎么做?

我是否误解了你,还是你试图将y_err系列用作权重矩阵? - urschrei
4个回答

9

不完全确定这是否是您的意思,但是使用pandas、statsmodels和patsy,我们可以比较普通最小二乘拟合和加权最小二乘拟合,其中使用您提供的噪声的逆作为权重矩阵(顺便说一下,statsmodels会抱怨样本量小于20)。

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mpl
mpl.rcParams['figure.dpi'] = 300

import statsmodels.formula.api as sm

x_list = [0.3333333333333333, 0.2886751345948129, 0.25, 0.23570226039551587, 0.22360679774997896, 0.20412414523193154, 0.2, 0.16666666666666666]
y_list = [0.13250359351851854, 0.12098339583333334, 0.12398501145833334, 0.09152715, 0.11167239583333334, 0.10876248333333333, 0.09814170444444444, 0.08560799305555555]
y_err = [0.003306749165349316, 0.003818446389148108, 0.0056036878203831785, 0.0036635292592592595, 0.0037034897788415424, 0.007576672222222223, 0.002981084130692832, 0.0034913019065973983]

# put x and y into a pandas DataFrame, and the weights into a Series
ws = pd.DataFrame({
    'x': x_list,
    'y': y_list
})
weights = pd.Series(y_err)

wls_fit = sm.wls('x ~ y', data=ws, weights=1 / weights).fit()
ols_fit = sm.ols('x ~ y', data=ws).fit()

# show the fit summary by calling wls_fit.summary()
# wls fit r-squared is 0.754
# ols fit r-squared is 0.701

# let's plot our data
plt.clf()
fig = plt.figure()
ax = fig.add_subplot(111, facecolor='w')
ws.plot(
    kind='scatter',
    x='x',
    y='y',
    style='o',
    alpha=1.,
    ax=ax,
    title='x vs y scatter',
    edgecolor='#ff8300',
    s=40
)

# weighted prediction
wp, = ax.plot(
    wls_fit.predict(),
    ws['y'],
    color='#e55ea2',
    lw=1.,
    alpha=1.0,
)
# unweighted prediction
op, = ax.plot(  
    ols_fit.predict(),
    ws['y'],
    color='k',
    ls='solid',
    lw=1,
    alpha=1.0,
)
leg = plt.legend(
    (op, wp),
    ('Ordinary Least Squares', 'Weighted Least Squares'),
    loc='upper left',
    fontsize=8)

plt.tight_layout()
fig.set_size_inches(6.40, 5.12)
plt.show()

OLS vs WLS

加权最小二乘法残差:

[0.025624005084707302,
 0.013611438189866154,
 -0.033569595462217161,
 0.044110895217014695,
 -0.025071632845910546,
 -0.036308252199571928,
 -0.010335514810672464,
 -0.0081511479431851663]

加权拟合残差的均方误差(wls_fit.mse_residwls_fit.scale)为0.22964802498892287,拟合的R平方值为0.754

您可以调用其summary()方法获取有关拟合的大量数据,或者执行dir(wls_fit),如果需要列出每个可用属性和方法的列表。


你确定参数weights应该设置为1/y_err吗?statsmodel页面上的示例使用weights=1/(w**2)。他们写道:“在这个例子中,w是误差的标准差。WLS要求权重与误差方差的倒数成比例。” https://www.statsmodels.org/stable/examples/notebooks/generated/wls.html - Jim
@Jim我不确定!我不能确定statsmodels是否在7年前建议关于WLS的误差方差,因此我只使用了我在统计学入门课程中学到的内容来指定模型。您是否尝试过使用权重= 1 /(w ** 2)运行示例?无论哪种方式,非常高兴纠正答案。 - urschrei
我还没有尝试使用1/(w**2)的权重来运行它。我认为它应该是相似的。而且我不确定1(w**2)是否正确。我想在过去的某个时候,我曾决定在我的分析中使用1/(w**2),但那是很久以前的事了。 - Jim

3
我写了一个简洁的函数来执行数据集的加权线性回归,这是GSL的"gsl_fit_wlinear"函数的直接翻译。如果您想知道函数在执行拟合时正在做什么,这将非常有用。
def wlinear_fit (x,y,w) :
    """
    Fit (x,y,w) to a linear function, using exact formulae for weighted linear
    regression. This code was translated from the GNU Scientific Library (GSL),
    it is an exact copy of the function gsl_fit_wlinear.
    """
    # compute the weighted means and weighted deviations from the means
    # wm denotes a "weighted mean", wm(f) = (sum_i w_i f_i) / (sum_i w_i)
    W = np.sum(w)
    wm_x = np.average(x,weights=w)
    wm_y = np.average(y,weights=w)
    dx = x-wm_x
    dy = y-wm_y
    wm_dx2 = np.average(dx**2,weights=w)
    wm_dxdy = np.average(dx*dy,weights=w)
    # In terms of y = a + b x
    b = wm_dxdy / wm_dx2
    a = wm_y - wm_x*b
    cov_00 = (1.0/W) * (1.0 + wm_x**2/wm_dx2)
    cov_11 = 1.0 / (W*wm_dx2)
    cov_01 = -wm_x / (W*wm_dx2)
    # Compute chi^2 = \sum w_i (y_i - (a + b * x_i))^2
    chi2 = np.sum (w * (y-(a+b*x))**2)
    return a,b,cov_00,cov_11,cov_01,chi2

执行您的拟合,您需要执行以下操作
a,b,cov_00,cov_11,cov_01,chi2 = wlinear_fit(x_list,y_list,1.0/y_err**2)

该函数将返回线性回归系数a(截距)和b(斜率)的最佳估计值,以及协方差矩阵元素cov_00、cov_01和cov_11。a误差的最佳估计值是cov_00的平方根,b误差的最佳估计值是cov_11的平方根。残差的加权和在chi2变量中返回。
重要提示:此函数接受逆方差作为数据点的权重,而不是逆标准偏差。

3

sklearn.linear_model.LinearRegression 支持在 fit 过程中指定权重:

x_data = np.array(x_list).reshape(-1, 1)  # The model expects shape (n_samples, n_features).
y_data = np.array(y_list)
y_err  = np.array(y_err)

model = LinearRegression()
model.fit(x_data, y_data, sample_weight=1/y_err)

这里指定了样本权重为1 / y_err。可能存在不同的版本,并且通常建议在情况下将这些样本权重剪裁到最大值,以防y_err变化剧烈或具有小的离群值:

sample_weight = 1 / y_err
sample_weight = np.minimum(sample_weight, MAX_WEIGHT)

在确定 MAX_WEIGHT 的值时,需要从您的数据中获取信息(可查看 y_err1 / y_err 分布情况,例如,如果存在异常值可以进行裁剪)。


0

我发现this的文档有助于理解和设置自己的加权最小二乘程序(适用于任何编程语言)。

通常学习和使用优化的程序是最好的选择,但有时了解程序的内部机制非常重要。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接