def testHazard(self): t = [1, 2, 2, 3, 5] haz = Hazard.from_seq(t) # () uses forward to interpolate self.assertAlmostEqual(haz(1), 0.2) self.assertAlmostEqual(haz(2), 0.5) self.assertAlmostEqual(haz(3), 0.5) self.assertAlmostEqual(haz(4), 0) self.assertAlmostEqual(haz(5), 1.0) self.assertAlmostEqual(haz(6), 0) xs = [0, 1, 2, 3, 4, 5, 6] res = haz(xs) for x, y in zip(res, [0, 0.2, 0.5, 0.5, 0, 1, 0]): self.assertAlmostEqual(x, y) cdf = Cdf.from_seq(t) haz2 = cdf.make_hazard() res = haz2(xs) for x, y in zip(res, [0, 0.2, 0.5, 0.5, 0, 1, 0]): self.assertAlmostEqual(x, y) surv = Surv.from_seq(t) haz3 = surv.make_hazard() res = haz3(xs) for x, y in zip(res, [0, 0.2, 0.5, 0.5, 0, 1, 0]): self.assertAlmostEqual(x, y)
def testHazardSampling(self): haz = Hazard.from_seq([1, 2, 3, 4, 5, 6]) expected = [2, 4, 2, 1, 5, 4, 4, 4, 1, 3] np.random.seed(17) a = haz.choice(10) self.assertTrue(np.all((a == expected))) a = haz.sample(10, replace=True, random_state=17) self.assertTrue(np.all((a == expected)))
def testHazardComparison(self): d4 = Hazard.from_seq(range(1,5)) self.assertEqual(d4.gt_dist(2), 0.5) self.assertEqual(d4.gt_dist(d4), 0.375) self.assertEqual(d4.lt_dist(2), 0.25) self.assertEqual(d4.lt_dist(d4), 0.375) self.assertEqual(d4.ge_dist(2), 0.75) self.assertEqual(d4.ge_dist(d4), 0.625) self.assertEqual(d4.le_dist(2), 0.5) self.assertEqual(d4.le_dist(d4), 0.625) self.assertEqual(d4.eq_dist(2), 0.25) self.assertEqual(d4.eq_dist(d4), 0.25) self.assertEqual(d4.ne_dist(2), 0.75) self.assertEqual(d4.ne_dist(d4), 0.75)
def testConversionFunctions(self): t = [1, 2, 2, 3, 5, 5, 7, 10] pmf = Pmf.from_seq(t) cdf = Cdf.from_seq(t) surv = Surv.from_seq(t) haz = Hazard.from_seq(t) cdf2 = pmf.make_cdf() self.almost_equal_dist(cdf, cdf2) surv2 = pmf.make_surv() self.almost_equal_dist(surv, surv2) haz2 = pmf.make_hazard() self.almost_equal_dist(haz, haz2) surv3 = haz2.make_surv() self.almost_equal_dist(surv, surv3) cdf3 = haz2.make_cdf() self.almost_equal_dist(cdf, cdf3) pmf3 = haz2.make_pmf() self.almost_equal_dist(pmf, pmf3)