22import numpy
33import unittest
44
5- from csep .core .poisson_evaluations import _simulate_catalog , _poisson_likelihood_test
6- from csep .core .binomial_evaluations import binary_joint_log_likelihood_ndarray
7-
5+ import csep .core .poisson_evaluations as poisson
6+ import csep .core .binomial_evaluations as binary
87
98def get_datadir ():
109 root_dir = os .path .dirname (os .path .abspath (__file__ ))
@@ -48,21 +47,21 @@ def test_simulate_catalog(self):
4847
4948 # this is taken from the test likelihood function
5049 sim_fore = numpy .empty (sampling_weights .shape )
51- sim_fore = _simulate_catalog (num_events , sampling_weights , sim_fore ,
50+ sim_fore = poisson . _simulate_catalog (num_events , sampling_weights , sim_fore ,
5251 random_numbers = self .random_matrix )
5352
5453 # final statement
5554 numpy .testing .assert_allclose (expected_catalog , sim_fore )
5655
5756 # test again to ensure that fill works properply
58- sim_fore = _simulate_catalog (num_events , sampling_weights , sim_fore ,
57+ sim_fore = poisson . _simulate_catalog (num_events , sampling_weights , sim_fore ,
5958 random_numbers = self .random_matrix )
6059
6160 # final statement
6261 numpy .testing .assert_allclose (expected_catalog , sim_fore )
6362
6463 def test_likelihood (self ):
65- qs , obs_ll , simulated_ll = _poisson_likelihood_test (self .forecast_data , self .observed_data , num_simulations = 1 ,
64+ qs , obs_ll , simulated_ll = poisson . _poisson_likelihood_test (self .forecast_data , self .observed_data , num_simulations = 1 ,
6665 random_numbers = self .random_matrix , use_observed_counts = True )
6766
6867 # very basic result to pass "laugh" test
@@ -78,13 +77,42 @@ def test_likelihood(self):
7877class TestBinomialLikelihood (unittest .TestCase ):
7978 def __init__ (self , * args , ** kwargs ):
8079 super ().__init__ (* args , ** kwargs )
80+ self .seed = 0
81+ numpy .random .seed (self .seed )
8182 self .forecast_data = numpy .array ([[0.1 , 0.3 , 0.4 ], [0.2 , 0.1 , 0.1 ]])
8283 self .observed_data = numpy .array ([[0 , 1 , 2 ], [1 , 1 , 0 ]])
84+ self .random_matrix = numpy .random .rand (1 , 9 )
8385
8486 def test_joint_likelihood_calculation (self ):
85- bill = binary_joint_log_likelihood_ndarray (self .forecast_data , self .observed_data )
87+ bill = binary .binary_joint_log_likelihood_ndarray (self .forecast_data , self .observed_data )
88+ numpy .testing .assert_allclose (bill , - 6.7197988064 )
8689
90+ def test_simulate_active_cells (self ):
91+ #With fixed seed we get the same random numbers if we get all the number at once or one by one.
92+ #Making sure random number generated by seed 0 match.
93+ expected_random_numbers = numpy .array ([[0.5488135 , 0.71518937 , 0.60276338 , 0.54488318 , 0.4236548 , 0.64589411 ,
94+ 0.4375872112626925 , 0.8917730007820798 , 0.9636627605010293 ]])
95+
96+ numpy .testing .assert_allclose (expected_random_numbers , self .random_matrix )
97+
98+ #We can expect the following catalog, if we get the above random numbers.
99+ #We get 4 active cells after 9th random sample.
100+ expected_catalog = [0 , 0 , 1 , 1 , 1 , 1 ]
101+
102+ sampling_weights = numpy .cumsum (self .forecast_data .ravel ()) / numpy .sum (self .forecast_data )
103+ sim_fore = numpy .zeros (sampling_weights .shape )
104+ obs_active_cells = len (numpy .unique (numpy .nonzero (self .observed_data .ravel ())))
105+ #resetting seed again to 0, to make sure _simulate_catalog uses this.
106+ seed = 0
107+ numpy .random .seed (seed )
108+ sim_fore = binary ._simulate_catalog (obs_active_cells , sampling_weights , sim_fore )
109+ numpy .testing .assert_allclose (expected_catalog , sim_fore )
110+
111+ def test_binomial_likelihood (self ):
112+ qs , bill , simulated_ll = binary ._binary_likelihood_test (self .forecast_data ,self .observed_data , num_simulations = 1 ,seed = 0 , verbose = True )
87113 numpy .testing .assert_allclose (bill , - 6.7197988064 )
114+ numpy .testing .assert_allclose (qs , 1 )
115+ numpy .testing .assert_allclose (simulated_ll [0 ], - 7.921741654647629 )
88116
89117
90118if __name__ == '__main__' :
0 commit comments