Commit 4a571e71 authored by Gregory Ashton's avatar Gregory Ashton

Resolve "Generate initial live points in dynesty"

parent f7ac2a19
......@@ -388,12 +388,62 @@ class Sampler(object):
self.check_draw(draw)
return draw
def check_draw(self, draw):
""" Checks if the draw will generate an infinite prior or likelihood """
if np.isinf(self.log_likelihood(draw)):
logger.warning('Prior draw {} has inf likelihood'.format(draw))
if np.isinf(self.log_prior(draw)):
logger.warning('Prior draw {} has inf prior'.format(draw))
def get_initial_points_from_prior(self, npoints=1):
""" Method to draw a set of live points from the prior
This iterates over draws from the prior until all the samples have a
finite prior and likelihood (relevant for constrained priors).
Parameters
----------
npoints: int
The number of values to return
Returns
-------
unit_cube, parameters, likelihood: tuple of array_like
unit_cube (nlive, ndim) is an array of the prior samples from the
unit cube, parameters (nlive, ndim) is the unit_cube array
transformed to the target space, while likelihood (nlive) are the
likelihood evaluations.
"""
unit_cube = []
parameters = []
likelihood = []
while len(unit_cube) < npoints:
unit = np.random.rand(self.ndim)
theta = self.prior_transform(unit)
if self.check_draw(theta, warning=False):
unit_cube.append(unit)
parameters.append(theta)
likelihood.append(self.log_likelihood(theta))
return np.array(unit_cube), np.array(parameters), np.array(likelihood)
def check_draw(self, theta, warning=True):
""" Checks if the draw will generate an infinite prior or likelihood
Parameters
----------
theta: array_like
Parameter values at which to evaluate likelihood
Returns
-------
bool, cube (nlive,
True if the likelihood and prior are finite, false otherwise
"""
if np.isinf(self.log_prior(theta)):
if warning:
logger.warning('Prior draw {} has inf prior'.format(theta))
return False
if np.isinf(self.log_likelihood(theta)):
if warning:
logger.warning('Prior draw {} has inf likelihood'.format(theta))
return False
return True
def run_sampler(self):
"""A template method to run in subclasses"""
......
......@@ -202,6 +202,10 @@ class Dynesty(NestedSampler):
def run_sampler(self):
import dynesty
if self.kwargs['live_points'] is None:
self.kwargs['live_points'] = (
self.get_initial_points_from_prior(
self.kwargs['nlive']))
self.sampler = dynesty.NestedSampler(
loglikelihood=self.log_likelihood,
prior_transform=self.prior_transform,
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment