Skip to content

Commit ec78dd0

Browse files
authored
Binomial testing (#208)
* added to unit test for binary_joint_LL
1 parent 55f3abd commit ec78dd0

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

csep/core/binomial_evaluations.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,13 +88,13 @@ def binary_joint_log_likelihood_ndarray(forecast, catalog):
8888
It has to be a either zero or positive integer only (No Floating Point)
8989
"""
9090
#First, we mask the forecast in cells where we could find log=0.0 singularities:
91-
forecast_masked = np.ma.masked_where(forecast.ravel() <= 0.0, forecast.ravel())
91+
forecast_masked = numpy.ma.masked_where(forecast.ravel() <= 0.0, forecast.ravel())
9292

9393
#Then, we compute the log-likelihood of observing one or more events given a Poisson distribution, i.e., 1 - Pr(0)
9494
target_idx = numpy.nonzero(catalog.ravel())
9595
y = numpy.zeros(forecast_masked.ravel().shape)
9696
y[target_idx[0]] = 1
97-
first_term = y * (np.log(1.0 - np.exp(-forecast_masked.ravel())))
97+
first_term = y * (numpy.log(1.0 - numpy.exp(-forecast_masked.ravel())))
9898

9999
#Also, we estimate the log-likelihood in cells no events are observed:
100100
second_term = (1-y) * (-forecast_masked.ravel().data)

tests/test_evaluations.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import unittest
44

55
from csep.core.poisson_evaluations import _simulate_catalog, _poisson_likelihood_test
6+
from csep.core.binomial_evaluations import binary_joint_log_likelihood_ndarray
7+
68

79
def get_datadir():
810
root_dir = os.path.dirname(os.path.abspath(__file__))
@@ -71,3 +73,17 @@ def test_likelihood(self):
7173
# calculated by hand given the expected data, see explanation in zechar et al., 2010.
7274
numpy.testing.assert_allclose(simulated_ll[0], -7.178053830347945)
7375

76+
77+
class TestBinomialLikelihood(unittest.TestCase):
78+
def __init__(self, *args, **kwargs):
79+
super().__init__(*args, **kwargs)
80+
self.forecast_data = numpy.array([[0.1, 0.3, 0.4], [0.2, 0.1, 0.1]])
81+
self.observed_data = numpy.array([[0, 1, 2], [1, 1, 0]])
82+
83+
def test_likelihood(self):
84+
bill = binary_joint_log_likelihood_ndarray(self.forecast_data, self.observed_data)
85+
86+
numpy.testing.assert_allclose(bill, -6.7197988064)
87+
88+
if __name__ == '__main__':
89+
unittest.main()

0 commit comments

Comments
 (0)