def calculate(self, inputs, verbose=False): n_evs = inputs[:len(self.signals)] binned_signals = inputs[len(self.signals):-1] x_kj = inputs[-1] if x_kj is not self.last_x_kj: if x_kj.shape == tuple( [len(edges) - 1 for edges in self.bin_edges]): print( 'NOTE: pre-binned data detected; assuming binning is correct' ) self.counts = cp.asarray(x_kj) else: self.counts, _ = cp.histogramdd(cp.asarray(x_kj), bins=self.bin_edges) self.last_x_kj = x_kj if verbose: print('Evaluate:', ', '.join(['%0.3f' % s for s in n_evs])) expected = cp.sum(cp.asarray( [n * bin_ints for n, bin_ints in zip(n_evs, binned_signals)]), axis=0) expected = expected.reshape(self.counts.shape) mask = expected != 0 res = cp.sum(n_evs) - cp.sum( self.counts[mask] * cp.log(expected[mask])) if verbose: print('NLL:', res) if np.isnan(res): if self.nan_behavior == 'unlikely': return 1e200 else: raise Exception('NaN likelihood!') return res if np == cp else res.get()
def bin_mc(self, t_ij, w_i): ''' ''' counts, _ = cp.histogramdd(cp.asarray(t_ij), bins=self.bin_edges, weights=cp.asarray(w_i)) return (counts.flatten() / self.bin_vol / cp.sum(counts)).reshape( counts.shape)
def load_mc(self, t_ij): for j, (l, h) in enumerate( zip(self.observables.lows, self.observables.highs)): in_bounds = np.logical_and(t_ij[:, j] > l, t_ij[:, j] < h) t_ij = t_ij[in_bounds] self.t_ij = cp.asarray(t_ij) self.w_i = cp.ones(t_ij.shape[0]) if self.bootstrap_binning is not None: counts, _ = cp.histogramdd(cp.asarray(self.t_ij), bins=self.bin_edges, weights=cp.asarray(self.w_i)) self.counts = (cp.asarray(counts).flatten() / self.bin_vol / cp.sum(cp.asarray(counts))).reshape(counts.shape) self.sigma_j = cp.std(self.t_ij, axis=0) self.h_ij = self._adapt_bandwidth() for j, (l, h, refl) in enumerate( zip(self.observables.lows, self.observables.highs, self.reflect_axes)): if not refl: continue if type(refl) == tuple: low, high = refl mask = self.t_ij[:, j] < low t_ij_reflected_low = cp.copy(self.t_ij[mask, :]) h_ij_reflected_low = self.h_ij[mask, :] w_i_reflected_low = self.w_i[mask, :] t_ij_reflected_low[:, j] = 2 * l - t_ij_reflected_low[:, j] mask = self.t_ij[:, j] > high t_ij_reflected_high = cp.copy(self.t_ij[mask, :]) h_ij_reflected_high = self.h_ij[mask, :] w_i_reflected_high = self.w_i[mask, :] t_ij_reflected_high[:, j] = 2 * h - t_ij_reflected_high[:, j] else: t_ij_reflected_low = cp.copy(self.t_ij) h_ij_reflected_low = self.h_ij w_i_reflected_low = self.w_i t_ij_reflected_low[:, j] = 2 * l - self.t_ij[:, j] t_ij_reflected_high = cp.copy(self.t_ij) h_ij_reflected_high = self.h_ij w_i_reflected_high = self.w_i t_ij_reflected_high[:, j] = 2 * h - self.t_ij[:, j] self.t_ij = cp.concatenate( [self.t_ij, t_ij_reflected_low, t_ij_reflected_high]) self.h_ij = cp.concatenate( [self.h_ij, h_ij_reflected_low, h_ij_reflected_high]) self.w_i = cp.concatenate( [self.w_i, w_i_reflected_low, w_i_reflected_high]) self.t_ij = cp.ascontiguousarray(self.t_ij) self.h_ij = cp.ascontiguousarray(self.h_ij) self.w_i = cp.ascontiguousarray(self.w_i)
def test_histogramdd_disallow_arraylike_bins(self): x = testing.shaped_random((16, 2), cupy, scale=100) bins = [[0, 10, 20, 50, 90]] * 2 # too many dimensions with pytest.raises(ValueError): y, bin_edges = cupy.histogramdd(x, bins=bins)
def time_fine_binning(self): np.histogramdd(self.d, (10000, 10000), ((0, 100), (0, 100)))
def time_small_coverage(self): np.histogramdd(self.d, (200, 200), ((50, 51), (50, 51)))
def time_full_coverage(self): np.histogramdd(self.d, (200, 200), ((0, 100), (0, 100)))