/darts

A python library for user-friendly forecasting and anomaly detection on time series.

Primary LanguagePythonApache License 2.0Apache-2.0

Time Series Made Easy in Python

darts


PyPI version Conda Version Supported versions Docker Image Version (latest by date) GitHub Release Date GitHub Workflow Status Downloads Downloads codecov Code style: black Join the chat at https://gitter.im/u8darts/darts

Darts is a Python library for user-friendly forecasting and anomaly detection on time series. It contains a variety of models, from classics such as ARIMA to deep neural networks. The forecasting models can all be used in the same way, using fit() and predict() functions, similar to scikit-learn. The library also makes it easy to backtest models, combine the predictions of several models, and take external data into account. Darts supports both univariate and multivariate time series and models. The ML-based models can be trained on potentially large datasets containing multiple time series, and some of the models offer a rich support for probabilistic forecasting.

Darts also offers extensive anomaly detection capabilities. For instance, it is trivial to apply PyOD models on time series to obtain anomaly scores, or to wrap any of Darts forecasting or filtering models to obtain fully fledged anomaly detection models.

Documentation

High Level Introductions
Articles on Selected Topics

Quick Install

We recommend to first setup a clean Python environment for your project with Python 3.7+ using your favorite tool (conda, venv, virtualenv with or without virtualenvwrapper).

Once your environment is set up you can install darts using pip:

pip install darts

For more details you can refer to our installation instructions.

Example Usage

Forecasting

Create a TimeSeries object from a Pandas DataFrame, and split it in train/validation series:

import pandas as pd
from darts import TimeSeries

# Read a pandas DataFrame
df = pd.read_csv("AirPassengers.csv", delimiter=",")

# Create a TimeSeries, specifying the time and value columns
series = TimeSeries.from_dataframe(df, "Month", "#Passengers")

# Set aside the last 36 months as a validation series
train, val = series[:-36], series[-36:]

Fit an exponential smoothing model, and make a (probabilistic) prediction over the validation series' duration:

from darts.models import ExponentialSmoothing

model = ExponentialSmoothing()
model.fit(train)
prediction = model.predict(len(val), num_samples=1000)

Plot the median, 5th and 95th percentiles:

import matplotlib.pyplot as plt

series.plot()
prediction.plot(label="forecast", low_quantile=0.05, high_quantile=0.95)
plt.legend()
darts forecast example

Anomaly Detection

Load a multivariate series, trim it, keep 2 components, split train and validation sets:

from darts.datasets import ETTh2Dataset

series = ETTh2Dataset().load()[:10000][["MUFL", "LULL"]]
train, val = series.split_before(0.6)

Build a k-means anomaly scorer, train it on the train set and use it on the validation set to get anomaly scores:

from darts.ad import KMeansScorer

scorer = KMeansScorer(k=2, window=5)
scorer.fit(train)
anom_score = scorer.score(val)

Build a binary anomaly detector and train it over train scores, then use it over validation scores to get binary anomaly classification:

from darts.ad import QuantileDetector

detector = QuantileDetector(high_quantile=0.99)
detector.fit(scorer.score(train))
binary_anom = detector.detect(anom_score)

Plot (shifting and scaling some of the series to make everything appear on the same figure):

import matplotlib.pyplot as plt

series.plot()
(anom_score / 2. - 100).plot(label="computed anomaly score", c="orangered", lw=3)
(binary_anom * 45 - 150).plot(label="detected binary anomaly", lw=4)
darts anomaly detection example

Features

  • Forecasting Models: A large collection of forecasting models; from statistical models (such as ARIMA) to deep learning models (such as N-BEATS). See table of models below.

  • Anomaly Detection The darts.ad module contains a collection of anomaly scorers, detectors and aggregators, which can all be combined to detect anomalies in time series. It is easy to wrap any of Darts forecasting or filtering models to build a fully fledged anomaly detection model that compares predictions with actuals. The PyODScorer makes it trivial to use PyOD detectors on time series.

  • Multivariate Support: TimeSeries can be multivariate - i.e., contain multiple time-varying dimensions instead of a single scalar value. Many models can consume and produce multivariate series.

  • Multiple series training (global models): All machine learning based models (incl. all neural networks) support being trained on multiple (potentially multivariate) series. This can scale to large datasets too.

  • Probabilistic Support: TimeSeries objects can (optionally) represent stochastic time series; this can for instance be used to get confidence intervals, and many models support different flavours of probabilistic forecasting (such as estimating parametric distributions or quantiles). Some anomaly detection scorers are also able to exploit these predictive distributions.

  • Past and Future Covariates support: Many models in Darts support past-observed and/or future-known covariate (external data) time series as inputs for producing forecasts.

  • Static Covariates support: In addition to time-dependent data, TimeSeries can also contain static data for each dimension, which can be exploited by some models.

  • Hierarchical Reconciliation: Darts offers transformers to perform reconciliation. These can make the forecasts add up in a way that respects the underlying hierarchy.

  • Regression Models: It is possible to plug-in any scikit-learn compatible model to obtain forecasts as functions of lagged values of the target series and covariates.

  • Explainability: Darts has the ability to explain some forecasting models using Shap values.

  • Data processing: Tools to easily apply (and revert) common transformations on time series data (scaling, filling missing values, differencing, boxcox, ...)

  • Metrics: A variety of metrics for evaluating time series' goodness of fit; from R2-scores to Mean Absolute Scaled Error.

  • Backtesting: Utilities for simulating historical forecasts, using moving time windows.

  • PyTorch Lightning Support: All deep learning models are implemented using PyTorch Lightning, supporting among other things custom callbacks, GPUs/TPUs training and custom trainers.

  • Filtering Models: Darts offers three filtering models: KalmanFilter, GaussianProcessFilter, and MovingAverageFilter, which allow to filter time series, and in some cases obtain probabilistic inferences of the underlying states/values.

  • Datasets The darts.datasets submodule contains some popular time series datasets for rapid and reproducible experimentation.

Forecasting Models

Here's a breakdown of the forecasting models currently implemented in Darts. We are constantly working on bringing more models and features.

Model Univariate Multivariate Probabilistic Multiple series (global) Past-observed covariates Future-known covariates Static covariates Reference
ARIMA
VARIMA
AutoARIMA
StatsForecastAutoARIMA (faster AutoARIMA) Nixtla's statsforecast
ExponentialSmoothing
StatsForecastETS Nixtla's statsforecast
StatsForecastAutoCES Nixtla's statsforecast
BATS and TBATS TBATS paper
Theta and FourTheta Theta & 4 Theta
StatsForecastAutoTheta Nixtla's statsforecast
Prophet (see install notes) Prophet repo
FFT (Fast Fourier Transform)
KalmanForecaster using the Kalman filter and N4SID for system identification N4SID paper
Croston method
RegressionModel; generic wrapper around any sklearn regression model
RandomForest
LinearRegressionModel
LightGBMModel
CatBoostModel
XGBModel
RNNModel (incl. LSTM and GRU); equivalent to DeepAR in its probabilistic version DeepAR paper
BlockRNNModel (incl. LSTM and GRU)
NBEATSModel N-BEATS paper
NHiTSModel N-HiTS paper
TCNModel TCN paper, DeepTCN paper, blog post
TransformerModel
TFTModel (Temporal Fusion Transformer) TFT paper, PyTorch Forecasting
DLinearModel DLinear paper
NLinearModel NLinear paper
Naive Baselines

Community & Contact

Anyone is welcome to join our Discord server Gitter room to ask questions, make proposals, discuss use-cases, and more. If you spot a bug or or have suggestions, GitHub issues are also welcome.

If what you want to tell us is not suitable for Discord or Github, feel free to send us an email at darts@unit8.co for darts related matters or info@unit8.co for any other inquiries.

Contribute

The development is ongoing, and we welcome suggestions, pull requests and issues on GitHub. All contributors will be acknowledged on the change log page.

Before working on a contribution (a new feature or a fix), check our contribution guidelines.

Citation

If you are using Darts in your scientific work, we would appreciate citations to the following JMLR paper.

Darts: User-Friendly Modern Machine Learning for Time Series

Bibtex entry:

@article{JMLR:v23:21-1177,
  author  = {Julien Herzen and Francesco Lässig and Samuele Giuliano Piazzetta and Thomas Neuer and Léo Tafti and Guillaume Raille and Tomas Van Pottelbergh and Marek Pasieka and Andrzej Skrodzki and Nicolas Huguenin and Maxime Dumonal and Jan Kościsz and Dennis Bader and Frédérick Gusset and Mounir Benheddi and Camila Williamson and Michal Kosinski and Matej Petrik and Gaël Grosch},
  title   = {Darts: User-Friendly Modern Machine Learning for Time Series},
  journal = {Journal of Machine Learning Research},
  year    = {2022},
  volume  = {23},
  number  = {124},
  pages   = {1-6},
  url     = {http://jmlr.org/papers/v23/21-1177.html}
}