Skip to content

Commit

Permalink
Fixed tests, passing now
Browse files Browse the repository at this point in the history
  • Loading branch information
gschwefer committed Aug 24, 2023
1 parent 7b15a90 commit 6766c79
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
5 changes: 3 additions & 2 deletions ctapipe/image/pixel_likelihood.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,9 @@ def neg_log_likelihood_approx(image, prediction, spe_width, pedestal):
"""
theta = pedestal**2 + prediction * (1 + spe_width**2)

# This is really 2 times the full log likelihood
neg_log_l = np.log(2 * np.pi * theta + EPSILON) + (image - prediction) ** 2 / theta
neg_log_l = 0.5 * (
np.log(2 * np.pi * theta + EPSILON) + (image - prediction) ** 2 / theta
)

return np.sum(neg_log_l)

Expand Down
17 changes: 12 additions & 5 deletions ctapipe/image/tests/test_pixel_likelihood.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from ctapipe.image import (
neg_log_likelihood,
neg_log_likelihood_approx,
neg_log_likelihood_numeric,
mean_poisson_likelihood_gaussian,
chi_squared,
mean_poisson_likelihood_full,
Expand Down Expand Up @@ -56,27 +57,33 @@ def test_full_likelihood():

full_like_small = neg_log_likelihood(image_small, expectation_small, spe, pedestal)
exp_diff = full_like_small - np.sum(
np.asarray([2.75630505, 2.62168656, 3.39248449])
np.asarray([1.37815294, 1.31084662, 1.69627197])
)

# Check against known values
assert exp_diff / np.sum(full_like_small) < 1e-4
assert np.abs(exp_diff / np.sum(full_like_small)) < 1e-4

image_large = np.array([40, 50, 60])
expectation_large = np.array([50, 50, 50])

full_like_large = neg_log_likelihood(image_large, expectation_large, spe, pedestal)
# Check against known values
exp_diff = full_like_large - np.sum(
np.asarray([7.45489137, 5.99305388, 7.66226007])
np.asarray([3.72744569, 2.99652694, 3.83113004])
)

assert exp_diff / np.sum(full_like_large) < 1e-4
assert np.abs(exp_diff / np.sum(full_like_large)) < 3e-4

gaus_like_large = neg_log_likelihood_approx(
image_large, expectation_large, spe, pedestal
)

numeric_like_large = neg_log_likelihood_numeric(
image_large, expectation_large, spe, pedestal
)

# Check thats in large signal case the full expectation is equal to the
# gaussian approximation (to 5%)
assert np.all(np.abs((full_like_large - gaus_like_large) / full_like_large) < 0.05)
assert np.all(
np.abs((numeric_like_large - gaus_like_large) / numeric_like_large) < 0.05
)

0 comments on commit 6766c79

Please sign in to comment.