PranavSharma commited on
Commit
0f30466
·
verified ·
1 Parent(s): a52ec73

MAE percentage metrics used

Browse files
Files changed (1) hide show
  1. utils/metrics.py +79 -35
utils/metrics.py CHANGED
@@ -1,37 +1,81 @@
1
- import numpy as np
2
-
3
-
4
- # Adding type hints for better code clarity and numpy style comments for documentation
5
- def mae(y_true: np.ndarray, y_pred: np.ndarray) -> float:
6
- """Mean Absolute Error
7
- Parameters
8
- ----------
9
- y_true : np.ndarray
10
- True values
11
- y_pred : np.ndarray
12
- Predicted values
13
- Returns
14
- -------
15
- float
16
- Mean Absolute Error
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17
  """
18
- y_true = np.array(y_true)
19
- y_pred = np.array(y_pred)
20
- return np.mean(np.abs(y_true - y_pred))
21
-
22
- def bias(y_true: np.ndarray, y_pred: np.ndarray) -> float:
23
- """Bias
24
- Parameters
25
- ----------
26
- y_true : np.ndarray
27
- True values
28
- y_pred : np.ndarray
29
- Predicted values
30
- Returns
31
- -------
32
- float
33
- Bias
34
  """
35
- y_true = np.array(y_true)
36
- y_pred = np.array(y_pred)
37
- return np.mean(y_pred - y_true)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # import numpy as np
2
+
3
+
4
+ # # Adding type hints for better code clarity and numpy style comments for documentation
5
+ # def mae(y_true: np.ndarray, y_pred: np.ndarray) -> float:
6
+ # """Mean Absolute Error
7
+ # Parameters
8
+ # ----------
9
+ # y_true : np.ndarray
10
+ # True values
11
+ # y_pred : np.ndarray
12
+ # Predicted values
13
+ # Returns
14
+ # -------
15
+ # float
16
+ # Mean Absolute Error
17
+ # """
18
+ # y_true = np.array(y_true)
19
+ # y_pred = np.array(y_pred)
20
+ # return np.mean(np.abs(y_true - y_pred))
21
+
22
+ # def bias(y_true: np.ndarray, y_pred: np.ndarray) -> float:
23
+ # """Bias
24
+ # Parameters
25
+ # ----------
26
+ # y_true : np.ndarray
27
+ # True values
28
+ # y_pred : np.ndarray
29
+ # Predicted values
30
+ # Returns
31
+ # -------
32
+ # float
33
+ # Bias
34
+ # """
35
+ # y_true = np.array(y_true)
36
+ # y_pred = np.array(y_pred)
37
+ # return np.mean(y_pred - y_true)
38
+
39
+
40
+ import numpy as np
41
+
42
+ def mae(y_true: np.ndarray, y_pred: np.ndarray, eps: float = 1e-12) -> float:
43
+ """
44
+ Sum-based MAE percentage, in %.
45
+
46
+ Formula:
47
+ MAE%_sum = 100 * sum(|y_true - y_pred|) / sum(|y_true|)
48
+
49
+ Notes:
50
+ - Equivalent to WAPE when computed on one series/window.
51
+ - Stable for intermittent demand compared with per-point MAPE.
52
  """
53
+ y_true = np.asarray(y_true, dtype=float)
54
+ y_pred = np.asarray(y_pred, dtype=float)
55
+
56
+ denom = np.sum(np.abs(y_true))
57
+ if denom < eps:
58
+ return np.nan # undefined if all actuals are ~0
59
+
60
+ return 100.0 * np.sum(np.abs(y_true - y_pred)) / denom
61
+
62
+
63
+ def bias(y_true: np.ndarray, y_pred: np.ndarray, eps: float = 1e-12) -> float:
 
 
 
 
 
64
  """
65
+ Sum-based Bias percentage, in %.
66
+
67
+ Formula:
68
+ Bias%_sum = 100 * sum(y_pred - y_true) / sum(|y_true|)
69
+
70
+ Sign:
71
+ > 0 => over-forecasting in aggregate
72
+ < 0 => under-forecasting in aggregate
73
+ """
74
+ y_true = np.asarray(y_true, dtype=float)
75
+ y_pred = np.asarray(y_pred, dtype=float)
76
+
77
+ denom = np.sum(np.abs(y_true))
78
+ if denom < eps:
79
+ return np.nan # undefined if all actuals are ~0
80
+
81
+ return 100.0 * np.sum(y_pred - y_true) / denom