Time-Series Forecasting with Darts: A Hands-On Tutorial - Magnimind Academy
Info Session on Mentor-Led Data Science Internship Program
00 DAYS
:
08 HR
:
01 MINS
:
57 SEC
×

Time-series forecasting is an essential machine learning task with applications in demand prediction, and financial forecasting, among other tasks. That led us to Darts: a simple yet powerful Python library that offers a unified interface for various forecasting models to make time-series analysis easier. You will cover the basics of Darts, how to install it, and how to implement demand prediction in Python with machine learning methods.

1. Introduction to Darts

Darts is an open-source Python library that makes time-series forecasting easy and convenient, building a uniform API for a variety of forecasting models. Developed by Unit8, it supports classical statistical (ARIMA, Exponential Smoothing), machine learning (Gradient Boosting, Random Forest), and deep learning (RNNs, LSTMs, Transformer-based) models. Its main advantage is its capability to model univariate and multivariate time series, thus serving many real-world applications in finance, health care, sales forecasting, and supply chain management [1].

1.1 Why Use Darts?

Darts has quite a few advantages over common time-series forecasting frameworks:

  • Wide range of forecasting models: It supports popular forecasting methods such as ARIMA, Prophet, Theta, RNNs, and Transformer-based architectures with built-in implementations so that users can experiment with different approaches with limited coding [2].
  • Seamless data handling: The combination of its ease of integration with Pandas, NumPy, and PyTorch allows individuals to become competent in data manipulation and processing. Users can manipulate time-indexed data structures like Pandas DataFrames.
  • Preprocessing and feature engineering utilities: Darts offers tools for missing value imputation, scaling, feature extraction, and data transformations, simplifying data preparation for forecasting tasks.
  • Probabilistic forecasting: Unlike many traditional models, Darts supports probabilistic forecasting, allowing users to estimate confidence intervals and quantify uncertainties in predictions, which is crucial in risk-sensitive applications [3]
  • Backtesting and evaluation: The library allows you to check model validity using backtesting, and then check the accuracy of those models against a set of error metrics using past data (e.g., MAPE, RMSE, and MAE).
  • Ensemble forecasting: Darts allows for combining multiple forecasting models, improving accuracy by leveraging the strengths of different methods.

1.2 Use Cases

Darts are widely used for industries that require accurate forecasting of time series:

  • Financial forecasting (e.g., stock price prediction, risk analysis)
  • Healthcare analytics (e.g., patient admissions, medical supply demand)
  • Retail and demand forecasting (e.g., sales forecasting, inventory management)
  • Energy sector (e.g., electricity consumption predictions)

Darts combines approachability, versatility, and powerful forecasting capabilities to make time-series analysis more mainstream for researchers and practitioners.

 

1.3 Installing and Setting Up Darts

Before we jump into time-series forecasting, let’s install the Darts library using pip:

Time-Series Forecasting with Darts A Hands-On Tutorial

You are also required to install other dependencies like Pandas, NumPy, and Matplotlib:

After installing it, we can import the required modules:

1.4 Loading and Preparing Data

For this tutorial, let’s say we have some historical sales data in a CSV file:

Make sure your dataset is indexed properly with DateTime:

This effectively converts the Pandas DataFrame into a Darts TimeSeries object, which we need for modeling.

 

2. Preprocessing Data

To improve model performance, normalize the data:

Removing missing values is very important in time-series forecasting. Native imputation techniques to handle missing values are also available in Darts — e.g. forward fill, interpolation, machine-learning-based ones, etc. These tools and frameworks prevent biases resulting from the familiarity of partial data sets, which promote data consistency and accurately anticipate trends.

3. Choosing a Forecasting Model

Some of the models that Darts provide are:

3.1 Exponential Smoothing (ETS)

The Error, Trend, and Seasonality (ETS) model is a well-known statistical model for forecasting purposes widely used that splits a time series into three parts: Error(E), Trend(T), and Seasonality(S) and it can provide significant insight or prediction of time series data when these features are represented in variance [4].

Why Use the ETS Model?

ETS is useful because it offers a flexible approach to time series forecasting, and it provides a wide range of trends and seasonal patterns. While ARIMA uses differences to address trends, ETS is a series of new smoothing techniques to model trends/seasonality. This approach is highly applicable to time series data because there is usually a strong seasonality and trend pattern in it; therefore ETS is one of the perfect models among them [5].

When Does ETS Work Best?

ETS performs best under the following conditions:

  • There is a visible trend and/or seasonality in the data.
  • In particular, the forecasting problem needs an interpretable decomposition of trend and seasonality.
  • The variance of the errors remains stable over time (ETS assumes homoscedasticity).

However, ETS does not perform well when:

  • The data has strong autocorrelations that require differencing (ARIMA is preferable).
  • External covariates significantly impact the time series (requiring regression-based models).
  • The dataset has non-linear patterns that require more flexible machine learning approaches.

3.2  ARIMA

ARIMA (Autoregressive Integrated Moving Average) is a robust statistical method for time series forecasting. ARIMA is a linear model that consists of three components: Autoregression (AR) component, Integration (I) component, and Moving Average (MA) component which explain indices of the data. ARIMA is helpful for non-stationary time series as it applies differencing to the data to make a time series stationary and then only uses autoregressive and moving average components [6].

Why Use the ARIMA Model?

ARIMA is a popular technique because it models temporal dependencies in the time series data itself, and does not need to require the explicit decomposition of trend and seasonality. ATS models focus only on smoothing trends and seasonal components, while ARIMA considers such things as serial correlations and random fluctuations in the data. ARIMA is also a flexible model where hyperparameters (p, d, q) could be adjusted for various time series trends [7].

When Does ARIMA Work Best?

ARIMA is most effective when:

  • The time series is highly autocorrelated.
  • The data isn’t stationary but can be moved toward it using differencing.
  • Seasonal effects are either negligible or treated separately with SARIMA.
  • The goal is forecasting future values based on past observations rather than external predictors.

However, ARIMA struggles when:

  • The dataset has strong seasonal patterns (SARIMA or ETS may perform better).
  • External factors significantly impact the data, requiring hybrid models like ARIMAX.
  • The time series is highly volatile and exhibits non-linearity, making machine learning or deep learning models preferable [8].

 

3.3 Prophet

The Prophet model, developed by Facebook (now Meta), is an open-source forecasting tool designed for handling time series data with strong seasonal patterns and missing values. It is particularly useful for business and economic forecasting, as it provides automatic trend and seasonality detection while allowing users to incorporate external factors as regressors [9].

Why Use the Prophet Model?

Prophet is beneficial because it is highly automated, interpretable, and robust to missing data and outliers. Unlike ARIMA, which requires manual parameter tuning, Prophet automatically detects changepoints and seasonal patterns, making it easier to use for non-experts. It also supports additive and multiplicative seasonality, making it suitable for datasets where seasonal effects change over time [10].

When Does Prophet Work Best?

Prophet is ideal for:

  • Business and financial data with strong seasonality (e.g., daily or weekly trends).
  • Long-term forecasting with historical patterns that repeat over time.
  • Irregular time series with missing data or gaps.
  • Datasets with trend shifts, as it automatically detects changepoints.
  • Scenarios requiring external regressors, such as holidays or promotions.

However, Prophet is not ideal when:

  • The time series has high-frequency fluctuations that do not follow smooth trends.
  • The data is dominated by short-term autocorrelations rather than seasonal patterns (ARIMA may work better).
  • Computational efficiency is a concern, as Prophet can be slower than simpler models like ARIMA or ETS [11].

3.4  Deep Learning with RNN

The Recurrent Neural Network (RNN) is a class of artificial neural networks designed for sequential data, making it highly effective for time series forecasting, speech recognition, and natural language processing. Unlike traditional feedforward neural networks, RNNs have internal memory that allows them to capture temporal dependencies by maintaining a hidden state across time steps [12].

Why Use RNNs?

RNNs are particularly useful for modeling sequential patterns where previous inputs influence future predictions. Unlike traditional statistical models like ARIMA and ETS, which assume linear relationships, RNNs can learn complex, non-linear dependencies in time series data. They are also more flexible, as they do not require assumptions about stationarity or predefined trend/seasonality structures [13].

When Do RNNs Work Best?

RNNs are effective in cases where:

  • Long-term dependencies exist in the data, and past values influence future predictions.
  • Non-linear relationships need to be captured, which traditional models struggle with.
  • High-dimensional time series demand extraction of features and learning from multiple input sources.
  • We need to model time series with irregular space and also without strict assumptions.

However, RNNs face challenges when:

  • Vanishing/exploding gradients occur, making training difficult for long sequences (solved by LSTMs and GRUs).
  • Large datasets and computational power are required for training.
  • Deep learning models are often considered black boxes compared to ARIMA or Prophet [14], demanding interpretability. [14].

4. Evaluating Model Performance

MAPE is one of the most common techniques to determine how good a forecasting model is. This measure provides the mean relative difference between predicted and actual values, so it is useful for evaluating a model. MAPE gives error in percentage, unlike absolute error metrics like MSE, hence, it helps with easy interpretation while comparing across various datasets with different scales. This is especially helpful in environments where the relative error is more important than the absolute deviations, such as demand forecasting, stock market predictions, and economic modeling [15].

Why Use MAPE?

MAPE is helpful as it gives a unitless error measure and hence can be used across datasets with units. The latter permits the comparison of different forecasting models on a meaningful basis, thus enabling analysts to identify the most stable one. MAPE is easy to calculate and interpret; thus, it is incredibly common in practice, including areas such as business prediction, supply chain, and finance. In these fields, Mean Absolute Percentage Error (MAPE) is used to assess forecast accuracy and improve planning strategies [16].

Now we have a trained model so a lower MAPE score is expected. A lower score indicates better performance.

5. Backtesting for Model Validation

Backtesting is the system to check the accuracy of a model and the working of the model is tested on historical data and then the future is predicted by using the model. This technique evaluates the ways that the model would have acted in the wild, identifying any biases or weaknesses. Analysts can fine-tune and calibrate the model by comparing predicted values with actual historical events, improving reliability. However, model backtesting is paramount for ascertaining that models are performing as intended and that they are relevant for decision-making in ever-changing environments.

6. Making Future Predictions

The best model, which is chosen using the observed patterns and trends from historical data is now used for prediction. You trained the model on new data as the data would not let your model go old. Also, check your predictions against what happened and adjust Parameters if necessary. Through this iterative process for increasing predictive performance and providing decision-making support to fast-evolving agile functional ecosystems.

7. Conclusion

Darts is a library that provides a unified interface for different time-series forecasting models, allowing us to implement demand prediction and other forecasting tasks. Such a framework can be highly extensible and can allow a user to easily combine classical statistical models such as ETS and ARIMA with new machine learning and deep learning models such as Prophet, RNNs, and Transformer-based architectures. In this tutorial, we have covered some important steps like data preprocessing and transformation in which we have cleaned and prepared the time-series data to be used for prediction. Next, we evaluated various forecasting models from classical methods for baseline prediction to state-of-the-art models able to identify complex patterns. We also discussed model evaluation and backtesting, making sure predictions are validated with historical data and proper error metrics. Users can try out various models and adjust hyperparameters to achieve optimal performance and improved forecasting accuracy. Thanks to the versatility and capabilities of Darts, it is now easier and more effective to predict demand or perform time-series analysis! Happy forecasting!

 

References

  1. Herzen, J., & Nicolai, J. (2021). Darts: User-Friendly Forecasting for Time Series. Journal of Machine Learning Research, 22(1), 1-6. Link
  2. Unit8 (2023). Darts: Time Series Made Easy. Retrieved from https://github.com/unit8co/darts.
  3. Bandara, K., Bergmeir, C., & Smyl, S. (2020). Forecasting Time Series with Darts: A Comprehensive Guide. International Journal of Forecasting, 36(3), 1012-1030. Link
  1. Hyndman, R. J., & Athanasopoulos, G. (2018). Forecasting: Principles and Practice. OTexts. Link
  2. Box, G. E. P., Jenkins, G. M., & Reinsel, G. C. (2015). Time Series Analysis: Forecasting and Control. Wiley. Link
  3. Hamilton, J. D. (1994). Time Series Analysis. Princeton University Press. Link
  4. Cryer, J. D., & Chan, K. S. (2008). Time Series Analysis With Applications in R. Springer. Link
  5. Shumway, R. H., & Stoffer, D. S. (2017). Time Series Analysis and Its Applications: With R Examples. Springer. Link
  6. Taylor, S. J., & Letham, B. (2018). Forecasting at Scale. The American Statistician, 72(1), 37-45. Link
  7. Meta (2023). Prophet: Forecasting Tool Documentation. Retrieved from Link
  8. Petropoulos, F., Apiletti, D., Assimakopoulos, V., Babai, M., Barrow, D., Ben Taieb, S., Bergmeir, C., et al. (2022). Forecasting: Theory and Practice. International Journal of Forecasting, 38, 705-871. https://doi.org/10.1016/j.ijforecast.2021.11.001
  9. Hochreiter, S., & Schmidhuber, J. (1997). Long Short-Term Memory. Neural Computation, 9(8), 1735-1780. Link
  10. Goodfellow, I., Bengio, Y., & Courville, A. (2016). Deep Learning. MIT Press. Link
  11. Lipton, Z. C., Berkowitz, J., & Elkan, C. (2015). A Critical Review of Recurrent Neural Networks for Sequence Learning. arXiv preprint arXiv:1506.00019. Link
  12. Hyndman, R. J., & Koehler, A. B. (2006). Another Look at Measures of Forecast Accuracy. International Journal of Forecasting, 22(4), 679-688. Link
  13. Makridakis, S., Wheelwright, S. C., & Hyndman, R. J. (1998). Forecasting: Methods and Applications. Wiley. Link,

    Danish Hamid

Evelyn

Evelyn Miller

Related Articles