Skip to content
Snippets Groups Projects
Commit e5481028 authored by Gregory Ashton's avatar Gregory Ashton
Browse files

Fix error in Categorical prior and add tests

parent 0c1baa01
No related branches found
No related tags found
1 merge request!990Fix error in Categorical prior and add tests
Pipeline #252680 passed
...@@ -1469,7 +1469,7 @@ class Categorical(Prior): ...@@ -1469,7 +1469,7 @@ class Categorical(Prior):
======= =======
Union[float, array_like]: Rescaled probability Union[float, array_like]: Rescaled probability
""" """
return np.round(val * self.maximum) return np.floor(val * (1 + self.maximum))
def prob(self, val): def prob(self, val):
"""Return the prior probability of val. """Return the prior probability of val.
......
...@@ -15,13 +15,19 @@ class TestCategoricalPrior(unittest.TestCase): ...@@ -15,13 +15,19 @@ class TestCategoricalPrior(unittest.TestCase):
self.assertTrue(in_prior) self.assertTrue(in_prior)
def test_array_sample(self): def test_array_sample(self):
categorical_prior = bilby.core.prior.Categorical(3) ncat = 4
N = 1000 categorical_prior = bilby.core.prior.Categorical(ncat)
N = 100000
s = categorical_prior.sample(N) s = categorical_prior.sample(N)
zeros = np.sum(s == 0) zeros = np.sum(s == 0)
ones = np.sum(s == 1) ones = np.sum(s == 1)
twos = np.sum(s == 2) twos = np.sum(s == 2)
self.assertEqual(zeros + ones + twos, N) threes = np.sum(s == 3)
self.assertEqual(zeros + ones + twos + threes, N)
self.assertAlmostEqual(zeros / N, 1 / ncat, places=int(np.log10(np.sqrt(N))))
self.assertAlmostEqual(ones / N, 1 / ncat, places=int(np.log10(np.sqrt(N))))
self.assertAlmostEqual(twos / N, 1 / ncat, places=int(np.log10(np.sqrt(N))))
self.assertAlmostEqual(threes / N, 1 / ncat, places=int(np.log10(np.sqrt(N))))
def test_single_probability(self): def test_single_probability(self):
N = 3 N = 3
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment