def __init__(self, log_pdf, xlo, xhi, nx, ylo, yhi, ny, xstep='linear', ystep='linear'): self.log_pdf = log_pdf self.xlo, self.xhi = xlo, xhi self.nx = nx if xstep == 'linear' or xstep=='lin': self.xlinstep = True self.xvals = array( [x for x in lin_stepper(xlo, xhi, self.nx)] ) self.dx = (xhi-xlo)/(self.nx-1) elif xstep=='log' or xstep=='logarithmic': self.xlinstep = False self.xvals = array( [x for x in log_stepper(xlo, xhi, self.nx)] ) self.dx = log(xhi/xlo)/(self.nx-1) else: raise ValueError, 'Invalid xstep type!' self.ylo, self.yhi = ylo, yhi self.ny = ny if ystep == 'linear' or ystep=='lin': self.ylinstep = True self.yvals = array( [y for y in lin_stepper(ylo, yhi, self.ny)] ) self.dy = (yhi-ylo)/(self.ny-1) elif ystep=='log' or ystep=='logarithmic': self.ylinstep = False self.yvals = array( [y for y in log_stepper(ylo, yhi, self.ny)] ) self.dy = log(yhi/ylo)/(self.ny-1) else: raise ValueError, 'Invalid ystep type!' logpdf = zeros((nx, ny), float) for i,x in enumerate(self.xvals): for j,y in enumerate(self.yvals): logpdf[i,j] = log_pdf(x,y) self.logpdf = logpdf self.max = logpdf.max() self.spdf = exp(logpdf-self.max) # Scaled PDF # For log steps, change variables to log(x or y). if not self.xlinstep: for i, x in enumerate(self.xvals): self.spdf[i,:] = self.spdf[i,:]*x if not self.ylinstep: for j, y in enumerate(self.yvals): self.spdf[:,j] = self.spdf[:,j]*y # Find the overall normalization for spdf. self.norm = qgt2d(self.spdf, self.spdf.min())*self.dx*self.dy # lml is the log marginal likelihood if logpdf = prior*like. self.lml = log(self.norm) + self.max self.probs = [] self.deltas = [] self.levels = []
def fracgt(self, logratio): """ The fraction of the posterior with log density > maximum + logratio. """ ratio = exp(logratio) return qgt2d(self.spdf, ratio)*self.dx*self.dy/self.norm