Changepoint Detection

You can detect trend and seasonality changepoints with just a few lines of code.

Provide your timeseries as a pandas dataframe with timestamp and value.

For example, to work with daily sessions data, your dataframe could look like this:

import pandas as pd
df = pd.DataFrame({
    "datepartition": ["2020-01-08-00", "2020-01-09-00", "2020-01-10-00"],
    "macrosessions": [10231.0, 12309.0, 12104.0]
})

The time column can be any format recognized by pd.to_datetime.

In this example, we’ll load a dataset representing log(daily page views) on the Wikipedia page for Peyton Manning. It contains values from 2007-12-10 to 2016-01-20. More dataset info here.

27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
 import warnings

 warnings.filterwarnings("ignore")

 import pandas as pd
 import plotly

 from greykite.algo.changepoint.adalasso.changepoint_detector import ChangepointDetector
 from greykite.framework.benchmark.data_loader_ts import DataLoaderTS
 from greykite.framework.templates.autogen.forecast_config import ForecastConfig
 from greykite.framework.templates.forecaster import Forecaster
 from greykite.framework.templates.model_templates import ModelTemplateEnum

 # Loads dataset into UnivariateTimeSeries
 dl = DataLoaderTS()
 ts = dl.load_peyton_manning_ts()
 df = ts.df  # cleaned pandas.DataFrame

Detect trend change points

Let’s plot the original timeseries. There are actually trend changes within this data set. The UnivariateTimeSeries class is used to store a timeseries and to provide basic description and plotting functions. The load_peyton_manning function automatically returns a UnivariateTimeSeries instance, however, for any df, you can always initialize a UnivariateTimeSeries instance and do further explorations. (The interactive plot is generated by plotly: click to zoom!)

56
57
 fig = ts.plot()
 plotly.io.show(fig)

ChangepointDetector utilizes pre-filters, regularization with regression based models, and post-filters to find time points where trend changes.

To create a simple trend changepoint detection model, we first initialize the ChangepointDetector class, then run its attribute function find_trend_changepoints.

67
68
69
70
71
72
 model = ChangepointDetector()
 res = model.find_trend_changepoints(
     df=df,            # data df
     time_col="ts",    # time column name
     value_col="y")    # value column name
 pd.DataFrame({"trend_changepoints": res["trend_changepoints"]})  # prints a dataframe showing the result
trend_changepoints
0 2008-02-06
1 2008-07-06
2 2008-09-20
3 2008-12-18
4 2009-02-13
5 2009-06-08
6 2009-09-03
7 2009-12-07
8 2010-02-04
9 2010-07-02
10 2010-10-30
11 2011-01-24
12 2011-04-21
13 2011-07-16
14 2011-10-11
15 2011-12-09
16 2012-02-06
17 2013-02-15
18 2013-08-08
19 2014-01-28
20 2014-03-27
21 2014-12-12
22 2015-06-03


The code above runs trend changepoint detection with the default parameters. We may visualize the detection results by plotting it with the attribute function plot.

79
80
 fig = model.plot(plot=False)  # plot = False returns a plotly figure object.
 plotly.io.show(fig)