-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathobjectiveFunctions.py
70 lines (45 loc) · 1.74 KB
/
objectiveFunctions.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
# objective functions
import scipy.stats as sps
import numpy as np
def r2(x,y):
# from: https://en.wikipedia.org/wiki/Coefficient_of_determination#As_explained_variance
n = len(x)
m = len(y)
if np.sum(np.isnan(x)) == n or np.sum(np.isnan(y)) == m: return np.NaN
yBar = np.nanmean(y)
SStot = np.nansum(np.square(y-yBar)) # compute sum of squares total
SSres = np.nansum(np.square(y-x)) # residual sum of squares
return 1.-(SSres/SStot)
def NSE(x,y):
# from: https://en.wikipedia.org/wiki/Nash%E2%80%93Sutcliffe_model_efficiency_coefficient
n = len(x)
m = len(y)
if np.sum(np.isnan(x)) == n or np.sum(np.isnan(y)) == m: return np.NaN
yBar = np.nanmean(y)
return 1. - (np.nansum(np.square(x-y))/np.nansum(np.nanmean(y-yBar)))
def MAE(x,y):
# from: https://en.wikipedia.org/wiki/Mean_absolute_error
n = len(x)
m = len(y)
if np.sum(np.isnan(x)) == n or np.sum(np.isnan(y)) == m: return np.NaN
return np.nanmean(np.abs(x-y))
def RMSE(x,y):
# from: https://en.wikipedia.org/wiki/Root-mean-square_deviation
n = len(x)
m = len(y)
if np.sum(np.isnan(x)) == n or np.sum(np.isnan(y)) == m: return np.NaN
return np.sqrt(np.nanmean(np.square(x-y)))
def pearsonR(x,y):
# https://docs.scipy.org/doc/scipy-0.14.0/reference/generated/scipy.stats.pearsonr.html
n = len(x)
m = len(y)
if np.sum(np.isnan(x)) == n or np.sum(np.isnan(y)) == m: return np.NaN
R,p = sps.pearsonr(x,y)
return R
def pairedTtest(x,y):
# http://iaingallagher.tumblr.com/post/50980987285/t-tests-in-python
n = len(x)
m = len(y)
if np.sum(np.isnan(x)) == n or np.sum(np.isnan(y)) == m: return np.NaN
testStat,pVal = sps.ttest_rel(x,y)
return pVal