Skip to content

fixes issue #140 to add support for spatial forecasts #142

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion csep/core/poisson_evaluations.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import numpy
import scipy.stats
import scipy.spatial

from csep.models import EvaluationResult
from csep.utils.stats import poisson_joint_log_likelihood_ndarray
Expand Down Expand Up @@ -226,7 +227,7 @@ def poisson_spatial_likelihood(forecast, catalog):
scale = catalog.event_count / forecast.event_count

first_term = -forecast.spatial_counts() * scale
second_term = catalog.spatial_counts() * np.log(forecast.spatial_counts() * scale)
second_term = catalog.spatial_counts() * numpy.log(forecast.spatial_counts() * scale)
third_term = -scipy.special.loggamma(catalog.spatial_counts() + 1)

poll = first_term + second_term + third_term
Expand Down
7 changes: 6 additions & 1 deletion csep/utils/calc.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,12 @@ def bin1d_vec(p, bins, tol=None, right_continuous=False):
bins = numpy.array(bins)
p = numpy.array(p)
a0 = numpy.min(bins)
h = bins[1] - bins[0]
# if user supplies only a single bin, do 2 things: 1) fix right continuous to true, and use of h is arbitrary
if bins.size == 1:
right_continuous = True
h = 1
else:
h = bins[1] - bins[0]

a0_tol = numpy.abs(a0) * numpy.finfo(numpy.float).eps
h_tol = numpy.abs(h) * numpy.finfo(numpy.float).eps
Expand Down
8 changes: 8 additions & 0 deletions tests/test_calc.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,14 @@ def test_bin1d_vec_int(self):
expected = [0, 0, 0, 1, 2]
self.assertListEqual(test.tolist(), expected)

def test_bin1d_single_bin1(self):
data = [-1, 0, 2, 3, 1, 1.5, 1.0, 0.999999999999999]
bin_edges = [1]
# purposely leaving right_continous flag=False bc it should be forced in the bin1d_vec function
test = bin1d_vec(data, bin_edges)
expected = [-1, -1, 0, 0, 0, 0, 0, -1]
self.assertListEqual(test.tolist(), expected)

def test_upper_limit_right_continuous(self):
data = [40, 40, 40]
bin_edges = [0, 10, 20, 30]
Expand Down