def test_mi(): xs = [0, 0, 0, 0, 0, 0] ys = [0, 0, 0, 0, 0, 0] g = re.Remainder(xs, ys, k_max=2) g.pz_xy = np.array([[[1, 0], [0, 0]], [[0, 0], [0, 1]]]) g.pxy = np.array([[0.5, 0], [0, 0.5]]) assert np.allclose(g.mi, np.log(2)), "%0.3f > log(2)" % g.mi assert np.allclose(g.h, 0, atol=0.01), "%0.3f > 0" % g.h
def test_identity(): xs = np.array([0, 1, 0, 1]) ys = np.array([0, 0, 1, 1]) for _ in range(20): g = re.Remainder(xs, ys, k_max=2) zs = g.transform(xs, ys) assert np.all(zs == xs) predict_xs = g.predict(ys, zs) assert np.all(xs == predict_xs), predict_xs
def test_perfect(): xs = np.repeat([0, 0, 1, 1, 1, 0], 5) ys = np.repeat([0, 0, 0, 1, 1, 1], 5) g = re.Remainder(xs, ys, k_max=2) zs = g.transform(xs, ys) print zip(xs, ys, zs) assert np.allclose(g.mi, 0, atol=0.01), "%0.3f > 0" % g.mi assert np.allclose(g.h, 0, atol=0.01), "%0.3f > 0" % g.h predict_xs = g.predict(ys, zs) assert np.all(xs == predict_xs), predict_xs
def test_null(): xs = [0, 0, 0, 0, 0, 0] ys = [0, 0, 0, 0, 0, 0] g = re.Remainder(xs, ys, k_max=2) assert np.isclose(g.mi, 0), "No MI possible: %0.7f > 0" % g.mi assert np.isclose(g.h, 0), "No uncertainty in x anyway: %0.3f > 0" % g.h zs = g.transform(xs, ys) print zip(xs, ys, zs) assert np.all(zs == 0), "z=0 bc there is no signal." predict_xs = g.predict(ys, zs) assert np.all(xs == predict_xs), predict_xs
def test_deterministic(): xs = [0, 0, 1, 1] ys = [0, 0, 1, 1] g = re.Remainder(xs, ys, k_max=2) zs = g.transform(xs, ys) print zip(xs, ys, zs) assert np.allclose(g.mi, 0, atol=0.01), "%0.3f > 0" % g.mi assert np.allclose(g.h, 0, atol=0.01), "%0.3f > 0" % g.h predict_xs = g.predict(ys, zs) assert np.all(xs == predict_xs), predict_xs assert np.all(g.transform([0] * 100, [0] * 100) == g.transform([0], [0]))
def test_probabilistic(): xs = [0, 0, 0, 1, 1, 0] ys = [0, 0, 0, 1, 1, 1] g = re.Remainder(xs, ys, k_max=3) zs = g.transform(xs, ys) print zip(xs, ys, zs) print g.pz_xy assert g.mi < 0.0001, "%0.5f" % g.mi assert g.h < 0.0001, "%0.5f" % g.h predict_xs = g.predict(ys, zs) assert np.all(xs == predict_xs), predict_xs
def test_invertibility(): xs = np.random.randint(0, 5, 100) ys = xs / 2 + np.random.randint(0, 2, 100) g = re.Remainder(xs, ys, k_max=8) zs = g.transform(xs, ys) print 'mi, h', g.mi, g.h print zip(xs, ys, zs) print np.array_str(g.pz_xy, precision=2, suppress_small=True) predict_xs = g.predict(ys, zs) print zip(predict_xs, xs) assert np.all(xs == predict_xs), predict_xs assert g.h < 0.01, "%0.5f" % g.h assert g.mi < 0.1, "%0.5f" % g.mi
def __init__(self, x, **kwargs): k_max = kwargs.pop('k_max', 2) # Sets max cardinality for Remainder objects self.verbose = kwargs.get('verbose', False) self.corex = ce.Corex(**kwargs).fit(x) self.labels = self.corex.labels self.remainders = [ re.Remainder(xs[xs >= 0], self.labels[xs >= 0], k_max=k_max) for xs in x.T ] if self.verbose: print 'z cardinalities', [ r.pz_xy.shape[0] for r in self.remainders ]