Skip to main content
This tutorial demonstrates how to generate sample trajectories (simulated paths) using StatsForecast.
Prerequisites This tutorial assumes basic familiarity with StatsForecast. For a minimal example visit the Quick Start

Introduction

While standard forecasting methods often produce a single point forecast or prediction intervals, some scenarios require understanding the full range of possible future paths. Trajectory simulation allows you to generate multiple possible future realizations of a time series based on the fitted model’s error distribution. This is particularly useful for: - Risk analysis and stress testing. - Scenario planning (e.g., “what if” analyses). - Calculating complex metrics based on future paths (e.g., probability of breach). By the end of this tutorial, you’ll be able to use the simulate method in StatsForecast to generate and visualize these paths using various error distributions.
Important Trajectory simulation is currently supported for AutoARIMA and other statistical models that implement the simulate interface.
Outline:
  1. Install libraries
  2. Load and explore the data
  3. Basic simulation (Normal Distribution)
  4. Automatic parameter inference
  5. User-provided parameters
  6. Comparing distributions
  7. Handling large simulations
Tip You can use Colab to run this Notebook interactively Open In Colab

Install libraries

We assume that you have StatsForecast already installed. If not, check this guide for instructions on how to install StatsForecast

Load and explore the data

We’ll use a subset of the hourly dataset from the M4 Competition.

Basic Simulation (Normal Distribution)

A simulation is performed by calling the simulate method after fitting. By default, it samples from a Normal distribution using the model’s estimated variance from the residuals.
The output contains a sample_id column to distinguish between different trajectories.

Automatic Parameter Inference

StatsForecast can automatically infer distribution parameters from your model’s residuals. When you don’t specify error_params, the system uses Maximum Likelihood Estimation (MLE) to fit the distribution parameters to the residuals. This is particularly useful when you want the simulation to reflect the actual characteristics of your data’s errors.

Supported Distributions

  • ‘normal’: Standard normal distribution (default)
  • ‘t’: Student’s t-distribution (heavy tails, good for financial data)
  • ‘bootstrap’: Resample from empirical residuals (non-parametric)
  • ‘laplace’: Laplace distribution (sharper peak, heavier tails)
  • ‘skew-normal’: Skewed normal distribution (for asymmetric errors)
  • ‘ged’: Generalized Error Distribution (flexible shape)
Let’s demonstrate with different distributions. Note that parameters are automatically estimated from residuals.

User-Provided Parameters

Instead of relying on automatic inference, you can explicitly specify distribution parameters using the error_params dictionary. This gives you precise control over the simulation characteristics. This is useful when: - You have domain knowledge about the error distribution - You want to stress-test with extreme scenarios - You want to ensure consistency across different datasets

Parameter Specifications

  • ‘t’: {'df': degrees_of_freedom} - Controls tail heaviness (lower = heavier tails)
  • ‘skew-normal’: {'skewness': alpha} - Controls asymmetry (negative = left skew, positive = right skew)
  • ‘ged’: {'shape': beta} - Controls tail behavior (1 = Laplace, 2 = Normal, higher = lighter tails)

Comparing Distributions

Let’s visualize the differences between automatic inference and user-provided parameters. We’ll compare:
  1. Normal - The baseline distribution
  2. t-distribution (automatic) - Parameters estimated from residuals
  3. t-distribution (df=3) - User-specified heavy tails for stress testing
  4. Bootstrap - Non-parametric resampling from actual residuals

Key Observations

  1. Normal distribution provides symmetric paths around the mean - suitable when errors are well-behaved
  2. t-distribution (automatic) adapts to the data’s error characteristics, producing paths that reflect the actual residual distribution
  3. t-distribution (df=3) with user-specified parameters creates more extreme scenarios, useful for stress testing and risk analysis
  4. Bootstrap uses the empirical error distribution directly, making no parametric assumptions
When to use automatic inference: - You want simulations that reflect your data’s actual error characteristics - You don’t have prior knowledge about the error distribution - You want the model to learn from the residuals When to provide custom parameters: - You have domain expertise about the error distribution - You want to test specific scenarios (e.g., extreme market conditions) - You need reproducible behavior across different datasets - You want to explore “what-if” scenarios with controlled assumptions

Quantitative Comparison

Let’s examine the statistical properties of the simulated paths to understand how each distribution affects the forecasts.

References

Rob J. Hyndman and George Athanasopoulos (2018). “Forecasting principles and practice, The Statistical Forecasting Perspective”.