def testPoisson(self): """Check output on a continuous Poisson-like distribution""" l = 4 #Expected number of counts poiss = lambda k: l**k * math.exp(-l) / scipy.special.gamma(k + 1) mean = lambda x: sum(x) / len(x) if self.long_test: poiss_values = toolbox.dist_to_list(poiss, 5000, 0, 100) (ci_low, ci_high) = poppy.boots_ci(poiss_values, 50000, 95.0, mean, seed=0) #IF this were normal (which it isn't), expected confidence interval #3.94456 - 4.05544, in reality should be skewed right self.assertAlmostEqual(3.97171801164, ci_low, places=10) self.assertAlmostEqual(4.08051711605, ci_high, places=10) else: poiss_values = toolbox.dist_to_list(poiss, 100, 0, 100) (ci_low, ci_high) = poppy.boots_ci(poiss_values, 100, 95.0, mean, seed=0) #'Expected' 3.608 - 4.392 self.assertAlmostEqual(3.57505503117, ci_low, places=10) self.assertAlmostEqual(4.4100232162, ci_high, places=10)
def testLogNormal(self): """Check output on a LogNormal""" lnorm = lambda x: math.exp(-1*(math.log(x) - 5.1)**2./(2*0.3**2.) ) \ / (x*math.sqrt(2*math.pi) * 0.3) if self.long_test: dist_values = toolbox.dist_to_list(lnorm, 5000, min=0) (ci_low, ci_high) = poppy.boots_ci(dist_values, 50000, 95.0, numpy.median, seed=0) #confidence interval is given as [exp(mu-sigma*q), exp(mu+sigma*q)] #where q is the (1 - alpha/2) quantile of the standard normal distr. #Here, the theoretical median is 164.022 (6 s.f.) self.assertAlmostEqual(162.31603275984526, ci_low, places=10) self.assertAlmostEqual(165.73324112594128, ci_high, places=10) else: dist_values = toolbox.dist_to_list(lnorm, 100, min=0) (ci_low, ci_high) = poppy.boots_ci(dist_values, 100, 95.0, numpy.median, seed=0) #TODO: the CI for lognorms should be calculated by the given form #currently this is just taken from the test code output #after the distributions have been calculated. The values do fit #with the theoretical median though... self.assertAlmostEqual(152.6079463660717, ci_low, places=10) self.assertAlmostEqual(174.61664971746504, ci_high, places=10)
def testGaussian(self): """Check output on a Gaussian""" gauss = lambda x: math.exp(-(x ** 2) / (2 * 5 ** 2)) / \ (5 * math.sqrt(2 * math.pi)) mean = lambda x: sum(x) / len(x) if self.long_test: gauss_values = toolbox.dist_to_list(gauss, 5000) (ci_low, ci_high, prc) = poppy.boots_ci(gauss_values, 50000, 95.0, mean, seed=0, target=-0.138) #standard error on the mean for 5000 samples, standard dev 5 is #0.0707, 95% confidence interval is 1.96 standard dev or #0.13859292911256332 (+/-) self.assertAlmostEqual(-0.138373060749, ci_low, places=10) self.assertAlmostEqual(0.137630863579, ci_high, places=10) self.assertAlmostEqual(97.453789371746154, prc, places=10) else: gauss_values = toolbox.dist_to_list(gauss, 100) (ci_low, ci_high, prc) = poppy.boots_ci(gauss_values, 100, 95.0, mean, seed=0, target=-1.04) #for 100 samples, stderr is 0.5, 95% is +/- 0.98 self.assertAlmostEqual(-1.03977357727, ci_low, places=10) self.assertAlmostEqual(0.914472603387, ci_high, places=10) self.assertAlmostEqual(97.502939756605045, prc, places=10)
def testDistToList(self): """Convert probability distribution to list of values""" inputs = [[lambda x: x, 10, 0, 10], [lambda x: numpy.exp(-(x ** 2) / (2 * 5 ** 2)) / \ (5 * numpy.sqrt(2 * numpy.pi)), 20], ] outputs = [[2.2360679774998005, 3.8729833462074126, 5.0, 5.91607978309959, 6.708203932499401, 7.416198487095684, 8.06225774829855, 8.66025403784434, 9.219544457292841, 9.746794344808976], [-9.799819946289062, -7.197657585144043, -5.751746901776642, -4.672946454957128, -3.777075132355094, -2.9888006299734116, -2.268810950219631, -1.5931968204677105, -0.9455921351909637, -0.31353388726711273, 0.3135339021682739, 0.9455921053886414, 1.5931968688964844, 2.268810987472534, 2.988800525665283, 3.7770752906799316, 4.672946453094482, 5.751747131347656, 7.197657346725464, 9.799819946289062] ] for (input, output) in zip(inputs, outputs): numpy.testing.assert_almost_equal(output, tb.dist_to_list(*input))
def testDistToList(self): """Convert probability distribution to list of values""" inputs = [[lambda x: x, 10, 0, 10], [lambda x: numpy.exp(-(x ** 2) / (2 * 5 ** 2)) / \ (5 * numpy.sqrt(2 * numpy.pi)), 20], ] outputs = [[2.2360679774998005, 3.8729833462074126, 5.0, 5.91607978309959, 6.708203932499401, 7.416198487095684, 8.06225774829855, 8.66025403784434, 9.219544457292841, 9.746794344808976], [-9.799819946289062, -7.197657585144043, -5.751746901776642, -4.672946454957128, -3.777075132355094, -2.9888006299734116, -2.268810950219631, -1.5931968204677105, -0.9455921351909637, -0.31353388726711273, 0.3135339021682739, 0.9455921053886414, 1.5931968688964844, 2.268810987472534, 2.988800525665283, 3.7770752906799316, 4.672946453094482, 5.751747131347656, 7.197657346725464, 9.799819946289062] ] for (input, output) in zip(inputs, outputs): numpy.testing.assert_almost_equal(output, tb.dist_to_list(*input))
def testPoisson(self): """Check output on a continuous Poisson-like distribution""" l = 4 #Expected number of counts poiss = lambda k: l **k * math.exp(-l) / scipy.special.gamma(k + 1) mean = lambda x: sum(x) / len(x) if self.long_test: poiss_values = toolbox.dist_to_list(poiss, 5000, 0, 100) (ci_low, ci_high) = poppy.boots_ci(poiss_values, 50000, 95.0, mean, seed=0) #IF this were normal (which it isn't), expected confidence interval #3.94456 - 4.05544, in reality should be skewed right self.assertAlmostEqual(3.97171801164, ci_low, places=10) self.assertAlmostEqual(4.08051711605, ci_high, places=10) else: poiss_values = toolbox.dist_to_list(poiss, 100, 0, 100) (ci_low, ci_high) = poppy.boots_ci(poiss_values, 100, 95.0, mean, seed=0) #'Expected' 3.608 - 4.392 self.assertAlmostEqual(3.57505503117, ci_low, places=10) self.assertAlmostEqual(4.4100232162, ci_high, places=10)
def testGaussian(self): """Check output on a Gaussian""" gauss = lambda x: math.exp(-(x ** 2) / (2 * 5 ** 2)) / \ (5 * math.sqrt(2 * math.pi)) mean = lambda x: sum(x) / len(x) if self.long_test: gauss_values = toolbox.dist_to_list(gauss, 5000) (ci_low, ci_high, prc) = poppy.boots_ci( gauss_values, 50000, 95.0, mean, seed=0, target=-0.138) #standard error on the mean for 5000 samples, standard dev 5 is #0.0707, 95% confidence interval is 1.96 standard dev or #0.13859292911256332 (+/-) self.assertAlmostEqual(-0.138373060749, ci_low, places=10) self.assertAlmostEqual(0.137630863579, ci_high, places=10) self.assertAlmostEqual(97.453789371746154, prc, places=10) else: gauss_values = toolbox.dist_to_list(gauss, 100) (ci_low, ci_high, prc) = poppy.boots_ci( gauss_values, 100, 95.0, mean, seed=0, target=-1.04) #for 100 samples, stderr is 0.5, 95% is +/- 0.98 self.assertAlmostEqual(-1.03977357727, ci_low, places=10) self.assertAlmostEqual(0.914472603387, ci_high, places=10) self.assertAlmostEqual(97.502939756605045, prc, places=10)
def testLogNormal(self): """Check output on a LogNormal""" lnorm = lambda x: math.exp(-1*(math.log(x) - 5.1)**2./(2*0.3**2.) ) \ / (x*math.sqrt(2*math.pi) * 0.3) if self.long_test: dist_values = toolbox.dist_to_list(lnorm, 5000, min=0) (ci_low, ci_high) = poppy.boots_ci(dist_values, 50000, 95.0, numpy.median, seed=0) #confidence interval is given as [exp(mu-sigma*q), exp(mu+sigma*q)] #where q is the (1 - alpha/2) quantile of the standard normal distr. #Here, the theoretical median is 164.022 (6 s.f.) self.assertAlmostEqual(162.31603275984526, ci_low, places=10) self.assertAlmostEqual(165.73324112594128, ci_high, places=10) else: dist_values = toolbox.dist_to_list(lnorm, 100, min=0) (ci_low, ci_high) = poppy.boots_ci(dist_values, 100, 95.0, numpy.median, seed=0) #TODO: the CI for lognorms should be calculated by the given form #currently this is just taken from the test code output #after the distributions have been calculated. The values do fit #with the theoretical median though... self.assertAlmostEqual(152.6079463660717, ci_low, places=10) self.assertAlmostEqual(174.61664971746504, ci_high, places=10)