-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathEEstatslib.py
More file actions
194 lines (172 loc) · 5.58 KB
/
Copy pathEEstatslib.py
File metadata and controls
194 lines (172 loc) · 5.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# -*- coding: utf-8 -*-
"""
Created on Wed Sep 1 11:32:51 2021
This script provides general statistic numbers calculations for model-data comparison
This should be use as a follow up script after using dffromdatfile function in dataFrameFromdatfiles.py
Default input format are pandas dataframe and series
@author: DSI
"""
import numpy as np
# ------------------------------------------------------------------------------
# statistic functions
# ------------------------------------------------------------------------------
def drop_nan(df):
"""
this function reads in dataframe after using
dffromdatfile function in dataFrameFromdatfiles.py
then returns a dataframe without nan
"""
df_dropped = df.dropna()
return df_dropped
def data_paired(df):
"""
this function return the number of data paired
after dropping nan values
"""
return df.shape[0]
def bias(s, o):
"""
Bias
input:
s: simulated
o: observed
output:
bias
"""
return np.mean(s-o)
def rbias(s, o):
"""
Relative Bias
input:
s: simulated
o: observed
output:
relative bias
"""
return 100*(np.sum(s-o))/np.sum(o)
def mae(s, o):
"""
Mean(Average) Absolute Error
input:
s: simulated
o: observed
output:
mean absolute error
"""
return np.mean(np.abs(s-o))
def rmse(s, o):
"""
Root Mean Squared Error
input:
s: simulated
o: observed
output:
root mean squared error
"""
return np.sqrt(np.mean((s-o)**2))
def rrmse(s, o):
"""
Relative Root Mean Squared Error
input:
s: simulated
o: observed
output:
relative root mean squared error
"""
return 100*np.sqrt(np.mean((s-o)**2))/(o.max()-o.min())
def correlation(s, o):
"""
Correlation Coefficient
input:
s: simulated
o: observed
output:
correlation coefficient
"""
return np.corrcoef(o, s)[0, 1]
def r_sqr(s, o):
"""
R Squared (Square of Correlation Coefficient)
input:
s: simulated
o: observed
output:
R Squared
"""
return correlation(s, o)**2
def nsi(s, o):
"""
Nash-Sutcliffe Index of Efficiency
input:
s: simulated
o: observed
output:
nash-sutcliffe index of efficiency
"""
return 1-np.sum((s-o)**2)/np.sum((o-np.mean(o))**2)
def coe(s, o):
"""
Coefficient of Efficiency
input:
s: simulated
o: observed
output:
coefficient of efficiency
"""
return 1 - np.sum(np.abs(s-o))/np.sum(np.abs(o-np.mean(o)))
def ioa(s, o):
"""
Index of Agreement
input:
s: simulated
o: observed
output:
index of agreement
"""
return 1 - (np.sum((o-s)**2))/\
(np.sum((np.abs(s-np.mean(o))+np.abs(o-np.mean(o)))**2))
def kge(s, o):
"""
Kling-Gupta Efficiency
input:
s: simulated
o: observed
output:
kgef: kling-gupta efficiency
cc: correlation
alpha: ratio of the standard deviation
beta: ratio of the mean
"""
cc = correlation(s, o)
alpha = np.std(s)/np.std(o)
beta = np.sum(s)/np.sum(o)
kgef = 1 - np.sqrt((cc-1)**2 + (alpha-1)**2 + (beta-1)**2)
return kgef, cc, alpha, beta
def stats_summary(df, sim_column_idx=0, obs_column_idx=1, decimals=3):
"""
Statistics Summary, output all statistics number in dictionary
input:
df: dataframe from EE.dat file
(default just two columns, model and data)
sim_column_idx: column index for simulated values (default 0)
obs_column_idx: column index for observed values (default 1)
decimals: round all statistics to the given number of decimals (default 3)
output:
statsummary: dictionary with all statistics number
"""
df_drop = drop_nan(df)
simulated = df_drop.iloc[:, sim_column_idx]
observed = df_drop.iloc[:, obs_column_idx]
statsummary = {'Data Paired': data_paired(df_drop),
'Bias': np.round(bias(simulated, observed), decimals),
'Percent Bias': np.round(rbias(simulated, observed), decimals),
'Mean Absolute Error': np.round(mae(simulated, observed), decimals),
'RMSE': np.round(rmse(simulated, observed), decimals),
'RRMSE': np.round(rrmse(simulated, observed), decimals),
'R': np.round(correlation(simulated, observed), decimals),
'R-Sqr': np.round(r_sqr(simulated, observed), decimals),
'Nash-Sutcliffe Efficiency': np.round(nsi(simulated, observed), decimals),
'Coefficient of Efficiency': np.round(coe(simulated, observed),decimals),
'Index of Agreement': np.round(ioa(simulated, observed), decimals),
'Kling-Gupta Efficiency': np.round(list(kge(simulated, observed))[0], decimals)}
return statsummary