Skip to main content

statespace

Linear Gaussian state space models, following Part I of Durbin and Koopman, Time Series Analysis by State Space Methods (2nd ed., 2012). The core is a Fortran library; ssfortran is its Python package.

  • Filtering and smoothing:
    • the Kalman filter with a steady-state shortcut
    • exact diffuse initialization, in both univariate and multivariate forms
    • univariate treatment, including correlated and time-varying H
    • state, disturbance, fast, classical, two-filter, fixed-point and fixed-lag smoothers
    • augmented and square-root filters
    • collapsing large observation vectors, and linear restrictions
  • Likelihood and estimation:
    • exact, diffuse, concentrated and marginal log likelihoods
    • maximum likelihood with L-BFGS-B, using DK's analytic score where it applies and numerical derivatives elsewhere
    • EM for variance parameters
    • standard errors, and the effect of parameter estimation on the smoothed states
  • Simulation, forecasting and diagnostics: simulation smoothers, forecasts, standardized and auxiliary residuals, and residual tests.
  • Built-in models (DK ch. 3): irregular, level, trend, seasonal (dummy, trigonometric, Harrison–Stevens), cycle, regression and intervention effects, ARIMA, continuous-time components and splines. Components apply to one series or several (SUTSE), and can load on common signals.

The documentation (user guide, examples, design notes, and Python, Fortran and C references) builds with make -C docs html; see docs/install.rst. The illustrations of DK chapter 8 are reproduced in example/. Differences from statsmodels' tsa.statespace are listed in docs/statsmodels_differences.md.

Fortran

The library builds with fpm and needs gfortran, LAPACK and BLAS.

fpm test --profile release                              # the test suite
fpm run --profile release --example nile_mle            # a model defined in Fortran
fpm run --profile release --example dk_8_2_seatbelt     # needs data/fetch_dk_data.py first

Models are defined by extending ssm_model_t (see example/nile_mle.f90), or assembled from components with structural_model (see example/dk_8_2_seatbelt.f90). fit_many fits independent models in parallel when built with --flag -fopenmp --link-flag -fopenmp.

Python

pip install ssfortran
# or
uv add ssfortran           # in a uv project; or: uv pip install ssfortran

Wheels for Linux (x86_64 and aarch64, glibc 2.28 or later) include the compiled library, gfortran's runtime and OpenBLAS, so they need no compiler. On other platforms pip and uv build from the source distribution, which needs gfortran, LAPACK and BLAS. The package needs Python 3.10 or later and numpy.

import numpy as np
import ssfortran as ss

y = np.loadtxt("data/nile.csv", delimiter=",", skiprows=1)[:, 1]  # from this repository

# Built-in components
mod = ss.StructuralModel(y, [ss.Irregular(), ss.Level()])
res = mod.fit()
print(res.summary())
smoothed = res.smooth().smoothed_state

# Matrix-level: fill the system matrices yourself
rep = ss.Representation(y, k_states=1)
rep["design"] = [[1.0]]
rep["transition"] = [[1.0]]
rep["selection"] = [[1.0]]
rep["obs_cov"] = [[15099.0]]
rep["state_cov"] = [[1469.1]]
rep.initialize_diffuse()
print(rep.loglike())

Fitting results have summary() (estimates with z-tests and intervals, fit statistics, residual tests), fittedvalues, resid, get_forecast(steps) with conf_int(), components() and estimation_bias(). A Representation also offers:

  • the other smoothers of DK ch. 4: fast, classical, two-filter, Whittle, fixed-point, fixed-lag and updating
  • smoothed covariances between periods, and filtering and smoothing weights
  • the square-root and augmented filters (with the marginal likelihood)
  • EM, collapsing, and linear state restrictions
  • auxiliary residuals, de Jong–Penzer statistics, least squares residuals and R²_D
  • the mean-correction and de Jong–Shephard simulation smoothers

A model with parameters can be defined in three ways:

How Speed
StructuralModel built-in components all in Fortran; works with fit_many
MappedModel declare which matrix entries each parameter sets all in Fortran; works with fit_many
MLEModel subclass and write update(params), as in statsmodels calls Python for each likelihood evaluation

For the timing comparison with statsmodels from Python, run bench/bench_python.py. It shows 2–8× speedups for single models, and 36× for 1000 series with fit_many. example/bench.f90 and bench/bench_statsmodels.py compare the Fortran core directly.

To install from a clone, pip install . (or uv pip install .). To develop without installing:

cmake -S . -B build/cmake -G Ninja && cmake --build build/cmake
pytest            # uses python/src and build/cmake (pyproject.toml)

Code is formatted to 88 columns. ruff format and ruff check cover the Python (settings in pyproject.toml); Fortitude's fortitude check covers the Fortran (settings in fpm.toml), whose long lines are wrapped by hand.

License

MIT (see LICENSE); third-party notices are in THIRD_PARTY_NOTICES.md, and citations in docs/references.md.

Metadata

Release files for ssfortran 0.1.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for ssfortran 0.1.2
File Size Uploaded
ssfortran-0.1.2.tar.gz 157.5 kB Details

Built distributions (wheels)

Table of built distributions (wheels) for ssfortran 0.1.2
File Interpreter ABI Platform
ssfortran-0.1.2-py3-none-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl Python 3 none Linux glibc 2.28+ x86-64, Linux glibc 2.27+ x86-64 Details
ssfortran-0.1.2-py3-none-manylinux_2_27_aarch64.manylinux_2_28_aarch64.whl Python 3 none Linux glibc 2.27+ ARM64, Linux glibc 2.28+ ARM64 Details

Total release size: 19.5 MB

Release files / ssfortran-0.1.2.tar.gz

Download URL ssfortran-0.1.2.tar.gz
Size 157.5 kB
Tags Source
SHA-256 checksum
How to use checksums
0485e7525fc76d2b9ddbaafb0cf291a98fb54da427250a1fb0e9135ac7daae22
BLAKE2b-256 checksum
How to use checksums
86b88641245d78d41b473d98eec748cfa2ca251c8437a3de9023b11f1e09f08a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 28, 2026.

Transparency log

Release files / ssfortran-0.1.2-py3-none-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl

Download URL ssfortran-0.1.2-py3-none-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Size 13.2 MB
Tags Linux glibc 2.27+ x86-64 Linux glibc 2.28+ x86-64 Python 3
SHA-256 checksum
How to use checksums
08556b77afe9717b27b62fa30b71826bab5467733a4a5bba89f4fd18976ded5b
BLAKE2b-256 checksum
How to use checksums
d360662492643d7ef3c3f152f4b87781d5b35bf62e94e0ef6d1f940050ab10ce
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 28, 2026.

Transparency log

Release files / ssfortran-0.1.2-py3-none-manylinux_2_27_aarch64.manylinux_2_28_aarch64.whl

Download URL ssfortran-0.1.2-py3-none-manylinux_2_27_aarch64.manylinux_2_28_aarch64.whl
Size 6.2 MB
Tags Linux glibc 2.27+ ARM64 Linux glibc 2.28+ ARM64 Python 3
SHA-256 checksum
How to use checksums
235a66442f3ceb3b95fef3cc984f4985ede1164b963243e7591b508bf45d98c2
BLAKE2b-256 checksum
How to use checksums
7c2a3232b52c4070819eb173e941dab3d2255cee6a922b473aa188aabe5aa3c5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 28, 2026.

Transparency log

Release history Release notifications | RSS feed

0.1.4

3 release files

0.1.3

3 release files

This release

0.1.2 This release

3 release files

0.1.1

3 release files

0.1.0

3 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page