组件图与 df_pred 在每月季节性上不一致 它可能适用于内置的每周季节性不适用于自定义的每月季节性

问题描述

背景

我用时间序列进行季节性分解。在我的用例中,我有趋势/每周(7 天)/每月(先知文档建议的 30.5 天)组件。

对我来说,我想获得每个组件图的数据,以便我可以知道每个工作日/一个月中的每一天的估计数量

image

我制作了一个合成数据集来重现我的发现。

ValueError: Expected singleton: gi.classroom(12,13)

我的尝试

我不知道在每个分量图中获取数据点,因此我根据 import pandas as pd from prophet import Prophet ###### data genaration ds = pd.date_range(start = '2020-05-01',end = '2021-05-01',freq = 'D') synthetic_data = pd.DataFrame({'ds':ds}) # linear trend synthetic_data['trend'] = synthetic_data.index*0.05 + 1 # weekly seasonality synthetic_data['weekly'] = 10*np.sin(synthetic_data.ds.dt.weekday/7*2*np.pi) # monthly seasonality synthetic_data['monthly'] = 1*synthetic_data.ds.dt.day # add noise to get additive ts synthetic_data['y'] = synthetic_data['trend'] + synthetic_data['weekly'] + synthetic_data['monthly'] + np.random.normal(0,5) ###### model fitting and components plot m = Prophet(seasonality_mode='additive',# changepoint_prior_scale = 0.005,# holidays_prior_scale= 20,# yearly_seasonality= 1,weekly_seasonality= 1,daily_seasonality= 0 ) m.add_seasonality('monthly',period= 30.5,fourier_order = 5) m.fit(synthetic_data) future = m.make_future_dataframe(periods= 0,freq = 'd') df_pred = m.predict(future) fig = m.plot_components(df_pred) plt.suptitle('components plots is inconsistent with df_pred on monthly seasonality',y= 1.02,fontsize = 20) plt.show() 计算它们。

它可能适用于内置的每周季节性

我想我可以使用以下代码获取每周季节性组件的数据点:

df_pred = m.predict(future)

ds 0 0.150872 1 7.748597 2 9.511470 3 4.112012 4 -4.383875 5 -9.578614 6 -7.560462 名称:每周,数据类型:float64

如果我们比较代码输出和每周组件图,我们可以发现它们是一致的。

不适用于自定义的每月季节性

然后我也从 # 6 for Sunday,0 for Monday. This weekly predicted result is consistent with components plot. df_pred.groupby(df_pred.ds.dt.weekday).weekly.mean() 计算每月的季节性。

df_pred

ds 1 -3.567997 2 -10.775579 3 -13.482367 4 -12.816576 5 -10.644709 6 -8.886286 7 -8.210471 8 -7.986831 9 -7.286549 10 -5.873002 11 -4.305186 12 -3.227050 13 -2.673673 14 -2.097752 15 -1.015542 16 0.460024 17 1.766795 18 2.539023 19 3.032579 20 3.850227 21 5.250232 22 6.802167 23 7.821215 24 8.174247 25 8.571022 26 9.883497 27 12.022506 28 13.481589 29 12.635564 30 7.903723 31 1.138457 名称:每月,数据类型:float64

但是,我发现每月季节性的结果不一致:

  1. 合成数据中的月季节性为 1*synthetic_data.ds.dt.day,这是简单的线性关系。在分量图中,每月的季节性不是线性的。
  2. 每月的季节性与我在 # I think that 1 for First day,31 for Lastday. This monthly predicted result is nonconsistent with components plot. df_pred.groupby(df_pred.ds.dt.day).monthly.mean() 上的计算不同

    image

我的问题摘要

  1. 我在模型拟合过程中是否做错了什么?例如声明错误的每月季节性 (df_pred.monthly)。所以我得到了错误的月度分量图。
  2. 如何在组件图中获取数据点?我在每周组件上做对了吗?自定义每月组件有什么问题?从源代码中,我知道 m.add_seasonality('monthly',fourier_order = 5) 分别对待内置的每周季节性和自定义的每月季节性。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)