Пример #1
0
 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)
Пример #2
0
    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)
Пример #3
0
 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)
Пример #4
0
 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)
Пример #5
0
 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)
Пример #6
0
    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)