_spfun_stats.py 3.7 KB

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