计算最小二乘拟合的置信带 [英] Calculate confidence band of least-square fit

查看:74
本文介绍了计算最小二乘拟合的置信带的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题困扰了我好几天.

<块引用>

如何计算拟合的 (95%) 置信区间?

将曲线拟合到数据是每个物理学家的日常工作——所以我认为这应该在某个地方实施——但我找不到实现这一点,我也不知道如何在数学上做到这一点.

我唯一发现的是 模块.

另见这个例子这个答案.

这是您问题的答案:

将 numpy 导入为 np从 matplotlib 导入 pyplot 作为 plt将 statsmodels.api 导入为 sm从 statsmodels.stats.outliers_influence 导入 summary_tablex = np.linspace(0,10)y = 3*np.random.randn(50) + xX = sm.add_constant(x)res = sm.OLS(y, X).fit()st, 数据, ss2 = summary_table(res, alpha=0.05)拟合值 = 数据 [:,2]predict_mean_se = 数据[:,3]predict_mean_ci_low, predict_mean_ci_upp = data[:,4:6].Tpredict_ci_low, predict_ci_upp = data[:,6:8].T图, ax = plt.subplots(figsize=(8,6))ax.plot(x, y, 'o', label="data")ax.plot(X, 拟合值, 'r-', label='OLS')ax.plot(X, predict_ci_low, 'b--')ax.plot(X, predict_ci_upp, 'b--')ax.plot(X, predict_mean_ci_low, 'g--')ax.plot(X, predict_mean_ci_upp, 'g--')ax.legend(loc='best');plt.show()

I got a question that I fight around for days with now.

How do I calculate the (95%) confidence band of a fit?

Fitting curves to data is the every day job of every physicist -- so I think this should be implemented somewhere -- but I can't find an implementation for this neither do I know how to do this mathematically.

The only thing I found is seaborn that does a nice job for linear least-square.

import numpy as np                                                                                                                                                                                                                         
from matplotlib import pyplot as plt
import seaborn as sns
import pandas as pd

x = np.linspace(0,10)
y = 3*np.random.randn(50) + x

data = {'x':x, 'y':y}
frame = pd.DataFrame(data, columns=['x', 'y'])
sns.lmplot('x', 'y', frame, ci=95)

plt.savefig("confidence_band.pdf")

But this is just linear least-square. When I want to fit e.g. a saturation curve like , I'm screwed.

Sure, I can calculate the t-distribution from the std-error of a least-square method like scipy.optimize.curve_fit but that is not what I'm searching for.

Thanks for any help!!

解决方案

You can achieve this easily using StatsModels module.

Also see this example and this answer.

Here is an answer for your question:

import numpy as np
from matplotlib import pyplot as plt

import statsmodels.api as sm
from statsmodels.stats.outliers_influence import summary_table

x = np.linspace(0,10)
y = 3*np.random.randn(50) + x
X = sm.add_constant(x)
res = sm.OLS(y, X).fit()

st, data, ss2 = summary_table(res, alpha=0.05)
fittedvalues = data[:,2]
predict_mean_se  = data[:,3]
predict_mean_ci_low, predict_mean_ci_upp = data[:,4:6].T
predict_ci_low, predict_ci_upp = data[:,6:8].T

fig, ax = plt.subplots(figsize=(8,6))
ax.plot(x, y, 'o', label="data")
ax.plot(X, fittedvalues, 'r-', label='OLS')
ax.plot(X, predict_ci_low, 'b--')
ax.plot(X, predict_ci_upp, 'b--')
ax.plot(X, predict_mean_ci_low, 'g--')
ax.plot(X, predict_mean_ci_upp, 'g--')
ax.legend(loc='best');
plt.show()

这篇关于计算最小二乘拟合的置信带的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆