By GUPPI

‘The only constant in life is change’ an idea that comes from Greek philosopher Heraclitus holds true as we navigate through life. Change is a fundamental aspect of life. From the smallest particles to the largest galaxies, everything is in a constant state of change. Panta rhei, everything flows.

Generated by Stable Diffusion

Change is not limited to our personal lives but also affects businesses and organisations, and the data they collect. To better understand these shifts in data and make more accurate forecasts and predictions, we use changepoint detection.

Changepoint detection is a method used to identify sudden shifts in time-series data. Its objective is to uncover points in time where the underlying pattern or behaviour of a time series changes abruptly.

It can be applied in various fields and domains such as finance in which sudden changes in financial data such as stock prices or exchange rates can be detected, helping investors and traders optimise their decisions. Furthermore, trends and patterns in web traffic and sales can be forecasted to help businesses improve their inventory management or marketing efforts. Additional applications can be found in manufacturing, health care, environment sector and many more to detect changes in for instance sensor data, heart rate, blood pressure, temperature, air pollution, water quality and others.

In this blogpost, we will tackle the problem of detecting changepoints by using Meta’s Prophet library.

Types of changepoints

Change the way you look at things and the things you look at change. - Wayne W. Dyer

Before we dive deep into changepoint detection using Prophet, we will first briefly explain some types of changepoints we may encounter.

The first type of changepoint is one of the most obvious ones and entails a shift in mean value. An example here is if the energy consumption of a household has been relatively stable over time, but then suddenly increases significantly.

Figure 1: change in mean value

A second type shows a change in variance. This type of changepoint identifies sudden changes in the dispersion of time-series data and could reflect for example changes in the volatility of stock prices or exchange rates.

Figure 2: change in variance

A third type indicates a change in periodicity and will detect changes in the frequency/ rate at which events occur. For instance, if a bus route runs every 30 minutes, but suddenly runs every 20 minutes, this is considered a periodicity changepoint.

Figure 3: change in periodicity

Introduction to Prophet

The best prophet of the future is the past - George Byron

Prophet is a Python library developed by Meta for forecasting time series. It is based on an additive model incorporating a piecewise linear or logistic growth trend, periodic changes such as weekly and yearly seasonality and holiday effects. Prophet is robust to missing data and outliers.

From theory to practice

Experience without theory is blind, but theory without experience is mere intellectual play. - Immanuel Kant

Let’s get started with Prophet on real data! We first need to find data that will have a clear changepoint that we can identify. Given the recent significant increase in inflation, it will be an ideal candidate for our analysis.

Let’s plot the monthly Belgian inflation levels ranging from 2013 up till 2022:

Figure 4: Monthly Belgian inflation levels from 2013-2022

Altogether, we observe a fluctuating inflation rate and as assumed, inflation levels were surging mid 2021-2022.

Let’s now run Prophet on this time-series using default hyperparameter and threshold values:

Figure 5: Prophet’s detected changepoints using default hyperparameters and threshold values. The black dotted lines represent the actual time-series values. The blue solid line displays Prophet’s forecast (with confidence interval). The red dashed vertical lines show the changepoints detected.

Upon running Prophet on our inflation data, it appears that the model has identified 6 changepoints. While some of these changepoints align closely with the actual big surge around mid 2021, others were detected slightly too early around 2020.

But how exactly does Prophet detect these changepoints? It utilizes a Baysian approach. The model will initially specify a large number of potential changepoints (by default 25) at which a time series rate is allowed to change, but it will try to use as few of them as possible. We plotted the magnitude of the rate change at each changepoint below.

Figure 6: Magnitude of the rate change for each potential changepoint

We observe that most changepoints in fact go unused as their rate change magnitude is minimal. Now, Prophet also allows us to modify the threshold on trend change magnitude for significance. By increasing this threshold, the magnitude of a potential changepoint must be greater for it to be considered a real changepoint. Prophet’s default threshold value is 0.01. As seen in the plot above, there are 6 changepoints that exceed this threshold. By raising the threshold for the magnitude of the rate change, we can see a decrease in the number of changepoints identified. For instance, by setting the threshold at 0.3, we can observe that only 3 changepoints surpass this threshold and exhibit a higher rate change magnitude, as evident in the plot.

Figure 7: Increasing the threshold value to 0.3

Improve our model fit

The man who moves a mountain begins by carrying away small stones. - Confucius

Before, we detected changepoints leveraging Prophet’s default hyperparameters. As observed from the plot, the model fit is not super great yet: it does not model the fluctuations before 2022 and instead just fits a slightly increasing slope.

One of the most impactful parameters is the changepoint prior scale which determines the flexibility of the trend: how much a trend is allowed to change at the changepoints. However, increasing the value of this parameter allows for more changepoints to be identified, but if set too high, it can lead to overfitting of the trend. On the other hand, setting the value too low can result in underfitting of the trend.

Another parameter to be tuned is the seasonality prior scale which controls the flexibility of the seasonality. A large value allows for large seasonal fluctuation while a low value shrinks the influence of the seasonality. The default value is 10.

Let’s first increase the changepoint prior scale value to for example 0.5 while setting the threshold value to 0.5 as well.

Figure 8: (left) changepoints detected using a changepoint prior scale of 0.5. (right) The rate of change for each potential changepoint

The trend now is fitting more tightly the time-series data and we now find both more accurate increasing as well as decreasing changepoint, as can be seen from the positive and negative values for the magnitude of change in the right plot. Additionally, the trend is now allowed to change more significantly at each changepoint, as evidenced by the higher magnitudes of trend change in the right plot.

Figure 9: (left) seasonality prior scale set to 0.01 - (right) seasonality prior scale set to 20

Experimenting with different seasonality prior scales (0.01 and 20), we do not see a significant difference between the two plots and hence no influence on the detected changepoints were noted.

Of course, Prophet has a couple of other hyperparameters to tune and provides built-in cross-validation and hyperparameter tuning functionalities that provides a lot of flexibility when it comes to how cross-validation is done. One can customize how many days are used to train the model, every how many days (frequency) a forecast should be made and the duration of that forecast to measure forecast errors using historical data.

Perfection is not attainable, but if we chase perfection we can catch excellence. - Vince Lombardi

Conclusion

The only way to make sense out of change is to plunge into it, move with it, and join the dance. - Alan Watts

In conclusion, changepoint detection is a powerful tool for uncovering and forecasting trends in time series data. In this blog post, we have demonstrated how to use Prophet to detect changepoints in monthly Belgian inflation data. We highlighted the importance of selecting the appropriate threshold value for changepoint significance depending on your business use case. A higher threshold value will detect fewer, yet more substantial changepoints. Furthermore, we noted the influence of the changepoint prior scale parameter on the model fit and on the detected changepoints. However, we did not perform thorough hyperparameter tuning and since Prophet has a couple more hyperparameters to tune, results could still change. It is important to keep in mind that this is just a starting point and that further experimentation and tuning could yield even better results. To make the circle round, let’s end with another thought of Heraclitus: it is in change that we find purpose!

You might also like

Statistics Saga 2: Dimensionality Reduction - Chiel Mues
Welcome back! If you remember from last time [https://dataroots.io/research/contributions/statistics-saga-1-matrix-factorization] I told you we would continue with matrix factorisations, more specifically dive into some dimensionality reduction techniques! Hope you’re ready! This blogpost will giv…