Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/jiwidi/time-series-forecasting-with-python

A use-case focused tutorial for time series forecasting with python
https://github.com/jiwidi/time-series-forecasting-with-python

machine-learning python python-time-series time-series time-series-analysis time-series-forecasting time-series-prediction

Last synced: about 2 months ago
JSON representation

A use-case focused tutorial for time series forecasting with python

Awesome Lists containing this project

README

        

# :hourglass_flowing_sand: time-series-forecasting-wiki
This repository contains a series of analysis, transforms and forecasting models frequently used when dealing with time series. The aim of this repository is to showcase how to model time series from the scratch, for this we are using a real usecase dataset ([Beijing air polution dataset](https://archive.ics.uci.edu/ml/datasets/Beijing+PM2.5+Data) to avoid perfect use cases far from reality that are often present in this types of tutorials. If you want to rerun the notebooks make sure you install al neccesary dependencies, [Guide](docs/setup.md)

You can find the more detailed toc on the main [notebook](time-series-forecasting-tutorial.ipynb)

# :open_file_folder: Dataset

The dataset used is the [Beijing air quality](https://archive.ics.uci.edu/ml/datasets/Beijing+PM2.5+Data) public dataset. This dataset contains polution data from 2014 to 2019 sampled every 10 minutes along with extra weather features such as preassure, temperature etc. We decided to resample the dataset with daily frequency for both easier data handling and proximity to a real use case scenario (no one would build a model to predict polution 10 minutes ahead, 1 day ahead looks more realistic). In this case the series is already stationary with some small seasonalities which change every year #MORE ONTHIS

In order to obtain a exact copy of the dataset used in this tutorial please run the [script](https://github.com/jiwidi/time-series-forecasting-wiki/blob/master/datasets/download_datasets.py) under `datasets/download_datasets.py` which will automatically download the dataset and preprocess it for you.

# 📚 Analysis and transforms

* Time series decomposition
* Level
* Trend
* Seasonality
* Noise

* Stationarity
* AC and PAC plots
* Rolling mean and std
* Dickey-Fuller test

* Making our time series stationary
* Difference transform
* Log scale
* Smoothing
* Moving average

# :triangular_ruler: Models tested

* Autoregression ([AR](https://www.statsmodels.org/stable/generated/statsmodels.tsa.ar_model.AR.html))
* Moving Average (MA)
* Autoregressive Moving Average (ARMA)
* Autoregressive integraded moving average (ARIMA)
* Seasonal autoregressive integrated moving average (SARIMA)
* Bayesian regression [Link](https://scikit-learn.org/stable/auto_examples/linear_model/plot_bayesian_ridge.html)
* Lasso [Link](https://scikit-learn.org/stable/modules/generated/sklearn.linear_model.Lasso.html)
* SVM [Link](https://scikit-learn.org/stable/modules/classes.html?highlight=svm#module-sklearn.svm)
* Randomforest [Link](https://scikit-learn.org/stable/modules/generated/sklearn.ensemble.RandomForestRegressor.html?highlight=randomforest#sklearn.ensemble.RandomForestRegressor)
* Nearest neighbors [Link](https://scikit-learn.org/stable/modules/neighbors.html)
* XGBoost [Link](https://xgboost.readthedocs.io/en/latest/)
* Lightgbm [Link](https://github.com/microsoft/LightGBM)
* Prophet [Link](https://facebook.github.io/prophet/docs/quick_start.html)
* Long short-term memory with tensorflow (LSTM)[Link](https://www.tensorflow.org/)

* DeepAR

# :mag: Forecasting results
We will devide our results wether the extra features columns such as temperature or preassure were used by the model as this is a huge step in metrics and represents two different scenarios. Metrics used were:

## Evaluation Metrics
* Mean Absolute Error (MAE)
* Mean Absolute Percentage Error (MAPE)
* Root Mean Squared Error (RMSE)
* Coefficient of determination (R2)

Model
mae
rmse
mape
r2

EnsembleXG+TF
27.64
40.23
0.42
0.76

EnsembleLIGHT+TF
27.34
39.27
0.42
0.77

EnsembleXG+LIGHT+TF
27.63
39.69
0.44
0.76

EnsembleXG+LIGHT
29.95
42.7
0.52
0.73

Randomforest tunned
40.79
53.2
0.9
0.57

SVM RBF GRID SEARCH
38.57
50.34
0.78
0.62

DeepAR
71.37
103.97
0.96
-0.63

Tensorflow simple LSTM
30.13
43.08
0.42
0.72

Prophet multivariate
38.25
50.45
0.74
0.62

Kneighbors
57.05
80.39
1.08
0.03

SVM RBF
40.81
56.03
0.79
0.53

Lightgbm
30.21
42.76
0.52
0.72

XGBoost
32.13
45.59
0.56
0.69

Randomforest
45.84
59.45
1.03
0.47

Lasso
39.24
54.58
0.71
0.55

BayesianRidge
39.24
54.63
0.71
0.55

Prophet univariate
61.33
83.64
1.26
-0.05

AutoSARIMAX (1, 0, 1),(0, 0, 0, 6)
51.29
71.49
0.91
0.23

SARIMAX
51.25
71.33
0.91
0.23

AutoARIMA (0, 0, 3)
47.01
64.71
1.0
0.37

ARIMA
48.25
66.39
1.06
0.34

ARMA
47.1
64.86
1.01
0.37

MA
49.04
66.2
1.05
0.34

AR
47.24
65.32
1.02
0.36

HWES
52.96
74.67
1.11
0.16

SES
52.96
74.67
1.11
0.16

Yesterdays value
52.67
74.52
1.04
0.16

Naive mean
59.38
81.44
1.32
-0.0

# :shipit: Additional resources and literature

## Models not tested but that are gaining popularity
There are several models we have not tried in this tutorials as they come from the academic world and their implementation is not 100% reliable, but is worth mentioning them:

* Neural basis expansion analysis for interpretable time series forecasting (N-BEATS) | [link](https://arxiv.org/abs/1905.10437) [Code](https://github.com/philipperemy/n-beats)
* ESRRN [link](https://eng.uber.com/m4-forecasting-competition/) [Code](https://github.com/damitkwr/ESRNN-GPU)

#
| | |
| - | - |
| Adhikari, R., & Agrawal, R. K. (2013). An introductory study on time series modeling and forecasting | [[1]](https://arxiv.org/ftp/arxiv/papers/1302/1302.6613.pdf)|
| Introduction to Time Series Forecasting With Python | [[2]](https://machinelearningmastery.com/introduction-to-time-series-forecasting-with-python/)|
| Deep Learning for Time Series Forecasting | [[3]](https://machinelearningmastery.com/deep-learning-for-time-series-forecasting/ )
| The Complete Guide to Time Series Analysis and Forecasting| [[4]](https://towardsdatascience.com/the-complete-guide-to-time-series-analysis-and-forecasting-70d476bfe775)|
| How to Decompose Time Series Data into Trend and Seasonality| [[5]](https://machinelearningmastery.com/decompose-time-series-data-trend-seasonality/)

# Contributing
Want to see another model tested? Do you have anything to add or fix? I'll be happy to talk about it! Open an issue/PR :)