def critlevel(self, p=None, snsig=None, tol=3.e-4): """ The log critical PDF ratio (wrt mode) bounding an HPD credible region with probability p. """ # Take as an initial guess the ratio for a 1-d normal. if snsig: if p: raise ValueError('Provide only one of p, snsig!') lr1 = log(sig2ratio(1, snsig)) p = sig2prob(1, snsig) else: lr1 = log(prob2ratio(1, p)) p = float(p) # Otherwise it may be a scalar array. # Use this to bracket the solution and then solve. lr2 = lr1 - 0.1 # print 'Guesses:', lr1, lr2 self._target = p lr1, lr2 = zbracket(self._diff, lr1, lr2) # print 'Bracket:', lr1, lr2 logratio = float(brentq(self._diff, lr1, lr2, xtol=tol)) # print repr(logratio) self.probs.append(p) self.deltas.append(logratio) self.levels.append(self.max+logratio) return logratio
def critlevel(self, p=None, snsig=None, tol=3.e-4): """ The log critical PDF ratio (wrt mode) bounding an HPD credible region with probability p. """ # Take as an initial guess the ratio for a 1-d normal. if snsig: if p: raise ValueError, 'Provide only one of p, snsig!' lr1 = log(sig2ratio(1, snsig)) p = sig2prob(1, snsig) else: lr1 = log(prob2ratio(1, p)) p = float(p) # Otherwise it may be a scalar array. # Use this to bracket the solution and then solve. lr2 = lr1 - 0.1 #print 'Guesses:', lr1, lr2 self._target = p lr1, lr2 = zbracket(self._diff, lr1, lr2) #print 'Bracket:', lr1, lr2 logratio = float( brentq(self._diff, lr1, lr2, xtol=tol) ) #print repr(logratio) self.probs.append(p) self.deltas.append(logratio) self.levels.append(self.max+logratio) return logratio
def critlevel(self, p=None, snsig=None, tol=3.e-4): """ The log critical PDF ratio (wrt mode) bounding an HPD credible region with probability p. """ # Take as an initial guess the ratio for a 1-d normal. if snsig: if p: raise ValueError('Provide only one of p, snsig!') lr1 = log(sig2ratio(1, snsig)) p = sig2prob(1, snsig) else: lr1 = log(prob2ratio(1, p)) # Use the guess to bracket the solution and then solve. lr2 = lr1 - 0.1 # print 'Guesses:', lr1, lr2 self._target = p lr1, lr2 = zbracket(self._diff, lr1, lr2) # print 'Bracket:', lr1, lr2 logratio = float(brentq(self._diff, lr1, lr2, xtol=tol)) # print repr(logratio) self.probs.append(p) self.deltas.append(logratio) self.levels.append(self.max+logratio) # 10 is the max # boundaries; it should handle pretty bumpy PDFs! bounds, nb, ok = xvalues(self.xvals, self.logpdf-self.max, logratio, 10) if not ok: raise RuntimeError('Too many boundary points in PDF!') bounds = bounds[:nb] self.bounds.append(bounds) return logratio, bounds
def critlevel(self, p=None, snsig=None, tol=3.e-4): """ The log critical PDF ratio (wrt mode) bounding an HPD credible region with probability p. """ # Take as an initial guess the ratio for a 1-d normal. if snsig: if p: raise ValueError, 'Provide only one of p, snsig!' lr1 = log(sig2ratio(1, snsig)) p = sig2prob(1, snsig) else: lr1 = log(prob2ratio(1, p)) # Use the guess to bracket the solution and then solve. lr2 = lr1 - 0.1 #print 'Guesses:', lr1, lr2 self._target = p lr1, lr2 = zbracket(self._diff, lr1, lr2) #print 'Bracket:', lr1, lr2 logratio = float( brentq(self._diff, lr1, lr2, xtol=tol) ) #print repr(logratio) self.probs.append(p) self.deltas.append(logratio) self.levels.append(self.max+logratio) # 10 is the max # boundaries; it should handle pretty bumpy PDFs! bounds, nb, ok = xvalues(self.xvals, self.logpdf-self.max, logratio, 10) if not ok: raise RuntimeError, 'Too many boundary points in PDF!' bounds = bounds[:nb] self.bounds.append(bounds) return logratio, bounds