forked from forgi86/sysid-transformers
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmetrics.py
More file actions
176 lines (132 loc) · 4.58 KB
/
metrics.py
File metadata and controls
176 lines (132 loc) · 4.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
import numpy as np
def r_squared(y_true, y_pred, time_axis=0):
""" Computes the R-square index.
The R-squared index is computed separately on each channel.
Parameters
----------
y_true : np.array
Array of true values. If must be at least 2D.
y_pred : np.array
Array of predicted values. If must be compatible with y_true'
time_axis : int
Time axis. All other axes define separate channels.
Returns
-------
r_squared_val : np.array
Array of r_squared value.
"""
SSE = np.sum((y_pred - y_true)**2, axis=time_axis)
y_mean = np.mean(y_true, axis=time_axis, keepdims=True)
SST = np.sum((y_true - y_mean)**2, axis=time_axis)
return 1.0 - SSE/SST
def rmse(y_true, y_pred, time_axis=0):
""" Computes the Root Mean Square Error (RMSE).
The RMSE index is computed separately on each channel.
Parameters
----------
y_true : np.array
Array of true values. If must be at least 2D.
y_pred : np.array
Array of predicted values. If must be compatible with y_true'
time_axis : int
Time axis. All other axes define separate channels.
Returns
-------
RMSE : np.array
Array of r_squared value.
"""
SSE = np.mean((y_pred - y_true)**2, axis=time_axis)
RMSE = np.sqrt(SSE)
return RMSE
def nrmse(y_true, y_pred, time_axis=0):
""" Computes the Normalized Root Mean Square Error (NRMSE).
The NRMSE index is computed separately on each channel.
Parameters
----------
y_true : np.array
Array of true values. If must be at least 2D.
y_pred : np.array
Array of predicted values. If must be compatible with y_true'
time_axis : int
Time axis. All other axes define separate channels.
Returns
-------
NRMSE : np.array
Array of r_squared value.
"""
SSE = np.mean((y_pred - y_true)**2, axis=time_axis)
RMSE = np.sqrt(SSE)
NRMSE = RMSE/np.std(y_true, axis=time_axis)
return NRMSE
def error_mean(y_true, y_pred, time_axis=0):
""" Computes the error mean value.
The error mean is computed separately on each channel.
Parameters
----------
y_true : np.array
Array of true values. If must be at least 2D.
y_pred : np.array
Array of predicted values. If must be compatible with y_true'
time_axis : int
Time axis. All other axes define separate channels.
Returns
-------
e_mean : np.array
Array of error means.
"""
e_mean = np.mean(y_true - y_pred, axis=time_axis)
return e_mean
def mae(y_true, y_pred, time_axis=0):
""" Computes the error Mean Absolute Value (MAE)
The MAE index is computed separately on each channel.
Parameters
----------
y_true : np.array
Array of true values. If must be at least 2D.
y_pred : np.array
Array of predicted values. If must be compatible with y_true'
time_axis : int
Time axis. All other axes define separate channels.
Returns
-------
e_mae : np.array
Array of error mean absolute values.
"""
e_mae = np.mean(np.abs(y_true - y_pred), axis=time_axis)
return e_mae
def fit_index(y_true, y_pred, time_axis=0):
""" Computes the per-channel fit index.
The fit index is commonly used in System Identification. See the definition in the System Identification Toolbox
or in the paper 'Nonlinear System Identification: A User-Oriented Road Map',
https://arxiv.org/abs/1902.00683, page 31.
The fit index is computed separately on each channel.
Parameters
----------
y_true : np.array
Array of true values. If must be at least 2D.
y_pred : np.array
Array of predicted values. If must be compatible with y_true'
time_axis : int
Time axis. All other axes define separate channels.
Returns
-------
fit : np.array
Array of fit index.
"""
err_norm = np.linalg.norm(y_true - y_pred, axis=time_axis, ord=2) # || y - y_pred ||
y_mean = np.mean(y_true, axis=time_axis, keepdims=True)
err_mean_norm = np.linalg.norm(y_true - y_mean, axis=time_axis, ord=2) # || y - y_mean ||
fit = 100*(1 - err_norm/err_mean_norm)
return fit
if __name__ == '__main__':
N = 20
ny = 2
SNR = 10
y_true = SNR*np.random.randn(N, 2)
y_pred = np.copy(y_true) + np.random.randn(N, 2)
err_rmse_val = rmse(y_pred, y_true)
r_squared_val = r_squared(y_true, y_pred)
fit_val = fit_index(y_true, y_pred)
print(f"RMSE: {err_rmse_val}")
print(f"R-squared: {r_squared_val}")
print(f"fit index: {fit_val}")