Skip to content

Commit

Permalink
Add unit test for mean gaussian likelihood
Browse files Browse the repository at this point in the history
  • Loading branch information
gschwefer committed Aug 29, 2023
1 parent 327a49a commit dedd180
Showing 1 changed file with 25 additions and 3 deletions.
28 changes: 25 additions & 3 deletions ctapipe/image/tests/test_pixel_likelihood.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,43 @@ def test_chi_squared():


def test_mean_poisson_likelihoood_gaussian():
prediction = np.array([1, 1, 1], dtype="float")
prediction = np.array([50, 50, 50], dtype="float")
spe = 0.5

small_mean_likelihood = mean_poisson_likelihood_gaussian(prediction, spe, 0)
large_mean_likelihood = mean_poisson_likelihood_gaussian(prediction, spe, 1)

assert small_mean_likelihood < large_mean_likelihood

# Test that the mean likelihood of abunch of samples drawn from the gaussian
# behind the aprroximate log likelihood is indeed the precalculated mean

rng = np.random.default_rng(123456)

ped = 1

mean_likelihood = mean_poisson_likelihood_gaussian(prediction[0], spe, ped)

distribution_width = np.sqrt(ped**2 + prediction[0] * (1 + spe**2))

normal_samples = rng.normal(
loc=prediction[0], scale=distribution_width, size=100000
)

rel_diff = (
2 * neg_log_likelihood_approx(normal_samples, prediction[0], spe, ped) / 100000
- mean_likelihood
) / mean_likelihood

assert np.abs(rel_diff) < 5e-4


def test_mean_poisson_likelihood_full():
prediction = np.array([30.0, 30.0])
prediction = np.array([10.0, 10.0])

spe = np.array([0.5])

small_mean_likelihood = mean_poisson_likelihood_full(prediction, spe, [0])
small_mean_likelihood = mean_poisson_likelihood_full(prediction, spe, [0.1])
large_mean_likelihood = mean_poisson_likelihood_full(prediction, spe, [1])

assert small_mean_likelihood < large_mean_likelihood
Expand Down

0 comments on commit dedd180

Please sign in to comment.