Python 中 LOWESS 的置信区间
Confidence interval for LOWESS in Python
如何计算 Python 中 LOWESS 回归的置信区间?我想将这些作为阴影区域添加到使用以下代码创建的 LOESS 图中(除 statsmodels 之外的其他包也可以)。
import numpy as np
import pylab as plt
import statsmodels.api as sm
x = np.linspace(0,2*np.pi,100)
y = np.sin(x) + np.random.random(100) * 0.2
lowess = sm.nonparametric.lowess(y, x, frac=0.1)
plt.plot(x, y, '+')
plt.plot(lowess[:, 0], lowess[:, 1])
plt.show()
我从网络日志 Serious Stats 添加了一个带有置信区间的示例图(它是使用 R 中的 ggplot 创建的)。
LOESS 没有明确的标准误差概念。它只是在这种情况下没有任何意义。既然这样,你就坚持使用蛮力方法。
Bootstrap 你的数据。您要将 LOESS 曲线拟合到自举数据。请参阅本页的中间部分,以找到您所做工作的漂亮图片。 http://statweb.stanford.edu/~susan/courses/s208/node20.html
获得大量不同的 LOESS 曲线后,您可以找到顶部和底部的第 X 个百分位数。
这是一个非常古老的问题,但它是 google 搜索中最先出现的问题之一。您可以使用 scikit-misc 中的 loess() 函数来执行此操作。这是一个示例(我试图保留您的原始变量名,但我稍微提高了噪音以使其更明显)
import numpy as np
import pylab as plt
from skmisc.loess import loess
x = np.linspace(0,2*np.pi,100)
y = np.sin(x) + np.random.random(100) * 0.4
l = loess(x,y)
l.fit()
pred = l.predict(x, stderror=True)
conf = pred.confidence()
lowess = pred.values
ll = conf.lower
ul = conf.upper
plt.plot(x, y, '+')
plt.plot(x, lowess)
plt.fill_between(x,ll,ul,alpha=.33)
plt.show()
结果:
对于我的一个项目,我需要为 time-series 建模创建间隔,并且为了使过程更高效,我创建了 tsmoothie:python 库 [=24] =] 以矢量化方式进行平滑和异常值检测。
它提供了不同的平滑算法以及计算间隔的可能性。
在LowessSmoother
的情况下:
import numpy as np
import matplotlib.pyplot as plt
from tsmoothie.smoother import *
from tsmoothie.utils_func import sim_randomwalk
# generate 10 randomwalks of length 200
np.random.seed(33)
data = sim_randomwalk(n_series=10, timesteps=200,
process_noise=10, measure_noise=30)
# operate smoothing
smoother = LowessSmoother(smooth_fraction=0.1, iterations=1)
smoother.smooth(data)
# generate intervals
low, up = smoother.get_intervals('prediction_interval', confidence=0.05)
# plot the first smoothed timeseries with intervals
plt.figure(figsize=(11,6))
plt.plot(smoother.smooth_data[0], linewidth=3, color='blue')
plt.plot(smoother.data[0], '.k')
plt.fill_between(range(len(smoother.data[0])), low[0], up[0], alpha=0.3)
我还指出,tsmoothie 可以以矢量化的方式对多个 time-series 进行平滑处理。希望这可以帮助某人
如何计算 Python 中 LOWESS 回归的置信区间?我想将这些作为阴影区域添加到使用以下代码创建的 LOESS 图中(除 statsmodels 之外的其他包也可以)。
import numpy as np
import pylab as plt
import statsmodels.api as sm
x = np.linspace(0,2*np.pi,100)
y = np.sin(x) + np.random.random(100) * 0.2
lowess = sm.nonparametric.lowess(y, x, frac=0.1)
plt.plot(x, y, '+')
plt.plot(lowess[:, 0], lowess[:, 1])
plt.show()
我从网络日志 Serious Stats 添加了一个带有置信区间的示例图(它是使用 R 中的 ggplot 创建的)。
LOESS 没有明确的标准误差概念。它只是在这种情况下没有任何意义。既然这样,你就坚持使用蛮力方法。
Bootstrap 你的数据。您要将 LOESS 曲线拟合到自举数据。请参阅本页的中间部分,以找到您所做工作的漂亮图片。 http://statweb.stanford.edu/~susan/courses/s208/node20.html
获得大量不同的 LOESS 曲线后,您可以找到顶部和底部的第 X 个百分位数。
这是一个非常古老的问题,但它是 google 搜索中最先出现的问题之一。您可以使用 scikit-misc 中的 loess() 函数来执行此操作。这是一个示例(我试图保留您的原始变量名,但我稍微提高了噪音以使其更明显)
import numpy as np
import pylab as plt
from skmisc.loess import loess
x = np.linspace(0,2*np.pi,100)
y = np.sin(x) + np.random.random(100) * 0.4
l = loess(x,y)
l.fit()
pred = l.predict(x, stderror=True)
conf = pred.confidence()
lowess = pred.values
ll = conf.lower
ul = conf.upper
plt.plot(x, y, '+')
plt.plot(x, lowess)
plt.fill_between(x,ll,ul,alpha=.33)
plt.show()
结果:
对于我的一个项目,我需要为 time-series 建模创建间隔,并且为了使过程更高效,我创建了 tsmoothie:python 库 [=24] =] 以矢量化方式进行平滑和异常值检测。
它提供了不同的平滑算法以及计算间隔的可能性。
在LowessSmoother
的情况下:
import numpy as np
import matplotlib.pyplot as plt
from tsmoothie.smoother import *
from tsmoothie.utils_func import sim_randomwalk
# generate 10 randomwalks of length 200
np.random.seed(33)
data = sim_randomwalk(n_series=10, timesteps=200,
process_noise=10, measure_noise=30)
# operate smoothing
smoother = LowessSmoother(smooth_fraction=0.1, iterations=1)
smoother.smooth(data)
# generate intervals
low, up = smoother.get_intervals('prediction_interval', confidence=0.05)
# plot the first smoothed timeseries with intervals
plt.figure(figsize=(11,6))
plt.plot(smoother.smooth_data[0], linewidth=3, color='blue')
plt.plot(smoother.data[0], '.k')
plt.fill_between(range(len(smoother.data[0])), low[0], up[0], alpha=0.3)
我还指出,tsmoothie 可以以矢量化的方式对多个 time-series 进行平滑处理。希望这可以帮助某人