# Discrete¶

 Binomial(n, p, *args, **kwargs) Binomial log-likelihood. ZeroInflatedBinomial(psi, n, p, *args, **kwargs) Zero-inflated Binomial log-likelihood. BetaBinomial(alpha, beta, n, *args, **kwargs) Beta-binomial log-likelihood. Bernoulli([p, logit_p]) Bernoulli log-likelihood Poisson(mu, *args, **kwargs) Poisson log-likelihood. ZeroInflatedPoisson(psi, theta, *args, **kwargs) Zero-inflated Poisson log-likelihood. NegativeBinomial(mu, alpha, *args, **kwargs) Negative binomial log-likelihood. ZeroInflatedNegativeBinomial(psi, mu, alpha, …) Zero-Inflated Negative binomial log-likelihood. DiscreteUniform(lower, upper, *args, **kwargs) Discrete uniform distribution. Geometric(p, *args, **kwargs) Geometric log-likelihood. Categorical(p, *args, **kwargs) Categorical log-likelihood. DiscreteWeibull(q, beta, *args, **kwargs) Discrete Weibull log-likelihood Constant(c, *args, **kwargs) Constant log-likelihood. OrderedLogistic(eta, cutpoints, *args, **kwargs) Ordered Logistic log-likelihood.
class pymc3.distributions.discrete.Binomial(n, p, *args, **kwargs)

Binomial log-likelihood.

The discrete probability distribution of the number of successes in a sequence of n independent yes/no experiments, each of which yields success with probability p. The pmf of this distribution is

$f(x \mid n, p) = \binom{n}{x} p^x (1-p)^{n-x}$
 Support $$x \in \{0, 1, \ldots, n\}$$ Mean $$n p$$ Variance $$n p (1 - p)$$
Parameters
nint

Number of Bernoulli trials (n >= 0).

pfloat

Probability of success in each trial (0 < p < 1).

logp(self, value)

Calculate log-probability of Binomial distribution at specified value.

Parameters
valuenumeric

Value(s) for which log-probability is calculated. If the log probabilities for multiple values are desired the values must be provided in a numpy array or theano tensor

Returns
TensorVariable
random(self, point=None, size=None)

Draw random values from Binomial distribution.

Parameters
pointdict, optional

Dict of variable values on which random values are to be conditioned (uses default point if not specified).

sizeint, optional

Desired size of random sample (returns one sample if not specified).

Returns
array
class pymc3.distributions.discrete.BetaBinomial(alpha, beta, n, *args, **kwargs)

Beta-binomial log-likelihood.

Equivalent to binomial random variable with success probability drawn from a beta distribution. The pmf of this distribution is

$f(x \mid \alpha, \beta, n) = \binom{n}{x} \frac{B(x + \alpha, n - x + \beta)}{B(\alpha, \beta)}$
 Support $$x \in \{0, 1, \ldots, n\}$$ Mean $$n \dfrac{\alpha}{\alpha + \beta}$$ Variance $$n \dfrac{\alpha \beta}{(\alpha+\beta)^2 (\alpha+\beta+1)}$$
Parameters
nint

Number of Bernoulli trials (n >= 0).

alphafloat

alpha > 0.

betafloat

beta > 0.

logp(self, value)

Calculate log-probability of BetaBinomial distribution at specified value.

Parameters
valuenumeric

Value(s) for which log-probability is calculated. If the log probabilities for multiple values are desired the values must be provided in a numpy array or theano tensor

Returns
TensorVariable
random(self, point=None, size=None)

Draw random values from BetaBinomial distribution.

Parameters
pointdict, optional

Dict of variable values on which random values are to be conditioned (uses default point if not specified).

sizeint, optional

Desired size of random sample (returns one sample if not specified).

Returns
array
class pymc3.distributions.discrete.Bernoulli(p=None, logit_p=None, *args, **kwargs)

Bernoulli log-likelihood

The Bernoulli distribution describes the probability of successes (x=1) and failures (x=0). The pmf of this distribution is

$f(x \mid p) = p^{x} (1-p)^{1-x}$
 Support $$x \in \{0, 1\}$$ Mean $$p$$ Variance $$p (1 - p)$$
Parameters
pfloat

Probability of success (0 < p < 1).

logit_pfloat

Logit of success probability. Only one of p and logit_p can be specified.

logp(self, value)

Calculate log-probability of Bernoulli distribution at specified value.

Parameters
valuenumeric

Value(s) for which log-probability is calculated. If the log probabilities for multiple values are desired the values must be provided in a numpy array or theano tensor

Returns
TensorVariable
random(self, point=None, size=None)

Draw random values from Bernoulli distribution.

Parameters
pointdict, optional

Dict of variable values on which random values are to be conditioned (uses default point if not specified).

sizeint, optional

Desired size of random sample (returns one sample if not specified).

Returns
array
class pymc3.distributions.discrete.DiscreteWeibull(q, beta, *args, **kwargs)

Discrete Weibull log-likelihood

The discrete Weibull distribution is a flexible model of count data that can handle both over- and under-dispersion. The pmf of this distribution is

$f(x \mid q, \beta) = q^{x^{\beta}} - q^{(x + 1)^{\beta}}$
 Support $$x \in \mathbb{N}_0$$ Mean $$\mu = \sum_{x = 1}^{\infty} q^{x^{\beta}}$$ Variance $$2 \sum_{x = 1}^{\infty} x q^{x^{\beta}} - \mu - \mu^2$$
logp(self, value)

Calculate log-probability of DiscreteWeibull distribution at specified value.

Parameters
valuenumeric

Value(s) for which log-probability is calculated. If the log probabilities for multiple values are desired the values must be provided in a numpy array or theano tensor

Returns
TensorVariable
random(self, point=None, size=None)

Draw random values from DiscreteWeibull distribution.

Parameters
pointdict, optional

Dict of variable values on which random values are to be conditioned (uses default point if not specified).

sizeint, optional

Desired size of random sample (returns one sample if not specified).

Returns
array
class pymc3.distributions.discrete.Poisson(mu, *args, **kwargs)

Poisson log-likelihood.

Often used to model the number of events occurring in a fixed period of time when the times at which events occur are independent. The pmf of this distribution is

$f(x \mid \mu) = \frac{e^{-\mu}\mu^x}{x!}$
 Support $$x \in \mathbb{N}_0$$ Mean $$\mu$$ Variance $$\mu$$
Parameters
mufloat

Expected number of occurrences during the given interval (mu >= 0).

Notes

The Poisson distribution can be derived as a limiting case of the binomial distribution.

logp(self, value)

Calculate log-probability of Poisson distribution at specified value.

Parameters
valuenumeric

Value(s) for which log-probability is calculated. If the log probabilities for multiple values are desired the values must be provided in a numpy array or theano tensor

Returns
TensorVariable
random(self, point=None, size=None)

Draw random values from Poisson distribution.

Parameters
pointdict, optional

Dict of variable values on which random values are to be conditioned (uses default point if not specified).

sizeint, optional

Desired size of random sample (returns one sample if not specified).

Returns
array
class pymc3.distributions.discrete.NegativeBinomial(mu, alpha, *args, **kwargs)

Negative binomial log-likelihood.

The negative binomial distribution describes a Poisson random variable whose rate parameter is gamma distributed. The pmf of this distribution is

$f(x \mid \mu, \alpha) = \binom{x + \alpha - 1}{x} (\alpha/(\mu+\alpha))^\alpha (\mu/(\mu+\alpha))^x$
 Support $$x \in \mathbb{N}_0$$ Mean $$\mu$$
Parameters
mufloat

Poission distribution parameter (mu > 0).

alphafloat

Gamma distribution parameter (alpha > 0).

logp(self, value)

Calculate log-probability of NegativeBinomial distribution at specified value.

Parameters
valuenumeric

Value(s) for which log-probability is calculated. If the log probabilities for multiple values are desired the values must be provided in a numpy array or theano tensor

Returns
TensorVariable
random(self, point=None, size=None)

Draw random values from NegativeBinomial distribution.

Parameters
pointdict, optional

Dict of variable values on which random values are to be conditioned (uses default point if not specified).

sizeint, optional

Desired size of random sample (returns one sample if not specified).

Returns
array
pymc3.distributions.discrete.ConstantDist
class pymc3.distributions.discrete.Constant(c, *args, **kwargs)

Constant log-likelihood.

Parameters
valuefloat or int

Constant parameter.

logp(self, value)

Calculate log-probability of Constant distribution at specified value.

Parameters
valuenumeric

Value(s) for which log-probability is calculated. If the log probabilities for multiple values are desired the values must be provided in a numpy array or theano tensor

Returns
TensorVariable
random(self, point=None, size=None)

Draw random values from Constant distribution.

Parameters
pointdict, optional

Dict of variable values on which random values are to be conditioned (uses default point if not specified).

sizeint, optional

Desired size of random sample (returns one sample if not specified).

Returns
array
class pymc3.distributions.discrete.ZeroInflatedPoisson(psi, theta, *args, **kwargs)

Zero-inflated Poisson log-likelihood.

Often used to model the number of events occurring in a fixed period of time when the times at which events occur are independent. The pmf of this distribution is

$\begin{split}f(x \mid \psi, \theta) = \left\{ \begin{array}{l} (1-\psi) + \psi e^{-\theta}, \text{if } x = 0 \\ \psi \frac{e^{-\theta}\theta^x}{x!}, \text{if } x=1,2,3,\ldots \end{array} \right.\end{split}$
 Support $$x \in \mathbb{N}_0$$ Mean $$\psi\theta$$ Variance $$\theta + \frac{1-\psi}{\psi}\theta^2$$
Parameters
psifloat

Expected proportion of Poisson variates (0 < psi < 1)

thetafloat

Expected number of occurrences during the given interval (theta >= 0).

logp(self, value)

Calculate log-probability of ZeroInflatedPoisson distribution at specified value.

Parameters
valuenumeric

Value(s) for which log-probability is calculated. If the log probabilities for multiple values are desired the values must be provided in a numpy array or theano tensor

Returns
TensorVariable
random(self, point=None, size=None)

Draw random values from ZeroInflatedPoisson distribution.

Parameters
pointdict, optional

Dict of variable values on which random values are to be conditioned (uses default point if not specified).

sizeint, optional

Desired size of random sample (returns one sample if not specified).

Returns
array
class pymc3.distributions.discrete.ZeroInflatedBinomial(psi, n, p, *args, **kwargs)

Zero-inflated Binomial log-likelihood.

The pmf of this distribution is

$\begin{split}f(x \mid \psi, n, p) = \left\{ \begin{array}{l} (1-\psi) + \psi (1-p)^{n}, \text{if } x = 0 \\ \psi {n \choose x} p^x (1-p)^{n-x}, \text{if } x=1,2,3,\ldots,n \end{array} \right.\end{split}$
 Support $$x \in \mathbb{N}_0$$ Mean $$(1 - \psi) n p$$ Variance $$(1-\psi) n p [1 - p(1 - \psi n)].$$
Parameters
psifloat

Expected proportion of Binomial variates (0 < psi < 1)

nint

Number of Bernoulli trials (n >= 0).

pfloat

Probability of success in each trial (0 < p < 1).

logp(self, value)

Calculate log-probability of ZeroInflatedBinomial distribution at specified value.

Parameters
valuenumeric

Value(s) for which log-probability is calculated. If the log probabilities for multiple values are desired the values must be provided in a numpy array or theano tensor

Returns
TensorVariable
random(self, point=None, size=None)

Draw random values from ZeroInflatedBinomial distribution.

Parameters
pointdict, optional

Dict of variable values on which random values are to be conditioned (uses default point if not specified).

sizeint, optional

Desired size of random sample (returns one sample if not specified).

Returns
array
class pymc3.distributions.discrete.ZeroInflatedNegativeBinomial(psi, mu, alpha, *args, **kwargs)

Zero-Inflated Negative binomial log-likelihood.

The Zero-inflated version of the Negative Binomial (NB). The NB distribution describes a Poisson random variable whose rate parameter is gamma distributed. The pmf of this distribution is

$\begin{split}f(x \mid \psi, \mu, \alpha) = \left\{ \begin{array}{l} (1-\psi) + \psi \left ( \frac{\alpha}{\alpha+\mu} \right) ^\alpha, \text{if } x = 0 \\ \psi \frac{\Gamma(x+\alpha)}{x! \Gamma(\alpha)} \left ( \frac{\alpha}{\mu+\alpha} \right)^\alpha \left( \frac{\mu}{\mu+\alpha} \right)^x, \text{if } x=1,2,3,\ldots \end{array} \right.\end{split}$
 Support $$x \in \mathbb{N}_0$$ Mean $$\psi\mu$$ Var $$\psi\mu + \left (1 + \frac{\mu}{\alpha} + \frac{1-\psi}{\mu} \right)$$
Parameters
psifloat

Expected proportion of NegativeBinomial variates (0 < psi < 1)

mufloat

Poission distribution parameter (mu > 0).

alphafloat

Gamma distribution parameter (alpha > 0).

logp(self, value)

Calculate log-probability of ZeroInflatedNegativeBinomial distribution at specified value.

Parameters
valuenumeric

Value(s) for which log-probability is calculated. If the log probabilities for multiple values are desired the values must be provided in a numpy array or theano tensor

Returns
TensorVariable
random(self, point=None, size=None)

Draw random values from ZeroInflatedNegativeBinomial distribution.

Parameters
pointdict, optional

Dict of variable values on which random values are to be conditioned (uses default point if not specified).

sizeint, optional

Desired size of random sample (returns one sample if not specified).

Returns
array
class pymc3.distributions.discrete.DiscreteUniform(lower, upper, *args, **kwargs)

Discrete uniform distribution. The pmf of this distribution is

$f(x \mid lower, upper) = \frac{1}{upper-lower+1}$
 Support $$x \in {lower, lower + 1, \ldots, upper}$$ Mean $$\dfrac{lower + upper}{2}$$ Variance $$\dfrac{(upper - lower)^2}{12}$$
Parameters
lowerint

Lower limit.

upperint

Upper limit (upper > lower).

logp(self, value)

Calculate log-probability of DiscreteUniform distribution at specified value.

Parameters
valuenumeric

Value(s) for which log-probability is calculated. If the log probabilities for multiple values are desired the values must be provided in a numpy array or theano tensor

Returns
TensorVariable
random(self, point=None, size=None)

Draw random values from DiscreteUniform distribution.

Parameters
pointdict, optional

Dict of variable values on which random values are to be conditioned (uses default point if not specified).

sizeint, optional

Desired size of random sample (returns one sample if not specified).

Returns
array
class pymc3.distributions.discrete.Geometric(p, *args, **kwargs)

Geometric log-likelihood.

The probability that the first success in a sequence of Bernoulli trials occurs on the x’th trial. The pmf of this distribution is

$f(x \mid p) = p(1-p)^{x-1}$
 Support $$x \in \mathbb{N}_{>0}$$ Mean $$\dfrac{1}{p}$$ Variance $$\dfrac{1 - p}{p^2}$$
Parameters
pfloat

Probability of success on an individual trial (0 < p <= 1).

logp(self, value)

Calculate log-probability of Geometric distribution at specified value.

Parameters
valuenumeric

Value(s) for which log-probability is calculated. If the log probabilities for multiple values are desired the values must be provided in a numpy array or theano tensor

Returns
TensorVariable
random(self, point=None, size=None)

Draw random values from Geometric distribution.

Parameters
pointdict, optional

Dict of variable values on which random values are to be conditioned (uses default point if not specified).

sizeint, optional

Desired size of random sample (returns one sample if not specified).

Returns
array
class pymc3.distributions.discrete.Categorical(p, *args, **kwargs)

Categorical log-likelihood.

The most general discrete distribution. The pmf of this distribution is

$f(x \mid p) = p_x$
 Support $$x \in \{0, 1, \ldots, |p|-1\}$$
Parameters
parray of floats

p > 0 and the elements of p must sum to 1. They will be automatically rescaled otherwise.

logp(self, value)

Calculate log-probability of Categorical distribution at specified value.

Parameters
valuenumeric

Value(s) for which log-probability is calculated. If the log probabilities for multiple values are desired the values must be provided in a numpy array or theano tensor

Returns
TensorVariable
random(self, point=None, size=None)

Draw random values from Categorical distribution.

Parameters
pointdict, optional

Dict of variable values on which random values are to be conditioned (uses default point if not specified).

sizeint, optional

Desired size of random sample (returns one sample if not specified).

Returns
array
class pymc3.distributions.discrete.OrderedLogistic(eta, cutpoints, *args, **kwargs)

Ordered Logistic log-likelihood.

Useful for regression on ordinal data values whose values range from 1 to K as a function of some predictor, $$\eta$$. The cutpoints, $$c$$, separate which ranges of $$\eta$$ are mapped to which of the K observed dependent variables. The number of cutpoints is K - 1. It is recommended that the cutpoints are constrained to be ordered.

$\begin{split}f(k \mid \eta, c) = \left\{ \begin{array}{l} 1 - \text{logit}^{-1}(\eta - c_1) \,, \text{if } k = 0 \\ \text{logit}^{-1}(\eta - c_{k - 1}) - \text{logit}^{-1}(\eta - c_{k}) \,, \text{if } 0 < k < K \\ \text{logit}^{-1}(\eta - c_{K - 1}) \,, \text{if } k = K \\ \end{array} \right.\end{split}$
Parameters
etafloat

The predictor.

carray

The length K - 1 array of cutpoints which break $$\eta$$ into ranges. Do not explicitly set the first and last elements of $$c$$ to negative and positive infinity.

Examples

# Generate data for a simple 1 dimensional example problem
n1_c = 300; n2_c = 300; n3_c = 300
cluster1 = np.random.randn(n1_c) + -1
cluster2 = np.random.randn(n2_c) + 0
cluster3 = np.random.randn(n3_c) + 2

x = np.concatenate((cluster1, cluster2, cluster3))
y = np.concatenate((1*np.ones(n1_c),
2*np.ones(n2_c),
3*np.ones(n3_c))) - 1

# Ordered logistic regression
with pm.Model() as model:
cutpoints = pm.Normal("cutpoints", mu=[-1,1], sigma=10, shape=2,
transform=pm.distributions.transforms.ordered)
y_ = pm.OrderedLogistic("y", cutpoints=cutpoints, eta=x, observed=y)
tr = pm.sample(1000)

# Plot the results
plt.hist(cluster1, 30, alpha=0.5);
plt.hist(cluster2, 30, alpha=0.5);
plt.hist(cluster3, 30, alpha=0.5);
plt.hist(tr["cutpoints"][:,0], 80, alpha=0.2, color='k');
plt.hist(tr["cutpoints"][:,1], 80, alpha=0.2, color='k');