def test_no_limits(self): P = JointNormal( mu=[1, 1], labels=['x', 'y'], cov=[ [1, 1], [1, 1.01], ], N=10) with self.assertRaises(ValueError): P.probability(x=2, y=2)
def test_correct_value(self): P = JointNormal( mu=[0, 0], labels=['x', 'y'], cov=[ [1, 0], [0, 1], ], N=10) N = stats.norm() self.assertAlmostEqual(P.probability(x__gt=0, y=0), N.cdf(0), places=14) self.assertAlmostEqual(P.probability(x__lt=1, y=0), N.cdf(1), places=14) self.assertAlmostEqual(P.probability(x__gt=5, y=0), 1. - N.cdf(5), places=14)
def test_sums_to_one(self): P = JointNormal( mu=[1, 1], labels=['x', 'y'], cov=[ [1, 1], [1, 1.01], ], N=10) px_gt = P.probability(x__gt=2, y=2) px_lt = P.probability(x__lt=2, y=2) py_gt = P.probability(y__gt=2, x=-20) py_lt = P.probability(y__lt=2, x=-20) self.assertAlmostEqual(sum([px_gt, px_lt]), 1, places=14) self.assertAlmostEqual(sum([py_gt, py_lt]), 1, places=14)
def test_iterable(self): P = JointNormal( mu=[0, 0], labels=['x', 'y'], cov=[ [1, 0], [0, 1], ], N=10) x0 = range(-5, 6) p_gt = np.array(P.probability(x__gt=x0)) p_lt = np.array(P.probability(x__lt=x0, y=2)) p_sum = p_gt + p_lt self.assertTrue(all(round(x, 2) == 1 for x in p_sum)) self.assertAlmostEqual(p_gt[5], .5) self.assertTrue(p_gt[0] > .5) self.assertTrue(p_lt[0] < .5) self.assertTrue(p_gt[-1] < .5) self.assertTrue(p_lt[-1] > .5)
def test_correct_value(self): P = JointNormal( mu=[0, 0], labels=['x', 'y'], cov=[ [1, 0], [0, 1], ], N=10) percentile_value = P.percentile(x=.75) self.assertAlmostEqual(P.probability(x__lt=percentile_value), .75, places=12)
def test_iterable(self): P = JointNormal( mu=[0, 0], labels=['x', 'y'], cov=[ [1, 0], [0, 1], ], N=10) percentiles = [.25, .5, .75] values_at_percentiles = P.percentile(x=percentiles) new_percentiles = P.probability(x__lt=values_at_percentiles) self.assertAlmostEqual(new_percentiles[0], percentiles[0]) self.assertAlmostEqual(new_percentiles[1], percentiles[1]) self.assertAlmostEqual(new_percentiles[2], percentiles[2])