Ejemplo n.º 1
0
    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
Ejemplo n.º 2
0
 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())
Ejemplo n.º 3
0
    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)
Ejemplo n.º 4
0
# 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,
Ejemplo n.º 5
0
class TestJointLikelihood(unittest.TestCase):
    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 tearDown(self):
        del self.x
        del self.y
        del self.first_likelihood
        del self.second_likelihood
        del self.third_likelihood
        del self.joint_likelihood

    def test_parameters_consistent_from_init(self):
        expected = dict(param1=1, param2=2, param3=3, param4=4, param5=5,)
        self.assertDictEqual(expected, self.joint_likelihood.parameters)

    def test_log_likelihood_correctly_sums(self):
        expected = (
            self.first_likelihood.log_likelihood()
            + self.second_likelihood.log_likelihood()
            + self.third_likelihood.log_likelihood()
        )
        self.assertEqual(expected, self.joint_likelihood.log_likelihood())

    def test_log_likelihood_checks_parameter_updates(self):
        self.first_likelihood.parameters["param2"] = 7
        self.second_likelihood.parameters["param2"] = 7
        self.joint_likelihood.parameters["param2"] = 7
        expected = (
            self.first_likelihood.log_likelihood()
            + self.second_likelihood.log_likelihood()
            + self.third_likelihood.log_likelihood()
        )
        self.assertEqual(expected, self.joint_likelihood.log_likelihood())

    def test_list_element_parameters_are_updated(self):
        self.joint_likelihood.parameters["param2"] = 7
        self.assertEqual(
            self.joint_likelihood.parameters["param2"],
            self.joint_likelihood.likelihoods[0].parameters["param2"],
        )
        self.assertEqual(
            self.joint_likelihood.parameters["param2"],
            self.joint_likelihood.likelihoods[1].parameters["param2"],
        )

    def test_log_noise_likelihood(self):
        self.first_likelihood.noise_log_likelihood = MagicMock(return_value=1)
        self.second_likelihood.noise_log_likelihood = MagicMock(return_value=2)
        self.third_likelihood.noise_log_likelihood = MagicMock(return_value=3)
        self.joint_likelihood = JointLikelihood(
            self.first_likelihood, self.second_likelihood, self.third_likelihood
        )
        expected = (
            self.first_likelihood.noise_log_likelihood()
            + self.second_likelihood.noise_log_likelihood()
            + self.third_likelihood.noise_log_likelihood()
        )
        self.assertEqual(expected, self.joint_likelihood.noise_log_likelihood())

    def test_init_with_list_of_likelihoods(self):
        with self.assertRaises(ValueError):
            JointLikelihood(
                [self.first_likelihood, self.second_likelihood, self.third_likelihood]
            )

    def test_setting_single_likelihood(self):
        self.joint_likelihood.likelihoods = self.first_likelihood
        self.assertEqual(
            self.first_likelihood.log_likelihood(),
            self.joint_likelihood.log_likelihood(),
        )

    def test_setting_likelihood_other(self):
        with self.assertRaises(ValueError):
            self.joint_likelihood.likelihoods = "test"
Ejemplo n.º 6
0
 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))
Ejemplo n.º 7
0
 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())
Ejemplo n.º 8
0
 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()
Ejemplo n.º 9
0
 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()
Ejemplo n.º 10
0
 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()
Ejemplo n.º 11
0
 def test_init__y_negative(self):
     with self.assertRaises(ValueError):
         PoissonLikelihood(self.x, self.yneg, self.function)
Ejemplo n.º 12
0
 def test_init_y_non_integer(self):
     with self.assertRaises(ValueError):
         PoissonLikelihood(self.x, self.yfloat, self.function)
Ejemplo n.º 13
0
class TestPoissonLikelihood(unittest.TestCase):
    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)

    def tearDown(self):
        del self.N
        del self.mu
        del self.x
        del self.y
        del self.yfloat
        del self.yneg
        del self.function
        del self.function_array
        del self.poisson_likelihood

    def test_init_y_non_integer(self):
        with self.assertRaises(ValueError):
            PoissonLikelihood(self.x, self.yfloat, self.function)

    def test_init__y_negative(self):
        with self.assertRaises(ValueError):
            PoissonLikelihood(self.x, self.yneg, self.function)

    def test_neg_rate(self):
        self.poisson_likelihood.parameters["c"] = -2
        with self.assertRaises(ValueError):
            self.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(self):
        self.assertTrue(np.array_equal(self.y, self.poisson_likelihood.y))

    def test_set_y_to_array(self):
        new_y = np.arange(start=0, stop=50, step=2)
        self.poisson_likelihood.y = new_y
        self.assertTrue(np.array_equal(new_y, self.poisson_likelihood.y))

    def test_set_y_to_positive_int(self):
        new_y = 5
        self.poisson_likelihood.y = new_y
        expected_y = np.array([new_y])
        self.assertTrue(np.array_equal(expected_y, self.poisson_likelihood.y))

    def test_set_y_to_negative_int(self):
        with self.assertRaises(ValueError):
            self.poisson_likelihood.y = -5

    def test_set_y_to_float(self):
        with self.assertRaises(ValueError):
            self.poisson_likelihood.y = 5.3

    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_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_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_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 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))