forked from grsharman/sediment_unmixing
-
Notifications
You must be signed in to change notification settings - Fork 0
/
populationMetrics.py
288 lines (188 loc) · 10.2 KB
/
populationMetrics.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
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
#!/usr/bin/env python
'''
There are two classes of functions here. The mixture model class requires a function
that takes as input a daughter population, a list of parent populations, and a
list of mixture coefficients. This function returns some value that corresponds to
the quality of the fit (e.g. based on some statistical test or comparison function).
This ensures that all these tests are able to get all the inputs they need through the population class.
In some cases these functions will have optional additional arguments. In this case
these arguments can be fixed using pythons 'lambda' function.
Alternatively, I've also included some stripped down functions too that
just compare two sets of populations.
Dependencies:
numpy
scipy
detritalPopulation
'''
__author__ = "Sam Johnstone"
__copyright__ = "2016"
__credits__ = ["Sam Johnstone", "Glenn Sharman", "Lauren Shumaker"]
__license__ = "MIT"
__maintainer__ = "Sam Johnstone"
__email__ = "sjohnstone@usgs.gov"
__status__ = "Production"
#==============================================================================
# Import helpful shtuff
#==============================================================================
import numpy as np
from detritalPopulation import population
from scipy.stats import ks_2samp
#==============================================================================
# Definition of utility functions for comparing two distributions
#==============================================================================
#==============================================================================
# Dmax of CDFs
#==============================================================================
def mixture_Dmax(daughterPop,parentPops,mixingCoefficients):
'''Determine the Dmax value, the maximum difference between CDFs
:param daughterPop: an instance of the population class describing a daughter population
:param parentPops: a list of instances of the population class describing the parents
:param mixingCoefficients: a list, or array, of fractional mixing coefficients that must sum to one
'''
mixedPop = population(parentPopulations = parentPops,mixingCoefficients = mixingCoefficients)
return Dmax(daughterPop,mixedPop)
#==============================================================================
# Cross correlation coefficient (e.g. r^2) of pdfs
#==============================================================================
def mixture_correlationCoeff(daughterPop,parentPops,mixingCoefficients):
'''Calculate the correlation coefficient (e.g. r^2) b/w the two pdfs.
This method was proposed by Saylor and Sundell, 2016, Geosphere
:param daughterPop: an instance of the population class describing a daughter population
:param parentPops: a list of instances of the population class describing the parents
:param mixingCoefficients: a list, or array, of fractional mixing coefficients that must sum to one
'''
mixedPop = population(parentPopulations = parentPops,mixingCoefficients = mixingCoefficients)
return correlationCoeff(daughterPop,mixedPop)
#==============================================================================
# Similarity
#==============================================================================
def mixture_Similarity(daughterPop,parentPops,mixingCoefficients):
'''Determine the \'similarity\' of the the mixed and daughtter DZ populations using the Gehrels, 2000 method
This function is a wrapper to call calcSimilarity on the PDFs defined by
two population objects
:param daughterPop: an instance of the population class describing a daughter population
:param parentPops: a list of instances of the population class describing the parents
:param mixingCoefficients: a list, or array, of fractional mixing coefficients that must sum to one
'''
mixedPop = population(parentPopulations = parentPops,mixingCoefficients = mixingCoefficients)
return similarity(daughterPop,mixedPop)
#==============================================================================
# Likeness
#==============================================================================
def mixture_Likeness(daughterPop,parentPops,mixingCoefficients):
'''Determine the \'likeness\' of the daughter and mixed parent populations.
Here likeness is defined following Satkoski et al., 2013 as summarized in
Saylor and Sundell, 2016
:param daughterPop: an instance of the population class describing a daughter population
:param parentPops: a list of instances of the population class describing the parents
:param mixingCoefficients: a list, or array, of fractional mixing coefficients that must sum to one
'''
mixedPop = population(parentPopulations = parentPops,mixingCoefficients = mixingCoefficients)
return likeness(daughterPop,mixedPop)
#==============================================================================
# Vmax of Kuiper statistive
#==============================================================================
def mixture_Vmax(daughterPop,parentPops,mixingCoefficients):
'''Calculate the V max value of the kuiper statistic as summarized.
by Saylor and Sundell, 2016, Geosphere
:param daughterPop: an instance of the population class describing a daughter population
:param parentPops: a list of instances of the population class describing the parents
:param mixingCoefficients: a list, or array, of fractional mixing coefficients that must sum to one
'''
mixedPop = population(parentPopulations = parentPops,mixingCoefficients = mixingCoefficients)
return Vmax(daughterPop,mixedPop)
#==============================================================================
#
# Functions for direct comparisons of populations or distributions
#
#==============================================================================
def Dmax(pop1,pop2):
'''return the maximum distance between the two CDFS contained within the
populations
:param pop1: an instance of the population class
:param pop2: an instance of the population class
'''
if not isinstance(pop1,population):
print('Error, both inputs must be population objects, first is not')
pass
if not isinstance(pop2,population):
print('Error, both inputs must be population objects, second is not')
pass
if np.any(np.logical_not(pop1._tAxisCDF_ == pop2._tAxisCDF_)):
print('Error, axes of CDFs do not match')
pass
return np.max(np.abs(pop1.CDF - pop2.CDF))
def likeness(pop1,pop2):
'''Determine the \'likeness\' of the two populations.
Here likeness is defined following Satkoski et al., 2013 as summarized in
Saylor and Sundell, 2016
:param pop1: an instance of the population class
:param pop2: an instance of the population class
'''
if not isinstance(pop1,population):
print('Error, both inputs must be population objects, first is not')
pass
if not isinstance(pop2,population):
print('Error, both inputs must be population objects, second is not')
pass
if np.any(np.logical_not(pop1._tAxisDF_ == pop2._tAxisDF_)):
print('Error, age axes of PDFs do not match')
pass
return 1.0 - np.sum(np.abs(pop1.densFunc - pop2.densFunc))/2.0
def correlationCoeff(pop1,pop2):
''' Determine r^2 of the two PDFS. As proposed by Saylor and Sundell, 2016
:param pop1: an instance of the population class
:param pop2: an instance of the population class
'''
mean1 = np.mean(pop1.densFunc)
mean2 = np.mean(pop2.densFunc)
r_numerator = np.sum((pop1.densFunc - mean1)*(pop2.densFunc - mean2))
r_denom = np.sqrt(np.sum((pop1.densFunc - mean1)**2))*np.sqrt(np.sum((pop2.densFunc - mean2)**2))
return (r_numerator/r_denom)**2
def KSTest(pop1,pop2):
'''Wrapper function to calculate the KS statistic from the CDFs defined by two populations.
Following definition as provided in Saylor and Sundell 2016, Geosphere, Eqn. 2
:param pop1: an instance of the population class
:param pop2: an instance of the population class
'''
if not isinstance(pop1,population):
print('Error, both inputs must be population objects, first is not')
pass
if not isinstance(pop2,population):
print('Error, both inputs must be population objects, second is not')
pass
return ks_2samp(pop1.ages,pop2.ages).pvalue
def Vmax(pop1,pop2):
'''Wrapper for calcVmax
:param pop1: an instance of the population class
:param pop2: an instance of the population class
'''
return calcVmax(pop1.CDF,pop2.CDF)
def calcVmax(CDF1,CDF2):
'''Return the sum of the maximum distances (most positive & most negative)
b/w the two CDFS, the V value of Saylor and Sundell 2016, Geosphere; Eqn. 6
:param CDF1: a CDF
:param CDF2: a different CDF... or is it the same?!
'''
return np.max(CDF1-CDF2) + np.max(CDF2-CDF1)
def similarity(pop1,pop2):
'''Determine the \'similarity\' of the two DZ populations using the Gehrels, 2000 method
This function is a wrapper to call calcSimilarity on the PDFs defined by
two population objects
:param pop1: an instance of the population class
:param pop2: an instance of the population class
'''
if not isinstance(pop1,population):
print('Error, both inputs must be population objects, first is not')
pass
if not isinstance(pop2,population):
print('Error, both inputs must be population objects, second is not')
pass
return calcSimilarity(pop1.densFunc,pop2.densFunc)
def calcSimilarity(PDF1,PDF2):
'''Determine the \'similarity\' of the two DZ populations using the Gehrels, 2000 method
This is the sum of the product to the two probability density functions
:param PDF1: a PDF
:param PDF2: a different PDF... or is it the same?!
'''
return np.sum(np.sqrt(PDF1*PDF2))