Skip to main content

tsfm provides an easy interface to use TimeSeries Foundational Models.

Project description

UML Diagram - TSFM Codebase

classDiagram
    %% Exceptions
    class TSFMError {
        <<Exception>>
    }
    
    class InvalidInputError {
        <<Exception>>
        +__init__(message: str)
    }
    
    Exception <|-- TSFMError
    TSFMError <|-- InvalidInputError
    
    %% Output Classes
    class ForecastOutput {
        -df_preds: DataFrame
        -meta: dict[str, Any]
        +rmsfe: DataFrame
        +mae: DataFrame
        +me: DataFrame
        +metric(name: Literal) DataFrame
        +summary(digits: int) None
        +plot_actual_vs_pred(horizon: int, ax: Axes, start: Timestamp, end: Timestamp, return_ax: bool) Axes
        -_agg_mean(s: Series, name: str, post: Callable) DataFrame
        -_summary(digits: int) str
    }
    
    %% Base Model (Abstract)
    class Model {
        <<Abstract>>
        +registry: ClassVar[dict]
        +name: property
        +__init_subclass__(cls, name: str, **kwargs)
        +build(name: str, **kwargs) Model
        +_pred(df: DataFrame, y: str, X: list, ctx_len: int, horizon: int, oos_start: str)* DataFrame
        +pred(df: DataFrame, y: str, X: list, ctx_len: int, horizon: int, oos_start: str) ForecastOutput
    }
    
    ABC <|-- Model
    
    %% ARModel
    class ARModel {
        +_pred(df: DataFrame, y: str, X: list, ctx_len: int, horizon: int, oos_start: str) DataFrame
    }
    
    Model <|-- ARModel : name="armodel"
    ARModel ..> InvalidInputError : raises
    ARModel ..> ForecastOutput : creates
    
    %% Moirai Model
    class Moirai {
        +get_model(ctx_len: int, horizon: int, n_covariates: int) MoiraiForecast
        +_pred(df: DataFrame, y: str, X: list, ctx_len: int, horizon: int, oos_start: str) DataFrame
    }
    
    Model <|-- Moirai : name="moirai"
    Moirai ..> ForecastOutput : creates
    
    %% Moirai2 Model
    class Moirai2 {
        +get_model(ctx_len: int, horizon: int, n_covariates: int) Moirai2Forecast
        +_pred(df: DataFrame, y: str, X: list, ctx_len: int, horizon: int, oos_start: str) DataFrame
    }
    
    Model <|-- Moirai2 : name="moirai2"
    Moirai2 ..> ForecastOutput : creates
    
    %% Data Module Functions
    class DataModule {
        <<module: data.py>>
        +generator(n: int, phi: float, beta0: float, beta1: float, sigma: float, trend: float, season_period: int, season_ampl: float, seed: int) DataFrame
        +split_is_oos(df: DataFrame, test_frac: float) tuple[DataFrame, DataFrame]
    }
    
    %% ARModel Helper Functions
    class ARModelHelpers {
        <<module: armodel.py>>
        +create_lags(var: Series, lags: int) DataFrame
        +get_design_matrix(ys: Series, y_lags: int, horizon: int) tuple[Series, DataFrame]
        +fit(ys: Series, y_lags: int, horizon: int) RegressionResultsWrapper
        +pred(ys: Series, y_lags: int, horizon: int, oos_start: str) Series
        +build_oos_panel(ys: Series, y_lags: int, horizon: int, oos_start: str) DataFrame
        -_to_month_end(idx: Index) DatetimeIndex
    }
    
    ARModel ..> ARModelHelpers : uses
    
    %% Moirai Helper Functions
    class MoiraiHelpers {
        <<module: moirai.py>>
        +make_fc_df(forecasts: list, y_true_series: Series) DataFrame
        +prepare_data(df: DataFrame, y: str, X: list, horizon: int, oos_start: str) TestData
        +MODEL_ID: str
    }
    
    Moirai ..> MoiraiHelpers : uses
    
    %% Moirai2 Helper Functions
    class Moirai2Helpers {
        <<module: moirai2.py>>
        +make_fc_df(forecasts: list, y_true_series: Series) DataFrame
        +prepare_data(df: DataFrame, y: str, X: list, horizon: int, oos_start: str) TestData
        +MODEL_ID: str
    }
    
    Moirai2 ..> Moirai2Helpers : uses
    
    %% Output Helper Functions
    class OutputHelpers {
        <<module: outputs.py>>
        +get_horizon_groupby(df: DataFrame) Index
    }
    
    ForecastOutput ..> OutputHelpers : uses
    
    %% Relationships with external libraries
    Model ..> ForecastOutput : returns
    
    %% Package level
    class TSFMPackage {
        <<module: __init__.py>>
        +ARModel
        +Model
        +Moirai
        +Moirai2
        +generator
    }
    
    TSFMPackage ..> ARModel : exports
    TSFMPackage ..> Model : exports
    TSFMPackage ..> Moirai : exports
    TSFMPackage ..> Moirai2 : exports
    TSFMPackage ..> DataModule : exports generator

    %% Notes
    note for Model "Registry pattern:\nSubclasses auto-register\nby name parameter"
    
    note for ForecastOutput "Contains forecast results\nwith MultiIndex[cutoff, oos_date]\nand metrics calculation"
    
    note for ARModel "Autoregressive model\nusing statsmodels OLS\nwith HAC standard errors"
    
    note for Moirai "Pretrained foundation model\nfrom Salesforce\nmodel: moirai-1.1-R-small"
    
    note for Moirai2 "Pretrained foundation model\nfrom Salesforce\nmodel: moirai-2.0-R-small\nuses uni2ts library"

Class Descriptions

Core Classes

  1. Model (Abstract Base Class)

    • Registry pattern for dynamic model creation
    • All models inherit from this class
    • Defines interface: _pred() (abstract) and pred() (concrete)
    • Registry allows building models by name
  2. ARModel

    • Autoregressive time series model
    • Uses statsmodels for OLS regression with HAC standard errors
    • Does not support covariates (raises InvalidInputError)
    • Implements expanding window forecasting
  3. Moirai

    • Wrapper for Salesforce's Moirai foundation model
    • Supports covariates
    • Uses GluonTS for data preparation
    • Generates 100 samples for probabilistic forecasting
  4. Moirai2

    • Wrapper for Salesforce's Moirai 2.0 foundation model
    • Supports covariates
    • Uses uni2ts library for inference
    • Uses quantile-based forecasting (median as point forecast)

Data Structures

  1. ForecastOutput
    • Dataclass containing forecast results
    • MultiIndex DataFrame: (cutoff, oos_date)
    • Cached properties for metrics: RMSFE, MAE, ME
    • Methods for summary statistics and plotting

Exceptions

  1. TSFMError

    • Base exception for the package
  2. InvalidInputError

    • Raised when input validation fails

Utility Modules

  1. DataModule (data.py)

    • generator(): Simulates time series with AR, trend, seasonality
    • split_is_oos(): Splits data into train/test
  2. Helper Functions

    • ARModel helpers: lag creation, design matrix, OLS fitting
    • Moirai helpers: forecast formatting, data preparation
    • Moirai2 helpers: forecast formatting, data preparation (uni2ts-based)
    • Output helpers: horizon grouping utilities

Architecture Patterns

  • Registry Pattern: Models self-register via __init_subclass__
  • Template Method: Base Model.pred() calls abstract _pred()
  • Dataclass: ForecastOutput with cached properties
  • Facade: Simple interface (pred()) over complex forecasting logic

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

tsfmecb-0.1.1.tar.gz (9.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

tsfmecb-0.1.1-py3-none-any.whl (13.2 kB view details)

Uploaded Python 3

File details

Details for the file tsfmecb-0.1.1.tar.gz.

File metadata

  • Download URL: tsfmecb-0.1.1.tar.gz
  • Upload date:
  • Size: 9.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.8.23

File hashes

Hashes for tsfmecb-0.1.1.tar.gz
Algorithm Hash digest
SHA256 b5e2ae2298da15cfb27b08fd9f98935ada3e35a77903fee712b72dae89913e55
MD5 f120e194ad29725b54d0e017124174db
BLAKE2b-256 9db29309bf0df62710de42002e647cdae7a7a606a6f6ce74a25b428931845541

See more details on using hashes here.

File details

Details for the file tsfmecb-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: tsfmecb-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 13.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.8.23

File hashes

Hashes for tsfmecb-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 ff83cdbf0849427d9c716576cc11ad2b6c95cc5fa2eaeb1330c9a95e549c7c28
MD5 5ed6108d4b401c44d8bc5262378713f0
BLAKE2b-256 45a9d144ed9ba28ea5fa3145928faa7334565c6930d39c8a7d2d8f3d16670460

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page