Skip to content

Commit

Permalink
Merge pull request #39 from FlorianPfaff/hyperrectangular_uniform_custom
Browse files Browse the repository at this point in the history
Added custom and uniform distributions for hyperrectangles
  • Loading branch information
FlorianPfaff authored May 22, 2023
2 parents 3aefdd1 + b12ff6a commit 1744a0b
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 0 deletions.
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 pyrecest/distributions/hyperrectangular_uniform_distribution.py
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)



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]
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)))


0 comments on commit 1744a0b

Please sign in to comment.