|
| 1 | +"""Functional interface for the mean absolute percentage error metric.""" |
| 2 | +from typing import Tuple, Union |
| 3 | + |
| 4 | +import array_api_compat as apc |
| 5 | + |
| 6 | +from cyclops.evaluate.metrics.experimental.utils.types import Array |
| 7 | +from cyclops.evaluate.metrics.experimental.utils.validation import ( |
| 8 | + _basic_input_array_checks, |
| 9 | + _check_same_shape, |
| 10 | +) |
| 11 | + |
| 12 | + |
| 13 | +def _mean_absolute_percentage_error_update( |
| 14 | + target: Array, |
| 15 | + preds: Array, |
| 16 | + epsilon: float = 1.17e-06, |
| 17 | +) -> Tuple[Array, int]: |
| 18 | + """Update and return variables required to compute the Mean Percentage Error. |
| 19 | +
|
| 20 | + Parameters |
| 21 | + ---------- |
| 22 | + target : Array |
| 23 | + Ground truth target values. |
| 24 | + preds : Array |
| 25 | + Estimated target values. |
| 26 | + epsilon : float, optional, default=1.17e-06 |
| 27 | + Specifies the lower bound for target values. Any target value below epsilon |
| 28 | + is set to epsilon (avoids division by zero errors). |
| 29 | +
|
| 30 | + Returns |
| 31 | + ------- |
| 32 | + Tuple[Array, int] |
| 33 | + Sum of absolute value of percentage errors over all observations and number |
| 34 | + of observations. |
| 35 | +
|
| 36 | + """ |
| 37 | + _basic_input_array_checks(target, preds) |
| 38 | + _check_same_shape(target, preds) |
| 39 | + xp = apc.array_namespace(target, preds) |
| 40 | + |
| 41 | + abs_diff = xp.abs(preds - target) |
| 42 | + abs_target = xp.abs(target) |
| 43 | + clamped_abs_target = xp.where( |
| 44 | + abs_target < epsilon, |
| 45 | + xp.asarray(epsilon, dtype=abs_target.dtype, device=apc.device(abs_target)), |
| 46 | + abs_target, |
| 47 | + ) |
| 48 | + abs_per_error = abs_diff / clamped_abs_target |
| 49 | + |
| 50 | + sum_abs_per_error = xp.sum(abs_per_error, dtype=xp.float32) |
| 51 | + |
| 52 | + num_obs = int(apc.size(target) or 0) |
| 53 | + |
| 54 | + return sum_abs_per_error, num_obs |
| 55 | + |
| 56 | + |
| 57 | +def _mean_absolute_percentage_error_compute( |
| 58 | + sum_abs_per_error: Array, |
| 59 | + num_obs: Union[int, Array], |
| 60 | +) -> Array: |
| 61 | + """Compute the Mean Absolute Percentage Error. |
| 62 | +
|
| 63 | + Parameters |
| 64 | + ---------- |
| 65 | + sum_abs_per_error : Array |
| 66 | + Sum of absolute value of percentage errors over all observations. |
| 67 | + ``(percentage error = (target - prediction) / target)`` |
| 68 | + num_obs : int, Array |
| 69 | + Number of observations. |
| 70 | +
|
| 71 | + Returns |
| 72 | + ------- |
| 73 | + Array |
| 74 | + The mean absolute percentage error. |
| 75 | +
|
| 76 | + """ |
| 77 | + return sum_abs_per_error / num_obs # type: ignore[no-any-return] |
| 78 | + |
| 79 | + |
| 80 | +def mean_absolute_percentage_error(target: Array, preds: Array) -> Array: |
| 81 | + """Compute the mean absolute percentage error. |
| 82 | +
|
| 83 | + Parameters |
| 84 | + ---------- |
| 85 | + target : Array |
| 86 | + Ground truth target values. |
| 87 | + preds : Array |
| 88 | + Estimated target values. |
| 89 | +
|
| 90 | + Returns |
| 91 | + ------- |
| 92 | + Array |
| 93 | + The mean absolute percentage error. |
| 94 | +
|
| 95 | + Raises |
| 96 | + ------ |
| 97 | + TypeError |
| 98 | + If `target` or `preds` is not an array object that is compatible with |
| 99 | + the Python array API standard. |
| 100 | + ValueError |
| 101 | + If `target` or `preds` is empty. |
| 102 | + ValueError |
| 103 | + If `target` or `preds` is not a numeric array. |
| 104 | + ValueError |
| 105 | + If the shape of `target` and `preds` are not the same. |
| 106 | +
|
| 107 | + Notes |
| 108 | + ----- |
| 109 | + The epsilon value is taken from `scikit-learn's implementation of MAPE`. |
| 110 | +
|
| 111 | + Examples |
| 112 | + -------- |
| 113 | + >>> import numpy.array_api as anp |
| 114 | + >>> from cyclops.evaluate.metrics.experimental.functional import ( |
| 115 | + ... mean_absolute_percentage_error |
| 116 | + ... ) |
| 117 | + >>> target = anp.asarray([1., 10., 1e6]) |
| 118 | + >>> preds = anp.asarray([0.9, 15., 1.2e6]) |
| 119 | + >>> mean_absolute_percentage_error(target, preds) |
| 120 | + Array(0.26666668, dtype=float32) |
| 121 | +
|
| 122 | + """ |
| 123 | + sum_abs_per_error, num_obs = _mean_absolute_percentage_error_update(target, preds) |
| 124 | + return _mean_absolute_percentage_error_compute(sum_abs_per_error, num_obs) |
0 commit comments