Skip to main content

pyforesight

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.

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

Install

pip install git+https://github.com/StrategicProjects/pyforesight

Python 3.9 or later. Installing from the repository compiles the Rust code, so it needs a Rust toolchain (https://rustup.rs).

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 every built-in model
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. Without fs.Series (or fs.monthly, fs.quarterly), pass the seasonal period: fs.Theta().fit(values, period=12). Missing values (None, NaN) are only accepted by the cleaning functions.

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 rolling origin (expanding or fixed window) on all cores; 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

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.

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.

Authors

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

License

MIT.

Metadata

Release files for pyforesight 0.1.0

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.0
File Size Uploaded
pyforesight-0.1.0.tar.gz 88.3 kB Details

Built distributions (wheels)

Table of built distributions (wheels) for pyforesight 0.1.0
File
pyforesight-0.1.0-cp39-abi3-win_amd64.whl CPython 3.9 abi3 Windows x86-64 Details
pyforesight-0.1.0-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.0-cp39-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl CPython 3.9 abi3 Linux glibc 2.17+ ARM64 Details
pyforesight-0.1.0-cp39-abi3-macosx_11_0_arm64.whl CPython 3.9 abi3 macOS 11.0+ ARM64 Details
pyforesight-0.1.0-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.0.tar.gz

Download URL pyforesight-0.1.0.tar.gz
Size 88.3 kB
Tags Source
SHA-256 checksum
How to use checksums
bf0eaaf19d2e653ba475464416fa9433e51fde563997cd680fdfc79c90beb6b1
BLAKE2b-256 checksum
How to use checksums
c722834886d062882cbb569814dde44abb2b5a14e2c3a2c43580e6e149f920c2
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.0-cp39-abi3-win_amd64.whl

Download URL pyforesight-0.1.0-cp39-abi3-win_amd64.whl
Size 495.2 kB
Tags CPython 3.9 Windows x86-64 abi3
SHA-256 checksum
How to use checksums
5d9de27b9267dccffe51ea5b3e14c8a71dd22e9a90ccb3f1d9f8372c364c2e93
BLAKE2b-256 checksum
How to use checksums
b1c8606285c3cfde83dafec5b5cab3852c1b1d8059a82d723ee647ebc7391ab4
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.0-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL pyforesight-0.1.0-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Size 601.2 kB
Tags CPython 3.9 Linux glibc 2.17+ x86-64 abi3
SHA-256 checksum
How to use checksums
6faa709494e22550845cc879d6c4718ffc69c2f3919668c1926b77230aa9ef69
BLAKE2b-256 checksum
How to use checksums
50d848c8d264f1d92d115b200ec15e5f6c7d665b18955f3d94c652a00e36835f
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.0-cp39-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl

Download URL pyforesight-0.1.0-cp39-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Size 572.6 kB
Tags CPython 3.9 Linux glibc 2.17+ ARM64 abi3
SHA-256 checksum
How to use checksums
724f9dea8a8350cbd98e359d84d36cbb065075f474d04d5980f9588fc73cca91
BLAKE2b-256 checksum
How to use checksums
cb4aa068dec8deac47aa04b5d606c49a1b23180bb77d6befbdda988e0e16f828
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.0-cp39-abi3-macosx_11_0_arm64.whl

Download URL pyforesight-0.1.0-cp39-abi3-macosx_11_0_arm64.whl
Size 534.8 kB
Tags CPython 3.9 abi3 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
a97c5915db51907055a2cc61ad7760caff34a3f8e4c397e6cd010668d03bdbc3
BLAKE2b-256 checksum
How to use checksums
496d6b9d52b0d9a6f235b379583058bede61d7c9105528ce6a6336168e008d94
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.0-cp39-abi3-macosx_10_12_x86_64.whl

Download URL pyforesight-0.1.0-cp39-abi3-macosx_10_12_x86_64.whl
Size 559.5 kB
Tags CPython 3.9 abi3 macOS 10.12+ x86-64
SHA-256 checksum
How to use checksums
675253f9bab195d9babd6bce44797cc58e0045c109c9a95a209255bb147c28d4
BLAKE2b-256 checksum
How to use checksums
75ef5da3d3e8f397979884ede37012737fde5f1f7b241505bba2633431baa8a3
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

0.1.1

6 release files

This release

0.1.0 This release

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