MeanDirectionalAccuracy¶
yohou.metrics.point.MeanDirectionalAccuracy
¶
Bases: BasePointScorer
Mean Directional Accuracy metric for point forecasts.
Computes the proportion of time steps where the predicted direction of change matches the actual direction of change. This metric evaluates whether the forecast correctly predicts upward or downward movements.
The MDA is defined as:
where \(\Delta y_i = y_i - y_{i-1}\) and \(\Delta \hat{y}_i = \hat{y}_i - \hat{y}_{i-1}\).
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 MDA. Higher values indicate better directional prediction. |
Examples¶
>>> import polars as pl
>>> from datetime import datetime
>>> from yohou.metrics import MeanDirectionalAccuracy
>>> y_true = pl.DataFrame({
... "time": [
... datetime(2020, 1, 1),
... datetime(2020, 1, 2),
... datetime(2020, 1, 3),
... datetime(2020, 1, 4),
... datetime(2020, 1, 5),
... ],
... "value": [10.0, 15.0, 12.0, 18.0, 20.0],
... })
>>> y_pred = pl.DataFrame({
... "vintage_time": [datetime(2019, 12, 31)] * 5,
... "time": [
... datetime(2020, 1, 1),
... datetime(2020, 1, 2),
... datetime(2020, 1, 3),
... datetime(2020, 1, 4),
... datetime(2020, 1, 5),
... ],
... "value": [10.0, 14.0, 15.0, 17.0, 19.0],
... })
>>> mda = MeanDirectionalAccuracy()
>>> _ = mda.fit(y_true)
>>> mda.score(y_true, y_pred)
0.75
Notes¶
- MDA = 1.0 means all directional changes were predicted correctly
- MDA = 0.5 is equivalent to random guessing for direction
- MDA = 0.0 means all directional predictions were wrong
- Requires at least 2 time steps (N-1 comparisons from
.diff()) - Returns 0.0 when fewer than 2 rows are available
- Overrides
score()because computing direction requires.diff()on the full columns, not per-row errors
See Also¶
MeanAbsoluteError: Error magnitude metric (not directional)R2Score: Variance explained metric
Source Code¶
Show/Hide source
1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 | |
Methods¶
score(y_truth, y_pred, /, vintage_weight=None, **params)
¶
Compute Mean Directional Accuracy.
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
|
MDA score between 0 and 1. 1.0 for perfect directional prediction. |
Raises¶
| Type | Description |
|---|---|
TypeError
|
If time_weight or step_weight are passed. |
Source Code¶
Show/Hide source
1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 | |