def __init__(self, samples=None, pdf_fun=None, xmin=None, xmax=None, unit=None, wrapped=False, bw_method=None, pdf_npoints=1500, samples_ref=True): # If needed, constructs a pdf function from samples, using KDE if samples is not None: unit, [xmin_val, xmax_val, samples_val] = unit_checker(unit, [xmin, xmax, samples]) assert unit is not None, 'At least one input must have a unit or a astropy unit must be provided' if (xmin is None) or (xmax is None): std = np.std(samples_val) if xmin is None: xmin_val = samples_val.min() - std if xmax is None: xmax_val = samples_val.max() + std pdf_fun = stats.gaussian_kde(samples_val, bw_method=bw_method) else: assert (xmin is not None and xmax is not None ), 'both xmin and xmax must be given for pdf_fun' unit, [xmin_val, xmax_val] = unit_checker(unit, [xmin, xmax]) # Evaluates the PDF pdf_x = np.linspace(xmin_val, xmax_val, pdf_npoints) if unit is not None: pdf_y = pdf_fun(pdf_x << unit) else: pdf_y = pdf_fun(pdf_x) super().__init__(xmin=xmin_val, xmax=xmax_val, unit=unit, wrapped=wrapped, pdf_npoints=pdf_npoints) dx = (xmax_val - xmin_val) / pdf_npoints inv_norm = pdf_y.sum() * dx pdf_y = (pdf_y / inv_norm) self._pdf_y = pdf_y self._pdf_x = pdf_x if (samples is not None) and samples_ref: # If requested, stores the samples to allow later (in the Pipeline) # determination of correlations between parameters self.samples = samples << self.unit # Adjusts units to avoid errors
def __init__(self, xmin=None, xmax=None, wrapped=False, unit=None, pdf_npoints=1500): # Ensures interval is quantity with consistent units unit, [xmin_val, xmax_val] = unit_checker(unit, [xmin, xmax]) if unit is None: unit = u.Unit(s='') self.unit = unit if xmin_val is None: xmin_val = -np.inf if xmax_val is None: xmax_val = np.inf self.range = [xmin_val, xmax_val] * unit self._pdf_npoints = pdf_npoints # Placeholders self._cdf = None self._inv_cdf = None self._distr = None self._pdf = None self.wrapped = wrapped self.samples = None
def __init__(self, distr, *args, loc=0.0, scale=1.0, xmin=None, xmax=None, unit=None, wrapped=False, pdf_npoints=1500, **kwargs): super().__init__(xmin=xmin, xmax=xmax, unit=unit, wrapped=wrapped, pdf_npoints=pdf_npoints) assert isinstance( distr, stats.rv_continuous ), 'distr must be instance of scipy.stats.rv_continuous' distr_instance = distr(*args, loc=loc, scale=scale, **kwargs) if (xmin is None) and (xmax is None): self._pdf = distr_instance.pdf self._cdf = distr_instance.cdf self._inv_cdf = distr_instance.ppf self._distr = distr_instance else: # If a trucated distribution is required, proceed as in # the empirical case unit, [xmin_val, xmax_val] = unit_checker(unit, [xmin, xmax]) self._pdf_x = np.linspace(xmin_val, xmax_val, pdf_npoints) self._pdf_y = distr_instance.pdf(self._pdf_x)
def __call__(self, cube): log.debug('@ flat_prior::__call__') unit, [cube_val] = unit_checker(self.unit, [cube]) # Rescales to the correct interval cube_val = cube_val * (self.range[1].value - self.range[0].value) cube_val += self.range[0].value return cube_val << unit
def pdf(self, x): """ Probability density function (PDF) associated with this prior. """ if self._pdf is None: self._pdf = interp1d(x=self._pdf_x, y=self._pdf_y) unit, [x_val] = unit_checker(self.unit, [x]) return self._pdf(x)
def __init__(self, mu=None, sigma=None, xmin=None, xmax=None, unit=None, **kwargs): assert mu is not None, 'A value for mu must be provided' assert sigma is not None, 'A value for sigma must be provided' unit, [mu_val, sigma_val] = unit_checker(unit, [mu, sigma]) super().__init__(distr=norm, loc=mu, scale=sigma, unit=unit, xmin=xmin, xmax=xmax, **kwargs)