Metadata-Version: 2.5
Name: sfhybrid
Version: 0.3.0a2
Summary: SFHybrid: probabilistic forecasting of multi-series, multi-scale systems with modelers, a statistical slow layer, a held-out-admitted FX layer and optional agents (alpha)
Project-URL: Homepage, https://github.com/chengjunyan1/sfhybrid
Project-URL: Documentation, https://junyan.ch/sfx/
Project-URL: Repository, https://github.com/chengjunyan1/sfhybrid
Project-URL: Issues, https://github.com/chengjunyan1/sfhybrid/issues
Project-URL: Changelog, https://github.com/chengjunyan1/sfhybrid/blob/main/CHANGELOG.md
Author: Junyan Cheng
License-Expression: Apache-2.0
License-File: LICENSE
Keywords: copula,forecasting,llm-agents,neurosymbolic,probabilistic,regime-switching,scoring-rules,spatiotemporal
Classifier: Development Status :: 3 - Alpha
Classifier: Intended Audience :: Science/Research
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
Classifier: Topic :: Scientific/Engineering :: Mathematics
Requires-Python: >=3.10
Requires-Dist: numpy>=1.24
Requires-Dist: pandas<4,>=1.5
Requires-Dist: scipy>=1.9
Provides-Extra: all
Requires-Dist: exchange-calendars>=4.5; extra == 'all'
Requires-Dist: holidays>=0.40; extra == 'all'
Requires-Dist: httpx<1,>=0.25; extra == 'all'
Requires-Dist: lightgbm>=4.0; extra == 'all'
Requires-Dist: pyyaml>=6.0; extra == 'all'
Provides-Extra: calendars
Requires-Dist: exchange-calendars>=4.5; extra == 'calendars'
Provides-Extra: docs
Requires-Dist: mkdocs-material>=9.5; extra == 'docs'
Requires-Dist: mkdocstrings[python]>=0.26; extra == 'docs'
Provides-Extra: holidays
Requires-Dist: holidays>=0.40; extra == 'holidays'
Provides-Extra: httpx
Requires-Dist: httpx<1,>=0.25; extra == 'httpx'
Provides-Extra: lightgbm
Requires-Dist: lightgbm>=4.0; extra == 'lightgbm'
Provides-Extra: test
Requires-Dist: pytest>=7; extra == 'test'
Requires-Dist: pyyaml>=6.0; extra == 'test'
Requires-Dist: scikit-learn>=1.2; extra == 'test'
Provides-Extra: torch
Requires-Dist: torch>=2.1; extra == 'torch'
Provides-Extra: vertex
Requires-Dist: anthropic[vertex]>=2.3; extra == 'vertex'
Requires-Dist: google-auth>=2.0; extra == 'vertex'
Provides-Extra: yaml
Requires-Dist: pyyaml>=6.0; extra == 'yaml'
Description-Content-Type: text/markdown

# sfhybrid

[![docs](https://img.shields.io/badge/docs-junyan.ch%2Fsfx-7B3F61)](https://junyan.ch/sfx/)
[![status: alpha](https://img.shields.io/badge/status-alpha-orange)](https://github.com/chengjunyan1/sfhybrid/blob/main/docs/experimental.md)

**SFHybrid** (abbreviated **SFX**) is a framework for probabilistic forecasting of time-varying multivariate
systems: many series, several time scales and hierarchy levels, with optional text and agents.

Documentation: **<https://junyan.ch/sfx/>**

```bash
pip install "sfhybrid>=0.3.0a2"      # 0.3.0a2 is a pre-release; core needs only numpy, scipy, pandas
```

Ask for the pre-release by version as above rather than with `pip install --pre`, which also lets every dependency
resolve to its own pre-releases.

```python
import sfhybrid as sfx
from sfhybrid.datasets import make_panel

panel, _ = make_panel(n_nodes=6, n_steps=24 * 90)      # synthetic: long (ts, node, value)
m = sfx.SFForecaster().fit(panel)                      # Level 0: no configuration
d = m.predict_dist()                                   # one joint predictive law
d.quantile([0.05, 0.5, 0.95]); d.prob(above=0.0)
d.aggregate("all").sample(1000)                        # coherent sums over nodes and sub-windows
m.explain()                                            # every learned and admitted move, with held-out evidence
```

Your own data: a long table with one row per timestamp and series.

```python
import pandas as pd
df = pd.read_csv("data.csv")                           # columns ts, node, value (ts parseable as dates)
m = sfx.SFForecaster().fit(df)                         # other names: sfx.Panel(df, ts="date", node="store", value="sales")
print(m.predict())                                     # origin, target_ts, step, node, mean, std, q0.05, q0.5, q0.95
```

Timestamps are stored and returned as naive UTC (tz-aware inputs are converted) and node ids as strings.

## Status

**Alpha.** The API may change. This package makes **no performance claims**; the research results (benchmarks,
ablations and the agent study) live in the SFHybrid tech report (cited below), and this library is not presented as a
reproduction of its numbers. What is experimental, what is a stub and what is known not to be calibrated is listed
in [What is experimental](https://junyan.ch/sfx/experimental/).

The tech report: Junyan Cheng, *SFHybrid: Spatiotemporal Neurosymbolic Modeling of Socio-Cyber-Physical Systems*,
technical report, Dartmouth College, 2026 (forthcoming).

## What it is

SFHybrid = **modelers** (centers, base widths) + an **FX layer** that splits each forecast into **spectra**
(frequency, magnitude and correlation by default; lead-lag and co-volatility opt-in), grown in an automatic order,
each admitted on held-out data (the magnitude regime trees, the Haar band split and the lead-lag coupling recursion
by one self-similar rule, the **fractal splitter**: score by exact gain, admit on held-out data, recurse), and
composes the parts into **one joint law**. Each accepted split exposes **slots**; a statistical slow layer, **StatSlow**,
fills them by default. An optional agent contributes **discrete
labels** through the **TypedDecision** protocol. The **adapter** is a small regression layer over StatSlow's band
probabilities: each label adds a learned log-odds offset per band (ridge regression on past outcomes), a
probability output gets one log-linear interpolation weight, and the result is used only if it passes a per-slot
held-out **acceptance test**. Tree splits, the regime slot, dependence structures, agent judgments and data-driven
symbolic modules are candidates admitted by the same test, at its false-acceptance rate, so a useless candidate
usually collapses to the simpler model. Cited facts are the exception: they act as hard constraints without a test
(a closure fact replaces the forecast with the last value).

- Spectra (`sfx.spectra`): frequency, scale and coherence by default; phase (lead-lag) and co-scale (shared
  volatility states) opt-in; your own through a registry. Each has a split algorithm, an agent slot with its own
  decoder, and an ordering contract; the engine orders them automatically and validates a fixed order.
- The fractal splitter (`sfx.FractalSplitter`): exact-gain splits for Gaussian scale, location and joint, Poisson,
  gamma, Bernoulli and coupling families, admitted tile by tile on held-out data.
- Leaves: Gaussian, Student-t, negative binomial, discretised Student-t, categorical.
- Exact split gains (scale, location, gamma, Poisson, categorical) and their Newton approximations.
- Acceptance rules d / e1 / e2 (default) and the exact surrogate test.
- Readouts: marginals, quantiles, event probabilities, sums over nodes and sub-windows, joint draws, log score,
  CRPS, energy score.
- Backtest mode with as-of text reads, a model-cutoff check and a prompt datescan; live mode.
- StatSlow's as-of level stretch is opt-in (`StatSlow(recal=True)`): in the SFHybrid tech report's out-of-sample
  check the level stretch did not replicate, so it is off by default.
- Presets: `xgboost_like`, `gamlss_like`, `factor_model`, `msm_volatility`, `mfdm`, `agent_hybrid`, each with a
  parity test on synthetic data.
- Symbolic modules: formulas as modelers, topologies, hierarchies, cited facts, rules; built-in calendar, market and
  seismology symbols.
- Modelers: a HAR-type ridge (default), a small numpy multilayer perceptron (`"mlp"`), LightGBM (extra).
- Hosting (`sfx.Host`): other forecasters (deep networks, foundation models, baselines, other configurations) as
  members of the modeler axis, a mixture of joint predictive laws whose members are admitted on held-out data.
- Experimental interventions (`sfx.interventions`, `experimental=True`): typed, logged and stamped on every law and
  score; no claims are made for them.
- `SFRegressor` / `SFClassifier`: the degenerate no-clock (tabular) case, for API uniformity only.

## Documentation

The site, <https://junyan.ch/sfx/>, is built from `docs/` with MkDocs Material:
[quickstart](https://junyan.ch/sfx/quickstart/),
[concepts](https://junyan.ch/sfx/concepts/overview/),
[presets](https://junyan.ch/sfx/presets/), [hosting other models](https://junyan.ch/sfx/hosting/), the API reference (hand-written pages and pages
generated from the docstrings), the [FAQ](https://junyan.ch/sfx/faq/) and the
[changelog](https://github.com/chengjunyan1/sfhybrid/blob/main/CHANGELOG.md). Runnable examples,
[examples/quickstart.py](https://github.com/chengjunyan1/sfhybrid/blob/main/examples/quickstart.py) and
[examples/hosting.py](https://github.com/chengjunyan1/sfhybrid/blob/main/examples/hosting.py), ship in the
repository and the source distribution (not in the wheel).

## Optional extras

`pip install "sfhybrid[lightgbm]>=0.3.0a2"` (gradient-boosted modeler), `[httpx]` (LLM backends over HTTP; httpx
0.x), `[yaml]` (YAML rules), `[holidays]`, `[calendars]` (symbolic calendars: `exchange_calendars`), `[all]` (all of
these: lightgbm, httpx, yaml, holidays, calendars), `[vertex]` (Claude and Gemini on Vertex AI, for the documented
default LLM `Agent(llm="claude-opus-4-6")`; not in `[all]`), `[torch]` (for a planned PyTorch modeler; nothing uses
it yet, and `MLPModeler` needs no extra), `[docs]` (MkDocs Material and mkdocstrings), `[test]`.

## Licence

Apache-2.0.
