Beispiel #1
0
 def test_metric(self):
     r"""
     There are a few ways these functions can get initialized.
     Here we test the varying permutations
     """
     self.int_set = self.emulation_set
     compP.compare(self.left_set, self.right_set)
     compP.compare(self.left_set, self.right_set, 10)
     compP.comparison(self.int_set, self.left_set, self.right_set)
     compP.comparison(self.int_set)
Beispiel #2
0
 def test_aprox_symmetry(self):
     r"""
     Error up to approximation in emulation. We know the expected variance
     given a sample size to be 1/sqrt(N).
     """
     n = 100
     m1 = compP.compare(self.left_set, self.right_set, n)
     d1 = m1.value()
     m2 = compP.compare(self.right_set, self.left_set, n)
     d2 = m2.value()
     nptest.assert_almost_equal(d1 - d2, 0, 1, 'Distance not symmetric.')
Beispiel #3
0
 def test_identity(self):
     r"""
     Ensure passing identical sets returns 0 distance.
     """
     for dist in ['tv', 'norm', '2-norm', 'hell']:
         m = compP.compare(self.left_set, self.left_set)
         d = m.value(dist)
         nptest.assert_equal(d, 0, 'Distance not definite.')
         m = compP.compare(self.left_set, self.left_set)
         d = m.value(dist)
         nptest.assert_equal(d, 0, 'Distance not definite.')
Beispiel #4
0
    def test_exact_symmetry(self):
        r"""
        If two comparison objects are defined with swapped names of
        left and right sample sets, the distance should still be identical
        """

        m1 = compP.comparison(self.int_set, self.left_set, self.right_set)
        m2 = compP.comparison(self.int_set, self.right_set, self.left_set)
        for dist in ['tv', 'mink', '2-norm', 'sqhell']:
            d1 = m1.value(dist)
            d2 = m2.value(dist)
            nptest.assert_almost_equal(d1 - d2, 0, 12,
                                       'Distance %s not symmetric.' % dist)
        import scipy.spatial.distance as ds

        # should be able to overwrite and still get correct answer.
        for dist in ['tv', ds.cityblock]:
            m = compP.compare(self.left_set, self.right_set)
            d1 = m.value(dist)
            m.set_right(self.left_set)
            m.set_left(self.right_set)
            d2 = m.value(dist)
            nptest.assert_almost_equal(d1 - d2, 0, 12,
                                       'Distance not symmetric.')
            # grabbing copies like this should also work.
            ll = m.get_left().copy()
            m.set_left(m.get_right())
            m.set_right(ll)
            d2 = m.value(dist)
            nptest.assert_almost_equal(d1 - d2, 0, 12,
                                       'Distance not symmetric.')
Beispiel #5
0
 def test_discretization(self):
     r"""
     Support for passing discretization objects.
     """
     dl = sample.discretization(self.left_set, self.right_set)
     dr = sample.discretization(self.right_set, self.left_set)
     mm = compP.compare(dl, dr)
     nptest.assert_array_equal(self.mtrc.get_left()._values,
                               mm.get_left()._values)
     nptest.assert_array_equal(self.mtrc.get_right()._values,
                               mm.get_right()._values)
     mm.set_right(dr)  # assuming input sample set
     mm.set_left(dl)
     nptest.assert_array_equal(self.mtrc.get_left()._values,
                               mm.get_left()._values)
     nptest.assert_array_equal(self.mtrc.get_right()._values,
                               mm.get_right()._values)
Beispiel #6
0
and the number of samples will determine the fidelity of the
approximation since we are using voronoi-cell approximations.
"""
num_left_samples = 50
num_right_samples = 50
delta = 0.5  # width of measure's support per dimension
dim = 2
# define two sets that will be compared
L = unit_center_set(dim, num_left_samples, delta)
R = unit_center_set(dim, num_right_samples, delta)

# choose a reference sigma-algebra to compare both solutions
# against (using nearest-neighbor query).
num_comparison_samples = 2000
# the compP.compare method instantiates the compP.comparison class.
mm = compP.compare(L, R, num_comparison_samples)  # initialize metric

# Use existing common library functions

# Use a function of your own!


def inftynorm(x, y):
    """
    Infinity norm between two vectors.
    """
    return np.max(np.abs(x - y))


mm.set_left(unit_center_set(2, 1000, delta / 2))
mm.set_right(unit_center_set(2, 1000, delta))