Skip to content

[FEAT] Unified error distributions across the StatsForecast suite (ETS, CES, Theta + shared C++/Python infra) - #1116

Merged
Saul (nasaul) merged 36 commits into
mainfrom
feat/ets_likelihood
Jul 2, 2026
Merged

Saul (nasaul) merged 36 commits into
mainfrom
feat/ets_likelihood

Conversation

@nasaul

@nasaul Saul (nasaul) commented Mar 19, 2026 •

Copy link
Copy Markdown
Contributor

Summary

This PR began as "add non-normal error distributions for fitting ETS" but its scope has expanded into unified error-distribution infrastructure across the StatsForecast suite. It introduces a single C++ header and a single Python module that own all distribution math and MLE-fitting helpers, then wires four models — ARIMA, ETS, AutoCES, and AutoTheta — through them.

Supported distributions: normal, laplace, t, skew-normal, ged.

Motivation

PR #1113 added distribution-aware MLE fitting to ARIMA, and the original version of this PR added it to ETS — independently, duplicating the C++ likelihood math and splitting the Python distribution utilities between utils.py and simulation.py. This PR consolidates that into shared layers so each future method (and each future distribution) is a small, single-recipe extension rather than another copy of the math.

What's included

Shared infrastructure

  • include/statsforecast/distributions.h — single C++ source of truth: the Distribution enum and per-observation negative-log-likelihood cores (negloglik_laplace/t/skewnorm/ged).
  • python/statsforecast/distributions.py — single Python hub: re-exports the Distribution enum / quantile utilities and adds shared fitting helpers (switch_distribution, dist_init_params, extract_dist_params, aic_bic_aicc, error_params_from_model).

Model integration

  • ETS — refactored to use both shared layers with no behavior change (proving the infra; the existing distribution tests are the regression guard).
  • ARIMA — distribution imports now point at the shared hub (distributions.py); no numeric change.
  • AutoCES — new distribution-aware one-pass fitting (optimize_dist in ces.cpp), distribution-aware prediction intervals, and distribution= threaded through cesmodel/auto_ces/AutoCES.
  • AutoTheta — new C++ likelihood-based fitting for non-normal distributions (optimize_dist in theta.cpp) with AIC-based model selection; normal path unchanged. distribution= threaded through thetamodel/auto_theta/AutoTheta.

Simulation

  • .simulate() now defaults error_distribution to the fitted model_["distribution"] and emits a UserWarning on an explicit mismatch (it still proceeds). SUPPORTED_DISTRIBUTIONS kept as a deprecated alias.

Tests

  • tests/test_distributions.py — unified parametrized suite covering the model-dict key contract across ARIMA/ETS/CES/Theta, scipy quantile/log-likelihood cross-checks, prediction-interval ordering, and the simulate-mismatch warning.

Notes / limitations

  • Distribution is user-fixed: auto_ces/auto_theta select only the structural type (seasontype / modeltype), not the distribution.
  • CES normal lik is the concentrated n·log(SSE), while the non-normal objective is a full -2logL; these are not comparable across distributions — fine because the distribution is fixed per fit.
  • Fitting everywhere is one-pass: Python seeds the extended x0, C++ runs a single Nelder-Mead.

Out of scope (future PRs)

  • AutoMFLES, AutoTBATS, GARCH
  • Additional distributions beyond the five above

Checklist

  • Shared C++ header + Python hub
  • ETS/ARIMA refactored onto shared layers (no behavior change)
  • CES & Theta distribution-aware fitting + intervals
  • Simulation defaults to fitted distribution + mismatch warning
  • Unified parametrized test suite

@codspeed

codspeed Bot commented Mar 19, 2026 •

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 38 untouched benchmarks


Comparing feat/ets_likelihood (9a654f7) with main (b49ff13)

Open in CodSpeed

@nasaul
Saul (nasaul) marked this pull request as ready for review April 27, 2026 14:56
Spec for extending non-normal error distribution fitting (ARIMA, ETS)
to AutoCES and AutoTheta via shared C++ header and Python distributions module.
# Conflicts:
#	python/statsforecast/utils.py
- Create python/statsforecast/distributions.py: re-exports Distribution,
  ArimaMethod, _quantiles, _calculate_intervals from utils.py and adds
  dist_init_params, extract_dist_params, distribution_n_extra_params,
  switch_distribution, aic_bic_aicc helpers shared by ARIMA/ETS/CES/Theta.
- Add tests/test_distributions.py with base tests (test_switch_distribution_maps_all
  is deferred until Task 3 adds _ets.Distribution C++ binding).
- Fix Distribution.__str__ in utils.py to return value string on Python 3.11+.
…vior change)

Replace local Distribution enum with dist::Distribution from distributions.h;
swap inline per-distribution NLL math in ObjectiveFunctionDist with calls to
dist::negloglik_* (adding sumlog_adj for multiplicative errors); update the
pybind enum binding to bind dist::Distribution. In ets.py, delete local
switch_distribution and replace inline dist_init/extract/AIC blocks with the
four shared helpers: switch_distribution, dist_init_params, extract_dist_params,
aic_bic_aicc.
Point arima.py's ArimaMethod, Distribution, _VALID_DISTRIBUTIONS, _quantiles
imports at the shared distributions.py hub (re-exports) instead of utils.py
directly. No numeric or behavioral change.
… intervals

Add distribution-aware CES fitting (ces_target_fn_dist, optimize_dist, Distribution
binding in C++) with one-pass joint optimization over smoothing + dist params.
Thread distribution="normal" through cesmodel/auto_ces/AutoCES; rewire
_simulate_pred_intervals to use sample_errors with fitted distribution.
Add error_params_from_model helper to distributions.py.
Add cross-method key-contract matrix (4 models × 5 distributions) and
heavy-tailed AIC sanity test (ETS t-dist < normal on Student-t AR(1) data).
…validation messages

- simulation.py: use d.value instead of str(d) to build _SUPPORTED_SIMULATION_DISTRIBUTIONS
- models.py: render clean value tuples in AutoCES/AutoTheta distribution validation error messages
- distributions.py: add : str type annotation to distribution_n_extra_params parameter
@nasaul Saul (nasaul) changed the title [FEAT] Adds t, ged, normal-skewed and laplace distributions for fitting ETS feat: unified error distributions across the StatsForecast suite (ETS, CES, Theta + shared C++/Python infra) Jun 24, 2026
@nasaul Saul (nasaul) changed the title feat: unified error distributions across the StatsForecast suite (ETS, CES, Theta + shared C++/Python infra) [FEAT] Unified error distributions across the StatsForecast suite (ETS, CES, Theta + shared C++/Python infra) Jun 24, 2026
…istribution factory

- Replace _quantiles body with one-line delegation to frozen_error_distribution(1.0, ...)
- Remove now-unused scipy.stats imports (norm, laplace_dist, t_dist, skewnorm_dist, gennorm_dist) from utils.py
- Update ETS _calculate_intervals call to pass error_params_from_model(model) instead of raw model dict
- Update both ARIMA _quantiles call sites to pass error_params_from_model(model/self.model_.model)
- Add error_params_from_model to distributions imports in ets.py and arima.py
- Laplace analytic intervals now use scale=sigma/sqrt(2) (consistent with MC); no test value changes needed
…a factory

Replace the explicit if/elif parametric dispatch (lines 225-278) in
sample_errors with a single frozen_error_distribution().rvs() call.
This fixes the sigma=scale vs sigma=SD mismatch for t, skew-normal, and
ged distributions — the old code incorrectly rescaled sigma to match SD
rather than using it directly as the scipy scale parameter.

Add MC scale test (test_sample_errors_uses_sigma_as_scale) that verifies
empirical p97.5 matches the analytic PPF within 2% for all three
parametric distributions at sigma=2.0.

Update two existing validation tests whose error-message assertions
targeted the old custom dispatch: invalid-df/invalid-shape now raise
ValueError via scipy's domain check rather than hand-rolled messages.
Comment thread python/statsforecast/ets.py Resolved
…bution

Replace the per-distribution elif ladder in _compute_pred_intervals with a
single frozen_error_distribution() call driven by error_params_from_model(),
removing the direct scipy.stats imports from ets.py. Distributionally
equivalent to the prior branches (verified scale factors match).
Replace the per-distribution elif ladder in _compute_pred_intervals with the
shared statsforecast.simulation.sample_errors() helper, matching how CES
(_simulate_pred_intervals) and Theta (compute_pi_samples) already sample
errors. Drops the direct scipy.stats imports from ets.py and swaps the global
np.random.seed(1) for a local np.random.default_rng(1), so all three models
share one sampling path (params via error_params_from_model, residual-fit for
normal/laplace when params is None).

@marcopeix Marco (marcopeix) left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very nice!

@nasaul
Saul (nasaul) merged commit fe9c3f8 into main Jul 2, 2026
111 of 113 checks passed
@nasaul
Saul (nasaul) deleted the feat/ets_likelihood branch July 2, 2026 17:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants