o
    5Bi                     @   sf  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  d dl!m"  m#Z# dd
l$m%Z%m&Z&m'Z' dd 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Z2G dd deZ3e3dd d!d"Z4G d#d$ d$eZ5e5d%dZ6G d&d' d'eZ7e7d(dZ8G d)d* d*eZ9e9dd+d,d"Z:G d-d. d.eZ;e;d/d0d1Z<G d2d3 d3eZ=e=d d4d5d"Z>G d6d7 d7eZ?e?d8d d9d:Z@G d;d< d<eZAeAd=d>d1ZBG d?d@ d@eZCeCddAdBd"ZDdCdD ZEdEdF ZFdGdH ZGG dIdJ dJeZHeHddKdLd"ZIG dMdN dNeZJeJejK dOdPd"ZLG dQdR dReZMeMejK dSdTd"ZNG dUdV dVeZOeOdWddXZPdYdZ ZQG d[d\ d\eZRG d]d^ d^eRZSeSd_d`d1ZTG dadb dbeRZUeUdcddd1ZVeWeX Y Z Z[ee[e\Z\Z]e\e] Z^dS )e    )partial)special)entr	logsumexpbetalngammalnzeta)
_lazywhererng_integers)interp1d)
floorceillogexpsqrtlog1pexpm1tanhcoshsinhN   )rv_discreteget_distribution_names_check_shape
_ShapeInfo)_PyFishersNCHypergeometric_PyWalleniusNCHypergeometric_PyStochasticLib3c                 C   s   | t | kS N)nproundx r#   \/var/www/html/Trade-python/venv/lib/python3.10/site-packages/scipy/stats/_discrete_distns.py_isintegral      r%   c                   @   st   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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

    c                 C   "   t dddtjfdt ddddgS 	NnTr   TFpFr   r   TTr   r   infselfr#   r#   r$   _shape_info8      zbinom_gen._shape_infoNc                 C      | |||S r   )binomialr2   r*   r,   sizerandom_stater#   r#   r$   _rvs<   r&   zbinom_gen._rvsc                 C   s    |dkt |@ |dk@ |dk@ S Nr   r   r%   r2   r*   r,   r#   r#   r$   	_argcheck?       zbinom_gen._argcheckc                 C   s
   | j |fS r   ar=   r#   r#   r$   _get_supportB   s   
zbinom_gen._get_supportc                 C   sR   t |}t|d t|d t|| d   }|t|| t|| |  S Nr   )r   gamlnr   xlogyxlog1py)r2   r"   r*   r,   kcombilnr#   r#   r$   _logpmfE   s   ("zbinom_gen._logpmfc                 C      t |||S r   )_boost
_binom_pdfr2   r"   r*   r,   r#   r#   r$   _pmfJ      zbinom_gen._pmfc                 C      t |}t|||S r   )r   rK   
_binom_cdfr2   r"   r*   r,   rG   r#   r#   r$   _cdfN      zbinom_gen._cdfc                 C   rP   r   )r   rK   	_binom_sfrR   r#   r#   r$   _sfR   rT   zbinom_gen._sfc                 C   rJ   r   )rK   
_binom_isfrM   r#   r#   r$   _isfV   r&   zbinom_gen._isfc                 C   rJ   r   )rK   
_binom_ppfr2   qr*   r,   r#   r#   r$   _ppfY   r&   zbinom_gen._ppfmvc                 C   sT   t ||}t ||}d\}}d|v rt ||}d|v r$t ||}||||fS )NNNsrG   )rK   _binom_mean_binom_variance_binom_skewness_binom_kurtosis_excess)r2   r*   r,   momentsmuvarg1g2r#   r#   r$   _stats\   s   zbinom_gen._statsc                 C   s2   t jd|d  }| |||}t jt|ddS )Nr   r   axis)r   r_rN   sumr   )r2   r*   r,   rG   valsr#   r#   r$   _entropyf   s   zbinom_gen._entropyr^   r]   __name__
__module____qualname____doc__r3   r:   r>   rB   rI   rN   rS   rV   rX   r\   ri   ro   r#   r#   r#   r$   r'      s    


r'   binom)namec                   @   r   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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

    c                 C      t ddddgS Nr,   Fr-   r.   r   r1   r#   r#   r$   r3         zbernoulli_gen._shape_infoNc                 C   s   t j| d|||dS )Nr   r8   r9   )r'   r:   r2   r,   r8   r9   r#   r#   r$   r:         zbernoulli_gen._rvsc                 C   s   |dk|dk@ S r;   r#   r2   r,   r#   r#   r$   r>      r}   zbernoulli_gen._argcheckc                 C   s   | j | jfS r   )rA   br   r#   r#   r$   rB      s   zbernoulli_gen._get_supportc                 C      t |d|S rC   )rv   rI   r2   r"   r,   r#   r#   r$   rI      r&   zbernoulli_gen._logpmfc                 C   r   rC   )rv   rN   r   r#   r#   r$   rN         zbernoulli_gen._pmfc                 C   r   rC   )rv   rS   r   r#   r#   r$   rS      r&   zbernoulli_gen._cdfc                 C   r   rC   )rv   rV   r   r#   r#   r$   rV      r&   zbernoulli_gen._sfc                 C   r   rC   )rv   rX   r   r#   r#   r$   rX      r&   zbernoulli_gen._isfc                 C   r   rC   )rv   r\   )r2   r[   r,   r#   r#   r$   r\      r&   zbernoulli_gen._ppfc                 C   s   t d|S rC   )rv   ri   r   r#   r#   r$   ri         zbernoulli_gen._statsc                 C   s   t |t d|  S rC   )r   r   r#   r#   r$   ro      r   zbernoulli_gen._entropyr^   rq   r#   r#   r#   r$   ry   o   s    
ry   	bernoulli)r   rw   c                   @   sL   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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

    c                 C   :   t dddtjfdt dddtjfdt dddtjfdgS 	Nr*   Tr   r+   rA   FFFr   r/   r1   r#   r#   r$   r3         zbetabinom_gen._shape_infoNc                 C      | |||}||||S r   )betar6   r2   r*   rA   r   r8   r9   r,   r#   r#   r$   r:         zbetabinom_gen._rvsc                 C   s   d|fS Nr   r#   r2   r*   rA   r   r#   r#   r$   rB         zbetabinom_gen._get_supportc                 C       |dkt |@ |dk@ |dk@ S r   r<   r   r#   r#   r$   r>      r?   zbetabinom_gen._argcheckc                 C   sP   t |}t|d  t|| d |d  }|t|| || |  t|| S rC   )r   r   r   r2   r"   r*   rA   r   rG   rH   r#   r#   r$   rI      s   $$zbetabinom_gen._logpmfc                 C      t | ||||S r   r   rI   r2   r"   r*   rA   r   r#   r#   r$   rN      r   zbetabinom_gen._pmfr]   c                 C   s  |||  }d| }|| }||| |  | | || d  }d\}	}
d|v rHdt | }	|	|| d|  ||  9 }	|	|| d ||   }	d|v r|| |j}
|
|| 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   r^   r_         ?   rG            )r   astypedtype)r2   r*   rA   r   rd   e_pe_qre   rf   rg   rh   r#   r#   r$   ri      s(   $4zbetabinom_gen._statsr^   rp   )rr   rs   rt   ru   r3   r:   rB   r>   rI   rN   ri   r#   r#   r#   r$   r      s    #
r   	betabinomc                   @   j   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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 :math:`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

    c                 C   r(   r)   r/   r1   r#   r#   r$   r3   ;  r4   znbinom_gen._shape_infoNc                 C   r5   r   )negative_binomialr7   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>   B     znbinom_gen._argcheckc                 C   rJ   r   )rK   _nbinom_pdfrM   r#   r#   r$   rN   E  rO   znbinom_gen._pmfc                 C   s>   t || t |d  t | }||t|  t||  S rC   )rD   r   r   rF   )r2   r"   r*   r,   coeffr#   r#   r$   rI   I  s    znbinom_gen._logpmfc                 C   rP   r   )r   rK   _nbinom_cdfrR   r#   r#   r$   rS   M  rT   znbinom_gen._cdfc           	      C   s   t |}t|||\}}}| |||}|dk}dd }|}tjdd" ||| || || ||< t||  || < W d    |S 1 sJw   Y  |S )N      ?c                 S   s   t t| d |d|  S rC   )r   r   r   betainc)rG   r*   r,   r#   r#   r$   f1V     znbinom_gen._logcdf.<locals>.f1ignore)divide)r   r   broadcast_arraysrS   errstater   )	r2   r"   r*   r,   rG   cdfcondr   logcdfr#   r#   r$   _logcdfQ  s   
znbinom_gen._logcdfc                 C   rP   r   )r   rK   
_nbinom_sfrR   r#   r#   r$   rV   `  rT   znbinom_gen._sfc                 C   >   t jdd t|||W  d    S 1 sw   Y  d S Nr   over)r   r   rK   _nbinom_isfrM   r#   r#   r$   rX   d     $znbinom_gen._isfc                 C   r   r   )r   r   rK   _nbinom_ppfrZ   r#   r#   r$   r\   h  r   znbinom_gen._ppfc                 C   s,   t ||t ||t ||t ||fS r   )rK   _nbinom_mean_nbinom_variance_nbinom_skewness_nbinom_kurtosis_excessr=   r#   r#   r$   ri   l  s
   



znbinom_gen._statsr^   )rr   rs   rt   ru   r3   r:   r>   rN   rI   rS   r   rV   rX   r\   ri   r#   r#   r#   r$   r     s    2
r   nbinomc                   @   sD   e Zd ZdZdd ZdddZdd Zd	d
 Zdd ZdddZ	dS )betanbinom_genaK  A beta-negative-binomial discrete random variable.

    %(before_notes)s

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

    The probability mass function for `betanbinom` is:

    .. math::

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

    for :math:`k \ge 0`, :math:`n \geq 0`, :math:`a > 0`,
    :math:`b > 0`, where :math:`B(a, b)` is the beta function.

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

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

    %(after_notes)s

    .. versionadded:: 1.12.0

    See Also
    --------
    betabinom : Beta binomial distribution

    %(example)s

    c                 C   r   r   r/   r1   r#   r#   r$   r3     r   zbetanbinom_gen._shape_infoNc                 C   r   r   )r   r   r   r#   r#   r$   r:     r   zbetanbinom_gen._rvsc                 C   r   r   r<   r   r#   r#   r$   r>     r?   zbetanbinom_gen._argcheckc                 C   sF   t |}t||  t||d  }|t|| ||  t|| S rC   )r   r   r   r   r   r#   r#   r$   rI     s    zbetanbinom_gen._logpmfc                 C   r   r   r   r   r#   r#   r$   rN     r   zbetanbinom_gen._pmfr]   c                 C   s   dd }t |dk|||f|tjd}dd }t |dk|||f|tjd}d\}}	d	d
 }
d|v r>t |dk|||f|
tjd}dd }d|v rTt |dk|||f|tjd}	||||	fS )Nc                 S   s   | | |d  S Nr   r#   r*   rA   r   r#   r#   r$   mean  r}   z#betanbinom_gen._stats.<locals>.meanr   )f	fillvaluec                 S   s4   | | | | d  || d  |d |d d   S )Nr          @r#   r   r#   r#   r$   rf     s   z"betanbinom_gen._stats.<locals>.varr   r^   c                 S   sT   d|  | d d| | d  |d  t | | | | d  || d  |d   S )Nr   r         @r   r   r   r#   r#   r$   skew  s    z#betanbinom_gen._stats.<locals>.skewr_   r   c                 S   s   |d }|d d |d |d| d   d|d  |   d| d  |d |d  |d |d  |  d|d d     d|d  |  |d |d  |d |d  |  d|d d     }|d	 |d  | |  || d  ||  d  }|| | d S )
Nr   r   r         @r         @r   r   g      @r#   )r*   rA   r   termterm_2denominatorr#   r#   r$   kurtosis  s0    "

z'betanbinom_gen._stats.<locals>.kurtosisrG      r	   r   r0   )r2   r*   rA   r   rd   r   re   rf   rg   rh   r   r   r#   r#   r$   ri     s   zbetanbinom_gen._statsr^   rp   )
rr   rs   rt   ru   r3   r:   r>   rI   rN   ri   r#   r#   r#   r$   r   x  s    $
r   
betanbinomc                   @   r   )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

    c                 C   rz   r{   r|   r1   r#   r#   r$   r3     r}   zgeom_gen._shape_infoNc                 C      |j ||dS Nr8   )	geometricr   r#   r#   r$   r:     r&   zgeom_gen._rvsc                 C   s   |dk|dk@ S Nr   r   r#   r   r#   r#   r$   r>     r}   zgeom_gen._argcheckc                 C   s   t d| |d | S rC   )r   powerr2   rG   r,   r#   r#   r$   rN     r   zgeom_gen._pmfc                 C   s   t |d | t| S rC   )r   rF   r   r   r#   r#   r$   rI        zgeom_gen._logpmfc                 C   s   t |}tt| |  S r   )r   r   r   r2   r"   r,   rG   r#   r#   r$   rS        zgeom_gen._cdfc                 C   s   t | ||S r   )r   r   _logsfr   r#   r#   r$   rV     s   zgeom_gen._sfc                 C   s   t |}|t|  S r   )r   r   r   r#   r#   r$   r     rT   zgeom_gen._logsfc                 C   sF   t t| t|  }| |d |}t||k|dk@ |d |S r   )r   r   rS   r   where)r2   r[   r,   rn   tempr#   r#   r$   r\     s   zgeom_gen._ppfc                 C   sP   d| }d| }|| | }d| t | }tg d|d|  }||||fS )Nr   r   )r   ir   )r   r   polyval)r2   r,   re   qrrf   rg   rh   r#   r#   r$   ri     s   zgeom_gen._statsc                 C   s$   t | t | d|  |  S r   )r   r   r   r   r#   r#   r$   ro     s   $zgeom_gen._entropyr^   )rr   rs   rt   ru   r3   r:   r>   rN   rI   rS   rV   r   r\   ri   ro   r#   r#   r#   r$   r     s    
r   geomzA geometric)rA   rw   longnamec                   @   rx   )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
    --------
    >>> import numpy as np
    >>> 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

    c                 C   :   t dddtjfdt dddtjfdt dddtjfdgS )NMTr   r+   r*   Nr/   r1   r#   r#   r$   r3   f  r   zhypergeom_gen._shape_infoNc                 C   s   |j ||| ||dS r   )hypergeometric)r2   r   r*   r   r8   r9   r#   r#   r$   r:   k     zhypergeom_gen._rvsc                 C   s    t |||  dt ||fS r   r   maximumminimum)r2   r   r*   r   r#   r#   r$   rB   n  r?   zhypergeom_gen._get_supportc                 C   sL   |dk|dk@ |dk@ }|||k||k@ M }|t |t |@ t |@ M }|S r   r<   )r2   r   r*   r   r   r#   r#   r$   r>   q  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 rC   r   )	r2   rG   r   r*   r   totgoodbadresultr#   r#   r$   rI   w  s   
0zhypergeom_gen._logpmfc                 C      t ||||S r   )rK   _hypergeom_pdfr2   rG   r   r*   r   r#   r#   r$   rN     r}   zhypergeom_gen._pmfc                 C   r   r   )rK   _hypergeom_cdfr   r#   r#   r$   rS     r}   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 )Nr   r   r   r   r   r   r   )rK   _hypergeom_mean_hypergeom_variance_hypergeom_skewness)r2   r   r*   r   mrh   r#   r#   r$   ri     s   (((zhypergeom_gen._statsc                 C   sB   t j|||  t||d  }| ||||}t jt|ddS )Nr   r   rj   )r   rl   minpmfrm   r   )r2   r   r*   r   rG   rn   r#   r#   r$   ro     s    zhypergeom_gen._entropyc                 C   r   r   )rK   _hypergeom_sfr   r#   r#   r$   rV     r}   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   )zipr   r   appendr   r   r   aranger   rI   asarrayr2   rG   r   r*   r   resquantr   r   draw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  r   r   r  r   r   logsfr  r   rI   r  r  r#   r#   r$   r     s     "
zhypergeom_gen._logcdfr^   )rr   rs   rt   ru   r3   r:   rB   r>   rI   rN   rS   ri   ro   rV   r   r   r#   r#   r#   r$   r   #  s    B
r   	hypergeomc                   @   sJ   e Zd ZdZdd Zdd Zdd Zdd	d
Zdd Zdd Z	dd Z
dS )nhypergeom_genab  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
    --------
    >>> import numpy as np
    >>> 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   r   )Nr   Tr   r+   r*   rr/   r1   r#   r#   r$   r3     r   znhypergeom_gen._shape_infoc                 C      d|fS r   r#   )r2   r   r*   r  r#   r#   r$   rB   $  r   znhypergeom_gen._get_supportc                 C   sD   |dk||k@ |dk@ ||| k@ }|t |t |@ t |@ M }|S r   r<   )r2   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   nextextrapolate)kind
fill_valuer   )	supportr   r  r   r   uniformr   intitem)r   r*   r  r8   r9   rA   r   ksr   ppfrvsr1   r#   r$   _rvs1.  s   z"nhypergeom_gen._rvs.<locals>._rvs1r~   _vectorize_rvs_over_shapes)r2   r   r*   r  r8   r9   r  r#   r1   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 rC   r   )rG   r   r*   r  r#   r#   r$   <lambda>?  s   z(nhypergeom_gen._logpmf.<locals>.<lambda>        )r   )r	   )r2   rG   r   r*   r  r   r   r#   r#   r$   rI   <  s   znhypergeom_gen._logpmfc                 C   r   r   r   )r2   rG   r   r*   r  r#   r#   r$   rN   F  s   znhypergeom_gen._pmfc                 C   s   d| d| d| }}}|| || d  }||d  | || d || d   d||| d    }d\}}||||fS )Nr   r   r   r^   r#   )r2   r   r*   r  re   rf   rg   rh   r#   r#   r$   ri   K  s
   <znhypergeom_gen._statsr^   )rr   rs   rt   ru   r3   rB   r>   r:   rI   rN   ri   r#   r#   r#   r$   r    s    d

r  
nhypergeomc                   @   :   e Zd ZdZd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

    c                 C   rz   r{   r|   r1   r#   r#   r$   r3   v  r}   zlogser_gen._shape_infoNc                 C   r   r   )	logseriesr   r#   r#   r$   r:   y  r   zlogser_gen._rvsc                 C   s   |dk|dk @ S r;   r#   r   r#   r#   r$   r>   ~  r}   zlogser_gen._argcheckc                 C   s"   t || d | t|  S r   )r   r   r   r   r   r#   r#   r$   rN        "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 )	Nr   r   r         ?r   r   r   r   )r   r   r   r   )r2   r,   r  re   mu2prf   mu3pmu3rg   mu4pmu4rh   r#   r#   r$   ri     s   :,zlogser_gen._statsr^   )	rr   rs   rt   ru   r3   r:   r>   rN   ri   r#   r#   r#   r$   r!  ]  s    
r!  logserzA logarithmicc                   @   sZ   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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      t dddtjfdgS )Nre   Fr   r+   r/   r1   r#   r#   r$   r3     r   zpoisson_gen._shape_infoc                 C   s   |dkS r   r#   )r2   re   r#   r#   r$   r>     r   zpoisson_gen._argcheckNc                 C   s   | ||S r   poisson)r2   re   r8   r9   r#   r#   r$   r:     r   zpoisson_gen._rvsc                 C   s    t ||t|d  | }|S rC   )r   rE   rD   )r2   rG   re   Pkr#   r#   r$   rI     s   zpoisson_gen._logpmfc                 C      t | ||S r   r   )r2   rG   re   r#   r#   r$   rN     s   zpoisson_gen._pmfc                 C      t |}t||S r   )r   r   pdtrr2   r"   re   rG   r#   r#   r$   rS        zpoisson_gen._cdfc                 C   r1  r   )r   r   pdtrcr3  r#   r#   r$   rV     r4  zpoisson_gen._sfc                 C   s>   t t||}t|d d}t||}t||k||S r   )r   r   pdtrikr   r   r2  r   )r2   r[   re   rn   vals1r   r#   r#   r$   r\     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   r   r!   r#   r#   r$   r    s    z$poisson_gen._stats.<locals>.<lambda>c                 S   s   d|  S r   r#   r!   r#   r#   r$   r    s    )r   r  r	   r0   )r2   re   rf   tmp
mu_nonzerorg   rh   r#   r#   r$   ri     s   
zpoisson_gen._statsr^   )rr   rs   rt   ru   r3   r>   r:   rI   rN   rS   rV   r\   ri   r#   r#   r#   r$   r+    s    
r+  r.  z	A Poisson)rw   r   c                   @   sb   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 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   r,  )NlambdaFr   r   r/   r1   r#   r#   r$   r3     r   zplanck_gen._shape_infoc                 C      |dkS r   r#   )r2   lambda_r#   r#   r$   r>     r   zplanck_gen._argcheckc                 C   s   t |  t| |  S r   )r   r   )r2   rG   r=  r#   r#   r$   rN     r   zplanck_gen._pmfc                 C   s   t |}t| |d   S rC   )r   r   r2   r"   r=  rG   r#   r#   r$   rS     r   zplanck_gen._cdfc                 C   r0  r   )r   r   )r2   r"   r=  r#   r#   r$   rV     r}   zplanck_gen._sfc                 C   s   t |}| |d  S rC   r   r>  r#   r#   r$   r   	  rT   zplanck_gen._logsfc                 C   sL   t d| t|  d }|d j| | }| ||}t||k||S )N      r   )r   r   cliprB   rS   r   r   )r2   r[   r=  rn   r7  r   r#   r#   r$   r\     s   zplanck_gen._ppfNc                 C   s   t |  }|j||dd S )Nr   r   )r   r   )r2   r=  r8   r9   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   r   r   r   )r   r   r   )r2   r=  re   rf   rg   rh   r#   r#   r$   ri     s
   zplanck_gen._statsc                 C   s&   t |  }|t|  | t| S r   )r   r   r   )r2   r=  Cr#   r#   r$   ro     s   zplanck_gen._entropyr^   )rr   rs   rt   ru   r3   r>   rN   rS   rV   r   r\   r:   ri   ro   r#   r#   r#   r$   r:    s    
r:  planckzA discrete exponential c                   @   H   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 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(   t dddtjfdt dddtjfdgS )Nr=  Fr   r   r   Tr/   r1   r#   r#   r$   r3   =     zboltzmann_gen._shape_infoc                 C   s   |dk|dk@ t |@ S r   r<   r2   r=  r   r#   r#   r$   r>   A  r   zboltzmann_gen._argcheckc                 C   s   | j |d fS rC   r@   rG  r#   r#   r$   rB   D  r&   zboltzmann_gen._get_supportc                 C   s2   dt |  dt | |   }|t | |  S rC   r   )r2   rG   r=  r   factr#   r#   r$   rN   G  s    zboltzmann_gen._pmfc                 C   s0   t |}dt| |d   dt| |   S rC   )r   r   )r2   r"   r=  r   rG   r#   r#   r$   rS   M  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   rA  r   r0   rS   r   )r2   r[   r=  r   qnewrn   r7  r   r#   r#   r$   r\   Q  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 )Nr   r   r   r   r$  r   rH  )r2   r=  r   zzNre   rf   trmtrm2rg   rh   r#   r#   r$   ri   X  s   
((@zboltzmann_gen._statsN)rr   rs   rt   ru   r3   r>   rB   rN   rS   r\   ri   r#   r#   r#   r$   rE  '  s    rE  	boltzmannz!A truncated discrete exponential )rw   rA   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 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

    Examples
    --------
    >>> import numpy as np
    >>> from scipy.stats import randint
    >>> import matplotlib.pyplot as plt
    >>> fig, ax = plt.subplots(1, 1)

    Calculate the first four moments:

    >>> low, high = 7, 31
    >>> mean, var, skew, kurt = randint.stats(low, high, moments='mvsk')

    Display the probability mass function (``pmf``):

    >>> x = np.arange(low - 5, high + 5)
    >>> ax.plot(x, randint.pmf(x, low, high), 'bo', ms=8, label='randint pmf')
    >>> ax.vlines(x, 0, randint.pmf(x, low, high), colors='b', lw=5, alpha=0.5)
    
    Alternatively, the distribution object can be called (as a function) to 
    fix the shape and location. This returns a "frozen" RV object holding the
    given parameters fixed.

    Freeze the distribution and display the frozen ``pmf``:

    >>> rv = randint(low, high)
    >>> ax.vlines(x, 0, rv.pmf(x), colors='k', linestyles='-',
    ...           lw=1, label='frozen pmf')
    >>> ax.legend(loc='lower center')
    >>> plt.show()
    
    Check the relationship between the cumulative distribution function
    (``cdf``) and its inverse, the percent point function (``ppf``):

    >>> q = np.arange(low, high)
    >>> p = randint.cdf(q, low, high)
    >>> np.allclose(q, randint.ppf(p, low, high))
    True

    Generate random numbers:

    >>> r = randint.rvs(low, high, size=1000)

    c                 C   s0   t ddtj tjfdt ddtj tjfdgS )NlowTr   highr/   r1   r#   r#   r$   r3     s   zrandint_gen._shape_infoc                 C   s   ||kt |@ t |@ S r   r<   r2   rQ  rR  r#   r#   r$   r>     r   zrandint_gen._argcheckc                 C   s   ||d fS rC   r#   rS  r#   r#   r$   rB     r   zrandint_gen._get_supportc                 C   s,   t |||  }t ||k||k @ |dS )Nr  )r   	ones_liker   )r2   rG   rQ  rR  r,   r#   r#   r$   rN     s   zrandint_gen._pmfc                 C   s   t |}|| d ||  S r   r?  )r2   r"   rQ  rR  rG   r#   r#   r$   rS     r   zrandint_gen._cdfc                 C   sH   t |||  | d }|d ||}| |||}t||k||S rC   )r   rA  rS   r   r   )r2   r[   rQ  rR  rn   r7  r   r#   r#   r$   r\     s   zrandint_gen._ppfc           
      C   sj   t |t |}}|| d d }|| }|| d d }d}d|| d  || d  }	||||	fS )Nr   r   r   g      (@r  g333333)r   r  )
r2   rQ  rR  m2m1re   drf   rg   rh   r#   r#   r$   ri     s   zrandint_gen._statsNc                 C   sv   t |jdkrt |jdkrt||||dS |dur(t ||}t ||}t jtt|t tgd}|||S )z=An array of *size* random integers >= ``low`` and < ``high``.r   r   N)otypes)	r   r  r8   r
   broadcast_to	vectorizer   r   r  )r2   rQ  rR  r8   r9   randintr#   r#   r$   r:     s    

zrandint_gen._rvsc                 C   s   t || S r   )r   rS  r#   r#   r$   ro     r   zrandint_gen._entropyr^   )rr   rs   rt   ru   r3   r>   rB   rN   rS   r\   ri   r:   ro   r#   r#   r#   r$   rP  j  s    ?
	rP  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`.

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

    c                 C   r,  )NrA   Fr   r   r/   r1   r#   r#   r$   r3     r   zzipf_gen._shape_infoNc                 C   r   r   )zipf)r2   rA   r8   r9   r#   r#   r$   r:     r&   zzipf_gen._rvsc                 C   r<  rC   r#   r2   rA   r#   r#   r$   r>     r   zzipf_gen._argcheckc                 C   s*   | tj}dt|d ||   }|S Nr   r   )r   r   float64r   r   )r2   rG   rA   r/  r#   r#   r$   rN     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 rC   )r   r   )rA   r*   r#   r#   r$   r  $  s    z zipf_gen._munp.<locals>.<lambda>r   )r2   r*   rA   r#   r#   r$   _munp!  s
   zzipf_gen._munpr^   )	rr   rs   rt   ru   r3   r:   r>   rN   ra  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*   rA   r#   r#   r$   _gen_harmonic_gt1+  s   rc  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r   r   r   )r   r8   max
zeros_likefloatr  )r*   rA   n_maxoutimaskr#   r#   r$   _gen_harmonic_leq11  s   

rm  c                 C   s(   t | |\} }t|dk| |fttdS )zGeneralized harmonic numberr   r   f2)r   r   r	   rc  rm  rb  r#   r#   r$   _gen_harmonic>  s   rp  c                   @   rD  )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`.

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

    c                 C   s(   t dddtjfdt dddtjfdgS )NrA   Fr   r+   r*   Tr   r/   r1   r#   r#   r$   r3   t  rF  zzipfian_gen._shape_infoc                 C   s"   |dk|dk@ |t j|tdk@ S )Nr   rd  )r   r  r  r2   rA   r*   r#   r#   r$   r>   x  r#  zzipfian_gen._argcheckc                 C   r  rC   r#   rr  r#   r#   r$   rB   |  r   zzipfian_gen._get_supportc                 C   s$   | tj}dt|| ||   S r   )r   r   r`  rp  r2   rG   rA   r*   r#   r#   r$   rN     s   zzipfian_gen._pmfc                 C   s   t ||t || S r   rp  rs  r#   r#   r$   rS     r   zzipfian_gen._cdfc                 C   s:   |d }|| t ||t ||  d || t ||  S rC   rt  rs  r#   r#   r$   rV     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   r   r   r   r$  r   rt  )r2   rA   r*   HnaHna1Hna2Hna3Hna4mu1mu2nmu2dmu2rg   rh   r#   r#   r$   ri     s"   
82
zzipfian_gen._statsN)rr   rs   rt   ru   r3   r>   rB   rN   rS   rV   ri   r#   r#   r#   r$   rq  E  s    .rq  zipfianz	A Zipfianc                   @   sJ   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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   r,  )NrA   Fr   r   r/   r1   r#   r#   r$   r3     r   zdlaplace_gen._shape_infoc                 C   s   t |d t| t|  S Nr   )r   r   abs)r2   rG   rA   r#   r#   r$   rN     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_  rH  rG   rA   r#   r#   r$   r     r   zdlaplace_gen._cdf.<locals>.fc                 S   s   t || d  t |d  S rC   rH  r  r#   r#   r$   ro       zdlaplace_gen._cdf.<locals>.f2r   rn  )r   r	   )r2   r"   rA   rG   r   ro  r#   r#   r$   rS     s   zdlaplace_gen._cdfc                 C   st   dt | }tt|ddt |   k t|| | d td| |  | }|d }t| |||k||S )Nr   r   )r   r   r   r   r   rS   )r2   r[   rA   constrn   r7  r#   r#   r$   r\     s   zdlaplace_gen._ppfc                 C   s\   t |}d| |d d  }d| |d d|  d  |d d  }d|d||d  d fS )Nr   r   r   g      $@r   r  r   rH  )r2   rA   ear}  r)  r#   r#   r$   ri     s   (zdlaplace_gen._statsc                 C   s   |t | tt|d  S r  )r   r   r   r^  r#   r#   r$   ro     r  zdlaplace_gen._entropyNc                 C   s8   t t |  }|j||d}|j||d}|| S r   )r   r   r  r   )r2   rA   r8   r9   probOfSuccessr"   yr#   r#   r$   r:     s   zdlaplace_gen._rvsr^   )rr   rs   rt   ru   r3   rN   rS   r\   ri   ro   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

    c                 C   s(   t dddtjfdt dddtjfdgS )Nrz  Fr   r   r}  r/   r1   r#   r#   r$   r3     rF  zskellam_gen._shape_infoNc                 C   s   |}| ||| || S r   r-  )r2   rz  r}  r8   r9   r*   r#   r#   r$   r:     s   

zskellam_gen._rvsc                 C   s   t jdd0 t |dk td| dd|  d| d td| dd|  d| d }W d    |S 1 s9w   Y  |S )Nr   r   r   r   r   )r   r   r   rK   	_ncx2_pdfr2   r"   rz  r}  pxr#   r#   r$   rN     s   
  
zskellam_gen._pmfc                 C   s   t |}tjdd, t|dk td| d| d| dtd| d|d  d|  }W d    |S 1 s9w   Y  |S )Nr   r   r   r   r   )r   r   r   r   rK   	_ncx2_cdfr  r#   r#   r$   rS   $  s   
 
zskellam_gen._cdfc                 C   s4   || }|| }|t |d  }d| }||||fS )Nr   r   r   )r2   rz  r}  r   rf   rg   rh   r#   r#   r$   ri   ,  s
   zskellam_gen._statsr^   )	rr   rs   rt   ru   r3   r:   rN   rS   ri   r#   r#   r#   r$   r    s    
r  skellamz	A Skellamc                   @   sZ   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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

    c                 C   r,  )NalphaFr   r   r/   r1   r#   r#   r$   r3   Y  r   zyulesimon_gen._shape_infoNc                 C   s6   | |}| |}t| tt| |   }|S r   )standard_exponentialr   r   r   )r2   r  r8   r9   E1E2ansr#   r#   r$   r:   \  s   

zyulesimon_gen._rvsc                 C   s   |t ||d  S rC   r   r   r2   r"   r  r#   r#   r$   rN   b  r   zyulesimon_gen._pmfc                 C   r<  r   r#   )r2   r  r#   r#   r$   r>   e  r   zyulesimon_gen._argcheckc                 C   s   t |t||d  S rC   r   r   r   r  r#   r#   r$   rI   h  r   zyulesimon_gen._logpmfc                 C   s   d|t ||d   S rC   r  r  r#   r#   r$   rS   k  r   zyulesimon_gen._cdfc                 C   s   |t ||d  S rC   r  r  r#   r#   r$   rV   n  r   zyulesimon_gen._sfc                 C   s   t |t||d  S rC   r  r  r#   r#   r$   r   q  r   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  |d    t j}t |dkt j|}||||fS )	Nr   r   r   r   r      1      )r   r   r0   nanr   )r2   r  re   r}  rg   rh   r#   r#   r$   ri   t  s&   

"
zyulesimon_gen._statsr^   )rr   rs   rt   ru   r3   r:   rN   r>   rI   rS   rV   r   ri   r#   r#   r#   r$   r  7  s    !
r  	yulesimon)rw   rA   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#   )r   squeeze).0argrk  r#   r$   
<listcomp>  s    z<_vectorize_rvs_over_shapes.<locals>._rvs.<locals>.<listcomp>)r   shaper   arrayallemptyr  ndimhstackmoveaxisndindex)r8   r9   args
_rvs1_size_rvs1_indicesrj  j0j1r  r  r$   r:     s"   




z(_vectorize_rvs_over_shapes.<locals>._rvsr#   )r  r:   r#   r  r$   r    s   	r  c                   @   sJ   e Zd ZdZdZdZd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   sL   t dddtjfdt dddtjfdt dddtjfdt dddtjfd	gS )
Nr   Tr   r+   r*   r   oddsFr   r/   r1   r#   r#   r$   r3     s
   z_nchypergeom_gen._shape_infoc           	      C   s<   |||}}}|| }t d|| }t ||}||fS r   r   )	r2   r   r*   r   r  rV  rU  x_minx_maxr#   r#   r$   rB     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 r   )r   r  r   r  )r2   r   r*   r   r  cond1cond2cond3cond4cond5cond6r#   r#   r$   r>     s   z_nchypergeom_gen._argcheckc                    s$   t  fdd}|||||||dS )Nc           
         s<   t |}t }t| j}|||| |||}	|	|}	|	S r   )r   prodr   getattrrvs_namereshape)
r   r*   r   r  r8   r9   lengthurnrv_genr  r1   r#   r$   r    s   

z$_nchypergeom_gen._rvs.<locals>._rvs1r~   r  )r2   r   r*   r   r  r8   r9   r  r#   r1   r$   r:     s   z_nchypergeom_gen._rvsc                    sR   t |||||\}}}}}|jdkrt |S t j fdd}||||||S )Nr   c                    s     ||||d}|| S Ng-q=)distprobability)r"   r   r*   r   r  r  r1   r#   r$   _pmf1  s   
z$_nchypergeom_gen._pmf.<locals>._pmf1)r   r   r8   
empty_likerZ  )r2   r"   r   r*   r   r  r  r#   r1   r$   rN     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 r  )r  rd   )r   r*   r   r  r  r1   r#   r$   	_moments1  s   z*_nchypergeom_gen._stats.<locals>._moments1r   vr^   )r   rZ  )r2   r   r*   r   r  rd   r  r   r  r_   rG   r#   r1   r$   ri     s   z_nchypergeom_gen._statsr^   )rr   rs   rt   ru   r  r  r3   rB   r>   r:   rN   ri   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

    
rvs_fisherN)rr   rs   rt   ru   r  r   r  r#   r#   r#   r$   r        Ir  nchypergeom_fisherz$A Fisher's noncentral hypergeometricc                   @   r  )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

    rvs_walleniusN)rr   rs   rt   ru   r  r   r  r#   r#   r#   r$   r  K  r  r  nchypergeom_walleniusz&A Wallenius' noncentral hypergeometric)_	functoolsr   scipyr   scipy.specialr   r   r   r   rD   r   scipy._lib._utilr	   r
   scipy.interpolater   numpyr   r   r   r   r   r   r   r   r   r   r   _distn_infrastructurer   r   r   r   scipy.stats._booststatsrK   
_biasedurnr   r   r   r%   r'   rv   ry   r   r   r   r   r   r   r   r   r   r   r  r  r  r!  r*  r+  r.  r:  rC  rE  rO  rP  r[  r\  r]  rc  rm  rp  rq  r~  r  r0   r  r  r  r  r  r  r  r  r  r  r  listglobalscopyitemspairs_distn_names_distn_gen_names__all__r#   r#   r#   r$   <module>   s   0
PA
R
m
]H 
 
 8BG?wBYP?O&INN