2626
2727
2828class R2Metric (CumulativeIterationMetric ):
29- r"""Computes :math:`R^{2}` score (coefficient of determination):
29+ r"""Computes :math:`R^{2}` score (coefficient of determination). :math:`R^{2}` is used to evaluate
30+ a regression model. In the best case, when the predictions match exactly the observed values, :math:`R^{2} = 1`.
31+ It has no lower bound, and the more negative it is, the worse the model is. Finally, a baseline model, which always
32+ predicts the mean of observed values, will get :math:`R^{2} = 0`.
3033
3134 .. math::
3235 \operatorname {R^{2}}\left(Y, \hat{Y}\right) = 1 - \frac {\sum _{i=1}^{n}\left(y_i-\hat{y_i} \right)^{2}}
3336 {\sum _{i=1}^{n}\left(y_i-\bar{y} \right)^{2}},
37+ :label: r2
3438
35- where :math:`\bar{y}` is the mean of observed :math:`y` ; or adjusted :math:`R^{2}` score:
39+ where :math:`\bar{y}` is the mean of observed :math:`y`.
40+
41+ However, :math:`R^{2}` automatically increases when extra
42+ variables are added to the model. To account for this phenomenon and penalize the addition of unnecessary variables,
43+ :math:`adjusted \ R^{2}` (:math:`\bar{R}^{2}`) is defined:
3644
3745 .. math::
3846 \operatorname {\bar{R}^{2}} = 1 - (1-R^{2}) \frac {n-1}{n-p-1},
47+ :label: r2_adjusted
3948
4049 where :math:`p` is the number of independant variables used for the regression.
4150
@@ -57,7 +66,7 @@ class R2Metric(CumulativeIterationMetric):
5766 - ``"variance_weighted"``: the scores of all outputs are averaged, weighted by the variances of
5867 each individual output.
5968 p: non-negative integer.
60- Number of independent variables used for regression. ``p`` is used to compute adjusted :math:`R ^{2}` score.
69+ Number of independent variables used for regression. ``p`` is used to compute :math:`\bar{R} ^{2}` score.
6170 Defaults to 0 (standard :math:`R^{2}` score).
6271
6372 """
@@ -121,7 +130,8 @@ def _calculate(y_pred: np.ndarray, y: np.ndarray, p: int) -> float:
121130def compute_r2_score (
122131 y_pred : torch .Tensor , y : torch .Tensor , multi_output : MultiOutput | str = MultiOutput .UNIFORM , p : int = 0
123132) -> np .ndarray | float | npt .ArrayLike :
124- """Computes :math:`R^{2}` score (coefficient of determination).
133+ """Computes :math:`R^{2}` score (coefficient of determination). :math:`R^{2}` is used to evaluate
134+ a regression model according to equations :eq:`r2` and :eq:`r2_adjusted`.
125135
126136 Args:
127137 y_pred: input data to compute :math:`R^{2}` score, the first dim must be batch.
@@ -137,7 +147,7 @@ def compute_r2_score(
137147 - ``"variance_weighted"``: the scores of all outputs are averaged, weighted by the variances
138148 each individual output.
139149 p: non-negative integer.
140- Number of independent variables used for regression. ``p`` is used to compute adjusted :math:`R ^{2}` score.
150+ Number of independent variables used for regression. ``p`` is used to compute :math:`\b ar{R} ^{2}` score.
141151 Defaults to 0 (standard :math:`R^{2}` score).
142152
143153 Raises:
0 commit comments