指数平滑仅适用于没有任何缺失时间序列值的数据。对于您提到的三种方法,我将向您展示对未来 +5 天数据的预测:
- 指数拟合(您的猜测“似乎呈指数增长”)
- 样条插值
- 指数平滑
注意:我通过从您的绘图中窃取数据来获取您的数据,并将日期保存到 dates 并将数据值保存到 values
import pandas as pd
import numpy as np
from statsmodels.tsa.holtwinters import ExponentialSmoothing
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
from scipy.optimize import curve_fit
from scipy.interpolate import splrep, splev
df = pd.DataFrame()
# mdates.date2num allows functions like curve_fit and spline to digest time series data
df['dates'] = mdates.date2num(dates)
df['values'] = values
# Exponential fit function
def exponential_func(x, a, b, c, d):
return a*np.exp(b*(x-c))+d
# Spline interpolation
def spline_interp(x, y, x_new):
tck = splrep(x, y)
return splev(x_new, tck)
# define forecast timerange (forecasting 5 days into future)
dates_forecast = np.linspace(df['dates'].min(), df['dates'].max() + 5, 100)
dd = mdates.num2date(dates_forecast)
# Doing exponential fit
popt, pcov = curve_fit(exponential_func, df['dates'], df['values'],
p0=(1, 1e-2, df['dates'][0], 1))
# Doing spline interpolation
yy = spline_interp(df['dates'], df['values'], dates_forecast)
到目前为止,直截了当(mdates.date2num 函数除外)。由于您丢失了数据,因此您必须对实际数据使用样条插值来用插值数据填充缺失的时间点
# Interpolating data for exponential smoothing (no missing data in time series allowed)
df_interp = pd.DataFrame()
df_interp['dates'] = np.arange(dates[0], dates[-1] + 1, dtype='datetime64[D]')
df_interp['values'] = spline_interp(df['dates'], df['values'],
mdates.date2num(df_interp['dates']))
series_interp = pd.Series(df_interp['values'].values,
pd.date_range(start='2020-01-19', end='2020-03-04', freq='D'))
# Now the exponential smoothing works fine, provide the `trend` argument given your data
# has a clear (kind of exponential) trend
fit1 = ExponentialSmoothing(series_interp, trend='mul').fit(optimized=True)
您可以绘制这三种方法,看看它们对未来五天的预测如何
# Plot data
plt.plot(mdates.num2date(df['dates']), df['values'], 'o')
# Plot exponential function fit
plt.plot(dd, exponential_func(dates_forecast, *popt))
# Plot interpolated values
plt.plot(dd, yy)
# Plot Exponential smoothing prediction using function `forecast`
plt.plot(np.concatenate([series_interp.index.values, fit1.forecast(5).index.values]),
np.concatenate([series_interp.values, fit1.forecast(5).values]))
所有三种方法的比较表明您选择指数平滑是正确的。它看起来比其他两种方法更好地预测未来五天
关于你的其他问题
我不明白 predict(3) 是什么意思,以及为什么它会返回我已有日期的预测总和。
ExponentialSmoothing.fit() 返回一个 statsmodels.tsa.holtwinters.HoltWintersResults 对象,它有两个函数可以用于预测/预测值:predict 和 forecast:
predict 对您的数据进行 start 和 end 观察,并将 ExponentialSmoothing 模型应用于相应的日期值。为了预测未来的值,您必须指定一个 end 参数,该参数在未来
>> fit1.predict(start=np.datetime('2020-03-01'), end=np.datetime64('2020-03-09'))
2020-03-01 4240.649526
2020-03-02 5631.207307
2020-03-03 5508.614325
2020-03-04 5898.717779
2020-03-05 6249.810230
2020-03-06 6767.659081
2020-03-07 7328.416024
2020-03-08 7935.636353
2020-03-09 8593.169945
Freq: D, dtype: float64
在您的示例中,predict(3)(等于 predict(start=3) 根据您从第三个日期开始的日期预测值,并且没有任何预测。
forecast() 只做预测。您只需将要预测的观察次数传递给未来。
>> fit1.forecast(5)
2020-03-05 6249.810230
2020-03-06 6767.659081
2020-03-07 7328.416024
2020-03-08 7935.636353
2020-03-09 8593.169945
Freq: D, dtype: float64
由于这两个函数都基于相同的ExponentialSmoothing.fit 模型,因此它们的值在相同的日期是相等的。