a
    acY                    @   s  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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 g dZedej ZedZ eejZ!dZ"dd Z#diddZ$djddZ%G dd dZ&G dd dZ'G dd dZ(dZ)dZ*dZ+dZ,e)e*e"d Z-e+e,e"d Z.G d!d" d"e'Z/e/ Z0G d#d$ d$e(Z1d%D ]:Z2e/j3e2 Z4e1j3e2 Z5e6e4j7e.e5_7e6e4j7e-e4_7qfd&Z8d'Z9dZ:dZ;e8e9e"d(Z<e:e;e"d(Z=G d)d* d*e'Z>e> Z?G d+d, d,e(Z@d-D ]:Z2e>j3e2 Z4e@j3e2 Z5e6e4j7e=e5_7e6e4j7e<e4_7qd.ZAdZBdZCeAe"d/ZDeBe"d/ZEd0d1 ZFd2d3 ZGd4d5 ZHG d6d7 d7e'ZIeI ZJG d8d9 d9e(ZKd:D ]:Z2eIj3e2 Z4eKj3e2 Z5e6e4j7eEe5_7e6e4j7eDe4_7qd;ZLdZMdZNdZOeLeMe"d<ZPeNeOe"d<ZQG d=d> d>e'ZReR ZSG d?d@ d@e(ZTdAD ]:Z2eRj3e2 Z4eTj3e2 Z5e6e4j7eQe5_7e6e4j7ePe4_7q dkdCdDZUG dEdF dFeRZVeV ZWG dGdH dHe(ZXdID ]:Z2eVj3e2 Z4eTj3e2 Z5e6e4j7eQe5_7e6e4j7ePe4_7qdJZYdKZZdZ[dZ\eYeZe"d<Z]e[e\e"d<Z^G dLdM dMe'Z_e_ Z`G dNdO dOe(ZadPD ]:Z2e_j3e2 Z4eaj3e2 Z5e6e4j7e^e5_7e6e4j7e]e4_7qG dQdR dRe'Zbeb ZcG dSdT dTe(ZdG dUdV dVe'Zeee ZfG dWdX dXe'Zgeg ZhG dYdZ dZe'Ziei Zjd[Zkd\ZldZmekele"d]Zndeme"d]ZoG d^d_ d_e'ZpG d`da dae(Zqep Zrd-D ]:Z2epj3e2 Z4eqj3e2 Z5e6e4j7eoe5_7e6e4j7ene4_7qdbZsdcZtdZudZvesete"d<Zweueve"d<ZxG ddde dee'Zyey ZzG dfdg dge(Z{dhD ]:Z2eyj3e2 Z4e{j3e2 Z5e6e4j7exe5_7e6e4j7ewe4_7qdS )l    N)asarray_chkfiniteasarray)doccer)gammalnpsimultigammalnxlogyentrbetaln)check_random_state)drot)LinAlgError)get_lapack_funcs   )binom)mvn)multivariate_normalmatrix_normal	dirichletwishart
invwishartmultinomialspecial_ortho_grouportho_grouprandom_correlationunitary_groupmultivariate_tmultivariate_hypergeom   a  random_state : {None, int, `numpy.random.Generator`,
                `numpy.random.RandomState`}, optional

    If `seed` is None (or `np.random`), the `numpy.random.RandomState`
    singleton is used.
    If `seed` is an int, a new ``RandomState`` instance is used,
    seeded with `seed`.
    If `seed` is already a ``Generator`` or ``RandomState`` instance then
    that instance is used.
c                 C   s   |   } | jdkr| d } | S )z_
    Remove single-dimensional entries from array and convert to scalar,
    if necessary.
    r    )Zsqueezendim)outr   r   i/Users/vegardjervell/Documents/master/model/venv/lib/python3.9/site-packages/scipy/stats/_multivariate.py_squeeze_output1   s    
r#   c                 C   sT   |dur|}|dv r>| j j }ddd}|| t|j }|tt|  }|S )a  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.

    N)Ng     @@g    .A)fd)dtypecharlowernpZfinfoepsmaxabs)ZspectrumcondrcondtZfactorr+   r   r   r"   _eigvalsh_to_eps<   s    
r1   h㈵>c                    s   t j fdd| D tdS )a  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.

    c                    s$   g | ]}t | krd nd| qS )r   r   )r-   ).0xr+   r   r"   
<listcomp>n       z_pinv_1d.<locals>.<listcomp>r'   )r*   arrayfloat)vr+   r   r5   r"   _pinv_1d^   s    r<   c                   @   s&   e Zd ZdZdddZedd ZdS )	_PSDaN  
    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().

    NTc                 C   s   t jj|||d\}}t|||}	t||	 k r:td|||	k }
t|
t|k rf|sftjdt	||	}t
|t|}t|
| _|| _tt|
| _d | _d S )N)r)   check_finitez.the input matrix must be positive semidefinitezsingular matrix)scipylinalgZeighr1   r*   min
ValueErrorlenr   r<   multiplysqrtrankUsumloglog_pdet_pinv)selfMr.   r/   r)   r>   allow_singularsur+   r&   Zs_pinvrG   r   r   r"   __init__   s    

z_PSD.__init__c                 C   s$   | j d u rt| j| jj| _ | j S N)rK   r*   dotrG   TrL   r   r   r"   pinv   s    
z	_PSD.pinv)NNTTT)__name__
__module____qualname____doc__rQ   propertyrV   r   r   r   r"   r=   q   s   '  
r=   c                       sD   e Zd ZdZd
 fdd	Zedd Zejdd Zdd	 Z  Z	S )multi_rv_genericzc
    Class which encapsulates common functionality between all multivariate
    distributions.
    Nc                    s   t    t|| _d S rR   )superrQ   r   _random_staterL   seed	__class__r   r"   rQ      s    
zmulti_rv_generic.__init__c                 C   s   | j S )a   Get or set the Generator object for generating random variates.

        If `seed` is None (or `np.random`), the `numpy.random.RandomState`
        singleton is used.
        If `seed` is an int, a new ``RandomState`` instance is used,
        seeded with `seed`.
        If `seed` is already a ``Generator`` or ``RandomState`` instance then
        that instance is used.

        )r^   rU   r   r   r"   random_state   s    zmulti_rv_generic.random_statec                 C   s   t || _d S rR   r   r^   r_   r   r   r"   rc      s    c                 C   s   |d urt |S | jS d S rR   rd   )rL   rc   r   r   r"   _get_random_state   s    z"multi_rv_generic._get_random_state)N)
rW   rX   rY   rZ   rQ   r[   rc   setterre   __classcell__r   r   ra   r"   r\      s   

r\   c                   @   s*   e Zd ZdZedd Zejdd ZdS )multi_rv_frozenzj
    Class which encapsulates common functionality between all frozen
    multivariate distributions.
    c                 C   s   | j jS rR   )_distr^   rU   r   r   r"   rc      s    zmulti_rv_frozen.random_statec                 C   s   t || j_d S rR   )r   ri   r^   r_   r   r   r"   rc      s    N)rW   rX   rY   rZ   r[   rc   rf   r   r   r   r"   rh      s
   
rh   a   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)
a2  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.
     z>See class definition for a detailed description of parameters.)_mvn_doc_default_callparams_mvn_doc_callparams_note_doc_random_statec                       s   e Zd ZdZd fdd	ZdddZd	d
 Zdd Zdd Zd ddZ	d!ddZ
dd Zd"ddZd#ddZd$ddZd%ddZ  ZS )&multivariate_normal_gena  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))

    Nc                    s    t  | t| jt| _d S rR   )r]   rQ   r   	docformatrZ   mvn_docdict_paramsr_   ra   r   r"   rQ   _  s    z multivariate_normal_gen.__init__r   Fc                 C   s   t ||||dS )zzCreate a frozen multivariate normal distribution.

        See `multivariate_normal_frozen` for more information.
        )rN   r`   )multivariate_normal_frozen)rL   meancovrN   r`   r   r   r"   __call__c  s    z multivariate_normal_gen.__call__c                 C   s  |du r^|du rH|du rd}q\t j|td}|jdk r<d}q\|jd }qpt j|td}|j}nt |sptd|du rt |}t j|td}|du rd}t j|td}|dkr|	d}|	dd}|jdks|jd |krtd| |jdkr|t 
| }n|jdkr t |}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rtd|j |||fS )z
        Infer dimensionality from mean or covariance matrix, ensure that
        mean and covariance are full vector resp. matrix.
        Nr   r8   r   r   z.Dimension of random variable must be a scalar.      ?z+Array 'mean' must be a vector of length %d.HArray 'cov' must be square if it is two dimensional, but cov.shape = %s.zTDimension mismatch: array 'cov' is of shape %s, but 'mean' is a vector of length %d.>Array 'cov' must be at most two-dimensional, but cov.ndim = %d)r*   r   r:   r    shapesizeisscalarrB   zerosreshapeeyediagstrrC   )rL   dimrr   rs   rowscolsmsgr   r   r"   _process_parametersl  sV    






z+multivariate_normal_gen._process_parametersc                 C   s`   t j|td}|jdkr$|t j }n8|jdkr\|dkrJ|ddt jf }n|t jddf }|S l
        Adjust quantiles array so that last axis labels the components of
        each data point.
        r8   r   r   Nr*   r   r:   r    newaxisrL   r4   r   r   r   r"   _process_quantiles  s    

z*multivariate_normal_gen._process_quantilesc                 C   s8   || }t jt t ||dd}d|t | |  S )a  Log of the multivariate normal probability density function.

        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.

        r$   axis      )r*   rH   squarerS   _LOG_2PI)rL   r4   rr   prec_UZlog_det_covrF   devmahar   r   r"   _logpdf  s    zmultivariate_normal_gen._logpdfc                 C   sL   |  d||\}}}| ||}t||d}| |||j|j|j}t|S )a  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

        NrN   )r   r   r=   r   rG   rJ   rF   r#   rL   r4   rr   rs   rN   r   Zpsdr!   r   r   r"   logpdf  s
    zmultivariate_normal_gen.logpdfc              	   C   sR   |  d||\}}}| ||}t||d}t| |||j|j|j}t	|S )a  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

        Nr   )
r   r   r=   r*   expr   rG   rJ   rF   r#   r   r   r   r"   pdf  s
    zmultivariate_normal_gen.pdfc           	         s>   t jt j  fdd}t |d|}t|S )a  Log of the multivariate normal cumulative distribution function.

        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 |  d S )Nr   )r   Zmvnun)Zx_sliceabsepsrs   r)   maxptsrr   relepsr   r"   <lambda>$  s   z.multivariate_normal_gen._cdf.<locals>.<lambda>r$   )r*   fullrx   infZapply_along_axisr#   )	rL   r4   rr   rs   r   r   r   Zfunc1dr!   r   r   r"   _cdf  s    zmultivariate_normal_gen._cdfr2   c           
   
   C   sV   |  d||\}}}| ||}t||d |s8d| }t| ||||||}	|	S )a  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

        Nr   @B )r   r   r=   r*   rI   r   
rL   r4   rr   rs   rN   r   r   r   r   r!   r   r   r"   logcdf)  s    zmultivariate_normal_gen.logcdfc           
      C   sP   |  d||\}}}| ||}t||d |s8d| }| ||||||}	|	S )a  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

        Nr   r   )r   r   r=   r   r   r   r   r"   cdfO  s    zmultivariate_normal_gen.cdfc                 C   s4   |  d||\}}}| |}||||}t|S )a  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)r   re   r   r#   )rL   rr   rs   ry   rc   r   r!   r   r   r"   rvsu  s    
zmultivariate_normal_gen.rvsc                 C   s<   |  d||\}}}tjdtj tj | \}}d| S )aG  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

        Nr         ?)r   r*   r@   Zslogdetpie)rL   rr   rs   r   _logdetr   r   r"   entropy  s     zmultivariate_normal_gen.entropy)N)Nr   FN)Nr   F)Nr   F)Nr   FNr2   r2   )Nr   FNr2   r2   )Nr   r   N)Nr   )rW   rX   rY   rZ   rQ   rt   r   r   r   r   r   r   r   r   r   r   rg   r   r   ra   r"   rn     s"   S
	=

!  
&  
&
rn   c                   @   sH   e Zd ZdddZdd Zd	d
 Zdd Zdd ZdddZdd Z	dS )rq   Nr   Fr2   c                 C   sZ   t || _| jd||\| _| _| _t| j|d| _|sDd| j }|| _|| _	|| _
dS )ax  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, int, `numpy.random.Generator`,
                `numpy.random.RandomState`}, optional

            If `seed` is None (or `np.random`), the `numpy.random.RandomState`
            singleton is used.
            If `seed` is an int, a new ``RandomState`` instance is used,
            seeded with `seed`.
            If `seed` is already a ``Generator`` or ``RandomState`` instance
            then that instance is used.
        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.]])

        Nr   r   )rn   ri   r   r   rr   rs   r=   cov_infor   r   r   )rL   rr   rs   rN   r`   r   r   r   r   r   r"   rQ     s    -

z#multivariate_normal_frozen.__init__c                 C   s:   | j || j}| j || j| jj| jj| jj}t	|S rR   )
ri   r   r   r   rr   r   rG   rJ   rF   r#   rL   r4   r!   r   r   r"   r     s
    z!multivariate_normal_frozen.logpdfc                 C   s   t | |S rR   r*   r   r   rL   r4   r   r   r"   r     s    zmultivariate_normal_frozen.pdfc                 C   s   t | |S rR   )r*   rI   r   r   r   r   r"   r     s    z!multivariate_normal_frozen.logcdfc                 C   s8   | j || j}| j || j| j| j| j| j}t	|S rR   )
ri   r   r   r   rr   rs   r   r   r   r#   r   r   r   r"   r     s
    zmultivariate_normal_frozen.cdfc                 C   s   | j | j| j||S rR   )ri   r   rr   rs   rL   ry   rc   r   r   r"   r     s    zmultivariate_normal_frozen.rvsc                 C   s$   | j j}| j j}d|td  |  S )zComputes the differential entropy of the multivariate normal.

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

        r   r   )r   rJ   rF   r   )rL   rJ   rF   r   r   r"   r     s    	z"multivariate_normal_frozen.entropy)Nr   FNNr2   r2   )r   N)
rW   rX   rY   rQ   r   r   r   r   r   r   r   r   r   r"   rq     s     
7
rq   )r   r   r   r   r   a  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`)
a  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.
    )_matnorm_doc_default_callparams_matnorm_doc_callparams_noterm   c                       sb   e Zd ZdZd fdd	ZdddZdd	 Zd
d Zdd ZdddZ	dddZ
dddZ  ZS )matrix_normal_gena`  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

    Nc                    s    t  | t| jt| _d S rR   )r]   rQ   r   ro   rZ   matnorm_docdict_paramsr_   ra   r   r"   rQ     s    zmatrix_normal_gen.__init__r   c                 C   s   t ||||dS )zoCreate a frozen matrix normal distribution.

        See `matrix_normal_frozen` for more information.

        r`   )matrix_normal_frozenrL   rr   rowcovcolcovr`   r   r   r"   rt     s    zmatrix_normal_gen.__call__c           
      C   s  |durFt j|td}|j}t|dkr0tdt |dkrFtdt j|td}|jdkr|durz|t |d  }q|t d }n|jdkrt 	|}|j}t|dkrtd|d |d krtd	|d dkrtd
|d }t j|td}|jdkr4|dur$|t |d  }n|t d }n|jdkrJt 	|}|j}t|dkrftd|d |d krtd|d dkrtd|d }|dur|d |krtd|d |krtdnt 
||f}||f}	|	|||fS )z
        Infer dimensionality from mean or covariance matrices. Handle
        defaults. Ensure compatible dimensions.
        Nr8   r   z%Array `mean` must be two dimensional.r   zArray `mean` has invalid shape.r   z(`rowcov` must be a scalar or a 2D array.zArray `rowcov` must be square.z!Array `rowcov` has invalid shape.z(`colcov` must be a scalar or a 2D array.zArray `colcov` must be square.z!Array `colcov` has invalid shape.z=Arrays `mean` and `rowcov` must have the same number of rows.z@Arrays `mean` and `colcov` must have the same number of columns.)r*   r   r:   rx   rC   rB   anyr    identityr~   r{   )
rL   rr   r   r   Z	meanshapeZrowshapenumrowsZcolshapenumcolsdimsr   r   r"   r     sZ    






z%matrix_normal_gen._process_parametersc                 C   sH   t j|td}|jdkr*|t jddf }|jdd |krDtd|S )zp
        Adjust quantiles array so that last two axes labels the components of
        each data point.
        r8   r   NzJThe shape of array `X` is not compatible with the distribution parameters.)r*   r   r:   r    r   rx   rB   )rL   Xr   r   r   r"   r     s    
z$matrix_normal_gen._process_quantilesc                 C   sv   |\}}	t j|| ddd}
t |jt |
|d}t jt jt |dddd}d||	 t |	|  ||  |  S )a  Log of the matrix normal probability density function.

        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.

        r$   r   r   startr   r   r   )r*   rollaxis	tensordotrT   rS   rH   r   r   )rL   r   r   rr   Zrow_prec_rtZlog_det_rowcovZcol_prec_rtZlog_det_colcovr   r   Zroll_devZ	scale_devr   r   r   r"   r     s    zmatrix_normal_gen._logpdfc           	   	   C   s`   |  |||\}}}}| ||}t|dd}t|dd}| ||||j|j|j|j}t|S )a  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

        Fr   )r   r   r=   r   rG   rJ   r#   )	rL   r   rr   r   r   r   rowpsdcolpsdr!   r   r   r"   r     s    zmatrix_normal_gen.logpdfc                 C   s   t | ||||S )a  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   )rL   r   rr   r   r   r   r   r"   r   5  s    zmatrix_normal_gen.pdfc                 C   s   t |}| |||\}}}}tjj|dd}tjj|dd}| |}|j|d ||d fd}	t|t	|	|j
d}
tj|
j
ddd|tjddddf  }|dkr||j}|S )a  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

        Tr)   r   r   ry   r   N)intr   r?   r@   choleskyre   Zstandard_normalr*   r   rS   rT   r   r   r|   rx   )rL   rr   r   r   ry   rc   r   ZrowcholZcolcholZstd_normZroll_rvsr!   r   r   r"   r   J  s    
*zmatrix_normal_gen.rvs)N)Nr   r   N)Nr   r   )Nr   r   )Nr   r   r   N)rW   rX   rY   rZ   rQ   rt   r   r   r   r   r   r   rg   r   r   ra   r"   r   4  s   c
B&

r   c                   @   s4   e Zd ZdZdddZdd Zdd	 Zdd
dZdS )r   a  Create a frozen matrix normal distribution.

    Parameters
    ----------
    %(_matnorm_doc_default_callparams)s
    seed : {None, int, `numpy.random.Generator`,
        `numpy.random.RandomState`}, optional

        If `seed` is None (or `np.random`), the `numpy.random.RandomState`
        singleton is used.
        If `seed` is an int, a new ``RandomState`` instance is used,
        seeded with `seed`.
        If `seed` is already a ``Generator`` or ``RandomState`` instance
        then that instance is used.

    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
    Nr   c                 C   sN   t || _| j|||\| _| _| _| _t| jdd| _t| jdd| _	d S )NFr   )
r   ri   r   r   rr   r   r   r=   r   r   r   r   r   r"   rQ     s
    
zmatrix_normal_frozen.__init__c              	   C   sD   | j || j}| j | j|| j| jj| jj| jj| jj}t	|S rR   )
ri   r   r   r   rr   r   rG   rJ   r   r#   )rL   r   r!   r   r   r"   r     s    zmatrix_normal_frozen.logpdfc                 C   s   t | |S rR   r   )rL   r   r   r   r"   r     s    zmatrix_normal_frozen.pdfc                 C   s   | j | j| j| j||S rR   )ri   r   rr   r   r   r   r   r   r"   r     s    zmatrix_normal_frozen.rvs)Nr   r   N)r   N)rW   rX   rY   rZ   rQ   r   r   r   r   r   r   r"   r   p  s
   
r   )r   r   r   zalpha : array_like
    The concentration parameters. The number of entries determines the
    dimensionality of the distribution.
)!_dirichlet_doc_default_callparamsrm   c                 C   s@   t | } t | dkr"tdn| jdkr<td| jf | S )Nr   z%All parameters must be greater than 0r   z?Parameter vector 'a' must be one dimensional, but a.shape = %s.)r*   r   rA   rB   r    rx   alphar   r   r"   _dirichlet_check_parameters  s    


r   c                 C   sp  t |}|jd d | jd krJ|jd | jd krJtd| j|jf |jd | jd krt dt |d g}|jdkrt ||}n"|jdkrt ||f}ntdt 	|dk rtdt 
|dkrtd|dk}| dk }|j| jkrt j||jd dd	|j}t ||}t |r8td
t t |dd dk rltdt |d |S )Nr   r   zVector '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.r   zUThe input must be one dimensional or a two dimensional matrix containing the entries.z8Each entry in 'x' must be greater than or equal to zero.z/Each entry in 'x' must be smaller or equal one.r$   r   zJEach entry in 'x' must be greater than zero if its alpha is less than one.ru   g&.>zOThe input vector 'x' must lie within the normal simplex. but np.sum(x, 0) = %s.)r*   r   rx   rB   r9   rH   r    appendZvstackrA   r,   repeatr|   logical_andr-   r   )r   r4   ZxkZxeq0Zalphalt1Zchkr   r   r"   _dirichlet_check_input  s8    
,


 
r   c                 C   s   t t| tt |  S )av  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*   rH   r   r   r   r   r"   _lnB  s    r   c                       sf   e Zd ZdZd fdd	ZdddZdd Zd	d
 Zdd Zdd Z	dd Z
dd ZdddZ  ZS )dirichlet_gena  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 = 1

    where 0 < x_i < 1.

    If the quantiles don't lie within the simplex, a ValueError is raised.

    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]])

    Nc                    s    t  | t| jt| _d S rR   )r]   rQ   r   ro   rZ   dirichlet_docdict_paramsr_   ra   r   r"   rQ   z  s    zdirichlet_gen.__init__c                 C   s   t ||dS )Nr   )dirichlet_frozenrL   r   r`   r   r   r"   rt   ~  s    zdirichlet_gen.__call__c                 C   s(   t |}| tt|d |jjd S )a  Log of the Dirichlet probability density function.

        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.

        r   r   )r   r*   rH   r   rT   )rL   r4   r   lnBr   r   r"   r     s    zdirichlet_gen._logpdfc                 C   s&   t |}t||}| ||}t|S )ay  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#   rL   r4   r   r!   r   r   r"   r     s    
zdirichlet_gen.logpdfc                 C   s,   t |}t||}t| ||}t|S )ak  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#   r   r   r   r"   r     s    
zdirichlet_gen.pdfc                 C   s   t |}|t| }t|S )zCompute the mean of the dirichlet distribution.

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

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

        r   r*   rH   r#   )rL   r   r!   r   r   r"   rr     s    zdirichlet_gen.meanc                 C   s6   t |}t|}|||  || |d   }t|S )a  Compute the variance of the dirichlet distribution.

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

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

        r   r   )rL   r   alpha0r!   r   r   r"   var  s    
zdirichlet_gen.varc                 C   s^   t |}t|}t|}|jd }||| tj|  t|d tj|  }t|S )a  Compute the differential entropy of the dirichlet distribution.

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

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

        r   r   )	r   r*   rH   r   rx   r?   Zspecialr   r#   )rL   r   r   r   Kr!   r   r   r"   r     s    

zdirichlet_gen.entropyr   c                 C   s    t |}| |}|j||dS )a  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.

        r   )r   re   r   )rL   r   ry   rc   r   r   r"   r     s    
zdirichlet_gen.rvs)N)N)r   N)rW   rX   rY   rZ   rQ   rt   r   r   r   rr   r   r   r   rg   r   r   ra   r"   r     s   j
r   c                   @   sH   e Zd ZdddZdd Zdd Zdd	 Zd
d Zdd ZdddZ	dS )r   Nc                 C   s   t || _t|| _d S rR   )r   r   r   ri   r   r   r   r"   rQ     s    
zdirichlet_frozen.__init__c                 C   s   | j || jS rR   )ri   r   r   r   r   r   r"   r     s    zdirichlet_frozen.logpdfc                 C   s   | j || jS rR   )ri   r   r   r   r   r   r"   r     s    zdirichlet_frozen.pdfc                 C   s   | j | jS rR   )ri   rr   r   rU   r   r   r"   rr      s    zdirichlet_frozen.meanc                 C   s   | j | jS rR   )ri   r   r   rU   r   r   r"   r   #  s    zdirichlet_frozen.varc                 C   s   | j | jS rR   )ri   r   r   rU   r   r   r"   r   &  s    zdirichlet_frozen.entropyr   c                 C   s   | j | j||S rR   )ri   r   r   r   r   r   r"   r   )  s    zdirichlet_frozen.rvs)N)r   N)
rW   rX   rY   rQ   r   r   rr   r   r   r   r   r   r   r"   r     s   
r   )r   r   r   rr   r   r   zdf : 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
)Z_doc_default_callparamsZ_doc_callparams_noterm   c                       s   e Zd ZdZd, fdd	Zd-ddZdd Zd	d
 Zdd Zdd Z	dd Z
dd Zdd Zdd Zdd Zdd Zdd Zdd Zdd  Zd!d" Zd.d$d%Zd&d' Zd(d) Zd*d+ Z  ZS )/wishart_gena  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). These arguments must satisfy the relationship
    ``df > scale.ndim - 1``, but see notes on using the `rvs` method with
    ``df < scale.ndim``.

    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.

    The algorithm [2]_ implemented by the `rvs` method may
    produce numerically singular matrices with :math:`p - 1 < \nu < p`; the
    user may wish to check for this condition and generate replacement samples
    as necessary.


    .. 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.

    Nc                    s    t  | t| jt| _d S rR   r]   rQ   r   ro   rZ   wishart_docdict_paramsr_   ra   r   r"   rQ     s    zwishart_gen.__init__c                 C   s   t |||S )zbCreate a frozen Wishart distribution.

        See `wishart_frozen` for more information.
        )wishart_frozenrL   dfscaler`   r   r   r"   rt     s    zwishart_gen.__call__c                 C   s   |d u rd}t j|td}|jdkr6|t jt jf }n`|jdkrLt |}nJ|jdkr~|jd |jd ks~tdt|j n|jdkrtd|j |jd }|d u r|}n(t 	|stdn||d krtd	|||fS )
Nru   r8   r   r   r   zLArray 'scale' must be square if it is two dimensional, but scale.scale = %s.zBArray 'scale' must be at most two-dimensional, but scale.ndim = %dz$Degrees of freedom must be a scalar.zNDegrees of freedom must be greater than the dimension of scale matrix minus 1.)
r*   r   r:   r    r   r~   rx   rB   r   rz   )rL   r   r   r   r   r   r"   r     s.    





zwishart_gen._process_parametersc                 C   sX  t j|td}|jdkr:|t |ddddt jf  }|jdkr|dkrd|t jt jddf }nt |ddddt jf }n|jdkr|jd |jd kstdt	|j |ddddt jf }nP|jdkr|jd |jd kstdt	|j n|jdkrtd	|j |jdd ||fksTtd
||f|jdd f |S )r   r8   r   Nr   r   zGQuantiles must be square if they are two dimensional, but x.shape = %s.   zeQuantiles must be square in the first two dimensions if they are three dimensional, but x.shape = %s.znQuantiles must be at most two-dimensional with an additional dimension for multiplecomponents, but x.ndim = %dz=Quantiles have incompatible dimensions: should be %s, got %s.)
r*   r   r:   r    r}   r   r~   rx   rB   r   r   r   r   r"   r     s6    
"
 
zwishart_gen._process_quantilesc                 C   sV   t |}|jdkr |t j }n|jdkr>tdtt| | }t|}||fS )Nr   r   z_Size must be an integer or tuple of integers; thus must have dimension <= 1. Got size.ndim = %s)r*   r   r    r   rB   r   tupleprod)rL   ry   nrx   r   r   r"   _process_size
  s    



zwishart_gen._process_sizec                 C   s  t |jd }t |j}t |jd }	t|jd D ]|}
| |dddd|
f \}||
< tj|df|dddd|
f |dddd|
f< |dddd|
f  |	|
< q:d|| d  | d|	  d| | t	 d| |  t
d| |  }|S )a  Log of the Wishart probability density function.

        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.

        r$   NTr   r   )r*   emptyrx   range_cholesky_logdetr?   r@   	cho_solvetrace_LOG_2r   )rL   r4   r   r   r   log_det_scaleC	log_det_xZscale_inv_xZtr_scale_inv_xir   r!   r   r   r"   r     s    $6 zwishart_gen._logpdfc                 C   sH   |  ||\}}}| ||}| |\}}| ||||||}t|S )a  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

        r   r   r   r   r#   )rL   r4   r   r   r   r   r   r!   r   r   r"   r   F  s
    zwishart_gen.logpdfc                 C   s   t | |||S )a  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   rL   r4   r   r   r   r   r"   r   c  s    zwishart_gen.pdfc                 C   s   || S )aA  Mean of the Wishart distribution.

        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.

        r   rL   r   r   r   r   r   r"   _meany  s    zwishart_gen._meanc                 C   s(   |  ||\}}}| |||}t|S )zMean of the Wishart distribution.

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

        Returns
        -------
        mean : float
            The mean of the distribution
        r   r   r#   rL   r   r   r   r!   r   r   r"   rr     s    zwishart_gen.meanc                 C   s&   ||d kr|| d | }nd}|S )aA  Mode of the Wishart distribution.

        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.

        r   Nr   rL   r   r   r   r!   r   r   r"   _mode  s    zwishart_gen._modec                 C   s4   |  ||\}}}| |||}|dur0t|S |S )aE  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r   r   r#   r   r   r   r"   mode  s    zwishart_gen.modec                 C   s,   |d }|  }|t||7 }||9 }|S )aD  Variance of the Wishart distribution.

        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.

        r   diagonalr*   outerrL   r   r   r   r   r~   r   r   r"   _var  s
    zwishart_gen._varc                 C   s(   |  ||\}}}| |||}t|S )zVariance of the Wishart distribution.

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

        Returns
        -------
        var : float
            The variance of the distribution
        r   r  r#   r   r   r   r"   r     s    zwishart_gen.varc                    s   ||d  d }j | d||f }tj fddt|D  |f|ddd  j}t|||f }	ttdddgt	| }
tj
|dd}||	|
| < t|}||	|
| < |	S )	ax  
        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 : {None, int, `numpy.random.Generator`,
                        `numpy.random.RandomState`}, optional

            If `seed` is None (or `np.random`), the `numpy.random.RandomState`
            singleton is used.
            If `seed` is an int, a new ``RandomState`` instance is used,
            seeded with `seed`.
            If `seed` is already a ``Generator`` or ``RandomState`` instance
            then that instance is used.

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

        r   r   r   c                    s*   g | ]"}j  |d   d  dd qS )r   r   r   )	chisquarer3   r   r   r   rc   r   r"   r6   	  s   z-wishart_gen._standard_rvs.<locals>.<listcomp>Nr$   k)normalr|   r*   r_r   rT   r{   r   slicerC   Ztril_indicesZdiag_indices)rL   r   rx   r   r   rc   Zn_trilZcovariancesZ	variancesAZsize_idxZtril_idxZdiag_idxr   r  r"   _standard_rvs  s(    
zwishart_gen._standard_rvsc           
      C   sR   |  |}| |||||}t|D ]&}t||| }	t|	|	j||< q&|S )a]  Draw random samples from a Wishart distribution.

        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 triangular.
        %(_doc_random_state)s

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

        )re   r  r*   ndindexrS   rT   )
rL   r   rx   r   r   r   rc   r  indexCAr   r   r"   _rvs  s    
zwishart_gen._rvsr   c           
      C   sL   |  |\}}| ||\}}}tjj|dd}| ||||||}	t|	S )a  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

        Tr   )r   r   r?   r@   r   r  r#   )
rL   r   r   ry   rc   r   rx   r   r   r!   r   r   r"   r   H  s
    zwishart_gen.rvsc                    sj   d|d  | d| |d  t   td  | d | d  t fddt|D   d  |  S )a  Compute the differential entropy of the Wishart.

        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.

        r   r   c                    s$   g | ]}t d  d |d   qS )r   r   )r   r  r   r   r"   r6   ~  r7   z(wishart_gen._entropy.<locals>.<listcomp>)r   r   r*   rH   r   )rL   r   r   r   r   r  r"   _entropyg  s    
zwishart_gen._entropyc                 C   s.   |  ||\}}}| |\}}| |||S )a'  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

        )r   r   r  )rL   r   r   r   r   r   r   r   r"   r     s    zwishart_gen.entropyc                 C   s0   t jj|dd}dtt|  }||fS )a  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.

        Tr   r   )r?   r@   r   r*   rH   rI   r   )rL   r   Zc_decompr   r   r   r"   r     s    zwishart_gen._cholesky_logdet)N)NNN)r   N)rW   rX   rY   rZ   rQ   rt   r   r   r   r   r   r   r   rr   r   r   r  r   r  r  r   r  r   r   rg   r   r   ra   r"   r   S  s*   i
%.4-
r   c                   @   sT   e Zd ZdZdddZdd Zdd Zd	d
 Zdd Zdd Z	dddZ
dd ZdS )r   ae  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, int, `numpy.random.Generator`,
            `numpy.random.RandomState`}, optional

        If `seed` is None (or `np.random`), the `numpy.random.RandomState`
        singleton is used.
        If `seed` is an int, a new ``RandomState`` instance is used,
        seeded with `seed`.
        If `seed` is already a ``Generator`` or ``RandomState`` instance then
        that instance is used.

    Nc                 C   s>   t || _| j||\| _| _| _| j| j\| _| _d S rR   )	r   ri   r   r   r   r   r   r   r   r   r   r   r"   rQ     s
    
zwishart_frozen.__init__c                 C   s8   | j || j}| j || j| j| j| j| j}t|S rR   )	ri   r   r   r   r   r   r   r   r#   r   r   r   r"   r     s
    zwishart_frozen.logpdfc                 C   s   t | |S rR   r   r   r   r   r"   r     s    zwishart_frozen.pdfc                 C   s   | j | j| j| j}t|S rR   ri   r   r   r   r   r#   rL   r!   r   r   r"   rr     s    zwishart_frozen.meanc                 C   s*   | j | j| j| j}|d ur&t|S |S rR   ri   r   r   r   r   r#   r  r   r   r"   r     s    zwishart_frozen.modec                 C   s   | j | j| j| j}t|S rR   ri   r  r   r   r   r#   r  r   r   r"   r     s    zwishart_frozen.varr   c                 C   s4   | j |\}}| j ||| j| j| j|}t|S rR   ri   r   r  r   r   r   r#   rL   ry   rc   r   rx   r!   r   r   r"   r     s
    zwishart_frozen.rvsc                 C   s   | j | j| j| jS rR   )ri   r  r   r   r   rU   r   r   r"   r     s    zwishart_frozen.entropy)N)r   N)rW   rX   rY   rZ   rQ   r   r   rr   r   r   r   r   r   r   r   r"   r     s   

r   )r   r   rr   r   r   r   r   Tc           	      C   s*  |rt | }nt| }t|jdk s8|jd |jd kr@tdtd|f\}}tj| jd dd\}}t|jdd D ]}||| d	d
d
d\||< }|dkrt	d| |dk rtd|  ||| d	d
d\||< }|dkrt	d|dk rtd|  || ||f || ||f< q||S )a*  
    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

    r   r   r$   z-expected square matrix in last two dimensions)potrfpotrir   r  NTF)r)   Zoverwrite_acleanr   z)%d-th leading minor not positive definitez1illegal value in %d-th argument of internal potrf)r)   Zoverwrite_cz!the inverse could not be computed)
r   r   rC   rx   rB   r   r*   Ztriu_indicesr  r   )	ar>   Za1r  r  Z	triu_rowsZ	triu_colsr  infor   r   r"   _cho_inv_batch  s8    
"
r  c                       s   e Zd ZdZd  fdd	Zd!ddZdd Zd	d
 Zdd Zdd Z	dd Z
dd Zdd Zdd Zdd Z fddZd"ddZdd Z  ZS )#invwishart_gena  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.

    Nc                    s    t  | t| jt| _d S rR   r   r_   ra   r   r"   rQ   	  s    zinvwishart_gen.__init__c                 C   s   t |||S )znCreate a frozen inverse Wishart distribution.

        See `invwishart_frozen` for more information.

        )invwishart_frozenr   r   r   r"   rt   	  s    zinvwishart_gen.__call__c                 C   s   t |jd }t |j}|dkr.t| nd| }t |jd }t|jd D ]^}	tjj	|dddd|	f dd\}
}dt 
t |
  ||	< t |||	  ||	< qTd| | d|  d| | t d|| d  |   td| | }|S )	ag  Log of the inverse Wishart probability density function.

        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.

        r$   r   ru   NTr   r   r   )r*   r   rx   copyrT   r  r   r?   r@   
cho_factorrH   rI   r   rS   r   r   r   )rL   r4   r   r   r   r   r   Zx_invZtr_scale_x_invr   r   r)   r!   r   r   r"   r   	  s     
&"zinvwishart_gen._logpdfc                 C   sF   |  ||\}}}| ||}| |\}}| |||||}t|S )a  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

        r   )rL   r4   r   r   r   r   r   r!   r   r   r"   r   	  s
    zinvwishart_gen.logpdfc                 C   s   t | |||S )a  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   r   r"   r   	  s    zinvwishart_gen.pdfc                 C   s&   ||d kr||| d  }nd}|S )aI  Mean of the inverse Wishart distribution.

        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.

        r   Nr   r   r   r   r"   r   
  s    zinvwishart_gen._meanc                 C   s4   |  ||\}}}| |||}|dur0t|S |S )aX  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

        Nr   r   r   r   r"   rr   
  s    zinvwishart_gen.meanc                 C   s   ||| d  S )aI  Mode of the inverse Wishart distribution.

        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.

        r   r   r   r   r   r"   r   -
  s    zinvwishart_gen._modec                 C   s(   |  ||\}}}| |||}t|S )zMode of the inverse Wishart distribution.

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

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

        r   r   r   r   r"   r   >
  s    zinvwishart_gen.modec                 C   sv   ||d krn|| d |d  }|  }||| d t|| 7 }||| || d d  || d   }nd}|S )aL  Variance of the inverse Wishart distribution.

        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.

        r   r   r   Nr   r  r   r   r"   r  O
  s    *zinvwishart_gen._varc                 C   s4   |  ||\}}}| |||}|dur0t|S |S )aX  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
        Nr  r   r   r   r"   r   g
  s    zinvwishart_gen.varc                    s   |  |}t |||||}t|}td|f}	t|jdd D ]p}
t|||
 }|dkr|	||dd\}}|dkrt	d|dk rt
d	|  nd
| }t|j|||
< qH|S )ae  Draw random samples from an inverse Wishart distribution.

        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.

        trtrsNr   r   Tr   r   zSingular matrix.z1Illegal value in %d-th argument of internal trtrsru   )re   r]   r  r*   r}   r   r  rx   rS   r   rB   rT   )rL   r   rx   r   r   r   rc   r  r}   r$  r  r  r  ra   r   r"   r  z
  s"    

zinvwishart_gen._rvsr   c                 C   s|   |  |\}}| ||\}}}t|}tjj|dd\}	}
tj|	|
f|}tjj|dd}| 	||||||}t
|S )a
  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

        Tr   )r   r   r*   r}   r?   r@   r#  r   r   r  r#   )rL   r   r   ry   rc   r   rx   r   r}   Lr)   	inv_scaler   r!   r   r   r"   r   
  s    
zinvwishart_gen.rvsc                 C   s   t d S rR   AttributeErrorrU   r   r   r"   r   
  s    zinvwishart_gen.entropy)N)NNN)r   N)rW   rX   rY   rZ   rQ   rt   r   r   r   r   rr   r   r   r  r   r  r   r   rg   r   r   ra   r"   r   8	  s   b
-1
#r   c                   @   sP   e Zd ZdddZdd Zdd Zdd	 Zd
d Zdd ZdddZ	dd Z
dS )r!  Nc                 C   s   t || _| j||\| _| _| _tjj| jdd\}}dt	
t	|  | _t	| j}tj||f|| _tjj| jdd| _dS )aG  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, int, `numpy.random.Generator`}, optional
            If `seed` is None the `numpy.random.Generator` singleton is used.
            If `seed` is an int, a new ``Generator`` instance is used,
            seeded with `seed`.
            If `seed` is already a ``Generator`` instance then that instance is
            used.

        Tr   r   N)r   ri   r   r   r   r   r?   r@   r#  r*   rH   rI   r   r   r}   r   r&  r   r   )rL   r   r   r`   r   r)   r}   r   r   r"   rQ   
  s    
zinvwishart_frozen.__init__c                 C   s4   | j || j}| j || j| j| j| j}t|S rR   )ri   r   r   r   r   r   r   r#   r   r   r   r"   r   
  s
    zinvwishart_frozen.logpdfc                 C   s   t | |S rR   r   r   r   r   r"   r   
  s    zinvwishart_frozen.pdfc                 C   s*   | j | j| j| j}|d ur&t|S |S rR   r  r  r   r   r"   rr     s    zinvwishart_frozen.meanc                 C   s   | j | j| j| j}t|S rR   r  r  r   r   r"   r     s    zinvwishart_frozen.modec                 C   s*   | j | j| j| j}|d ur&t|S |S rR   r  r  r   r   r"   r   	  s    zinvwishart_frozen.varr   c                 C   s4   | j |\}}| j ||| j| j| j|}t|S rR   r  r  r   r   r"   r     s
    zinvwishart_frozen.rvsc                 C   s   t d S rR   r'  rU   r   r   r"   r     s    zinvwishart_frozen.entropy)N)r   N)rW   rX   rY   rQ   r   r   rr   r   r   r   r   r   r   r   r"   r!  
  s   
!
r!  )r   r   rr   r   r   r   zsn : int
    Number of trials
p : array_like
    Probability of a trial falling into each category; should sum to 1
a  `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.
c                       s~   e Zd ZdZd fdd	ZdddZdd Zd	d
 Zdd Zdd Z	dd Z
dd Zdd Zdd Zdd ZdddZ  ZS )multinomial_gena  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.Generator.multinomial : Sampling from the multinomial distribution.
    scipy.stats.multivariate_hypergeom :
        The multivariate hypergeometric distribution.
    Nc                    s    t  | t| jt| _d S rR   )r]   rQ   r   ro   rZ   multinomial_docdict_paramsr_   ra   r   r"   rQ     s    zmultinomial_gen.__init__c                 C   s   t |||S )zjCreate a frozen multinomial distribution.

        See `multinomial_frozen` for more information.
        )multinomial_frozen)rL   r   pr`   r   r   r"   rt     s    zmultinomial_gen.__call__c                 C   s   t j|t jdd}d|dddf jdd |d< t j|d	k dd}|t j|d
kddO }t j|t jdd}|d	k}||||B fS )zReturns: n_, p_, npcond.

        n_ and p_ are arrays of the correct shape; npcond is a boolean array
        flagging values out of the domain.
        T)r'   r"  ru   .Nr$   r   ).r$   r   r   )r*   r9   float64rH   r   int_)rL   r   r,  Zpcondncondr   r   r"   r     s     z#multinomial_gen._process_parametersc                 C   s   t j|t jd}|jdkr"td|jdkr\|jd |jd ks\td|jd |jd f t j||kdd}|t j|dk ddO }|t j|dd|kB }||fS )z}Returns: x_, xcond.

        x_ is an int array; xcond is a boolean array flagging values out of the
        domain.
        r8   r   zx must be an array.r$   zHSize of each quantile should be size of p: received %d, but expected %d.r   )	r*   r   r.  r    rB   ry   rx   r   rH   )rL   r4   r   r,  xxr.   r   r   r"   r     s    
z"multinomial_gen._process_quantilesc                 C   s<   t |}|jdkr|||< n|r8|jdkr0|S ||d< |S )Nr   .r*   r   r    rL   resultr.   Z	bad_valuer   r   r"   _checkresult  s    



zmultinomial_gen._checkresultc                 C   s,   t |d tjt||t |d  dd S )Nr   r$   r   )r   r*   rH   r   rL   r4   r   r,  r   r   r"   _logpmf  s    zmultinomial_gen._logpmfc           	      C   s~   |  ||\}}}| |||\}}| |||}|tj|jtjdB }| ||tj}|tj|jtjdB }| ||tj	S )a  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
        r8   )
r   r   r6  r*   r{   rx   bool_r4  NINFNAN)	rL   r4   r   r,  npcondxcondr3  xcond_Znpcond_r   r   r"   logpmf  s    zmultinomial_gen.logpmfc                 C   s   t | |||S )a  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=  r5  r   r   r"   pmf  s    zmultinomial_gen.pmfc                 C   s4   |  ||\}}}|dtjf | }| ||tjS )zMean of the Multinomial distribution.

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

        Returns
        -------
        mean : float
            The mean of the distribution
        .)r   r*   r   r4  r9  )rL   r   r,  r:  r3  r   r   r"   rr   #  s    zmultinomial_gen.meanc                 C   s~   |  ||\}}}|dtjtjf }|td| | }t|jd D ]&}|d||f  ||d|f  7  < qF| ||tjS )zCovariance matrix of the multinomial distribution.

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

        Returns
        -------
        cov : ndarray
            The covariance matrix of the distribution
        .z...j,...k->...jkr$   )r   r*   r   einsumr   rx   r4  nan)rL   r   r,  r:  nnr3  r   r   r   r"   rs   3  s    $zmultinomial_gen.covc                 C   s   |  ||\}}}tjdt|d  }|tjt|dd }|t|d 8 }|dtjf }t|j|j|j d }| j	d| 7  _	tjt
|||t|d  dd| fd}| || |tjS )a  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
        r   r$   r   .)r   )r   r*   r
  r,   rH   r	   r   r   r    rx   r   r?  r4  rA  )rL   r   r,  r:  r4   Zterm1Znew_axes_neededZterm2r   r   r"   r   J  s    
zmultinomial_gen.entropyc                 C   s*   |  ||\}}}| |}||||S )a  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
        )r   re   r   )rL   r   r,  ry   rc   r:  r   r   r"   r   q  s    
zmultinomial_gen.rvs)N)N)NN)rW   rX   rY   rZ   rQ   rt   r   r   r4  r6  r=  r?  rr   rs   r   r   rg   r   r   ra   r"   r)  C  s   f
 'r)  c                   @   sL   e Zd ZdZdddZdd Zdd Zd	d
 Zdd Zdd Z	dddZ
dS )r+  ag  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, int, `numpy.random.Generator`,
            `numpy.random.RandomState`}, optional

        If `seed` is None (or `np.random`), the `numpy.random.RandomState`
        singleton is used.
        If `seed` is an int, a new ``RandomState`` instance is used,
        seeded with `seed`.
        If `seed` is already a ``Generator`` or ``RandomState`` instance then
        that instance is used.
    Nc                    s<   t | _ j||\ _ _ _ fdd}| j_d S )Nc                    s    j  j jfS rR   )r   r,  r:  )r   r,  rU   r   r"   r     s    z8multinomial_frozen.__init__.<locals>._process_parameters)r)  ri   r   r   r,  r:  )rL   r   r,  r`   r   r   rU   r"   rQ     s    
zmultinomial_frozen.__init__c                 C   s   | j || j| jS rR   )ri   r=  r   r,  r   r   r   r"   r=    s    zmultinomial_frozen.logpmfc                 C   s   | j || j| jS rR   )ri   r?  r   r,  r   r   r   r"   r?    s    zmultinomial_frozen.pmfc                 C   s   | j | j| jS rR   )ri   rr   r   r,  rU   r   r   r"   rr     s    zmultinomial_frozen.meanc                 C   s   | j | j| jS rR   )ri   rs   r   r,  rU   r   r   r"   rs     s    zmultinomial_frozen.covc                 C   s   | j | j| jS rR   )ri   r   r   r,  rU   r   r   r"   r     s    zmultinomial_frozen.entropyr   c                 C   s   | j | j| j||S rR   )ri   r   r   r,  r   r   r   r"   r     s    zmultinomial_frozen.rvs)N)r   N)rW   rX   rY   rZ   rQ   r=  r?  rr   rs   r   r   r   r   r   r"   r+    s   

r+  )r=  r?  rr   rs   r   c                       s>   e Zd ZdZd fdd	ZdddZdd Zdd
dZ  ZS )special_ortho_group_gena  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`. For a random rotation in three
    dimensions, see `scipy.spatial.transform.Rotation.random`.

    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.

    See Also
    --------
    ortho_group, scipy.spatial.transform.Rotation.random

    Nc                    s   t  | t| j| _d S rR   r]   rQ   r   ro   rZ   r_   ra   r   r"   rQ     s    z special_ortho_group_gen.__init__c                 C   s   t ||dS )zlCreate a frozen SO(N) distribution.

        See `special_ortho_group_frozen` for more information.
        r   )special_ortho_group_frozenrL   r   r`   r   r   r"   rt     s    z special_ortho_group_gen.__call__c                 C   s2   |du s&t |r&|dks&|t|kr.td|S )5Dimension N must be specified; it cannot be inferred.Nr   zmDimension of rotation must be specified,
                                and must be a scalar greater than 1.r*   rz   r   rB   rL   r   r   r   r"   r     s    &z+special_ortho_group_gen._process_parametersr   c           
         sh   t|}|dkr:t fddt|D S   t }t f}t d D ]}j | fd}t	||}|d 
 }	|d dkrt|d nd||< |d  || t| 7  < |t||	d  |d d  d  }|dd|df  tt	|dd|df ||8  < qfd	 d  |dd	   |d	< ||j j}|S )
as  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)

        r   c                    s   g | ]}j  d dqS r   ry   rc   r   r  r   rc   rL   r   r"   r6   (  s   z/special_ortho_group_gen.rvs.<locals>.<listcomp>r   r   r          @Nr$   )re   r   r*   r9   r   r   r}   r   r	  rS   itemsignrE   r   r   rT   )
rL   r   ry   rc   HDr   r4   norm2x0r   rM  r"   r     s(    


"&> zspecial_ortho_group_gen.rvs)N)NN)r   N)	rW   rX   rY   rZ   rQ   rt   r   r   rg   r   r   ra   r"   rC    s
   9
rC  c                   @   s    e Zd ZdddZdddZdS )	rE  Nc                 C   s   t || _| j|| _dS )a  Create a frozen SO(N) distribution.

        Parameters
        ----------
        dim : scalar
            Dimension of matrices
        seed : {None, int, `numpy.random.Generator`,
                `numpy.random.RandomState`}, optional

            If `seed` is None (or `np.random`), the `numpy.random.RandomState`
            singleton is used.
            If `seed` is an int, a new ``RandomState`` instance is used,
            seeded with `seed`.
            If `seed` is already a ``Generator`` or ``RandomState`` instance
            then that instance is used.

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

        N)rC  ri   r   r   rF  r   r   r"   rQ   B  s    
z#special_ortho_group_frozen.__init__r   c                 C   s   | j | j||S rR   )ri   r   r   r   r   r   r"   r   ]  s    zspecial_ortho_group_frozen.rvs)NN)r   N)rW   rX   rY   rQ   r   r   r   r   r"   rE  A  s   
rE  c                       s4   e Zd ZdZd
 fdd	Zdd Zddd	Z  ZS )ortho_group_gena  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.

    Nc                    s   t  | t| j| _d S rR   rD  r_   ra   r   r"   rQ     s    zortho_group_gen.__init__c                 C   s2   |du s&t |r&|dks&|t|kr.td|S rG  Nr   zLDimension of rotation must be specified,and must be a scalar greater than 1.rH  rI  r   r   r"   r     s    &z#ortho_group_gen._process_parametersr   c           
         s6   t|}|dkr:t fddt|D S   t }t D ]}j | fd}t||}|d 	 }|d dkrt
|d nd}	|d  |	t| 7  < |t||d  |d d  d  }|	 |dd|df tt|dd|df ||  |dd|df< qV|S )	ar  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)

        r   c                    s   g | ]}j  d dqS rJ  rL  r  rM  r   r"   r6     s   z'ortho_group_gen.rvs.<locals>.<listcomp>r   r   r   rN  N)re   r   r*   r9   r   r   r}   r	  rS   rO  rP  rE   r   )
rL   r   ry   rc   rQ  r   r4   rS  rT  rR  r   rM  r"   r     s"    


&Pzortho_group_gen.rvs)N)r   NrW   rX   rY   rZ   rQ   r   r   rg   r   r   ra   r"   rU  a  s   /rU  c                       sD   e Zd ZdZd fdd	Zdd Zdd Zd	d
 ZdddZ  Z	S )random_correlation_gena0  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
    >>> rng = np.random.default_rng()
    >>> x = random_correlation.rvs((.5, .8, 1.2, 1.5), random_state=rng)
    >>> x
    array([[ 1.        , -0.07198934, -0.20411041, -0.24385796],
           [-0.07198934,  1.        ,  0.12968613, -0.29471382],
           [-0.20411041,  0.12968613,  1.        ,  0.2828693 ],
           [-0.24385796, -0.29471382,  0.2828693 ,  1.        ]])
    >>> import scipy.linalg
    >>> e, v = scipy.linalg.eigh(x)
    >>> e
    array([ 0.5,  0.8,  1.2,  1.5])

    Nc                    s   t  | t| j| _d S rR   rD  r_   ra   r   r"   rQ     s    zrandom_correlation_gen.__init__c                 C   s   t j|td}|j}|jdks4|jd |ks4|dkr<tdt t || |kr\td|D ]}|| k r`tdq`||fS )Nr8   r   r   z7Array 'eigs' must be a vector of length greater than 1.z-Sum of eigenvalues must equal dimensionality.z%All eigenvalues must be non-negative.)	r*   r   r:   ry   r    rx   rB   fabsrH   )rL   eigstolr   r4   r   r   r"   r     s     

z*random_correlation_gen._process_parametersc           
      C   s   |d }|d }|dkrdS t t|d ||  d}|t || | }dt d||   }|dkrpd}	n|| }	||	fS )a&  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.

        ru   r   )g        ru   r   )mathrE   r,   copysign)
rL   ZaiiZajjZaijZaiidZajjdddr0   crO   r   r   r"   _givens_to_1  s    z#random_correlation_gen._givens_to_1c                 C   s>  |j jr(|jtjkr(|jd |jd ks.t |jd }t|d D ]}|||f dkr\qDnb|||f dkrt|d |D ]}|||f dk rz qqzn(t|d |D ]}|||f dkr qq| |||f |||f |||f \}}|	 }t
|||| ||| d|| dddd t
|||| |||||ddd qD|S )z
        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
        r   r   T)r   ZoffxZincxZoffyZincyZoverwrite_xZoverwrite_y)flagsc_contiguousr'   r*   r-  rx   rB   r   r`  Zravelr   )rL   mr&   r   jr_  rO   mvr   r   r"   _to_corr0  s4    
*zrandom_correlation_gen._to_corrvIh%<=Hz>c                 C   st   | j ||d\}}| |}tj||d}tt|t||j}| |}t	|
 d  |krptd|S )a  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[  )rc   r   z-Failed to generate a valid correlation matrix)r   re   r   r   r*   rS   r~   rT   rf  r-   r   r,   RuntimeError)rL   rZ  rc   r[  Zdiag_tolr   rc  r   r   r"   r   Y  s    

zrandom_correlation_gen.rvs)N)Nrg  rh  )
rW   rX   rY   rZ   rQ   r   r`  rf  r   rg   r   r   ra   r"   rX    s   2!)rX  c                       s4   e Zd ZdZd
 fdd	Zdd Zddd	Z  ZS )unitary_group_gena  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.

    Nc                    s   t  | t| j| _d S rR   rD  r_   ra   r   r"   rQ     s    zunitary_group_gen.__init__c                 C   s2   |du s&t |r&|dks&|t|kr.td|S rV  rH  rI  r   r   r"   r     s    &z%unitary_group_gen._process_parametersr   c                    s    t|}|dkr:t fddt|D S   dtd j  fddj  fd   }t	j
|\}}| }||t| 9 }|S )ai  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)

        r   c                    s   g | ]}j  d dqS rJ  rL  r  rM  r   r"   r6     s   z)unitary_group_gen.rvs.<locals>.<listcomp>r   r   y              ?)re   r   r*   r9   r   r   r\  rE   r	  r?   r@   Zqrr   r-   )rL   r   ry   rc   zqrr&   r   rM  r"   r     s    

zunitary_group_gen.rvs)N)r   NrW  r   r   ra   r"   rj    s   (rj  a  
loc : array_like, optional
    Location of the distribution. (default ``0``)
shape : array_like, optional
    Positive semidefinite matrix of the distribution. (default ``1``)
df : float, optional
    Degrees of freedom of the distribution; must be greater than zero.
    If ``np.inf`` then results are multivariate normal. The default is ``1``.
allow_singular : bool, optional
    Whether to allow a singular matrix. (default ``False``)
a  Setting the parameter `loc` to ``None`` is equivalent to having `loc`
be the zero-vector. The parameter `shape` can be a scalar, in which case
the shape matrix is the identity times that value, a vector of
diagonal entries for the shape matrix, or a two-dimensional array_like.
)_mvt_doc_default_callparams_mvt_doc_callparams_noterm   c                       sb   e Zd ZdZd fdd	ZdddZdd	d
ZdddZdd ZdddZ	dd Z
dd Z  ZS )multivariate_t_gena	  A multivariate t-distributed random variable.

    The `loc` parameter specifies the location. The `shape` parameter specifies
    the positive semidefinite shape matrix. The `df` parameter specifies the
    degrees of freedom.

    In addition to calling the methods below, the object itself may be called
    as a function to fix the location, shape matrix, and degrees of freedom
    parameters, returning a "frozen" multivariate t-distribution random.

    Methods
    -------
    ``pdf(x, loc=None, shape=1, df=1, allow_singular=False)``
        Probability density function.
    ``logpdf(x, loc=None, shape=1, df=1, allow_singular=False)``
        Log of the probability density function.
    ``rvs(loc=None, shape=1, df=1, size=1, random_state=None)``
        Draw random samples from a multivariate t-distribution.

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

    Notes
    -----
    %(_mvt_doc_callparams_note)s
    The matrix `shape` must be a (symmetric) positive semidefinite matrix. The
    determinant and inverse of `shape` are computed as the pseudo-determinant
    and pseudo-inverse, respectively, so that `shape` does not need to have
    full rank.

    The probability density function for `multivariate_t` is

    .. math::

        f(x) = \frac{\Gamma(\nu + p)/2}{\Gamma(\nu/2)\nu^{p/2}\pi^{p/2}|\Sigma|^{1/2}}
               \exp\left[1 + \frac{1}{\nu} (\mathbf{x} - \boldsymbol{\mu})^{\top}
               \boldsymbol{\Sigma}^{-1}
               (\mathbf{x} - \boldsymbol{\mu}) \right]^{-(\nu + p)/2},

    where :math:`p` is the dimension of :math:`\mathbf{x}`,
    :math:`\boldsymbol{\mu}` is the :math:`p`-dimensional location,
    :math:`\boldsymbol{\Sigma}` the :math:`p \times p`-dimensional shape
    matrix, and :math:`\nu` is the degrees of freedom.

    .. versionadded:: 1.6.0

    Examples
    --------
    >>> import matplotlib.pyplot as plt
    >>> from scipy.stats import multivariate_t
    >>> x, y = np.mgrid[-1:3:.01, -2:1.5:.01]
    >>> pos = np.dstack((x, y))
    >>> rv = multivariate_t([1.0, -0.5], [[2.1, 0.3], [0.3, 1.5]], df=2)
    >>> fig, ax = plt.subplots(1, 1)
    >>> ax.set_aspect('equal')
    >>> plt.contourf(x, y, rv.pdf(pos))

    Nc                    s*   t  | t| jt| _t|| _dS )zInitialize a multivariate t-distributed random variable.

        Parameters
        ----------
        seed : Random state.

        N)r]   rQ   r   ro   rZ   mvt_docdict_paramsr   r^   r_   ra   r   r"   rQ   B  s    zmultivariate_t_gen.__init__r   Fc                 C   s,   |t jkrt||||dS t|||||dS )zjCreate a frozen multivariate t-distribution.

        See `multivariate_t_frozen` for parameters.
        )rr   rs   rN   r`   )locrx   r   rN   r`   )r*   r   rq   multivariate_t_frozen)rL   rr  rx   r   rN   r`   r   r   r"   rt   N  s    
zmultivariate_t_gen.__call__c           	   	   C   sT   |  |||\}}}}| ||}t||d}| |||j|j|||j}t|S )ac  Multivariate t-distribution probability density function.

        Parameters
        ----------
        x : array_like
            Points at which to evaluate the probability density function.
        %(_mvt_doc_default_callparams)s

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

        Examples
        --------
        >>> from scipy.stats import multivariate_t
        >>> x = [0.4, 5]
        >>> loc = [0, 1]
        >>> shape = [[1, 0.1], [0.1, 1]]
        >>> df = 7
        >>> multivariate_t.pdf(x, loc, shape, df)
        array([0.00075713])

        r   )	r   r   r=   r   rG   rJ   rF   r*   r   )	rL   r4   rr  rx   r   rN   r   
shape_infor   r   r   r"   r   [  s    zmultivariate_t_gen.pdfc              	   C   sF   |  |||\}}}}| ||}t|}| |||j|j|||jS )a  Log of the multivariate t-distribution probability density function.

        Parameters
        ----------
        x : array_like
            Points at which to evaluate the log of the probability density
            function.
        %(_mvt_doc_default_callparams)s

        Returns
        -------
        logpdf : Log of the probability density function evaluated at `x`.

        Examples
        --------
        >>> from scipy.stats import multivariate_t
        >>> x = [0.4, 5]
        >>> loc = [0, 1]
        >>> shape = [[1, 0.1], [0.1, 1]]
        >>> df = 7
        >>> multivariate_t.logpdf(x, loc, shape, df)
        array([-7.1859802])

        See Also
        --------
        pdf : Probability density function.

        )r   r   r=   r   rG   rJ   rF   )rL   r4   rr  rx   r   r   rt  r   r   r"   r   z  s    zmultivariate_t_gen.logpdfc                 C   s   |t jkrt|||||S || }t t ||jdd}	d||  }
t|
}td| }|d t |t j	  }d| }|
 t dd| |	   }t
|| | | | S )aB  Utility method `pdf`, `logpdf` for parameters.

        Parameters
        ----------
        x : ndarray
            Points at which to evaluate the log of the probability density
            function.
        loc : ndarray
            Location of the distribution.
        prec_U : ndarray
            A decomposition such that `np.dot(prec_U, prec_U.T)` is the inverse
            of the shape matrix.
        log_pdet : float
            Logarithm of the determinant of the shape matrix.
        df : float
            Degrees of freedom of the distribution.
        dim : int
            Dimension of the quantiles x.
        rank : int
            Rank of the shape matrix.

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

        r$   r   r   rN  r   ru   )r*   r   r   r   r   rS   rH   r   rI   r   r#   )rL   r4   rr  r   rJ   r   r   rF   r   r   r0   r  Br   rR  Er   r   r"   r     s    
zmultivariate_t_gen._logpdfc                 C   s   |  |||\}}}}|dur(t|}n| j}t|rDt|}n|j||d| }|jt|||d}	||	t	|dddf   }
t
|
S )a  Draw random samples from a multivariate t-distribution.

        Parameters
        ----------
        %(_mvt_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`, `P`), where `P` is the
            dimension of the random variable.

        Examples
        --------
        >>> from scipy.stats import multivariate_t
        >>> x = [0.4, 5]
        >>> loc = [0, 1]
        >>> shape = [[1, 0.1], [0.1, 1]]
        >>> df = 7
        >>> multivariate_t.rvs(loc, shape, df)
        array([[0.93477495, 3.00408716]])

        Nr   )r   r   r^   r*   isinfZonesr  r   r{   rE   r#   )rL   rr  rx   r   ry   rc   r   rngr4   rk  Zsamplesr   r   r"   r     s     

zmultivariate_t_gen.rvsc                 C   s`   t j|td}|jdkr$|t j }n8|jdkr\|dkrJ|ddt jf }n|t jddf }|S r   r   r   r   r   r"   r     s    

z%multivariate_t_gen._process_quantilesc                 C   s  |du r2|du r2t jdtd}t jdtd}d}n|du rnt j|td}|jdk rXd}n
|jd }t |}nJ|du rt j|td}|j}t |}n"t j|td}t j|td}|j}|dkr|d}|dd}|jdks|jd |krt	d| |jdkr|t | }n|jdkr.t 
|}n~|jdkr|j||fkr|j\}}||krndt|j }nd}|t|jt|f }t	|n|jdkrt	d	|j |du rd}n(|dkrt	d
nt |rt	d||||fS )z
        Infer dimensionality from location array and shape matrix, handle
        defaults, and ensure compatible dimensions.
        Nr   r8   r   r   z*Array 'loc' must be a vector of length %d.rv   zSDimension mismatch: array 'cov' is of shape %s, but 'loc' is a vector of length %d.rw   z'df' must be greater than zero.z8'df' is 'nan' but must be greater than zero or 'np.inf'.)r*   r   r:   r    rx   r{   ry   r}   r|   rB   r~   r   rC   isnan)rL   rr  rx   r   r   r   r   r   r   r   r"   r     s`    








z&multivariate_t_gen._process_parameters)N)Nr   r   FN)Nr   r   F)Nr   r   )Nr   r   r   N)rW   rX   rY   rZ   rQ   rt   r   r   r   r   r   r   rg   r   r   ra   r"   rp    s   ?  


#+
/rp  c                   @   s0   e Zd ZdddZdd Zdd	 Zdd
dZdS )rs  Nr   Fc                 C   sP   t || _| j|||\}}}}||||f\| _| _| _| _t||d| _dS )a  Create a frozen multivariate t distribution.

        Parameters
        ----------
        %(_mvt_doc_default_callparams)s

        Examples
        --------
        >>> loc = np.zeros(3)
        >>> shape = np.eye(3)
        >>> df = 10
        >>> dist = multivariate_t(loc, shape, df)
        >>> dist.rvs()
        array([[ 0.81412036, -1.53612361,  0.42199647]])
        >>> dist.pdf([1, 1, 1])
        array([0.01237803])

        r   N)	rp  ri   r   r   rr  rx   r   r=   rt  )rL   rr  rx   r   rN   r`   r   r   r   r"   rQ   E  s    
zmultivariate_t_frozen.__init__c              	   C   sB   | j || j}| jj}| jj}| j || j||| j| j| jj	S rR   )
ri   r   r   rt  rG   rJ   r   rr  r   rF   )rL   r4   rG   rJ   r   r   r"   r   ^  s    zmultivariate_t_frozen.logpdfc                 C   s   t | |S rR   r   r   r   r   r"   r   e  s    zmultivariate_t_frozen.pdfc                 C   s   | j j| j| j| j||dS )N)rr  rx   r   ry   rc   )ri   r   rr  rx   r   r   r   r   r"   r   h  s    
zmultivariate_t_frozen.rvs)Nr   r   FN)r   N)rW   rX   rY   rQ   r   r   r   r   r   r   r"   rs  C  s     
rs  zm : array_like
    The number of each type of object in the population.
    That is, :math:`m[i]` is the number of objects of
    type :math:`i`.
n : array_like
    The number of samples taken from the population.
a  `m` must be an array of positive integers. If the quantile
:math:`i` contains values out of the range :math:`[0, m_i]`
where :math:`m_i` is the number of objects of type :math:`i`
in the population or if the parameters are inconsistent with one
another (e.g. ``x.sum() != n``), methods return the appropriate
value (e.g. ``0`` for ``pmf``). If `m` or `n` contain negative
values, the result will contain ``nan`` there.
c                       s~   e Zd ZdZd fdd	ZdddZdd Zd	d
 Zdd Zdd Z	dd Z
dd Zdd Zdd Zdd ZdddZ  ZS )multivariate_hypergeom_gena  A multivariate hypergeometric random variable.

    Methods
    -------
    ``pmf(x, m, n)``
        Probability mass function.
    ``logpmf(x, m, n)``
        Log of the probability mass function.
    ``rvs(m, n, size=1, random_state=None)``
        Draw random samples from a multivariate hypergeometric
        distribution.
    ``mean(m, n)``
        Mean of the multivariate hypergeometric distribution.
    ``var(m, n)``
        Variance of the multivariate hypergeometric distribution.
    ``cov(m, n)``
        Compute the covariance matrix of the multivariate
        hypergeometric distribution.

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

    Notes
    -----
    %(_doc_callparams_note)s

    The probability mass function for `multivariate_hypergeom` is

    .. math::

        P(X_1 = x_1, X_2 = x_2, \ldots, X_k = x_k) = \frac{\binom{m_1}{x_1}
        \binom{m_2}{x_2} \cdots \binom{m_k}{x_k}}{\binom{M}{n}}, \\ \quad
        (x_1, x_2, \ldots, x_k) \in \mathbb{N}^k \text{ with }
        \sum_{i=1}^k x_i = n

    where :math:`m_i` are the number of objects of type :math:`i`, :math:`M`
    is the total number of objects in the population (sum of all the
    :math:`m_i`), and :math:`n` is the size of the sample to be taken
    from the population.

    .. versionadded:: 1.6.0

    Examples
    --------
    To evaluate the probability mass function of the multivariate
    hypergeometric distribution, with a dichotomous population of size
    :math:`10` and :math:`20`, at a sample of size :math:`12` with
    :math:`8` objects of the first type and :math:`4` objects of the
    second type, use:

    >>> from scipy.stats import multivariate_hypergeom
    >>> multivariate_hypergeom.pmf(x=[8, 4], m=[10, 20], n=12)
    0.0025207176631464523

    The `multivariate_hypergeom` distribution is identical to the
    corresponding `hypergeom` distribution (tiny numerical differences
    notwithstanding) when only two types (good and bad) of objects
    are present in the population as in the example above. Consider
    another example for a comparison with the hypergeometric distribution:

    >>> from scipy.stats import hypergeom
    >>> multivariate_hypergeom.pmf(x=[3, 1], m=[10, 5], n=4)
    0.4395604395604395
    >>> hypergeom.pmf(k=3, M=15, n=4, N=10)
    0.43956043956044005

    The functions ``pmf``, ``logpmf``, ``mean``, ``var``, ``cov``, and ``rvs``
    support broadcasting, under the convention that the vector parameters
    (``x``, ``m``, and ``n``) are interpreted as if each row along the last
    axis is a single object. For instance, we can combine the previous two
    calls to `multivariate_hypergeom` as

    >>> multivariate_hypergeom.pmf(x=[[8, 4], [3, 1]], m=[[10, 20], [10, 5]],
    ...                            n=[12, 4])
    array([0.00252072, 0.43956044])

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

    >>> multivariate_hypergeom.cov(m=[[7, 9], [10, 15]], n=[8, 12])
    array([[[ 1.05, -1.05],
            [-1.05,  1.05]],
           [[ 1.56, -1.56],
            [-1.56,  1.56]]])

    That is, ``result[0]`` is equal to
    ``multivariate_hypergeom.cov(m=[7, 9], n=8)`` and ``result[1]`` is equal
    to ``multivariate_hypergeom.cov(m=[10, 15], n=12)``.

    Alternatively, the object may be called (as a function) to fix the `m`
    and `n` parameters, returning a "frozen" multivariate hypergeometric
    random variable.

    >>> rv = multivariate_hypergeom(m=[10, 20], n=12)
    >>> rv.pmf(x=[8, 4])
    0.0025207176631464523

    See Also
    --------
    scipy.stats.hypergeom : The hypergeometric distribution.
    scipy.stats.multinomial : The multinomial distribution.

    References
    ----------
    .. [1] The Multivariate Hypergeometric Distribution,
           http://www.randomservices.org/random/urn/MultiHypergeometric.html
    .. [2] Thomas J. Sargent and John Stachurski, 2020,
           Multivariate Hypergeometric Distribution
           https://python.quantecon.org/_downloads/pdf/multi_hyper.pdf
    Nc                    s    t  | t| jt| _d S rR   )r]   rQ   r   ro   rZ   mhg_docdict_paramsr_   ra   r   r"   rQ     s    z#multivariate_hypergeom_gen.__init__c                 C   s   t |||dS )zCreate a frozen multivariate_hypergeom distribution.

        See `multivariate_hypergeom_frozen` for more information.
        r   )multivariate_hypergeom_frozen)rL   rc  r   r`   r   r   r"   rt     s    z#multivariate_hypergeom_gen.__call__c              	   C   s   t |}t |}|jdkr(|t}|jdkr<|t}t |jt jsTtdt |jt jsltd|j	dkr~t
d|jdkr|dt jf }t ||\}}|jdkr|d }|dk }|jdd}|dk ||kB }|||||t j|dd|B fS )	Nr   z'm' must an array of integers.z'n' must an array of integers.z1'm' must be an array with at least one dimension...r   r$   r   )r*   r   ry   Zastyper   
issubdtyper'   integer	TypeErrorr    rB   r   broadcast_arraysrH   r   )rL   rc  r   mcondrM   r/  r   r   r"   r     s*    








z.multivariate_hypergeom_gen._process_parametersc              	   C   s   t |}t |jt js"td|jdkr4td|jd |jd ksjtd|jd  d|jd  d|j	dkr|dt j
f }|dt j
f }t ||||\}}}}|j	dkr|d	 |d	  }}|dk ||kB }|||||t j|dd
|jdd
|kB fS )Nz'x' must an array of integers.r   z1'x' must be an array with at least one dimension.r$   z4Size of each quantile must be size of 'm': received z, but expected ..r}  r   )r*   r   r~  r'   r  r  r    rB   rx   ry   r   r  r   rH   )rL   r4   rM   rc  r   r;  r   r   r"   r   >  s*    





z-multivariate_hypergeom_gen._process_quantilesc                 C   s<   t |}|jdkr|||< n|r&|S |jdkr8|d S |S )Nr   r   r1  r2  r   r   r"   r4  Y  s    



z'multivariate_hypergeom_gen._checkresultc           	      C   s   t j|t jd}t j|t jd}||  ||   }}||  ||   }}t|d dt|d || d  || < t|d dt|d || d  || < t j||< t j||< |jdd}|| S )Nr8   r   r$   r   )r*   Z
zeros_likeZfloat_r
   rA  rH   )	rL   r4   rM   rc  r   mxcondr/  numZdenr   r   r"   r6  c  s    **

z"multivariate_hypergeom_gen._logpmfc                 C   s   |  ||\}}}}}}| ||||\}}}}}}	||B }
|tj|jtjdB }| |||||
|}|	tj|jtjdB }| ||tj}|tj|	jtjdB }| ||tj	S )a  Log of the multivariate hypergeometric 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
        r8   )
r   r   r*   r{   rx   r7  r6  r4  r8  rA  )rL   r4   rc  r   rM   r  r/  mncondr;  Zxcond_reducedr  r3  r<  Zmncond_r   r   r"   r=  q  s    z!multivariate_hypergeom_gen.logpmfc                 C   s   t | |||}|S )a  Multivariate hypergeometric 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>  )rL   r4   rc  r   r!   r   r   r"   r?    s    zmultivariate_hypergeom_gen.pmfc                 C   s   |  ||\}}}}}}|jdkr@|dtjf |dtjf  }}|dk}tjj||d}|||  }|jdkr|dtjf tj|jtjdB }| 	||tj
S )zMean of the multivariate hypergeometric distribution.

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

        Returns
        -------
        mean : array_like or scalar
            The mean of the distribution
        r   .maskr8   r   ry   r*   r   mamasked_arrayr{   rx   r7  r4  rA  )rL   rc  r   rM   r   r  r.   mur   r   r"   rr     s    

zmultivariate_hypergeom_gen.meanc                 C   s   |  ||\}}}}}}|jdkr@|dtjf |dtjf  }}|dk|d dk@ }tjj||d}|| | ||  | ||  |d  }|jdkr|dtjf tj|jtjdB }| 	||tj
S )aQ  Variance of the multivariate hypergeometric distribution.

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

        Returns
        -------
        array_like
            The variances of the components of the distribution.  This is
            the diagonal of the covariance matrix of the distribution
        r   .r   r  r8   r  )rL   rc  r   rM   r   r  r.   outputr   r   r"   r     s    
(
zmultivariate_hypergeom_gen.varc           
      C   s  |  ||\}}}}}}|jdkrF|dtjtjf }|dtjtjf }|dk|d dk@ }tjj||d}| ||  |d  td|| |d  }|jdkr|d |d  }}|d }|jd }t|D ]v}	|||  |d|	f  ||d|	f   |d|	|	f< |d|	|	f |d  |d|	|	f< |d|	|	f |d  |d|	|	f< q|jdkrt|dtjtjf tj	|jtj
d	B }| ||tjS )
a  Covariance matrix of the multivariate hypergeometric distribution.

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

        Returns
        -------
        cov : array_like
            The covariance matrix of the distribution
        r   .r   r  z...i,...j->...ijr   ).r   r   r$   r8   )r   ry   r*   r   r  r  r@  rx   r   r{   r7  r4  rA  )
rL   rc  r   rM   r   r  r.   r  r   r   r   r   r"   rs     s0    


2 "zmultivariate_hypergeom_gen.covc           
      C   s   |  ||\}}}}}}| |}|dur:t|tr:|f}|du rVtj|j|jd}ntj||jd f |jd}|}t|jd d D ]V}	||d|	f  }|dk|j	|d|	f |||dk |d |d|	f< ||d|	f  }q||d|jd d f< |S )a  Draw random samples from a multivariate hypergeometric distribution.

        Parameters
        ----------
        %(_doc_default_callparams)s
        size : integer or iterable of integers, optional
            Number of samples to draw. Default is ``None``, in which case a
            single variate is returned as an array with shape ``m.shape``.
        %(_doc_random_state)s

        Returns
        -------
        rvs : array_like
            Random variates of shape ``size`` or ``m.shape``
            (if ``size=None``).

        Notes
        -----
        %(_doc_callparams_note)s

        Also note that NumPy's `multivariate_hypergeometric` sampler is not
        used as it doesn't support broadcasting.
        Nr8   r$   r   .r   r   )
r   re   
isinstancer   r*   r   rx   r'   r   Zhypergeometric)
rL   rc  r   ry   rc   rM   r   r   remr_  r   r   r"   r     s&    

zmultivariate_hypergeom_gen.rvs)N)N)NN)rW   rX   rY   rZ   rQ   rt   r   r   r4  r6  r=  r?  rr   r   rs   r   rg   r   r   ra   r"   rz    s   p
 
$&rz  c                   @   sH   e Zd ZdddZdd Zdd Zdd	 Zd
d Zdd ZdddZ	dS )r|  Nc                    sH   t | _ j||\ _ _ _ _ _ _ fdd}| j_d S )Nc                    s    j  j j j j jfS rR   )rM   rc  r   r  r/  r  )rc  r   rU   r   r"   r   ?  s    zCmultivariate_hypergeom_frozen.__init__.<locals>._process_parameters)	rz  ri   r   rM   rc  r   r  r/  r  )rL   rc  r   r`   r   r   rU   r"   rQ   8  s    
z&multivariate_hypergeom_frozen.__init__c                 C   s   | j || j| jS rR   )ri   r=  rc  r   r   r   r   r"   r=  E  s    z$multivariate_hypergeom_frozen.logpmfc                 C   s   | j || j| jS rR   )ri   r?  rc  r   r   r   r   r"   r?  H  s    z!multivariate_hypergeom_frozen.pmfc                 C   s   | j | j| jS rR   )ri   rr   rc  r   rU   r   r   r"   rr   K  s    z"multivariate_hypergeom_frozen.meanc                 C   s   | j | j| jS rR   )ri   r   rc  r   rU   r   r   r"   r   N  s    z!multivariate_hypergeom_frozen.varc                 C   s   | j | j| jS rR   )ri   rs   rc  r   rU   r   r   r"   rs   Q  s    z!multivariate_hypergeom_frozen.covr   c                 C   s   | j j| j| j||dS )NrK  )ri   r   rc  r   r   r   r   r"   r   T  s    z!multivariate_hypergeom_frozen.rvs)N)r   N)
rW   rX   rY   rQ   r=  r?  rr   r   rs   r   r   r   r   r"   r|  7  s   
r|  )r=  r?  rr   r   rs   r   )NN)r2   )T)|r\  Znumpyr*   r   r   Zscipy.linalgr?   Z
scipy._libr   Zscipy.specialr   r   r   r   r	   r
   Zscipy._lib._utilr   Zscipy.linalg.blasr   Zscipy.linalg.miscr   Zscipy.linalg.lapackr   Z_discrete_distnsr   rj   r   __all__rI   r   r   r   Z_LOG_PIrm   r#   r1   r<   r=   r\   rh   rk   rl   Z_mvn_doc_frozen_callparamsZ_mvn_doc_frozen_callparams_noterp   Zmvn_docdict_noparamsrn   r   rq   name__dict__methodZmethod_frozenro   rZ   r   r   Z_matnorm_doc_frozen_callparamsZ#_matnorm_doc_frozen_callparams_noter   Zmatnorm_docdict_noparamsr   r   r   r   Z _dirichlet_doc_frozen_callparamsZ%_dirichlet_doc_frozen_callparams_noter   Zdirichlet_docdict_noparamsr   r   r   r   r   r   Z_wishart_doc_default_callparamsZ_wishart_doc_callparams_noteZ_wishart_doc_frozen_callparamsZ#_wishart_doc_frozen_callparams_noter   Zwishart_docdict_noparamsr   r   r   r  r   r   r!  Z#_multinomial_doc_default_callparamsZ _multinomial_doc_callparams_noteZ"_multinomial_doc_frozen_callparamsZ'_multinomial_doc_frozen_callparams_noter*  Zmultinomial_docdict_noparamsr)  r   r+  rC  r   rE  rU  r   rX  r   rj  r   rn  ro  Z_mvt_doc_frozen_callparams_noterq  Zmvt_docdict_noparamsrp  rs  r   Z_mhg_doc_default_callparamsZ_mhg_doc_callparams_noteZ_mhg_doc_frozen_callparamsZ_mhg_doc_frozen_callparams_noter{  Zmhg_docdict_noparamsrz  r   r|  r   r   r   r"   <module>   s   

"
G"
   ]


  ;8


,  

    c<


?   F

  H2


w c <V  C-

	
   %

