bilby-dev / bilby

A unified framework for stochastic sampling packages and gravitational-wave inference in Python. Note that we are currently transitioning from git.ligo.org/lscsoft/bilby, please bear with us!
https://bilby-dev.github.io/bilby/
MIT License
59 stars 67 forks source link

Zero Likelihood run stucks with Dynesty >= 1.1 #658

Closed bilby-bot closed 3 weeks ago

bilby-bot commented 3 years ago

In GitLab by @git.ligo:yumeng.xu on Aug 24, 2021, 15:11

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.

bilby-bot commented 3 years ago

In GitLab by @git.ligo:gregory.ashton on Aug 25, 2021, 16:38

mentioned in commit d4bbe04ad3710efc7ed2203005dd4b40c46a4107

bilby-bot commented 3 years ago

In GitLab by @git.ligo:gregory.ashton on Aug 25, 2021, 17:07

I've temporarily pinned the dynesty version in !1020 and created #601 to track updating the API.

bilby-bot commented 3 years ago

In GitLab by @git.ligo:gregory.ashton on Aug 27, 2021, 14:16

mentioned in commit 5e32d7173a727b411b67470f3f99d9531bed9614