Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/scipy/special/_spfun_stats.py: 38%
13 statements
« prev ^ index » next coverage.py v7.3.2, created at 2023-12-12 06:31 +0000
« prev ^ index » next coverage.py v7.3.2, created at 2023-12-12 06:31 +0000
1# Last Change: Sat Mar 21 02:00 PM 2009 J
3# Copyright (c) 2001, 2002 Enthought, Inc.
4#
5# All rights reserved.
6#
7# Redistribution and use in source and binary forms, with or without
8# modification, are permitted provided that the following conditions are met:
9#
10# a. Redistributions of source code must retain the above copyright notice,
11# this list of conditions and the following disclaimer.
12# b. Redistributions in binary form must reproduce the above copyright
13# notice, this list of conditions and the following disclaimer in the
14# documentation and/or other materials provided with the distribution.
15# c. Neither the name of the Enthought nor the names of its contributors
16# may be used to endorse or promote products derived from this software
17# without specific prior written permission.
18#
19#
20# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23# ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR
24# ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
27# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
30# DAMAGE.
32"""Some more special functions which may be useful for multivariate statistical
33analysis."""
35import numpy as np
36from scipy.special import gammaln as loggam
39__all__ = ['multigammaln']
42def multigammaln(a, d):
43 r"""Returns the log of multivariate gamma, also sometimes called the
44 generalized gamma.
46 Parameters
47 ----------
48 a : ndarray
49 The multivariate gamma is computed for each item of `a`.
50 d : int
51 The dimension of the space of integration.
53 Returns
54 -------
55 res : ndarray
56 The values of the log multivariate gamma at the given points `a`.
58 Notes
59 -----
60 The formal definition of the multivariate gamma of dimension d for a real
61 `a` is
63 .. math::
65 \Gamma_d(a) = \int_{A>0} e^{-tr(A)} |A|^{a - (d+1)/2} dA
67 with the condition :math:`a > (d-1)/2`, and :math:`A > 0` being the set of
68 all the positive definite matrices of dimension `d`. Note that `a` is a
69 scalar: the integrand only is multivariate, the argument is not (the
70 function is defined over a subset of the real set).
72 This can be proven to be equal to the much friendlier equation
74 .. math::
76 \Gamma_d(a) = \pi^{d(d-1)/4} \prod_{i=1}^{d} \Gamma(a - (i-1)/2).
78 References
79 ----------
80 R. J. Muirhead, Aspects of multivariate statistical theory (Wiley Series in
81 probability and mathematical statistics).
83 Examples
84 --------
85 >>> import numpy as np
86 >>> from scipy.special import multigammaln, gammaln
87 >>> a = 23.5
88 >>> d = 10
89 >>> multigammaln(a, d)
90 454.1488605074416
92 Verify that the result agrees with the logarithm of the equation
93 shown above:
95 >>> d*(d-1)/4*np.log(np.pi) + gammaln(a - 0.5*np.arange(0, d)).sum()
96 454.1488605074416
97 """
98 a = np.asarray(a)
99 if not np.isscalar(d) or (np.floor(d) != d):
100 raise ValueError("d should be a positive integer (dimension)")
101 if np.any(a <= 0.5 * (d - 1)):
102 raise ValueError("condition a (%f) > 0.5 * (d-1) (%f) not met"
103 % (a, 0.5 * (d-1)))
105 res = (d * (d-1) * 0.25) * np.log(np.pi)
106 res += np.sum(loggam([(a - (j - 1.)/2) for j in range(1, d+1)]), axis=0)
107 return res