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 | class EffVsDist:
"""Search efficiency and sensitive volume as a function of distance.
The efficiency in a distance bin is the binomial fraction of injections
made in that bin that were recovered, and ``low``/``high`` bracket it with
the corresponding binomial (Wilson score) interval. The sensitive volume
is that efficiency integrated over the distances the injections were drawn
from.
``low`` and ``high`` are per-bin intervals. ``vt()`` integrates them bin
by bin, so the band it returns is the volume with every bin simultaneously
at its one sigma extreme: an envelope, deliberately the same one GstLAL
plots, and much wider than the uncertainty on the integral itself.
An empty distance bin is counted as having no sensitive volume, so
estimating in more bins than the injection set can fill biases the volume
low. The number of bins is therefore reduced until each holds
``injections_per_bin`` on average.
"""
def __init__(
self,
m,
f,
mcstart=0.0,
mcend=numpy.inf,
dist_bins=DIST_BINS,
injections_per_bin=INJECTIONS_PER_BIN,
):
def mc(m1, m2):
return (m1 * m2) ** 0.6 / (m1 + m2) ** 0.2
self.dm = sorted(
[
d
for m1, m2, d in zip(
m.simulation.mass1, m.simulation.mass2, m.simulation.distance
)
if mcstart <= mc(m1, m2) < mcend
]
)
self.df = sorted(
[
d
for m1, m2, d in zip(
f.simulation.mass1, f.simulation.mass2, f.simulation.distance
)
if mcstart <= mc(m1, m2) < mcend
]
)
if not self:
self.dist_bins = None
self.dint = None
self.edges = None
self.eff = self.low = self.high = None
return
# In the limit of a single bin this degrades gracefully to the plain
# Monte Carlo estimate: the injected volume times the found fraction.
self.dist_bins = max(
1, min(dist_bins, (len(self.dm) + len(self.df)) // injections_per_bin)
)
# Integrate over every distance an injection was placed at, not just
# the range where missed and found overlap: the near region, where
# everything is recovered, carries real sensitive volume.
self.dint = (min(self.dm[0], self.df[0]), max(self.dm[-1], self.df[-1]))
self.edges = numpy.linspace(*self.dint, self.dist_bins + 1)
found, _ = numpy.histogram(self.df, bins=self.edges)
# concatenate rather than add: these are lists, not arrays
total, _ = numpy.histogram(
numpy.concatenate((self.df, self.dm)), bins=self.edges
)
k = found.astype(float)
n = total.astype(float)
# An empty distance bin contributes no volume rather than 0/0.
nonempty = n > 0.0
n = numpy.where(nonempty, n, 1.0)
root = numpy.sqrt(4 * n * k * (n - k) + n**2)
self.eff = numpy.where(nonempty, k / n, 0.0)
self.low = numpy.where(
nonempty, (n * (2 * k + 1) - root) / (2 * n * (n + 1)), 0.0
)
self.high = numpy.where(
nonempty, (n * (2 * k + 1) + root) / (2 * n * (n + 1)), 0.0
)
def __bool__(self):
return len(self.dm) >= MIN_INJECTIONS and len(self.df) >= MIN_INJECTIONS
def darr(self):
if not self:
return None, None
deltas = numpy.diff(self.edges)
return self.edges[:-1] + deltas / 2, deltas
def __call__(self, d):
d = numpy.asarray(d, dtype=float)
if not self:
eff, low, high = (numpy.zeros(d.shape) for _ in range(3))
else:
# Bin each distance; anything outside the injected range has no
# measured efficiency. The upper edge belongs to the last bin,
# as it does to the histograms the efficiency was counted in.
idx = numpy.clip(
numpy.searchsorted(self.edges, d, side="right") - 1,
0,
self.dist_bins - 1,
)
inside = (d >= self.edges[0]) & (d <= self.edges[-1])
eff, low, high = (
numpy.where(inside, arr[idx], 0.0)
for arr in (self.eff, self.low, self.high)
)
if d.ndim == 0:
return float(eff), float(low), float(high)
return eff, low, high
def vt(self, t):
if not self:
return None
# Exact shell volumes rather than the midpoint rule, and the sphere
# inside the closest injection taken at the efficiency of the innermost
# bin. Injections drawn uniformly in volume put the binomial
# efficiency of a bin at the volume weighted mean across it, so the
# integral is then unbiased however coarse the binning has to be.
volumes = 4 * numpy.pi / 3 * numpy.diff(self.edges**3)
inner = 4 * numpy.pi / 3 * self.edges[0] ** 3
def f(eff_of_d, t=t):
return float((volumes * eff_of_d).sum() + inner * eff_of_d[0]) * t
return f(self.eff), f(self.low), f(self.high)
|