-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #39 from FlorianPfaff/hyperrectangular_uniform_custom
Added custom and uniform distributions for hyperrectangles
- Loading branch information
Showing
4 changed files
with
63 additions
and
0 deletions.
There are no files selected for viewing
7 changes: 7 additions & 0 deletions
7
pyrecest/distributions/custom_hyperrectangular_distribution.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
from .nonperiodic.abstract_hyperrectangular_distribution import AbstractHyperrectangularDistribution | ||
from .custom_distribution import CustomDistribution | ||
|
||
class CustomHyperrectangularDistribution(AbstractHyperrectangularDistribution, CustomDistribution): | ||
def __init__(self, f, bounds): | ||
AbstractHyperrectangularDistribution.__init__(self, bounds) | ||
CustomDistribution.__init__(self, f, self.dim) |
14 changes: 14 additions & 0 deletions
14
pyrecest/distributions/hyperrectangular_uniform_distribution.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
|
||
from .abstract_uniform_distribution import AbstractUniformDistribution | ||
from .nonperiodic.abstract_hyperrectangular_distribution import AbstractHyperrectangularDistribution | ||
|
||
class HyperrectangularUniformDistribution(AbstractUniformDistribution, AbstractHyperrectangularDistribution): | ||
def __init__(self, bounds): | ||
AbstractUniformDistribution.__init__(self, bounds.shape[-1]) | ||
AbstractHyperrectangularDistribution.__init__(self, bounds) | ||
|
||
def get_manifold_size(self): | ||
return AbstractHyperrectangularDistribution.get_manifold_size(self) | ||
|
||
|
||
|
29 changes: 29 additions & 0 deletions
29
pyrecest/distributions/nonperiodic/abstract_hyperrectangular_distribution.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import numpy as np | ||
from ..abstract_bounded_nonperiodic_distribution import AbstractBoundedNonPeriodicDistribution | ||
import scipy | ||
|
||
class AbstractHyperrectangularDistribution(AbstractBoundedNonPeriodicDistribution): | ||
def __init__(self, bounds): | ||
AbstractBoundedNonPeriodicDistribution.__init__(self, np.size(bounds[0])) | ||
self.bounds = bounds | ||
|
||
def get_manifold_size(self): | ||
s = np.prod(np.diff(self.bounds, axis=1)) | ||
return s | ||
|
||
@property | ||
def input_dim(self): | ||
return self.dim | ||
|
||
def integrate(self, integration_boundaries=None): | ||
if integration_boundaries is None: | ||
integration_boundaries = self.bounds | ||
|
||
left = np.atleast_1d(integration_boundaries[0]) | ||
right = np.atleast_1d(integration_boundaries[1]) | ||
|
||
def pdf_fun(*args): | ||
return self.pdf(np.array(args)) | ||
|
||
integration_boundaries = [(left[i], right[i]) for i in range(self.dim)] | ||
return scipy.integrate.nquad(pdf_fun, integration_boundaries)[0] |
13 changes: 13 additions & 0 deletions
13
pyrecest/tests/distributions/test_custom_hyperrectangular_distribution.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import numpy as np | ||
import unittest | ||
from pyrecest.distributions.hyperrectangular_uniform_distribution import HyperrectangularUniformDistribution | ||
from pyrecest.distributions.custom_hyperrectangular_distribution import CustomHyperrectangularDistribution | ||
|
||
class TestCustomHyperrectangularDistribution(unittest.TestCase): | ||
def test_basic(self): | ||
hud = HyperrectangularUniformDistribution(np.array([[1, 3], [2, 5]])) | ||
cd = CustomHyperrectangularDistribution(hud.pdf, hud.bounds) | ||
x_mesh, y_mesh = np.meshgrid(np.linspace(1, 3, 50), np.linspace(2, 5, 50)) | ||
self.assertTrue(np.allclose(cd.pdf(np.column_stack((x_mesh.ravel(), y_mesh.ravel()))), 1/6 * np.ones(50**2))) | ||
|
||
|