def _profile_loglike_spline(self, x): """Internal function to calculate and cache the profile likelihood """ z = [] y = [] yv = self._nuis_pdf.profile_bins() nuis_vals = self._nuis_pdf.log_value(yv) - self._nuis_log_norm for xtmp in x: zv = -1. * self._lnlfn.interp(xtmp * yv) + nuis_vals sp = splrep(yv, zv, k=2, s=0) rf = lambda t: splev(t, sp, der=1) ix = np.argmax(splev(yv, sp)) imin, imax = max(0, ix - 3), min(len(yv) - 1, ix + 3) try: y0 = opt.brentq(rf, yv[imin], yv[imax], xtol=1e-10) except: y0 = yv[ix] z0 = self.loglike(xtmp, y0) z.append(z0) y.append(y0) self._prof_y = np.array(y) self._prof_z = np.array(z) self._prof_z = self._prof_z.max() - self._prof_z self._prof_interp = castro.Interpolator(x, self._prof_z) return self._prof_y, self._prof_z
def _profile_loglike_spline(self, x): """Internal function to calculate and cache the profile likelihood """ z = [] y = [] yv = self._nuis_pdf.profile_bins() nuis_vals = self._nuis_pdf.log_value(yv) - self._nuis_log_norm for xtmp in x: zv = -1. * self._lnlfn.interp(xtmp * yv) + nuis_vals sp = splrep(yv, zv, k=2, s=0) def rf(t): """Functor for spline evaluation""" return splev(t, sp, der=1) # pylint: disable=cell-var-from-loop ix = np.argmax(splev(yv, sp)) imin, imax = max(0, ix - 3), min(len(yv) - 1, ix + 3) try: y0 = opt.brentq(rf, yv[imin], yv[imax], xtol=1e-10) except Exception: y0 = yv[ix] z0 = self.loglike(xtmp, y0) z.append(z0) y.append(y0) prof_y = np.array(y) prof_z = np.array(z) prof_z = prof_z.max() - prof_z self._prof_interp = castro.Interpolator(x, prof_z) return prof_y, prof_z
def _posterior(self, x): """Internal function to calculate and cache the posterior """ yedge = self._nuis_pdf.marginalization_bins() yc = 0.5 * (yedge[1:] + yedge[:-1]) yw = yedge[1:] - yedge[:-1] like_array = self.like(x[:, np.newaxis], yc[np.newaxis, :]) * yw like_array /= like_array.sum() self._post = like_array.sum(1) self._post_interp = castro.Interpolator(x, self._post) return self._post
def _profile_loglike(self, x): """Internal function to calculate and cache the profile likelihood """ x = np.array(x, ndmin=1) z = [] y = [] for xtmp in x: fn = lambda t: -self.loglike(xtmp, t) ytmp = opt.fmin(fn, 1.0, disp=False)[0] ztmp = self.loglike(xtmp, ytmp) z.append(ztmp) y.append(ytmp) self._prof_y = np.array(y) self._prof_z = np.array(z) self._prof_z = self._prof_z.max() - self._prof_z self._prof_interp = castro.Interpolator(x, self._prof_z) return self._prof_y, self._prof_z
def _marginal_loglike(self, x): """Internal function to calculate and cache the marginal likelihood """ yedge = self._nuis_pdf.marginalization_bins() yw = yedge[1:] - yedge[:-1] yc = 0.5 * (yedge[1:] + yedge[:-1]) s = self.like(x[:, np.newaxis], yc[np.newaxis, :]) # This does the marginalization integral z = 1. * np.sum(s * yw, axis=1) marg_z = np.zeros(z.shape) msk = z > 0 marg_z[msk] = -1 * np.log(z[msk]) # Extrapolate to unphysical values dlogzdx = (np.log(z[msk][-1]) - np.log(z[msk][-2])) / (x[msk][-1] - x[msk][-2]) marg_z[~msk] = marg_z[msk][-1] + \ (marg_z[~msk] - marg_z[msk][-1]) * dlogzdx self._marg_interp = castro.Interpolator(x, marg_z) return marg_z
def _profile_loglike(self, x): """Internal function to calculate and cache the profile likelihood """ x = np.array(x, ndmin=1) z = [] y = [] for xtmp in x: #def fn(t): # """Functor to return profile likelihood""" # return -self.loglike(xtmp, t) fn = partial(LnLFn_norm_prior.nll_static, self, xtmp) ytmp = opt.fmin(fn, 1.0, disp=False)[0] ztmp = self.loglike(xtmp, ytmp) z.append(ztmp) y.append(ytmp) prof_y = np.array(y) prof_z = np.array(z) prof_z = prof_z.max() - prof_z self._prof_interp = castro.Interpolator(x, prof_z) return prof_y, prof_z
def _profile_loglike(self, x): """Internal function to calculate and cache the profile likelihood """ x = np.array(x, ndmin=1) z = [] y = [] for xtmp in x: def fn(t): """Functor to return profile likelihood""" return -self.loglike(xtmp, t) # pylint: disable=cell-var-from-loop ytmp = opt.fmin(fn, 1.0, disp=False)[0] ztmp = self.loglike(xtmp, ytmp) z.append(ztmp) y.append(ytmp) prof_y = np.array(y) prof_z = np.array(z) prof_z = prof_z.max() - prof_z self._prof_interp = castro.Interpolator(x, prof_z) return prof_y, prof_z