prfmodel.utils.calculate_r_squared

prfmodel.utils.calculate_r_squared(observed: numpy.ndarray | prfmodel.typing.Tensor, predicted: numpy.ndarray | prfmodel.typing.Tensor) → numpy.ndarray

Calculate the coefficient of determination (R-squared) of predicted responses for each unit.

The R-squared is the proportion of variance in the observed response over time that is explained by the predicted response.

Parameters:
Returns:

R-squared of each unit with shape (num_units,).

Return type:

numpy.ndarray

Raises:

ValueError – If observed and predicted do not have the same shape.

Notes

The R-squared is at most one and can be negative when the prediction is worse than a flat line at the mean of the observed response (e.g., when evaluating predictions out-of-sample). It is NaN for units with a constant observed response, for which the explained variance is undefined. It is calculated as:

\[R^2 = 1 - \frac{\sum_t (y_t - \hat{y}_t)^2}{\sum_t (y_t - \bar{y})^2}\]

where \(y_t\) is the observed and \(\hat{y}_t\) the predicted response at time frame \(t\), and \(\bar{y}\) is the mean of the observed response.

The score is computed over the last axis, so observed and predicted can also have shape (num_frames,) for a single unit.

Examples

>>> import numpy as np
>>> observed = np.array([[1.0, 2.0, 3.0, 4.0], [1.0, 2.0, 3.0, 4.0]])
>>> predicted = np.array([[1.0, 2.0, 3.0, 4.0], [2.5, 2.5, 2.5, 2.5]])
>>> calculate_r_squared(observed, predicted)
array([1., 0.])