/
exp_abb_ks.py
168 lines (121 loc) · 4.8 KB
/
exp_abb_ks.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
"""Alberta boreal SW and AW competition experiment.
Generate all KS related plots for the with_comp and with_nocomp
flavours.
"""
import numpy as np
import cPickle as pickle
import matplotlib.pylab as plt
from exp_abb_kernels import abb_init_kernels, ignore
from utils.stats import ks1
##############################################################################
# ks plots
def ks_plots(plots, flavour, species):
pvals = []
for plotname in plots:
if plotname in ignore:
continue
plot = plots[plotname]
L, U, N, T, x, plotfile = plot['attrs']
sw, aw = abb_init_kernels(L, U, N, flavour, plotname)
nsw, naw = plot['nsw'], plot['naw']
for j, t in enumerate(T):
if t in sw.years:
if j > 0:
if species == 'sw':
ccdf, dcdf, d, p = ks1(sw.meas['SW'][t], nsw[j], x)
z = np.sort(sw.meas['SW'][t])
elif species == 'aw':
ccdf, dcdf, d, p = ks1(aw.meas['AW'][t], naw[j], x)
z = np.sort(aw.meas['AW'][t])
pvals.append(p)
plt.figure()
plt.plot(z, ccdf, '-k', label='model')
plt.plot(z, dcdf, '-r', label='meas')
plt.xlabel('dbh')
plt.ylabel('cdf')
plt.title('plot: %s, year: %d' % (plotname, t))
plt.legend(loc='best')
plt.savefig('plots/kscdf_%s_%s_%s_%d.png' % (flavour, plotname, species, t))
plt.close()
# histogram of pvals
plt.figure()
plt.hist(pvals, bins=21)
plt.xlabel('p-value')
plt.ylabel('frequency')
plt.savefig('plots/kshist_%s_%s.png' % (flavour, species))
plt.close()
# sorted scatter plot
plt.figure()
x = range(len(pvals))
plt.plot(x, sorted(pvals), '.k')
plt.plot(x, len(x)*[0.05], '-r')
plt.xlabel('index')
plt.ylabel('p-value')
plt.savefig('plots/ksscatter_%s_%s.png' % (flavour, species))
plt.close()
pvals = np.asarray(pvals)
n = len(pvals)
nsig = np.count_nonzero(pvals > 0.05)
print flavour, species, n, nsig, n - nsig
return pvals
def ks_compare(plots1, plots2, flavour, species):
pvals1 = []
pvals2 = []
for plotname in plots1:
if plotname in ignore:
continue
plot1 = plots1[plotname]
plot2 = plots2[plotname]
L, U, N, T, x, plotfile = plot1['attrs']
sw, aw = abb_init_kernels(L, U, N, flavour, plotname)
nsw1, naw1 = plot1['nsw'], plot1['naw']
nsw2, naw2 = plot2['nsw'], plot2['naw']
for j, t in enumerate(T):
if t in sw.years:
if j > 0:
if species == 'sw':
ccdf, dcdf, d, p = ks1(sw.meas['SW'][t], nsw1[j], x)
# pvals1.append(p)
pvals1.append(d)
ccdf, dcdf, d, p = ks1(sw.meas['SW'][t], nsw2[j], x)
# pvals2.append(p)
pvals2.append(d)
elif species == 'aw':
ccdf, dcdf, d, p = ks1(aw.meas['AW'][t], naw1[j], x)
# pvals1.append(p)
pvals1.append(d)
ccdf, dcdf, d, p = ks1(aw.meas['AW'][t], naw2[j], x)
# pvals2.append(p)
pvals2.append(d)
# pmax = 1.0
pmax = 0.6
# p-val vs p-val (actually d-val vs d-val for now...)
plt.figure()
plt.plot(pvals1, pvals2, 'ok', alpha=0.5)
# plt.axhline(y=0.05, color='black', linestyle='--')
# plt.axvline(x=0.05, color='black', linestyle='--')
plt.plot([0.0, pmax], [0.0, pmax], color='black')
# plt.xlabel('p-value (without competition)')
# plt.ylabel('p-value (with competition)')
plt.xlabel('KS statistic (without competition)')
plt.ylabel('KS statistic (with competition)')
dvals1 = np.asarray(pvals1)
dvals2 = np.asarray(pvals2)
print 'species', species
print 'npoints', len(dvals1)
print 'nbetter', np.count_nonzero(dvals1 > dvals2)
plt.savefig('plots/ksscatter_%s_comp.png' % species)
plt.close()
##############################################################################
# main
if __name__ == '__main__':
with open('out/abb_with_nocomp.pkl', 'r') as f:
plots1 = pickle.load(f)
# pvals = ks_plots(plots1, 'with_nocomp', 'aw')
# pvals = ks_plots(plots1, 'with_nocomp', 'sw')
with open('out/abb_with_comp.pkl', 'r') as f:
plots2 = pickle.load(f)
# pvals = ks_plots(plots2, 'with_comp', 'aw')
# pvals = ks_plots(plots2, 'with_comp', 'sw')
ks_compare(plots1, plots2, 'with_comp', 'sw')
ks_compare(plots1, plots2, 'with_comp', 'aw')