Skip to content
Snippets Groups Projects
Commit 4a571e71 authored by Gregory Ashton's avatar Gregory Ashton
Browse files

Resolve "Generate initial live points in dynesty"

parent f7ac2a19
No related branches found
No related tags found
No related merge requests found
...@@ -388,12 +388,62 @@ class Sampler(object): ...@@ -388,12 +388,62 @@ class Sampler(object):
self.check_draw(draw) self.check_draw(draw)
return draw return draw
def check_draw(self, draw): def get_initial_points_from_prior(self, npoints=1):
""" Checks if the draw will generate an infinite prior or likelihood """ """ Method to draw a set of live points from the prior
if np.isinf(self.log_likelihood(draw)):
logger.warning('Prior draw {} has inf likelihood'.format(draw)) This iterates over draws from the prior until all the samples have a
if np.isinf(self.log_prior(draw)): finite prior and likelihood (relevant for constrained priors).
logger.warning('Prior draw {} has inf prior'.format(draw))
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): def run_sampler(self):
"""A template method to run in subclasses""" """A template method to run in subclasses"""
......
...@@ -202,6 +202,10 @@ class Dynesty(NestedSampler): ...@@ -202,6 +202,10 @@ class Dynesty(NestedSampler):
def run_sampler(self): def run_sampler(self):
import dynesty 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( self.sampler = dynesty.NestedSampler(
loglikelihood=self.log_likelihood, loglikelihood=self.log_likelihood,
prior_transform=self.prior_transform, prior_transform=self.prior_transform,
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment