MeanDirectionalAccuracy¶
yohou.metrics.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
|
time_weighter
|
BaseWeighter or None
|
Weighter applied along the time axis (observed timestamps). Because MDA is non-linear, only zero-weight timestamps are dropped; non-zero weights do not otherwise reweight the score. If None, all timestamps contribute equally. |
None
|
step_weighter
|
BaseWeighter or None
|
Weighter applied along the forecasting-step axis. Only zero-weight steps
are dropped (see |
None
|
vintage_weighter
|
BaseWeighter or None
|
Weighter applied along the vintage-time axis. If None, all vintages contribute equally. |
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 and
aggregation_method='all'; raisesValueErrorfor all other aggregation settings - 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 ¶
Source code in src/yohou/metrics/point.py
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 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 | |
Methods ¶
score(y_truth, y_pred, /, **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 |
**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. |
Source Code ¶
Source code in src/yohou/metrics/point.py
1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 | |