...
 
Commits (17)
......@@ -17,6 +17,7 @@ class Likelihood(object):
parameters:
"""
self.parameters = parameters
self._return_zero = False
def __repr__(self):
return self.__class__.__name__ + '(parameters={})'.format(self.parameters)
......@@ -48,6 +49,22 @@ class Likelihood(object):
"""
return self.log_likelihood() - self.noise_log_likelihood()
@property
def null_likelihood(self):
return self._return_zero
@null_likelihood.setter
def null_likelihood(self, value):
self._return_zero = value
if value:
self.__log_likelihood = self.log_likelihood
self.__log_likelihood_ratio = self.log_likelihood_ratio
self.log_likelihood = lambda: 0
self.log_likelihood_ratio = lambda: 0
else:
self.log_likelihood = self.__log_likelihood
self.log_likelihood_ratio = self.__log_likelihood_ratio
@property
def meta_data(self):
try:
......
......@@ -78,6 +78,8 @@ class Cpnest(NestedSampler):
for key in self.search_parameter_keys]
model = Model(self.search_parameter_keys, bounds)
out = CPNest(model, **self.kwargs)
if self.likelihood.null_likelihood:
out.NS.prior_sampling = True
out.run()
if self.plot:
......
from __future__ import absolute_import
# import bilby
import unittest
from mock import MagicMock
import mock
import numpy as np
from bilby.core.likelihood import (
Likelihood, GaussianLikelihood, PoissonLikelihood, StudentTLikelihood,
Analytical1DLikelihood, ExponentialLikelihood, JointLikelihood)
......@@ -587,5 +589,27 @@ class TestJointLikelihood(unittest.TestCase):
# self.assertDictEqual(self.joint_likelihood.parameters, joint_likelihood.parameters)
class TestNullLikelihood(unittest.TestCase):
def setUp(self):
npoints = 100
sigma = 0.1
xx = np.random.normal(0, sigma, npoints)
yy = xx
self.likelihood = GaussianLikelihood(
x=xx, y=yy, func=lambda x: x, sigma=sigma)
def test_set_null_likelihood(self):
self.likelihood.null_likelihood = True
self.assertEqual(self.likelihood.log_likelihood(), 0)
def test_unset_null_likelihood(self):
initial_log_l = self.likelihood.log_likelihood()
self.likelihood.null_likelihood = True
self.likelihood.null_likelihood = False
self.assertEqual(self.likelihood.log_likelihood(), initial_log_l)
if __name__ == '__main__':
unittest.main()
from __future__ import absolute_import
import bilby
from bilby.core import prior
import unittest
from mock import MagicMock
import numpy as np
import os
import sys
import shutil
import copy
import unittest
from mock import MagicMock
import numpy as np
from scipy.stats import ks_2samp
import bilby
from bilby.core import prior
class TestSampler(unittest.TestCase):
......@@ -497,8 +499,68 @@ class TestRunningSamplers(unittest.TestCase):
def test_run_PTMCMCSampler(self):
_ = bilby.run_sampler(
likelihood=self.likelihood, priors=self.priors,
sampler= 'PTMCMCsampler', Niter=101, burn =2,
isave = 100 ,save=False)
sampler='PTMCMCsampler', Niter=101, burn=2,
isave=100, save=False)
class TestNullLikelihood(unittest.TestCase):
"""
Test that when using a likelihood which returns lnL = 0 you recover the
prior distribution.
"""
def setUp(self):
self.priors = prior.PriorDict()
self.priors['aa'] = prior.Uniform(minimum=-5, maximum=5)
self.priors['bb'] = prior.Beta(minimum=-1, maximum=2, alpha=3, beta=4)
self.priors['cc'] = prior.TruncatedGaussian(
mu=0.7, sigma=1, minimum=-10, maximum=10)
xx = np.linspace(3, 7, 1000)
self.priors['dd'] = prior.Interped(xx=xx, yy=np.log(xx) + xx**7.3)
self.likelihood = bilby.core.likelihood.GaussianLikelihood(
x=1, y=1, sigma=1, func=lambda x: x)
self.likelihood.null_likelihood = True
self.min_pvalue = 1 - 0.99**(1 / len(self.priors))
def test_cpnest(self):
self._test_sampler('cpnest')
def test_dynesty(self):
self._test_sampler('dynesty')
def test_emcee(self):
self._test_sampler('emcee')
def test_nestle(self):
self._test_sampler('nestle')
def test_polychord(self):
self._test_sampler('pypolychord')
def test_ptemcee(self):
self._test_sampler('ptemcee')
def test_ptmcmc(self):
self._test_sampler('ptmcmcsampler')
def test_pymc3(self):
self._test_sampler('pymc3')
def test_pymultinest(self):
self._test_sampler('pymultinest')
def _test_sampler(self, sampler):
result = bilby.run_sampler(
likelihood=self.likelihood, priors=self.priors, sampler=sampler,
nlive=1000, iterations=1000, nwalkers=100, save=False)
pvalues = list()
for key in self.priors:
pvalues.append(ks_2samp(
self.priors.sample(len(result.posterior))[key],
result.posterior[key]).pvalue)
self.assertGreater(min(pvalues), self.min_pvalue)
if __name__ == '__main__':
......