dendropy.calculate.probability: General Probability

Functions to calculate or draw values from various probability distributions.

dendropy.calculate.probability.binomial_rv(n, p, rng=None)[source]

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).

dendropy.calculate.probability.chisq_pdf(chisq, df)[source]

Returns the probability value associated with the provided chi-square value and df. Adapted from chisq.c in Gary Perlman’s Stat.

dendropy.calculate.probability.exp_pdf(value, rate)[source]

Returns the probability density for an exponential distribution with an intensity of rate, evaluated at value.

dendropy.calculate.probability.geometric_rv(p, rng=None)[source]

Geometric distribution per Devroye, Luc. Non-Uniform Random Variate Generation, 1986, p 500.

dendropy.calculate.probability.hypergeometric_pmf(x, m, n, k)[source]

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)
dendropy.calculate.probability.num_poisson_events(rate, period, rng=None)[source]

Returns the number of events that have occurred in a Poisson process of rate over period.

dendropy.calculate.probability.poisson_pmf(k, rate)[source]

Returns the probability of a number, k, drawn from a Poisson distribution with rate parameter, rate (= 1/mean).

dendropy.calculate.probability.poisson_rv(rate, rng=None)[source]

Returns a random number from a Poisson distribution with rate of rate (mean of 1/rate).

dendropy.calculate.probability.sample_multinomial(probs, rng=None)[source]

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).

dendropy.calculate.probability.weighted_choice(seq, weights, rng=None)[source]

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).

dendropy.calculate.probability.weighted_index_choice(weights, rng=None)[source]

(From: 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.


Returns the probability value associated with the provided z-score. Adapted from z.c in Gary Perlman’s Stat.