Beta-binomial distribution¶
See https://en.wikipedia.org/wiki/Beta-binomial_distribution
- mpsci.distributions.betabinomial.cdf(k, n, a, b)¶
Cumulative distribution function of the beta-binomial distribution.
This function uses
mpmath.mp.hyp3f2(a1, a2, a3, b1, b2, z)
. According to the docstring of that function, “Evaluation for|z-1|
small can currently be inaccurate or slow for some parameter combinations”.
- mpsci.distributions.betabinomial.kurtosis(n, a, b)¶
Excess kurtosis of the beta-binomial distribution.
- mpsci.distributions.betabinomial.logpmf(k, n, a, b)¶
Logarithm of PMF of the beta-binomial distribution.
- mpsci.distributions.betabinomial.mean(n, a, b)¶
Mean of the beta-binomial distribution.
- mpsci.distributions.betabinomial.mle(x, *, counts=None, n=None, a=None, b=None)¶
Maximum likelihood estimation for the beta-binomial distribution.
This function does not estimate
n
; a fixed value ofn
must be given.Returns (n, a, b), where each value is the maximum likelihood estimate of the parameter if it was not given in the function call.
If counts is given, it must be a sequence of nonnegative integers with the same length as x. It gives the number of occurrences in the sample of the corresponding value in x.
The function works best when a good initial guess is provided for the parameters to be fit. Use
mpsci.distributions.Initial
to specify an initial guess for the parameter.Examples
>>> from mpmath import mp >>> mp.dps = 25 >>> from mpsci.distributions import betabinomial, Initial
x
is our sample, which we assume is drawn from a beta binomial distribution withn=8
.>>> x = [3, 4, 5, 6, 0, 0, 0, 0, 3, 1, 0, 0, ... 2, 3, 1, 8, 4, 3, 0, 5, 5, 0, 4, 3] ...
Fit both
a` and ``b
. Usea=Initial(0.5)
andb=Initial(1.5)
to provide initial guesses to the numerical optimizer.>>> n1, a1, b1 = betabinomial.mle(x, n=8, a=Initial(0.5), b=Initial(1.5)) >>> a1 >>> mpf('0.6183654180946715501688528635') >>> b1 >>> mpf('1.418640359545716670987007935')
If we know
a
must be 1, we can pass in the parametera=1
to fix the value ofa
.>>> n2, a2, b2 = betabinomial.mle(x, n=8, a=1, b=Initial(1.5))
>>> a2 >>> mpf('1.0') >>> b2 >>> mpf('2.143528853730739316264441767')
The following example is from the wikipedia article “Beta-binomial distribution” (https://en.wikipedia.org/wiki/Beta-binomial_distribution), where the data is described as:
The following data gives the number of male children among the first 12 children of family size 13 in 6115 families taken from hospital records in 19th century Saxony (Sokal and Rohlf, p. 59 from Lindsey). The 13th child is ignored to blunt the effect of families non-randomly stopping when a desired gender is reached.
>>> values = list(range(13)) >>> counts = [3, 24, 104, 286, 670, 1033, 1343, 1112, 829, 478, 181, 45, 7] >>> n, a, b = betabinomial.mle(values, counts=counts, ... n=12, a=Initial(34), b=Initial(32)) ... >>> float(a), float(b) (34.10285761580158, 31.578234155042065)
- mpsci.distributions.betabinomial.nll(x, n, a, b, *, counts=None)¶
Negative log-likelihood of the beta-binomial distribution.
x must be a sequence of nonnegative integers, with
0 <= x[i] <= n
.
- mpsci.distributions.betabinomial.pmf(k, n, a, b)¶
Probability mass function of the beta-binomial distribution.
- mpsci.distributions.betabinomial.sf(k, n, a, b)¶
Survival function of the beta-binomial distribution.
This function uses
mpmath.mp.hyp3f2(a1, a2, a3, b1, b2, z)
. According to the docstring of that function, “Evaluation for|z-1|
small can currently be inaccurate or slow for some parameter combinations”.
- mpsci.distributions.betabinomial.skewness(n, a, b)¶
Skewness of the beta-bimonial distribution.
- mpsci.distributions.betabinomial.support(n, a, b)¶
Support of the beta-binomial distribution.
The support is the integers 0, 1, 2, …, n; this is implemented by returning range(n + 1). That is, the return value is the range instance, not a sequence.
Examples
>>> from mpsci.distributions import betabinomial >>> sup = betabinomial.support(5, 2.5, 3) >>> [k for k in sup] [0, 1, 2, 3, 4, 5]
- mpsci.distributions.betabinomial.var(n, a, b)¶
Variance of the beta-binomial distribution.