Metrics and diagnostics¶
In short. Metrics measure how far a forecast is, both from what actually
happened (mse, mae, mase) and from the answer key (avoidable_mse);
lower is better. Every report records these; more metrics, such as brier and
log_loss for yes/no outcomes, or functions of your own, are recorded when you
ask for them with metrics=. Output checks of your own, such as a range the
values must stay in, are added with checks=. For forecasts of a yes/no outcome,
calibration says whether a forecast of 70% came true about 70% of the time. A diagnostic is one check's result: a verdict (PASS, FAIL,
INCONCLUSIVE, UNSUPPORTED or ERROR), a message, and the numbers it rests
on, called its evidence. Accuracy diagnostics are always INCONCLUSIVE,
because TimeGym sets no pass mark for accuracy; that threshold is yours.
How TimeGym decides explains the verdicts.
Metrics¶
All metrics are losses, so lower is better. Each takes finite arrays and raises
ValueError for empty, nonfinite or misshapen input: predicted matches the
shape of observed, quantiles add a trailing level axis, and samples add a
leading sample axis. UndefinedMetricError, a ValueError subclass, marks a
metric that is undefined for otherwise valid data.
Point metrics¶
A point metric is minimized in expectation by one functional of the predictive
distribution, so it has to match what the point denotes
(Gneiting 2011).
Forecast.point denotes a predictive mean.
| Function | What it computes | What it measures | Evaluator records it |
|---|---|---|---|
mse(observed, predicted) |
Mean squared error over all coordinates | The mean: squared error is minimized in expectation by the predictive mean | With a point output |
mae(observed, predicted) |
Mean absolute error over all coordinates | The median: absolute error is minimized in expectation by the predictive median | With a point output |
mase(observed, predicted, history, seasonality=1) |
Absolute error divided, per target, by the mean absolute seasonality-step change of that target in the history (time is axis zero), then averaged |
The median, as for mae, because each target's scale is fixed by the history |
With a point output, always with seasonality=1 |
rmse(observed, predicted) |
Square root of mse, in the data's units |
The mean, as for mse |
When asked for with metrics= |
rmsse(observed, predicted, history, seasonality=1) |
Each target's RMSE divided by the root mean squared seasonality-step change of that target in the history, then averaged over targets: the scaled error of the M5 competition (Makridakis, Spiliotis and Assimakopoulos 2022) |
The mean, as for mse |
When asked for with metrics= |
mean_reference_mse(reference_mean, predicted) |
Mean squared distance from a supplied conditional mean | The conditional mean itself: with a valid conditional mean it is the conditional excess squared-error risk, not realized loss against a future observation | With a point output and a case reference, as avoidable_mse |
mase and rmsse raise UndefinedMetricError when a target's naive-error
scale is zero, as for a constant target, or when the history is no longer than
seasonality; they add no epsilon to the denominator and do not drop constant
targets. MAPE and sMAPE are left out on purpose: they are undefined when an
actual value is zero and favour forecasts that are too low
(Hyndman and Koehler 2006);
mase and rmsse meet the same need.
evaluate_forecast always calls mase with seasonality=1, so the evaluator
scales each target by its lag-1 naive MAE over the history whatever the scenario's
seasonal period; point_accuracy evidence records mase_seasonality: 1.
mean_reference_mse is not a reference for other losses.
Each case with an answer key records the distance from it as avoidable_mse:
the part of the error the model could have avoided, beside the noise no
forecaster can remove, under the name the scorecard's table gives the same
number. Its old name still reads it, and warns: case.metrics["mean_reference_mse"]
and metric="mean_reference_mse" in plot_errors, result.compare and
compare_reports.
Reports saved by TimeGym 1.14.1 and earlier load with the new name, so they
compare with new ones.
Uncertainty metrics¶
Calibration and sharpness are the two properties these metrics reward. Calibration is the statistical consistency between the predictive distribution and the observations; sharpness is the concentration of the predictive distribution, a property of the forecast alone. The aim is to maximize sharpness subject to calibration (Gneiting, Balabdaoui and Raftery 2007). A proper scoring rule rewards both at once: its expected value is lowest when the forecast is the distribution the observation is drawn from (Gneiting and Katzfuss 2014).
| Function | What it computes | What it measures | Evaluator records it |
|---|---|---|---|
pinball(observed, quantiles, levels) |
Mean quantile (pinball) loss over coordinates and levels, without the optional factor of two; levels must increase strictly in (0, 1) and quantiles must not cross | The quantile at each level: pinball loss at level α is minimized in expectation by the α-quantile | With a quantile output |
interval_score(observed, quantiles, levels) |
For the band from the lowest to the highest level, which must be symmetric (such as 0.1 and 0.9, a central band of 1 − α = 80%): its width plus 2/α times the distance by which the outcome falls outside it, averaged over coordinates (Winkler 1972; Gneiting and Raftery 2007) | The band's two quantiles, as a proper score for a central interval: narrow bands that still hold the outcome do best | When asked for with metrics= |
weighted_interval_score(observed, quantiles, levels) |
The median's absolute error times 1/2 plus each symmetric pair's interval score times α/2, divided by the number of pairs plus 1/2 (Bracher et al. 2021); levels must be 0.5 plus pairs symmetric around it. It equals twice pinball over the same levels |
Every quantile at once, as a proper score that approximates CRPS | When asked for with metrics= |
crps(observed, samples) |
Mean marginal CRPS of the finite empirical ensemble: the expected absolute error of a draw minus half the expected absolute difference between two draws, diagonal pairs included | Each coordinate's whole marginal distribution, as a proper scoring rule; it is unchanged when samples are permuted independently per coordinate, so it cannot see dependence | With a sample output |
energy_score(observed, samples, chunk_size=128) |
Energy score of the whole flattened horizon-target vector with the Euclidean norm, not divided by the number of coordinates; diagonal pairs included | The whole joint distribution, as a proper scoring rule for multivariate forecasts; with one coordinate it equals CRPS | With a sample output |
variogram_score(observed, samples, exponent=0.5) |
Mean squared difference between the realized and the expected abs(x_i - x_j) ** exponent over all distinct coordinate pairs, equally weighted; exponent lies in (0, 2) |
Pairwise dependence between coordinates; it ignores a shift shared by all coordinates and is not strictly proper over unrestricted joint distributions | With a sample output; undefined for one coordinate |
crps and energy_score score the empirical distribution of the finite
ensemble; neither is the fair-ensemble estimator. chunk_size only bounds the
memory of the pairwise distances.
With at least two quantile levels the evaluator also records
interval_coverage, the share of coordinates between the lowest and the highest
quantile; interval_nominal_coverage, the highest level minus the lowest; and
interval_width, the mean distance between those two quantiles. Coverage bears
on calibration and width on sharpness. One path's coverage across possibly
dependent coordinates does not establish calibration, so the interval_coverage
diagnostic stays INCONCLUSIVE.
Yes/no outcomes¶
A forecast of a yes/no outcome, such as rain tomorrow, a customer leaving this
month or a machine failing within a day, is a probability, and the outcome is
1 or 0. Put the probability in Forecast.point, the predictive mean.
| Function | What it computes | What it measures | Evaluator records it |
|---|---|---|---|
brier(observed, predicted) |
Mean squared distance between the probability and the 0/1 outcome (Brier 1950): mse, after checking that outcomes are 0 or 1 and forecasts lie in [0, 1] |
The probability, as a proper score | When asked for with metrics= |
log_loss(observed, predicted, *, eps=None) |
Mean of minus the natural log of the probability given to what happened (Good 1952) | The probability, as a strictly proper score that punishes confident mistakes hard | When asked for with metrics= |
Both raise ValueError naming the first outcome that is not 0 or 1, or the
first forecast outside [0, 1]. A forecast of exactly 0 or 1 that turned out
wrong gives an infinite log loss, so log_loss raises UndefinedMetricError,
which a report shows as INCONCLUSIVE with the reason. Pass eps (between 0
and 0.5, such as 1e-6) to clip every forecast to [eps, 1 − eps] first; nothing
is clipped unless you ask, and the size of eps sets how much a confident
mistake costs.
Whether a forecaster's chances are honest, so that its forecasts of 70% come
true about 70% of the time, is a question for many forecasts at once:
calibration.
Your own metrics¶
evaluate_forecast, Pipeline.run, ForecastRun.from_forecasts and
Pipeline.test take metrics=: a list of names of functions in
timegym.metrics and functions of your own, or a dictionary from the name to
record to a function. Each value is stored in case.metrics under its name,
beside the metrics every report records, so plot_errors(name), the saved JSON
and comparisons can use it.
from functools import partial
import numpy as np
from timegym.metrics import log_loss
def within_10_percent(observed, predicted):
"""Share of forecasts within 10% of what happened; higher is better."""
return float(np.mean(np.abs(predicted - observed) <= 0.1 * np.abs(observed)))
metrics = ["rmse", within_10_percent] # by name, or your own
metrics = {"log_loss": partial(log_loss, eps=1e-6), "close": within_10_percent} # with settings
A metric says what it needs by its argument names: observed (what happened),
predicted (the point forecast), quantiles, levels, samples and history
(the past the model saw), each a NumPy array shaped as above, time on the first
axis of observed and history. An argument with a default is passed only when
the forecast has it; any other argument must have a default, or the request is
refused before anything runs. An argument named context, the old name of
history, still gets the history and warns once per run. A request is also
refused when its name is already recorded in every report, such as "mse", or
asked for twice: record a metric again with other settings under a new name,
such as {"mase_12": partial(mase, seasonality=12)}. A function needs a name
of its own, so give a lambda or a partial one with a dictionary.
For each case, a metric whose required input the forecast lacks is
UNSUPPORTED, one that raises UndefinedMetricError is INCONCLUSIVE, and one
that raises anything else, or returns something other than one finite number,
is an ERROR whose message names the exception. Each appears as
metric_<name>. The report's metadata lists what was asked for under
"metrics": each metric's name, function, keyword settings and whether it is
replayable. Replay computes again the metrics that come from
timegym.metrics, with plain settings, and leaves the others out of the
comparison, listing them in metadata["replay"]["not_recomputed"]. Nothing is
recorded unless you ask, so reports saved earlier replay exactly as before; one
saved by TimeGym 1.14.1 or earlier lists its metrics under "scores", and replays
them the same way. scores=, the old name of metrics=, still works and warns.
Your own output checks¶
The output checks every forecast goes through (dates, series, shape, finite
values, quantile order) know nothing about your data: a rainfall forecast of
−3 mm or a chance of rain of 1.2 passes them. evaluate_forecast,
Pipeline.run and ForecastRun.from_forecasts take
checks=, a list of functions or a dictionary from name to function, and add
each one to result.checks and assert_passed beside the built-in checks.
timegym.checks.within(lower=None, upper=None) makes the most common one, a
range:
from timegym.checks import within
result = pipeline.run(cases=cases, checks=[within(0, 1)])
result.checks # now also "Values between 0 and 1"
result.assert_passed() # raises when a value falls outside 0 to 1
It reads the point forecast, the quantiles and the samples, whichever the
forecast has. A value equal to a bound passes, and None leaves a side open:
within(0, None) for rainfall or sales. A dictionary from series name to
bound gives each series its own range, and a series it leaves out is open on
that side:
within(lower={"rain_mm": 0, "humidity": 0}, upper={"humidity": 100})
A failed range check names each output and series out of range, how many
values and the most extreme one, such as quantiles: 7 values below 0 (lowest
-3.088). A dictionary that names a series the forecast doesn't have is an
ERROR, so a typo can't switch the check off. Its name in a report is
within; to use two, name them with a dictionary.
timegym.checks.dependence(n_calibration=99, alpha=0.05) asks for the
covariance check below on every case: whether the sample paths of several
series, or of many steps, move together as the answer key says.
from timegym.checks import dependence
result = pipeline.run(["seasonal"], checks=[dependence()])
result.checks # now also "Joint spread matches the answer key"
It needs sample paths and an answer key with a covariance; without them its
finding is UNSUPPORTED. Its row in result.checks is dependence and its
finding in each case is reference_covariance. A report records
n_calibration and alpha under metadata["diagnostic_settings"], so replay
runs the same check. Each case's calibration draws are seeded 100,000 after the
case's own seed, so they never reuse the case's draws; evaluate_forecast
takes that seed as dependence_seed. The old way to ask for it,
check_dependence=True with n_calibration=, alpha= and
dependence_seed_offset= on Pipeline.run, ForecastRun.from_forecasts,
evaluate_forecast and Pipeline.test, still works and warns; given beside
dependence(), it is refused.
A check of your own is a function that asks for its inputs by argument name,
as a metric does (observed, predicted, quantiles, levels, samples,
history), and may also ask for target_names, the names of the series in
order. It returns True when the forecast passes, or a message saying what is
wrong. Its title, shown in report.checks and in the error of
assert_passed, is the first line of its docstring without a final full stop,
or its name when it has no docstring:
import numpy as np
def totals_add_up(predicted, target_names):
"""Store totals add up."""
north, south, total = (predicted[:, target_names.index(name)] for name in ("north", "south", "total"))
gap = float(np.max(np.abs(north + south - total)))
return gap <= 1e-6 or f"the total is off by up to {gap:.3g}"
result = pipeline.run(cases=cases, checks=[within(0, None), totals_add_up])
A check runs only on a forecast that passed the built-in checks. Its finding
is check_<name>: PASS; FAIL with the message, or when the function
returns False; ERROR when it raises or returns anything else, such as
None; or UNSUPPORTED when it needs an output the forecast lacks, and then
it isn't counted for that case. A request is refused before anything runs when
a function has no name of its own (give a lambda one with a dictionary),
takes the name of a built-in check such as runs, is asked for twice, or needs
an argument TimeGym cannot give. Each finding keeps the check's title in its
evidence, so a saved report lists and counts the checks without your code. The
report's metadata lists what was asked for under "checks", as for metrics.
Replay runs within checks again and leaves your own functions out of the
comparison, listing them in metadata["replay"]["checks_not_rerun"]. Without
checks=, reports are exactly as before.
Statistical and metamorphic diagnostics¶
timegym.diagnostics.covariance_dependence_check(reference, samples, *, seed=0,
n_calibration=99, alpha=0.05) tests independent sampling from the specified conditional reference
law. It compares normalized covariance discrepancy with independent ensembles of
the same sample size using a conservative finite Monte Carlo rank p-value. Its
rejection can reflect variance or dependence errors. The threshold is per check;
no multiple-comparison adjustment is silently applied. Too few samples or
insufficient p-value resolution yield INCONCLUSIVE. Fewer than two coordinates
or a deterministic reference yield UNSUPPORTED. Both this check and
evaluate_forecast require explicit nonnegative integer dependence seeds.
Invalid seeds are caller errors and raise before evaluation, including when
the dependence check is not asked for; dependence() refuses invalid
calibration counts and alpha when it is made.
Point, quantile, and sample accuracy diagnostics include their available metrics
in evidence, with reference capabilities when a simulation reference supplies them.
timegym.diagnostics.rank_alarm(observed, calibration, *, alpha=0.05) asks
whether a number is unusually large next to numbers you know are fine. This
check uses it, and so does
comparing a scorecard with a saved baseline.
It ranks the observed value among n calibration values from something
known to be correct, which gives a Monte Carlo p-value, and returns a frozen
RankAlarm with observed, p_value, status, threshold, rank, n and
alpha:
p_value = (1 + number of calibration values >= observed) / (n + 1)
Ties count against the alarm. status is FAIL when p_value <= alpha,
PASS when it is larger, and INCONCLUSIVE when 1 / (n + 1) > alpha,
because then no observed value could fail. threshold is the calibration
value at position rank = ceil((n + 1) * (1 - alpha)) in increasing order,
without interpolation, so observed > threshold exactly when
p_value <= alpha; the rank is computed so that rounding cannot break that
equivalence. When rank exceeds n there is no such value: threshold is
math.inf and the status is INCONCLUSIVE. If the observed value and the
calibration values are exchangeable, for example independent draws of the
same statistic from the same correct source, FAIL happens with probability
at most alpha. Large values are the alarming ones. A calibration array that
is empty, not one-dimensional or not finite, a nonfinite observed value, or an
alpha outside (0, 1) raises ValueError.
from timegym.diagnostics import rank_alarm
result = rank_alarm(observed, calibration, alpha=0.05)
result.p_value, result.status, result.threshold
timegym.checks.check_shift_equivariance(pipeline, history, future_index, *,
shift=10.0, atol=1e-8, rtol=1e-7) tests
f(y+c)=f(y)+c. Use it only when this property is part of the model's intended
contract; many legitimate models are not location equivariant.
Diagnostics catalogue¶
Each row is one diagnostic name in a report, with the statuses it can return,
the tolerance that separates its outcomes and its oracle type. The entries
below the table add its evidence keys, the function that emits it and what a
failure usually means; <name> and <code> stand for parts filled in at run
time. A row without a threshold is descriptive and returns INCONCLUSIVE.
| Diagnostic | Category | Statuses it can return | Tolerance | Oracle type |
|---|---|---|---|---|
forecast_contract |
correctness |
PASS, ERROR |
Exact rules | Partial oracle |
contract_<code> |
correctness |
FAIL |
Exact rules | Partial oracle |
evaluation_data |
evaluation |
ERROR |
Exact rules | None: checks the evaluator's own inputs |
evaluation_reference |
evaluation |
ERROR |
Exact rules | None: checks the evaluator's own inputs |
point_accuracy |
capability |
INCONCLUSIVE, UNSUPPORTED |
None (descriptive) | Simplified data with known results |
quantile_accuracy |
capability |
INCONCLUSIVE, UNSUPPORTED |
None (descriptive) | Simplified data with known results |
sample_scores |
capability |
INCONCLUSIVE, UNSUPPORTED |
None (descriptive) | Simplified data with known results |
metric_<name> |
capability |
INCONCLUSIVE, UNSUPPORTED, ERROR |
None (descriptive) | Simplified data with known results |
check_<name> |
correctness |
PASS, FAIL, UNSUPPORTED, ERROR |
Set by the check, such as the bounds of within |
Partial oracle |
interval_coverage |
uncertainty |
INCONCLUSIVE, UNSUPPORTED |
None (descriptive) | Simplified data with known results |
reference_covariance |
distribution |
PASS, FAIL, INCONCLUSIVE, UNSUPPORTED, ERROR |
alpha, default 0.05 |
Statistical metamorphic test |
shift_equivariance |
metamorphic |
PASS, FAIL, UNSUPPORTED, ERROR |
atol and rtol |
Known properties |
model_execution |
execution |
ERROR |
Any exception | Execution record |
Exception evidence is exception_type, exception_message and traceback.
reference evidence appears only with a
SimulationReference and holds its kind and capability entries. The <code>
in contract_<code> is one of index_type, index_missing, index_nonfinite,
index_empty, index_duplicate, index_order, index_mismatch,
target_names, target_mismatch, invalid_levels, duplicate_levels,
nonmonotonic_levels, missing_quantiles, missing_prediction, point_shape,
quantile_shape, sample_shape, quantile_crossing, or nonreal_ or
nonfinite_ followed by point, quantiles or samples.
Fault fixtures lists the code each output fault fixture
triggers. The study's pseudo-oracle is an experimental comparator, not a package
diagnostic, so it has no row.
forecast_contract¶
- Emitted by:
evaluate_forecast, throughForecast.validate(task) - Evidence keys:
PASS: none.ERROR: exception evidence - Threshold or tolerance: None: exact rules
- What a FAIL or ERROR usually means:
ERROR: validation itself raised, usually on an output object it cannot inspect. A violation is reported ascontract_<code>instead.
contract_<code>¶
- Emitted by:
evaluate_forecast, one row perForecast.validate(task)finding - Evidence keys:
code - Threshold or tolerance: None: exact rules
- What a FAIL or ERROR usually means: The output breaks the named rule, such as a shifted index, swapped targets, a wrong shape, nonfinite values or crossing quantiles. The case is not scored.
evaluation_data¶
- Emitted by:
evaluate_forecast, after the contract holds - Evidence keys: Exception evidence
- Threshold or tolerance: None: exact rules
- What a FAIL or ERROR usually means: The held-out observations do not match the task's shape, index or target order, or are not finite: a case-construction problem, not a model fault.
evaluation_reference¶
- Emitted by:
evaluate_forecast, when the case has a reference - Evidence keys: Exception evidence
- Threshold or tolerance: None: exact rules
- What a FAIL or ERROR usually means: The reference fails its own validation or does not match the task's index and target order: usually a custom case or reference, not a model fault.
point_accuracy¶
- Emitted by:
evaluate_forecast - Evidence keys:
INCONCLUSIVE: the computedmse,mae,maseandavoidable_mse,mase_seasonality,reference.UNSUPPORTED: none - Threshold or tolerance: None: descriptive
- What a FAIL or ERROR usually means: Never
FAILorERROR; a metric that cannot be recorded appears asmetric_<name>.UNSUPPORTED: no point output.
quantile_accuracy¶
- Emitted by:
evaluate_forecast - Evidence keys:
INCONCLUSIVE: the computedpinball,reference.UNSUPPORTED: none - Threshold or tolerance: None: descriptive
- What a FAIL or ERROR usually means: Never
FAILorERROR.UNSUPPORTED: no quantile output.
sample_scores¶
- Emitted by:
evaluate_forecast - Evidence keys:
INCONCLUSIVE: the computedcrps,energy_scoreandvariogram_score,reference.UNSUPPORTED: none - Threshold or tolerance: None: descriptive
- What a FAIL or ERROR usually means: Never
FAILorERROR.UNSUPPORTED: no sample output.
metric_<name>¶
- Emitted by:
evaluate_forecast, only when a metric cannot be recorded;<name>ismse,mae,mase,avoidable_mse,pinball,interval_width,crps,energy_score,variogram_score, or the name of a metric asked for withmetrics= - Evidence keys:
INCONCLUSIVE:reason(undefined_metric).ERROR: exception evidence.UNSUPPORTED:needs, the inputs the requested metric needs - Threshold or tolerance: None: descriptive
- What a FAIL or ERROR usually means:
ERROR: the calculation raised or returned a nonfinite value, such as an overflowing interval width, or a requested metric raised, such asbrieron outcomes that are not 0 or 1; the metric is missing frommetrics.INCONCLUSIVE: the metric is undefined for valid data, such asmaseon a constant target,variogram_scorewith one coordinate orlog_losson a confident mistake withouteps.UNSUPPORTED: a requested metric needs an output the forecast lacks, such as samples.
check_<name>¶
- Emitted by:
evaluate_forecast, for each check asked for withchecks=;<name>is its name, such aswithin - Evidence keys:
title, the check's title.UNSUPPORTED: alsoneeds, the inputs the check needs.ERROR: also exception evidence when the check raised - Threshold or tolerance: Set by the check, such as the bounds of
within - What a FAIL or ERROR usually means:
FAIL: the forecast breaks your rule, such as a value outside the range ofwithin; the message says where.ERROR: the check raised or returned something other than True or a message, orwithinnames a series the forecast doesn't have.UNSUPPORTED: the check needs an output the forecast lacks.
interval_coverage¶
- Emitted by:
evaluate_forecast, with a quantile output - Evidence keys:
INCONCLUSIVE:observed_coverage,nominal_coverage,lower_level,upper_level, andmean_widthwheninterval_widthwas recorded.UNSUPPORTED: none - Threshold or tolerance: None: descriptive; one path's coverage does not establish calibration
- What a FAIL or ERROR usually means: Never
FAILorERROR.UNSUPPORTED: fewer than two quantile levels.
reference_covariance¶
- Emitted by:
covariance_dependence_check;evaluate_forecast,Pipeline.testandPipeline.runwithdependence()(timegym.checks.dependence) inchecks=, where an exception becomes theERROR - Evidence keys:
statistic,statistic_name,p_value,alpha,n_calibration,critical_value,minimum_p_value,n_samples,n_coordinates,seed,null,multiplicity;reference, andcovariance_targetfor a Monte Carlo covariance target. EarlyUNSUPPORTEDandINCONCLUSIVEresults record fewer keys;ERRORrecords exception evidence - Threshold or tolerance:
FAILwhen the Monte Carlo rank p-value is at mostalpha(defaultsalpha=0.05,n_calibration=99).INCONCLUSIVEbelow 20 samples or when1 / (n_calibration + 1)exceedsalpha - What a FAIL or ERROR usually means:
FAIL: the ensemble covariance differs from the reference law, through a variance or dependence error or samples that are not independent draws.ERROR: the comparison raised, for example on invalid reference calibration draws.
shift_equivariance¶
- Emitted by:
check_shift_equivariance, called directly;Pipeline.testdoes not run it - Evidence keys:
PASSandFAIL:shift,max_absolute_error,atol,rtol.ERROR:violationsor exception evidence.UNSUPPORTED: none - Threshold or tolerance:
FAILunlessnp.allclose(shifted, original + shift, atol=atol, rtol=rtol); defaultsshift=10.0,atol=1e-8,rtol=1e-7 - What a FAIL or ERROR usually means:
FAIL: the shifted point forecast is not the original plus the shift, which is a fault only if the model declares location equivariance.ERROR: either forecast broke its contract, or prediction raised.
model_execution¶
- Emitted by:
Pipeline.runandPipeline.test - Evidence keys: Exception evidence
- Threshold or tolerance: None: any exception
- What a FAIL or ERROR usually means: The model or its target transform raised while fitting or predicting, or
predictreturned something other than aForecast; no metric was recorded for that case.
Compare two forecasters on the same cases¶
result.compare(baseline, *, metric, by=None, cluster=None, alpha=0.05,
higher_is_better=False, seed=0) says whether the forecaster behind result,
a run or a report, is better or worse than a baseline on the same cases, or
shows no clear difference. baseline is a run or a report too. Both must come
from the same cases, in the same order: it pairs them by position and refuses two reports whose case
manifests differ, naming the first difference. Name the model where you make
it, with Pipeline(model, name=...), not in the case manifest. metric is
any metric in case.metrics, your own included; lower is better unless
higher_is_better=True.
import numpy as np
import pandas as pd
from timegym import (DriftForecaster, LastValueForecaster, MeanForecaster, Pipeline,
SeasonalLastValueForecaster, cases_from_series)
rng = np.random.default_rng(0)
days = pd.date_range("2024-01-01", periods=140, freq="D")
cases = []
for store in range(16):
weekly = 10 * np.sin(2 * np.pi * np.arange(140) / 7 + store)
sales = pd.Series(40 + weekly + rng.normal(0, 3, 140), index=days, name="sales")
cases += cases_from_series(sales, origins=[84, 98, 112, 126], horizon=7, name="sales",
manifest={"store": f"store {store}", "region": ["north", "south"][store % 2]})
baseline = Pipeline(LastValueForecaster()).run(cases=cases)
candidate = Pipeline(SeasonalLastValueForecaster(period=7)).run(cases=cases)
diff = candidate.compare(baseline, metric="mae", by="region", cluster="store")
print(diff)
diff.assert_no_regression()
mae against the baseline (lower is better; alpha 0.05; cases in one store move together)
region north better 3.52 against 9.15 (-62%) over 32 cases in 8 clusters, p = 0.017
region south better 3.44 against 9.08 (-62%) over 32 cases in 8 clusters, p = 0.017
diff.table has one row per candidate and group: candidate, group (the
by value, or "all cases"), cases scored in both reports and clusters
among them, baseline_mean, candidate_mean, mean_difference (candidate
minus baseline), p_value, verdict, and the cases that are newly_failing
or newly_passing. diff.better, diff.worse and diff.too_few hold those
rows. assert_no_regression() raises when a row is worse or too few to tell.
- Groups.
bynames a manifest key, such as"region"; each value gets a row, and Holm's correction runs across the groups. - Cases that move together.
clusternames a manifest key, such as"store", whose cases share their ups and downs, as cases cut from one series do. All cases of a cluster count as one piece of evidence. Leaving it out when cases do move together makes chance differences look real far more often thanalpha. - Too few to tell. With
kclusters (or cases, withoutcluster) that differ, the smallest possible p-value is2 / 2**k, so with five or fewer nothing can reach 0.05. Such a row says"too few to tell"and failsassert_no_regression(), rather than pass a test it could never fail. With several groups, Holm's correction needs more clusters per group: with two regions of six stores each, the example above would show no clear difference even though every store improved. - Crashes. A case the baseline scored that crashes or fails an output
check in the candidate makes its row
"worse". A case the candidate scores and the baseline did not is counted innewly_passingand left out of the test. - Roundoff. Two values of a metric for one case that differ by no more than
1e-9, or1e-8times the largest value in the row, count as the same, as in a scorecard's comparison. Two reports with the same values show no clear difference, with a p-value of 1.
Many candidates against one baseline¶
timegym.compare_reports(candidates, baseline, *, metric, ...) takes the
same settings and compares several candidates with one baseline at once; with
one candidate it is candidate.compare(baseline, ...). Pass it a list of
reports, each labelled by the name its pipeline was given or, without one, by
its model; a dictionary of reports keyed by name works too. Each candidate's
p-value is adjusted for how many were tried, so the best of many does not
beat the baseline by luck.
from timegym import compare_reports
candidates = [
Pipeline(SeasonalLastValueForecaster(period=7), name="same weekday last week").run(cases=cases),
Pipeline(MeanForecaster(), name="mean so far").run(cases=cases),
Pipeline(DriftForecaster(), name="straight line").run(cases=cases),
]
print(compare_reports(candidates, baseline, metric="mae", cluster="store"))
mae against the baseline (lower is better; alpha 0.05; cases in one store move together)
same weekday last week better 3.48 against 9.11 (-62%) over 64 cases in 16 clusters, p = 0.0001
mean so far better 6.54 against 9.11 (-28%) over 64 cases in 16 clusters, p = 0.0004
straight line no clear difference 9.2 against 9.11 (+0.91%) over 64 cases in 16 clusters, p = 0.074
How the test works¶
For each pair of values, the difference is candidate minus baseline. If the
two forecasters were alike, each difference, or each cluster's sum of
differences, would be as likely to be positive as negative. The test flips
their signs at random 9,999 times, seeded from seed and the number of
clusters (or cases), and the p-value is the share of sign patterns, the observed one
included, whose mean difference is at least as far from zero as the observed
one. It is two-sided, so the verdict can go either way. With cluster, all
cases of a cluster flip together (Canay, Romano and Shaikh
2017). With several candidates, one set
of sign flips is applied to every candidate and group. Each row's statistic is
divided by the square root of its sum of squared differences, which no flip
changes, so a group with larger errors does not dominate. Each row's p-value
is then the share of sign patterns whose largest statistic, over all rows, is
at least that row's observed one (White
2000; Romano and Wolf
2005). Rows where too few
clusters differ are left out of both corrections.
The test assumes that different clusters are independent and that, when the two forecasters are alike, a cluster's difference is as likely to be positive as negative. It says how these two forecasters did on these cases; whether the difference holds on other cases depends on whether these are a fair sample of them. A scorecard's comparison with a saved baseline uses the same sign flips and Holm's correction, one-sided, because it asks only whether the new version is worse.
Are the chances honest?¶
When a forecast of a yes/no outcome says 70%, it should happen about 70% of
the time. result.calibration(*, bins=10, cluster=None, interval_level=0.95,
seed=0) checks that on a run of such forecasts (made with keep_data=True,
since it reads every forecast and what happened). The function
timegym.diagnostics.calibration does the same on a run,
calibration(result, ...), and on arrays of outcomes (0 or 1) and chances,
calibration(observed, predicted, ...). level=, the old name of
interval_level=, still works and warns, and so does Calibration.level.
It returns a Calibration:
table: one row per bin of equal width, fromlowtohigh, with the number offorecastsin it, theirmean_forecastand the share thathappened. A calibrated forecaster's last two columns agree.slope, from a logistic regression of what happened on the log-odds of the forecast (Cox 1958): 1 is calibrated; below 1 the forecasts swing further from the average than what happened supports (too confident), above 1 not far enough (too timid).slope_intervalis its bootstrap interval atinterval_level.intercept, from the same regression with the slope held at 1: 0 is right on average, below 0 the forecasts are too high, above 0 too low (Van Calster et al. 2019).plot(): the table as a reliability diagram (Murphy and Winkler 1977), with the number of forecasts in each bin below it.
Here twenty weather stations record rain (1) or none (0), with a chance that rises and falls over a 12-step cycle, and five cases are cut from each. The forecaster gives the share of rainy steps so far at the same point of the cycle, after adding one rainy and one dry step to each count:
import numpy as np
import pandas as pd
from timegym import Pipeline, cases_from_series
rng = np.random.default_rng(5)
chance = 0.5 + 0.37 * np.sin(2 * np.pi * np.arange(240) / 12) # rises and falls over 12 steps
cases = []
for station in range(20):
rain = pd.Series((rng.random(240) < chance).astype(float), name="rain")
cases += cases_from_series(rain, origins=[120, 144, 168, 192, 216], horizon=24, name="rain",
manifest={"station": station})
class ShareAtThisPointOfTheCycle:
"""The share of rainy steps so far at this point of the cycle, after adding one rainy and one dry step."""
def fit(self, history):
values = history.to_numpy()[:, 0]
self.length_ = len(values)
self.share_ = np.array([(values[phase::12].sum() + 1) / (len(values[phase::12]) + 2)
for phase in range(12)])
return self
def predict(self, future_index):
return self.share_[np.arange(self.length_, self.length_ + len(future_index)) % 12]
result = Pipeline(ShareAtThisPointOfTheCycle()).run(cases=cases, keep_data=True)
cal = result.calibration(cluster="station")
print(cal)
Calibration of 2,400 forecasts from 100 cases in 20 clusters by station
slope 0.92 (95% interval 0.86 to 1.01): 1 is calibrated, below 1 too confident, above 1 too timid
intercept +0.00: 0 is right on average, below 0 too high, above 0 too low
forecast mean happened forecasts
0.00 to 0.10 0.07 0.20 60
0.10 to 0.20 0.15 0.16 260
0.20 to 0.30 0.25 0.25 274
0.30 to 0.40 0.34 0.32 272
0.40 to 0.50 0.43 0.41 182
0.50 to 0.60 0.53 0.53 370
0.60 to 0.70 0.65 0.68 294
0.70 to 0.80 0.74 0.75 314
0.80 to 0.90 0.83 0.79 288
0.90 to 1.00 0.93 0.93 86
The slope is 0.92 and its interval reaches 1.01, so this forecaster is close
to calibrated, perhaps a little too sure of itself. The five cases of a
station share its history, so cluster="station" resamples them together.
Which forecasts it reads. From a run, the point forecast is the chance, or
the mean of the sample paths when there is no point forecast. Cases that
crashed, failed a built-in check, or have neither are left out and counted in
skipped. Each case is resampled whole for the interval, since its steps
share one history; cluster= names a manifest key whose cases are resampled
together. With arrays, each value is resampled on its own, or cluster gives
one label per value.
Forecasts of exactly 0 or 1. Their log-odds are infinite, so the slope and
the intercept read them as 0.000001 and 0.999999, and certain counts them. A
sure forecast that turns out wrong then pulls the slope far toward 0. Without
the added steps, the forecaster above says 0 or 1 exactly 130 times and its
slope drops to 0.46. That is the right direction, since a sure forecast that
fails was too sure, but the size of the drop depends on how 0 and 1 are read:
read the table beside the slope.
When there is no number. With every outcome the same there is no slope or
intercept; with every forecast the same, no slope; when the forecasts separate
the outcomes perfectly, the slope has no finite value. note says which, and
the value is NaN. A slope near 0 next to forecasts that stay near the average
means they carry no information, not that they are too sure: the share of rainy
steps so far, MeanForecaster(), forecasts between 40% and 60% on these
cases and has a slope of −0.14.
Case-level uncertainty intervals¶
timegym.statistics.wilson_interval(values) accepts a nonempty vector of binary
observations and returns count, n, rate, and the Wilson ci95 bounds.
timegym.statistics.paired_t_interval(values) accepts at least two finite
case-level differences and returns mean, n, and Student t ci95 bounds.
Cases must be independent; horizons and targets within a case are not replications.
The t interval is for continuous differences in a metric, not differences in alarm rates.
Signatures¶
timegym.metrics.mase
¶
mase(observed, predicted, history, seasonality=1)
MASE, scaling each target by its seasonal naive MAE over the history.
Time is axis zero. A constant target or insufficient history makes the
aggregate undefined and raises UndefinedMetricError; no epsilon
denominator or omission of constant targets is silently introduced.
The old keyword context= still works and warns.
timegym.metrics.rmse
¶
rmse(observed, predicted)
Root mean squared error over observation coordinates, in the data's units.
timegym.metrics.rmsse
¶
rmsse(observed, predicted, history, seasonality=1)
RMSSE: each series' RMSE over its seasonal naive RMSE in the history, averaged over series.
Time is axis zero. Each series is scaled by the root mean squared change
over seasonality steps in its history. A constant series or too short
a history makes the score undefined and raises UndefinedMetricError,
as mase does. The old keyword context= still works and warns.
timegym.metrics.mean_reference_mse
¶
mean_reference_mse(reference_mean, predicted)
Squared distance from a supplied conditional mean, not generic regret.
With a valid conditional mean this is the conditional excess squared-error risk, averaged over coordinates. It is not realised loss against a future observation and is not a reference for other losses.
timegym.metrics.pinball
¶
pinball(observed, quantiles, levels)
Mean quantile (pinball) loss, without the optional factor of two.
timegym.metrics.interval_score
¶
interval_score(observed, quantiles, levels)
Mean interval score of the band from the lowest to the highest level.
For a central band holding 1 - alpha of the outcomes, the score is its
width plus 2 / alpha times the distance by which an outcome falls
outside it. The lowest and highest levels must be symmetric around 0.5.
timegym.metrics.weighted_interval_score
¶
weighted_interval_score(observed, quantiles, levels)
Mean weighted interval score over 0.5 and pairs of levels symmetric around it.
With K pairs, the score is the median's absolute error times 1/2 plus each pair's interval score times alpha/2, all divided by K + 1/2 (Bracher et al. 2021). That equals twice the mean pinball loss over the levels.
timegym.metrics.crps
¶
crps(observed, samples)
Mean marginal CRPS of the finite empirical ensemble distribution.
Computes E|X-y| - E|X-X'|/2 with replacement, including diagonal pairs. The sorted formula is O(S log S) per coordinate, not O(S squared). This is the empirical-distribution score, not the fair-ensemble estimator.
timegym.metrics.energy_score
¶
energy_score(observed, samples, chunk_size=128)
Energy score of the entire horizon-target vector using Euclidean norm.
Pairwise distances are evaluated in chunks to bound memory. Like crps,
this scores the empirical distribution and includes diagonal sample pairs.
The vector norm is not divided by the number of coordinates.
timegym.metrics.variogram_score
¶
variogram_score(observed, samples, exponent=0.5)
Mean squared variogram discrepancy across unordered coordinate pairs.
Horizon and target axes are flattened together. All distinct pairs receive equal weight and the sum is divided by their count. This explicitly fixes the normalization, which otherwise varies between score implementations.
timegym.metrics.brier
¶
brier(observed, predicted)
Brier score: the mean squared distance between a probability and the 0/1 outcome.
timegym.metrics.log_loss
¶
log_loss(observed, predicted, *, eps=None)
Mean of minus the natural log of the probability given to what happened.
A forecast of exactly 0 or 1 that turned out wrong makes the score
infinite, so it raises UndefinedMetricError unless eps is given;
eps (between 0 and 0.5) first clips every forecast to
[eps, 1 - eps]. Nothing is clipped without being asked.
timegym.metrics.UndefinedMetricError
¶
A metric is undefined for the supplied, otherwise valid, data.
timegym.diagnostics.evaluate_forecast
¶
evaluate_forecast(case, forecast, *, dependence_seed=0, metrics=None, checks=None, **old_arguments)
Validate a forecast and record metrics without inventing pass thresholds.
Malformed forecasts are rejected before scoring. Missing point, quantile,
or ensemble outputs are UNSUPPORTED, not reconstructed from other output
types. A known-mean discrepancy and one-path interval coverage are
descriptive evidence and are INCONCLUSIVE as capability decisions.
The case is labelled by manifest["kind"] and manifest["seed"]. A
case not drawn from a TimeGym scenario (its manifest has no
schema_version) may leave them out: it is then labelled "case"
with no seed.
metrics records more metrics in result.metrics: names of functions
in timegym.metrics, your own functions, or a dictionary from name to
function. A metric asks for observed, predicted, quantiles,
levels, samples or history by its argument names (one that
asks for context, the old name of history, gets the same values
and warns). One whose input the forecast lacks is UNSUPPORTED,
UndefinedMetricError makes it INCONCLUSIVE and any other exception an
ERROR, each as metric_<name>.
checks adds output checks of your own: a list of functions, such as
timegym.checks.within(0, None), or a dictionary from name to
function. A check asks for the same inputs as a metric, plus
target_names, and returns True or a message saying what is wrong. Its
finding is check_<name>: PASS, FAIL with the message, ERROR when it
raises or returns anything else, or UNSUPPORTED when the forecast lacks
an input it needs. The finding's evidence keeps the check's title, the
first line of its docstring. Checks run only on a forecast that passed
the built-in checks. scores, the old name of metrics, still
works and warns.
timegym.checks.dependence() among the checks runs the dependence
check, reference_covariance: whether the sample paths move together
as the answer key says. Its calibration draws are seeded with
dependence_seed. check_dependence, n_calibration and
alpha, its old form, still work and warn.
timegym.compare_reports
¶
compare_reports(candidate, baseline, *, metric, by=None, cluster=None, alpha=0.05, higher_is_better=False, seed=0)
Compare forecasters on the same cases, one metric at a time; returns a ReportComparison.
candidate and baseline are reports made on the same cases, in the
same order: pipeline.run(...) results or their reports, whose case manifests
agree. candidate may also be a list of reports, each labelled by its
name (Pipeline(..., name=...)) or, without one, by its model, or a
dictionary of named reports.
metric is a metric in case.metrics, your own included; lower is
better unless higher_is_better=True. "mean_reference_mse", the
old name of "avoidable_mse", still works and warns.
Each row says whether the candidate is "better", "worse" or shows
"no clear difference", from a two-sided paired sign-flip test on the
mean difference over the cases both reports scored. by names a
manifest key and gives one row per value, such as by="region".
cluster names a manifest key whose cases move together, such as
cluster="store" for cases cut from one store's series: their signs flip
together, so they count as one piece of evidence. A row is "too few to
tell" when too few cases, or clusters, differ for any difference to
reach alpha: six at the default 0.05.
A case the baseline scored that crashes or fails an output check in the
candidate makes its row worse. With several groups, p-values are adjusted
with Holm's correction; with several candidates, each candidate's p-value
counts how often the most extreme of all candidates and groups, under the
same sign flips, is at least as extreme, so the best of many does not beat
the baseline by luck. seed fixes the random sign flips.
With one candidate, candidate.compare(baseline, metric=...) on a run
or a report does the same.
timegym.diagnostics.ReportComparison
dataclass
¶
ReportComparison(table: DataFrame, metric: str, higher_is_better: bool, by: str | None, cluster: str | None, alpha: float, seed: int, units_needed: int)
Candidates compared with a baseline on the same cases, one row per candidate and group.
table has the candidate's label, the group (the by value,
or "all cases"), the number of cases both reports scored and of
clusters among them, the baseline_mean and candidate_mean of
the metric over those cases and their mean_difference (candidate minus
baseline), the p_value adjusted for every group and candidate tried,
the verdict ("better", "worse", "no clear difference" or
"too few to tell"), and the cases that newly_failing crash or fail
an output check in the candidate after the baseline scored them, and that
are newly_passing. better and worse hold those rows, and
assert_no_regression() raises when a row is worse or too few to tell.
The statistics: a two-sided paired sign-flip test on the mean difference,
with 9,999 random sign patterns and the observed one; with cluster,
one sign per cluster. It assumes that, when two forecasters are alike,
each case's difference, or each cluster's, is as likely to be positive
as negative, and that different clusters are independent. Holm's
correction runs across the groups of one candidate. With several
candidates every row's statistic is divided by its spread and compared
with the largest of all rows under the same flips, which keeps the chance
of any false verdict near alpha however alike the candidates are.
Rows where too few cases or clusters differ are left out of the
correction, and rows with the same values in both reports get a p-value
of 1.
timegym.diagnostics.calibration
¶
calibration(run, predicted=None, *, bins=10, cluster=None, interval_level=0.95, seed=0)
How often yes/no forecasts came true, by forecast, and the calibration slope; returns a Calibration.
calibration(run), or run.calibration(), reads a run's forecasts
of 0/1 outcomes, such as rain or no rain: the point forecast is the chance, or the mean of the
sample paths when there is none. Cases that crashed or failed a built-in
check are left out and counted. calibration(observed, predicted)
reads arrays of outcomes (0 or 1) and chances (between 0 and 1) instead.
The table splits the chances into bins of equal width and gives,
for each, how many forecasts fell in it, their mean and the share that
happened. The slope comes from a logistic regression of what happened on
the log-odds of the forecast: 1 means calibrated; below 1 the forecasts
swing further from the average than what happened supports (too
confident), above 1 not far enough (too timid).
The intercept comes from the same regression with the slope held at 1:
0 means right on average, below 0 forecasts too high, above 0 too low.
A forecast of exactly 0 or 1 counts as 0.000001 or 0.999999 in both.
The slope's interval, at interval_level, comes from 2,000 bootstrap resamples
with seed. With a run each case is resampled whole, since its steps
move together; cluster names a manifest key whose cases are
resampled together, such as cluster="series". With arrays each value
is resampled on its own, or cluster gives one label per value.
level, the old name of interval_level, still works and warns.
timegym.diagnostics.Calibration
dataclass
¶
Calibration(table: DataFrame, slope: float, slope_interval: tuple, intercept: float, interval_level: float, forecasts: int, cases: int | None, clusters: int | None, cluster: str | None, skipped: int, certain: int, seed: int, note: str | None = None)
How often yes/no forecasts came true, by forecast, with the calibration slope and intercept.
table has one row per bin of chances: its low and high
ends, the number of forecasts in it, their mean_forecast and the
share that happened; a calibrated forecaster's two columns agree.
slope is 1 when calibrated, below 1 when too confident and above 1
when too timid, and slope_interval is its bootstrap interval at
interval_level. intercept is 0 when the forecasts are right on average,
below 0 when they are too high and above 0 when too low. forecasts
counts the values read, cases the cases of a run they came from
(None for arrays), clusters the clusters resampled, skipped the
cases left out, and certain the forecasts of exactly 0 or 1, which
weigh heavily on the slope when wrong. note says why a number is
NaN. plot() draws the table as a reliability diagram. level is
the old name of interval_level; it still works and warns.
timegym.plotting.plot_calibration
¶
plot_calibration(calibration, *, figsize=None)
Draw a reliability diagram: how often yes/no forecasts came true against what they said.
calibration is a timegym.diagnostics.Calibration. The top panel
puts each bin's mean forecast against the share that happened, with the
diagonal a calibrated forecaster follows; the bottom panel counts the
forecasts in each bin. The title gives the slope and its interval.
timegym.diagnostics.covariance_dependence_check
¶
covariance_dependence_check(reference, samples, *, seed=0, n_calibration=99, alpha=0.05)
Monte Carlo check of an ensemble's covariance against a known law.
Flatten horizon and target axes in C order. The discrepancy is the
Frobenius distance between unbiased sample covariance and reference
covariance, divided by the latter's Frobenius norm. Calibration ensembles
have the same sample count as the forecast. The rank p-value, from
rank_alarm, is (1 + # calibration statistics >= observed) / (B + 1);
ties are conservative. This tests covariance consistency under iid draws from the
specified reference, not general calibration or a causal fault mechanism.
Variance errors can also trigger this check. Alpha is per check, without
automatic adjustment across multiple cases. A numerical covariance target
remains fixed across the observed and null statistics; its Monte Carlo
stream must be independent of every ensemble used in rank calibration.
timegym.diagnostics.rank_alarm
¶
rank_alarm(observed, calibration, *, alpha=0.05)
Rank observed among calibration values from a correct source; returns a RankAlarm.
It asks whether a number is unusually large next to numbers you know are
fine: "FAIL" means that few of them are as large as it.
The p-value is (1 + number of calibration values >= observed) / (n + 1)
for n calibration values, so ties are conservative, and the status is
"FAIL" when it is at most alpha. If observed and the
calibration values are exchangeable, for example independent draws of the
same statistic from the same correct source, a FAIL happens with
probability at most alpha. The rule assumes nothing else about their
distribution. Large values are the alarming ones; negate
a statistic for which small values are.
calibration must be a finite, one-dimensional, nonempty array,
observed a finite number and alpha a number in (0, 1); anything
else raises ValueError.
timegym.diagnostics.RankAlarm
dataclass
¶
RankAlarm(observed: float, p_value: float, status: str, threshold: float, rank: int, n: int, alpha: float)
The rank rule applied to one observed statistic; rank_alarm returns it.
p_value is (1 + number of calibration values >= observed) / (n + 1).
status is "FAIL" when p_value <= alpha, "PASS" when it is
larger, and "INCONCLUSIVE" when 1 / (n + 1) > alpha, because then
no observed value could fail. threshold is the calibration value at
position rank in increasing order (counted from 1), with no
interpolation: observed > threshold exactly when p_value <= alpha.
rank is ceil((n + 1) * (1 - alpha)), computed so that rounding
cannot break that equivalence. When it exceeds n there is no such
calibration value, threshold is math.inf and the status
is "INCONCLUSIVE".
timegym.checks.check_shift_equivariance
¶
check_shift_equivariance(pipeline, history, future_index, *, shift=10.0, atol=1e-08, rtol=1e-07)
Check f(y+c)=f(y)+c for a model declaring location equivariance.
The pipeline forecasts future_index from history and from the
history shifted by shift. This property is not universal:
regularization, clipping or absolute features may intentionally violate
it. Callers choose whether it applies to their model. The old keyword
context= still works and warns.
timegym.checks.within
¶
within(lower=None, upper=None)
Check that every forecast value lies in a range; pass it in checks=.
lower and upper are numbers, or None to leave that side open:
within(0, None) for rainfall or sales, within(0, 1) for a chance.
A value equal to a bound passes. A dictionary from series name to bound
gives each series its own, such as
within(lower={"rain_mm": 0, "humidity": 0}, upper={"humidity": 100});
a series the dictionary leaves out is open on that side, and a series the
forecast doesn't have makes the check an ERROR. The check reads the point
forecast, the quantiles and the samples, whichever the forecast has, and
fails with a message naming each output (and series) out of range, how
many values and the most extreme one. Its name in a report is
within and its title says the range, such as "Values between 0 and
1"; to use two, name them with a dictionary. Replay runs it again.
timegym.checks.dependence
¶
dependence(n_calibration=99, alpha=0.05)
Check that sample paths move together as the answer key says; pass it in checks=.
pipeline.run(scenarios, checks=[dependence()]) runs
timegym.diagnostics.covariance_dependence_check on every case: it
compares the covariance of the forecast's sample paths, over every step
and series, with the answer key's, against n_calibration sets of
samples drawn from the answer key itself, and fails when its p-value is
at most alpha. A case without sample paths, or without an answer key
with a covariance, gets UNSUPPORTED. The finding in each case is
reference_covariance and its row in result.checks is
"dependence". A report records n_calibration and alpha under
metadata["diagnostic_settings"], and replay runs the check again.
Each case's calibration draws are seeded 100,000 after the case's own
seed; evaluate_forecast takes the seed as dependence_seed. It
replaces check_dependence=True, n_calibration= and alpha=,
which still work and warn.
timegym.statistics.wilson_interval
¶
wilson_interval(values: Sequence[bool]) -> dict
Wilson 95% interval across independent binary observations.
timegym.statistics.paired_t_interval
¶
paired_t_interval(values: Sequence[float]) -> dict
Student t 95% interval for the mean of independent paired differences.
Supply one difference per independent case, not dependent coordinates. Constant differences yield a zero-width interval; this is not a binary proportion-difference interval.