R2Score¶
yohou.metrics.point.R2Score
¶
Bases: BasePointScorer
R-squared (Coefficient of Determination) metric for point forecasts.
Computes the proportion of variance in the true values that is explained by the predictions. A score of 1.0 indicates perfect prediction, 0.0 indicates performance equivalent to predicting the mean, and negative values indicate worse performance than predicting the mean.
The R² is defined as:
where \(y_i\) is the actual value, \(\hat{y}_i\) is the predicted value, \(\bar{y}\) is the mean of actual values, and \(n\) is the number of observations.
Parameters¶
| Name | Type | Description | Default |
|---|---|---|---|
aggregation_method
|
list of str or str
|
Dimensions to aggregate over. Options: - "stepwise": Aggregate across forecasting steps. - "vintagewise": Aggregate across vintages (observed times). - "componentwise": Aggregate across components, return per-timestep DataFrame - "groupwise": Aggregate across panel groups (panel data only) - "all": Aggregate across all dimensions (returns scalar). Same as ["stepwise", "vintagewise", "componentwise", "groupwise"]. |
"all"
|
groups
|
list of str, dict of str to float, or None
|
Panel group filter (list) or filter with weights (dict). |
None
|
components
|
list of str, dict of str to float, or None
|
Component filter (list) or filter with weights (dict). |
None
|
Attributes¶
| Name | Type | Description |
|---|---|---|
lower_is_better |
bool
|
Always False for R². Higher values indicate better fit. |
Examples¶
>>> import polars as pl
>>> from datetime import datetime
>>> from yohou.metrics import R2Score
>>> y_true = pl.DataFrame({
... "time": [datetime(2020, 1, 1), datetime(2020, 1, 2), datetime(2020, 1, 3)],
... "value": [10.0, 20.0, 30.0],
... })
>>> y_pred = pl.DataFrame({
... "vintage_time": [datetime(2019, 12, 31)] * 3,
... "time": [datetime(2020, 1, 1), datetime(2020, 1, 2), datetime(2020, 1, 3)],
... "value": [12.0, 18.0, 31.0],
... })
>>> r2 = R2Score()
>>> _ = r2.fit(y_true)
>>> r2.score(y_true, y_pred)
0.955
Notes¶
- R² = 1.0 means perfect prediction
- R² = 0.0 means predictions are as good as predicting the mean
- R² < 0 means predictions are worse than predicting the mean
- When SS_tot = 0 (constant true values), returns 0.0 by convention
- Overrides
score()because computing the denominator (SS_tot) requires access to the fully_truthcolumn, not just per-row errors
See Also¶
MeanSquaredError: Mean Squared Error, the numerator component of R²MeanAbsoluteError: Mean Absolute Error, alternative regression metric
Source Code¶
Show/Hide source
1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 | |
Methods¶
score(y_truth, y_pred, /, vintage_weight=None, **params)
¶
Compute R-squared score.
Parameters¶
| Name | Type | Description | Default |
|---|---|---|---|
y_truth
|
DataFrame
|
True values with "time" column. |
required |
y_pred
|
DataFrame
|
Predicted values with "time" column. |
required |
vintage_weight
|
callable, pl.DataFrame, dict, or None
|
Per-vintage weights for cross-vintage aggregation. |
None
|
**params
|
dict
|
Metadata to route to nested estimators. |
{}
|
Returns¶
| Type | Description |
|---|---|
float or DataFrame
|
R² score. 1.0 for perfect predictions, 0.0 for mean-level predictions. |
Raises¶
| Type | Description |
|---|---|
TypeError
|
If time_weight or step_weight are passed. |