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.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 pyforesight 0.1.2
File Size Uploaded
pyforesight-0.1.2.tar.gz 101.0 kB Details

Built distributions (wheels)

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

Download URL pyforesight-0.1.2.tar.gz
Size 101.0 kB
Tags Source
SHA-256 checksum
How to use checksums
9f5686b139fe0ba239ab9addd484bd8c4bc9dbd8d38997cae31fd59f55ccee97
BLAKE2b-256 checksum
How to use checksums
2aeca7dc9b4c8fc8bf1fd626c009c630002466953757bc603b1ff3f90bb6cebe
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.2-cp39-abi3-win_amd64.whl

Download URL pyforesight-0.1.2-cp39-abi3-win_amd64.whl
Size 502.7 kB
Tags CPython 3.9 Windows x86-64 abi3
SHA-256 checksum
How to use checksums
e4a90c854892aabc3e869a7f7bdf8dfeafb195b180ca73507eb47afe8b95f457
BLAKE2b-256 checksum
How to use checksums
9bd7a6fc761e66f003a00fe632c611f0df7b08527f0dcaeab3911624b92581aa
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.2-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL pyforesight-0.1.2-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Size 613.3 kB
Tags CPython 3.9 Linux glibc 2.17+ x86-64 abi3
SHA-256 checksum
How to use checksums
1d816d0c5d6b7c8049bcdf7a3d32a6ed65bc741b1227f6fa19ae431dbbceb45f
BLAKE2b-256 checksum
How to use checksums
1b8816b6f3194445e686665bbab5c40644c48ebd715f993559fc1ac8a1485621
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.2-cp39-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl

Download URL pyforesight-0.1.2-cp39-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Size 580.7 kB
Tags CPython 3.9 Linux glibc 2.17+ ARM64 abi3
SHA-256 checksum
How to use checksums
4f1a1f86e2f5595076acc5c294b1af20642ae01c9bbc531860fb18ce3810284e
BLAKE2b-256 checksum
How to use checksums
61038e86183c7f81ca3deaa7cdc82157b1db0e58fe092b794ade767fd5cfbc12
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.2-cp39-abi3-macosx_11_0_arm64.whl

Download URL pyforesight-0.1.2-cp39-abi3-macosx_11_0_arm64.whl
Size 544.7 kB
Tags CPython 3.9 abi3 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
7ba6b1dccec8b04d1a46694ab5432d40b8ff60a9d748f6e6689142139785e1d7
BLAKE2b-256 checksum
How to use checksums
26ee79eb6ebbb19275a9c9e771102b22a304d3ef98eba70bd285abd00e9235b2
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.2-cp39-abi3-macosx_10_12_x86_64.whl

Download URL pyforesight-0.1.2-cp39-abi3-macosx_10_12_x86_64.whl
Size 570.6 kB
Tags CPython 3.9 abi3 macOS 10.12+ x86-64
SHA-256 checksum
How to use checksums
32fd2c96d4ed8eb2bd402ee5ee5dc5ecaa0b1b5e9dedf289032b9998daafbbda
BLAKE2b-256 checksum
How to use checksums
2490c71dcc53e33225dee463d308b2492aab02a1da3e1ef8753c6038027b68c9
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

This release

0.1.2 This release

6 release files

0.1.1

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