Skip to main content

pyforesight

PyPI DOI CI License: MIT Dependencies: none

Time series forecasting in Python that picks its model by what would have worked.

foresight fits several models to a series, replays the past to see how each would have done, chooses by out-of-sample error and reports intervals taken from the errors actually observed, including intervals for the total of the next k periods.

The models, the backtest and the utilities are the Rust crate foresight, compiled into the package: the numbers are the crate's, the backtest runs on all cores, and nothing else is needed at run time. NumPy and pandas are accepted and, for pandas, produced on request, but neither is required.

Architecture: the Rust crate foresight holds every computation; pyforesight (Python, PyO3) and foresightr (R, extendr) call it; foresight-go is an independent Go port checked against it.

Website: https://strategicprojects.github.io/pyforesight/ · Português

Install

pip install pyforesight

Python 3.9 or later. Wheels are ready for Linux (x86-64 and aarch64), macOS (Intel and Apple silicon) and Windows; elsewhere pip compiles the Rust code, which needs a Rust toolchain (https://rustup.rs). The package is imported as foresight.

Use

import foresight as fs

# monthly data whose first observation is in March
y = fs.monthly(values, first_month=3)

# replay the last 36 months, 12 months ahead, with the 11 default models
report = fs.backtest(y)

best = report.best
print(f"{best.name}: MAPE {best.score:.1f}%")
for p in best.forecast:
    lo, hi = p.interval(0.80)
    print(p.horizon, round(p.mean), round(lo), round(hi))

half_year = best.cumulative(6)      # the total of the next six months, with its own interval
report.to_pandas()                  # one row per candidate (needs pandas)
best.to_pandas()                    # the forecast with its intervals

One model on its own:

# seasonal ARIMA on the log scale
fit = fs.log(fs.Arima.airline()).fit(y)
next_year = fit.forecast(12)

# orders chosen from the data, inspected
auto = fs.AutoArima().fit(fs.monthly(log_values, first_month=3))
auto.details["order"], auto.details["seasonal_order"], auto.aicc

A trend that bends, with dated events:

model = fs.Prophet(
    events={"campaign": [10, 34, 58, 82, 106, 130]},  # future ones included
    steps={"new_law": 80},                              # a lasting change of level
)
fit = model.fit(y)
fit.details["changepoints"], fit.details["effects"]

Several models combined, and the wider set of candidates:

ensemble = fs.Ensemble(fs.defaults(), weighting="stacked")
report = fs.backtest(y, fs.thorough() + [ensemble.named("my_ensemble")])

Any sequence of numbers works where a series is expected: a list, a NumPy array, a pandas Series. Only the values are read (an index of dates is not), so say what the series is with fs.monthly, fs.quarterly or fs.Series, or pass the seasonal period: fs.Theta().fit(values, period=12). Missing values (None, NaN) are only accepted by the cleaning functions.

Good to know:

  • Positions count from 0 at the first observation: Prophet's events and steps, Outlier.index, Report.first_origin and the changepoints of a fit. Months and quarters (first_month, first_season) count from 1.
  • A candidate that cannot forecast at every origin and from the whole series is left out of the report, with a warning; report.dropped names them.
  • fs.set_max_threads(n) limits the threads of backtests and ensembles; the results do not depend on it. The GIL is released while models are fitted.
  • Models, fits and reports hold Rust objects and cannot be pickled or copied; a model is cheap to build again in another process.

What is in it

Piece What it does
Series, monthly, quarterly values + seasonal period + season of the first observation
Model / Fit fit once, forecast any horizon, inspect params, details, likelihood and residuals
Models Mean, Naive, Drift, SeasonalNaive, Theta, HoltWinters, LogLinear (optionally deflated by a price index), Arima (seasonal, exact maximum likelihood, optionally with regressors), AutoArima, Ets, AutoEts, Prophet (changepoints, Fourier seasonality, dated events and steps), Tbats (several seasonal periods, not necessarily whole numbers), Croston (with SBA and TSB)
Transformed, log any model on the log or another Box-Cox scale, λ fixed or by Guerrero's method
Decomposed, stl, mstl trend, seasonal patterns and remainder by LOESS; any model on the seasonally adjusted series
Ensemble average, median, weights by inverse error or stacked weights
Regressors external variables, Fourier terms, seasonal dummies
defaults, thorough ready sets of 11 and 18 candidates
backtest, set_max_threads rolling origin (expanding or fixed window) on all cores, or as many as allowed; MAPE, MAE, RMSE, MASE and bias by horizon; average of the best models; choice by out-of-sample error; empirical intervals by horizon and for totals
interpolate, outliers, clean gaps filled and outliers found and replaced, with the season taken into account
Measures and tests mape, bias, mae, rmse, mase, acf, difference, kpss, ndiffs, nsdiffs, seasonal_strength, box_cox, inv_box_cox, guerrero

How it differs from the usual toolkits

How a model is chosen: every candidate is refitted at each origin and forecasts ahead; the errors by horizon rank the candidates and give the empirical intervals.

Most forecasting libraries choose a model by an in-sample information criterion and derive intervals from distributional assumptions. Here the choice and the intervals both come from forecasts made without seeing the future they are judged against. The interval for a total (say, the rest of a fiscal year) is measured on totals, because adding up monthly limits overstates its uncertainty.

Checked

The package runs the Rust crate, so its numbers are the crate's; the tests check that nothing is lost on the way, against results recorded by the crate: ARIMA, regression with ARIMA errors, ETS, Prophet, TBATS, STL and MSTL, Croston, cleaning, ensembles, tests of stationarity and seasonality, and the backtests of 11 and 18 candidates on three public series. The crate itself is compared with the R packages forecast 9.0.2 and prophet 1.1.7, and reproduced independently by the Go edition foresight-go. The same methods are available in R: foresightr.

pip install maturin pytest
maturin develop --release
pytest                 # about 30 s; pytest -m "not slow" skips TBATS and the thorough backtest

Data

tests/data has two public series: the monthly ICMS and FPE revenue of the state of Piauí, Brazil (Siconfi/STN, with the IPCA price index from the Central Bank of Brazil), and the airline passengers of Box & Jenkins.

Citation

Zenodo: https://doi.org/10.5281/zenodo.23050402 (all versions); see also CITATION.cff.

Authors

André Leite, Marcos Wasiliew, Hugo Vasconcelos, Carlos Amorim and Diogo Bezerra.

License

MIT.

Metadata

Release files for pyforesight 0.1.1

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

Source distribution (sdist)

Source distribution for pyforesight 0.1.1
File Size Uploaded
pyforesight-0.1.1.tar.gz 101.0 kB Details

Built distributions (wheels)

Table of built distributions (wheels) for pyforesight 0.1.1
File
pyforesight-0.1.1-cp39-abi3-win_amd64.whl CPython 3.9 abi3 Windows x86-64 Details
pyforesight-0.1.1-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl CPython 3.9 abi3 Linux glibc 2.17+ x86-64 Details
pyforesight-0.1.1-cp39-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl CPython 3.9 abi3 Linux glibc 2.17+ ARM64 Details
pyforesight-0.1.1-cp39-abi3-macosx_11_0_arm64.whl CPython 3.9 abi3 macOS 11.0+ ARM64 Details
pyforesight-0.1.1-cp39-abi3-macosx_10_12_x86_64.whl CPython 3.9 abi3 macOS 10.12+ x86-64 Details

Total release size: 2.9 MB

Release files / pyforesight-0.1.1.tar.gz

Download URL pyforesight-0.1.1.tar.gz
Size 101.0 kB
Tags Source
SHA-256 checksum
How to use checksums
3d334f482056f4485bc72afcb1d437972e5c4630fe1cbe57a962bd27843111bc
BLAKE2b-256 checksum
How to use checksums
70a3682de95e21e4bd96c5e41904bb11a6fb61760ee8de2347c8f8047bfddd6f
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 30, 2026.

Transparency log

Release files / pyforesight-0.1.1-cp39-abi3-win_amd64.whl

Download URL pyforesight-0.1.1-cp39-abi3-win_amd64.whl
Size 502.5 kB
Tags CPython 3.9 Windows x86-64 abi3
SHA-256 checksum
How to use checksums
60103cd127b64bcfc47c4b0f48b4f9c6788092add19c06d501248c94d760f066
BLAKE2b-256 checksum
How to use checksums
8accd43d36668bcab0932e521e0bc75852ad17192d32d1d328c389575fec848c
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 30, 2026.

Transparency log

Release files / pyforesight-0.1.1-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL pyforesight-0.1.1-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Size 613.1 kB
Tags CPython 3.9 Linux glibc 2.17+ x86-64 abi3
SHA-256 checksum
How to use checksums
353391100251348c8eb03325cb6b98b1d31908e426da6d4eedeab2913137dbd8
BLAKE2b-256 checksum
How to use checksums
306ffe3808b47573618f7cbc260faf831c3c348a37834c91b65b01fa8744afc7
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 30, 2026.

Transparency log

Release files / pyforesight-0.1.1-cp39-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl

Download URL pyforesight-0.1.1-cp39-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Size 580.5 kB
Tags CPython 3.9 Linux glibc 2.17+ ARM64 abi3
SHA-256 checksum
How to use checksums
b78f71292790864f6f8a2dca9a23f0447431eb0a6285ca3ca012bff9ae47f7ac
BLAKE2b-256 checksum
How to use checksums
ae5889b2031d1e1e0ba116c1027e71087d0adeca6025b077e1402b7866f7df32
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 30, 2026.

Transparency log

Release files / pyforesight-0.1.1-cp39-abi3-macosx_11_0_arm64.whl

Download URL pyforesight-0.1.1-cp39-abi3-macosx_11_0_arm64.whl
Size 544.6 kB
Tags CPython 3.9 abi3 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
7370a12fa5f01e738185c155231c45a6f63f7c6a7f6d06c1c0beabaedb9b22d6
BLAKE2b-256 checksum
How to use checksums
1650aaa48978a8b2d3fc4bffab5e33429398c780a1506336c4577dd83891539e
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 30, 2026.

Transparency log

Release files / pyforesight-0.1.1-cp39-abi3-macosx_10_12_x86_64.whl

Download URL pyforesight-0.1.1-cp39-abi3-macosx_10_12_x86_64.whl
Size 570.3 kB
Tags CPython 3.9 abi3 macOS 10.12+ x86-64
SHA-256 checksum
How to use checksums
255774a1751bede9ea544c0d6f8a391a0207f5fe407567dbea74f451b66ad551
BLAKE2b-256 checksum
How to use checksums
2fe69a0564173784ae0c2d27a2b0fb2cc40b664147f6a89d3eb09569ac7e18a0
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 30, 2026.

Transparency log

Release history Release notifications | RSS feed

0.1.2

6 release files

This release

0.1.1 This release

6 release files

0.1.0

6 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