-
Notifications
You must be signed in to change notification settings - Fork 0
/
kde_contour.py
252 lines (204 loc) · 8.64 KB
/
kde_contour.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
# The following script was copied from
# https://github.com/maxisi/ringdown/blob/c5388c010049dea59b1b54d02989ea9c19f4b12d/ringdown/kde_contour.py
# and modified to work with scripts in this repository.
__all__ = ['Bounded_2d_kde', 'Bounded_1d_kde', 'kdeplot_2d_clevels']
from pylab import *
import scipy.stats as ss
import seaborn as sns
# The following routine, Bounded_2d_kde, was copied from
# https://git.ligo.org/publications/gw190412/gw190412-discovery/-/blob/851f91431b7c36e7ea66fa47e8516f2aef9d7daf/scripts/bounded_2d_kde.py
class Bounded_2d_kde(ss.gaussian_kde):
r"""Represents a two-dimensional Gaussian kernel density estimator
for a probability distribution function that exists on a bounded
domain."""
def __init__(self, pts, xlow=None, xhigh=None, ylow=None, yhigh=None,
*args, **kwargs):
"""Initialize with the given bounds. Either ``low`` or
``high`` may be ``None`` if the bounds are one-sided. Extra
parameters are passed to :class:`gaussian_kde`.
:param xlow: The lower x domain boundary.
:param xhigh: The upper x domain boundary.
:param ylow: The lower y domain boundary.
:param yhigh: The upper y domain boundary.
"""
pts = np.atleast_2d(pts)
assert pts.ndim == 2, 'Bounded_kde can only be two-dimensional'
super(Bounded_2d_kde, self).__init__(pts.T, *args, **kwargs)
self._xlow = xlow
self._xhigh = xhigh
self._ylow = ylow
self._yhigh = yhigh
@property
def xlow(self):
"""The lower bound of the x domain."""
return self._xlow
@property
def xhigh(self):
"""The upper bound of the x domain."""
return self._xhigh
@property
def ylow(self):
"""The lower bound of the y domain."""
return self._ylow
@property
def yhigh(self):
"""The upper bound of the y domain."""
return self._yhigh
def evaluate(self, pts):
"""Return an estimate of the density evaluated at the given
points."""
pts = np.atleast_2d(pts)
assert pts.ndim == 2, 'points must be two-dimensional'
x, y = pts.T
pdf = super(Bounded_2d_kde, self).evaluate(pts.T)
if self.xlow is not None:
pdf += super(Bounded_2d_kde, self).evaluate([2*self.xlow - x, y])
if self.xhigh is not None:
pdf += super(Bounded_2d_kde, self).evaluate([2*self.xhigh - x, y])
if self.ylow is not None:
pdf += super(Bounded_2d_kde, self).evaluate([x, 2*self.ylow - y])
if self.yhigh is not None:
pdf += super(Bounded_2d_kde, self).evaluate([x, 2*self.yhigh - y])
if self.xlow is not None:
if self.ylow is not None:
pdf += super(Bounded_2d_kde, self).evaluate([2*self.xlow - x,
2*self.ylow - y])
if self.yhigh is not None:
pdf += super(Bounded_2d_kde, self).evaluate([2*self.xlow - x,
2*self.yhigh - y])
if self.xhigh is not None:
if self.ylow is not None:
pdf += super(Bounded_2d_kde, self).evaluate([2*self.xhigh - x,
2*self.ylow - y])
if self.yhigh is not None:
pdf += super(Bounded_2d_kde, self).evaluate([2*self.xhigh - x,
2*self.yhigh - y])
return pdf
def __call__(self, pts):
pts = np.atleast_2d(pts)
out_of_bounds = np.zeros(pts.shape[0], dtype='bool')
if self.xlow is not None:
out_of_bounds[pts[:, 0] < self.xlow] = True
if self.xhigh is not None:
out_of_bounds[pts[:, 0] > self.xhigh] = True
if self.ylow is not None:
out_of_bounds[pts[:, 1] < self.ylow] = True
if self.yhigh is not None:
out_of_bounds[pts[:, 1] > self.yhigh] = True
results = self.evaluate(pts)
results[out_of_bounds] = 0.
return results
# ############################################################################
# PLOTTING
def kdeplot_2d_clevels(xs, ys, levels=11, rng=None, min_size=500, **kwargs):
""" Plot contours at specified credible levels.
Arguments
---------
xs: array
samples of the first variable.
ys: array
samples of the second variable, drawn jointly with `xs`.
levels: float, array
if float, interpreted as number of credible levels to be equally
spaced between (0, 1); if array, interpreted as list of credible
levels.
xlow: float
lower bound for abscissa passed to Bounded_2d_kde (optional).
xigh: float
upper bound for abscissa passed to Bounded_2d_kde (optional).
ylow: float
lower bound for ordinate passed to Bounded_2d_kde (optional).
yhigh: float
upper bound for ordinate passed to Bounded_2d_kde (optional).
ax: Axes
matplotlib axes on which to plot (optional).
kwargs:
additional arguments passed to plt.contour().
"""
try:
xs = xs.values.astype(float)
ys = ys.values.astype(float)
except AttributeError:
pass
if all(~isfinite(xs)) or all(~isfinite(ys)):
return None
try:
len(levels)
f = 1 - np.array(levels)
except TypeError:
f = linspace(0, 1, levels+2)[1:-1]
if kwargs.pop('auto_bound', False):
kwargs['xlow'] = min(xs)
kwargs['xhigh'] = max(xs)
kwargs['ylow'] = min(ys)
kwargs['yhigh'] = max(ys)
kde_kws = {k: kwargs.pop(k, None) for k in ['xlow', 'xhigh', 'ylow', 'yhigh']}
k = Bounded_2d_kde(np.column_stack((xs, ys)), **kde_kws)
size = max([10*(len(f)+2), min_size])
if not isinstance(rng, np.random.Generator):
rng = np.random.default_rng(rng)
c = rng.choice(len(xs), size=size)
p = k(np.column_stack((xs[c], ys[c])))
i = argsort(p)
l = array([p[i[int(round(ff*len(i)))]] for ff in f])
Dx = np.percentile(xs, 99) - np.percentile(xs, 1)
Dy = np.percentile(ys, 99) - np.percentile(ys, 1)
x = linspace(np.percentile(xs, 1)-0.1*Dx, np.percentile(xs, 99)+0.1*Dx, 128)
y = linspace(np.percentile(ys, 1)-0.1*Dy, np.percentile(ys, 99)+0.1*Dy, 128)
XS, YS = meshgrid(x, y, indexing='ij')
ZS = k(np.column_stack((XS.flatten(), YS.flatten()))).reshape(XS.shape)
ax = kwargs.pop('ax', gca())
kwargs['colors'] = kwargs.get('colors', [kwargs.pop('color', None),])
if kwargs.pop('fill', False):
l = list(l) + [max(ZS.flatten())] # fill to max
ax.contourf(XS, YS, ZS, levels=l, **kwargs)
else:
ax.contour(XS, YS, ZS, levels=l, **kwargs)
class Bounded_1d_kde(ss.gaussian_kde):
""" Represents a one-dimensional Gaussian kernel density estimator
for a probability distribution function that exists on a bounded
domain.
Authorship: Ben Farr, LIGO
"""
def __init__(self, pts, xlow=None, xhigh=None, *args, **kwargs):
"""Initialize with the given bounds. Either ``low`` or
``high`` may be ``None`` if the bounds are one-sided. Extra
parameters are passed to :class:`gaussian_kde`.
:param xlow: The lower x domain boundary.
:param xhigh: The upper x domain boundary.
"""
pts = np.atleast_1d(pts)
assert pts.ndim == 1, 'Bounded_1d_kde can only be one-dimensional'
super(Bounded_1d_kde, self).__init__(pts.T, *args, **kwargs)
self._xlow = xlow
self._xhigh = xhigh
@property
def xlow(self):
"""The lower bound of the x domain."""
return self._xlow
@property
def xhigh(self):
"""The upper bound of the x domain."""
return self._xhigh
def evaluate(self, pts):
"""Return an estimate of the density evaluated at the given
points."""
pts = np.atleast_1d(pts)
assert pts.ndim == 1, 'points must be one-dimensional'
x = pts.T
pdf = super(Bounded_1d_kde, self).evaluate(pts.T)
if self.xlow is not None:
pdf += super(Bounded_1d_kde, self).evaluate(2*self.xlow - x)
if self.xhigh is not None:
pdf += super(Bounded_1d_kde, self).evaluate(2*self.xhigh - x)
return pdf
def __call__(self, pts):
pts = np.atleast_1d(pts)
out_of_bounds = np.zeros(pts.shape[0], dtype='bool')
if self.xlow is not None:
out_of_bounds[pts < self.xlow] = True
if self.xhigh is not None:
out_of_bounds[pts > self.xhigh] = True
results = self.evaluate(pts)
results[out_of_bounds] = 0.
return results