Skip to main content

JAX-based Sequential Monte Carlo library for time series prediction

Project description

smcs

CI PyPI version Python 3.10+ License

smcs is a JAX-based Sequential Monte Carlo library for time series prediction. It combines the academic rigor of particles, the functional design patterns of BlackJAX, and the state-space model abstractions of Dynamax.

Features

  • JAX-native: Full JIT compilation and GPU/TPU support
  • Type-safe: Comprehensive jaxtyping annotations with runtime checking
  • Multiple SMC algorithms:
    • Bootstrap Particle Filter
    • Auxiliary Particle Filter
    • Liu-West Filter (online parameter learning)
    • Storvik Filter (sufficient statistics)
    • SMC² (nested SMC for parameters)
    • PMMH (Particle MCMC)
    • Waste-Free SMC
  • State-space models:
    • Dynamic Linear Models (Local Level, Local Linear Trend)
    • ARIMA/SARIMA
    • Stochastic Volatility
    • GARCH family
    • Dynamic Factor Models
    • Regime-switching models
  • High-level forecasting agents for easy use
  • Pandas DataFrame integration

Installation

pip install smcs

For development:

pip install smcs[dev]

Quick Start

Using High-Level Agents

import jax.numpy as jnp
from smcs import LocalLevelAgent, SMCConfig
from smcs.io import from_dataframe

# Load data
data, timestamps = from_dataframe(df)

# Create and configure agent
config = SMCConfig(n_particles=1000, seed=42)
agent = LocalLevelAgent(config)

# Fit model
agent.fit(data, timestamps)

# Generate forecasts
forecast = agent.predict(horizon=10)
print(f"Forecast mean: {forecast.mean}")
print(f"95% interval: [{forecast.quantiles[0.05]}, {forecast.quantiles[0.95]}]")

# Online update with new observation
agent.update(jnp.array([new_value]))

Using Low-Level API

import jax
import jax.numpy as jnp
from smcs import (
    run_bootstrap_filter,
    LocalLevelModel,
    LocalLevelParams,
)

# Define model and parameters
model = LocalLevelModel()
params = LocalLevelParams(
    sigma_obs=0.5,
    sigma_level=0.1,
    m0=0.0,
    C0=1.0,
)

# Generate synthetic data
key = jax.random.PRNGKey(42)
observations = jax.random.normal(key, shape=(100, 1))

# Run particle filter
filter_key = jax.random.PRNGKey(123)
state, info = run_bootstrap_filter(
    filter_key,
    observations,
    model,
    params,
    n_particles=1000,
)

print(f"Log-likelihood: {state.log_likelihood:.4f}")
print(f"Final ESS: {info.ess[-1]:.1f}")

Parameter Learning with Liu-West Filter

from smcs import run_liu_west_filter, LocalLevelModel

model = LocalLevelModel()

# Define parameter conversion
def param_to_model_params(param_vec):
    return LocalLevelParams(
        sigma_obs=jnp.exp(param_vec[0]),
        sigma_level=jnp.exp(param_vec[1]),
        m0=0.0,
        C0=1.0,
    )

# Run Liu-West filter
state, info = run_liu_west_filter(
    key,
    observations,
    model,
    param_to_model_params,
    initial_state_sampler,
    initial_param_sampler,
    n_particles=1000,
    delta=0.98,
)

# Get estimated parameters
estimated_params = state.weighted_param_mean()

Architecture

smcs/
├── src/smcs/
│   ├── core/           # Particles, resampling, ESS computation
│   ├── algorithms/     # SMC algorithm implementations
│   ├── models/         # State space model definitions
│   ├── agents/         # High-level forecasting agents
│   ├── config/         # Pydantic configuration
│   └── io/             # DataFrame utilities
├── tests/
└── docs/

SMC Algorithms

Algorithm Use Case Complexity
Bootstrap PF Basic filtering O(NT)
Auxiliary PF Informative observations O(NT)
Liu-West Online parameter learning O(NT)
Storvik Models with sufficient statistics O(NT)
SMC² Full online parameter learning O(Nθ×Nx×T)
PMMH Batch parameter learning O(N×MCMC)
Waste-Free Efficient MCMC utilization O(NT)

State Space Models

Model Description
Local Level Random walk + noise
Local Linear Trend Level + slope dynamics
ARIMA Autoregressive integrated moving average
Stochastic Volatility Time-varying volatility
GARCH Deterministic volatility dynamics
Dynamic Factor Multivariate with latent factors
Regime-Switching Markov-switching dynamics

Configuration

from smcs import SMCConfig

config = SMCConfig(
    n_particles=1000,          # Number of particles
    seed=42,                   # Random seed
    ess_threshold=0.5,         # ESS/N ratio for resampling
    resampling_method="systematic",  # Resampling algorithm
    liu_west_delta=0.98,       # Liu-West discount factor
    jit_compile=True,          # JIT compilation
)

Development

# Clone repository
git clone https://github.com/smcs-authors/smcs.git
cd smcs

# Install in development mode
pip install -e ".[dev]"

# Run tests
pytest

# Run linter
ruff check src tests

# Type checking
mypy src

References

  1. Gordon, N. J., Salmond, D. J., & Smith, A. F. (1993). Novel approach to nonlinear/non-Gaussian Bayesian state estimation. IEE Proceedings F.
  2. Pitt, M. K., & Shephard, N. (1999). Filtering via simulation: Auxiliary particle filters. JASA.
  3. Liu, J., & West, M. (2001). Combined parameter and state estimation in simulation-based filtering.
  4. Chopin, N., Jacob, P. E., & Papaspiliopoulos, O. (2013). SMC²: an efficient algorithm for sequential analysis of state space models. JRSS-B.
  5. Andrieu, C., Doucet, A., & Holenstein, R. (2010). Particle Markov chain Monte Carlo methods. JRSS-B.
  6. Dau, H. D., & Chopin, N. (2022). Waste-free sequential Monte Carlo. JRSS-B.

License

MIT License - see LICENSE for details.

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

smcs-0.1.1.tar.gz (41.0 kB view details)

Uploaded Source

Built Distribution

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

smcs-0.1.1-py3-none-any.whl (60.5 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: smcs-0.1.1.tar.gz
  • Upload date:
  • Size: 41.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for smcs-0.1.1.tar.gz
Algorithm Hash digest
SHA256 ef0f9176e9858f301a032ff3a4e6da09f9bf13589358bb9a323ca27de42d4a6d
MD5 574a58ec7ab83d76642f94734fb4e9f1
BLAKE2b-256 0751c428a5afae0d734ee8109729a2e8f9764ac49c2a07d178d1b68520234575

See more details on using hashes here.

Provenance

The following attestation bundles were made for smcs-0.1.1.tar.gz:

Publisher: publish.yml on HarudoBoruzu/smcs

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

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

File metadata

  • Download URL: smcs-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 60.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for smcs-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 8609e888c1f8f76cce3358a6107d205042b21131baa05f006d06e0f5b8cca505
MD5 4b1b4f41102320d42ab95f5530031878
BLAKE2b-256 7ebdbbcdeb573787d3b0d9dc2c89a8099916b45aafeff886a3f4962e9c3617af

See more details on using hashes here.

Provenance

The following attestation bundles were made for smcs-0.1.1-py3-none-any.whl:

Publisher: publish.yml on HarudoBoruzu/smcs

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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