a
    ˜¥»ahÈ  ã                   @   sD  d dl mZ d dlmZ d dlmZmZmZmZ	m
Z
 d dlmZmZ d dlmZ d dlmZmZmZmZmZmZmZmZmZmZ d dlZdd	lmZmZmZm Z m!Z! d dl"m#  m$Z$ dd
l%m&Z&m'Z'm(Z( G dd„ deƒZ)e)dd�Z*G dd„ de)ƒZ+e+ddd�Z,G dd„ deƒZ-e-dd�Z.G dd„ deƒZ/e/dd�Z0G dd„ deƒZ1e1dddd�Z2G dd„ deƒZ3e3d d�Z4G d!d"„ d"eƒZ5e5d#d�Z6G d$d%„ d%eƒZ7e7dd&d'd�Z8G d(d)„ d)eƒZ9e9d*d+d,�Z:G d-d.„ d.eƒZ;e;d d/d0d�Z<G d1d2„ d2eƒZ=e=d3d d4d5�Z>G d6d7„ d7eƒZ?e?d8d9d,�Z@G d:d;„ d;eƒZAeAdd<d=d�ZBd>d?„ ZCd@dA„ ZDdBdC„ ZEG dDdE„ dEeƒZFeFddFdGd�ZGG dHdI„ dIeƒZHeHejI dJdKd�ZJG dLdM„ dMeƒZKeKejI dNdOd�ZLG dPdQ„ dQeƒZMeMdRddS�ZNdTdU„ ZOG dVdW„ dWeƒZPG dXdY„ dYePƒZQeQdZd[d,�ZRG d\d]„ d]ePƒZSeSd^d_d,�ZTeUeVƒ  W¡ ƒZXe eXeƒ\ZYZZeYeZ Z[dS )`é    )Úpartial)Úspecial)ÚentrÚ	logsumexpÚbetalnÚgammalnÚzeta)Ú
_lazywhereÚrng_integers)Úinterp1d)
ÚfloorÚceilÚlogÚexpÚsqrtÚlog1pÚexpm1ÚtanhÚcoshÚsinhNé   )Úrv_discreteÚ	_ncx2_pdfÚ	_ncx2_cdfÚget_distribution_namesÚ_check_shape)Ú_PyFishersNCHypergeometricÚ_PyWalleniusNCHypergeometricÚ_PyStochasticLib3c                   @   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„ Z
dd„ Zdd„ Zddd„Zdd„ ZdS )Ú	binom_gena  A binomial discrete random variable.

    %(before_notes)s

    Notes
    -----
    The probability mass function for `binom` is:

    .. math::

       f(k) = \binom{n}{k} p^k (1-p)^{n-k}

    for :math:`k \in \{0, 1, \dots, n\}`, :math:`0 \leq p \leq 1`

    `binom` takes :math:`n` and :math:`p` as shape parameters,
    where :math:`p` is the probability of a single success
    and :math:`1-p` is the probability of a single failure.

    %(after_notes)s

    %(example)s

    See Also
    --------
    hypergeom, nbinom, nhypergeom

    Nc                 C   s   |  |||¡S ©N)Úbinomial©ÚselfÚnÚpÚsizeÚrandom_state© r(   úV/home/yuhw/wc/pochoir/venv/lib/python3.9/site-packages/scipy/stats/_discrete_distns.pyÚ_rvs3   s    zbinom_gen._rvsc                 C   s   |dk|dk@ |dk@ S ©Nr   r   r(   ©r#   r$   r%   r(   r(   r)   Ú	_argcheck6   s    zbinom_gen._argcheckc                 C   s
   | j |fS r    ©Úar,   r(   r(   r)   Ú_get_support9   s    zbinom_gen._get_supportc                 C   sR   t |ƒ}t|d ƒt|d ƒt|| d ƒ  }|t ||¡ t || | ¡ S ©Nr   )r   Úgamlnr   ÚxlogyÚxlog1py)r#   Úxr$   r%   ÚkÚcombilnr(   r(   r)   Ú_logpmf<   s    (zbinom_gen._logpmfc                 C   s   t  |||¡S r    )Ú_boostZ
_binom_pdf©r#   r5   r$   r%   r(   r(   r)   Ú_pmfA   s    zbinom_gen._pmfc                 C   s   t |ƒ}t |||¡S r    )r   r9   Z
_binom_cdf©r#   r5   r$   r%   r6   r(   r(   r)   Ú_cdfE   s    zbinom_gen._cdfc                 C   s   t |ƒ}t |||¡S r    )r   r9   Z	_binom_sfr<   r(   r(   r)   Ú_sfI   s    zbinom_gen._sfc                 C   s   t  |||¡S r    )r9   Z
_binom_isfr:   r(   r(   r)   Ú_isfM   s    zbinom_gen._isfc                 C   s   t  |||¡S r    )r9   Z
_binom_ppf©r#   Úqr$   r%   r(   r(   r)   Ú_ppfP   s    zbinom_gen._ppfÚmvc                 C   sT   t  ||¡}t  ||¡}d\}}d|v r4t  ||¡}d|v rHt  ||¡}||||fS )N©NNÚsr6   )r9   Z_binom_meanZ_binom_varianceZ_binom_skewnessZ_binom_kurtosis_excess)r#   r$   r%   ÚmomentsÚmuÚvarÚg1Úg2r(   r(   r)   Ú_statsS   s    zbinom_gen._statsc                 C   s2   t jd|d … }|  |||¡}t jt|ƒdd�S )Nr   r   ©Zaxis)ÚnpÚr_r;   Úsumr   )r#   r$   r%   r6   Úvalsr(   r(   r)   Ú_entropy]   s    zbinom_gen._entropy)NN)rC   ©Ú__name__Ú
__module__Ú__qualname__Ú__doc__r*   r-   r0   r8   r;   r=   r>   r?   rB   rK   rQ   r(   r(   r(   r)   r      s   


r   Úbinom)Únamec                   @   sj   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„ Z
dd„ Zdd„ Zdd„ Zdd„ ZdS )Úbernoulli_gena  A Bernoulli discrete random variable.

    %(before_notes)s

    Notes
    -----
    The probability mass function for `bernoulli` is:

    .. math::

       f(k) = \begin{cases}1-p  &\text{if } k = 0\\
                           p    &\text{if } k = 1\end{cases}

    for :math:`k` in :math:`\{0, 1\}`, :math:`0 \leq p \leq 1`

    `bernoulli` takes :math:`p` as shape parameter,
    where :math:`p` is the probability of a single success
    and :math:`1-p` is the probability of a single failure.

    %(after_notes)s

    %(example)s

    Nc                 C   s   t j| d|||d�S )Nr   ©r&   r'   )r   r*   ©r#   r%   r&   r'   r(   r(   r)   r*      s    zbernoulli_gen._rvsc                 C   s   |dk|dk@ S r+   r(   ©r#   r%   r(   r(   r)   r-   ‚   s    zbernoulli_gen._argcheckc                 C   s   | j | jfS r    )r/   Úbr\   r(   r(   r)   r0   …   s    zbernoulli_gen._get_supportc                 C   s   t  |d|¡S r1   )rW   r8   ©r#   r5   r%   r(   r(   r)   r8   ‰   s    zbernoulli_gen._logpmfc                 C   s   t  |d|¡S r1   )rW   r;   r^   r(   r(   r)   r;   Œ   s    zbernoulli_gen._pmfc                 C   s   t  |d|¡S r1   )rW   r=   r^   r(   r(   r)   r=   ‘   s    zbernoulli_gen._cdfc                 C   s   t  |d|¡S r1   )rW   r>   r^   r(   r(   r)   r>   ”   s    zbernoulli_gen._sfc                 C   s   t  |d|¡S r1   )rW   r?   r^   r(   r(   r)   r?   —   s    zbernoulli_gen._isfc                 C   s   t  |d|¡S r1   )rW   rB   )r#   rA   r%   r(   r(   r)   rB   š   s    zbernoulli_gen._ppfc                 C   s   t  d|¡S r1   )rW   rK   r\   r(   r(   r)   rK   �   s    zbernoulli_gen._statsc                 C   s   t |ƒt d| ƒ S r1   )r   r\   r(   r(   r)   rQ       s    zbernoulli_gen._entropy)NNrR   r(   r(   r(   r)   rY   f   s   
rY   Ú	bernoulli)r]   rX   c                   @   sD   e Zd ZdZddd„Zdd„ Zdd„ Zd	d
„ Zdd„ Zddd„Z	dS )Úbetabinom_gena  A beta-binomial discrete random variable.

    %(before_notes)s

    Notes
    -----
    The beta-binomial distribution is a binomial distribution with a
    probability of success `p` that follows a beta distribution.

    The probability mass function for `betabinom` is:

    .. math::

       f(k) = \binom{n}{k} \frac{B(k + a, n - k + b)}{B(a, b)}

    for :math:`k \in \{0, 1, \dots, n\}`, :math:`n \geq 0`, :math:`a > 0`,
    :math:`b > 0`, where :math:`B(a, b)` is the beta function.

    `betabinom` takes :math:`n`, :math:`a`, and :math:`b` as shape parameters.

    References
    ----------
    .. [1] https://en.wikipedia.org/wiki/Beta-binomial_distribution

    %(after_notes)s

    .. versionadded:: 1.4.0

    See Also
    --------
    beta, binom

    %(example)s

    Nc                 C   s   |  |||¡}| |||¡S r    )Úbetar!   )r#   r$   r/   r]   r&   r'   r%   r(   r(   r)   r*   Ì   s    zbetabinom_gen._rvsc                 C   s   d|fS ©Nr   r(   ©r#   r$   r/   r]   r(   r(   r)   r0   Ð   s    zbetabinom_gen._get_supportc                 C   s   |dk|dk@ |dk@ S rb   r(   rc   r(   r(   r)   r-   Ó   s    zbetabinom_gen._argcheckc                 C   sP   t |ƒ}t|d ƒ t|| d |d ƒ }|t|| || | ƒ t||ƒ S r1   )r   r   r   )r#   r5   r$   r/   r]   r6   r7   r(   r(   r)   r8   Ö   s    $zbetabinom_gen._logpmfc                 C   s   t |  ||||¡ƒS r    ©r   r8   )r#   r5   r$   r/   r]   r(   r(   r)   r;   Û   s    zbetabinom_gen._pmfrC   c                 C   sz  |||  }d| }|| }||| |  | | || d  }d\}	}
d|v r�dt |ƒ }	|	|| d|  ||  9 }	|	|| d ||   }	d|v �rn|| }
|
|| d d|  9 }
|
d| | |d  7 }
|
d|d  7 }
|
d| | | d|  8 }
|
d	| | |d  8 }
|
|| d d| |  9 }
|
|| | || d  || d  || |   }
|
d8 }
|||	|
fS )
Nr   rD   rE   ç      ð?é   r6   é   é   é   ©r   )r#   r$   r/   r]   rF   Ze_pZe_qrG   rH   rI   rJ   r(   r(   r)   rK   Þ   s(    $
4zbetabinom_gen._stats)NN)rC   )
rS   rT   rU   rV   r*   r0   r-   r8   r;   rK   r(   r(   r(   r)   r`   §   s   $
r`   Ú	betabinomc                   @   sb   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„ Z
dd„ Zdd„ Zdd„ ZdS )Ú
nbinom_gena±  A negative binomial discrete random variable.

    %(before_notes)s

    Notes
    -----
    Negative binomial distribution describes a sequence of i.i.d. Bernoulli
    trials, repeated until a predefined, non-random number of successes occurs.

    The probability mass function of the number of failures for `nbinom` is:

    .. math::

       f(k) = \binom{k+n-1}{n-1} p^n (1-p)^k

    for :math:`k \ge 0`, :math:`0 < p \leq 1`

    `nbinom` takes :math:`n` and :math:`p` as shape parameters where n is the
    number of successes, :math:`p` is the probability of a single success,
    and :math:`1-p` is the probability of a single failure.

    Another common parameterization of the negative binomial distribution is
    in terms of the mean number of failures :math:`\mu` to achieve :math:`n`
    successes. The mean :math:`\mu` is related to the probability of success
    as

    .. math::

       p = \frac{n}{n + \mu}

    The number of successes :math:`n` may also be specified in terms of a
    "dispersion", "heterogeneity", or "aggregation" parameter :math:`\alpha`,
    which relates the mean :math:`\mu` to the variance :math:`\sigma^2`,
    e.g. :math:`\sigma^2 = \mu + \alpha \mu^2`. Regardless of the convention
    used for :math:`\alpha`,

    .. math::

       p &= \frac{\mu}{\sigma^2} \\
       n &= \frac{\mu^2}{\sigma^2 - \mu}

    %(after_notes)s

    %(example)s

    See Also
    --------
    hypergeom, binom, nhypergeom

    Nc                 C   s   |  |||¡S r    )Znegative_binomialr"   r(   r(   r)   r*   +  s    znbinom_gen._rvsc                 C   s   |dk|dk@ |dk@ S r+   r(   r,   r(   r(   r)   r-   .  s    znbinom_gen._argcheckc                 C   s   t  |||¡S r    )r9   Z_nbinom_pdfr:   r(   r(   r)   r;   1  s    znbinom_gen._pmfc                 C   s>   t || ƒt |d ƒ t |ƒ }||t|ƒ  t || ¡ S r1   )r2   r   r   r4   )r#   r5   r$   r%   Zcoeffr(   r(   r)   r8   5  s     znbinom_gen._logpmfc                 C   s   t |ƒ}t |||¡S r    )r   r9   Z_nbinom_cdfr<   r(   r(   r)   r=   9  s    znbinom_gen._cdfc                    sx   t |ƒ}|  |||¡‰ ˆ dk}dd„ }‡ fdd„}tjdd��& t||||f||d�W  d   ƒ S 1 sj0    Y  d S )	Nç      à?c                 S   s   t  t | d |d| ¡ ¡S r1   )rM   r   r   Zbetainc©r6   r$   r%   r(   r(   r)   Úf1B  s    znbinom_gen._logcdf.<locals>.f1c                    s
   t  ˆ ¡S r    )rM   r   rn   ©Úcdfr(   r)   Úf2E  s    znbinom_gen._logcdf.<locals>.f2Úignore)Údivide©Úfrr   )r   r=   rM   Zerrstater	   )r#   r5   r$   r%   r6   Úcondro   rr   r(   rp   r)   Ú_logcdf=  s    znbinom_gen._logcdfc                 C   s   t |ƒ}t |||¡S r    )r   r9   Z
_nbinom_sfr<   r(   r(   r)   r>   K  s    znbinom_gen._sfc                 C   s   t  |||¡S r    )r9   Z_nbinom_isfr:   r(   r(   r)   r?   O  s    znbinom_gen._isfc                 C   s   t  |||¡S r    )r9   Z_nbinom_ppfr@   r(   r(   r)   rB   R  s    znbinom_gen._ppfc                 C   s,   t  ||¡t  ||¡t  ||¡t  ||¡fS r    )r9   Z_nbinom_meanZ_nbinom_varianceZ_nbinom_skewnessZ_nbinom_kurtosis_excessr,   r(   r(   r)   rK   U  s
    



üznbinom_gen._stats)NN)rS   rT   rU   rV   r*   r-   r;   r8   r=   rx   r>   r?   rB   rK   r(   r(   r(   r)   rl   ø   s   2
rl   Únbinomc                   @   sZ   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„ Z
dd„ Zdd„ ZdS )Úgeom_genaÖ  A geometric discrete random variable.

    %(before_notes)s

    Notes
    -----
    The probability mass function for `geom` is:

    .. math::

        f(k) = (1-p)^{k-1} p

    for :math:`k \ge 1`, :math:`0 < p \leq 1`

    `geom` takes :math:`p` as shape parameter,
    where :math:`p` is the probability of a single success
    and :math:`1-p` is the probability of a single failure.

    %(after_notes)s

    See Also
    --------
    planck

    %(example)s

    Nc                 C   s   |j ||d�S ©N©r&   )Ú	geometricr[   r(   r(   r)   r*   }  s    zgeom_gen._rvsc                 C   s   |dk|dk@ S ©Nr   r   r(   r\   r(   r(   r)   r-   €  s    zgeom_gen._argcheckc                 C   s   t  d| |d ¡| S r1   )rM   Úpower©r#   r6   r%   r(   r(   r)   r;   ƒ  s    zgeom_gen._pmfc                 C   s   t  |d | ¡t|ƒ S r1   )r   r4   r   r€   r(   r(   r)   r8   †  s    zgeom_gen._logpmfc                 C   s   t |ƒ}tt| ƒ| ƒ S r    )r   r   r   ©r#   r5   r%   r6   r(   r(   r)   r=   ‰  s    zgeom_gen._cdfc                 C   s   t  |  ||¡¡S r    )rM   r   Ú_logsfr^   r(   r(   r)   r>   �  s    zgeom_gen._sfc                 C   s   t |ƒ}|t| ƒ S r    )r   r   r�   r(   r(   r)   r‚   �  s    zgeom_gen._logsfc                 C   sF   t t| ƒt| ƒ ƒ}|  |d |¡}t ||k|dk@ |d |¡S r~   )r   r   r=   rM   Úwhere)r#   rA   r%   rP   Útempr(   r(   r)   rB   ”  s    zgeom_gen._ppfc                 C   sP   d| }d| }|| | }d| t |ƒ }t g d¢|¡d|  }||||fS )Nre   ç       @)r   iúÿÿÿrg   )r   rM   Zpolyval)r#   r%   rG   ZqrrH   rI   rJ   r(   r(   r)   rK   ™  s    zgeom_gen._stats)NN)rS   rT   rU   rV   r*   r-   r;   r8   r=   r>   r‚   rB   rK   r(   r(   r(   r)   rz   a  s   
rz   ÚgeomzA geometric)r/   rX   Úlongnamec                   @   sb   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„ Z
dd„ Zdd„ Zdd„ ZdS )Úhypergeom_genaì  A hypergeometric discrete random variable.

    The hypergeometric distribution models drawing objects from a bin.
    `M` is the total number of objects, `n` is total number of Type I objects.
    The random variate represents the number of Type I objects in `N` drawn
    without replacement from the total population.

    %(before_notes)s

    Notes
    -----
    The symbols used to denote the shape parameters (`M`, `n`, and `N`) are not
    universally accepted.  See the Examples for a clarification of the
    definitions used here.

    The probability mass function is defined as,

    .. math:: p(k, M, n, N) = \frac{\binom{n}{k} \binom{M - n}{N - k}}
                                   {\binom{M}{N}}

    for :math:`k \in [\max(0, N - M + n), \min(n, N)]`, where the binomial
    coefficients are defined as,

    .. math:: \binom{n}{k} \equiv \frac{n!}{k! (n - k)!}.

    %(after_notes)s

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

    Suppose we have a collection of 20 animals, of which 7 are dogs.  Then if
    we want to know the probability of finding a given number of dogs if we
    choose at random 12 of the 20 animals, we can initialize a frozen
    distribution and plot the probability mass function:

    >>> [M, n, N] = [20, 7, 12]
    >>> rv = hypergeom(M, n, N)
    >>> x = np.arange(0, n+1)
    >>> pmf_dogs = rv.pmf(x)

    >>> fig = plt.figure()
    >>> ax = fig.add_subplot(111)
    >>> ax.plot(x, pmf_dogs, 'bo')
    >>> ax.vlines(x, 0, pmf_dogs, lw=2)
    >>> ax.set_xlabel('# of dogs in our group of chosen animals')
    >>> ax.set_ylabel('hypergeom PMF')
    >>> plt.show()

    Instead of using a frozen distribution we can also use `hypergeom`
    methods directly.  To for example obtain the cumulative distribution
    function, use:

    >>> prb = hypergeom.cdf(x, M, n, N)

    And to generate random numbers:

    >>> R = hypergeom.rvs(M, n, N, size=10)

    See Also
    --------
    nhypergeom, binom, nbinom

    Nc                 C   s   |j ||| ||d�S r{   )Zhypergeometric)r#   ÚMr$   ÚNr&   r'   r(   r(   r)   r*   ç  s    zhypergeom_gen._rvsc                 C   s    t  |||  d¡t  ||¡fS rb   ©rM   ÚmaximumZminimum)r#   r‰   r$   rŠ   r(   r(   r)   r0   ê  s    zhypergeom_gen._get_supportc                 C   s0   |dk|dk@ |dk@ }|||k||k@ M }|S rb   r(   )r#   r‰   r$   rŠ   rw   r(   r(   r)   r-   í  s    zhypergeom_gen._argcheckc           	      C   sŠ   || }}|| }t |d dƒt |d dƒ t || d |d ƒ t |d || d ƒ t || d || | d ƒ t |d dƒ }|S r1   ©r   )	r#   r6   r‰   r$   rŠ   ÚtotÚgoodÚbadÚresultr(   r(   r)   r8   ò  s    
0ÿÿþzhypergeom_gen._logpmfc                 C   s   t |  ||||¡ƒS r    rd   )r#   r6   r‰   r$   rŠ   r(   r(   r)   r;   ú  s    zhypergeom_gen._pmfc           
      C   s0  d| d| d|   }}}|| }|| }|| }|| | ||  d || |d   }|| |d|   |d  t |d || | ||   ƒ }||d  d| ||   d| |  }	|	|d | | 9 }	|	d| | ||  | d| d  7 }	|	|| ||  | |d  |d   }	||||	fS )	Nre   r   rf   r…   g      @g      @rg   ç      @rj   )
r#   r‰   r$   rŠ   Úmr%   rG   rH   rI   rJ   r(   r(   r)   rK   ÿ  s    (<(((zhypergeom_gen._statsc                 C   sB   t j|||  t||ƒd … }|  ||||¡}t jt|ƒdd�S )Nr   r   rL   )rM   rN   ÚminZpmfrO   r   )r#   r‰   r$   rŠ   r6   rP   r(   r(   r)   rQ     s     zhypergeom_gen._entropyc                 C   s`   g }t t ||||¡Ž D ]<\}}}}	t |d |	d ¡}
| t |  |
|||	¡¡¡ qt |¡S r1   )ÚziprM   Úbroadcast_arraysÚarangeÚappendrO   r;   Úasarray©r#   r6   r‰   r$   rŠ   ÚresZquantrŽ   r�   ZdrawZk2r(   r(   r)   r>     s
     zhypergeom_gen._sfc                 C   s    g }t t ||||¡Ž D ]|\}}}}	|d |d  |d |	d  k rf| tt|  ||||	¡ƒ ƒ¡ qt |d |	d ¡}
| t|  	|
|||	¡ƒ¡ qt 
|¡S )Nrm   r   )r•   rM   r–   r˜   r   r   Zlogcdfr—   r   r8   r™   rš   r(   r(   r)   r‚   "  s      "zhypergeom_gen._logsfc                 C   sœ   g }t t ||||¡Ž D ]x\}}}}	|d |d  |d |	d  krf| tt|  ||||	¡ƒ ƒ¡ qt d|d ¡}
| t|  	|
|||	¡ƒ¡ qt 
|¡S )Nrm   r   r   )r•   rM   r–   r˜   r   r   Zlogsfr—   r   r8   r™   rš   r(   r(   r)   rx   .  s      "zhypergeom_gen._logcdf)NN)rS   rT   rU   rV   r*   r0   r-   r8   r;   rK   rQ   r>   r‚   rx   r(   r(   r(   r)   rˆ   ¥  s   A
rˆ   Ú	hypergeomc                   @   sB   e Zd ZdZdd„ Zdd„ Zddd„Zd	d
„ Zdd„ Zdd„ Z	dS )Únhypergeom_genaG  A negative hypergeometric discrete random variable.

    Consider a box containing :math:`M` balls:, :math:`n` red and
    :math:`M-n` blue. We randomly sample balls from the box, one
    at a time and *without* replacement, until we have picked :math:`r`
    blue balls. `nhypergeom` is the distribution of the number of
    red balls :math:`k` we have picked.

    %(before_notes)s

    Notes
    -----
    The symbols used to denote the shape parameters (`M`, `n`, and `r`) are not
    universally accepted. See the Examples for a clarification of the
    definitions used here.

    The probability mass function is defined as,

    .. math:: f(k; M, n, r) = \frac{{{k+r-1}\choose{k}}{{M-r-k}\choose{n-k}}}
                                   {{M \choose n}}

    for :math:`k \in [0, n]`, :math:`n \in [0, M]`, :math:`r \in [0, M-n]`,
    and the binomial coefficient is:

    .. math:: \binom{n}{k} \equiv \frac{n!}{k! (n - k)!}.

    It is equivalent to observing :math:`k` successes in :math:`k+r-1`
    samples with :math:`k+r`'th sample being a failure. The former
    can be modelled as a hypergeometric distribution. The probability
    of the latter is simply the number of failures remaining
    :math:`M-n-(r-1)` divided by the size of the remaining population
    :math:`M-(k+r-1)`. This relationship can be shown as:

    .. math:: NHG(k;M,n,r) = HG(k;M,n,k+r-1)\frac{(M-n-(r-1))}{(M-(k+r-1))}

    where :math:`NHG` is probability mass function (PMF) of the
    negative hypergeometric distribution and :math:`HG` is the
    PMF of the hypergeometric distribution.

    %(after_notes)s

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

    Suppose we have a collection of 20 animals, of which 7 are dogs.
    Then if we want to know the probability of finding a given number
    of dogs (successes) in a sample with exactly 12 animals that
    aren't dogs (failures), we can initialize a frozen distribution
    and plot the probability mass function:

    >>> M, n, r = [20, 7, 12]
    >>> rv = nhypergeom(M, n, r)
    >>> x = np.arange(0, n+2)
    >>> pmf_dogs = rv.pmf(x)

    >>> fig = plt.figure()
    >>> ax = fig.add_subplot(111)
    >>> ax.plot(x, pmf_dogs, 'bo')
    >>> ax.vlines(x, 0, pmf_dogs, lw=2)
    >>> ax.set_xlabel('# of dogs in our group with given 12 failures')
    >>> ax.set_ylabel('nhypergeom PMF')
    >>> plt.show()

    Instead of using a frozen distribution we can also use `nhypergeom`
    methods directly.  To for example obtain the probability mass
    function, use:

    >>> prb = nhypergeom.pmf(x, M, n, r)

    And to generate random numbers:

    >>> R = nhypergeom.rvs(M, n, r, size=10)

    To verify the relationship between `hypergeom` and `nhypergeom`, use:

    >>> from scipy.stats import hypergeom, nhypergeom
    >>> M, n, r = 45, 13, 8
    >>> k = 6
    >>> nhypergeom.pmf(k, M, n, r)
    0.06180776620271643
    >>> hypergeom.pmf(k, M, n, k+r-1) * (M - n - (r-1)) / (M - (k+r-1))
    0.06180776620271644

    See Also
    --------
    hypergeom, binom, nbinom

    References
    ----------
    .. [1] Negative Hypergeometric Distribution on Wikipedia
           https://en.wikipedia.org/wiki/Negative_hypergeometric_distribution

    .. [2] Negative Hypergeometric Distribution from
           http://www.math.wm.edu/~leemis/chart/UDR/PDFs/Negativehypergeometric.pdf

    c                 C   s   d|fS rb   r(   )r#   r‰   r$   Úrr(   r(   r)   r0   ¢  s    znhypergeom_gen._get_supportc                 C   s(   |dk||k@ |dk@ ||| k@ }|S rb   r(   )r#   r‰   r$   rž   rw   r(   r(   r)   r-   ¥  s    $znhypergeom_gen._argcheckNc                    s"   t ‡ fdd„ƒ}||||||d�S )Nc                    sl   ˆ   | ||¡\}}t ||d ¡}ˆ  || ||¡}t||ddd�}	|	|j|d�ƒ t¡}
|d u rh|
 ¡ S |
S )Nr   ÚnextZextrapolate)ÚkindZ
fill_valuer|   )	ZsupportrM   r—   rq   r   ÚuniformÚastypeÚintÚitem)r‰   r$   rž   r&   r'   r/   r]   Úksrq   ZppfÚrvs©r#   r(   r)   Ú_rvs1«  s    z"nhypergeom_gen._rvs.<locals>._rvs1rZ   ©Ú_vectorize_rvs_over_shapes)r#   r‰   r$   rž   r&   r'   r¨   r(   r§   r)   r*   ©  s    znhypergeom_gen._rvsc                 C   s2   |dk|dk@ }t | ||||fdd„ dd�}|S )Nr   c                 S   sv   t | d |ƒ t | | dƒ t ||  d || | d ƒ t || |  d dƒ t |d || d ƒ t |d dƒ S r1   r�   )r6   r‰   r$   rž   r(   r(   r)   Ú<lambda>¼  s    ÿÿþþz(nhypergeom_gen._logpmf.<locals>.<lambda>ç        )Ú	fillvalue)r	   )r#   r6   r‰   r$   rž   rw   r‘   r(   r(   r)   r8   ¹  s    ûznhypergeom_gen._logpmfc                 C   s   t |  ||||¡ƒS r    rd   )r#   r6   r‰   r$   rž   r(   r(   r)   r;   Ã  s    znhypergeom_gen._pmfc                 C   s€   d| d| d|   }}}|| || d  }||d  | || d || d   d||| d    }d\}}||||fS )Nre   r   rf   rD   r(   )r#   r‰   r$   rž   rG   rH   rI   rJ   r(   r(   r)   rK   È  s
    <znhypergeom_gen._stats)NN)
rS   rT   rU   rV   r0   r-   r*   r8   r;   rK   r(   r(   r(   r)   r�   >  s   c

r�   Ú
nhypergeomc                   @   s2   e Zd ZdZddd„Zdd„ Zdd„ Zd	d
„ ZdS )Ú
logser_genaÔ  A Logarithmic (Log-Series, Series) discrete random variable.

    %(before_notes)s

    Notes
    -----
    The probability mass function for `logser` is:

    .. math::

        f(k) = - \frac{p^k}{k \log(1-p)}

    for :math:`k \ge 1`, :math:`0 < p < 1`

    `logser` takes :math:`p` as shape parameter,
    where :math:`p` is the probability of a single success
    and :math:`1-p` is the probability of a single failure.

    %(after_notes)s

    %(example)s

    Nc                 C   s   |j ||d�S r{   )Z	logseriesr[   r(   r(   r)   r*   ò  s    zlogser_gen._rvsc                 C   s   |dk|dk @ S r+   r(   r\   r(   r(   r)   r-   ÷  s    zlogser_gen._argcheckc                 C   s"   t  ||¡ d | t | ¡ S ©Nre   )rM   r   r   r   r€   r(   r(   r)   r;   ú  s    zlogser_gen._pmfc                 C   s  t  | ¡}||d  | }| | |d d  }|||  }| | d|  d| d  }|d| |  d|d   }|t |d¡ }| | d|d d  d| |d d   d| | |d d    }	|	d| |  d| | |  d|d   }
|
|d  d }||||fS )	Nre   rf   rh   ç      ø?r   rg   é   r’   )r   r   rM   r   )r#   r%   rž   rG   Zmu2prH   Zmu3pZmu3rI   Zmu4pÚmu4rJ   r(   r(   r)   rK   þ  s    :ÿ,zlogser_gen._stats)NN)rS   rT   rU   rV   r*   r-   r;   rK   r(   r(   r(   r)   r¯   Ú  s
   
r¯   ÚlogserzA logarithmicc                   @   sR   e Zd ZdZ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S )Úpoisson_gena›  A Poisson discrete random variable.

    %(before_notes)s

    Notes
    -----
    The probability mass function for `poisson` is:

    .. math::

        f(k) = \exp(-\mu) \frac{\mu^k}{k!}

    for :math:`k \ge 0`.

    `poisson` takes :math:`\mu \geq 0` as shape parameter.
    When :math:`\mu = 0`, the ``pmf`` method
    returns ``1.0`` at quantile :math:`k = 0`.

    %(after_notes)s

    %(example)s

    c                 C   s   |dkS rb   r(   )r#   rG   r(   r(   r)   r-   +  s    zpoisson_gen._argcheckNc                 C   s   |  ||¡S r    ©Úpoisson)r#   rG   r&   r'   r(   r(   r)   r*   .  s    zpoisson_gen._rvsc                 C   s    t  ||¡t|d ƒ | }|S r1   )r   r3   r2   )r#   r6   rG   ÚPkr(   r(   r)   r8   1  s    zpoisson_gen._logpmfc                 C   s   t |  ||¡ƒS r    rd   )r#   r6   rG   r(   r(   r)   r;   5  s    zpoisson_gen._pmfc                 C   s   t |ƒ}t ||¡S r    )r   r   Úpdtr©r#   r5   rG   r6   r(   r(   r)   r=   9  s    zpoisson_gen._cdfc                 C   s   t |ƒ}t ||¡S r    )r   r   Zpdtrcrº   r(   r(   r)   r>   =  s    zpoisson_gen._sfc                 C   s>   t t ||¡ƒ}t |d d¡}t ||¡}t ||k||¡S r~   )r   r   ZpdtrikrM   rŒ   r¹   rƒ   )r#   rA   rG   rP   Úvals1r„   r(   r(   r)   rB   A  s    zpoisson_gen._ppfc                 C   sN   |}t  |¡}|dk}t||fdd„ t jƒ}t||fdd„ t jƒ}||||fS )Nr   c                 S   s   t d|  ƒS r°   rj   ©r5   r(   r(   r)   r«   K  ó    z$poisson_gen._stats.<locals>.<lambda>c                 S   s   d|  S r°   r(   r¼   r(   r(   r)   r«   L  r½   )rM   r™   r	   Úinf)r#   rG   rH   ÚtmpZ
mu_nonzerorI   rJ   r(   r(   r)   rK   G  s    
zpoisson_gen._stats)NN)rS   rT   rU   rV   r-   r*   r8   r;   r=   r>   rB   rK   r(   r(   r(   r)   rµ     s   
rµ   r·   z	A Poisson)rX   r‡   c                   @   sZ   e Zd Z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S )Ú
planck_gena  A Planck discrete exponential random variable.

    %(before_notes)s

    Notes
    -----
    The probability mass function for `planck` is:

    .. math::

        f(k) = (1-\exp(-\lambda)) \exp(-\lambda k)

    for :math:`k \ge 0` and :math:`\lambda > 0`.

    `planck` takes :math:`\lambda` as shape parameter. The Planck distribution
    can be written as a geometric distribution (`geom`) with
    :math:`p = 1 - \exp(-\lambda)` shifted by ``loc = -1``.

    %(after_notes)s

    See Also
    --------
    geom

    %(example)s

    c                 C   s   |dkS rb   r(   )r#   Úlambda_r(   r(   r)   r-   o  s    zplanck_gen._argcheckc                 C   s   t | ƒ t| | ƒ S r    )r   r   )r#   r6   rÁ   r(   r(   r)   r;   r  s    zplanck_gen._pmfc                 C   s   t |ƒ}t| |d  ƒ S r1   )r   r   ©r#   r5   rÁ   r6   r(   r(   r)   r=   u  s    zplanck_gen._cdfc                 C   s   t |  ||¡ƒS r    )r   r‚   )r#   r5   rÁ   r(   r(   r)   r>   y  s    zplanck_gen._sfc                 C   s   t |ƒ}| |d  S r1   ©r   rÂ   r(   r(   r)   r‚   |  s    zplanck_gen._logsfc                 C   sL   t d| t| ƒ d ƒ}|d j|  |¡Ž }|  ||¡}t ||k||¡S )Nç      ð¿r   )r   r   Úclipr0   r=   rM   rƒ   )r#   rA   rÁ   rP   r»   r„   r(   r(   r)   rB   €  s    zplanck_gen._ppfNc                 C   s   t | ƒ }|j||d�d S )Nr|   re   )r   r}   )r#   rÁ   r&   r'   r%   r(   r(   r)   r*   †  s    zplanck_gen._rvsc                 C   sP   dt |ƒ }t| ƒt | ƒd  }dt|d ƒ }ddt|ƒ  }||||fS )Nr   rf   r…   r²   )r   r   r   )r#   rÁ   rG   rH   rI   rJ   r(   r(   r)   rK   ‹  s
    zplanck_gen._statsc                 C   s&   t | ƒ }|t| ƒ | t|ƒ S r    )r   r   r   )r#   rÁ   ÚCr(   r(   r)   rQ   ’  s    zplanck_gen._entropy)NN)rS   rT   rU   rV   r-   r;   r=   r>   r‚   rB   r*   rK   rQ   r(   r(   r(   r)   rÀ   S  s   
rÀ   ÚplanckzA discrete exponential c                   @   s@   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ Zdd„ Z	dS )Úboltzmann_gena—  A Boltzmann (Truncated Discrete Exponential) random variable.

    %(before_notes)s

    Notes
    -----
    The probability mass function for `boltzmann` is:

    .. math::

        f(k) = (1-\exp(-\lambda)) \exp(-\lambda k) / (1-\exp(-\lambda N))

    for :math:`k = 0,..., N-1`.

    `boltzmann` takes :math:`\lambda > 0` and :math:`N > 0` as shape parameters.

    %(after_notes)s

    %(example)s

    c                 C   s   |dk|dk@ S rb   r(   ©r#   rÁ   rŠ   r(   r(   r)   r-   °  s    zboltzmann_gen._argcheckc                 C   s   | j |d fS r1   r.   rÉ   r(   r(   r)   r0   ³  s    zboltzmann_gen._get_supportc                 C   s2   dt | ƒ dt | | ƒ  }|t | | ƒ S r1   ©r   )r#   r6   rÁ   rŠ   Zfactr(   r(   r)   r;   ¶  s     zboltzmann_gen._pmfc                 C   s0   t |ƒ}dt| |d  ƒ dt| | ƒ  S r1   )r   r   )r#   r5   rÁ   rŠ   r6   r(   r(   r)   r=   ¼  s    zboltzmann_gen._cdfc                 C   sd   |dt | | ƒ  }td| td| ƒ d ƒ}|d  dtj¡}|  |||¡}t ||k||¡S )Nr   rÄ   r¬   )r   r   r   rÅ   rM   r¾   r=   rƒ   )r#   rA   rÁ   rŠ   ZqnewrP   r»   r„   r(   r(   r)   rB   À  s
    zboltzmann_gen._ppfc                 C   s  t | ƒ}t | | ƒ}|d|  || d|   }|d| d  || | d| d   }d| d|  }||d  || |  }|d|  |d  |d | d|   }	|	|d  }	|dd|  ||   |d  |d | dd|  ||    }
|
| | }
|||	|
fS )Nre   r   rf   rh   r±   r²   rÊ   )r#   rÁ   rŠ   ÚzZzNrG   rH   ZtrmZtrm2rI   rJ   r(   r(   r)   rK   Ç  s    
((@zboltzmann_gen._statsN)
rS   rT   rU   rV   r-   r0   r;   r=   rB   rK   r(   r(   r(   r)   rÈ   š  s   rÈ   Ú	boltzmannz!A truncated discrete exponential )rX   r/   r‡   c                   @   sR   e Zd Z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S )Úrandint_gena”  A uniform discrete random variable.

    %(before_notes)s

    Notes
    -----
    The probability mass function for `randint` is:

    .. math::

        f(k) = \frac{1}{\texttt{high} - \texttt{low}}

    for :math:`k \in \{\texttt{low}, \dots, \texttt{high} - 1\}`.

    `randint` takes :math:`\texttt{low}` and :math:`\texttt{high}` as shape
    parameters.

    %(after_notes)s

    %(example)s

    c                 C   s   ||kS r    r(   ©r#   ÚlowÚhighr(   r(   r)   r-   ð  s    zrandint_gen._argcheckc                 C   s   ||d fS r1   r(   rÎ   r(   r(   r)   r0   ó  s    zrandint_gen._get_supportc                 C   s,   t  |¡||  }t  ||k||k @ |d¡S )Nr¬   )rM   Z	ones_likerƒ   )r#   r6   rÏ   rÐ   r%   r(   r(   r)   r;   ö  s    zrandint_gen._pmfc                 C   s   t |ƒ}|| d ||  S r°   rÃ   )r#   r5   rÏ   rÐ   r6   r(   r(   r)   r=   û  s    zrandint_gen._cdfc                 C   sH   t |||  | ƒd }|d  ||¡}|  |||¡}t ||k||¡S r1   )r   rÅ   r=   rM   rƒ   )r#   rA   rÏ   rÐ   rP   r»   r„   r(   r(   r)   rB   ÿ  s    zrandint_gen._ppfc           
      C   sj   t  |¡t  |¡ }}|| d d }|| }|| d d }d}d|| d  || d  }	||||	fS )Nre   rf   r   g      (@r¬   g333333ó¿)rM   r™   )
r#   rÏ   rÐ   Úm2Úm1rG   ÚdrH   rI   rJ   r(   r(   r)   rK     s    zrandint_gen._statsNc                 C   sr   t  |¡jdkr0t  |¡jdkr0t||||d�S |durPt  ||¡}t  ||¡}t jtt|ƒt jgd�}|||ƒS )z=An array of *size* random integers >= ``low`` and < ``high``.r   r|   N)Zotypes)rM   r™   r&   r
   Zbroadcast_toÚ	vectorizer   Úint_)r#   rÏ   rÐ   r&   r'   Úrandintr(   r(   r)   r*     s     ÿzrandint_gen._rvsc                 C   s   t || ƒS r    )r   rÎ   r(   r(   r)   rQ     s    zrandint_gen._entropy)NN)rS   rT   rU   rV   r-   r0   r;   r=   rB   rK   r*   rQ   r(   r(   r(   r)   rÍ   Ù  s   	
rÍ   rÖ   z#A discrete uniform (random integer)c                   @   s2   e Zd ZdZddd„Zdd„ Zdd„ Zd	d
„ ZdS )Úzipf_genaŒ  A Zipf (Zeta) discrete random variable.

    %(before_notes)s

    See Also
    --------
    zipfian

    Notes
    -----
    The probability mass function for `zipf` is:

    .. math::

        f(k, a) = \frac{1}{\zeta(a) k^a}

    for :math:`k \ge 1`, :math:`a > 1`.

    `zipf` takes :math:`a > 1` as shape parameter. :math:`\zeta` is the
    Riemann zeta function (`scipy.special.zeta`)

    The Zipf distribution is also known as the zeta distribution, which is
    a special case of the Zipfian distribution (`zipfian`).

    %(after_notes)s

    References
    ----------
    .. [1] "Zeta Distribution", Wikipedia,
           https://en.wikipedia.org/wiki/Zeta_distribution

    %(example)s

    Confirm that `zipf` is the large `n` limit of `zipfian`.

    >>> from scipy.stats import zipfian
    >>> k = np.arange(11)
    >>> np.allclose(zipf.pmf(k, a), zipfian.pmf(k, a, n=10000000))
    True

    Nc                 C   s   |j ||d�S r{   )Úzipf)r#   r/   r&   r'   r(   r(   r)   r*   R  s    zzipf_gen._rvsc                 C   s   |dkS r1   r(   ©r#   r/   r(   r(   r)   r-   U  s    zzipf_gen._argcheckc                 C   s   dt  |d¡ ||  }|S ©Nre   r   ©r   r   )r#   r6   r/   r¸   r(   r(   r)   r;   X  s    zzipf_gen._pmfc                 C   s    t ||d k||fdd„ tjƒS )Nr   c                 S   s   t  | | d¡t  | d¡ S r1   rÛ   )r/   r$   r(   r(   r)   r«   `  r½   z zipf_gen._munp.<locals>.<lambda>)r	   rM   r¾   )r#   r$   r/   r(   r(   r)   Ú_munp]  s
    ýzzipf_gen._munp)NN)rS   rT   rU   rV   r*   r-   r;   rÜ   r(   r(   r(   r)   r×   (  s
   )
r×   rØ   zA Zipfc                 C   s   t |dƒt || d ƒ S )z"Generalized harmonic number, a > 1r   )r   ©r$   r/   r(   r(   r)   Ú_gen_harmonic_gt1g  s    rÞ   c                 C   sf   t  | ¡s| S t  | ¡}t j|td�}t j|ddtd�D ](}|| k}||  d|||   7  < q8|S )z#Generalized harmonic number, a <= 1©Zdtyper   éÿÿÿÿr   )rM   r&   ÚmaxZ
zeros_likeÚfloatr—   )r$   r/   Zn_maxÚoutÚiÚmaskr(   r(   r)   Ú_gen_harmonic_leq1m  s    

ræ   c                 C   s(   t  | |¡\} }t|dk| |fttd�S )zGeneralized harmonic numberr   ru   )rM   r–   r	   rÞ   ræ   rÝ   r(   r(   r)   Ú_gen_harmonicz  s    ÿrç   c                   @   s@   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ Zdd„ Z	dS )Úzipfian_gena`  A Zipfian discrete random variable.

    %(before_notes)s

    See Also
    --------
    zipf

    Notes
    -----
    The probability mass function for `zipfian` is:

    .. math::

        f(k, a, n) = \frac{1}{H_{n,a} k^a}

    for :math:`k \in \{1, 2, \dots, n-1, n\}`, :math:`a \ge 0`,
    :math:`n \in \{1, 2, 3, \dots\}`.

    `zipfian` takes :math:`a` and :math:`n` as shape parameters.
    :math:`H_{n,a}` is the :math:`n`:sup:`th` generalized harmonic
    number of order :math:`a`.

    The Zipfian distribution reduces to the Zipf (zeta) distribution as
    :math:`n \rightarrow \infty`.

    %(after_notes)s

    References
    ----------
    .. [1] "Zipf's Law", Wikipedia, https://en.wikipedia.org/wiki/Zipf's_law
    .. [2] Larry Leemis, "Zipf Distribution", Univariate Distribution
           Relationships. http://www.math.wm.edu/~leemis/chart/UDR/PDFs/Zipf.pdf

    %(example)s

    Confirm that `zipfian` reduces to `zipf` for large `n`, `a > 1`.

    >>> from scipy.stats import zipf
    >>> k = np.arange(11)
    >>> np.allclose(zipfian.pmf(k, a=3.5, n=10000000), zipf.pmf(k, a=3.5))
    True

    c                 C   s"   |dk|dk@ |t j|td�k@ S )Nr   rß   )rM   r™   r£   ©r#   r/   r$   r(   r(   r)   r-   ®  s    zzipfian_gen._argcheckc                 C   s   d|fS r1   r(   ré   r(   r(   r)   r0   ²  s    zzipfian_gen._get_supportc                 C   s   dt ||ƒ ||  S r°   ©rç   ©r#   r6   r/   r$   r(   r(   r)   r;   µ  s    zzipfian_gen._pmfc                 C   s   t ||ƒt ||ƒ S r    rê   rë   r(   r(   r)   r=   ¸  s    zzipfian_gen._cdfc                 C   s:   |d }|| t ||ƒt ||ƒ  d || t ||ƒ  S r1   rê   rë   r(   r(   r)   r>   »  s    ÿzzipfian_gen._sfc                 C   sþ   t ||ƒ}t ||d ƒ}t ||d ƒ}t ||d ƒ}t ||d ƒ}|| }|| |d  }	|d }
|	|
 }|| d| | |d   d|d  |d   |d  }|d | d|d  | |  d| |d  |  d|d   |	d  }|d8 }||||fS )Nr   rf   rh   r²   r±   rg   rê   )r#   r/   r$   ZHnaZHna1ZHna2ZHna3ZHna4Úmu1Zmu2nZmu2dÚmu2rI   rJ   r(   r(   r)   rK   Á  s"    
82
ÿÿzzipfian_gen._statsN)
rS   rT   rU   rV   r-   r0   r;   r=   r>   rK   r(   r(   r(   r)   rè   �  s   ,rè   Úzipfianz	A Zipfianc                   @   sB   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ Zddd„Z	dS )Údlaplace_genaL  A  Laplacian discrete random variable.

    %(before_notes)s

    Notes
    -----
    The probability mass function for `dlaplace` is:

    .. math::

        f(k) = \tanh(a/2) \exp(-a |k|)

    for integers :math:`k` and :math:`a > 0`.

    `dlaplace` takes :math:`a` as shape parameter.

    %(after_notes)s

    %(example)s

    c                 C   s   t |d ƒt| t|ƒ ƒ S ©Nr…   )r   r   Úabs)r#   r6   r/   r(   r(   r)   r;   ë  s    zdlaplace_gen._pmfc                 C   s0   t |ƒ}dd„ }dd„ }t|dk||f||d�S )Nc                 S   s   dt | |  ƒt |ƒd   S rÚ   rÊ   ©r6   r/   r(   r(   r)   r«   ñ  r½   z#dlaplace_gen._cdf.<locals>.<lambda>c                 S   s   t || d  ƒt |ƒd  S r1   rÊ   rò   r(   r(   r)   r«   ò  r½   r   ru   )r   r	   )r#   r5   r/   r6   rv   rr   r(   r(   r)   r=   ï  s    zdlaplace_gen._cdfc                 C   st   dt |ƒ }tt |ddt | ƒ  k t|| ƒ| d td| | ƒ | ¡ƒ}|d }t |  ||¡|k||¡S )Nr   re   )r   r   rM   rƒ   r   r=   )r#   rA   r/   ÚconstrP   r»   r(   r(   r)   rB   õ  s    þzdlaplace_gen._ppfc                 C   s\   t |ƒ}d| |d d  }d| |d d|  d  |d d  }d|d||d  d fS )Nr…   re   rf   g      $@r²   r¬   r’   rÊ   )r#   r/   Zearí   r³   r(   r(   r)   rK   ý  s    (zdlaplace_gen._statsc                 C   s   |t |ƒ tt|d ƒƒ S rð   )r   r   r   rÙ   r(   r(   r)   rQ     s    zdlaplace_gen._entropyNc                 C   s8   t  t  |¡ ¡ }|j||d�}|j||d�}|| S r{   )rM   r   r™   r}   )r#   r/   r&   r'   ZprobOfSuccessr5   Úyr(   r(   r)   r*     s    zdlaplace_gen._rvs)NN)
rS   rT   rU   rV   r;   r=   rB   rK   rQ   r*   r(   r(   r(   r)   rï   Õ  s   rï   ÚdlaplacezA discrete Laplacianc                   @   s2   e Zd ZdZddd„Zdd„ Zdd„ Zd	d
„ ZdS )Úskellam_genaÍ  A  Skellam discrete random variable.

    %(before_notes)s

    Notes
    -----
    Probability distribution of the difference of two correlated or
    uncorrelated Poisson random variables.

    Let :math:`k_1` and :math:`k_2` be two Poisson-distributed r.v. with
    expected values :math:`\lambda_1` and :math:`\lambda_2`. Then,
    :math:`k_1 - k_2` follows a Skellam distribution with parameters
    :math:`\mu_1 = \lambda_1 - \rho \sqrt{\lambda_1 \lambda_2}` and
    :math:`\mu_2 = \lambda_2 - \rho \sqrt{\lambda_1 \lambda_2}`, where
    :math:`\rho` is the correlation coefficient between :math:`k_1` and
    :math:`k_2`. If the two Poisson-distributed r.v. are independent then
    :math:`\rho = 0`.

    Parameters :math:`\mu_1` and :math:`\mu_2` must be strictly positive.

    For details see: https://en.wikipedia.org/wiki/Skellam_distribution

    `skellam` takes :math:`\mu_1` and :math:`\mu_2` as shape parameters.

    %(after_notes)s

    %(example)s

    Nc                 C   s   |}|  ||¡|  ||¡ S r    r¶   )r#   rì   rí   r&   r'   r$   r(   r(   r)   r*   >  s    

ÿzskellam_gen._rvsc              	   C   sN   t  |dk td| dd|  d| ƒd td| dd|  d| ƒd ¡}|S )Nr   rf   r   )rM   rƒ   r   ©r#   r5   rì   rí   Zpxr(   r(   r)   r;   C  s
    
þzskellam_gen._pmfc              
   C   sN   t |ƒ}t |dk td| d| d| ƒdtd| d|d  d| ƒ ¡}|S )Nr   rf   éþÿÿÿr   )r   rM   rƒ   r   r÷   r(   r(   r)   r=   J  s    
þzskellam_gen._cdfc                 C   s4   || }|| }|t |d ƒ }d| }||||fS )Nrh   r   rj   )r#   rì   rí   ÚmeanrH   rI   rJ   r(   r(   r)   rK   Q  s
    zskellam_gen._stats)NN)rS   rT   rU   rV   r*   r;   r=   rK   r(   r(   r(   r)   rö      s
   
rö   Úskellamz	A Skellamc                   @   sR   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„ Z
dd„ ZdS )Úyulesimon_genaî  A Yule-Simon discrete random variable.

    %(before_notes)s

    Notes
    -----

    The probability mass function for the `yulesimon` is:

    .. math::

        f(k) =  \alpha B(k, \alpha+1)

    for :math:`k=1,2,3,...`, where :math:`\alpha>0`.
    Here :math:`B` refers to the `scipy.special.beta` function.

    The sampling of random variates is based on pg 553, Section 6.3 of [1]_.
    Our notation maps to the referenced logic via :math:`\alpha=a-1`.

    For details see the wikipedia entry [2]_.

    References
    ----------
    .. [1] Devroye, Luc. "Non-uniform Random Variate Generation",
         (1986) Springer, New York.

    .. [2] https://en.wikipedia.org/wiki/Yule-Simon_distribution

    %(after_notes)s

    %(example)s

    Nc                 C   s6   |  |¡}|  |¡}t| tt| | ƒ ƒ ƒ}|S r    )Zstandard_exponentialr   r   r   )r#   Úalphar&   r'   ZE1ZE2Zansr(   r(   r)   r*   ~  s    

zyulesimon_gen._rvsc                 C   s   |t  ||d ¡ S r1   ©r   ra   ©r#   r5   rü   r(   r(   r)   r;   „  s    zyulesimon_gen._pmfc                 C   s   |dkS rb   r(   )r#   rü   r(   r(   r)   r-   ‡  s    zyulesimon_gen._argcheckc                 C   s   t |ƒt ||d ¡ S r1   ©r   r   r   rþ   r(   r(   r)   r8   Š  s    zyulesimon_gen._logpmfc                 C   s   d|t  ||d ¡  S r1   rý   rþ   r(   r(   r)   r=   �  s    zyulesimon_gen._cdfc                 C   s   |t  ||d ¡ S r1   rý   rþ   r(   r(   r)   r>   �  s    zyulesimon_gen._sfc                 C   s   t |ƒt ||d ¡ S r1   rÿ   rþ   r(   r(   r)   r‚   “  s    zyulesimon_gen._logsfc                 C   s  t  |dkt j||d  ¡}t  |dk|d |d |d d   t j¡}t  |dkt j|¡}t  |dkt|d ƒ|d d  ||d   t j¡}t  |dkt j|¡}t  |dk|d |d d|  d ||d  |d    t j¡}t  |dkt j|¡}||||fS )Nr   rf   r…   rh   r²   é1   é   )rM   rƒ   r¾   Únanr   )r#   rü   rG   rí   rI   rJ   r(   r(   r)   rK   –  s*    
þ
"þ
ÿÿþzyulesimon_gen._stats)NN)rS   rT   rU   rV   r*   r;   r-   r8   r=   r>   r‚   rK   r(   r(   r(   r)   rû   \  s   !
rû   Ú	yulesimon)rX   r/   c                    s   ‡ fdd„}|S )z?Decorator that vectorizes _rvs method to work on ndarray shapesc                    sÚ   t |d j| ƒ\}}t | ¡} t |¡}t |¡}t |¡rRˆg |¢| ‘|‘R Ž S t | ¡}t |j¡}t ||  || f¡}t 	|||¡}tj
| |  Ž D ],‰ ˆg ‡ fdd„|D ƒ¢|‘|‘R Ž |ˆ < qžt 	|||¡S )Nr   c                    s   g | ]}t  |¡ˆ  ‘qS r(   )rM   Zsqueeze)Ú.0Úarg©rä   r(   r)   Ú
<listcomp>É  r½   z<_vectorize_rvs_over_shapes.<locals>._rvs.<locals>.<listcomp>)r   ÚshaperM   ÚarrayÚallÚemptyr—   ÚndimZhstackZmoveaxisZndindex)r&   r'   ÚargsZ
_rvs1_sizeZ_rvs1_indicesrã   Zj0Zj1©r¨   r  r)   r*   ³  s"    




ÿÿz(_vectorize_rvs_over_shapes.<locals>._rvsr(   )r¨   r*   r(   r  r)   rª   ª  s    	rª   c                   @   sB   e Zd ZdZdZdZdd„ Zdd„ Zddd„Zd	d
„ Z	dd„ Z
dS )Ú_nchypergeom_genz‰A noncentral hypergeometric discrete random variable.

    For subclassing by nchypergeom_fisher_gen and nchypergeom_wallenius_gen.

    Nc           	      C   s<   |||  }}}|| }t  d|| ¡}t  ||¡}||fS rb   r‹   )	r#   r‰   r$   rŠ   ÚoddsrÒ   rÑ   Zx_minZx_maxr(   r(   r)   r0   Ú  s
    z_nchypergeom_gen._get_supportc                 C   sž   t  |¡t  |¡ }}t  |¡t  |¡ }}| t¡|k|dk@ }| t¡|k|dk@ }| t¡|k|dk@ }|dk}||k}	||k}
||@ |@ |@ |	@ |
@ S rb   )rM   r™   r¢   r£   )r#   r‰   r$   rŠ   r  Zcond1Zcond2Zcond3Zcond4Zcond5Zcond6r(   r(   r)   r-   á  s    z_nchypergeom_gen._argcheckc                    s$   t ‡ fdd„ƒ}|||||||d�S )Nc           
         s<   t  |¡}tƒ }t|ˆ jƒ}|||| |||ƒ}	|	 |¡}	|	S r    )rM   Úprodr   ÚgetattrÚrvs_nameZreshape)
r‰   r$   rŠ   r  r&   r'   ÚlengthÚurnZrv_genr¦   r§   r(   r)   r¨   î  s    

z$_nchypergeom_gen._rvs.<locals>._rvs1rZ   r©   )r#   r‰   r$   rŠ   r  r&   r'   r¨   r(   r§   r)   r*   ì  s    z_nchypergeom_gen._rvsc                    s"   t j‡ fdd„ƒ}||||||ƒS )Nc                    s   ˆ   ||||d¡}| | ¡S ©Ngê-�™—q=)ÚdistZprobability)r5   r‰   r$   rŠ   r  r  r§   r(   r)   Ú_pmf1û  s    z$_nchypergeom_gen._pmf.<locals>._pmf1©rM   rÔ   )r#   r5   r‰   r$   rŠ   r  r  r(   r§   r)   r;   ù  s    z_nchypergeom_gen._pmfc                    sL   t j‡ fdd„ƒ}d|v s"d|v r0|||||ƒnd \}}d\}	}
|||	|
fS )Nc                    s   ˆ   ||| |d¡}| ¡ S r  )r  rF   )r‰   r$   rŠ   r  r  r§   r(   r)   Ú	_moments1  s    z*_nchypergeom_gen._stats.<locals>._moments1r“   ÚvrD   r  )r#   r‰   r$   rŠ   r  rF   r  r“   r  rE   r6   r(   r§   r)   rK     s    ÿÿz_nchypergeom_gen._stats)NN)rS   rT   rU   rV   r  r  r0   r-   r*   r;   rK   r(   r(   r(   r)   r  Ð  s   
	r  c                   @   s   e Zd ZdZdZeZdS )Únchypergeom_fisher_genag	  A Fisher's noncentral hypergeometric discrete random variable.

    Fisher's noncentral hypergeometric distribution models drawing objects of
    two types from a bin. `M` is the total number of objects, `n` is the
    number of Type I objects, and `odds` is the odds ratio: the odds of
    selecting a Type I object rather than a Type II object when there is only
    one object of each type.
    The random variate represents the number of Type I objects drawn if we
    take a handful of objects from the bin at once and find out afterwards
    that we took `N` objects.

    %(before_notes)s

    See Also
    --------
    nchypergeom_wallenius, hypergeom, nhypergeom

    Notes
    -----
    Let mathematical symbols :math:`N`, :math:`n`, and :math:`M` correspond
    with parameters `N`, `n`, and `M` (respectively) as defined above.

    The probability mass function is defined as

    .. math::

        p(x; M, n, N, \omega) =
        \frac{\binom{n}{x}\binom{M - n}{N-x}\omega^x}{P_0},

    for
    :math:`x \in [x_l, x_u]`,
    :math:`M \in {\mathbb N}`,
    :math:`n \in [0, M]`,
    :math:`N \in [0, M]`,
    :math:`\omega > 0`,
    where
    :math:`x_l = \max(0, N - (M - n))`,
    :math:`x_u = \min(N, n)`,

    .. math::

        P_0 = \sum_{y=x_l}^{x_u} \binom{n}{y}\binom{M - n}{N-y}\omega^y,

    and the binomial coefficients are defined as

    .. math:: \binom{n}{k} \equiv \frac{n!}{k! (n - k)!}.

    `nchypergeom_fisher` uses the BiasedUrn package by Agner Fog with
    permission for it to be distributed under SciPy's license.

    The symbols used to denote the shape parameters (`N`, `n`, and `M`) are not
    universally accepted; they are chosen for consistency with `hypergeom`.

    Note that Fisher's noncentral hypergeometric distribution is distinct
    from Wallenius' noncentral hypergeometric distribution, which models
    drawing a pre-determined `N` objects from a bin one by one.
    When the odds ratio is unity, however, both distributions reduce to the
    ordinary hypergeometric distribution.

    %(after_notes)s

    References
    ----------
    .. [1] Agner Fog, "Biased Urn Theory".
           https://cran.r-project.org/web/packages/BiasedUrn/vignettes/UrnTheory.pdf

    .. [2] "Fisher's noncentral hypergeometric distribution", Wikipedia,
           https://en.wikipedia.org/wiki/Fisher's_noncentral_hypergeometric_distribution

    %(example)s

    Z
rvs_fisherN)rS   rT   rU   rV   r  r   r  r(   r(   r(   r)   r    s   Ir  Únchypergeom_fisherz$A Fisher's noncentral hypergeometricc                   @   s   e Zd ZdZdZeZdS )Únchypergeom_wallenius_gena}	  A Wallenius' noncentral hypergeometric discrete random variable.

    Wallenius' noncentral hypergeometric distribution models drawing objects of
    two types from a bin. `M` is the total number of objects, `n` is the
    number of Type I objects, and `odds` is the odds ratio: the odds of
    selecting a Type I object rather than a Type II object when there is only
    one object of each type.
    The random variate represents the number of Type I objects drawn if we
    draw a pre-determined `N` objects from a bin one by one.

    %(before_notes)s

    See Also
    --------
    nchypergeom_fisher, hypergeom, nhypergeom

    Notes
    -----
    Let mathematical symbols :math:`N`, :math:`n`, and :math:`M` correspond
    with parameters `N`, `n`, and `M` (respectively) as defined above.

    The probability mass function is defined as

    .. math::

        p(x; N, n, M) = \binom{n}{x} \binom{M - n}{N-x}
        \int_0^1 \left(1-t^{\omega/D}\right)^x\left(1-t^{1/D}\right)^{N-x} dt

    for
    :math:`x \in [x_l, x_u]`,
    :math:`M \in {\mathbb N}`,
    :math:`n \in [0, M]`,
    :math:`N \in [0, M]`,
    :math:`\omega > 0`,
    where
    :math:`x_l = \max(0, N - (M - n))`,
    :math:`x_u = \min(N, n)`,

    .. math::

        D = \omega(n - x) + ((M - n)-(N-x)),

    and the binomial coefficients are defined as

    .. math:: \binom{n}{k} \equiv \frac{n!}{k! (n - k)!}.

    `nchypergeom_wallenius` uses the BiasedUrn package by Agner Fog with
    permission for it to be distributed under SciPy's license.

    The symbols used to denote the shape parameters (`N`, `n`, and `M`) are not
    universally accepted; they are chosen for consistency with `hypergeom`.

    Note that Wallenius' noncentral hypergeometric distribution is distinct
    from Fisher's noncentral hypergeometric distribution, which models
    take a handful of objects from the bin at once, finding out afterwards
    that `N` objects were taken.
    When the odds ratio is unity, however, both distributions reduce to the
    ordinary hypergeometric distribution.

    %(after_notes)s

    References
    ----------
    .. [1] Agner Fog, "Biased Urn Theory".
           https://cran.r-project.org/web/packages/BiasedUrn/vignettes/UrnTheory.pdf

    .. [2] "Wallenius' noncentral hypergeometric distribution", Wikipedia,
           https://en.wikipedia.org/wiki/Wallenius'_noncentral_hypergeometric_distribution

    %(example)s

    Zrvs_walleniusN)rS   rT   rU   rV   r  r   r  r(   r(   r(   r)   r  b  s   Ir  Únchypergeom_walleniusz&A Wallenius' noncentral hypergeometric)\Ú	functoolsr   Zscipyr   Zscipy.specialr   r   r   r   r2   r   Zscipy._lib._utilr	   r
   Zscipy.interpolater   Únumpyr   r   r   r   r   r   r   r   r   r   rM   Z_distn_infrastructurer   r   r   r   r   Zscipy.stats._boostÚstatsr9   Z	biasedurnr   r   r   r   rW   rY   r_   r`   rk   rl   ry   rz   r†   rˆ   rœ   r�   r®   r¯   r´   rµ   r·   rÀ   rÇ   rÈ   rÌ   rÍ   rÖ   r×   rØ   rÞ   ræ   rç   rè   rî   rï   r¾   rõ   rö   rú   rû   r  rª   r  r  r  r  r  ÚlistÚglobalsÚitemsÚpairsZ_distn_namesZ_distn_gen_namesÚ__all__r(   r(   r(   r)   Ú<module>   s†   0L
>N
f
A 
 
4?D;ÿJ<QGÿ9K&?NþNþ