def __init__(self, log_pdf, xlo, xhi, nx, step='linear'): """ Initialize the HPDGrid1D object. :Parameters: log_pdf : float function of scalar argument Function returning log pdf value log_pdf(x) xlo, xhi : float Range spanned by logpdf nx : int Number of points in the logpdf grid step : 'linear' OR 'lin' OR 'logarithmic' OR 'log' 'linear' for a grid linear in x; 'log' for a grid linear in log(x) """ self.log_pdf = log_pdf xlo, xhi = float(xlo), float(xhi) self.xlo, self.xhi = xlo, xhi self.nx = nx if step == 'linear' or step=='lin': self.linstep = True self.xvals = array( [x for x in lin_stepper(xlo, xhi, self.nx)] ) self.dx = (xhi-xlo)/(self.nx-1) elif step=='log' or step=='logarithmic': self.linstep = False self.xvals = [x for x in log_stepper(xlo, xhi, self.nx)] self.dx = log(xhi/xlo)/(self.nx-1) else: raise ValueError, 'Invalid step type!' logpdf = zeros(nx, float) for i, x in enumerate(self.xvals): logpdf[i] = log_pdf(x) self.logpdf = logpdf self.max = logpdf.max() self.spdf = exp(logpdf-self.max) # scaled PDF # For log steps, change variables to log(x). if not self.linstep: for i, x in enumerate(self.xvals): self.spdf[i] = self.spdf[i]*x # Find the overall normalization for spdf. self.snorm = qgt1d(self.spdf, self.spdf.min())*self.dx # lml is the log marginal likelihood if logpdf = prior*like. self.lml = log(self.snorm) + self.max self.probs = [] self.deltas = [] self.levels = [] self.bounds = []
def __init__(self, logpdf, xlo, xhi, step='linear'): """ Initialize the HPDGrid1D object. :Parameters: logpdf : float array Array of log pdf values xlo, xhi : float Range spanned by logpdf step : 'linear' OR 'lin' OR 'logarithmic' OR 'log' 'linear' for a grid linear in x; 'log' for a grid linear in log(x) """ if len(logpdf.shape) != 1: raise ValueError, 'logpdf array must be 1-d!' self.logpdf = logpdf self.xlo, self.xhi = xlo, xhi self.nx = logpdf.shape[0] if step == 'linear' or step=='lin': self.linstep = True self.xvals = array( [x for x in lin_stepper(xlo, xhi, self.nx)] ) self.dx = (xhi-xlo)/(self.nx-1.) elif step=='log' or step=='logarithmic': self.linstep = False self.xvals = [x for x in log_stepper(xlo, xhi, self.nx)] self.dx = log(xhi/xlo)/(self.nx-1) else: raise ValueError, 'Invalid step type!' self.max = logpdf.max() self.spdf = exp(logpdf-self.max) # scaled PDF # For log steps, change variables to log(x). if not self.linstep: for i, x in enumerate(self.xvals): self.spdf[i] = self.spdf[i]*x # Find the overall normalization for spdf. self.snorm = qgt1d(self.spdf, self.spdf.min())*self.dx # lml is the log marginal likelihood if logpdf = prior*like. self.lml = log(self.snorm) + self.max self.probs = [] self.deltas = [] self.levels = [] self.bounds = []
def fracgt(self, logratio): """ The fraction of the posterior with log density > maximum + logratio. """ ratio = exp(logratio) return qgt1d(self.spdf, ratio)*self.dx/self.snorm