/usr/lib/python2.7/dist-packages/cogent/maths/stats/distribution.py is in python-cogent 1.9-9.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 | #!/usr/bin/env python
"""Translations of functions from Release 2.3 of the Cephes Math Library,
which is (c) Stephen L. Moshier 1984, 1995.
"""
from __future__ import division
from cogent.maths.stats.special import erf, erfc, igamc, igam, betai, log1p, \
expm1, SQRTH, MACHEP, MAXNUM, PI, ndtri, incbi, igami, fix_rounding_error,\
ln_binomial
#ndtri import b/c it should be available via this module
from numpy import sqrt, exp, arctan as atan
__author__ = "Rob Knight"
__copyright__ = "Copyright 2007-2016, The Cogent Project"
__credits__ = ["Rob Knight", "Sandra Smit", "Gavin Huttley", "Daniel McDonald"]
__license__ = "GPL"
__version__ = "1.9"
__maintainer__ = "Rob Knight"
__email__ = "rob@spot.colorado.edu"
__status__ = "Production"
incbet = betai #shouldn't have renamed it...
#Probability integrals: low gives left-hand tail, high gives right-hand tail.
def z_low(x):
"""Returns left-hand tail of z distribution (0 to x).
x ranges from -infinity to +infinity; result ranges from 0 to 1
See Cephes docs for details."""
y = x * SQRTH
z = abs(y) #distribution is symmetric
if z < SQRTH:
return 0.5 + 0.5 * erf(y)
else:
if y > 0:
return 1 - 0.5 * erfc(z)
else:
return 0.5 * erfc(z)
def z_high(x):
"""Returns right-hand tail of z distribution (0 to x).
x ranges from -infinity to +infinity; result ranges from 0 to 1
See Cephes docs for details."""
y = x * SQRTH
z = abs(y)
if z < SQRTH:
return 0.5 - 0.5 * erf(y)
else:
if x < 0:
return 1 - 0.5 * erfc(z)
else:
return 0.5 * erfc(z)
def zprob(x):
"""Returns both tails of z distribution (-inf to -x, inf to x)."""
return 2 * z_high(abs(x))
def chi_low(x, df):
"""Returns left-hand tail of chi-square distribution (0 to x), given df.
x ranges from 0 to infinity.
df, the degrees of freedom, ranges from 1 to infinity (assume integers).
Typically, df is (r-1)*(c-1) for a r by c table.
Result ranges from 0 to 1.
See Cephes docs for details.
"""
x = fix_rounding_error(x)
if x < 0:
raise ValueError, "chi_low: x must be >= 0 (got %s)." % x
if df < 1:
raise ValueError, "chi_low: df must be >= 1 (got %s)." % df
return igam(df/2, x/2)
def chi_high(x, df):
"""Returns right-hand tail of chi-square distribution (x to infinity).
df, the degrees of freedom, ranges from 1 to infinity (assume integers).
Typically, df is (r-1)*(c-1) for a r by c table.
Result ranges from 0 to 1.
See Cephes docs for details.
"""
x = fix_rounding_error(x)
if x < 0:
raise ValueError, "chi_high: x must be >= 0 (got %s)." % x
if df < 1:
raise ValueError, "chi_high: df must be >= 1 (got %s)." % df
return igamc(df/2, x/2)
def t_low(t, df):
"""Returns left-hand tail of Student's t distribution (-infinity to x).
df, the degrees of freedom, ranges from 1 to infinity.
Typically, df is (n-1) for a sample size of n.
Result ranges from 0 to 1.
See Cephes docs for details.
"""
if df < 1:
raise ValueError, "t_low: df must be >= 1 (got %s)." % df
return stdtr(df, t)
def t_high(t, df):
"""Returns right-hand tail of Student's t distribution (x to infinity).
df, the degrees of freedom, ranges from 1 to infinity.
Typically, df is (n-1) for a sample size of n.
Result ranges from 0 to 1.
See Cephes docs for details.
"""
if df < 1:
raise ValueError, "t_high: df must be >= 1 (got %s)." % df
return stdtr(df, -t) #distribution is symmetric
def tprob(t, df):
"""Returns both tails of t distribution (-infinity to -x, infinity to x)"""
return 2 * t_high(abs(t), df)
def poisson_high(successes, mean):
"""Returns right tail of Poission distribution, Pr(X > x).
successes ranges from 0 to infinity. mean must be positive.
"""
return pdtrc(successes, mean)
def poisson_low(successes, mean):
"""Returns left tail of Poisson distribution, Pr(X <= x).
successes ranges from 0 to infinity. mean must be positive.
"""
return pdtr(successes, mean)
def poisson_exact(successes, mean):
"""Returns Poisson probablity for exactly Pr(X=successes).
Formula is e^-(mean) * mean^(successes) / (successes)!
"""
if successes == 0:
return pdtr(0, mean)
elif successes < mean: #use left tail
return pdtr(successes, mean) - pdtr(successes-1, mean)
else: #successes > mean: use right tail
return pdtrc(successes-1, mean) - pdtrc(successes, mean)
def binomial_high(successes, trials, prob):
"""Returns right-hand binomial tail (X > successes) given prob(success)."""
if -1 <= successes < 0:
return 1
return bdtrc(successes, trials, prob)
def binomial_low(successes, trials, prob):
"""Returns left-hand binomial tail (X <= successes) given prob(success)."""
return bdtr(successes, trials, prob)
def binomial_exact(successes, trials, prob):
"""Returns binomial probability of exactly X successes.
Works for integer and floating point values.
Note: this function is only a probability mass function for integer
values of 'trials' and 'successes', i.e. if you sum up non-integer
values you probably won't get a sum of 1.
"""
if (prob < 0) or (prob > 1):
raise ValueError, "Binomial prob must be between 0 and 1."
if (successes < 0) or (trials < successes):
raise ValueError, "Binomial successes must be between 0 and trials."
return exp(ln_binomial(successes, trials, prob))
def f_low(df1, df2, x):
"""Returns left-hand tail of f distribution (0 to x).
x ranges from 0 to infinity.
Result ranges from 0 to 1.
See Cephes docs for details.
"""
return fdtr(df1, df2, x)
def f_high(df1, df2, x):
"""Returns right-hand tail of f distribution (x to infinity).
Result ranges from 0 to 1.
See Cephes docs for details.
"""
return fdtrc(df1, df2, x)
def fprob(dfn, dfd, F, side='right'):
"""Returns both tails of F distribution (-inf to F and F to inf)
Use in case of two-tailed test. Usually this method is called by
f_two_sample, so you don't have to worry about choosing the right side.
side: right means return twice the right-hand tail of the F-distribution.
Use in case var(a) > var (b)
left means return twice the left-hand tail of the F-distribution.
Use in case var(a) < var(b)
"""
if F < 0:
raise ValueError, "fprob: F must be >= 0 (got %s)." % F
if side=='right':
return 2*f_high(dfn, dfd, F)
elif side=='left':
return 2*f_low(dfn, dfd, F)
else:
raise ValueError, "Not a valid value for side %s"%(side)
def stdtr(k, t):
"""Student's t distribution, -infinity to t.
See Cephes docs for details.
"""
if k <= 0:
raise ValueError, 'stdtr: df must be > 0.'
if t == 0:
return 0.5
if t < -2:
rk = k
z = rk / (rk + t * t)
return 0.5 * betai(0.5 * rk, 0.5, z)
#compute integral from -t to + t
if t < 0:
x = -t
else:
x = t
rk = k #degrees of freedom
z = 1 + (x * x)/rk
#test if k is odd or even
if (k & 1) != 0:
#odd k
xsqk = x/sqrt(rk)
p = atan(xsqk)
if k > 1:
f = 1
tz = 1
j = 3
while (j <= (k-2)) and ((tz/f) > MACHEP):
tz *= (j-1)/(z*j)
f += tz
j += 2
p += f * xsqk/z
p *= 2/PI
else:
#even k
f = 1
tz = 1
j = 2
while (j <= (k-2)) and ((tz/f) > MACHEP):
tz *= (j-1)/(z*j)
f += tz
j += 2
p = f * x/sqrt(z*rk)
#common exit
if t < 0:
p = -p #note destruction of relative accuracy
p = 0.5 + 0.5 * p
return p
def bdtr(k, n, p):
"""Binomial distribution, 0 through k.
Uses formula bdtr(k, n, p) = betai(n-k, k+1, 1-p)
See Cephes docs for details.
"""
p = fix_rounding_error(p)
if (p < 0) or (p > 1):
raise ValueError, "Binomial p must be between 0 and 1."
if (k < 0) or (n < k):
raise ValueError, "Binomial k must be between 0 and n."
if k == n:
return 1
dn = n - k
if k == 0:
return pow(1-p, dn)
else:
return betai(dn, k+1, 1-p)
def bdtrc(k, n, p):
"""Complement of binomial distribution, k+1 through n.
Uses formula bdtrc(k, n, p) = betai(k+1, n-k, p)
See Cephes docs for details.
"""
p = fix_rounding_error(p)
if (p < 0) or (p > 1):
raise ValueError, "Binomial p must be between 0 and 1."
if (k < 0) or (n < k):
raise ValueError, "Binomial k must be between 0 and n."
if k == n:
return 0
dn = n - k
if k == 0:
if p < .01:
dk = -expm1(dn * log1p(-p))
else:
dk = 1 - pow(1.0-p, dn)
else:
dk = k + 1
dk = betai(dk, dn, p)
return dk
def pdtr(k, m):
"""Returns sum of left tail of Poisson distribution, 0 through k.
See Cephes docs for details.
"""
if k < 0:
raise ValueError, "Poisson k must be >= 0."
if m < 0:
raise ValueError, "Poisson m must be >= 0."
return igamc(k+1, m)
def pdtrc(k, m):
"""Returns sum of right tail of Poisson distribution, k+1 through infinity.
See Cephes docs for details.
"""
if k < 0:
raise ValueError, "Poisson k must be >= 0."
if m < 0:
raise ValueError, "Poisson m must be >= 0."
return igam(k+1, m)
def fdtr(a, b, x):
"""Returns left tail of F distribution, 0 to x.
See Cephes docs for details.
"""
if min(a, b) < 1:
raise ValueError, "F a and b (degrees of freedom) must both be >= 1."
if x < 0:
raise ValueError, "F distribution value of f must be >= 0."
w = a * x
w /= float(b + w)
return betai(0.5 * a, 0.5 * b, w)
def fdtrc(a, b, x):
"""Returns right tail of F distribution, x to infinity.
See Cephes docs for details.
"""
if min(a, b) < 1:
raise ValueError, "F a and b (degrees of freedom) must both be >= 1."
if x < 0:
raise ValueError, "F distribution value of f must be >= 0."
w = float(b) / (b + a * x)
return betai(0.5 * b, 0.5 * a, w)
def gdtr(a, b, x):
"""Returns integral from 0 to x of Gamma distribution with params a and b.
"""
if x < 0.0:
raise ZeroDivisionError, "x must be at least 0."
return igam( b, a * x)
def gdtrc(a, b, x):
"""Returns integral from x to inf of Gamma distribution with params a and b.
"""
if x < 0.0:
raise ZeroDivisionError, "x must be at least 0."
return igamc(b, a * x)
#note: ndtri for the normal distribution is already imported
def chdtri(df, y):
"""Returns inverse of chi-squared distribution."""
y = fix_rounding_error(y)
if(y < 0.0 or y > 1.0 or df < 1.0):
raise ZeroDivisionError, "y must be between 0 and 1; df >= 1"
return 2 * igami(0.5*df, y)
def stdtri(k, p):
"""Returns inverse of Student's t distribution. k = df."""
p = fix_rounding_error(p)
# handle easy cases
if k <= 0 or p < 0.0 or p > 1.0:
raise ZeroDivisionError, "k must be >= 1, p between 1 and 0."
rk = k
#handle intermediate values
if p > 0.25 and p < 0.75:
if p == 0.5:
return 0.0
z = 1.0 - 2.0 * p;
z = incbi(0.5, 0.5*rk, abs(z))
t = sqrt(rk*z/(1.0-z))
if p < 0.5:
t = -t
return t
#handle extreme values
rflg = -1
if p >= 0.5:
p = 1.0 - p;
rflg = 1
z = incbi(0.5*rk, 0.5, 2.0*p)
if MAXNUM * z < rk:
return rflg * MAXNUM
t = sqrt(rk/z - rk)
return rflg * t
def pdtri(k, p):
"""Inverse of Poisson distribution.
Finds Poission mean such that integral from 0 to k is p.
"""
p = fix_rounding_error(p)
if k < 0 or p < 0.0 or p >= 1.0:
raise ZeroDivisionError, "k must be >=0, p between 1 and 0."
v = k+1;
return igami(v, p)
def bdtri(k, n, y):
"""Inverse of binomial distribution.
Finds binomial p such that sum of terms 0-k reaches cum probability y.
"""
y = fix_rounding_error(y)
if y < 0.0 or y > 1.0:
raise ZeroDivisionError, "y must be between 1 and 0."
if k < 0 or n <= k:
raise ZeroDivisionError, "k must be between 0 and n"
dn = n - k
if k == 0:
if y > 0.8:
p = -expm1(log1p(y-1.0) / dn)
else:
p = 1.0 - y**(1.0/dn)
else:
dk = k + 1;
p = incbet(dn, dk, 0.5)
if p > 0.5:
p = incbi(dk, dn, 1.0-y)
else:
p = 1.0 - incbi(dn, dk, y)
return p
def gdtri(a, b, y):
"""Returns Gamma such that y is the probability in the integral.
WARNING: if 1-y == 1, gives incorrect result. The scipy implementation
gets around this by using cdflib, which is in Fortran. Until someone
gets around to translating that, only use this function for values of
p greater than 1e-15 or so!
"""
y = fix_rounding_error(y)
if y < 0.0 or y > 1.0 or a <= 0.0 or b < 0.0:
raise ZeroDivisionError, "a and b must be non-negative, y from 0 to 1."
return igami(b, 1.0-y) / a
def fdtri(a, b, y):
"""Returns inverse of F distribution."""
y = fix_rounding_error(y)
if( a < 1.0 or b < 1.0 or y <= 0.0 or y > 1.0):
raise ZeroDivisionError, "y must be between 0 and 1; a and b >= 1"
y = 1.0-y
# Compute probability for x = 0.5
w = incbet(0.5*b, 0.5*a, 0.5)
# If that is greater than y, then the solution w < .5.
# Otherwise, solve at 1-y to remove cancellation in (b - b*w).
if w > y or y < 0.001:
w = incbi(0.5*b, 0.5*a, y)
x = (b - b*w)/(a*w)
else:
w = incbi(0.5*a, 0.5*b, 1.0-y)
x = b*w/(a*(1.0-w))
return x
|