spfun_stats.py 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. from __future__ import division, print_function, absolute_import
  33. import numpy as np
  34. from scipy.special import gammaln as loggam
  35. __all__ = ['multigammaln']
  36. def multigammaln(a, d):
  37. r"""Returns the log of multivariate gamma, also sometimes called the
  38. generalized gamma.
  39. Parameters
  40. ----------
  41. a : ndarray
  42. The multivariate gamma is computed for each item of `a`.
  43. d : int
  44. The dimension of the space of integration.
  45. Returns
  46. -------
  47. res : ndarray
  48. The values of the log multivariate gamma at the given points `a`.
  49. Notes
  50. -----
  51. The formal definition of the multivariate gamma of dimension d for a real
  52. `a` is
  53. .. math::
  54. \Gamma_d(a) = \int_{A>0} e^{-tr(A)} |A|^{a - (d+1)/2} dA
  55. with the condition :math:`a > (d-1)/2`, and :math:`A > 0` being the set of
  56. all the positive definite matrices of dimension `d`. Note that `a` is a
  57. scalar: the integrand only is multivariate, the argument is not (the
  58. function is defined over a subset of the real set).
  59. This can be proven to be equal to the much friendlier equation
  60. .. math::
  61. \Gamma_d(a) = \pi^{d(d-1)/4} \prod_{i=1}^{d} \Gamma(a - (i-1)/2).
  62. References
  63. ----------
  64. R. J. Muirhead, Aspects of multivariate statistical theory (Wiley Series in
  65. probability and mathematical statistics).
  66. """
  67. a = np.asarray(a)
  68. if not np.isscalar(d) or (np.floor(d) != d):
  69. raise ValueError("d should be a positive integer (dimension)")
  70. if np.any(a <= 0.5 * (d - 1)):
  71. raise ValueError("condition a (%f) > 0.5 * (d-1) (%f) not met"
  72. % (a, 0.5 * (d-1)))
  73. res = (d * (d-1) * 0.25) * np.log(np.pi)
  74. res += np.sum(loggam([(a - (j - 1.)/2) for j in range(1, d+1)]), axis=0)
  75. return res