-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPerfMetrics.py
147 lines (101 loc) · 3.97 KB
/
PerfMetrics.py
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
"""
Module to calculate model performance metrics
Author: Francesca Pianosi, francesca.pianosi@bristol.ac.uk
"""
import numpy as np
from scipy.stats import pearsonr
def CHECK_DIM(y_sim, y_obs):
Nsim = y_sim.shape
if len(Nsim) > 1: # multiple time series
N = Nsim[0] # number of rows (no of time series)
T = Nsim[1] # number of columns (no of time steps)
elif len(Nsim) == 1: # only one time series
T = Nsim[0] # number of time steps
N = 1
Nobs = y_obs.shape
err_message = ""
if len(Nobs) > 1:
if Nobs[0] != 1:
err_message = '"y_obs" be of shape (T, )'
if Nobs[1] != T:
err_message = 'the number of elements in "y_obs" must be equal to the number of columns in "y_sim"'
elif len(Nobs) == 1:
if Nobs[0] != T:
err_message = 'the number of elements in "y_obs" must be equal to the number of columns in "y_sim"'
return N, T, err_message
def CORR_ERR(y_sim, y_obs):
"""
ce = CORR_ERR(Y_sim,y_obs)
calculate the performance metric:
ce = ( r - 1 )^2
where r = Pearson correlation between y_sim and y_obs
Usage:
Y_sim = time series of modelled variable - numpy.ndarray (T, )
(N > 1 different time series can be or - numpy.ndarray (N,T)
evaluated at once)
y_obs = time series of observed variable - numpy.ndarray (T, )
ce = correlation error - scalar
or - numpy.ndarray (N, )
"""
N, T, err_message = CHECK_DIM(y_sim, y_obs)
if len(err_message)>0:
raise ValueError(err_message)
raise ValueError(err_message)
if N==1:
corr, _ = pearsonr(y_obs,y_sim)
if N>1:
corr=np.zeros((N,))
for n in range(N):
corr[n],_= pearsonr(y_obs,y_sim[n,:])
corr_err = (corr-1)**2
return corr_err
def STD_ERR(y_sim, y_obs):
"""
se = STD_ERR(Y_sim,y_obs)
calculate the performance metric:
se = ( S_sim/S_obs - 1 )^2
where S_sim = standard deviation of y_sim
S_obs = standard deviation of y_obs
Usage:
Y_sim = time series of modelled variable - numpy.ndarray (T, )
(N > 1 different time series can be or - numpy.ndarray (N,T)
evaluated at once)
y_obs = time series of observed variable - numpy.ndarray (T, )
se = standard deviation error - scalar
or - numpy.ndarray (N, )
"""
N, T, err_message = CHECK_DIM(y_sim, y_obs)
if len(err_message)>0:
raise ValueError(err_message)
std_obs = np.std(y_obs)
if N==1:
std_sim=np.std(y_sim)
elif N>=1:
std_sim = np.std(y_sim,axis=1)
std_err = (std_sim/std_obs-1)**2
return std_err
def MEAN_ERR(y_sim, y_obs):
"""
me = SMEAN_ERR(Y_sim,y_obs)
calculate the performance metric:
me = ( M_sim/M_obs - 1 )^2
where M_sim = mean of y_sim
M_obs = mean of y_obs
Usage:
Y_sim = time series of modelled variable - numpy.ndarray (T, )
(N > 1 different time series can be or - numpy.ndarray (N,T)
evaluated at once)
y_obs = time series of observed variable - numpy.ndarray (T, )
me = mean error - scalar
or - numpy.ndarray (N, )
"""
N, T, err_message = CHECK_DIM(y_sim, y_obs)
if len(err_message)>0:
raise ValueError(err_message)
mean_obs = np.mean(y_obs)
if N==1:
mean_sim=np.mean(y_sim)
elif N>=1:
mean_sim = np.mean(y_sim,axis=1)
mean_err = (mean_sim/mean_obs-1)**2
return mean_err