Functions to calculate or draw values from various probability distributions.
Returns the number of successes in a sample of n trials, with the probability of success given by p. Using the BINV algorithm, as given by Kachitvicyanukul, V. and B. Schmeiser. 1988. Binomial random variate generation. Communications of the ACM 31: 216-222. Note: NOT the best algorithm according to the authors of the paper (who present their own as an alternative). Apart from rounding errors accumulating in the loop, it may also take a long time to return a value as n * p become large or even moderate (e.g., n=380 and p=0.8 still).
Returns the probability value associated with the provided chi-square value and df. Adapted from chisq.c in Gary Perlman’s Stat.
Returns the probability density for an exponential distribution with an intensity of rate, evaluated at value.
Geometric distribution per Devroye, Luc. Non-Uniform Random Variate Generation, 1986, p 500. http://cg.scs.carleton.ca/~luc/rnbookindex.html
Given a population consisting of m items of class M and n items of class N, this returns the probability of observing x items of class M when sampling k times without replacement from the entire population (i.e., {M,N})
p(x) = (choose(m, x) * choose(n, k-x)) / choose(m+n, k)
Returns the number of events that have occurred in a Poisson process of rate over period.
Returns the probability of a number, k, drawn from a Poisson distribution with rate parameter, rate (= 1/mean).
Returns a random number from a Poisson distribution with rate of rate (mean of 1/rate).
Returns the index of the probability bin in probs. probs is assumed to sum to 1.0 (all rounding error contributes to the last bin).
Selects an element out of seq, with probabilities of each element given by the list weights (which must be at least as long as the length of seq - 1).
(From: http://eli.thegreenplace.net/2010/01/22/weighted-random-generation-in-python/) The following is a simple function to implement weighted random choice in Python. Given a list of weights, it returns an index randomly, according to these weights [1]. For example, given [2, 3, 5] it returns 0 (the index of the first element) with probability 0.2, 1 with probability 0.3 and 2 with probability 0.5. The weights need not sum up to anything in particular, and can actually be arbitrary Python floating point numbers. If we manage to sort the weights in descending order before passing them to weighted_choice_sub, it will run even faster, since the random call returns a uniformly distributed value and larger chunks of the total weight will be skipped in the beginning.