# nixtla/statsforecast architecture

> Architecture diagram of the GitHub repository nixtla/statsforecast, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-03-28). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/nixtla/statsforecast
- Repository: https://github.com/nixtla/statsforecast

## Overview

The StatsForecast project is a Python library built for high-performance time series forecasting. It provides a rich suite of statistical and econometric models (including ARIMA, ETS, CES, Theta, among others) and is optimized via numba and C++ components to deliver lightning-fast predictions. It also includes adapters and integrations with distributed computing tools like Spark, Dask, and Ray, along with a suite of experiments and notebooks for benchmarking and demonstration.

##### Main Components
- API & Core Library: Represent the primary user interface, which is the StatsForecast class. Show that users initialize this class and call its .fit and .predict methods.
- Model Implementations: List and group the different forecasting models provided (AutoARIMA, AutoETS, AutoCES, AutoTheta, etc.). Indicate that these models are defined in python modules (e.g., python/statsforecast/models.py and in individual model files such as arima.py, ets.py, theta.py).
- Performance Modules: Highlight the C++ components (under the “src” folder with files like arima.cpp, ets.cpp, theta.cpp, statsforecast.cpp) that are likely integrated with the Python library to improve performance.
- Adapters & Distributed Frameworks: Include components such as adapters for Prophet (in python/statsforecast/adapters) and modules for distributed processing (e.g., python/statsforecast/distributed containing modules for Fugue and multi-process execution).
- Utilities and Helpers: Represent the utils files (e.g., python/statsforecast/utils.py) which assist in data preprocessing, feature engineering, and orchestration.
- Experiments and Examples: Show the role of the experiments folder and notebooks (in nbs and experiments folders) that serve both as documentation (for end users) and benchmarking tools for model performance and scalability.

##### Relationships and Interactions
- That the StatsForecast API interacts with the various model components: users supply data to the API, which then delegates to a selected forecasting model.
- Directional flow from data input → API controller (StatsForecast) → model selection and fitting → predictions output.
- Integration points where adapters enable distributed or external processing (for instance, integration with Spark, Ray, Dask).

##### Architectural Patterns and Design Principles
- Modular Design: The separation into clear modules (core models, adapters, distributed, utilities) promotes loose coupling and ease of extension.
- Layered Architecture: Show the high-level API layer on top, model implementations in the middle layer, and performance-optimized low-level components (C++ libraries and numba-compiled functions) on the bottom.
- Extensibility: Emphasize that the architecture allows adding new models, adapters, or distributed computing backends without major rewrites.

##### Relevant Technologies, Frameworks, or Libraries
- Python is the primary language with key modules under the “python/statsforecast” directory.
- C++ components in the “src” folder optimize performance.
- Numba is used for just-in-time compilation.
- Distributed computing frameworks like Spark, Ray, Dask are supported (adaptation points are visible in experiments and adapter modules).
- CI/CD configuration details (GitHub workflows, Makefile, and tests) support the build and deployment processes.

- Around it, arrange model components such as ARIMA, ETS, CES, Theta, etc., drawing connections from the API to these models.
- Below the model layer, add the performance layer (C++ modules and numba optimizations) connected to each model implementation.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% User Interaction
    U["User Input"]:::input

    %% API Layer
    subgraph "API Layer"
        API["StatsForecast API (__init__.py)"]:::api
        CORE["Core Library (core.py)"]:::api
    end

    %% Models Layer
    subgraph "Models Layer"
        MODELS["Models Module (models.py)"]:::model
        ARIMA["ARIMA Model (arima.py)"]:::model
        ETS["ETS Model (ets.py)"]:::model
        THETA["Theta Model (theta.py)"]:::model
    end

    %% Performance Layer
    subgraph "Performance Layer"
        P_ARIMA["C++ ARIMA (arima.cpp)"]:::perf
        P_ETS["C++ ETS (ets.cpp)"]:::perf
        P_THETA["C++ Theta (theta.cpp)"]:::perf
        P_MAIN["C++ Core (statsforecast.cpp)"]:::perf
    end

    %% Adapters & Distributed Layer
    subgraph "Adapters & Distributed"
        ADAPTER["Prophet Adapter (prophet.py)"]:::adapter
        DISTRIB["Distributed Frameworks (distributed/)"]:::adapter
    end

    %% Utilities
    subgraph "Utilities"
        UTILS["Utilities (utils.py)"]:::util
    end

    %% Experiments & Notebooks
    subgraph "Experiments & Notebooks"
        EXP["Experiments (experiments/)"]:::exp
        NOTE["Notebooks (nbs/)"]:::exp
    end

    %% Connections
    U --> API
    API --> CORE
    CORE --> MODELS
    MODELS --> ARIMA
    MODELS --> ETS
    MODELS --> THETA
    ARIMA -->|"optimizes_via"| P_ARIMA
    ETS -->|"optimizes_via"| P_ETS
    THETA -->|"optimizes_via"| P_THETA
    MODELS -->|"delegates_to"| P_MAIN
    CORE -->|"integrates_with"| ADAPTER
    CORE -->|"integrates_with"| DISTRIB
    CORE -->|"uses"| UTILS
    CORE -->|"validates_with"| EXP
    EXP -->|"includes"| NOTE

    %% Styles
    classDef api fill:#cce5ff,stroke:#333,stroke-width:2px;
    classDef model fill:#d5f5e3,stroke:#333,stroke-width:2px;
    classDef perf fill:#f5b7b1,stroke:#333,stroke-width:2px;
    classDef adapter fill:#f9e79f,stroke:#333,stroke-width:2px;
    classDef util fill:#d2b4de,stroke:#333,stroke-width:2px;
    classDef exp fill:#aed6f1,stroke:#333,stroke-width:2px;
    classDef input fill:#efefef,stroke:#333,stroke-width:2px;

    %% Click Events
    click API "https://github.com/nixtla/statsforecast/blob/main/python/statsforecast/__init__.py"
    click CORE "https://github.com/nixtla/statsforecast/blob/main/python/statsforecast/core.py"
    click MODELS "https://github.com/nixtla/statsforecast/blob/main/python/statsforecast/models.py"
    click ARIMA "https://github.com/nixtla/statsforecast/blob/main/python/statsforecast/arima.py"
    click ETS "https://github.com/nixtla/statsforecast/blob/main/python/statsforecast/ets.py"
    click THETA "https://github.com/nixtla/statsforecast/blob/main/python/statsforecast/theta.py"
    click P_ARIMA "https://github.com/nixtla/statsforecast/blob/main/src/arima.cpp"
    click P_ETS "https://github.com/nixtla/statsforecast/blob/main/src/ets.cpp"
    click P_THETA "https://github.com/nixtla/statsforecast/blob/main/src/theta.cpp"
    click P_MAIN "https://github.com/nixtla/statsforecast/blob/main/src/statsforecast.cpp"
    click ADAPTER "https://github.com/nixtla/statsforecast/blob/main/python/statsforecast/adapters/prophet.py"
    click DISTRIB "https://github.com/nixtla/statsforecast/tree/main/python/statsforecast/distributed/"
    click UTILS "https://github.com/nixtla/statsforecast/blob/main/python/statsforecast/utils.py"
    click EXP "https://github.com/nixtla/statsforecast/tree/main/experiments"
    click NOTE "https://github.com/nixtla/statsforecast/tree/main/nbs"
```
