Skip to content

Commit

Permalink
Add test for cdf, adapt limit of integration to compute cdf.
Browse files Browse the repository at this point in the history
  • Loading branch information
ahaselsteiner committed Jul 27, 2020
1 parent afa9d83 commit 80d067c
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
13 changes: 11 additions & 2 deletions tests/test_distributions.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,12 +138,21 @@ def test_multivariate_cdf(self):
"""
Tests the cdf() function of MulvariateDistribution.
"""
p = self.mul_var_dist.cdf([2, 2])
p = self.mul_var_dist.cdf([2, 2], lower_integration_limit=[0, 0])
self.assertAlmostEqual(p, 0, delta=0.01)

p = self.mul_var_dist.cdf([[2, 20], [2, 16]])
p = self.mul_var_dist.cdf([[2, 20], [2, 16]], lower_integration_limit=[0, 0])
np.testing.assert_allclose(p, [0, 1], atol=0.01)

# Create a bivariate normal distribution, where we know that at the
# peak, the CDF must evaluate to 0.25.
dist = NormalDistribution(shape=None, loc=ConstantParam(1), scale=ConstantParam(1))
dep = (None, None, None)
bivariate_dist = MultivariateDistribution([dist, dist], [dep, dep])

p = bivariate_dist.cdf([1, 1])
self.assertAlmostEqual(p, 0.25, delta=0.001)

def test_multivariate_pdf(self):
"""
Tests the pdf() function of MulvariateDistribution.
Expand Down
13 changes: 8 additions & 5 deletions viroconcom/distributions.py
Original file line number Diff line number Diff line change
Expand Up @@ -782,7 +782,7 @@ def __init__(self, distributions=None, dependencies=None):
if not distributions is None:
self.add_distributions(distributions, dependencies)

def cdf(self, x):
def cdf(self, x, lower_integration_limit=(-np.inf, -np.inf)):
"""
Joint cumulative distribution function.
Expand All @@ -792,6 +792,9 @@ def cdf(self, x):
Points at which to calculate the cdf.
Array is of shape (d, n) with d being the number of variables and
n being the number of points.
lower_integration_limit : array_like, optional
Lower limits for the ingration of the pdf. Must have a length of n.
Defaults to (-np.inf, -np.inf).
Returns
-------
p : array_like
Expand All @@ -805,14 +808,14 @@ def cdf(self, x):
p = np.empty(x1.size)
if x0.size == 1:
p, error = scipy.integrate.nquad(self.pdf_2d, [
[0, x0],
[0, x1]
[lower_integration_limit[0], x0],
[lower_integration_limit[1], x1]
])
else:
for i in range(x0.size):
p[i], error = scipy.integrate.nquad(self.pdf_2d, [
[0, x0[i]],
[0, x1[i]]
[lower_integration_limit[0], x0[i]],
[lower_integration_limit[1], x1[i]]
])
else:
raise NotImplementedError('CDF is currently only implemented for '
Expand Down

0 comments on commit 80d067c

Please sign in to comment.