def test_fit_normal_to_confidence_interval(self): """fit_normal_to_confidence_interval should return a mean and variance given CI""" #Lets use a normal distribution to generate test values normal_95 = ndtri(0.95) mean = 0 upper = mean + normal_95 lower = mean - normal_95 obs_mean,obs_var =\ fit_normal_to_confidence_interval(upper,lower,confidence=0.95) exp_mean = mean exp_var = 1.0 self.assertFloatEqual(obs_mean,exp_mean) self.assertFloatEqual(obs_var,exp_var) #An alternative normal: normal_99 = ndtri(0.99) mean = 5.0 upper = mean + normal_99 lower = mean - normal_99 obs_mean,obs_var =\ fit_normal_to_confidence_interval(upper,lower,confidence=0.99) exp_mean = mean exp_var = 1.0 self.assertFloatEqual(obs_mean,exp_mean) self.assertFloatEqual(obs_var,exp_var)
def fit_normal_to_confidence_interval(upper,lower,mean=None, confidence = 0.95): """Return the mean and variance for a normal distribution given confidence intervals upper -- upper bound lower -- lower bound mean -- mean, if known. If not specified, will be set to be halfway between upper & lower bounds confidence -- the confidence for this interval, e.g. 0.95 for the 95% confidence intervals """ #Start by calculating Z-scores using the inverse normal distribution, #starting with the given confidence value z = ndtri(confidence) #print "Z:",z #Now we need to fit a normal distribution given the confidence #limits if mean is None: mean = (upper+lower)/2.0 #print "mean:",mean #Z-score = deviation from mean/ stdev (equivalently x-mu/sigma) # Therefore with a little algebra we get stdev = (upper_CI - mean)/Z estimated_stdev = abs(upper-mean)/z #print "stdev:",estimated_stdev variance = estimated_stdev**2 #i.e. sigma^2 #print "var:",variance return mean,variance
def test_ndtri(self): """ndtri should give same result as implementation in cephes""" exp=[-1.79769313486e+308, -2.32634787404, -2.05374891063, -1.88079360815, -1.75068607125, -1.64485362695, -1.5547735946, -1.47579102818, -1.40507156031, -1.34075503369, -1.28155156554, -1.22652812004, -1.17498679207, -1.12639112904, -1.08031934081, -1.03643338949, -0.99445788321, -0.954165253146, -0.915365087843, -0.877896295051, -0.841621233573, -0.806421247018, -0.772193214189, -0.738846849185, -0.70630256284, -0.674489750196, -0.643345405393, -0.612812991017, -0.582841507271, -0.553384719556, -0.524400512708, -0.495850347347, -0.467698799115, -0.439913165673, -0.412463129441, -0.385320466408, -0.358458793251, -0.331853346437, -0.305480788099, -0.279319034447, -0.253347103136, -0.227544976641, -0.201893479142, -0.176374164781, -0.150969215497, -0.125661346855, -0.100433720511, -0.0752698620998, -0.0501535834647, -0.0250689082587, 0.0, 0.0250689082587, 0.0501535834647, 0.0752698620998, 0.100433720511, 0.125661346855, 0.150969215497, 0.176374164781, 0.201893479142, 0.227544976641, 0.253347103136, 0.279319034447, 0.305480788099, 0.331853346437, 0.358458793251, 0.385320466408, 0.412463129441, 0.439913165673, 0.467698799115, 0.495850347347, 0.524400512708, 0.553384719556, 0.582841507271, 0.612812991017, 0.643345405393, 0.674489750196, 0.70630256284, 0.738846849185, 0.772193214189, 0.806421247018, 0.841621233573, 0.877896295051, 0.915365087843, 0.954165253146, 0.99445788321, 1.03643338949, 1.08031934081, 1.12639112904, 1.17498679207, 1.22652812004, 1.28155156554, 1.34075503369, 1.40507156031, 1.47579102818, 1.5547735946, 1.64485362695, 1.75068607125, 1.88079360815, 2.05374891063, 2.32634787404, ] obs = [ndtri(i/100.0) for i in range(100)] self.assertFloatEqual(obs, exp)
def test_ndtri(self): """ndtri should give same result as implementation in cephes""" exp = [ -1.79769313486e+308, -2.32634787404, -2.05374891063, -1.88079360815, -1.75068607125, -1.64485362695, -1.5547735946, -1.47579102818, -1.40507156031, -1.34075503369, -1.28155156554, -1.22652812004, -1.17498679207, -1.12639112904, -1.08031934081, -1.03643338949, -0.99445788321, -0.954165253146, -0.915365087843, -0.877896295051, -0.841621233573, -0.806421247018, -0.772193214189, -0.738846849185, -0.70630256284, -0.674489750196, -0.643345405393, -0.612812991017, -0.582841507271, -0.553384719556, -0.524400512708, -0.495850347347, -0.467698799115, -0.439913165673, -0.412463129441, -0.385320466408, -0.358458793251, -0.331853346437, -0.305480788099, -0.279319034447, -0.253347103136, -0.227544976641, -0.201893479142, -0.176374164781, -0.150969215497, -0.125661346855, -0.100433720511, -0.0752698620998, -0.0501535834647, -0.0250689082587, 0.0, 0.0250689082587, 0.0501535834647, 0.0752698620998, 0.100433720511, 0.125661346855, 0.150969215497, 0.176374164781, 0.201893479142, 0.227544976641, 0.253347103136, 0.279319034447, 0.305480788099, 0.331853346437, 0.358458793251, 0.385320466408, 0.412463129441, 0.439913165673, 0.467698799115, 0.495850347347, 0.524400512708, 0.553384719556, 0.582841507271, 0.612812991017, 0.643345405393, 0.674489750196, 0.70630256284, 0.738846849185, 0.772193214189, 0.806421247018, 0.841621233573, 0.877896295051, 0.915365087843, 0.954165253146, 0.99445788321, 1.03643338949, 1.08031934081, 1.12639112904, 1.17498679207, 1.22652812004, 1.28155156554, 1.34075503369, 1.40507156031, 1.47579102818, 1.5547735946, 1.64485362695, 1.75068607125, 1.88079360815, 2.05374891063, 2.32634787404, ] obs = [ndtri(i / 100.0) for i in range(100)] self.assertFloatEqual(obs, exp)