market_df = pd.read_csv('../examples/SP500.csv', index_col='DATE', parse_dates=True)

market_df.head()

"""
Explanation: Load data
Let's load our data to analyze. For this example, I'm going to use some stock market data to be able to show some clear trend changes. This data can be downloaded from FRED (https://fred.stlouisfed.org/series/SP500) or just grab it from the examples directory.
End of explanation
"""


df = market_df.reset_index().rename(columns={'DATE':'ds', 'SP500':'y'})
df['y'] = np.log(df['y'])

df.head()

"""
Explanation: Prepare for Prophet
End of explanation
"""


df.set_index('ds').y.plot()

"""
Explanation: let's take a quick look at our data
End of explanation
"""


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

"""
Explanation: Running Prophet
As before, let's instantiate prophet and fit our data (including our future dataframe). Take a look at http://pythondata.com/forecasting-time-series-data-prophet-jupyter-notebook/ for more information on the basics of Prophet.
End of explanation
"""


print model.changepoints

"""
Explanation: Now, let's take a look at our changepoints.
Prophet creates changespoint for us by default and stores them in  .changepoints. You can see below what the possible changepoints are (they are just shown as dates).   By default, Prophet adds 25 changepoints into the initial 80% of the dataset.  The number of changepoints can be set by using the n_changepoints parameter when initiallizing prophet (e.g., model=Prophet(n_changepoints=30)
End of explanation
"""


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

"""
Explanation: We can view the possible changepoints by plotting the forecast and changepoints using the following code:
End of explanation
"""


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()

"""
Explanation: 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 (edited from the fbprophet example here -> https://github.com/facebookincubator/prophet/blob/master/notebooks/trend_changepoints.ipynb)
End of explanation
"""


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);


"""
Explanation: 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.
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.
End of explanation
"""


