Skip to content

Commit

Permalink
[ENH] add test parameters to probabilistic metrics, docs improvement (s…
Browse files Browse the repository at this point in the history
…ktime#234)

This PR adds test parameters to the probabilistic metrics.

Also improves formatting of the docstrings.
  • Loading branch information
fkiraly authored Apr 5, 2024
1 parent bf8b74e commit 9a27d45
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 15 deletions.
49 changes: 35 additions & 14 deletions skpro/metrics/_classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,9 +236,9 @@ class LogLoss(BaseDistrMetric):
and a ground truth value :math:`y`, the logarithmic loss is
defined as :math:`L(y, d) := -\log p_d(y)`.
`evaluate` computes the average test sample loss.
`evaluate_by_index` produces the loss sample by test data point
`multivariate` controls averaging over variables.
* ``evaluate`` computes the average test sample loss.
* ``evaluate_by_index`` produces the loss sample by test data point.
* ``multivariate`` controls averaging over variables.
Parameters
----------
Expand Down Expand Up @@ -268,6 +268,13 @@ def _evaluate_by_index(self, y_true, y_pred, **kwargs):
else:
return res

@classmethod
def get_test_params(cls, parameter_set="default"):
"""Retrieve test parameters."""
params1 = {}
params2 = {"multivariate": True}
return [params1, params2]


class LinearizedLogLoss(BaseDistrMetric):
r"""Lineararized logarithmic loss for distributional predictions.
Expand All @@ -278,9 +285,9 @@ class LinearizedLogLoss(BaseDistrMetric):
and :math:`L(y, d) := -\log p_d(r) + 1 - \frac{1}{r} p_d(r)` otherwise,
where :math:`r` is the range of linearization parameter, `range` below.
`evaluate` computes the average test sample loss.
`evaluate_by_index` produces the loss sample by test data point
`multivariate` controls averaging over variables.
* ``evaluate`` computes the average test sample loss.
* ``evaluate_by_index`` produces the loss sample by test data point.
* ``multivariate`` controls averaging over variables.
Parameters
----------
Expand Down Expand Up @@ -347,9 +354,9 @@ class SquaredDistrLoss(BaseDistrMetric):
defined as :math:`L(y, d) := -2 p_d(y) + \|p_d\|^2`,
where :math:`\|p_d\|^2` is the (function) L2-norm of :math:`p_d`.
`evaluate` computes the average test sample loss.
`evaluate_by_index` produces the loss sample by test data point
`multivariate` controls averaging over variables.
* ``evaluate`` computes the average test sample loss.
* ``evaluate_by_index`` produces the loss sample by test data point.
* ``multivariate`` controls averaging over variables.
Parameters
----------
Expand Down Expand Up @@ -379,6 +386,13 @@ def _evaluate_by_index(self, y_true, y_pred, **kwargs):
else:
return res

@classmethod
def get_test_params(cls, parameter_set="default"):
"""Retrieve test parameters."""
params1 = {}
params2 = {"multivariate": True}
return [params1, params2]


class CRPS(BaseDistrMetric):
r"""Continuous rank probability score for distributional predictions.
Expand All @@ -391,11 +405,11 @@ class CRPS(BaseDistrMetric):
For a predictive distribution :math:`d` and a ground truth value :math:`y`,
the CRPS is defined as
:math:`L(y, d) := \mathbb{E}_{Y \sim d}|Y-y| - \frac{1}{2} \mathbb{E}_{X,Y \sim d}|X-Y|`. # noqa: E501
:math:`L(y, d) := \mathbb{E}_{Y \sim d}|Y-y| - \frac{1}{2} \mathbb{E}_{X,Y \sim d}|X-Y|`.
`evaluate` computes the average test sample loss.
`evaluate_by_index` produces the loss sample by test data point
`multivariate` controls averaging over variables.
* ``evaluate`` computes the average test sample loss.
* ``evaluate_by_index`` produces the loss sample by test data point.
* ``multivariate`` controls averaging over variables.
Parameters
----------
Expand All @@ -410,7 +424,7 @@ class CRPS(BaseDistrMetric):
CRPS is computed for entire row, results one score per row
if False, is univariate log-loss, per variable
CRPS is computed per variable marginal, results in many scores per row
"""
""" # noqa: E501

def __init__(self, multioutput="uniform_average", multivariate=False):
self.multivariate = multivariate
Expand All @@ -419,3 +433,10 @@ def __init__(self, multioutput="uniform_average", multivariate=False):
def _evaluate_by_index(self, y_true, y_pred, **kwargs):
# CRPS(d, y) = E_X,Y as d [abs(Y-y) - 0.5 abs(X-Y)]
return y_pred.energy(y_true) - y_pred.energy() / 2

@classmethod
def get_test_params(cls, parameter_set="default"):
"""Retrieve test parameters."""
params1 = {}
params2 = {"multivariate": True}
return [params1, params2]
2 changes: 1 addition & 1 deletion skpro/metrics/tests/test_distr_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@


class TestAllDistrMetrics(PackageConfig, BaseFixtureGenerator, QuickTester):
"""Generic tests for all regressors in the mini package."""
"""Generic tests for all probabilistic regression metrics in the package."""

# class variables which can be overridden by descendants
# ------------------------------------------------------
Expand Down

0 comments on commit 9a27d45

Please sign in to comment.