scipy.stats.dirichlet¶
-
scipy.stats.
dirichlet
= <scipy.stats._multivariate.dirichlet_gen object>[source]¶ A Dirichlet random variable.
The
alpha
keyword specifies the concentration parameters of the distribution.New in version 0.15.0.
Parameters: - x : array_like
Quantiles, with the last axis of x denoting the components.
- alpha : array_like
The concentration parameters. The number of entries determines the dimensionality of the distribution.
- random_state : None or int or np.random.RandomState instance, optional
If int or RandomState, use it for drawing the random variates. If None (or np.random), the global np.random state is used. Default is None.
- Alternatively, the object may be called (as a function) to fix
- concentration parameters, returning a “frozen” Dirichlet
- random variable:
- rv = dirichlet(alpha)
- Frozen object with the same methods but holding the given concentration parameters fixed.
Notes
Each \(\alpha\) entry must be positive. The distribution has only support on the simplex defined by
\[\sum_{i=1}^{K} x_i \le 1\]The probability density function for
dirichlet
is\[f(x) = \frac{1}{\mathrm{B}(\boldsymbol\alpha)} \prod_{i=1}^K x_i^{\alpha_i - 1}\]where
\[\mathrm{B}(\boldsymbol\alpha) = \frac{\prod_{i=1}^K \Gamma(\alpha_i)} {\Gamma\bigl(\sum_{i=1}^K \alpha_i\bigr)}\]and \(\boldsymbol\alpha=(\alpha_1,\ldots,\alpha_K)\), the concentration parameters and \(K\) is the dimension of the space where \(x\) takes values.
Note that the dirichlet interface is somewhat inconsistent. The array returned by the rvs function is transposed with respect to the format expected by the pdf and logpdf.
Examples
>>> from scipy.stats import dirichlet
Generate a dirichlet random variable
>>> quantiles = np.array([0.2, 0.2, 0.6]) # specify quantiles >>> alpha = np.array([0.4, 5, 15]) # specify concentration parameters >>> dirichlet.pdf(quantiles, alpha) 0.2843831684937255
The same PDF but following a log scale
>>> dirichlet.logpdf(quantiles, alpha) -1.2574327653159187
Once we specify the dirichlet distribution we can then calculate quantities of interest
>>> dirichlet.mean(alpha) # get the mean of the distribution array([0.01960784, 0.24509804, 0.73529412]) >>> dirichlet.var(alpha) # get variance array([0.00089829, 0.00864603, 0.00909517]) >>> dirichlet.entropy(alpha) # calculate the differential entropy -4.3280162474082715
We can also return random samples from the distribution
>>> dirichlet.rvs(alpha, size=1, random_state=1) array([[0.00766178, 0.24670518, 0.74563305]]) >>> dirichlet.rvs(alpha, size=2, random_state=2) array([[0.01639427, 0.1292273 , 0.85437844], [0.00156917, 0.19033695, 0.80809388]])
Methods
``pdf(x, alpha)`` Probability density function. ``logpdf(x, alpha)`` Log of the probability density function. ``rvs(alpha, size=1, random_state=None)`` Draw random samples from a Dirichlet distribution. ``mean(alpha)`` The mean of the Dirichlet distribution ``var(alpha)`` The variance of the Dirichlet distribution ``entropy(alpha)`` Compute the differential entropy of the Dirichlet distribution.