先知ppt_用先知预测趋势数据–趋势变化点

先知ppt

In these posts, I’ve been looking at using Prophet to forecast time series data at a monthly level using sales revenue data.  In this post, I want to look at a very interesting aspect of Prophet (and time series analysis) that most people overlook  – that of trend changepoints. This is the fourth in a series of posts about using Prophet to forecast time series data. The other parts can be found here:

在这些帖子中,我一直在研究使用Prophet通过销售收入数据每月预测时间序列数据。 在这篇文章中,我想看看先知(非常有趣)的一个方面(以及时间序列分析),大多数人都忽略了这一方面–趋势变化点。 这是有关使用先知来预测时间序列数据的系列文章中的第四篇。 其他部分可以在这里找到:

  • Forecasting Time Series data with Prophet – Part 1
  • Forecasting Time Series data with Prophet – Part 2
  • Forecasting Time Series data with Prophet – Part 3
  • 用先知预测时间序列数据–第1部分
  • 用先知预测时间序列数据–第2部分
  • 用先知预测时间序列数据–第3部分

Trend changepoint detection isn’t an easy thing to do. You could take the naive approach and just find local maxima and minima but those may or may not be changes in the overall trend of your signal.   There are many different methods for changepoint detection (a good paper looking at four methods can be found here – Trend analysis and change point techniques: a survey) but thankfully Prophet does trend changepoint detection behind the scenes for us (and it does a pretty good job of it).

趋势变化点检测并非易事。 您可能会采取幼稚的方法,只是找到局部最大值和最小值,但这些可能会或可能不会改变信号的总体趋势。 有很多不同的变化点检测方法(可以在这里找到一篇很好的论文,介绍四种方法- 趋势分析和变化点技术:调查 ),但幸运的是,Prophet为我们在后台进行了趋势变化点检测(它做得非常好)它的工作)。

For most people / tasks, the automatic detection performed by Prophet is good enough, but it never hurts to know how to tweak Prophet in case it misses some changepoints.

对于大多数人/任务而言,先知执行的自动检测功能已经足够好,但是知道如何调整先知以防万一错过一些变更点也毫不费力。

I’ve uploaded a jupyter notebook here and the sample data that I’m using here.  Rather than use the monthly sales data I’ve been using, i wanted to use something that has a bit more of a noticable trend so I grabbed the S&P 500 index from FRED.

我上传了这里jupyter笔记本和样本数据,我使用这里 。 我不想使用我一直在使用的月度销售数据,而是想使用一些趋势明显的东西,因此我从FRED手中获取了S&P 500指数 。

The jupyter notebook has bit more detail about loading data and running prophet for this data, so I’ll just throw the commands here and you can jump over there to see more detail. These first steps are no different than the standard data loading / prep for prophet discussed in previous posts.

jupyter笔记本有更多有关加载数据和运行该数据的先知的详细信息,因此我将命令放在此处,您可以跳到那里查看更多详细信息。 这些第一步与之前帖子中讨论的先知的标准数据加载/准备没有什么不同。

market_df = pd.read_csv('../examples/SP500.csv', index_col='DATE', parse_dates=True)
df = market_df.reset_index().rename(columns={'DATE':'ds', 'SP500':'y'})
df['y'] = np.log(df['y'])

#lets take a look at our data quickly
df.set_index('ds').y.plot()

先知ppt_用先知预测趋势数据–趋势变化点_第1张图片
SP500 Daily Data Plotted

SP500每日数据绘制

Now, let’s run prophet. Again, this is no different than the steps we’ve taken in previous posts for prophet.

现在,让我们先知。 同样,这与我们在先知中为先知所采取的步骤没有什么不同。

model = Prophet()
model.fit(df);
future = model.make_future_dataframe(periods=366)
forecast = model.predict(future)

Prophet has created our model and fit the data. It has also (behind the scenes) created some potential changepoints. We can access these changepoints with .changepoints.  By default, Prophet adds 25 changepoints into the initial 80% of the data-set. The number of changepoints can be set by using the n_changepoints parameter when initializing prophet (e.g., model=Prophet(n_changepoints=30).

先知创建了我们的模型并拟合了数据。 它还(在幕后)创建了一些潜在的变更点。 我们可以使用.changepoints访问这些变更.changepoints 默认情况下,Prophet将25个变更点添加到数据集的初始80%中。 初始化先知时,可以使用n_changepoints参数设置改变点的数量(例如, model=Prophet(n_changepoints=30)

You can view the changepoints by typing the following:

您可以通过键入以下内容来查看更改点:

model.changepoints

先知ppt_用先知预测趋势数据–趋势变化点_第2张图片

In addition to viewing the dates of the changepoints, we can also view a chart with changepoints added.

除了查看更改点的日期外,我们还可以查看添加了更改点的图表。

figure = model.plot(forecast)
for changepoint in model.changepoints:
    plt.axvline(changepoint,ls='--', lw=1)

先知ppt_用先知预测趋势数据–趋势变化点_第3张图片
S&P 500 Prophet Model with Changepoints Added (in oragen)

添加了变更点的S&P 500先知模型(在oragen中)

Taking a look at the possible changepoints (drawn in orange/red) in the above chart, we can see they fit pretty well with some of the highs and lows.

看一下上图中的可能变化点(以橙色/红色绘制),我们可以看到它们与某些高点和低点非常吻合。

Prophet will also let us take a look at the magnitudes of these possible changepoints. You can look at this visualization with the following code:

先知还将让我们看一下这些可能的变化点的数量。 您可以使用以下代码查看此可视化:

deltas = model.params['delta'].mean(0)
fig = plt.figure(facecolor='w')
ax = fig.add_subplot(111)
ax.bar(range(len(deltas)), deltas)
ax.grid(True, which='major', c='gray', ls='-', lw=1, alpha=0.2)
ax.set_ylabel('Rate change')
ax.set_xlabel('Potential changepoint')
fig.tight_layout()

先知ppt_用先知预测趋势数据–趋势变化点_第4张图片
SP500 Prophel Model changepoint Magnitudes

SP500 Prophel模型更改点幅度

We can see from the above chart, that there are quite a few of these changes points (found between 10 and 20 on the chart) that are very minimal in magnitude and are most likely to be ignored by prophet during forecasting be used in the forecasting.

从上图可以看出,这些变化点中有很多(在图表上的10到20之间),其变化幅度很小,在预测过程中最有可能被先知所忽略,并在预测中使用。

Now, if we know where trends changed in the past, we can add these known changepoints into our dataframe for use by Prophet. For this data, I’m going to use the FRED website to find some of the low points and high points to use as trend changepoints. Note: In actuality, just because there is a low or high doesn’t mean its a real changepoint or trend change, but let’s assume it does.

现在,如果我们知道趋势在过去发生了什么变化,可以将这些已知的变化点添加到数据框中,以供先知使用。 对于此数据,我将使用FRED网站查找一些低点和高点以用作趋势变化点。 注意:实际上,仅仅存在一个低点或高点并不意味着它是一个真实的变化点或趋势变化,而是假设它确实存在。

m = Prophet(changepoints=['2009-03-09', '2010-07-02', '2011-09-26', '2012-03-20', '2010-04-06'])
forecast = m.fit(df).predict(future)
m.plot(forecast);

先知ppt_用先知预测趋势数据–趋势变化点_第5张图片
S&P500 Prophet Model with Manually Set Changepoints

具有手动设置变更点的S&P500先知模型

We can see that by manually setting our changepoints (and only using a few points), we drastically changed the model compared to the model that prophet built for us using the automatic detection of changepoints. Unless you are very sure about your trend changepoints in the past, its probably good to keep the defaults that prophet provides.

我们可以看到,通过手动设置更改点(仅使用几个点),与先知使用自动检测更改点为我们构建的模型相比,我们彻底改变了模型。 除非您非常确定过去的趋势更改点,否则保留先知提供的默认值可能会很好。

结论 (Conclusion)

Prophet’s use (and accessibility) of trend changepoints is wonderful, especially for those signals / datasets that have significant changes in trend during the lifetime of the signal. That said, unless you are certain about your changepoints, it might be best to let prophet do its thing automatically.

先知对趋势变化点的使用(和可访问性)非常好,尤其是对于在信号生命周期中趋势发生重大变化的信号/数据集。 就是说,除非您对变更点有把握,否则最好让先知自动完成任务。

Note:  Please don’t think that because prophet does an OK job of forecasting the SP500 chart historically in this example that you should use it to ‘predict’ the markets. The markets are awfully tough to forecast…I used this market data because I knew there were some very clear changepoints in the data.

注意 :请不要以为,因为在此示例中,先知可以很好地预测历史上的SP500图表,因此您应该使用它来“预测”市场。 市场非常难以预测……我使用此市场数据是因为我知道数据中存在一些非常清晰的变化点。

Eric Brown 埃里克·布朗
Eric D. Brown , D.Sc. has a doctorate in Information Systems with a specialization in Data Sciences, Decision Support and Knowledge Management. He writes about utilizing python for data analytics at pythondata.com and the crossroads of technology and strategy at ericbrown.com
埃里克·布朗(Eric D.Brown) 拥有信息系统博士学位,专门研究数据科学,决策支持和知识管理。 他写了关于利用数据分析Python在 pythondata.com技术和战略的十字路口在 ericbrown.com
http://pythondata.com/ http://pythondata.com/

翻译自: https://www.pybloggers.com/2017/09/forecasting-time-series-data-with-prophet-trend-changepoints/

先知ppt

你可能感兴趣的:(先知ppt_用先知预测趋势数据–趋势变化点)