GammaResidual¶
yohou.metrics.GammaResidual
¶
Bases: BaseConformityScorer
Gamma residual scorer using relative prediction errors.
Computes conformity scores as the signed relative error, normalised by the predicted value:
This scorer is useful when the scale of the target variable varies
over time, because the conformity scores are relative to the prediction
magnitude. The epsilon parameter prevents division by zero when
predictions are near zero.
Parameters ¶
| Name | Type | Description | Default |
|---|---|---|---|
epsilon
|
float
|
Small constant added to the denominator to prevent division by zero. |
1e-8
|
See Also ¶
AbsoluteGammaResidual: Symmetric variant using absolute relative errors.Residual: Scale-dependent signed residual scorer.
Examples ¶
>>> import polars as pl
>>> from datetime import date
>>> from yohou.metrics.conformity import GammaResidual
>>> scorer = GammaResidual(epsilon=1e-8).fit(
... pl.DataFrame({"time": [date(2020, 1, 1), date(2020, 1, 2)], "y": [1.0, 2.0]})
... )
>>> y_truth = pl.DataFrame({"time": [date(2020, 1, 3)], "y": [10.0]})
>>> y_pred = pl.DataFrame({"time": [date(2020, 1, 3)], "y": [8.0]})
>>> scores = scorer.score(y_truth, y_pred)
>>> round(scores.drop("time").to_series().item(), 4)
0.25
Source Code ¶
Source code in src/yohou/metrics/conformity.py
290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 | |
Methods ¶
score(y_truth, y_pred, /, **score_params)
¶
Compute gamma (relative) residual conformity scores.
Parameters ¶
| Name | Type | Description | Default |
|---|---|---|---|
y_truth
|
DataFrame
|
True target values with "time" column. |
required |
y_pred
|
DataFrame
|
Predicted values with "time" column. |
required |
Returns ¶
| Type | Description |
|---|---|
DataFrame
|
Relative conformity scores (y_truth - y_pred) / (y_pred + epsilon) with "time" column preserved. |
Source Code ¶
Source code in src/yohou/metrics/conformity.py
inverse_score(y_pred, conformity_scores, coverage_rate)
¶
Construct prediction intervals from gamma conformity scores.
Parameters ¶
| Name | Type | Description | Default |
|---|---|---|---|
y_pred
|
DataFrame
|
Point predictions, optionally with "time" column. |
required |
conformity_scores
|
DataFrame
|
Computed conformity scores from calibration set, optionally with "time" column. |
required |
coverage_rate
|
float
|
Desired coverage probability (e.g., 0.9 for 90% intervals). |
required |
Returns ¶
| Type | Description |
|---|---|
DataFrame
|
Prediction intervals with lower and upper bounds, and time columns if input had them. |
Source Code ¶
Source code in src/yohou/metrics/conformity.py
Tutorials¶
The following example notebooks use this component:
-
Conformal Prediction Intervals
Build distribution-free prediction intervals with SplitConformalForecaster using calibration holdouts and configurable conformity scoring functions.
-
How to Handle Outliers in a Forecasting Pipeline
Detect and clip outliers with OutlierThresholdHandler and OutlierPercentileHandler, then see how outliers affect conformal prediction intervals.
-
How to Use Conformity Scorers
Compare Residual, AbsoluteResidual, GammaResidual, and AbsoluteGammaResidual conformity scorers with coverage/width analysis and DistanceSimilarity interaction.