Open In Colab

Fitting a changing trend#

We will use the time series of the log daily page views for the Wikipedia page for Peyton Manning as an example to illustrate how to fit a changing trend.

First, we load the data:

if "google.colab" in str(get_ipython()):
    !pip install git+ # may take a while
    #!pip install neuralprophet # much faster, but may not have the latest upgrades/bugfixes

import pandas as pd
from neuralprophet import NeuralProphet, set_log_level

data_location = ""
df = pd.read_csv(data_location + "wp_log_peyton_manning.csv")
ds y
0 2007-12-10 9.5908
1 2007-12-11 8.5196
2 2007-12-12 8.1837

Now we can fit an initial model without any customizations.

We specify the data frequency to be daily. The model will remember this later when we predict into the future.

m = NeuralProphet()
metrics =, freq="D")

SmoothL1Loss MAE RMSE RegLoss
0 0.638072 4.693779 5.698257 0.0
1 0.505663 4.038373 4.905737 0.0
2 0.345206 3.196466 3.885827 0.0

The returned metrics dataframe contains recoded metrics for each training epoch.

Next, we create a dataframe to predict on. Here, we specify that we want to predict one year into the future and that we want to include the entire history.

future = m.make_future_dataframe(df, periods=365, n_historic_predictions=True)
ds y
3326 2017-01-17 NaN
3327 2017-01-18 NaN
3328 2017-01-19 NaN

Note: ‘y’ and ‘y_scaled’ are not given for the period extending into the future, as we do not know their true values.

forecast = m.predict(future)
['ds', 'y', 'yhat1', 'residual1', 'trend', 'season_yearly', 'season_weekly']

The returned forecast dataframe contains the original datestamps, ‘y’ values, the predicted ‘yhat’ values, residuals and all the individual model components.

# plots the model predictions
fig1 = m.plot(forecast)
# plots the individual forecast components for the given time period.
# fig = m.plot_components(forecast, residuals=True)
# visualizes the model parameters.
fig2 = m.plot_parameters()

Adjusting Trend#

The default values work fairly well in this example. However, the default of 5 changepoints may not be adequate if the actual change in trend happens to fall in a region between the points.

Increasing Trend Flexibility#

We can address this by increasing the number of changepoints, giving the trend more flexibility, at the danger of overfitting.

Let’s try what happens if we increase the number of changepoints to 30. Additionally, we can increase the range of data on which we fit trend changepoints to only exlude the last 10 percent (default is 20 percent).

Further, we can limit the trend flexibility by regularizing it.

[ ]:
m = NeuralProphet(
metrics =, freq="D")
forecast = m.predict(df)
fig1 = m.plot(forecast)
fig2 = m.plot_parameters()

Looking at the trend rate changes it becomes evident that the trend is overfitting to short-term fluctuations.

Manual Trend Changepoints#

You can also manually specify the trend changepoints.

Note: A changepoint will always be added at the beginning. You can ignore it.

m = NeuralProphet(
    changepoints=["2012-01-01", "2014-01-01"],
metrics =, freq="D")
forecast = m.predict(df)

fig1 = m.plot(forecast)
fig2 = m.plot_parameters()
[ ]: