Skip to content

Zero Likelihood run stucks with Dynesty >= 1.1

Hi, recently I try to use bilby.core.likelihood.ZeroLikelihood in a new installed environment to calculate the prior distribution. But it got stuck after

bilby INFO    : Generating initial points from the prior

I have checked the code and it seems that it stuck in an infinite loop in dynesty.sampler._new_point which should break after logl > loglstar, but the values of both variables are 0.

The problem only exist for Dynesty >= 1.1. When changing to Dynesty = 1.0.1, it works fine. I haven't checked which changes caused this issue. But this simple test code can reproduce it

Install bilby directly

conda create --name bilby_test
conda activate bilby_test
conda install -c conda-forge bilby

Install with old dynesty

conda create --name bilby_test2
conda activate bilby_test2
conda install -c conda-forge dynesty=1.0.1
conda install -c conda-forge bilby

Then run the simple test python script in two environments

#!/usr/bin/env python
import bilby
import numpy as np

# A few simple setup steps
label = 'gaussian_example'
outdir = 'outdir'

# Here is minimum requirement for a Likelihood class to run with bilby. In this
# case, we setup a GaussianLikelihood, which needs to have a log_likelihood
# method. Note, in this case we will NOT make use of the `bilby`
# waveform_generator to make the signal.

# Making simulated data: in this case, we consider just a Gaussian

data = np.random.normal(3, 4, 100)


class SimpleGaussianLikelihood(bilby.Likelihood):
    def __init__(self, data):
        """
        A very simple Gaussian likelihood

        Parameters
        ----------
        data: array_like
            The data to analyse
        """
        super().__init__(parameters={'mu': None, 'sigma': None})
        self.data = data
        self.N = len(data)

    def log_likelihood(self):
        mu = self.parameters['mu']
        sigma = self.parameters['sigma']
        res = self.data - mu
        return -0.5 * (np.sum((res / sigma)**2) +
                       self.N * np.log(2 * np.pi * sigma**2))


likelihood = bilby.core.likelihood.ZeroLikelihood(SimpleGaussianLikelihood(data))
priors = dict(mu=bilby.core.prior.Uniform(0, 5, 'mu'),
              sigma=bilby.core.prior.Uniform(0, 10, 'sigma'))

# And run sampler
result = bilby.run_sampler(
    likelihood=likelihood, priors=priors, sampler='dynesty', npoints=500,
    walks=10, outdir=outdir, label=label)
result.plot_corner()

The bilby_test environment will stuck while the old version works fine.