Time-series forecasting and attribution utilities for Time-Anchor checkpoints.
Project description
license: apache-2.0 library_name: transformers tags:
- time-series
- forecasting
- explainability
- modernbert pipeline_tag: time-series-forecasting
Time-Anchor
A time-series forecasting package built around three outputs from one call:
| 📈 Quantile forecast | Probabilistic forecasts at any requested quantile levels |
| 🧭 Variable / time impact | Normalized contribution of every input variable at every time step |
| 📌 Anchor forecast | Forecasts conditioned on known future points you specify |
Hourly temperature in Tokyo (Open-Meteo archive), 64-hour holdout: median MAE 0.57 °C, with every actual value inside the q10–q90 band.
Installation
python -m pip install -e .
Quick Start
import pandas as pd
from time_anchor import predict_time_anchor
url = (
"https://archive-api.open-meteo.com/v1/archive"
"?latitude=35.69&longitude=139.69&start_date=2024-10-01&end_date=2024-12-31"
"&hourly=temperature_2m,relative_humidity_2m,surface_pressure,wind_speed_10m&format=csv"
)
weather = pd.read_csv(url, skiprows=3)
temperature = weather.iloc[:, 1].astype("float32")
result = predict_time_anchor(
"models/time-anchor-modernbert-32m",
target_context=temperature[:1440],
prediction_length=64,
quantile_levels=(0.1, 0.5, 0.9),
)
print(pd.DataFrame(result.forecast_rows))
The first argument may be a local checkpoint directory or a Hugging Face Hub model id.
For a combined code/model repository, add subfolder="models/time-anchor-modernbert-32m".
Variable / Time-Step Impact
Per-hour contribution of each weather variable over a one-week Tokyo context.
For each time_index, impact across all variables sums to 1.
result = predict_time_anchor(
"models/time-anchor-modernbert-32m",
target_context=temperature[:168],
explanatory_contexts=[weather.iloc[:168, i].astype("float32") for i in (2, 3, 4)],
gaf={"enabled": True, "topk_time_steps": 0},
)
print(pd.DataFrame(result.variable_impact_rows))
Validation: mixing three equal-amplitude sinusoids with known weights recovers the mixing ratios — true weights 0.60/0.30/0.10 yield measured shares 0.55/0.33/0.12, and reordering the weights reorders the shares. In a regime-switch test the per-time-step impact follows whichever function is active in each half of the context.
Anchor Forecast
Monthly airline passengers (Box & Jenkins), 24-month holdout: pinning six known months cuts the median forecast MAE from 46 to 10 thousand passengers.
url = "https://raw.githubusercontent.com/jbrownlee/Datasets/master/airline-passengers.csv"
passengers = pd.read_csv(url)["Passengers"].astype("float32")
result = predict_time_anchor(
"models/time-anchor-modernbert-32m",
target_context=passengers[:-24],
prediction_length=24,
anchor={
"mode": "observed",
"positions": [4, 8, 12, 16, 20, 24],
"values": [396, 559, 405, 461, 606, 432],
},
)
print(pd.DataFrame(result.forecast_rows))
positions are 1-based forecast horizon steps. Anchor forecasts use the target
history only; disable anchors when passing explanatory_contexts.
CLI
time-anchor-infer # forecast + impact on the bundled sample
time-anchor-infer --no-impact # forecast only
time-anchor-infer --no-impact --anchor-mode observed \
--anchor-positions 12,24,36 --anchor-values 0.2,0.4,0.1
time-anchor-infer --checkpoint K-Iwa/time-anchor-modernbert-32m --input data.csv
Outputs are written to output/forecast.csv, output/variable_impact.csv, and
output/result.json. time-anchor-sine-test runs a sine-wave anchor evaluation
(requires pip install -e ".[examples]").
Repository Layout
src/time_anchor— model, pipeline, anchor samplers, and explainability codemodels/time-anchor-modernbert-32m— checkpoint and model cardexamples/sample_data.csv— small input file for smoke testsdocs/publishing.md— Hugging Face / PyPI release checklist
Development
python -m pip install -e ".[dev]"
python -m ruff check .
python -m ruff format --check .
python -m pytest
References
- Ansari et al. Chronos: Learning the Language of Time Series. TMLR 2024 — informs the forecasting/tokenization design.
- Azarkhalili and Libbrecht. Generalized Attention Flow: Feature Attribution for Transformer Models via Maximum Flow. 2025 — the variable/time impact method (GAF).
See docs/references.md, NOTICE, and CITATION.cff for attribution details.
License
Apache-2.0. Some source files retain upstream Amazon copyright notices.
Figures use historical weather data from Open-Meteo (CC BY 4.0) and the classic airline passengers dataset (Box & Jenkins, 1976).
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file time_anchor-0.1.1.tar.gz.
File metadata
- Download URL: time_anchor-0.1.1.tar.gz
- Upload date:
- Size: 90.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8f9a30bd68cac09a003c8d6b1529176309b25dd50a874a3c6769c4500024b511
|
|
| MD5 |
1783ecd65e39f2b03fc7487d397c9f99
|
|
| BLAKE2b-256 |
25174aaa727378a0fe74bd64fa7edae39231a20a7032ffe900908664ad3dae57
|
File details
Details for the file time_anchor-0.1.1-py3-none-any.whl.
File metadata
- Download URL: time_anchor-0.1.1-py3-none-any.whl
- Upload date:
- Size: 86.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
38abd2056e2ff25a9905654608939d3cf68df9b30acf3c69eba1758d7eb47fbc
|
|
| MD5 |
e48aee036a8ee9fde67dfcdcb5fb0f20
|
|
| BLAKE2b-256 |
0fce782a82e2df7106b38972e76d2834f0c4b144bb6123df21bcc7191767721a
|