def __init_jeffreys(self, A, B): """Calculate jeffreys prior as defined in Sivia p.125""" self.kind = 'jeffreys' A = interpret_quantity(A, expect_sequence=False) B = interpret_quantity(B, expect_sequence=False) assert A.dimensionality == B.dimensionality self._state_attrs.extend(['A', 'B']) self.units = str(A.units) B = B.to(A.units) self.A = A self.B = B def llh(x): x = self.__strip(self.__convert(x)) A = self.__strip(self.A) B = self.__strip(self.B) return -np.log(x) + np.log(np.log(B) - np.log(A)) self.llh = llh self.max_at = self.A self.max_at_str = self.__stringify(self.max_at) self.valid_range = (self.A * ureg(self.units), self.B * ureg(self.units)) self._str = lambda s: "jeffreys' prior, range [%s,%s]" % (self.A, self. B)
def __init_gaussian(self, mean, stddev): mean = interpret_quantity(mean, expect_sequence=False) stddev = interpret_quantity(stddev, expect_sequence=False) assert mean.dimensionality == stddev.dimensionality self._state_attrs.extend(['mean', 'stddev']) self.kind = 'gaussian' if isinstance(mean, ureg.Quantity): self.units = str(mean.units) assert isinstance(stddev, ureg.Quantity), \ str(type(stddev)) stddev = stddev.to(self.units) self.mean = mean self.stddev = stddev def llh(x): x = self.__strip(self.__convert(x)) m = self.__strip(self.mean) s = self.__strip(self.stddev) return -(x - m)**2 / (2 * s**2) self.llh = llh self.max_at = self.mean self.max_at_str = self.__stringify(self.max_at) self.valid_range = (-np.inf * ureg(self.units), np.inf * ureg(self.units)) self._str = lambda s: 'gaussian prior: stddev=%s%s, maximum at %s%s' \ %(self.__stringify(self.stddev), self.units_str, self.__stringify(self.mean), self.units_str)
def __init_linterp(self, param_vals, llh_vals): param_vals = interpret_quantity(param_vals, expect_sequence=True) self._state_attrs.extend(['param_vals', 'llh_vals']) self.kind = 'linterp' if isinstance(param_vals, ureg.Quantity): self.units = str(param_vals.units) self.interp = interp1d(param_vals.magnitude, llh_vals, kind='linear', copy=True, bounds_error=True, assume_sorted=False) self.param_vals = param_vals self.llh_vals = llh_vals def llh(x): x = self.__strip(self.__convert(x)) return self.interp(x) self.llh = llh self.max_at = self.param_vals[self.llh_vals == np.max(self.llh_vals)] self.max_at_str = ', '.join([self.__stringify(v) for v in self.max_at]) self.valid_range = (np.min(self.param_vals) * ureg(self.units), np.max(self.param_vals) * ureg(self.units)) self._str = lambda s: 'linearly-interpolated prior: valid in [%s, %s]%s, maxima at (%s)%s' \ %(self.__stringify(np.min(self.param_vals)), self.__stringify(np.max(self.param_vals)), self.units_str, self.max_at_str, self.units_str)
def __init_spline(self, knots, coeffs, deg, units=None): knots = interpret_quantity(knots, expect_sequence=True) self._state_attrs.extend(['knots', 'coeffs', 'deg']) self.kind = 'spline' if isunitless(knots): knots = ureg.Quantity(knots, units) elif units is not None: units = ureg.Unit(units) assert knots.dimensionality == units.dimensionality knots = knots.to(units) self.units = str(knots.units) self.knots = knots self.coeffs = coeffs self.deg = deg def llh(x): x = self.__strip(self.__convert(x)) return splev(x, tck=(self.__strip(self.knots), coeffs, deg), ext=2) self.llh = llh self.max_at = fminbound( func=self.__attach_units_to_args(self.chi2), x1=np.min(self.__strip(self.knots)), x2=np.max(self.__strip(self.knots)), ) if self.units is not None: self.max_at = self.max_at * ureg(self.units) self.max_at_str = self.__stringify(self.max_at) self.valid_range = (np.min(self.knots) * ureg(self.units), np.max(self.knots) * ureg(self.units)) self._str = lambda s: 'spline prior: deg=%d, valid in [%s, %s]%s; max at %s%s' \ %(self.deg, self.__stringify(np.min(self.knots)), self.__stringify(np.max(self.knots)), self.units_str, self.max_at_str, self.units_str)