
&]\c        
   @` s  d  d l  m Z m Z m Z d  d l Z d  d l Z d  d l m Z m Z d  d l	 Z
 d  d l m Z d  d l m Z m Z m Z m Z m Z d  d l m Z d  d l m Z d  d l m Z d  d	 l m Z d
 d l m Z d
 d l m Z d d d d d d d d d d g
 Z e j  d e j!  Z" e j  d  Z# e j  e j!  Z$ d Z% d   Z& e' e' d  Z( d d  Z) d e* f d     YZ+ d e* f d      YZ, d! e* f d"     YZ- d# Z. d$ Z/ d% Z0 d& Z1 i e. d' 6e/ d( 6e% d) 6Z2 i e0 d' 6e1 d( 6e% d) 6Z3 d* e, f d+     YZ4 e4   Z5 d, e- f d-     YZ6 xg d. d/ d0 d1 d2 g D]P Z7 e4 j8 e7 Z9 e6 j8 e7 Z: e j; e9 j< e3  e: _< e j; e9 j< e2  e9 _< qNWd3 Z= d4 Z> d% Z? d& Z@ i e= d5 6e> d6 6e% d) 6ZA i e? d5 6e@ d6 6e% d) 6ZB d7 e, f d8     YZC eC   ZD d9 e- f d:     YZE xa d. d/ d2 g D]P Z7 eC j8 e7 Z9 eE j8 e7 Z: e j; e9 j< eB  e: _< e j; e9 j< eA  e9 _< q5Wd; ZF d% ZG d& ZH i eF d< 6e% d) 6ZI i eG d< 6e% d) 6ZJ d=   ZK d>   ZL d?   ZM d@ e, f dA     YZN eN   ZO dB e- f dC     YZP xj d. d/ d2 dD dE dF g D]P Z7 eN j8 e7 Z9 eP j8 e7 Z: e j; e9 j< eJ  e: _< e j; e9 j< eI  e9 _< q,WdG ZQ d% ZR d% ZS d& ZT i eQ dH 6eR dI 6e% d) 6ZU i eS dH 6eT dI 6e% d) 6ZV dJ e, f dK     YZW eW   ZX dL e- f dM     YZY xm d. d/ dD dN dE d2 dF g D]P Z7 eW j8 e7 Z9 eY j8 e7 Z: e j; e9 j< eV  e: _< e j; e9 j< eU  e9 _< qWeZ dO  Z[ dP eW f dQ     YZ\ e\   Z] dR e- f dS     YZ^ xj d. d/ dD dN dE d2 g D]P Z7 e\ j8 e7 Z9 eY j8 e7 Z: e j; e9 j< eV  e: _< e j; e9 j< eU  e9 _< qWdT Z_ dU Z` d% Za d& Zb i e_ dH 6e` dI 6e% d) 6Zc i ea dH 6eb dI 6e% d) 6Zd dV e, f dW     YZe ee   Zf dX e- f dY     YZg xg dZ d[ dD d\ d2 g D]P Z7 ee j8 e7 Z9 eg j8 e7 Z: e j; e9 j< ed  e: _< e j; e9 j< ec  e9 _< qWd] e, f d^     YZh eh   Zi d_ e- f d`     YZj da e, f db     YZk ek   Zl dc e, f dd     YZm em   Zn de e, f df     YZo eo   Zp d S(g   i    (   t   divisiont   print_functiont   absolute_importN(   t   asarray_chkfinitet   asarray(   t   doccer(   t   gammalnt   psit   multigammalnt   xlogyt   entr(   t   check_random_state(   t   drot(   t   LinAlgError(   t   get_lapack_funcsi   (   t   binom(   t   mvnt   multivariate_normalt   matrix_normalt	   dirichlett   wishartt
   invwishartt   multinomialt   special_ortho_groupt   ortho_groupt   random_correlationt   unitary_groupi   s   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.
c         C` s,   |  j    }  |  j d k r( |  d }  n  |  S(   s`   
    Remove single-dimensional entries from array and convert to scalar,
    if necessary.

    i    (    (   t   squeezet   ndim(   t   out(    (    s8   lib/python2.7/site-packages/scipy/stats/_multivariate.pyt   _squeeze_output,   s    c         C` s   | d k	 r | } n  | d k rd |  j j j   } i d d 6d d 6} | | t j |  j } n  | t j t |    } | S(   s  
    Determine which eigenvalues are "small" given the spectrum.

    This is for compatibility across various linear algebra functions
    that should agree about whether or not a Hermitian matrix is numerically
    singular and what is its numerical matrix rank.
    This is designed to be compatible with scipy.linalg.pinvh.

    Parameters
    ----------
    spectrum : 1d ndarray
        Array of eigenvalues of a Hermitian matrix.
    cond, rcond : float, optional
        Cutoff for small eigenvalues.
        Singular values smaller than rcond * largest_eigenvalue are
        considered zero.
        If None or -1, suitable machine precision is used.

    Returns
    -------
    eps : float
        Magnitude cutoff for numerical negligibility.

    ig     @@t   fg    .At   dN(   Ni(	   t   Nonet   dtypet   chart   lowert   npt   finfot   epst   maxt   abs(   t   spectrumt   condt   rcondt   tt   factorR'   (    (    s8   lib/python2.7/site-packages/scipy/stats/_multivariate.pyt   _eigvalsh_to_eps8   s    	gh㈵>c         C` sB   t  j g  |  D]( } t |  | k r+ d n d | ^ q d t S(   s  
    A helper function for computing the pseudoinverse.

    Parameters
    ----------
    v : iterable of numbers
        This may be thought of as a vector of eigenvalues or singular values.
    eps : float
        Values with magnitude no greater than eps are considered negligible.

    Returns
    -------
    v_pinv : 1d float ndarray
        A vector of pseudo-inverted numbers.

    i    i   R"   (   R%   t   arrayR)   t   float(   t   vR'   t   x(    (    s8   lib/python2.7/site-packages/scipy/stats/_multivariate.pyt   _pinv_1d[   s    t   _PSDc           B` s5   e  Z d  Z d d e e e d  Z e d    Z RS(   sN  
    Compute coordinated functions of a symmetric positive semidefinite matrix.

    This class addresses two issues.  Firstly it allows the pseudoinverse,
    the logarithm of the pseudo-determinant, and the rank of the matrix
    to be computed using one call to eigh instead of three.
    Secondly it allows these functions to be computed in a way
    that gives mutually compatible results.
    All of the functions are computed with a common understanding as to
    which of the eigenvalues are to be considered negligibly small.
    The functions are designed to coordinate with scipy.linalg.pinvh()
    but not necessarily with np.linalg.det() or with np.linalg.matrix_rank().

    Parameters
    ----------
    M : array_like
        Symmetric positive semidefinite matrix (2-D).
    cond, rcond : float, optional
        Cutoff for small eigenvalues.
        Singular values smaller than rcond * largest_eigenvalue are
        considered zero.
        If None or -1, suitable machine precision is used.
    lower : bool, optional
        Whether the pertinent array data is taken from the lower
        or upper triangle of M. (Default: lower)
    check_finite : bool, optional
        Whether to check that the input matrices contain only finite
        numbers. Disabling may give a performance gain, but may result
        in problems (crashes, non-termination) if the inputs do contain
        infinities or NaNs.
    allow_singular : bool, optional
        Whether to allow a singular matrix.  (Default: True)

    Notes
    -----
    The arguments are similar to those of scipy.linalg.pinvh().

    c         C` s	  t  j j | d | d | \ } } t | | |  }	 t j |  |	 k  r[ t d   n  | | |	 k }
 t |
  t |  k  r | r t j j d   n  t	 | |	  } t j
 | t j |   } t |
  |  _ | |  _ t j t j |
   |  _ d  |  _ d  S(   NR$   t   check_finites.   the input matrix must be positive semidefinites   singular matrix(   t   scipyt   linalgt   eighR/   R%   t   mint
   ValueErrort   lenR   R4   t   multiplyt   sqrtt   rankt   Ut   sumt   logt   log_pdetR!   t   _pinv(   t   selft   MR+   R,   R$   R6   t   allow_singulart   st   uR'   R    t   s_pinvR@   (    (    s8   lib/python2.7/site-packages/scipy/stats/_multivariate.pyt   __init__   s    $	c         C` s7   |  j  d  k r0 t j |  j |  j j  |  _  n  |  j  S(   N(   RD   R!   R%   t   dotR@   t   T(   RE   (    (    s8   lib/python2.7/site-packages/scipy/stats/_multivariate.pyt   pinv   s    !N(   t   __name__t
   __module__t   __doc__R!   t   TrueRK   t   propertyRN   (    (    (    s8   lib/python2.7/site-packages/scipy/stats/_multivariate.pyR5   o   s   &	t   multi_rv_genericc           B` sD   e  Z d  Z d d  Z e d    Z e j d    Z d   Z RS(   sd   
    Class which encapsulates common functionality between all multivariate
    distributions.

    c         C` s&   t  t |   j   t |  |  _ d  S(   N(   t   superRT   RK   R   t   _random_state(   RE   t   seed(    (    s8   lib/python2.7/site-packages/scipy/stats/_multivariate.pyRK      s    c         C` s   |  j  S(   sY   Get or set the RandomState object for generating random variates.

        This can be either None or an existing RandomState object.

        If None (or np.random), use the RandomState singleton used by np.random.
        If already a RandomState instance, use it.
        If an int, use a new RandomState instance seeded with seed.

        (   RV   (   RE   (    (    s8   lib/python2.7/site-packages/scipy/stats/_multivariate.pyt   random_state   s    c         C` s   t  |  |  _ d  S(   N(   R   RV   (   RE   RW   (    (    s8   lib/python2.7/site-packages/scipy/stats/_multivariate.pyRX      s    c         C` s!   | d  k	 r t |  S|  j Sd  S(   N(   R!   R   RV   (   RE   RX   (    (    s8   lib/python2.7/site-packages/scipy/stats/_multivariate.pyt   _get_random_state   s    
N(	   RO   RP   RQ   R!   RK   RS   RX   t   setterRY   (    (    (    s8   lib/python2.7/site-packages/scipy/stats/_multivariate.pyRT      s
   t   multi_rv_frozenc           B` s/   e  Z d  Z e d    Z e j d    Z RS(   sj   
    Class which encapsulates common functionality between all frozen
    multivariate distributions.
    c         C` s
   |  j  j S(   N(   t   _distRV   (   RE   (    (    s8   lib/python2.7/site-packages/scipy/stats/_multivariate.pyRX      s    c         C` s   t  |  |  j _ d  S(   N(   R   R\   RV   (   RE   RW   (    (    s8   lib/python2.7/site-packages/scipy/stats/_multivariate.pyRX      s    (   RO   RP   RQ   RS   RX   RZ   (    (    (    s8   lib/python2.7/site-packages/scipy/stats/_multivariate.pyR[      s   s   mean : array_like, optional
    Mean of the distribution (default zero)
cov : array_like, optional
    Covariance matrix of the distribution (default one)
allow_singular : bool, optional
    Whether to allow a singular covariance matrix.  (Default: False)
s2  Setting the parameter `mean` to `None` is equivalent to having `mean`
    be the zero-vector. The parameter `cov` can be a scalar, in which case
    the covariance matrix is the identity times that value, a vector of
    diagonal entries for the covariance matrix, or a two-dimensional
    array_like.
    t    s>   See class definition for a detailed description of parameters.t   _mvn_doc_default_callparamst   _mvn_doc_callparams_notet   _doc_random_statet   multivariate_normal_genc           B` s   e  Z d  Z d d  Z d d e d d  Z d   Z d   Z d   Z	 d d e d  Z
 d d e d  Z d	   Z d d e d d
 d
 d  Z d d e d d
 d
 d  Z d d d d d  Z d d d  Z RS(   s  
    A multivariate normal random variable.

    The `mean` keyword specifies the mean. The `cov` keyword specifies the
    covariance matrix.

    Methods
    -------
    ``pdf(x, mean=None, cov=1, allow_singular=False)``
        Probability density function.
    ``logpdf(x, mean=None, cov=1, allow_singular=False)``
        Log of the probability density function.
    ``cdf(x, mean=None, cov=1, allow_singular=False, maxpts=1000000*dim, abseps=1e-5, releps=1e-5)``
        Cumulative distribution function.
    ``logcdf(x, mean=None, cov=1, allow_singular=False, maxpts=1000000*dim, abseps=1e-5, releps=1e-5)``
        Log of the cumulative distribution function.
    ``rvs(mean=None, cov=1, size=1, random_state=None)``
        Draw random samples from a multivariate normal distribution.
    ``entropy()``
        Compute the differential entropy of the multivariate normal.

    Parameters
    ----------
    x : array_like
        Quantiles, with the last axis of `x` denoting the components.
    %(_mvn_doc_default_callparams)s
    %(_doc_random_state)s

    Alternatively, the object may be called (as a function) to fix the mean
    and covariance parameters, returning a "frozen" multivariate normal
    random variable:

    rv = multivariate_normal(mean=None, cov=1, allow_singular=False)
        - Frozen object with the same methods but holding the given
          mean and covariance fixed.

    Notes
    -----
    %(_mvn_doc_callparams_note)s

    The covariance matrix `cov` must be a (symmetric) positive
    semi-definite matrix. The determinant and inverse of `cov` are computed
    as the pseudo-determinant and pseudo-inverse, respectively, so
    that `cov` does not need to have full rank.

    The probability density function for `multivariate_normal` is

    .. math::

        f(x) = \frac{1}{\sqrt{(2 \pi)^k \det \Sigma}}
               \exp\left( -\frac{1}{2} (x - \mu)^T \Sigma^{-1} (x - \mu) \right),

    where :math:`\mu` is the mean, :math:`\Sigma` the covariance matrix,
    and :math:`k` is the dimension of the space where :math:`x` takes values.

    .. versionadded:: 0.14.0

    Examples
    --------
    >>> import matplotlib.pyplot as plt
    >>> from scipy.stats import multivariate_normal

    >>> x = np.linspace(0, 5, 10, endpoint=False)
    >>> y = multivariate_normal.pdf(x, mean=2.5, cov=0.5); y
    array([ 0.00108914,  0.01033349,  0.05946514,  0.20755375,  0.43939129,
            0.56418958,  0.43939129,  0.20755375,  0.05946514,  0.01033349])
    >>> fig1 = plt.figure()
    >>> ax = fig1.add_subplot(111)
    >>> ax.plot(x, y)

    The input quantiles can be any shape of array, as long as the last
    axis labels the components.  This allows us for instance to
    display the frozen pdf for a non-isotropic random variable in 2D as
    follows:

    >>> x, y = np.mgrid[-1:1:.01, -1:1:.01]
    >>> pos = np.dstack((x, y))
    >>> rv = multivariate_normal([0.5, -0.2], [[2.0, 0.3], [0.3, 0.5]])
    >>> fig2 = plt.figure()
    >>> ax2 = fig2.add_subplot(111)
    >>> ax2.contourf(x, y, rv.pdf(pos))

    c         C` s2   t  t |   j |  t j |  j t  |  _ d  S(   N(   RU   Ra   RK   R   t	   docformatRQ   t   mvn_docdict_params(   RE   RW   (    (    s8   lib/python2.7/site-packages/scipy/stats/_multivariate.pyRK   ^  s    i   c         C` s   t  | | d | d | S(   s   
        Create a frozen multivariate normal distribution.

        See `multivariate_normal_frozen` for more information.

        RG   RW   (   t   multivariate_normal_frozen(   RE   t   meant   covRG   RW   (    (    s8   lib/python2.7/site-packages/scipy/stats/_multivariate.pyt   __call__b  s    c         C` s]  | d k r | d k rj | d k r- d } q t j | d t } | j d k  rZ d } q | j d } q t j | d t } | j } n t j |  s t d   n  | d k r t j	 |  } n  t j | d t } | d k r d } n  t j | d t } | d k r'd | _ d | _ n  | j d k sI| j d | k r\t d |   n  | j d k r| t j
 |  } n | j d k rt j |  } n | j d k r+| j | | f k r+| j \ } } | | k rd t | j  } n% d	 } | t | j  t |  f } t |   n% | j d k rPt d
 | j   n  | | | f S(   s   
        Infer dimensionality from mean or covariance matrix, ensure that
        mean and covariance are full vector resp. matrix.

        i   R"   i   i    s.   Dimension of random variable must be a scalar.g      ?s+   Array 'mean' must be a vector of length %d.sH   Array 'cov' must be square if it is two dimensional, but cov.shape = %s.sT   Dimension mismatch: array 'cov' is of shape %s, but 'mean' is a vector of length %d.s>   Array 'cov' must be at most two-dimensional, but cov.ndim = %dN(   i   (   i   i   (   R!   R%   R   R1   R   t   shapet   sizet   isscalarR;   t   zerost   eyet   diagt   strR<   (   RE   t   dimRe   Rf   t   rowst   colst   msg(    (    s8   lib/python2.7/site-packages/scipy/stats/_multivariate.pyt   _process_parametersm  sP    				"$c         C` s   t  j | d t } | j d k r4 | t  j } nS | j d k r | d k rk | d d  t  j f } q | t  j d d  f } n  | S(   sm   
        Adjust quantiles array so that last axis labels the components of
        each data point.

        R"   i    i   N(   R%   R   R1   R   t   newaxis(   RE   R3   Ro   (    (    s8   lib/python2.7/site-packages/scipy/stats/_multivariate.pyt   _process_quantiles  s    c         C` sH   | | } t  j t  j t  j | |   d d } d | t | | S(   s  
        Parameters
        ----------
        x : ndarray
            Points at which to evaluate the log of the probability
            density function
        mean : ndarray
            Mean of the distribution
        prec_U : ndarray
            A decomposition such that np.dot(prec_U, prec_U.T)
            is the precision matrix, i.e. inverse of the covariance matrix.
        log_det_cov : float
            Logarithm of the determinant of the covariance matrix
        rank : int
            Rank of the covariance matrix.

        Notes
        -----
        As this function does no argument checking, it should not be
        called directly; use 'logpdf' instead.

        t   axisig      (   R%   RA   t   squareRL   t   _LOG_2PI(   RE   R3   Re   t   prec_Ut   log_det_covR?   t   devt   maha(    (    s8   lib/python2.7/site-packages/scipy/stats/_multivariate.pyt   _logpdf  s    
*c         C` sp   |  j  d | |  \ } } } |  j | |  } t | d | } |  j | | | j | j | j  } t |  S(   s  
        Log of the multivariate normal probability density function.

        Parameters
        ----------
        x : array_like
            Quantiles, with the last axis of `x` denoting the components.
        %(_mvn_doc_default_callparams)s

        Returns
        -------
        pdf : ndarray or scalar
            Log of the probability density function evaluated at `x`

        Notes
        -----
        %(_mvn_doc_callparams_note)s

        RG   N(	   Rs   R!   Ru   R5   R}   R@   RC   R?   R   (   RE   R3   Re   Rf   RG   Ro   t   psdR   (    (    s8   lib/python2.7/site-packages/scipy/stats/_multivariate.pyt   logpdf  s
    $c         C` sy   |  j  d | |  \ } } } |  j | |  } t | d | } t j |  j | | | j | j | j	   } t
 |  S(   s  
        Multivariate normal probability density function.

        Parameters
        ----------
        x : array_like
            Quantiles, with the last axis of `x` denoting the components.
        %(_mvn_doc_default_callparams)s

        Returns
        -------
        pdf : ndarray or scalar
            Probability density function evaluated at `x`

        Notes
        -----
        %(_mvn_doc_callparams_note)s

        RG   N(   Rs   R!   Ru   R5   R%   t   expR}   R@   RC   R?   R   (   RE   R3   Re   Rf   RG   Ro   R~   R   (    (    s8   lib/python2.7/site-packages/scipy/stats/_multivariate.pyt   pdf  s
    -c   	      ` sV   t  j  j t  j          f d   } t  j | d |  } t |  S(   s  
        Parameters
        ----------
        x : ndarray
            Points at which to evaluate the cumulative distribution function.
        mean : ndarray
            Mean of the distribution
        cov : array_like
            Covariance matrix of the distribution
        maxpts: integer
            The maximum number of points to use for integration
        abseps: float
            Absolute error tolerance
        releps: float
            Relative error tolerance

        Notes
        -----
        As this function does no argument checking, it should not be
        called directly; use 'cdf' instead.

        .. versionadded:: 1.0.0

        c         ` s#   t  j  |         d S(   Ni    (   R   t   mvnun(   t   x_slice(   t   absepsRf   R$   t   maxptsRe   t   releps(    s8   lib/python2.7/site-packages/scipy/stats/_multivariate.pyt   <lambda>(  s   i(   R%   t   fullRh   t   inft   apply_along_axisR   (	   RE   R3   Re   Rf   R   R   R   t   func1dR   (    (   R   Rf   R$   R   Re   R   s8   lib/python2.7/site-packages/scipy/stats/_multivariate.pyt   _cdf  s    gh㈵>c   
      C` s~   |  j  d | |  \ } } } |  j | |  } t | d | | sS d | } n  t j |  j | | | | | |   }	 |	 S(   s  
        Log of the multivariate normal cumulative distribution function.

        Parameters
        ----------
        x : array_like
            Quantiles, with the last axis of `x` denoting the components.
        %(_mvn_doc_default_callparams)s
        maxpts: integer, optional
            The maximum number of points to use for integration
            (default `1000000*dim`)
        abseps: float, optional
            Absolute error tolerance (default 1e-5)
        releps: float, optional
            Relative error tolerance (default 1e-5)

        Returns
        -------
        cdf : ndarray or scalar
            Log of the cumulative distribution function evaluated at `x`

        Notes
        -----
        %(_mvn_doc_callparams_note)s

        .. versionadded:: 1.0.0

        RG   i@B N(   Rs   R!   Ru   R5   R%   RB   R   (
   RE   R3   Re   Rf   RG   R   R   R   Ro   R   (    (    s8   lib/python2.7/site-packages/scipy/stats/_multivariate.pyt   logcdf-  s    'c   
      C` su   |  j  d | |  \ } } } |  j | |  } t | d | | sS d | } n  |  j | | | | | |  }	 |	 S(   s  
        Multivariate normal cumulative distribution function.

        Parameters
        ----------
        x : array_like
            Quantiles, with the last axis of `x` denoting the components.
        %(_mvn_doc_default_callparams)s
        maxpts: integer, optional
            The maximum number of points to use for integration
            (default `1000000*dim`)
        abseps: float, optional
            Absolute error tolerance (default 1e-5)
        releps: float, optional
            Relative error tolerance (default 1e-5)

        Returns
        -------
        cdf : ndarray or scalar
            Cumulative distribution function evaluated at `x`

        Notes
        -----
        %(_mvn_doc_callparams_note)s

        .. versionadded:: 1.0.0

        RG   i@B N(   Rs   R!   Ru   R5   R   (
   RE   R3   Re   Rf   RG   R   R   R   Ro   R   (    (    s8   lib/python2.7/site-packages/scipy/stats/_multivariate.pyt   cdfT  s    c         C` sL   |  j  d | |  \ } } } |  j |  } | j | | |  } t |  S(   s  
        Draw random samples from a multivariate normal distribution.

        Parameters
        ----------
        %(_mvn_doc_default_callparams)s
        size : integer, optional
            Number of samples to draw (default 1).
        %(_doc_random_state)s

        Returns
        -------
        rvs : ndarray or scalar
            Random variates of size (`size`, `N`), where `N` is the
            dimension of the random variable.

        Notes
        -----
        %(_mvn_doc_callparams_note)s

        N(   Rs   R!   RY   R   R   (   RE   Re   Rf   Ri   RX   Ro   R   (    (    s8   lib/python2.7/site-packages/scipy/stats/_multivariate.pyt   rvs{  s    c         C` sP   |  j  d | |  \ } } } t j j d t j t j |  \ } } d | S(   sP  
        Compute the differential entropy of the multivariate normal.

        Parameters
        ----------
        %(_mvn_doc_default_callparams)s

        Returns
        -------
        h : scalar
            Entropy of the multivariate normal distribution

        Notes
        -----
        %(_mvn_doc_callparams_note)s

        i   g      ?N(   Rs   R!   R%   R8   t   slogdett   pit   e(   RE   Re   Rf   Ro   t   _t   logdet(    (    s8   lib/python2.7/site-packages/scipy/stats/_multivariate.pyt   entropy  s    *N(   RO   RP   RQ   R!   RK   t   FalseRg   Rs   Ru   R}   R   R   R   R   R   R   R   (    (    (    s8   lib/python2.7/site-packages/scipy/stats/_multivariate.pyRa   	  s   S	?			 &&Rd   c           B` sb   e  Z d	 d  e d	 d	 d d d  Z d   Z d   Z d   Z d   Z d  d	 d  Z	 d   Z
 RS(
   i   gh㈵>c         C` s   t  |  |  _ |  j j d | |  \ |  _ |  _ |  _ t |  j d | |  _ | sg d |  j } n  | |  _	 | |  _
 | |  _ d S(   s&  
        Create a frozen multivariate normal distribution.

        Parameters
        ----------
        mean : array_like, optional
            Mean of the distribution (default zero)
        cov : array_like, optional
            Covariance matrix of the distribution (default one)
        allow_singular : bool, optional
            If this flag is True then tolerate a singular
            covariance matrix (default False).
        seed : None or int or np.random.RandomState instance, optional
            This parameter defines the RandomState object to use for drawing
            random variates.
            If None (or np.random), the global np.random state is used.
            If integer, it is used to seed the local RandomState instance
            Default is None.
        maxpts: integer, optional
            The maximum number of points to use for integration of the
            cumulative distribution function (default `1000000*dim`)
        abseps: float, optional
            Absolute error tolerance for the cumulative distribution function
            (default 1e-5)
        releps: float, optional
            Relative error tolerance for the cumulative distribution function
            (default 1e-5)

        Examples
        --------
        When called with the default parameters, this will create a 1D random
        variable with mean 0 and covariance 1:

        >>> from scipy.stats import multivariate_normal
        >>> r = multivariate_normal()
        >>> r.mean
        array([ 0.])
        >>> r.cov
        array([[1.]])

        RG   i@B N(   Ra   R\   Rs   R!   Ro   Re   Rf   R5   t   cov_infoR   R   R   (   RE   Re   Rf   RG   RW   R   R   R   (    (    s8   lib/python2.7/site-packages/scipy/stats/_multivariate.pyRK     s    +	!		c         C` sU   |  j  j | |  j  } |  j  j | |  j |  j j |  j j |  j j  } t	 |  S(   N(
   R\   Ru   Ro   R}   Re   R   R@   RC   R?   R   (   RE   R3   R   (    (    s8   lib/python2.7/site-packages/scipy/stats/_multivariate.pyR     s    c         C` s   t  j |  j |   S(   N(   R%   R   R   (   RE   R3   (    (    s8   lib/python2.7/site-packages/scipy/stats/_multivariate.pyR     s    c         C` s   t  j |  j |   S(   N(   R%   RB   R   (   RE   R3   (    (    s8   lib/python2.7/site-packages/scipy/stats/_multivariate.pyR     s    c         C` sR   |  j  j | |  j  } |  j  j | |  j |  j |  j |  j |  j  } t	 |  S(   N(
   R\   Ru   Ro   R   Re   Rf   R   R   R   R   (   RE   R3   R   (    (    s8   lib/python2.7/site-packages/scipy/stats/_multivariate.pyR     s    $c         C` s   |  j  j |  j |  j | |  S(   N(   R\   R   Re   Rf   (   RE   Ri   RX   (    (    s8   lib/python2.7/site-packages/scipy/stats/_multivariate.pyR     s    c         C` s,   |  j  j } |  j  j } d | t d | S(   s   
        Computes the differential entropy of the multivariate normal.

        Returns
        -------
        h : scalar
            Entropy of the multivariate normal distribution

        g      ?i   (   R   RC   R?   Rx   (   RE   RC   R?   (    (    s8   lib/python2.7/site-packages/scipy/stats/_multivariate.pyR     s    
N(   RO   RP   R!   R   RK   R   R   R   R   R   R   (    (    (    s8   lib/python2.7/site-packages/scipy/stats/_multivariate.pyRd     s   4				R   R   R   R   R   s  mean : array_like, optional
    Mean of the distribution (default: `None`)
rowcov : array_like, optional
    Among-row covariance matrix of the distribution (default: `1`)
colcov : array_like, optional
    Among-column covariance matrix of the distribution (default: `1`)
s  If `mean` is set to `None` then a matrix of zeros is used for the mean.
    The dimensions of this matrix are inferred from the shape of `rowcov` and
    `colcov`, if these are provided, or set to `1` if ambiguous.

    `rowcov` and `colcov` can be two-dimensional array_likes specifying the
    covariance matrices directly. Alternatively, a one-dimensional array will
    be be interpreted as the entries of a diagonal matrix, and a scalar or
    zero-dimensional array will be interpreted as this value times the
    identity matrix.
    t   _matnorm_doc_default_callparamst   _matnorm_doc_callparams_notet   matrix_normal_genc           B` s   e  Z d  Z d
 d  Z d
 d d d
 d  Z d   Z d   Z d   Z d
 d d d  Z	 d
 d d d  Z
 d
 d d d d
 d	  Z RS(   sd  
    A matrix normal random variable.

    The `mean` keyword specifies the mean. The `rowcov` keyword specifies the
    among-row covariance matrix. The 'colcov' keyword specifies the
    among-column covariance matrix.

    Methods
    -------
    ``pdf(X, mean=None, rowcov=1, colcov=1)``
        Probability density function.
    ``logpdf(X, mean=None, rowcov=1, colcov=1)``
        Log of the probability density function.
    ``rvs(mean=None, rowcov=1, colcov=1, size=1, random_state=None)``
        Draw random samples.

    Parameters
    ----------
    X : array_like
        Quantiles, with the last two axes of `X` denoting the components.
    %(_matnorm_doc_default_callparams)s
    %(_doc_random_state)s

    Alternatively, the object may be called (as a function) to fix the mean
    and covariance parameters, returning a "frozen" matrix normal
    random variable:

    rv = matrix_normal(mean=None, rowcov=1, colcov=1)
        - Frozen object with the same methods but holding the given
          mean and covariance fixed.

    Notes
    -----
    %(_matnorm_doc_callparams_note)s

    The covariance matrices specified by `rowcov` and `colcov` must be
    (symmetric) positive definite. If the samples in `X` are
    :math:`m \times n`, then `rowcov` must be :math:`m \times m` and
    `colcov` must be :math:`n \times n`. `mean` must be the same shape as `X`.

    The probability density function for `matrix_normal` is

    .. math::

        f(X) = (2 \pi)^{-\frac{mn}{2}}|U|^{-\frac{n}{2}} |V|^{-\frac{m}{2}}
               \exp\left( -\frac{1}{2} \mathrm{Tr}\left[ U^{-1} (X-M) V^{-1}
               (X-M)^T \right] \right),

    where :math:`M` is the mean, :math:`U` the among-row covariance matrix,
    :math:`V` the among-column covariance matrix.

    The `allow_singular` behaviour of the `multivariate_normal`
    distribution is not currently supported. Covariance matrices must be
    full rank.

    The `matrix_normal` distribution is closely related to the
    `multivariate_normal` distribution. Specifically, :math:`\mathrm{Vec}(X)`
    (the vector formed by concatenating the columns  of :math:`X`) has a
    multivariate normal distribution with mean :math:`\mathrm{Vec}(M)`
    and covariance :math:`V \otimes U` (where :math:`\otimes` is the Kronecker
    product). Sampling and pdf evaluation are
    :math:`\mathcal{O}(m^3 + n^3 + m^2 n + m n^2)` for the matrix normal, but
    :math:`\mathcal{O}(m^3 n^3)` for the equivalent multivariate normal,
    making this equivalent form algorithmically inefficient.

    .. versionadded:: 0.17.0

    Examples
    --------

    >>> from scipy.stats import matrix_normal

    >>> M = np.arange(6).reshape(3,2); M
    array([[0, 1],
           [2, 3],
           [4, 5]])
    >>> U = np.diag([1,2,3]); U
    array([[1, 0, 0],
           [0, 2, 0],
           [0, 0, 3]])
    >>> V = 0.3*np.identity(2); V
    array([[ 0.3,  0. ],
           [ 0. ,  0.3]])
    >>> X = M + 0.1; X
    array([[ 0.1,  1.1],
           [ 2.1,  3.1],
           [ 4.1,  5.1]])
    >>> matrix_normal.pdf(X, mean=M, rowcov=U, colcov=V)
    0.023410202050005054

    >>> # Equivalent multivariate normal
    >>> from scipy.stats import multivariate_normal
    >>> vectorised_X = X.T.flatten()
    >>> equiv_mean = M.T.flatten()
    >>> equiv_cov = np.kron(V,U)
    >>> multivariate_normal.pdf(vectorised_X, mean=equiv_mean, cov=equiv_cov)
    0.023410202050005054
    c         C` s2   t  t |   j |  t j |  j t  |  _ d  S(   N(   RU   R   RK   R   Rb   RQ   t   matnorm_docdict_params(   RE   RW   (    (    s8   lib/python2.7/site-packages/scipy/stats/_multivariate.pyRK     s    i   c         C` s   t  | | | d | S(   sx   
        Create a frozen matrix normal distribution.

        See `matrix_normal_frozen` for more information.

        RW   (   t   matrix_normal_frozen(   RE   Re   t   rowcovt   colcovRW   (    (    s8   lib/python2.7/site-packages/scipy/stats/_multivariate.pyRg     s    c   
      C` s  | d k	 rr t j | d t } | j } t |  d k rK t d   n  t j | d k  rr t d   qr n  t j | d t } | j d k r | d k	 r | t j	 | d  } q | t j	 d  } n! | j d k r t j
 |  } n  | j } t |  d k rt d   n  | d | d k r@t d   n  | d d k r_t d	   n  | d } t j | d t } | j d k r| d k	 r| t j	 | d  } q| t j	 d  } n! | j d k rt j
 |  } n  | j } t |  d k rt d
   n  | d | d k r7t d   n  | d d k rVt d   n  | d } | d k	 r| d | k rt d   n  | d | k rt d   qn t j | | f  } | | f }	 |	 | | | f S(   s   
        Infer dimensionality from mean or covariance matrices. Handle
        defaults. Ensure compatible dimensions.

        R"   i   s%   Array `mean` must be two dimensional.i    s   Array `mean` has invalid shape.i   s(   `rowcov` must be a scalar or a 2D array.s   Array `rowcov` must be square.s!   Array `rowcov` has invalid shape.s(   `colcov` must be a scalar or a 2D array.s   Array `colcov` must be square.s!   Array `colcov` has invalid shape.s=   Arrays `mean` and `rowcov` must have the same number of rows.s@   Arrays `mean` and `colcov` must have the same number of columns.N(   R!   R%   R   R1   Rh   R<   R;   t   anyR   t   identityRm   Rk   (
   RE   Re   R   R   t	   meanshapet   rowshapet   numrowst   colshapet   numcolst   dims(    (    s8   lib/python2.7/site-packages/scipy/stats/_multivariate.pyRs     sZ    		
	
c         C` sf   t  j | d t } | j d k r@ | t  j d d  f } n  | j d | k rb t d   n  | S(   sq   
        Adjust quantiles array so that last two axes labels the components of
        each data point.

        R"   i   NisJ   The shape of array `X` is not compatible with the distribution parameters.(   R%   R   R1   R   Rt   Rh   R;   (   RE   t   XR   (    (    s8   lib/python2.7/site-packages/scipy/stats/_multivariate.pyRu     s    c         C` s   | \ } }	 t  j | | d d d d }
 t  j | j t  j |
 |  d  } t  j t  j t  j |  d d d d } d | |	 t |	 | | | | S(   s  
        Parameters
        ----------
        dims : tuple
            Dimensions of the matrix variates
        X : ndarray
            Points at which to evaluate the log of the probability
            density function
        mean : ndarray
            Mean of the distribution
        row_prec_rt : ndarray
            A decomposition such that np.dot(row_prec_rt, row_prec_rt.T)
            is the inverse of the among-row covariance matrix
        log_det_rowcov : float
            Logarithm of the determinant of the among-row covariance matrix
        col_prec_rt : ndarray
            A decomposition such that np.dot(col_prec_rt, col_prec_rt.T)
            is the inverse of the among-column covariance matrix
        log_det_colcov : float
            Logarithm of the determinant of the among-column covariance matrix

        Notes
        -----
        As this function does no argument checking, it should not be
        called directly; use 'logpdf' instead.

        Rv   it   starti    i   g      (   R%   t   rollaxist	   tensordotRM   RL   RA   Rw   Rx   (   RE   R   R   Re   t   row_prec_rtt   log_det_rowcovt   col_prec_rtt   log_det_colcovR   R   t   roll_devt	   scale_devR|   (    (    s8   lib/python2.7/site-packages/scipy/stats/_multivariate.pyR}     s    -c   	      C` s   |  j  | | |  \ } } } } |  j | |  } t | d t } t | d t } |  j | | | | j | j | j | j  } t |  S(   s  
        Log of the matrix normal probability density function.

        Parameters
        ----------
        X : array_like
            Quantiles, with the last two axes of `X` denoting the components.
        %(_matnorm_doc_default_callparams)s

        Returns
        -------
        logpdf : ndarray
            Log of the probability density function evaluated at `X`

        Notes
        -----
        %(_matnorm_doc_callparams_note)s

        RG   (   Rs   Ru   R5   R   R}   R@   RC   R   (	   RE   R   Re   R   R   R   t   rowpsdt   colpsdR   (    (    s8   lib/python2.7/site-packages/scipy/stats/_multivariate.pyR   "  s    !c         C` s   t  j |  j | | | |   S(   s  
        Matrix normal probability density function.

        Parameters
        ----------
        X : array_like
            Quantiles, with the last two axes of `X` denoting the components.
        %(_matnorm_doc_default_callparams)s

        Returns
        -------
        pdf : ndarray
            Probability density function evaluated at `X`

        Notes
        -----
        %(_matnorm_doc_callparams_note)s

        (   R%   R   R   (   RE   R   Re   R   R   (    (    s8   lib/python2.7/site-packages/scipy/stats/_multivariate.pyR   ?  s    c         C` s  t  |  } |  j | | |  \ } } } } t j j | d t } t j j | d t } |  j |  } | j d | d | | d f  }	 t j	 | t j
 |	 | j  d  }
 t j |
 j d d d d | t j d d  d d  f } | d k r| j | j  } n  | S(   s  
        Draw random samples from a matrix normal distribution.

        Parameters
        ----------
        %(_matnorm_doc_default_callparams)s
        size : integer, optional
            Number of samples to draw (default 1).
        %(_doc_random_state)s

        Returns
        -------
        rvs : ndarray or scalar
            Random variates of size (`size`, `dims`), where `dims` is the
            dimension of the random matrices.

        Notes
        -----
        %(_matnorm_doc_callparams_note)s

        R$   Ri   i   i    Rv   R   N(   t   intRs   R7   R8   t   choleskyRR   RY   t   standard_normalR%   R   RL   RM   R   Rt   t   reshapeRh   (   RE   Re   R   R   Ri   RX   R   t   rowcholt   colcholt   std_normt   roll_rvsR   (    (    s8   lib/python2.7/site-packages/scipy/stats/_multivariate.pyR   U  s    #$>N(   RO   RP   RQ   R!   RK   Rg   Rs   Ru   R}   R   R   R   (    (    (    s8   lib/python2.7/site-packages/scipy/stats/_multivariate.pyR   ;  s   b		C		%R   c           B` s>   e  Z d d  d  d d  Z d   Z d   Z d  d d  Z RS(   i   c         C` ss   t  |  |  _ |  j j | | |  \ |  _ |  _ |  _ |  _ t |  j d t |  _	 t |  j d t |  _
 d S(   sM  
        Create a frozen matrix normal distribution.

        Parameters
        ----------
        %(_matnorm_doc_default_callparams)s
        seed : 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.

        Examples
        --------
        >>> from scipy.stats import matrix_normal

        >>> distn = matrix_normal(mean=np.zeros((3,3)))
        >>> X = distn.rvs(); X
        array([[-0.02976962,  0.93339138, -0.09663178],
               [ 0.67405524,  0.28250467, -0.93308929],
               [-0.31144782,  0.74535536,  1.30412916]])
        >>> distn.pdf(X)
        2.5160642368346784e-05
        >>> distn.logpdf(X)
        -10.590229595124615
        RG   N(   R   R\   Rs   R   Re   R   R   R5   R   R   R   (   RE   Re   R   R   RW   (    (    s8   lib/python2.7/site-packages/scipy/stats/_multivariate.pyRK   }  s    0c         C` sd   |  j  j | |  j  } |  j  j |  j | |  j |  j j |  j j |  j j |  j j  } t	 |  S(   N(
   R\   Ru   R   R}   Re   R   R@   RC   R   R   (   RE   R   R   (    (    s8   lib/python2.7/site-packages/scipy/stats/_multivariate.pyR     s
    !c         C` s   t  j |  j |   S(   N(   R%   R   R   (   RE   R   (    (    s8   lib/python2.7/site-packages/scipy/stats/_multivariate.pyR     s    c         C` s%   |  j  j |  j |  j |  j | |  S(   N(   R\   R   Re   R   R   (   RE   Ri   RX   (    (    s8   lib/python2.7/site-packages/scipy/stats/_multivariate.pyR     s    N(   RO   RP   R!   RK   R   R   R   (    (    (    s8   lib/python2.7/site-packages/scipy/stats/_multivariate.pyR   |  s    		s   alpha : array_like
    The concentration parameters. The number of entries determines the
    dimensionality of the distribution.
t!   _dirichlet_doc_default_callparamsc         C` s_   t  j |   }  t  j |   d k r3 t d   n( |  j d k r[ t d |  j f   n  |  S(   Ni    s%   All parameters must be greater than 0i   s?   Parameter vector 'a' must be one dimensional, but a.shape = %s.(   R%   R   R:   R;   R   Rh   (   t   alpha(    (    s8   lib/python2.7/site-packages/scipy/stats/_multivariate.pyt   _dirichlet_check_parameters  s    c         C` s  t  j |  } | j d d |  j d k rf | j d |  j d k rf t d |  j | j f   n  | j d |  j d k r t  j d t  j | d  g  } | j d k r t  j | |  } q | j d k r t  j | | f  } q t d   n  t  j	 |  d k  r t d   n  t  j
 |  d k rDt d   n  | d k } |  d k  } | j |  j k rt  j | | j d d	 d j | j  } n  t  j | |  } t  j |  rt d
   n  t  j t  j | d  d  d k j   rt d t  j | d    n  | S(   Ni    i   s   Vector 'x' must have either the same number of entries as, or one entry fewer than, parameter vector 'a', but alpha.shape = %s and x.shape = %s.i   sU   The input must be one dimensional or a two dimensional matrix containing the entries.s8   Each entry in 'x' must be greater than or equal to zero.s/   Each entry in 'x' must be smaller or equal one.iRv   sJ   Each entry in 'x' must be greater than zero if its alpha is less than one.g      ?g&.>sO   The input vector 'x' must lie within the normal simplex. but np.sum(x, 0) = %s.(   R%   R   Rh   R;   R0   RA   R   t   appendt   vstackR:   R(   t   repeatR   t   logical_andR)   R   (   R   R3   t   xkt   xeq0t   alphalt1t   chk(    (    s8   lib/python2.7/site-packages/scipy/stats/_multivariate.pyt   _dirichlet_check_input  s4    8".+c         C` s&   t  j t |    t t  j |    S(   sz  
    Internal helper function to compute the log of the useful quotient

    .. math::

        B(\alpha) = \frac{\prod_{i=1}{K}\Gamma(\alpha_i)}
                         {\Gamma\left(\sum_{i=1}^{K} \alpha_i \right)}

    Parameters
    ----------
    %(_dirichlet_doc_default_callparams)s

    Returns
    -------
    B : scalar
        Helper quotient, internal use only

    (   R%   RA   R   (   R   (    (    s8   lib/python2.7/site-packages/scipy/stats/_multivariate.pyt   _lnB   s    t   dirichlet_genc           B` sk   e  Z d  Z d d  Z d d  Z d   Z d   Z d   Z d   Z	 d   Z
 d   Z d	 d d
  Z RS(   sX  
    A Dirichlet random variable.

    The `alpha` keyword specifies the concentration parameters of the
    distribution.

    .. versionadded:: 0.15.0

    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.

    Parameters
    ----------
    x : array_like
        Quantiles, with the last axis of `x` denoting the components.
    %(_dirichlet_doc_default_callparams)s
    %(_doc_random_state)s

    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 :math:`\alpha` entry must be positive. The distribution has only
    support on the simplex defined by

    .. math::
        \sum_{i=1}^{K} x_i \le 1


    The probability density function for `dirichlet` is

    .. math::

        f(x) = \frac{1}{\mathrm{B}(\boldsymbol\alpha)} \prod_{i=1}^K x_i^{\alpha_i - 1}

    where

    .. math::

        \mathrm{B}(\boldsymbol\alpha) = \frac{\prod_{i=1}^K \Gamma(\alpha_i)}
                                     {\Gamma\bigl(\sum_{i=1}^K \alpha_i\bigr)}

    and :math:`\boldsymbol\alpha=(\alpha_1,\ldots,\alpha_K)`, the
    concentration parameters and :math:`K` is the dimension of the space
    where :math:`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]])

    c         C` s2   t  t |   j |  t j |  j t  |  _ d  S(   N(   RU   R   RK   R   Rb   RQ   t   dirichlet_docdict_params(   RE   RW   (    (    s8   lib/python2.7/site-packages/scipy/stats/_multivariate.pyRK     s    c         C` s   t  | d | S(   NRW   (   t   dirichlet_frozen(   RE   R   RW   (    (    s8   lib/python2.7/site-packages/scipy/stats/_multivariate.pyRg     s    c         C` s4   t  |  } | t j t | d | j  j d  S(   sc  
        Parameters
        ----------
        x : ndarray
            Points at which to evaluate the log of the probability
            density function
        %(_dirichlet_doc_default_callparams)s

        Notes
        -----
        As this function does no argument checking, it should not be
        called directly; use 'logpdf' instead.

        i   i    (   R   R%   RA   R	   RM   (   RE   R3   R   t   lnB(    (    s8   lib/python2.7/site-packages/scipy/stats/_multivariate.pyR}     s    c         C` s7   t  |  } t | |  } |  j | |  } t |  S(   s  
        Log of the Dirichlet probability density function.

        Parameters
        ----------
        x : array_like
            Quantiles, with the last axis of `x` denoting the components.
        %(_dirichlet_doc_default_callparams)s

        Returns
        -------
        pdf : ndarray or scalar
            Log of the probability density function evaluated at `x`.

        (   R   R   R}   R   (   RE   R3   R   R   (    (    s8   lib/python2.7/site-packages/scipy/stats/_multivariate.pyR     s    c         C` s@   t  |  } t | |  } t j |  j | |   } t |  S(   st  
        The Dirichlet probability density function.

        Parameters
        ----------
        x : array_like
            Quantiles, with the last axis of `x` denoting the components.
        %(_dirichlet_doc_default_callparams)s

        Returns
        -------
        pdf : ndarray or scalar
            The probability density function evaluated at `x`.

        (   R   R   R%   R   R}   R   (   RE   R3   R   R   (    (    s8   lib/python2.7/site-packages/scipy/stats/_multivariate.pyR     s    c         C` s)   t  |  } | t j |  } t |  S(   s  
        Compute the mean of the dirichlet distribution.

        Parameters
        ----------
        %(_dirichlet_doc_default_callparams)s

        Returns
        -------
        mu : ndarray or scalar
            Mean of the Dirichlet distribution.

        (   R   R%   RA   R   (   RE   R   R   (    (    s8   lib/python2.7/site-packages/scipy/stats/_multivariate.pyRe     s    c         C` sC   t  |  } t j |  } | | | | | | d } t |  S(   s  
        Compute the variance of the dirichlet distribution.

        Parameters
        ----------
        %(_dirichlet_doc_default_callparams)s

        Returns
        -------
        v : ndarray or scalar
            Variance of the Dirichlet distribution.

        i   (   R   R%   RA   R   (   RE   R   t   alpha0R   (    (    s8   lib/python2.7/site-packages/scipy/stats/_multivariate.pyt   var  s    c         C` s}   t  |  } t j |  } t |  } | j d } | | | t j j |  t j | d t j j |   } t |  S(   s  
        Compute the differential entropy of the dirichlet distribution.

        Parameters
        ----------
        %(_dirichlet_doc_default_callparams)s

        Returns
        -------
        h : scalar
            Entropy of the Dirichlet distribution

        i    i   (	   R   R%   RA   R   Rh   R7   t   specialR   R   (   RE   R   R   R   t   KR   (    (    s8   lib/python2.7/site-packages/scipy/stats/_multivariate.pyR     s    !i   c         C` s.   t  |  } |  j |  } | j | d | S(   s  
        Draw random samples from a Dirichlet distribution.

        Parameters
        ----------
        %(_dirichlet_doc_default_callparams)s
        size : int, optional
            Number of samples to draw (default 1).
        %(_doc_random_state)s

        Returns
        -------
        rvs : ndarray or scalar
            Random variates of size (`size`, `N`), where `N` is the
            dimension of the random variable.

        Ri   (   R   RY   R   (   RE   R   Ri   RX   (    (    s8   lib/python2.7/site-packages/scipy/stats/_multivariate.pyR     s    N(   RO   RP   RQ   R!   RK   Rg   R}   R   R   Re   R   R   R   (    (    (    s8   lib/python2.7/site-packages/scipy/stats/_multivariate.pyR     s   g						R   c           B` sP   e  Z d d   Z d   Z d   Z d   Z d   Z d   Z d d d  Z	 RS(	   c         C` s"   t  |  |  _ t |  |  _ d  S(   N(   R   R   R   R\   (   RE   R   RW   (    (    s8   lib/python2.7/site-packages/scipy/stats/_multivariate.pyRK      s    c         C` s   |  j  j | |  j  S(   N(   R\   R   R   (   RE   R3   (    (    s8   lib/python2.7/site-packages/scipy/stats/_multivariate.pyR   $  s    c         C` s   |  j  j | |  j  S(   N(   R\   R   R   (   RE   R3   (    (    s8   lib/python2.7/site-packages/scipy/stats/_multivariate.pyR   '  s    c         C` s   |  j  j |  j  S(   N(   R\   Re   R   (   RE   (    (    s8   lib/python2.7/site-packages/scipy/stats/_multivariate.pyRe   *  s    c         C` s   |  j  j |  j  S(   N(   R\   R   R   (   RE   (    (    s8   lib/python2.7/site-packages/scipy/stats/_multivariate.pyR   -  s    c         C` s   |  j  j |  j  S(   N(   R\   R   R   (   RE   (    (    s8   lib/python2.7/site-packages/scipy/stats/_multivariate.pyR   0  s    i   c         C` s   |  j  j |  j | |  S(   N(   R\   R   R   (   RE   Ri   RX   (    (    s8   lib/python2.7/site-packages/scipy/stats/_multivariate.pyR   3  s    N(
   RO   RP   R!   RK   R   R   Re   R   R   R   (    (    (    s8   lib/python2.7/site-packages/scipy/stats/_multivariate.pyR     s   					Re   R   R   s   df : int
    Degrees of freedom, must be greater than or equal to dimension of the
    scale matrix
scale : array_like
    Symmetric positive definite scale matrix of the distribution
t   _doc_default_callparamst   _doc_callparams_notet   wishart_genc           B` s   e  Z d  Z d d  Z d d d d  Z d   Z d   Z d   Z d   Z	 d   Z
 d   Z d	   Z d
   Z d   Z d   Z d   Z d   Z d   Z d   Z d d d  Z d   Z d   Z d   Z RS(   sx  
    A Wishart random variable.

    The `df` keyword specifies the degrees of freedom. The `scale` keyword
    specifies the scale matrix, which must be symmetric and positive definite.
    In this context, the scale matrix is often interpreted in terms of a
    multivariate normal precision matrix (the inverse of the covariance
    matrix).

    Methods
    -------
    ``pdf(x, df, scale)``
        Probability density function.
    ``logpdf(x, df, scale)``
        Log of the probability density function.
    ``rvs(df, scale, size=1, random_state=None)``
        Draw random samples from a Wishart distribution.
    ``entropy()``
        Compute the differential entropy of the Wishart distribution.

    Parameters
    ----------
    x : array_like
        Quantiles, with the last axis of `x` denoting the components.
    %(_doc_default_callparams)s
    %(_doc_random_state)s

    Alternatively, the object may be called (as a function) to fix the degrees
    of freedom and scale parameters, returning a "frozen" Wishart random
    variable:

    rv = wishart(df=1, scale=1)
        - Frozen object with the same methods but holding the given
          degrees of freedom and scale fixed.

    See Also
    --------
    invwishart, chi2

    Notes
    -----
    %(_doc_callparams_note)s

    The scale matrix `scale` must be a symmetric positive definite
    matrix. Singular matrices, including the symmetric positive semi-definite
    case, are not supported.

    The Wishart distribution is often denoted

    .. math::

        W_p(\nu, \Sigma)

    where :math:`\nu` is the degrees of freedom and :math:`\Sigma` is the
    :math:`p \times p` scale matrix.

    The probability density function for `wishart` has support over positive
    definite matrices :math:`S`; if :math:`S \sim W_p(\nu, \Sigma)`, then
    its PDF is given by:

    .. math::

        f(S) = \frac{|S|^{\frac{\nu - p - 1}{2}}}{2^{ \frac{\nu p}{2} }
               |\Sigma|^\frac{\nu}{2} \Gamma_p \left ( \frac{\nu}{2} \right )}
               \exp\left( -tr(\Sigma^{-1} S) / 2 \right)

    If :math:`S \sim W_p(\nu, \Sigma)` (Wishart) then
    :math:`S^{-1} \sim W_p^{-1}(\nu, \Sigma^{-1})` (inverse Wishart).

    If the scale matrix is 1-dimensional and equal to one, then the Wishart
    distribution :math:`W_1(\nu, 1)` collapses to the :math:`\chi^2(\nu)`
    distribution.

    .. versionadded:: 0.16.0

    References
    ----------
    .. [1] M.L. Eaton, "Multivariate Statistics: A Vector Space Approach",
           Wiley, 1983.
    .. [2] W.B. Smith and R.R. Hocking, "Algorithm AS 53: Wishart Variate
           Generator", Applied Statistics, vol. 21, pp. 341-345, 1972.

    Examples
    --------
    >>> import matplotlib.pyplot as plt
    >>> from scipy.stats import wishart, chi2
    >>> x = np.linspace(1e-5, 8, 100)
    >>> w = wishart.pdf(x, df=3, scale=1); w[:5]
    array([ 0.00126156,  0.10892176,  0.14793434,  0.17400548,  0.1929669 ])
    >>> c = chi2.pdf(x, 3); c[:5]
    array([ 0.00126156,  0.10892176,  0.14793434,  0.17400548,  0.1929669 ])
    >>> plt.plot(x, w)

    The input quantiles can be any shape of array, as long as the last
    axis labels the components.

    c         C` s2   t  t |   j |  t j |  j t  |  _ d  S(   N(   RU   R   RK   R   Rb   RQ   t   wishart_docdict_params(   RE   RW   (    (    s8   lib/python2.7/site-packages/scipy/stats/_multivariate.pyRK     s    c         C` s   t  | | |  S(   sl   
        Create a frozen Wishart distribution.

        See `wishart_frozen` for more information.

        (   t   wishart_frozen(   RE   t   dft   scaleRW   (    (    s8   lib/python2.7/site-packages/scipy/stats/_multivariate.pyRg     s    c         C` sJ  | d  k r d } n  t j | d t } | j d k rR | t j t j f } n | j d k rs t j |  } nk | j d k r | j d | j d k r t d t	 | j    n% | j d k r t d | j   n  | j d } | d  k r | } n= t j
 |  st d   n | | k  r=t d	 |   n  | | | f S(
   Ng      ?R"   i    i   i   sL   Array 'scale' must be square if it is two dimensional, but scale.scale = %s.sB   Array 'scale' must be at most two-dimensional, but scale.ndim = %ds$   Degrees of freedom must be a scalar.sM   Degrees of freedom cannot be less than dimension of scale matrix, but df = %d(   R!   R%   R   R1   R   Rt   Rm   Rh   R;   Rn   Rj   (   RE   R   R   Ro   (    (    s8   lib/python2.7/site-packages/scipy/stats/_multivariate.pyRs     s,    	*	c         C` s  t  j | d t } | j d k rV | t  j |  d d  d d  t  j f } n  | j d k r | d k r | t  j t  j d d  f } qt  j |  d d  d d  t  j f } n | j d k r+| j d | j d k st d t	 | j    n  | d d  d d  t  j f } nm | j d k rs| j d | j d k st d t	 | j    qn% | j d k rt d	 | j   n  | j d d !| | f k st d
 | | f | j d d !f   n  | S(   sl   
        Adjust quantiles array so that last axis labels the components of
        each data point.
        R"   i    Ni   i   sG   Quantiles must be square if they are two dimensional, but x.shape = %s.i   se   Quantiles must be square in the first two dimensions if they are three dimensional, but x.shape = %s.sn   Quantiles must be at most two-dimensional with an additional dimension for multiplecomponents, but x.ndim = %ds=   Quantiles have incompatible dimensions: should be %s, got %s.(
   R%   R   R1   R   Rl   Rt   Rm   Rh   R;   Rn   (   RE   R3   Ro   (    (    s8   lib/python2.7/site-packages/scipy/stats/_multivariate.pyRu     s.    2".%#c         C` s~   t  j |  } | j d k r. | t  j } n. | j d k r\ t d t t |     n  | j   } t |  } | | f S(   Ni    i   s_   Size must be an integer or tuple of integers; thus must have dimension <= 1. Got size.ndim = %s(   R%   R   R   Rt   R;   Rn   t   tuplet   prod(   RE   Ri   t   nRh   (    (    s8   lib/python2.7/site-packages/scipy/stats/_multivariate.pyt   _process_size  s    c         C` sS  t  j | j d  } t  j | j  } t  j | j d  }	 x t | j d  D] }
 |  j | d d  d d  |
 f  \ } | |
 <t j j | t f | d d  d d  |
 f  | d d  d d  |
 f <| d d  d d  |
 f j	   |	 |
 <qR Wd | | d | d |	 d | | t
 d | | t d | |  } | S(   s  
        Parameters
        ----------
        x : ndarray
            Points at which to evaluate the log of the probability
            density function
        dim : int
            Dimension of the scale matrix
        df : int
            Degrees of freedom
        scale : ndarray
            Scale matrix
        log_det_scale : float
            Logarithm of the determinant of the scale matrix
        C : ndarray
            Cholesky factorization of the scale matrix, lower triagular.

        Notes
        -----
        As this function does no argument checking, it should not be
        called directly; use 'logpdf' instead.

        iNg      ?i   (   R%   Rk   Rh   t   ranget   _cholesky_logdetR7   R8   t	   cho_solveRR   t   tracet   _LOG_2R   (   RE   R3   Ro   R   R   t   log_det_scalet   Ct	   log_det_xt   scale_inv_xt   tr_scale_inv_xt   iR   R   (    (    s8   lib/python2.7/site-packages/scipy/stats/_multivariate.pyR}     s    2M-c         C` sj   |  j  | |  \ } } } |  j | |  } |  j |  \ } } |  j | | | | | |  } t |  S(   s  
        Log of the Wishart probability density function.

        Parameters
        ----------
        x : array_like
            Quantiles, with the last axis of `x` denoting the components.
            Each quantile must be a symmetric positive definite matrix.
        %(_doc_default_callparams)s

        Returns
        -------
        pdf : ndarray
            Log of the probability density function evaluated at `x`

        Notes
        -----
        %(_doc_callparams_note)s

        (   Rs   Ru   R   R}   R   (   RE   R3   R   R   Ro   R   R   R   (    (    s8   lib/python2.7/site-packages/scipy/stats/_multivariate.pyR   J  s
    c         C` s   t  j |  j | | |   S(   s  
        Wishart probability density function.

        Parameters
        ----------
        x : array_like
            Quantiles, with the last axis of `x` denoting the components.
            Each quantile must be a symmetric positive definite matrix.
        %(_doc_default_callparams)s

        Returns
        -------
        pdf : ndarray
            Probability density function evaluated at `x`

        Notes
        -----
        %(_doc_callparams_note)s

        (   R%   R   R   (   RE   R3   R   R   (    (    s8   lib/python2.7/site-packages/scipy/stats/_multivariate.pyR   h  s    c         C` s   | | S(   s  
        Parameters
        ----------
        dim : int
            Dimension of the scale matrix
        %(_doc_default_callparams)s

        Notes
        -----
        As this function does no argument checking, it should not be
        called directly; use 'mean' instead.

        (    (   RE   Ro   R   R   (    (    s8   lib/python2.7/site-packages/scipy/stats/_multivariate.pyt   _mean  s    c         C` s:   |  j  | |  \ } } } |  j | | |  } t |  S(   s   
        Mean of the Wishart distribution

        Parameters
        ----------
        %(_doc_default_callparams)s

        Returns
        -------
        mean : float
            The mean of the distribution
        (   Rs   R   R   (   RE   R   R   Ro   R   (    (    s8   lib/python2.7/site-packages/scipy/stats/_multivariate.pyRe     s    c         C` s/   | | d k r% | | d | } n d } | S(   s  
        Parameters
        ----------
        dim : int
            Dimension of the scale matrix
        %(_doc_default_callparams)s

        Notes
        -----
        As this function does no argument checking, it should not be
        called directly; use 'mode' instead.

        i   N(   R!   (   RE   Ro   R   R   R   (    (    s8   lib/python2.7/site-packages/scipy/stats/_multivariate.pyt   _mode  s    c         C` sJ   |  j  | |  \ } } } |  j | | |  } | d k	 rF t |  S| S(   sN  
        Mode of the Wishart distribution

        Only valid if the degrees of freedom are greater than the dimension of
        the scale matrix.

        Parameters
        ----------
        %(_doc_default_callparams)s

        Returns
        -------
        mode : float or None
            The Mode of the distribution
        N(   Rs   R   R!   R   (   RE   R   R   Ro   R   (    (    s8   lib/python2.7/site-packages/scipy/stats/_multivariate.pyt   mode  s    c         C` s:   | d } | j    } | t j | |  7} | | 9} | S(   s  
        Parameters
        ----------
        dim : int
            Dimension of the scale matrix
        %(_doc_default_callparams)s

        Notes
        -----
        As this function does no argument checking, it should not be
        called directly; use 'var' instead.

        i   (   t   diagonalR%   t   outer(   RE   Ro   R   R   R   Rm   (    (    s8   lib/python2.7/site-packages/scipy/stats/_multivariate.pyt   _var  s
    

c         C` s:   |  j  | |  \ } } } |  j | | |  } t |  S(   s   
        Variance of the Wishart distribution

        Parameters
        ----------
        %(_doc_default_callparams)s

        Returns
        -------
        var : float
            The variance of the distribution
        (   Rs   R   R   (   RE   R   R   Ro   R   (    (    s8   lib/python2.7/site-packages/scipy/stats/_multivariate.pyR     s    c         C` s  | | d d } | j  d | |  j | | f  } t j g  t |  D]+ } | j | | d d d | d ^ qK j | f | d d d   j }	 t j | | | f  }
 t t	 d d d  g t |   } t j | d d } | |
 | | <t j |  } |	 |
 | | <|
 S(   s$  
        Parameters
        ----------
        n : integer
            Number of variates to generate
        shape : iterable
            Shape of the variates to generate
        dim : int
            Dimension of the scale matrix
        df : int
            Degrees of freedom
        random_state : np.random.RandomState instance
            RandomState used for drawing the random variates.

        Notes
        -----
        As this function does no argument checking, it should not be
        called directly; use 'rvs' instead.

        i   i   Ri   g      ?Nit   k(   t   normalR   R%   t   r_R   t	   chisquareRM   Rk   R   t   sliceR!   R<   t   tril_indicest   diag_indices(   RE   R   Rh   Ro   R   RX   t   n_trilt   covariancesR   t	   variancest   At   size_idxt   tril_idxt   diag_idx(    (    s8   lib/python2.7/site-packages/scipy/stats/_multivariate.pyt   _standard_rvs  s    		B%c   
      C` sw   |  j  |  } |  j | | | | |  } xF t j |  D]5 } t j | | |  }	 t j |	 |	 j  | | <q: W| S(   s]  
        Parameters
        ----------
        n : integer
            Number of variates to generate
        shape : iterable
            Shape of the variates to generate
        dim : int
            Dimension of the scale matrix
        df : int
            Degrees of freedom
        scale : ndarray
            Scale matrix
        C : ndarray
            Cholesky factorization of the scale matrix, lower triangular.
        %(_doc_random_state)s

        Notes
        -----
        As this function does no argument checking, it should not be
        called directly; use 'rvs' instead.

        (   RY   R   R%   t   ndindexRL   RM   (
   RE   R   Rh   Ro   R   R   RX   R   t   indext   CA(    (    s8   lib/python2.7/site-packages/scipy/stats/_multivariate.pyt   _rvs  s    i   c   
      C` sp   |  j  |  \ } } |  j | |  \ } } } t j j | d t } |  j | | | | | |  }	 t |	  S(   s
  
        Draw random samples from a Wishart distribution.

        Parameters
        ----------
        %(_doc_default_callparams)s
        size : integer or iterable of integers, optional
            Number of samples to draw (default 1).
        %(_doc_random_state)s

        Returns
        -------
        rvs : ndarray
            Random variates of shape (`size`) + (`dim`, `dim), where `dim` is
            the dimension of the scale matrix.

        Notes
        -----
        %(_doc_callparams_note)s

        R$   (   R   Rs   R7   R8   R   RR   R   R   (
   RE   R   R   Ri   RX   R   Rh   Ro   R   R   (    (    s8   lib/python2.7/site-packages/scipy/stats/_multivariate.pyR   H  s
    c      
   C` s   d | d | d | | d t  t d | |  d | | d t j g  t |  D]" } t d | d | d  ^ qV  d | | S(   s  
        Parameters
        ----------
        dim : int
            Dimension of the scale matrix
        df : int
            Degrees of freedom
        log_det_scale : float
            Logarithm of the determinant of the scale matrix

        Notes
        -----
        As this function does no argument checking, it should not be
        called directly; use 'entropy' instead.

        g      ?i   (   R   R   R%   RA   R   R   (   RE   Ro   R   R   R   (    (    s8   lib/python2.7/site-packages/scipy/stats/_multivariate.pyt   _entropyh  s    c         C` sC   |  j  | |  \ } } } |  j |  \ } } |  j | | |  S(   s0  
        Compute the differential entropy of the Wishart.

        Parameters
        ----------
        %(_doc_default_callparams)s

        Returns
        -------
        h : scalar
            Entropy of the Wishart distribution

        Notes
        -----
        %(_doc_callparams_note)s

        (   Rs   R   R   (   RE   R   R   Ro   R   R   (    (    s8   lib/python2.7/site-packages/scipy/stats/_multivariate.pyR     s    c         C` sD   t  j j | d t } d t j t j | j     } | | f S(   s  
        Compute Cholesky decomposition and determine (log(det(scale)).

        Parameters
        ----------
        scale : ndarray
            Scale matrix.

        Returns
        -------
        c_decomp : ndarray
            The Cholesky decomposition of `scale`.
        logdet : scalar
            The log of the determinant of `scale`.

        Notes
        -----
        This computation of ``logdet`` is equivalent to
        ``np.linalg.slogdet(scale)``.  It is ~2x faster though.

        R$   i   (   R7   R8   R   RR   R%   RA   RB   R   (   RE   R   t   c_decompR   (    (    s8   lib/python2.7/site-packages/scipy/stats/_multivariate.pyR     s    "N(   RO   RP   RQ   R!   RK   Rg   Rs   Ru   R   R}   R   R   R   Re   R   R   R   R   R   R   R   R   R   R   (    (    (    s8   lib/python2.7/site-packages/scipy/stats/_multivariate.pyR   ]  s*   a			%		-									-	. 		R   c           B` s_   e  Z d  Z d
 d  Z d   Z d   Z d   Z d   Z d   Z	 d d
 d  Z
 d	   Z RS(   s  
    Create a frozen Wishart distribution.

    Parameters
    ----------
    df : array_like
        Degrees of freedom of the distribution
    scale : array_like
        Scale matrix of the distribution
    seed : None or int or np.random.RandomState instance, optional
        This parameter defines the RandomState object to use for drawing
        random variates.
        If None (or np.random), the global np.random state is used.
        If integer, it is used to seed the local RandomState instance
        Default is None.

    c         C` s[   t  |  |  _ |  j j | |  \ |  _ |  _ |  _ |  j j |  j  \ |  _ |  _ d  S(   N(	   R   R\   Rs   Ro   R   R   R   R   R   (   RE   R   R   RW   (    (    s8   lib/python2.7/site-packages/scipy/stats/_multivariate.pyRK     s    	c         C` sR   |  j  j | |  j  } |  j  j | |  j |  j |  j |  j |  j  } t |  S(   N(	   R\   Ru   Ro   R}   R   R   R   R   R   (   RE   R3   R   (    (    s8   lib/python2.7/site-packages/scipy/stats/_multivariate.pyR     s    c         C` s   t  j |  j |   S(   N(   R%   R   R   (   RE   R3   (    (    s8   lib/python2.7/site-packages/scipy/stats/_multivariate.pyR     s    c         C` s+   |  j  j |  j |  j |  j  } t |  S(   N(   R\   R   Ro   R   R   R   (   RE   R   (    (    s8   lib/python2.7/site-packages/scipy/stats/_multivariate.pyRe     s    !c         C` s;   |  j  j |  j |  j |  j  } | d  k	 r7 t |  S| S(   N(   R\   R   Ro   R   R   R!   R   (   RE   R   (    (    s8   lib/python2.7/site-packages/scipy/stats/_multivariate.pyR     s    !c         C` s+   |  j  j |  j |  j |  j  } t |  S(   N(   R\   R   Ro   R   R   R   (   RE   R   (    (    s8   lib/python2.7/site-packages/scipy/stats/_multivariate.pyR     s    !i   c         C` sL   |  j  j |  \ } } |  j  j | | |  j |  j |  j |  } t |  S(   N(   R\   R   R   Ro   R   R   R   (   RE   Ri   RX   R   Rh   R   (    (    s8   lib/python2.7/site-packages/scipy/stats/_multivariate.pyR     s    c         C` s   |  j  j |  j |  j |  j  S(   N(   R\   R   Ro   R   R   (   RE   (    (    s8   lib/python2.7/site-packages/scipy/stats/_multivariate.pyR     s    N(   RO   RP   RQ   R!   RK   R   R   Re   R   R   R   R   (    (    (    s8   lib/python2.7/site-packages/scipy/stats/_multivariate.pyR     s   					R   c   	   	   C` s  | r t  |   } n t |   } t | j  d k  sP | j d | j d k r_ t d   n  t d | f  \ } } t j |  j d d d \ } } xt j | j d   D] } | | | d	 t	 d
 t
 d t
 \ | | <} | d k rt d |   n  | d k  r!t d |   n  | | | d	 t	 d t
 \ | | <} | d k rbt d   n  | d k  rt d |   n  | | | | f | | | | f <q W| S(   s*  
    Invert the matrices a_i, using a Cholesky factorization of A, where
    a_i resides in the last two dimensions of a and the other indices describe
    the index i.

    Overwrites the data in a.

    Parameters
    ----------
    a : array
        Array of matrices to invert, where the matrices themselves are stored
        in the last two dimensions.
    check_finite : bool, optional
        Whether to check that the input matrices contain only finite numbers.
        Disabling may give a performance gain, but may result in problems
        (crashes, non-termination) if the inputs do contain infinities or NaNs.

    Returns
    -------
    x : array
        Array of inverses of the matrices ``a_i``.

    See also
    --------
    scipy.linalg.cholesky : Cholesky factorization of a matrix

    i   iis-   expected square matrix in last two dimensionst   potrft   potriR   i   R$   t   overwrite_at   cleani    s)   %d-th leading minor not positive definites1   illegal value in %d-th argument of internal potrft   overwrite_cs!   the inverse could not be computed(   R  R  (   R   R   R<   Rh   R;   R   R%   t   triu_indicesR   RR   R   R   (	   t   aR6   t   a1R  R  t	   triu_rowst	   triu_colsR   t   info(    (    s8   lib/python2.7/site-packages/scipy/stats/_multivariate.pyt   _cho_inv_batch  s0    /"&&t   invwishart_genc           B` s   e  Z d  Z d d  Z d d d d  Z d   Z d   Z d   Z d   Z	 d   Z
 d   Z d	   Z d
   Z d   Z d   Z d d d  Z d   Z RS(   s  
    An inverse Wishart random variable.

    The `df` keyword specifies the degrees of freedom. The `scale` keyword
    specifies the scale matrix, which must be symmetric and positive definite.
    In this context, the scale matrix is often interpreted in terms of a
    multivariate normal covariance matrix.

    Methods
    -------
    ``pdf(x, df, scale)``
        Probability density function.
    ``logpdf(x, df, scale)``
        Log of the probability density function.
    ``rvs(df, scale, size=1, random_state=None)``
        Draw random samples from an inverse Wishart distribution.

    Parameters
    ----------
    x : array_like
        Quantiles, with the last axis of `x` denoting the components.
    %(_doc_default_callparams)s
    %(_doc_random_state)s

    Alternatively, the object may be called (as a function) to fix the degrees
    of freedom and scale parameters, returning a "frozen" inverse Wishart
    random variable:

    rv = invwishart(df=1, scale=1)
        - Frozen object with the same methods but holding the given
          degrees of freedom and scale fixed.

    See Also
    --------
    wishart

    Notes
    -----
    %(_doc_callparams_note)s

    The scale matrix `scale` must be a symmetric positive definite
    matrix. Singular matrices, including the symmetric positive semi-definite
    case, are not supported.

    The inverse Wishart distribution is often denoted

    .. math::

        W_p^{-1}(\nu, \Psi)

    where :math:`\nu` is the degrees of freedom and :math:`\Psi` is the
    :math:`p \times p` scale matrix.

    The probability density function for `invwishart` has support over positive
    definite matrices :math:`S`; if :math:`S \sim W^{-1}_p(\nu, \Sigma)`,
    then its PDF is given by:

    .. math::

        f(S) = \frac{|\Sigma|^\frac{\nu}{2}}{2^{ \frac{\nu p}{2} }
               |S|^{\frac{\nu + p + 1}{2}} \Gamma_p \left(\frac{\nu}{2} \right)}
               \exp\left( -tr(\Sigma S^{-1}) / 2 \right)

    If :math:`S \sim W_p^{-1}(\nu, \Psi)` (inverse Wishart) then
    :math:`S^{-1} \sim W_p(\nu, \Psi^{-1})` (Wishart).

    If the scale matrix is 1-dimensional and equal to one, then the inverse
    Wishart distribution :math:`W_1(\nu, 1)` collapses to the
    inverse Gamma distribution with parameters shape = :math:`\frac{\nu}{2}`
    and scale = :math:`\frac{1}{2}`.

    .. versionadded:: 0.16.0

    References
    ----------
    .. [1] M.L. Eaton, "Multivariate Statistics: A Vector Space Approach",
           Wiley, 1983.
    .. [2] M.C. Jones, "Generating Inverse Wishart Matrices", Communications
           in Statistics - Simulation and Computation, vol. 14.2, pp.511-514,
           1985.

    Examples
    --------
    >>> import matplotlib.pyplot as plt
    >>> from scipy.stats import invwishart, invgamma
    >>> x = np.linspace(0.01, 1, 100)
    >>> iw = invwishart.pdf(x, df=6, scale=1)
    >>> iw[:3]
    array([  1.20546865e-15,   5.42497807e-06,   4.45813929e-03])
    >>> ig = invgamma.pdf(x, 6/2., scale=1./2)
    >>> ig[:3]
    array([  1.20546865e-15,   5.42497807e-06,   4.45813929e-03])
    >>> plt.plot(x, iw)

    The input quantiles can be any shape of array, as long as the last
    axis labels the components.

    c         C` s2   t  t |   j |  t j |  j t  |  _ d  S(   N(   RU   R  RK   R   Rb   RQ   R   (   RE   RW   (    (    s8   lib/python2.7/site-packages/scipy/stats/_multivariate.pyRK   	  s    c         C` s   t  | | |  S(   sw   
        Create a frozen inverse Wishart distribution.

        See `invwishart_frozen` for more information.

        (   t   invwishart_frozen(   RE   R   R   RW   (    (    s8   lib/python2.7/site-packages/scipy/stats/_multivariate.pyRg   	  s    c         C` sK  t  j | j d  } t  j |  j } | d k rA t |  n
 d | } t  j | j d  } x t | j d  D] }	 t j j	 | d d  d d  |	 f d t
 \ }
 } d t  j t  j |
 j     | |	 <t  j | | |	  j   | |	 <qu Wd | | d | d | | t d | | d | t d | |  } | S(   s.  
        Parameters
        ----------
        x : ndarray
            Points at which to evaluate the log of the probability
            density function.
        dim : int
            Dimension of the scale matrix
        df : int
            Degrees of freedom
        scale : ndarray
            Scale matrix
        log_det_scale : float
            Logarithm of the determinant of the scale matrix

        Notes
        -----
        As this function does no argument checking, it should not be
        called directly; use 'logpdf' instead.

        ii   g      ?NR$   i   g      ?(   R%   Rk   Rh   t   copyRM   R  R   R7   R8   t
   cho_factorRR   RA   RB   R   RL   R   R   R   (   RE   R3   Ro   R   R   R   R   t   x_invt   tr_scale_x_invR   R   R$   R   (    (    s8   lib/python2.7/site-packages/scipy/stats/_multivariate.pyR}   	  s    
7&$7c         C` sg   |  j  | |  \ } } } |  j | |  } |  j |  \ } } |  j | | | | |  } t |  S(   s  
        Log of the inverse Wishart probability density function.

        Parameters
        ----------
        x : array_like
            Quantiles, with the last axis of `x` denoting the components.
            Each quantile must be a symmetric positive definite matrix.
        %(_doc_default_callparams)s

        Returns
        -------
        pdf : ndarray
            Log of the probability density function evaluated at `x`

        Notes
        -----
        %(_doc_callparams_note)s

        (   Rs   Ru   R   R}   R   (   RE   R3   R   R   Ro   R   R   R   (    (    s8   lib/python2.7/site-packages/scipy/stats/_multivariate.pyR   	  s
    c         C` s   t  j |  j | | |   S(   s  
        Inverse Wishart probability density function.

        Parameters
        ----------
        x : array_like
            Quantiles, with the last axis of `x` denoting the components.
            Each quantile must be a symmetric positive definite matrix.

        %(_doc_default_callparams)s

        Returns
        -------
        pdf : ndarray
            Probability density function evaluated at `x`

        Notes
        -----
        %(_doc_callparams_note)s

        (   R%   R   R   (   RE   R3   R   R   (    (    s8   lib/python2.7/site-packages/scipy/stats/_multivariate.pyR   	  s    c         C` s/   | | d k r% | | | d } n d } | S(   s  
        Parameters
        ----------
        dim : int
            Dimension of the scale matrix
        %(_doc_default_callparams)s

        Notes
        -----
        As this function does no argument checking, it should not be
        called directly; use 'mean' instead.

        i   N(   R!   (   RE   Ro   R   R   R   (    (    s8   lib/python2.7/site-packages/scipy/stats/_multivariate.pyR   
  s    c         C` sJ   |  j  | |  \ } } } |  j | | |  } | d k	 rF t |  S| S(   s`  
        Mean of the inverse Wishart distribution

        Only valid if the degrees of freedom are greater than the dimension of
        the scale matrix plus one.

        Parameters
        ----------
        %(_doc_default_callparams)s

        Returns
        -------
        mean : float or None
            The mean of the distribution

        N(   Rs   R   R!   R   (   RE   R   R   Ro   R   (    (    s8   lib/python2.7/site-packages/scipy/stats/_multivariate.pyRe   
  s    c         C` s   | | | d S(   s  
        Parameters
        ----------
        dim : int
            Dimension of the scale matrix
        %(_doc_default_callparams)s

        Notes
        -----
        As this function does no argument checking, it should not be
        called directly; use 'mode' instead.

        i   (    (   RE   Ro   R   R   (    (    s8   lib/python2.7/site-packages/scipy/stats/_multivariate.pyR   1
  s    c         C` s:   |  j  | |  \ } } } |  j | | |  } t |  S(   s   
        Mode of the inverse Wishart distribution

        Parameters
        ----------
        %(_doc_default_callparams)s

        Returns
        -------
        mode : float
            The Mode of the distribution

        (   Rs   R   R   (   RE   R   R   Ro   R   (    (    s8   lib/python2.7/site-packages/scipy/stats/_multivariate.pyR   A
  s    c         C` s   | | d k r | | d | d } | j    } | | | d t j | |  7} | | | | | d d | | d } n d } | S(   s  
        Parameters
        ----------
        dim : int
            Dimension of the scale matrix
        %(_doc_default_callparams)s

        Notes
        -----
        As this function does no argument checking, it should not be
        called directly; use 'var' instead.

        i   i   i   N(   R   R%   R   R!   (   RE   Ro   R   R   R   Rm   (    (    s8   lib/python2.7/site-packages/scipy/stats/_multivariate.pyR   S
  s    "-c         C` sJ   |  j  | |  \ } } } |  j | | |  } | d k	 rF t |  S| S(   s`  
        Variance of the inverse Wishart distribution

        Only valid if the degrees of freedom are greater than the dimension of
        the scale matrix plus three.

        Parameters
        ----------
        %(_doc_default_callparams)s

        Returns
        -------
        var : float
            The variance of the distribution
        N(   Rs   R   R!   R   (   RE   R   R   Ro   R   (    (    s8   lib/python2.7/site-packages/scipy/stats/_multivariate.pyR   j
  s    c         C` s  |  j  |  } t t |   j | | | | |  } t j |  } t d | f  }	 x t j | j d   D] }
 t j	 | | |
  } | d k r |	 | | d t
 \ } } | d k r t d   n  | d k  r t d |   q n
 d | } t j	 | j |  | |
 <qk W| S(	   s+  
        Parameters
        ----------
        n : integer
            Number of variates to generate
        shape : iterable
            Shape of the variates to generate
        dim : int
            Dimension of the scale matrix
        df : int
            Degrees of freedom
        C : ndarray
            Cholesky factorization of the scale matrix, lower triagular.
        %(_doc_random_state)s

        Notes
        -----
        As this function does no argument checking, it should not be
        called directly; use 'rvs' instead.

        t   trtrsii   R$   i    s   Singular matrix.s1   Illegal value in %d-th argument of internal trtrsg      ?(   RY   RU   R  R   R%   Rl   R   R   Rh   RL   RR   R   R;   RM   (   RE   R   Rh   Ro   R   R   RX   R   Rl   R  R   R   R  (    (    s8   lib/python2.7/site-packages/scipy/stats/_multivariate.pyR   ~
  s"    
i   c         C` s   |  j  |  \ } } |  j | |  \ } } } t j |  } t j j | d t \ }	 }
 t j j |	 |
 f |  } t j j	 | d t } |  j
 | | | | | |  } t |  S(   s  
        Draw random samples from an inverse Wishart distribution.

        Parameters
        ----------
        %(_doc_default_callparams)s
        size : integer or iterable of integers, optional
            Number of samples to draw (default 1).
        %(_doc_random_state)s

        Returns
        -------
        rvs : ndarray
            Random variates of shape (`size`) + (`dim`, `dim), where `dim` is
            the dimension of the scale matrix.

        Notes
        -----
        %(_doc_callparams_note)s

        R$   (   R   Rs   R%   Rl   R7   R8   R  RR   R   R   R   R   (   RE   R   R   Ri   RX   R   Rh   Ro   Rl   t   LR$   t	   inv_scaleR   R   (    (    s8   lib/python2.7/site-packages/scipy/stats/_multivariate.pyR   
  s    c         C` s
   t   d  S(   N(   t   AttributeError(   RE   (    (    s8   lib/python2.7/site-packages/scipy/stats/_multivariate.pyR   
  s    N(   RO   RP   RQ   R!   RK   Rg   R}   R   R   R   Re   R   R   R   R   R   R   R   (    (    (    s8   lib/python2.7/site-packages/scipy/stats/_multivariate.pyR  8	  s   b		,									1$R  c           B` sY   e  Z d	 d   Z d   Z d   Z d   Z d   Z d   Z d d	 d  Z	 d   Z
 RS(
   c         C` s   t  |  |  _ |  j j | |  \ |  _ |  _ |  _ t j j |  j d t	 \ } } d t
 j t
 j | j     |  _ t
 j |  j  } t j j | | f |  |  _ t j j |  j d t	 |  _ d S(   sZ  
        Create a frozen inverse Wishart distribution.

        Parameters
        ----------
        df : array_like
            Degrees of freedom of the distribution
        scale : array_like
            Scale matrix of the distribution
        seed : None or int or np.random.RandomState instance, optional
            This parameter defines the RandomState object to use for drawing
            random variates.
            If None (or np.random), the global np.random state is used.
            If integer, it is used to seed the local RandomState instance
            Default is None.

        R$   i   N(   R  R\   Rs   Ro   R   R   R7   R8   R  RR   R%   RA   RB   R   R   Rl   R   R  R   R   (   RE   R   R   RW   R   R$   Rl   (    (    s8   lib/python2.7/site-packages/scipy/stats/_multivariate.pyRK   
  s    	!%c         C` sL   |  j  j | |  j  } |  j  j | |  j |  j |  j |  j  } t |  S(   N(   R\   Ru   Ro   R}   R   R   R   R   (   RE   R3   R   (    (    s8   lib/python2.7/site-packages/scipy/stats/_multivariate.pyR   
  s    c         C` s   t  j |  j |   S(   N(   R%   R   R   (   RE   R3   (    (    s8   lib/python2.7/site-packages/scipy/stats/_multivariate.pyR     s    c         C` s;   |  j  j |  j |  j |  j  } | d  k	 r7 t |  S| S(   N(   R\   R   Ro   R   R   R!   R   (   RE   R   (    (    s8   lib/python2.7/site-packages/scipy/stats/_multivariate.pyRe     s    !c         C` s+   |  j  j |  j |  j |  j  } t |  S(   N(   R\   R   Ro   R   R   R   (   RE   R   (    (    s8   lib/python2.7/site-packages/scipy/stats/_multivariate.pyR     s    !c         C` s;   |  j  j |  j |  j |  j  } | d  k	 r7 t |  S| S(   N(   R\   R   Ro   R   R   R!   R   (   RE   R   (    (    s8   lib/python2.7/site-packages/scipy/stats/_multivariate.pyR     s    !i   c         C` sL   |  j  j |  \ } } |  j  j | | |  j |  j |  j |  } t |  S(   N(   R\   R   R   Ro   R   R   R   (   RE   Ri   RX   R   Rh   R   (    (    s8   lib/python2.7/site-packages/scipy/stats/_multivariate.pyR     s    c         C` s
   t   d  S(   N(   R  (   RE   (    (    s8   lib/python2.7/site-packages/scipy/stats/_multivariate.pyR     s    N(   RO   RP   R!   RK   R   R   Re   R   R   R   R   (    (    (    s8   lib/python2.7/site-packages/scipy/stats/_multivariate.pyR  
  s   "					ss   n : int
    Number of trials
p : array_like
    Probability of a trial falling into each category; should sum to 1
s  `n` should be a positive integer. Each element of `p` should be in the
interval :math:`[0,1]` and the elements should sum to 1. If they do not sum to
1, the last element of the `p` array is not used and is replaced with the
remaining probability left over from the earlier elements.
t   multinomial_genc           B` s   e  Z d  Z d d  Z d d  Z d   Z d   Z d   Z d   Z	 d   Z
 d   Z d	   Z d
   Z d   Z d d d  Z RS(   sc  
    A multinomial random variable.

    Methods
    -------
    ``pmf(x, n, p)``
        Probability mass function.
    ``logpmf(x, n, p)``
        Log of the probability mass function.
    ``rvs(n, p, size=1, random_state=None)``
        Draw random samples from a multinomial distribution.
    ``entropy(n, p)``
        Compute the entropy of the multinomial distribution.
    ``cov(n, p)``
        Compute the covariance matrix of the multinomial distribution.

    Parameters
    ----------
    x : array_like
        Quantiles, with the last axis of `x` denoting the components.
    %(_doc_default_callparams)s
    %(_doc_random_state)s

    Notes
    -----
    %(_doc_callparams_note)s

    Alternatively, the object may be called (as a function) to fix the `n` and
    `p` parameters, returning a "frozen" multinomial random variable:

    The probability mass function for `multinomial` is

    .. math::

        f(x) = \frac{n!}{x_1! \cdots x_k!} p_1^{x_1} \cdots p_k^{x_k},

    supported on :math:`x=(x_1, \ldots, x_k)` where each :math:`x_i` is a
    nonnegative integer and their sum is :math:`n`.

    .. versionadded:: 0.19.0

    Examples
    --------

    >>> from scipy.stats import multinomial
    >>> rv = multinomial(8, [0.3, 0.2, 0.5])
    >>> rv.pmf([1, 3, 4])
    0.042000000000000072

    The multinomial distribution for :math:`k=2` is identical to the
    corresponding binomial distribution (tiny numerical differences
    notwithstanding):

    >>> from scipy.stats import binom
    >>> multinomial.pmf([3, 4], n=7, p=[0.4, 0.6])
    0.29030399999999973
    >>> binom.pmf(3, 7, 0.4)
    0.29030400000000012

    The functions ``pmf``, ``logpmf``, ``entropy``, and ``cov`` support
    broadcasting, under the convention that the vector parameters (``x`` and
    ``p``) are interpreted as if each row along the last axis is a single
    object. For instance:

    >>> multinomial.pmf([[3, 4], [3, 5]], n=[7, 8], p=[.3, .7])
    array([0.2268945,  0.25412184])

    Here, ``x.shape == (2, 2)``, ``n.shape == (2,)``, and ``p.shape == (2,)``,
    but following the rules mentioned above they behave as if the rows
    ``[3, 4]`` and ``[3, 5]`` in ``x`` and ``[.3, .7]`` in ``p`` were a single
    object, and as if we had ``x.shape = (2,)``, ``n.shape = (2,)``, and
    ``p.shape = ()``. To obtain the individual elements without broadcasting,
    we would do this:

    >>> multinomial.pmf([3, 4], n=7, p=[.3, .7])
    0.2268945
    >>> multinomial.pmf([3, 5], 8, p=[.3, .7])
    0.25412184

    This broadcasting also works for ``cov``, where the output objects are
    square matrices of size ``p.shape[-1]``. For example:

    >>> multinomial.cov([4, 5], [[.3, .7], [.4, .6]])
    array([[[ 0.84, -0.84],
            [-0.84,  0.84]],
           [[ 1.2 , -1.2 ],
            [-1.2 ,  1.2 ]]])

    In this example, ``n.shape == (2,)`` and ``p.shape == (2, 2)``, and
    following the rules above, these broadcast as if ``p.shape == (2,)``.
    Thus the result should also be of shape ``(2,)``, but since each output is
    a :math:`2 \times 2` matrix, the result in fact has shape ``(2, 2, 2)``,
    where ``result[0]`` is equal to ``multinomial.cov(n=4, p=[.3, .7])`` and
    ``result[1]`` is equal to ``multinomial.cov(n=5, p=[.4, .6])``.

    See also
    --------
    scipy.stats.binom : The binomial distribution.
    numpy.random.multinomial : Sampling from the multinomial distribution.
    c         C` s2   t  t |   j |  t j |  j t  |  _ d  S(   N(   RU   R  RK   R   Rb   RQ   t   multinomial_docdict_params(   RE   RW   (    (    s8   lib/python2.7/site-packages/scipy/stats/_multivariate.pyRK     s    c         C` s   t  | | |  S(   ss   
        Create a frozen multinomial distribution.

        See `multinomial_frozen` for more information.
        (   t   multinomial_frozen(   RE   R   t   pRW   (    (    s8   lib/python2.7/site-packages/scipy/stats/_multivariate.pyRg     s    c         C` s   t  j | d t  j d t } d | d d d  f j d d  | d
 <t  j | d k  d d } | t  j | d	 k d d O} t  j | d t  j d t } | d k } | | | | Bf S(   s   
        Return: n_, p_, npcond.

        n_ and p_ are arrays of the correct shape; npcond is a boolean array
        flagging values out of the domain.
        R"   R  g      ?.NiRv   i    i   (   .i(   R%   R0   t   float64RR   RA   R   R   (   RE   R   R  t   pcondt   ncond(    (    s8   lib/python2.7/site-packages/scipy/stats/_multivariate.pyRs     s    *c         C` s   t  j | d t  j } | j d k r6 t d   n  | j d k r | j d | j d k r t d | j d | j d f   n  t  j | | k d d } | t  j | d k  d d O} | t  j | d d | k B} | | f S(   s   
        Return: x_, xcond.

        x_ is an int array; xcond is a boolean array flagging values out of the
        domain.
        R"   i    s   x must be an array.isH   Size of each quantile should be size of p: received %d, but expected %d.Rv   (	   R%   R   R   R   R;   Ri   Rh   R   RA   (   RE   R3   R   R  t   xxR+   (    (    s8   lib/python2.7/site-packages/scipy/stats/_multivariate.pyRu     s    *!c         C` sU   t  j |  } | j d k r+ | | | <n& | rQ | j d k rD | S| | d <n  | S(   Ni    .(   R%   R   R   (   RE   t   resultR+   t	   bad_value(    (    s8   lib/python2.7/site-packages/scipy/stats/_multivariate.pyt   _checkresult  s    c         C` s8   t  | d  t j t | |  t  | d  d d S(   Ni   Rv   i(   R   R%   RA   R	   (   RE   R3   R   R  (    (    s8   lib/python2.7/site-packages/scipy/stats/_multivariate.pyt   _logpmf  s    c   	      C` s   |  j  | |  \ } } } |  j | | |  \ } } |  j | | |  } | t j | j d t j B} |  j | | t j  } | t j | j d t j B} |  j | | t j	  S(   s  
        Log of the Multinomial probability mass function.

        Parameters
        ----------
        x : array_like
            Quantiles, with the last axis of `x` denoting the components.
        %(_doc_default_callparams)s

        Returns
        -------
        logpmf : ndarray or scalar
            Log of the probability mass function evaluated at `x`

        Notes
        -----
        %(_doc_callparams_note)s
        R"   (
   Rs   Ru   R"  R%   Rk   Rh   t   bool_R!  t   NINFt   NAN(	   RE   R3   R   R  t   npcondt   xcondR  t   xcond_t   npcond_(    (    s8   lib/python2.7/site-packages/scipy/stats/_multivariate.pyt   logpmf  s    c         C` s   t  j |  j | | |   S(   s  
        Multinomial probability mass function.

        Parameters
        ----------
        x : array_like
            Quantiles, with the last axis of `x` denoting the components.
        %(_doc_default_callparams)s

        Returns
        -------
        pmf : ndarray or scalar
            Probability density function evaluated at `x`

        Notes
        -----
        %(_doc_callparams_note)s
        (   R%   R   R*  (   RE   R3   R   R  (    (    s8   lib/python2.7/site-packages/scipy/stats/_multivariate.pyt   pmf  s    c         C` sH   |  j  | |  \ } } } | d t j f | } |  j | | t j  S(   s   
        Mean of the Multinomial distribution

        Parameters
        ----------
        %(_doc_default_callparams)s

        Returns
        -------
        mean : float
            The mean of the distribution
        .(   Rs   R%   Rt   R!  R%  (   RE   R   R  R&  R  (    (    s8   lib/python2.7/site-packages/scipy/stats/_multivariate.pyRe   -  s    c         C` s   |  j  | |  \ } } } | d t j t j f } | t j d | |  } xB t | j d  D]- } | d | | f c | | d | f 7<qb W|  j | | t j  S(   s   
        Covariance matrix of the multinomial distribution.

        Parameters
        ----------
        %(_doc_default_callparams)s

        Returns
        -------
        cov : ndarray
            The covariance matrix of the distribution
        .s   ...j,...k->...jki(   Rs   R%   Rt   t   einsumR   Rh   R!  t   nan(   RE   R   R  R&  t   nnR  R   (    (    s8   lib/python2.7/site-packages/scipy/stats/_multivariate.pyRf   >  s    +c         C` s  |  j  | |  \ } } } t j d t j |  d !} | t j t |  d d } | t | d  8} | d t j f } t | j | j  | j d } | j	 d | 7_	 t j t
 j | | |  t | d  d d d | f } |  j | | | t j  S(   s  
        Compute the entropy of the multinomial distribution.

        The entropy is computed using this expression:

        .. math::

            f(x) = - \log n! - n\sum_{i=1}^k p_i \log p_i +
            \sum_{i=1}^k \sum_{x=0}^n \binom n x p_i^x(1-p_i)^{n-x} \log x!

        Parameters
        ----------
        %(_doc_default_callparams)s

        Returns
        -------
        h : scalar
            Entropy of the Multinomial distribution

        Notes
        -----
        %(_doc_callparams_note)s
        i   Rv   i.(   i   (   Rs   R%   R   R(   RA   R
   R   Rt   R   Rh   R   R+  R!  R-  (   RE   R   R  R&  R3   t   term1t   new_axes_neededt   term2(    (    s8   lib/python2.7/site-packages/scipy/stats/_multivariate.pyR   V  s     )c         C` s=   |  j  | |  \ } } } |  j |  } | j | | |  S(   s  
        Draw random samples from a Multinomial distribution.

        Parameters
        ----------
        %(_doc_default_callparams)s
        size : integer or iterable of integers, optional
            Number of samples to draw (default 1).
        %(_doc_random_state)s

        Returns
        -------
        rvs : ndarray or scalar
            Random variates of shape (`size`, `len(p)`)

        Notes
        -----
        %(_doc_callparams_note)s
        (   Rs   RY   R   (   RE   R   R  Ri   RX   R&  (    (    s8   lib/python2.7/site-packages/scipy/stats/_multivariate.pyR   ~  s    N(   RO   RP   RQ   R!   RK   Rg   Rs   Ru   R!  R"  R*  R+  Re   Rf   R   R   (    (    (    s8   lib/python2.7/site-packages/scipy/stats/_multivariate.pyR  I  s   d					!				(R  c           B` sV   e  Z d  Z d	 d  Z d   Z d   Z d   Z d   Z d   Z	 d d	 d  Z
 RS(
   s  
    Create a frozen Multinomial distribution.

    Parameters
    ----------
    n : int
        number of trials
    p: array_like
        probability of a trial falling into each category; should sum to 1
    seed : None or int or np.random.RandomState instance, optional
        This parameter defines the RandomState object to use for drawing
        random variates.
        If None (or np.random), the global np.random state is used.
        If integer, it is used to seed the local RandomState instance
        Default is None.
    c         ` sU   t  |    _   j j | |  \   _   _   _   f d   } |   j _ d  S(   Nc         ` s     j    j   j f S(   N(   R   R  R&  (   R   R  (   RE   (    s8   lib/python2.7/site-packages/scipy/stats/_multivariate.pyRs     s    (   R  R\   Rs   R   R  R&  (   RE   R   R  RW   Rs   (    (   RE   s8   lib/python2.7/site-packages/scipy/stats/_multivariate.pyRK     s    'c         C` s   |  j  j | |  j |  j  S(   N(   R\   R*  R   R  (   RE   R3   (    (    s8   lib/python2.7/site-packages/scipy/stats/_multivariate.pyR*    s    c         C` s   |  j  j | |  j |  j  S(   N(   R\   R+  R   R  (   RE   R3   (    (    s8   lib/python2.7/site-packages/scipy/stats/_multivariate.pyR+    s    c         C` s   |  j  j |  j |  j  S(   N(   R\   Re   R   R  (   RE   (    (    s8   lib/python2.7/site-packages/scipy/stats/_multivariate.pyRe     s    c         C` s   |  j  j |  j |  j  S(   N(   R\   Rf   R   R  (   RE   (    (    s8   lib/python2.7/site-packages/scipy/stats/_multivariate.pyRf     s    c         C` s   |  j  j |  j |  j  S(   N(   R\   R   R   R  (   RE   (    (    s8   lib/python2.7/site-packages/scipy/stats/_multivariate.pyR     s    i   c         C` s   |  j  j |  j |  j | |  S(   N(   R\   R   R   R  (   RE   Ri   RX   (    (    s8   lib/python2.7/site-packages/scipy/stats/_multivariate.pyR     s    N(   RO   RP   RQ   R!   RK   R*  R+  Re   Rf   R   R   (    (    (    s8   lib/python2.7/site-packages/scipy/stats/_multivariate.pyR    s   
					R*  R+  Rf   t   special_ortho_group_genc           B` sA   e  Z d  Z d d  Z d d d  Z d   Z d d d  Z RS(   s
  
    A matrix-valued SO(N) random variable.

    Return a random rotation matrix, drawn from the Haar distribution
    (the only uniform distribution on SO(n)).

    The `dim` keyword specifies the dimension N.

    Methods
    -------
    ``rvs(dim=None, size=1, random_state=None)``
        Draw random samples from SO(N).

    Parameters
    ----------
    dim : scalar
        Dimension of matrices

    Notes
    ----------
    This class is wrapping the random_rot code from the MDP Toolkit,
    https://github.com/mdp-toolkit/mdp-toolkit

    Return a random rotation matrix, drawn from the Haar distribution
    (the only uniform distribution on SO(n)).
    The algorithm is described in the paper
    Stewart, G.W., "The efficient generation of random orthogonal
    matrices with an application to condition estimators", SIAM Journal
    on Numerical Analysis, 17(3), pp. 403-409, 1980.
    For more information see
    https://en.wikipedia.org/wiki/Orthogonal_matrix#Randomization

    See also the similar `ortho_group`.

    Examples
    --------
    >>> from scipy.stats import special_ortho_group
    >>> x = special_ortho_group.rvs(3)

    >>> np.dot(x, x.T)
    array([[  1.00000000e+00,   1.13231364e-17,  -2.86852790e-16],
           [  1.13231364e-17,   1.00000000e+00,  -1.46845020e-16],
           [ -2.86852790e-16,  -1.46845020e-16,   1.00000000e+00]])

    >>> import scipy.linalg
    >>> scipy.linalg.det(x)
    1.0

    This generates one random matrix from SO(3). It is orthogonal and
    has a determinant of 1.

    c         C` s/   t  t |   j |  t j |  j  |  _ d  S(   N(   RU   R2  RK   R   Rb   RQ   (   RE   RW   (    (    s8   lib/python2.7/site-packages/scipy/stats/_multivariate.pyRK   	  s    c         C` s   t  | d | S(   sv   
        Create a frozen SO(N) distribution.

        See `special_ortho_group_frozen` for more information.

        RW   (   t   special_ortho_group_frozen(   RE   Ro   RW   (    (    s8   lib/python2.7/site-packages/scipy/stats/_multivariate.pyRg     s    c         C` sM   | d k s: t j |  s: | d k s: | t |  k rI t d   n  | S(   sG   
        Dimension N must be specified; it cannot be inferred.
        i   sm   Dimension of rotation must be specified,
                                and must be a scalar greater than 1.N(   R!   R%   Rj   R   R;   (   RE   Ro   (    (    s8   lib/python2.7/site-packages/scipy/stats/_multivariate.pyRs     s    :i   c      	   C` s  |  j  |  } t |  } | d k rb t j g  t |  D]! } |  j | d d d | ^ q:  S|  j |  } t j |  } t j | f  } x t | d  D] } | j	 d | | f  } | d d k r t j
 | d  n d | | <| d c | | t j | | j    7<t j | |  d t j | |  | | j   }	 t j |  }
 |	 |
 | d  | d  f <t j | |
  } q Wd | d | d  j   | d <| | j j } | S(   s|  
        Draw random samples from SO(N).

        Parameters
        ----------
        dim : integer
            Dimension of rotation space (N).
        size : integer, optional
            Number of samples to draw (default 1).

        Returns
        -------
        rvs : ndarray or scalar
            Random size N-dimensional matrices, dimension (size, dim, dim)

        i   Ri   RX   i    g       @Ni(   RY   R   R%   R0   R   R   Rs   Rl   t   emptyR   t   signR>   RA   R   RL   R   RM   (   RE   Ro   Ri   RX   R   t   Ht   DR   R3   t   Hxt   mat(    (    s8   lib/python2.7/site-packages/scipy/stats/_multivariate.pyR   !  s&    	2-+5 N(   RO   RP   RQ   R!   RK   Rg   Rs   R   (    (    (    s8   lib/python2.7/site-packages/scipy/stats/_multivariate.pyR2    s
   4		R3  c           B` s&   e  Z d d d   Z d d d  Z RS(   c         C` s(   t  |  |  _ |  j j |  |  _ d S(   s  
        Create a frozen SO(N) distribution.

        Parameters
        ----------
        dim : scalar
            Dimension of matrices
        seed : None or int or np.random.RandomState instance, optional
            This parameter defines the RandomState object to use for drawing
            random variates.
            If None (or np.random), the global np.random state is used.
            If integer, it is used to seed the local RandomState instance
            Default is None.

        Examples
        --------
        >>> from scipy.stats import special_ortho_group
        >>> g = special_ortho_group(5)
        >>> x = g.rvs()

        N(   R2  R\   Rs   Ro   (   RE   Ro   RW   (    (    s8   lib/python2.7/site-packages/scipy/stats/_multivariate.pyRK   P  s    i   c         C` s   |  j  j |  j | |  S(   N(   R\   R   Ro   (   RE   Ri   RX   (    (    s8   lib/python2.7/site-packages/scipy/stats/_multivariate.pyR   i  s    N(   RO   RP   R!   RK   R   (    (    (    s8   lib/python2.7/site-packages/scipy/stats/_multivariate.pyR3  O  s   t   ortho_group_genc           B` s2   e  Z d  Z d d  Z d   Z d d d  Z RS(   s  
    A matrix-valued O(N) random variable.

    Return a random orthogonal matrix, drawn from the O(N) Haar
    distribution (the only uniform distribution on O(N)).

    The `dim` keyword specifies the dimension N.

    Methods
    -------
    ``rvs(dim=None, size=1, random_state=None)``
        Draw random samples from O(N).

    Parameters
    ----------
    dim : scalar
        Dimension of matrices

    Notes
    ----------
    This class is closely related to `special_ortho_group`.

    Some care is taken to avoid numerical error, as per the paper by Mezzadri.

    References
    ----------
    .. [1] F. Mezzadri, "How to generate random matrices from the classical
           compact groups", :arXiv:`math-ph/0609050v2`.

    Examples
    --------
    >>> from scipy.stats import ortho_group
    >>> x = ortho_group.rvs(3)

    >>> np.dot(x, x.T)
    array([[  1.00000000e+00,   1.13231364e-17,  -2.86852790e-16],
           [  1.13231364e-17,   1.00000000e+00,  -1.46845020e-16],
           [ -2.86852790e-16,  -1.46845020e-16,   1.00000000e+00]])

    >>> import scipy.linalg
    >>> np.fabs(scipy.linalg.det(x))
    1.0

    This generates one random matrix from O(3). It is orthogonal and
    has a determinant of +1 or -1.

    c         C` s/   t  t |   j |  t j |  j  |  _ d  S(   N(   RU   R:  RK   R   Rb   RQ   (   RE   RW   (    (    s8   lib/python2.7/site-packages/scipy/stats/_multivariate.pyRK     s    c         C` sM   | d k s: t j |  s: | d k s: | t |  k rI t d   n  | S(   sG   
        Dimension N must be specified; it cannot be inferred.
        i   sL   Dimension of rotation must be specified,and must be a scalar greater than 1.N(   R!   R%   Rj   R   R;   (   RE   Ro   (    (    s8   lib/python2.7/site-packages/scipy/stats/_multivariate.pyRs     s    :i   c      	   C` s{  |  j  |  } t |  } | d k rb t j g  t |  D]! } |  j | d d d | ^ q:  S|  j |  } t j |  } x t |  D] } | j d | | f  } | d d k r t j	 | d  n d } | d c | t j
 | | j    7<| t j | |  d t j | |  | | j   }	 t j |  }
 |	 |
 | d  | d  f <t j | |
  } q W| S(   s{  
        Draw random samples from O(N).

        Parameters
        ----------
        dim : integer
            Dimension of rotation space (N).
        size : integer, optional
            Number of samples to draw (default 1).

        Returns
        -------
        rvs : ndarray or scalar
            Random size N-dimensional matrices, dimension (size, dim, dim)

        i   Ri   RX   i    g       @N(   RY   R   R%   R0   R   R   Rs   Rl   R   R5  R>   RA   R   RL   (   RE   Ro   Ri   RX   R   R6  R   R3   R7  R8  R9  (    (    s8   lib/python2.7/site-packages/scipy/stats/_multivariate.pyR     s     	2)':N(   RO   RP   RQ   R!   RK   Rs   R   (    (    (    s8   lib/python2.7/site-packages/scipy/stats/_multivariate.pyR:  m  s   /	t   random_correlation_genc           B` sG   e  Z d  Z d d  Z d   Z d   Z d   Z d d d d  Z RS(	   s  
    A random correlation matrix.

    Return a random correlation matrix, given a vector of eigenvalues.

    The `eigs` keyword specifies the eigenvalues of the correlation matrix,
    and implies the dimension.

    Methods
    -------
    ``rvs(eigs=None, random_state=None)``
        Draw random correlation matrices, all with eigenvalues eigs.

    Parameters
    ----------
    eigs : 1d ndarray
        Eigenvalues of correlation matrix.

    Notes
    ----------

    Generates a random correlation matrix following a numerically stable
    algorithm spelled out by Davies & Higham. This algorithm uses a single O(N)
    similarity transformation to construct a symmetric positive semi-definite
    matrix, and applies a series of Givens rotations to scale it to have ones
    on the diagonal.

    References
    ----------

    .. [1] Davies, Philip I; Higham, Nicholas J; "Numerically stable generation
           of correlation matrices and their factors", BIT 2000, Vol. 40,
           No. 4, pp. 640 651

    Examples
    --------
    >>> from scipy.stats import random_correlation
    >>> np.random.seed(514)
    >>> x = random_correlation.rvs((.5, .8, 1.2, 1.5))
    >>> x
    array([[ 1.        , -0.20387311,  0.18366501, -0.04953711],
           [-0.20387311,  1.        , -0.24351129,  0.06703474],
           [ 0.18366501, -0.24351129,  1.        ,  0.38530195],
           [-0.04953711,  0.06703474,  0.38530195,  1.        ]])
    >>> import scipy.linalg
    >>> e, v = scipy.linalg.eigh(x)
    >>> e
    array([ 0.5,  0.8,  1.2,  1.5])

    c         C` s/   t  t |   j |  t j |  j  |  _ d  S(   N(   RU   R;  RK   R   Rb   RQ   (   RE   RW   (    (    s8   lib/python2.7/site-packages/scipy/stats/_multivariate.pyRK     s    c         C` s   t  j | d t } | j } | j d k sL | j d | k sL | d k r[ t d   n  t  j t  j |  |  | k r t d   n  x* | D]" } | | k  r t d   q q W| | f S(   NR"   i   i    s7   Array 'eigs' must be a vector of length greater than 1.s-   Sum of eigenvalues must equal dimensionality.s%   All eigenvalues must be non-negative.(	   R%   R   R1   Ri   R   Rh   R;   t   fabsRA   (   RE   t   eigst   tolRo   R3   (    (    s8   lib/python2.7/site-packages/scipy/stats/_multivariate.pyRs     s    	."c   
      C` s   | d } | d } | d k r$ d St  j t | d | | d   } | t  j | |  | } d t  j d | |  } | d k r d }	 n
 | | }	 | |	 f S(   s&  Computes a 2x2 Givens matrix to put 1's on the diagonal.

        The input matrix is a 2x2 symmetric matrix M = [ aii aij ; aij ajj ].

        The output matrix g is a 2x2 anti-symmetric matrix of the form
        [ c s ; -s c ];  the elements c and s are returned.

        Applying the output matrix to the input matrix (as b=g.T M g)
        results in a matrix with bii=1, provided tr(M) - det(M) >= 1
        and floating point issues do not occur. Otherwise, some other
        valid rotation is returned. When tr(M)==2, also bjj=1.

        g      ?i    g        i   (   g        g      ?(   t   mathR>   R(   t   copysign(
   RE   t   aiit   ajjt   aijt   aiidt   ajjdt   ddR-   t   cRH   (    (    s8   lib/python2.7/site-packages/scipy/stats/_multivariate.pyt   _givens_to_1!  s    

$	
c         C` s  | j  j o5 | j t j k o5 | j d | j d k sD t    n  | j d } xt | d  D]t} | | | f d k r qb n | | | f d k r xp t | d |  D]  } | | | f d k  r Pq q Wn8 x5 t | d |  D]  } | | | f d k r Pq q W|  j | | | f | | | f | | | f  \ } } | j	   } t
 | | | | d | d | | d d d | | d d d t d	 t t
 | | | | d | d | d | d | d | d t d	 t qb W| S(
   s   
        Given a psd matrix m, rotate to put one's on the diagonal, turning it
        into a correlation matrix.  This also requires the trace equal the
        dimensionality. Note: modifies input matrix
        i    i   R   t   offxt   incxt   offyt   incyt   overwrite_xt   overwrite_y(   t   flagst   c_contiguousR"   R%   R  Rh   R;   R   RH  t   ravelR   RR   (   RE   t   mR    R   t   jRG  RH   t   mv(    (    s8   lib/python2.7/site-packages/scipy/stats/_multivariate.pyt   _to_corrB  s.    9 gvIh%<=gHz>c         C` s   |  j  | d | \ } } |  j |  } t j | d | } t j t j | t j |   | j  } |  j |  } t	 | j
   d  j   | k r t d   n  | S(   s  
        Draw random correlation matrices

        Parameters
        ----------
        eigs : 1d ndarray
            Eigenvalues of correlation matrix
        tol : float, optional
            Tolerance for input parameter checks
        diag_tol : float, optional
            Tolerance for deviation of the diagonal of the resulting
            matrix. Default: 1e-7

        Raises
        ------
        RuntimeError
            Floating point error prevented generating a valid correlation
            matrix.

        Returns
        -------
        rvs : ndarray or scalar
            Random size N-dimensional matrices, dimension (size, dim, dim),
            each having eigenvalues eigs.

        R>  RX   i   s-   Failed to generate a valid correlation matrix(   Rs   RY   R   R   R%   RL   Rm   RM   RU  R)   R   R(   t   RuntimeError(   RE   R=  RX   R>  t   diag_tolRo   RR  (    (    s8   lib/python2.7/site-packages/scipy/stats/_multivariate.pyR   k  s    *"N(	   RO   RP   RQ   R!   RK   Rs   RH  RU  R   (    (    (    s8   lib/python2.7/site-packages/scipy/stats/_multivariate.pyR;    s   2		!	)t   unitary_group_genc           B` s2   e  Z d  Z d d  Z d   Z d d d  Z RS(   s  
    A matrix-valued U(N) random variable.

    Return a random unitary matrix.

    The `dim` keyword specifies the dimension N.

    Methods
    -------
    ``rvs(dim=None, size=1, random_state=None)``
        Draw random samples from U(N).

    Parameters
    ----------
    dim : scalar
        Dimension of matrices

    Notes
    ----------
    This class is similar to `ortho_group`.

    References
    ----------
    .. [1] F. Mezzadri, "How to generate random matrices from the classical
           compact groups", arXiv:math-ph/0609050v2.

    Examples
    --------
    >>> from scipy.stats import unitary_group
    >>> x = unitary_group.rvs(3)

    >>> np.dot(x, x.conj().T)
    array([[  1.00000000e+00,   1.13231364e-17,  -2.86852790e-16],
           [  1.13231364e-17,   1.00000000e+00,  -1.46845020e-16],
           [ -2.86852790e-16,  -1.46845020e-16,   1.00000000e+00]])

    This generates one random matrix from U(3). The dot product confirms that
    it is unitary up to machine precision.

    c         C` s/   t  t |   j |  t j |  j  |  _ d  S(   N(   RU   RX  RK   R   Rb   RQ   (   RE   RW   (    (    s8   lib/python2.7/site-packages/scipy/stats/_multivariate.pyRK     s    c         C` sM   | d k s: t j |  s: | d k s: | t |  k rI t d   n  | S(   sG   
        Dimension N must be specified; it cannot be inferred.
        i   sL   Dimension of rotation must be specified,and must be a scalar greater than 1.N(   R!   R%   Rj   R   R;   (   RE   Ro   (    (    s8   lib/python2.7/site-packages/scipy/stats/_multivariate.pyRs     s    :i   c   	   	   C` s   |  j  |  } t |  } | d k rb t j g  t |  D]! } |  j | d d d | ^ q:  S|  j |  } d t j d  | j	 d | | f  d | j	 d | | f  } t
 j j |  \ } } | j   } | | t |  9} | S(   sr  
        Draw random samples from U(N).

        Parameters
        ----------
        dim : integer
            Dimension of space (N).
        size : integer, optional
            Number of samples to draw (default 1).

        Returns
        -------
        rvs : ndarray or scalar
            Random size N-dimensional matrices, dimension (size, dim, dim)

        i   Ri   RX   i   y              ?(   RY   R   R%   R0   R   R   Rs   R?  R>   R   R7   R8   t   qrR   R)   (	   RE   Ro   Ri   RX   R   t   zt   qt   rR    (    (    s8   lib/python2.7/site-packages/scipy/stats/_multivariate.pyR     s    	2%N(   RO   RP   RQ   R!   RK   Rs   R   (    (    (    s8   lib/python2.7/site-packages/scipy/stats/_multivariate.pyRX    s   (	(q   t
   __future__R    R   R   R?  t   numpyR%   R   R   t   scipy.linalgR7   t
   scipy.miscR   t   scipy.specialR   R   R   R	   R
   t   scipy._lib._utilR   t   scipy.linalg.blasR   t   scipy.linalg.miscR   t   scipy.linalg.lapackR   t   _discrete_distnsR   R]   R   t   __all__RB   R   Rx   R   t   _LOG_PIR`   R   R!   R/   R4   t   objectR5   RT   R[   R^   R_   t   _mvn_doc_frozen_callparamst   _mvn_doc_frozen_callparams_noteRc   t   mvn_docdict_noparamsRa   R   Rd   t   namet   __dict__t   methodt   method_frozenRb   RQ   R   R   t   _matnorm_doc_frozen_callparamst#   _matnorm_doc_frozen_callparams_noteR   t   matnorm_docdict_noparamsR   R   R   R   t    _dirichlet_doc_frozen_callparamst%   _dirichlet_doc_frozen_callparams_noteR   t   dirichlet_docdict_noparamsR   R   R   R   R   R   t   _wishart_doc_default_callparamst   _wishart_doc_callparams_notet   _wishart_doc_frozen_callparamst#   _wishart_doc_frozen_callparams_noteR   t   wishart_docdict_noparamsR   R   R   RR   R  R  R   R  t#   _multinomial_doc_default_callparamst    _multinomial_doc_callparams_notet"   _multinomial_doc_frozen_callparamst'   _multinomial_doc_frozen_callparams_noteR  t   multinomial_docdict_noparamsR  R   R  R2  R   R3  R:  R   R;  R   RX  R   (    (    (    s8   lib/python2.7/site-packages/scipy/stats/_multivariate.pyt   <module>   sH  (		#G"

 	\	

 ?	2

	
	,	 		

  Y	:"? 	G

 O	0y	h		[