Skip to content

Commit 65e997a

Browse files
committed
[JTH] first implementation for kfold in RBF model
1 parent 835431b commit 65e997a

File tree

7 files changed

+255
-664
lines changed

7 files changed

+255
-664
lines changed

bluemath_tk/core/metrics.py

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
import numpy as np
2+
3+
4+
def bias(X_true: np.ndarray, X_pred: np.ndarray) -> float:
5+
"""
6+
Calculate the BIAS.
7+
8+
Parameters
9+
----------
10+
X_true : np.ndarray
11+
True values.
12+
X_pred : np.ndarray
13+
Predicted values.
14+
15+
Returns
16+
-------
17+
float
18+
The BIAS value.
19+
"""
20+
21+
if len(X_true) != len(X_pred):
22+
raise ValueError("X_true and X_pred must have the same length")
23+
24+
return float(sum(X_true - X_pred) / len(X_true))
25+
26+
27+
def si(X_true: np.ndarray, X_pred: np.ndarray) -> float:
28+
"""
29+
Calculate the Scatter Index (SI).
30+
31+
Parameters
32+
----------
33+
X_true : np.ndarray
34+
True values.
35+
X_pred : np.ndarray
36+
Predicted values.
37+
38+
Returns
39+
-------
40+
float
41+
The Scatter Index value.
42+
"""
43+
44+
if len(X_true) != len(X_pred):
45+
raise ValueError("X_true and X_pred must have the same length")
46+
47+
return float(
48+
np.sqrt(
49+
sum(((X_true - X_true.mean()) - (X_pred - X_pred.mean())) ** 2)
50+
/ sum(X_true**2)
51+
)
52+
)
53+
54+
55+
def mse(X_true: np.ndarray, X_pred: np.ndarray) -> float:
56+
"""
57+
Calculate Mean Squared Error (MSE).
58+
59+
Parameters
60+
----------
61+
X_true : np.ndarray
62+
True values.
63+
X_pred : np.ndarray
64+
Predicted values.
65+
66+
Returns
67+
-------
68+
float
69+
Mean squared error.
70+
"""
71+
72+
if len(X_true) != len(X_pred):
73+
raise ValueError("X_true and X_pred must have the same length")
74+
75+
return float(np.mean((X_true - X_pred) ** 2))
76+
77+
78+
def mae(X_true: np.ndarray, X_pred: np.ndarray) -> float:
79+
"""
80+
Calculate Mean Absolute Error (MAE).
81+
82+
Parameters
83+
----------
84+
X_true : np.ndarray
85+
True values.
86+
X_pred : np.ndarray
87+
Predicted values.
88+
89+
Returns
90+
-------
91+
float
92+
Mean absolute error.
93+
"""
94+
95+
if len(X_true) != len(X_pred):
96+
raise ValueError("X_true and X_pred must have the same length")
97+
98+
return float(np.mean(np.abs(X_true - X_pred)))
99+
100+
101+
def rmse(X_true: np.ndarray, X_pred: np.ndarray) -> float:
102+
"""
103+
Calculate Root Mean Squared Error (RMSE).
104+
105+
Parameters
106+
----------
107+
X_true : np.ndarray
108+
True values.
109+
X_pred : np.ndarray
110+
Predicted values.
111+
112+
Returns
113+
-------
114+
float
115+
Root mean squared error.
116+
"""
117+
118+
return float(np.sqrt(mse(X_true, X_pred)))
119+
120+
121+
def r2(X_true: np.ndarray, X_pred: np.ndarray) -> float:
122+
"""
123+
Calculate the R² score.
124+
125+
Parameters
126+
----------
127+
X_true : np.ndarray
128+
True values.
129+
X_pred : np.ndarray
130+
Predicted values.
131+
132+
Returns
133+
-------
134+
float
135+
The R² score.
136+
"""
137+
138+
if len(X_true) != len(X_pred):
139+
raise ValueError("X_true and X_pred must have the same length")
140+
141+
return float(
142+
1.0 - np.sum((X_true - X_pred) ** 2) / np.sum((X_true - X_true.mean()) ** 2)
143+
)

0 commit comments

Comments
 (0)