pymc_forecast.metrics#
Probabilistic forecast metrics, dim-aware.
Each eval_* metric takes forecast samples and ground truth and reduces them
to a single float; crps_empirical() returns elementwise scores and
make_mase() builds a metric from training data.
Inputs may be labeled (xarray.DataArray) or raw numpy:
DataArray predictions carry their sample dimensions by name —
chain/draw(as produced by the forecast drivers) or an already-stackedsampledim. Labeled truth is transposed to the prediction’s dim order and reordered to matching coordinates. Different or duplicate labels are rejected. Unlabeled inputs are positional and must match the value shape exactly; implicit broadcasting is rejected.numpy predictions follow the classical convention: sample axis first.
Ported from numpyro_forecast (itself porting pyro.ops.stats.crps_empirical)
with the JAX kernels replaced by NumPy.
- pymc_forecast.metrics.DEFAULT_METRICS = {'coverage': <function eval_coverage>, 'crps': <function eval_crps>, 'mae': <function eval_mae>, 'rmse': <function eval_rmse>}#
Default metrics used by
evaluate_forecast()andbacktest.
- pymc_forecast.metrics.crps_empirical(pred, truth)[source]#
Elementwise empirical Continuous Ranked Probability Score.
\[\mathrm{CRPS}(F, y) = \mathbb{E}|X - y| - \tfrac{1}{2}\,\mathbb{E}|X - X'|\]estimated from the forecast samples with the sorted-sample \(O(n \log n)\) identity.
- Parameters:
pred (
xarray.DataArrayorarray_like) – Forecast samples: a DataArray with sample dims namedchain,draworsample, or an array with the sample axis first. At least 2 samples are required.truth (
xarray.DataArrayorarray_like) – Ground-truth values with the prediction’s shape without the sample axis. Whenpredis labeled, labeled truth is aligned by dim name and coordinates; otherwise it is used positionally.
- Returns:
numpy.ndarray– Elementwise CRPS as an unlabeled float64 array, one value per data location (value dims in the prediction’s dim order).- Raises:
ValueError – If there are fewer than 2 samples, a labeled
predhas no sample dim, orpredandtruthdisagree on dims, sizes, coordinates or shape.
- pymc_forecast.metrics.eval_coverage(pred, truth, *, alpha=0.9)[source]#
Empirical coverage of the central
alphaprediction interval.A well-calibrated forecast has coverage close to
alpha. Bind a non-default level withfunctools.partial(eval_coverage, alpha=0.8).- Parameters:
pred (
xarray.DataArrayorarray_like) – Forecast samples: a DataArray with sample dims namedchain,draworsample, or an array with the sample axis first.truth (
xarray.DataArrayorarray_like) – Ground-truth values with the prediction’s shape without the sample axis. Whenpredis labeled, labeled truth is aligned by dim name and coordinates; otherwise it is used positionally.alpha (
float, default0.9) – Nominal interval probability, in the open interval (0, 1). The bounds are the(1 - alpha) / 2and1 - (1 - alpha) / 2sample quantiles and are inclusive.
- Returns:
float– Fraction of value elements whose truth lies inside the interval.- Raises:
ValueError – If
alphais not in (0, 1),predhas no samples, a labeledpredhas no sample dim, orpredandtruthdisagree on dims, sizes, coordinates or shape.
- pymc_forecast.metrics.eval_crps(pred, truth)[source]#
Mean empirical CRPS over all data elements (see
crps_empirical()).- Parameters:
pred (
xarray.DataArrayorarray_like) – Forecast samples: a DataArray with sample dims namedchain,draworsample, or an array with the sample axis first. At least 2 samples are required.truth (
xarray.DataArrayorarray_like) – Ground-truth values with the prediction’s shape without the sample axis. Whenpredis labeled, labeled truth is aligned by dim name and coordinates; otherwise it is used positionally.
- Returns:
float– The elementwise CRPS averaged over every value element.- Raises:
ValueError – If there are fewer than 2 samples, a labeled
predhas no sample dim, orpredandtruthdisagree on dims, sizes, coordinates or shape.
- pymc_forecast.metrics.eval_interval_score(pred, truth, *, alpha=0.9)[source]#
Mean Winkler interval score of the central
alphainterval.Rewards narrow intervals, penalizes truth falling outside; lower is better.
- Parameters:
pred (
xarray.DataArrayorarray_like) – Forecast samples: a DataArray with sample dims namedchain,draworsample, or an array with the sample axis first.truth (
xarray.DataArrayorarray_like) – Ground-truth values with the prediction’s shape without the sample axis. Whenpredis labeled, labeled truth is aligned by dim name and coordinates; otherwise it is used positionally.alpha (
float, default0.9) – Nominal interval probability, in the open interval (0, 1). The bounds are the(1 - alpha) / 2and1 - (1 - alpha) / 2sample quantiles.
- Returns:
float– The interval score averaged over every value element.- Raises:
ValueError – If
alphais not in (0, 1),predhas no samples, a labeledpredhas no sample dim, orpredandtruthdisagree on dims, sizes, coordinates or shape.
- pymc_forecast.metrics.eval_mae(pred, truth)[source]#
Mean absolute error of the forecast sample median.
- Parameters:
pred (
xarray.DataArrayorarray_like) – Forecast samples: a DataArray with sample dims namedchain,draworsample, or an array with the sample axis first.truth (
xarray.DataArrayorarray_like) – Ground-truth values with the prediction’s shape without the sample axis. Whenpredis labeled, labeled truth is aligned by dim name and coordinates; otherwise it is used positionally.
- Returns:
float– The absolute error averaged over every value element.- Raises:
ValueError – If
predhas no samples, a labeledpredhas no sample dim, orpredandtruthdisagree on dims, sizes, coordinates or shape.
- pymc_forecast.metrics.eval_pinball(pred, truth, *, quantile=0.5)[source]#
Mean pinball (quantile) loss of the forecast
quantile.At
quantile=0.5this is half the mean absolute error.- Parameters:
pred (
xarray.DataArrayorarray_like) – Forecast samples: a DataArray with sample dims namedchain,draworsample, or an array with the sample axis first.truth (
xarray.DataArrayorarray_like) – Ground-truth values with the prediction’s shape without the sample axis. Whenpredis labeled, labeled truth is aligned by dim name and coordinates; otherwise it is used positionally.quantile (
float, default0.5) – Quantile level, in the open interval (0, 1); the point estimate is the corresponding sample quantile.
- Returns:
float– The pinball loss averaged over every value element.- Raises:
ValueError – If
quantileis not in (0, 1),predhas no samples, a labeledpredhas no sample dim, orpredandtruthdisagree on dims, sizes, coordinates or shape.
- pymc_forecast.metrics.eval_rmse(pred, truth)[source]#
Root mean squared error of the forecast sample mean.
- Parameters:
pred (
xarray.DataArrayorarray_like) – Forecast samples: a DataArray with sample dims namedchain,draworsample, or an array with the sample axis first.truth (
xarray.DataArrayorarray_like) – Ground-truth values with the prediction’s shape without the sample axis. Whenpredis labeled, labeled truth is aligned by dim name and coordinates; otherwise it is used positionally.
- Returns:
float– The root of the squared error averaged over every value element.- Raises:
ValueError – If
predhas no samples, a labeledpredhas no sample dim, orpredandtruthdisagree on dims, sizes, coordinates or shape.
- pymc_forecast.metrics.evaluate_forecast(pred, truth, *, metrics=None)[source]#
Apply several metrics to the same forecast samples and ground truth.
- Parameters:
pred (
xarray.DataArrayorarray_like) – Forecast samples, passed unchanged to every metric (labeled with sample dims, or an array with the sample axis first).truth (
xarray.DataArrayorarray_like) – Ground-truth values, passed unchanged to every metric.metrics (
mapping, optional) – Mapping of name to metricfn(pred, truth);NoneusesDEFAULT_METRICS. Bind metric parameters withfunctools.partial.
- Returns:
dict– Maps each metric name (str) to its value (float, cast withfloat), in the mapping’s iteration order.- Raises:
ValueError – If a metric rejects the inputs (see the individual metrics).
- pymc_forecast.metrics.make_mase(train_data, *, seasonality=1)[source]#
Build a Mean Absolute Scaled Error metric scaled by
train_data.MASE divides the forecast MAE (sample-median point estimate) by the in-sample MAE of the seasonal-naive forecast on
train_data. The scale is computed once at factory time as a single scalar pooled over every element oftrain_data(not per series).- Parameters:
train_data (
xarray.DataArrayorarray_like) – Training data — a DataArray with a"time"dim, or an array with time on axis 0.seasonality (
int, default1) – Seasonal period (>= 1);1is the random-walk naive baseline.
- Returns:
callable– A metricmase(pred, truth) -> floatequal toeval_mae()divided by the training scale; it accepts the samepred/truthinputs and raises the same errors aseval_mae().- Raises:
ValueError – If
seasonality < 1,train_datais not longer thanseasonalityalong time, or the seasonal-naive scale is zero (constant training series).