Coverage for /home/martinb/.local/share/virtualenvs/camcops/lib/python3.6/site-packages/scipy/special/spfun_stats.py : 38%

Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
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 """
84 a = np.asarray(a)
85 if not np.isscalar(d) or (np.floor(d) != d):
86 raise ValueError("d should be a positive integer (dimension)")
87 if np.any(a <= 0.5 * (d - 1)):
88 raise ValueError("condition a (%f) > 0.5 * (d-1) (%f) not met"
89 % (a, 0.5 * (d-1)))
91 res = (d * (d-1) * 0.25) * np.log(np.pi)
92 res += np.sum(loggam([(a - (j - 1.)/2) for j in range(1, d+1)]), axis=0)
93 return res