o
    Eb                     @   sP  d dl mZ d dlZd dlmZ d dlmZmZmZm	Z
mZ d dlmZ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Z0e0ddZ1G dd deZ2e2ddddZ3G dd deZ4e4d dZ5G d!d" d"eZ6e6d#dZ7G d$d% d%eZ8e8dd&d'dZ9G d(d) d)eZ:e:d*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,ZAG d:d; d;eZBeBdd<d=dZCd>d? ZDd@dA ZEdBdC ZFG dDdE dEeZGeGddFdGdZHG dHdI dIeZIeIejJ dJdKdZKG dLdM dMeZLeLejJ dNdOdZMG dPdQ dQeZNeNdRddSZOdTdU ZPG dVdW dWeZQG dXdY dYeQZReRdZd[d,ZSG d\d] d]eQZTeTd^d_d,ZUeVeW X Y ZZe!eZe\Z[Z\e[e\ Z]dS )`    )partialN)special)entr	logsumexpbetalngammalnzeta)
_lazywhererng_integers)interp1d)
floorceillogexpsqrtlog1pexpm1tanhcoshsinh   )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 N)binomialselfnpsizerandom_state r)   >/usr/lib/python3/dist-packages/scipy/stats/_discrete_distns.py_rvs5      zbinom_gen._rvsc                 C   s   |dk|dk@ |dk@ S Nr   r   r)   r$   r%   r&   r)   r)   r*   	_argcheck8      zbinom_gen._argcheckc                 C   s
   | j |fS r!   ar.   r)   r)   r*   _get_support;      
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      t |||S r!   )_boostZ
_binom_pdfr$   r9   r%   r&   r)   r)   r*   _pmfC      zbinom_gen._pmfc                 C      t |}t|||S r!   )r   r>   Z
_binom_cdfr$   r9   r%   r&   r:   r)   r)   r*   _cdfG      zbinom_gen._cdfc                 C   rB   r!   )r   r>   Z	_binom_sfrC   r)   r)   r*   _sfK   rE   zbinom_gen._sfc                 C   r=   r!   )r>   Z
_binom_isfr?   r)   r)   r*   _isfO   r,   zbinom_gen._isfc                 C   r=   r!   )r>   Z
_binom_ppf)r$   qr%   r&   r)   r)   r*   _ppfR   r,   zbinom_gen._ppfmvc                 C   sT   t ||}t ||}d\}}d|v rt ||}d|v r$t ||}||||fS )NNNsr:   )r>   Z_binom_meanZ_binom_varianceZ_binom_skewnessZ_binom_kurtosis_excess)r$   r%   r&   momentsmuvarg1g2r)   r)   r*   _statsU   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&   r:   valsr)   r)   r*   _entropy_   s   zbinom_gen._entropyrK   rJ   __name__
__module____qualname____doc__r+   r/   r3   r<   r@   rD   rF   rG   rI   rR   rX   r)   r)   r)   r*   r      s    


r   binom)namec                   @   j   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+         zbernoulli_gen._rvsc                 C   s   |dk|dk@ S r-   r)   r$   r&   r)   r)   r*   r/         zbernoulli_gen._argcheckc                 C   s   | j | jfS r!   )r2   brf   r)   r)   r*   r3      s   zbernoulli_gen._get_supportc                 C      t |d|S r5   )r_   r<   r$   r9   r&   r)   r)   r*   r<      r,   zbernoulli_gen._logpmfc                 C   ri   r5   )r_   r@   rj   r)   r)   r*   r@         zbernoulli_gen._pmfc                 C   ri   r5   )r_   rD   rj   r)   r)   r*   rD      r,   zbernoulli_gen._cdfc                 C   ri   r5   )r_   rF   rj   r)   r)   r*   rF      r,   zbernoulli_gen._sfc                 C   ri   r5   )r_   rG   rj   r)   r)   r*   rG      r,   zbernoulli_gen._isfc                 C   ri   r5   )r_   rI   )r$   rH   r&   r)   r)   r*   rI      r,   zbernoulli_gen._ppfc                 C   s   t d|S r5   )r_   rR   rf   r)   r)   r*   rR         zbernoulli_gen._statsc                 C   s   t |t d|  S r5   )r   rf   r)   r)   r*   rX      re   zbernoulli_gen._entropyrK   rZ   r)   r)   r)   r*   rb   h   s    
rb   	bernoulli)rh   r`   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%   r2   rh   r'   r(   r&   r)   r)   r*   r+      s   zbetabinom_gen._rvsc                 C   s   d|fS Nr   r)   r$   r%   r2   rh   r)   r)   r*   r3         zbetabinom_gen._get_supportc                 C   s   |dk|dk@ |dk@ S rp   r)   rq   r)   r)   r*   r/      r0   zbetabinom_gen._argcheckc                 C   sP   t |}t|d  t|| d |d  }|t|| || |  t|| S r5   )r   r   r   )r$   r9   r%   r2   rh   r:   r;   r)   r)   r*   r<      s   $$zbetabinom_gen._logpmfc                 C      t | ||||S r!   r   r<   )r$   r9   r%   r2   rh   r)   r)   r*   r@      re   zbetabinom_gen._pmfrJ   c                 C   sx  |||  }d| }|| }||| |  | | || d  }d\}	}
d|v rHdt | }	|	|| d|  ||  9 }	|	|| d ||   }	d|v r|| }
|
|| 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   rK   rL         ?   r:            r   )r$   r%   r2   rh   rM   Ze_pZe_qrN   rO   rP   rQ   r)   r)   r*   rR      s(   $4zbetabinom_gen._statsrK   rY   )
r[   r\   r]   r^   r+   r3   r/   r<   r@   rR   r)   r)   r)   r*   rn      s    
$rn   	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   r    r!   )Znegative_binomialr#   r)   r)   r*   r+   -  r,   znbinom_gen._rvsc                 C   s   |dk|dk@ |dk@ S r-   r)   r.   r)   r)   r*   r/   0  r0   znbinom_gen._argcheckc                 C   r=   r!   )r>   Z_nbinom_pdfr?   r)   r)   r*   r@   3  rA   znbinom_gen._pmfc                 C   s>   t || t |d  t | }||t|  t||  S r5   )r6   r   r   r8   )r$   r9   r%   r&   Zcoeffr)   r)   r*   r<   7  s    znbinom_gen._logpmfc                 C   rB   r!   )r   r>   Z_nbinom_cdfrC   r)   r)   r*   rD   ;  rE   znbinom_gen._cdfc                    sx   t |}| |||  dk}dd } fdd}tjdd t||||f||dW  d    S 1 s5w   Y  d S )	N      ?c                 S   s   t t| d |d|  S r5   )rT   r   r   Zbetaincr:   r%   r&   r)   r)   r*   f1D  s   znbinom_gen._logcdf.<locals>.f1c                    s
   t  S r!   )rT   r   r~   cdfr)   r*   f2G  r4   znbinom_gen._logcdf.<locals>.f2ignore)Zdividefr   )r   rD   rT   Zerrstater	   )r$   r9   r%   r&   r:   condr   r   r)   r   r*   _logcdf?  s   $znbinom_gen._logcdfc                 C   rB   r!   )r   r>   Z
_nbinom_sfrC   r)   r)   r*   rF   M  rE   znbinom_gen._sfc                 C   L   t   d}t jd|d t|||W  d    S 1 sw   Y  d S )Nz#overflow encountered in _nbinom_isfr   message)warningscatch_warningsfilterwarningsr>   Z_nbinom_isf)r$   r9   r%   r&   r   r)   r)   r*   rG   Q  s
   
$znbinom_gen._isfc                 C   r   )Nz#overflow encountered in _nbinom_ppfr   r   )r   r   r   r>   Z_nbinom_ppf)r$   rH   r%   r&   r   r)   r)   r*   rI   X  s
   
$znbinom_gen._ppfc                 C   s,   t ||t ||t ||t ||fS r!   )r>   Z_nbinom_meanZ_nbinom_varianceZ_nbinom_skewnessZ_nbinom_kurtosis_excessr.   r)   r)   r*   rR   ^  s
   



znbinom_gen._statsrK   )r[   r\   r]   r^   r+   r/   r@   r<   rD   r   rF   rG   rI   rR   r)   r)   r)   r*   r|      s    
2r|   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      |j ||dS Nr'   )	geometricrd   r)   r)   r*   r+     r,   zgeom_gen._rvsc                 C   s   |dk|dk@ S Nr   r   r)   rf   r)   r)   r*   r/     rg   zgeom_gen._argcheckc                 C   s   t d| |d | S r5   )rT   powerr$   r:   r&   r)   r)   r*   r@     r0   zgeom_gen._pmfc                 C   s   t |d | t| S r5   )r   r8   r   r   r)   r)   r*   r<        zgeom_gen._logpmfc                 C   s   t |}tt| |  S r!   )r   r   r   r$   r9   r&   r:   r)   r)   r*   rD        zgeom_gen._cdfc                 C   s   t | ||S r!   )rT   r   _logsfrj   r)   r)   r*   rF     s   zgeom_gen._sfc                 C   s   t |}|t|  S r!   )r   r   r   r)   r)   r*   r     rE   zgeom_gen._logsfc                 C   sF   t t| t|  }| |d |}t||k|dk@ |d |S r   )r   r   rD   rT   where)r$   rH   r&   rW   tempr)   r)   r*   rI     s   zgeom_gen._ppfc                 C   sP   d| }d| }|| | }d| t | }tg d|d|  }||||fS )Nru          @)r   irw   )r   rT   Zpolyval)r$   r&   rN   ZqrrO   rP   rQ   r)   r)   r*   rR     s   zgeom_gen._statsrK   )r[   r\   r]   r^   r+   r/   r@   r<   rD   rF   r   rI   rR   r)   r)   r)   r*   r   j  s    
r   geomzA geometric)r2   r`   longnamec                   @   ra   )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+        zhypergeom_gen._rvsc                 C   s    t |||  dt ||fS rp   rT   maximumZminimum)r$   r   r%   r   r)   r)   r*   r3     s    zhypergeom_gen._get_supportc                 C   s0   |dk|dk@ |dk@ }|||k||k@ M }|S rp   r)   )r$   r   r%   r   r   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 r5   r   )	r$   r:   r   r%   r   totgoodZbadresultr)   r)   r*   r<     s   
0zhypergeom_gen._logpmfc                 C      t ||||S r!   )r>   Z_hypergeom_pdfr$   r:   r   r%   r   r)   r)   r*   r@     rg   zhypergeom_gen._pmfc                 C   r   r!   )r>   Z_hypergeom_cdfr   r)   r)   r*   rD     rg   zhypergeom_gen._cdfc                 C   s   d| d| d| }}}|| }||d  d| ||   d| |  }||d | | 9 }|d| | ||  | d| d  7 }||| ||  | |d  |d   }t |||t |||t ||||fS )Nru   r   g      @g      @rw   r         @)r>   Z_hypergeom_meanZ_hypergeom_varianceZ_hypergeom_skewness)r$   r   r%   r   mrQ   r)   r)   r*   rR   	  s   (((zhypergeom_gen._statsc                 C   sB   t j|||  t||d  }| ||||}t jt|ddS )Nr   r   rS   )rT   rU   minZpmfrV   r   )r$   r   r%   r   r:   rW   r)   r)   r*   rX     s    zhypergeom_gen._entropyc                 C   r   r!   )r>   Z_hypergeom_sfr   r)   r)   r*   rF     rg   zhypergeom_gen._sfc                 C   s   g }t t|||| D ]>\}}}}	|d |d  |d |	d  k r3|tt| ||||	  qt|d |	d }
|t| 	|
|||	 qt
|S )Nr}   r   )ziprT   broadcast_arraysappendr   r   Zlogcdfaranger   r<   asarrayr$   r:   r   r%   r   resZquantr   r   ZdrawZk2r)   r)   r*   r   "  s     "
zhypergeom_gen._logsfc                 C   s   g }t t|||| D ]<\}}}}	|d |d  |d |	d  kr3|tt| ||||	  qtd|d }
|t| 	|
|||	 qt
|S )Nr}   r   r   )r   rT   r   r   r   r   Zlogsfr   r   r<   r   r   r)   r)   r*   r   .  s     "
zhypergeom_gen._logcdfrK   )r[   r\   r]   r^   r+   r3   r/   r<   r@   rD   rR   rX   rF   r   r   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      d|fS rp   r)   )r$   r   r%   rr)   r)   r*   r3     rr   znhypergeom_gen._get_supportc                 C   s(   |dk||k@ |dk@ ||| k@ }|S rp   r)   )r$   r   r%   r   r   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 r4|
 S |
S )Nr   nextZextrapolate)kindZ
fill_valuer   )	ZsupportrT   r   r   r   Zuniformastypeintitem)r   r%   r   r'   r(   r2   rh   ksr   Zppfrvsr$   r)   r*   _rvs1  s   z"nhypergeom_gen._rvs.<locals>._rvs1rc   _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 r5   r   )r:   r   r%   r   r)   r)   r*   <lambda>  s   z(nhypergeom_gen._logpmf.<locals>.<lambda>        )	fillvalue)r	   )r$   r:   r   r%   r   r   r   r)   r)   r*   r<     s   znhypergeom_gen._logpmfc                 C   rs   r!   rt   )r$   r:   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 )Nru   r   rv   rK   r)   )r$   r   r%   r   rN   rO   rP   rQ   r)   r)   r*   rR     s
   <znhypergeom_gen._statsrK   )
r[   r\   r]   r^   r3   r/   r+   r<   r@   rR   r)   r)   r)   r*   r   >  s    c

r   
nhypergeomc                   @   2   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   r   r   )Z	logseriesrd   r)   r)   r*   r+     rk   zlogser_gen._rvsc                 C   s   |dk|dk @ S r-   r)   rf   r)   r)   r*   r/     rg   zlogser_gen._argcheckc                 C   s"   t || d | t|  S Nru   )rT   r   r   r   r   r)   r)   r*   r@        "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 )	Nru   rv   rx         ?r   rw      r   )r   r   rT   r   )r$   r&   r   rN   Zmu2prO   Zmu3pZmu3rP   Zmu4pmu4rQ   r)   r)   r*   rR     s   :,zlogser_gen._statsrK   )r[   r\   r]   r^   r+   r/   r@   rR   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 rp   r)   )r$   rN   r)   r)   r*   r/   +  rr   zpoisson_gen._argcheckNc                 C   s   | ||S r!   poisson)r$   rN   r'   r(   r)   r)   r*   r+   .  rl   zpoisson_gen._rvsc                 C   s    t ||t|d  | }|S r5   )r   r7   r6   )r$   r:   rN   Pkr)   r)   r*   r<   1  s   zpoisson_gen._logpmfc                 C      t | ||S r!   rt   )r$   r:   rN   r)   r)   r*   r@   5  s   zpoisson_gen._pmfc                 C      t |}t||S r!   )r   r   pdtrr$   r9   rN   r:   r)   r)   r*   rD   9     zpoisson_gen._cdfc                 C   r   r!   )r   r   Zpdtrcr   r)   r)   r*   rF   =  r   zpoisson_gen._sfc                 C   s>   t t||}t|d d}t||}t||k||S r   )r   r   ZpdtrikrT   r   r   r   )r$   rH   rN   rW   vals1r   r)   r)   r*   rI   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   rz   r9   r)   r)   r*   r   K  s    z$poisson_gen._stats.<locals>.<lambda>c                 S   s   d|  S r   r)   r   r)   r)   r*   r   L  s    )rT   r   r	   inf)r$   rN   rO   tmpZ
mu_nonzerorP   rQ   r)   r)   r*   rR   G  s   
zpoisson_gen._statsrK   )r[   r\   r]   r^   r/   r+   r<   r@   rD   rF   rI   rR   r)   r)   r)   r*   r     s    
r   r   z	A Poisson)r`   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      |dkS rp   r)   )r$   lambda_r)   r)   r*   r/   o  rr   zplanck_gen._argcheckc                 C   s   t |  t| |  S r!   )r   r   )r$   r:   r   r)   r)   r*   r@   r  r   zplanck_gen._pmfc                 C   s   t |}t| |d   S r5   )r   r   r$   r9   r   r:   r)   r)   r*   rD   u  r   zplanck_gen._cdfc                 C   r   r!   )r   r   )r$   r9   r   r)   r)   r*   rF   y  rg   zplanck_gen._sfc                 C   s   t |}| |d  S r5   r   r   r)   r)   r*   r   |  rE   zplanck_gen._logsfc                 C   sL   t d| t|  d }|d j| | }| ||}t||k||S )N      r   )r   r   clipr3   rD   rT   r   )r$   rH   r   rW   r   r   r)   r)   r*   rI     s   zplanck_gen._ppfNc                 C   s   t |  }|j||dd S )Nr   ru   )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   rv   r   r   )r   r   r   )r$   r   rN   rO   rP   rQ   r)   r)   r*   rR     s
   zplanck_gen._statsc                 C   s&   t |  }|t|  | t| S r!   )r   r   r   )r$   r   Cr)   r)   r*   rX     s   zplanck_gen._entropyrK   )r[   r\   r]   r^   r/   r@   rD   rF   r   rI   r+   rR   rX   r)   r)   r)   r*   r   S  s    
r   planckzA discrete exponential c                   @   @   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 rp   r)   r$   r   r   r)   r)   r*   r/     rg   zboltzmann_gen._argcheckc                 C   s   | j |d fS r5   r1   r   r)   r)   r*   r3     r,   zboltzmann_gen._get_supportc                 C   s2   dt |  dt | |   }|t | |  S r5   r   )r$   r:   r   r   Zfactr)   r)   r*   r@     s    zboltzmann_gen._pmfc                 C   s0   t |}dt| |d   dt| |   S r5   )r   r   )r$   r9   r   r   r:   r)   r)   r*   rD     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   rT   r   rD   r   )r$   rH   r   r   ZqnewrW   r   r   r)   r)   r*   rI     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 )Nru   r   rv   rx   r   r   r   )r$   r   r   zZzNrN   rO   ZtrmZtrm2rP   rQ   r)   r)   r*   rR     s   
((@zboltzmann_gen._statsN)
r[   r\   r]   r^   r/   r3   r@   rD   rI   rR   r)   r)   r)   r*   r     s    r   	boltzmannz!A truncated discrete exponential )r`   r2   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/     rr   zrandint_gen._argcheckc                 C   s   ||d fS r5   r)   r   r)   r)   r*   r3     rl   zrandint_gen._get_supportc                 C   s,   t |||  }t ||k||k @ |dS )Nr   )rT   Z	ones_liker   )r$   r:   r   r   r&   r)   r)   r*   r@     s   zrandint_gen._pmfc                 C   s   t |}|| d ||  S r   r   )r$   r9   r   r   r:   r)   r)   r*   rD     r   zrandint_gen._cdfc                 C   sH   t |||  | d }|d ||}| |||}t||k||S r5   )r   r   rD   rT   r   )r$   rH   r   r   rW   r   r   r)   r)   r*   rI     s   zrandint_gen._ppfc           
      C   sj   t |t |}}|| d d }|| }|| d d }d}d|| d  || d  }	||||	fS )Nru   rv   r   g      (@r   g333333)rT   r   )
r$   r   r   m2m1rN   drO   rP   rQ   r)   r)   r*   rR     s   zrandint_gen._statsNc                 C   sr   t |jdkrt |jdkrt||||dS |dur(t ||}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)rT   r   r'   r
   Zbroadcast_to	vectorizer   Z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*   rX     rl   zrandint_gen._entropyrK   )r[   r\   r]   r^   r/   r3   r@   rD   rI   rR   r+   rX   r)   r)   r)   r*   r     s    
	r   r   z#A discrete uniform (random integer)c                   @   r   )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   r   r   )zipf)r$   r2   r'   r(   r)   r)   r*   r+   R  r,   zzipf_gen._rvsc                 C   r   r5   r)   r$   r2   r)   r)   r*   r/   U  rr   zzipf_gen._argcheckc                 C   s   dt |d ||  }|S Nru   r   r   r   )r$   r:   r2   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 r5   r   )r2   r%   r)   r)   r*   r   `      z zipf_gen._munp.<locals>.<lambda>)r	   rT   r   )r$   r%   r2   r)   r)   r*   _munp]  s
   zzipf_gen._munprK   )r[   r\   r]   r^   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%   r2   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  < q|S )z#Generalized harmonic number, a <= 1Zdtyper   r   )rT   r'   maxZ
zeros_likefloatr   )r%   r2   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   r   )rT   r   r	   r   r  r   r)   r)   r*   _gen_harmonicz  s   r  c                   @   r   )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   )rT   r   r   r$   r2   r%   r)   r)   r*   r/     r   zzipfian_gen._argcheckc                 C   r   r5   r)   r	  r)   r)   r*   r3     rr   zzipfian_gen._get_supportc                 C   s   dt || ||  S r   r  r$   r:   r2   r%   r)   r)   r*   r@     r   zzipfian_gen._pmfc                 C   s   t ||t || S r!   r
  r  r)   r)   r*   rD     re   zzipfian_gen._cdfc                 C   s:   |d }|| t ||t ||  d || t ||  S r5   r
  r  r)   r)   r*   rF     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   rv   rx   r   r   rw   r
  )r$   r2   r%   ZHnaZHna1ZHna2ZHna3ZHna4mu1Zmu2nZmu2dmu2rP   rQ   r)   r)   r*   rR     s"   
82
zzipfian_gen._statsN)
r[   r\   r]   r^   r/   r3   r@   rD   rF   rR   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$   r:   r2   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   r:   r2   r)   r)   r*   r     s    z#dlaplace_gen._cdf.<locals>.<lambda>c                 S   s   t || d  t |d  S r5   r   r  r)   r)   r*   r     r   r   r   )r   r	   )r$   r9   r2   r:   r   r   r)   r)   r*   rD     s   zdlaplace_gen._cdfc                 C   st   dt | }tt|ddt |   k t|| | d td| |  | }|d }t| |||k||S )Nr   ru   )r   r   rT   r   r   rD   )r$   rH   r2   constrW   r   r)   r)   r*   rI     s   zdlaplace_gen._ppfc                 C   s\   t |}d| |d d  }d| |d d|  d  |d d  }d|d||d  d fS )Nr   ru   rv   g      $@r   r   r   r   )r$   r2   Zear  r   r)   r)   r*   rR     s   (zdlaplace_gen._statsc                 C   s   |t | tt|d  S r  )r   r   r   r   r)   r)   r*   rX     s   zdlaplace_gen._entropyNc                 C   s8   t t |  }|j||d}|j||d}|| S r   )rT   r   r   r   )r$   r2   r'   r(   ZprobOfSuccessr9   yr)   r)   r*   r+     s   zdlaplace_gen._rvsrK   )
r[   r\   r]   r^   r@   rD   rI   rR   rX   r+   r)   r)   r)   r*   r    s    r  dlaplacezA discrete Laplacianc                   @   r   )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   rv   r   )rT   r   r   r$   r9   r  r  Zpxr)   r)   r*   r@   D  s
   
zskellam_gen._pmfc              
   C   sN   t |}t|dk td| d| d| dtd| d|d  d|  }|S )Nr   rv   r   )r   rT   r   r   r  r)   r)   r*   rD   K  s   
zskellam_gen._cdfc                 C   s4   || }|| }|t |d  }d| }||||fS )Nrx   r   rz   )r$   r  r  ZmeanrO   rP   rQ   r)   r)   r*   rR   R  s
   zskellam_gen._statsrK   )r[   r\   r]   r^   r+   r@   rD   rR   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 r5   r   ro   r$   r9   r  r)   r)   r*   r@     re   zyulesimon_gen._pmfc                 C   r   rp   r)   )r$   r  r)   r)   r*   r/     rr   zyulesimon_gen._argcheckc                 C   s   t |t||d  S r5   r   r   r   r  r)   r)   r*   r<     r0   zyulesimon_gen._logpmfc                 C   s   d|t ||d   S r5   r  r  r)   r)   r*   rD     r0   zyulesimon_gen._cdfc                 C   s   |t ||d  S r5   r  r  r)   r)   r*   rF     re   zyulesimon_gen._sfc                 C   s   t |t||d  S r5   r  r  r)   r)   r*   r     r0   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   rv   r   rx   r   1      )rT   r   r   nanr   )r$   r  rN   r  rP   rQ   r)   r)   r*   rR     s*   

"
zyulesimon_gen._statsrK   )r[   r\   r]   r^   r+   r@   r/   r<   rD   rF   r   rR   r)   r)   r)   r*   r  ]  s    
!r  	yulesimon)r`   r2   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|r)g || |R  S t| }t|j}t||  || f}t	|||}tj
| |   D ] g  fdd|D ||R  | < qOt	|||S )Nr   c                    s   g | ]	}t |  qS r)   )rT   Zsqueeze).0argr  r)   r*   
<listcomp>  s    z<_vectorize_rvs_over_shapes.<locals>._rvs.<locals>.<listcomp>)r   shaperT   Z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_genzA noncentral hypergeometric discrete random variable.

    For subclassing by nchypergeom_fisher_gen and nchypergeom_wallenius_gen.

    Nc           	      C   s<   |||}}}|| }t d|| }t ||}||fS rp   r   )	r$   r   r%   r   oddsr   r   Zx_minZx_maxr)   r)   r*   r3     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 rp   )rT   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!   )rT   Z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>._rvs1rc   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)r9   r   r%   r   r.  r2  r   r)   r*   _pmf1  s   
z$_nchypergeom_gen._pmf.<locals>._pmf1rT   r   )r$   r9   r   r%   r   r.  r5  r)   r   r*   r@     s   z_nchypergeom_gen._pmfc                    sL   t j fdd}d|v sd|v r|||||nd\}}d\}	}
|||	|
fS )Nc                    s     ||| |d}| S r3  )r4  rM   )r   r%   r   r.  r2  r   r)   r*   	_moments1  s   z*_nchypergeom_gen._stats.<locals>._moments1r   vrK   r6  )r$   r   r%   r   r.  rM   r7  r   r8  rL   r:   r)   r   r*   rR     s   z_nchypergeom_gen._statsrK   )r[   r\   r]   r^   r0  r4  r3   r/   r+   r@   rR   r)   r)   r)   r*   r-    s    
	r-  c                   @      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)r[   r\   r]   r^   r0  r   r4  r)   r)   r)   r*   r:        Ir:  nchypergeom_fisherz$A Fisher's noncentral hypergeometricc                   @   r9  )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)r[   r\   r]   r^   r0  r   r4  r)   r)   r)   r*   r=  c  r;  r=  nchypergeom_walleniusz&A Wallenius' noncentral hypergeometric)^	functoolsr   r   Zscipyr   Zscipy.specialr   r   r   r   r6   r   Zscipy._lib._utilr	   r
   Zscipy.interpolater   Znumpyr   r   r   r   r   r   r   r   r   r   rT   Z_distn_infrastructurer   r   r   r   r   Zscipy.stats._boostZstatsr>   Z
_biasedurnr   r   r   r   r_   rb   rm   rn   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-  r:  r<  r=  r>  listglobalscopyitemsZpairsZ_distn_namesZ_distn_gen_names__all__r)   r)   r)   r*   <module>   s   0
L>
N
mA 
 
4?D;J<RG9K&?NN