def setUp(self): self.x = np.array([1, 2, 3]) self.y = np.array([1, 2, 3]) self.first_likelihood = GaussianLikelihood( x=self.x, y=self.y, func=lambda x, param1, param2: (param1 + param2) * x, sigma=1, ) self.second_likelihood = PoissonLikelihood( x=self.x, y=self.y, func=lambda x, param2, param3: (param2 + param3) * x ) self.third_likelihood = ExponentialLikelihood( x=self.x, y=self.y, func=lambda x, param4, param5: (param4 + param5) * x ) self.joint_likelihood = JointLikelihood( self.first_likelihood, self.second_likelihood, self.third_likelihood ) self.first_likelihood.parameters["param1"] = 1 self.first_likelihood.parameters["param2"] = 2 self.second_likelihood.parameters["param2"] = 2 self.second_likelihood.parameters["param3"] = 3 self.third_likelihood.parameters["param4"] = 4 self.third_likelihood.parameters["param5"] = 5 self.joint_likelihood.parameters["param1"] = 1 self.joint_likelihood.parameters["param2"] = 2 self.joint_likelihood.parameters["param3"] = 3 self.joint_likelihood.parameters["param4"] = 4 self.joint_likelihood.parameters["param5"] = 5
def test_log_likelihood_dummy(self): """ Merely tests if it goes into the right if else bracket """ poisson_likelihood = PoissonLikelihood( x=self.x, y=self.y, func=lambda x: np.linspace(1, 100, self.N)) with mock.patch('numpy.sum') as m: m.return_value = 1 self.assertEqual(1, poisson_likelihood.log_likelihood())
def setUp(self): self.N = 100 self.mu = 5 self.x = np.linspace(0, 1, self.N) self.y = np.random.poisson(self.mu, self.N) self.yfloat = np.copy(self.y) * 1.0 self.yneg = np.copy(self.y) self.yneg[0] = -1 def test_function(x, c): return c def test_function_array(x, c): return np.ones(len(x)) * c self.function = test_function self.function_array = test_function_array self.poisson_likelihood = PoissonLikelihood(self.x, self.y, self.function)
# get radioactive counts counts = np.random.poisson(rates) theoretical = decay_rate(delta_t, **injection_parameters) # We quickly plot the data to check it looks sensible fig, ax = plt.subplots() ax.semilogy(time[:-1], counts, "o", label="data") ax.semilogy(time[:-1], theoretical, "--r", label="signal") ax.set_xlabel("time") ax.set_ylabel("counts") ax.legend() fig.savefig("{}/{}_data.png".format(outdir, label)) # Now lets instantiate a version of the Poisson Likelihood, giving it # the time intervals, counts and rate model likelihood = PoissonLikelihood(delta_t, counts, decay_rate) # Make the prior priors = dict() priors["halflife"] = LogUniform(1e-5, 1e5, latex_label="$t_{1/2}$", unit="min") priors["n_init"] = LogUniform( 1e-25 / atto, 1e-10 / atto, latex_label="$N_0$", unit="attomole" ) # And run sampler result = bilby.run_sampler( likelihood=likelihood, priors=priors, sampler="dynesty", sample="unif", nlive=1000,
def test_repr(self): likelihood = PoissonLikelihood(self.x, self.y, self.function) expected = "PoissonLikelihood(x={}, y={}, func={})".format( self.x, self.y, self.function.__name__ ) self.assertEqual(expected, repr(likelihood))
def test_log_likelihood_zero_func_return_element(self): poisson_likelihood = PoissonLikelihood( x=self.x, y=self.y, func=lambda x: np.array([3, 6, 0]) ) self.assertEqual(-np.inf, poisson_likelihood.log_likelihood())
def test_log_likelihood_negative_func_return_element(self): poisson_likelihood = PoissonLikelihood( x=self.x, y=self.y, func=lambda x: np.array([3, 6, -2]) ) with self.assertRaises(ValueError): poisson_likelihood.log_likelihood()
def test_log_likelihood_wrong_func_return_type(self): poisson_likelihood = PoissonLikelihood( x=self.x, y=self.y, func=lambda x: "test" ) with self.assertRaises(ValueError): poisson_likelihood.log_likelihood()
def test_neg_rate_array(self): likelihood = PoissonLikelihood(self.x, self.y, self.function_array) likelihood.parameters["c"] = -2 with self.assertRaises(ValueError): likelihood.log_likelihood()
def test_init__y_negative(self): with self.assertRaises(ValueError): PoissonLikelihood(self.x, self.yneg, self.function)
def test_init_y_non_integer(self): with self.assertRaises(ValueError): PoissonLikelihood(self.x, self.yfloat, self.function)