L iddlmZddlmZddlmZmZmZmZ m Z ddl m Z ddl mcmZddlmZddlmZmZmZmZmZmZmZmZmZmZddlZdd lm Z m!Z!m"Z"m#Z#m$Z$m%Z%dd l&m'Z'm(Z(m)Z)dd l*m+Z+ddl,mcm-Z.Gd d e Z/e/dZ0Gdde/Z1e1ddZ2Gdde Z3e3dZ4Gdde Z5e5dZ6Gdde Z7e7dZ8Gdde Z9e9ddd !Z:Gd"d#e Z;e;d$Z<Gd%d&e Z=e=d'Z>Gd(d)e Z?e?dd*d+!Z@Gd,d-e ZAeAd.d/0ZBGd1d2e ZCeCdd3d4!ZDGd5d6e ZEeEd7dd89ZFGd:d;e ZGeGdd?e ZIeIdd@dA!ZJdBZKdCZLdDZMGdEdFe ZNeNddGdH!ZOGdIdJe ZPePej dKdL!ZRGdMdNe ZSeSdOdPdQRZTdjdSZUdkdTZVdldUZWeTeScZXZYeUjeXeYeT_UeVjeXeYeT_VeWjeXeYeT_WGdVdWe%Z[GdXdYe Z\e\ej dZd[!Z]Gd\d]e Z^e^d^d_Z_Gd`dae Z`Gdbdce`Zaeaddde0ZbGdfdge`Zcecdhdi0ZdeeefjjZie!eie \ZjZkejekzZly)m)partial)special)entr logsumexpbetalngammalnzeta) rng_integersN)interp1d) floorceillogexpsqrtlog1pexpm1tanhcoshsinh) rv_discreteget_distribution_names_vectorize_rvs_over_shapes _ShapeInfo _isintegralrv_discrete_frozen)_PyFishersNCHypergeometric_PyWalleniusNCHypergeometric_PyStochasticLib3)_poisson_binomc\eZdZdZdZddZdZdZdZdZ d Z d Z d Z d Z dd ZdZy) binom_gena2A 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. This distribution uses routines from the Boost Math C++ library for the computation of the ``pmf``, ``cdf``, ``sf``, ``ppf`` and ``isf`` methods. [1]_ %(after_notes)s References ---------- .. [1] The Boost Developers. "Boost C++ Libraries". https://www.boost.org/. %(example)s See Also -------- hypergeom, nbinom, nhypergeom cZtdddtjfdtddddgS NnTrTFpFrrTTrnpinfselfs b/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/scipy/stats/_discrete_distns.py _shape_infozbinom_gen._shape_infoB03q"&&k=A3v|<> >Nc(|j|||SN)binomialr.r%r'size random_states r/_rvszbinom_gen._rvsFs$$Q400r2c<|dk\t|z|dk\z|dkzSNrrrr.r%r's r/ _argcheckzbinom_gen._argcheckIs'Q+a.(AF3qAv>>r2c|j|fSr4ar=s r/ _get_supportzbinom_gen._get_supportLsvvqyr2ct|}t|dzt|dzt||z dzzz }|tj||ztj||z | zSNr)r gamlnrxlogyxlog1py)r.xr%r'kcombilns r/_logpmfzbinom_gen._logpmfOsb !H1:qseAaCEl!:;q!,,wqsQB/GGGr2c0tj|||Sr4)scu _binom_pmfr.rHr%r's r/_pmfzbinom_gen._pmfTs~~aA&&r2cFt|}tj|||Sr4)r rM _binom_cdfr.rHr%r'rIs r/_cdfzbinom_gen._cdfX !H~~aA&&r2cFt|}tj|||Sr4)r rM _binom_sfrSs r/_sfz binom_gen._sf\s !H}}Q1%%r2c0tj|||Sr4)rM _binom_isfrOs r/_isfzbinom_gen._isf`~~aA&&r2c0tj|||Sr4)rM _binom_ppfr.qr%r's r/_ppfzbinom_gen._ppfcr\r2c||z}||tj|zz }d\}}d|vrR|tj|z }tj||z} tj| } d|z| z } | | z }d|vr<|tj|z }||z} tj| } d|z } | | z }||||fS)NNNs@rI@)r+squarer reciprocal) r.r%r'momentsmuvarg1g2pqnpq_sqrtt1t2npqs r/_statszbinom_gen._statsfs U1ryy|##B '>RYYq\!Bwwq2vHx(B'X%BbB '>RYYq\!Bb&Cs#BQBbB3Br2ctjd|dz}|j|||}tjt |dS)Nrraxis)r+r_rPsumr)r.r%r'rIvalss r/_entropyzbinom_gen._entropyxs< EE!AENyyAq!vvd4jq))r2rcmv__name__ __module__ __qualname____doc__r0r9r>rBrKrPrTrXr[rarsrzr2r/r"r"sE"F>1?H ''&''$*r2r"binom)namecZeZdZdZdZddZdZdZdZdZ d Z d Z d Z d Z d ZdZy) bernoulli_genaA 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 tddddgSNr'Fr(r)rr-s r/r0zbernoulli_gen._shape_info3v|<==r2Nc6tj|d|||S)Nrr7r8)r"r9r.r'r7r8s r/r9zbernoulli_gen._rvss~~dAqt,~OOr2c|dk\|dkzSr;rr.r's r/r>zbernoulli_gen._argchecksQ16""r2c2|j|jfSr4)rAbrs r/rBzbernoulli_gen._get_supportsvvtvv~r2c0tj|d|SrD)rrKr.rHr's r/rKzbernoulli_gen._logpmfs}}Q1%%r2c0tj|d|SrD)rrPrs r/rPzbernoulli_gen._pmfszz!Q""r2c0tj|d|SrD)rrTrs r/rTzbernoulli_gen._cdfzz!Q""r2c0tj|d|SrD)rrXrs r/rXzbernoulli_gen._sfsyyAq!!r2c0tj|d|SrD)rr[rs r/r[zbernoulli_gen._isfrr2c0tj|d|SrD)rra)r.r`r's r/razbernoulli_gen._ppfrr2c.tjd|SrD)rrsrs r/rszbernoulli_gen._statss||Aq!!r2c6t|td|z zSrD)rrs r/rzzbernoulli_gen._entropysAwac""r2rcr}rr2r/rrsD0>P#&# #"##"#r2r bernoulli)rrc>eZdZdZdZd dZdZdZdZdZ d d Z y) betabinom_genaA 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. %(after_notes)s References ---------- .. [1] https://en.wikipedia.org/wiki/Beta-binomial_distribution .. versionadded:: 1.4.0 See Also -------- beta, binom %(example)s ctdddtjfdtdddtjfdtdddtjfdgS Nr%Trr&rAFFFrr*r-s r/r0zbetabinom_gen._shape_infoP3q"&&k=A3266{NC3266{NCE Er2NcN|j|||}|j|||Sr4)betar5r.r%rArr7r8r's r/r9zbetabinom_gen._rvss+   aD )$$Q400r2c d|fSNrrr.r%rArs r/rBzbetabinom_gen._get_support !t r2c<|dk\t|z|dkDz|dkDzSrr<rs r/r>zbetabinom_gen._argcheck'Q+a.(AE2a!e<tCyB 1q51q5=QU+ +B 1q519Q' 'B '>a% *B 1q519q1u$ %B !a%!)q1u% %B !a1f* B !c'A+/QU+ +B "s(S.16) )B 1q5Q,!a%!), ,B 1q519A *a!eai8AEAIF GB !GB3Br2rcr{) r~rrrr0r9rBr>rKrPrsrr2r/rrs-"FE 1=A -r2r betabinomcTeZdZdZdZddZdZdZdZdZ d Z d Z d Z d Z d Zy) nbinom_genaA 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} This distribution uses routines from the Boost Math C++ library for the computation of the ``pmf``, ``cdf``, ``sf``, ``ppf``, ``isf`` and ``stats`` methods. [1]_ %(after_notes)s References ---------- .. [1] The Boost Developers. "Boost C++ Libraries". https://www.boost.org/. %(example)s See Also -------- hypergeom, binom, nhypergeom cZtdddtjfdtddddgSr$r*r-s r/r0znbinom_gen._shape_infoUr1r2Nc(|j|||Sr4)negative_binomialr6s r/r9znbinom_gen._rvsYs--aD99r2c$|dkD|dkDz|dkzSr;rr=s r/r>znbinom_gen._argcheck\sA!a% AF++r2c0tj|||Sr4)rM _nbinom_pmfrOs r/rPznbinom_gen._pmf_sq!Q''r2ct||zt|dzz t|z }||t|zztj|| zSrD)rErrrG)r.rHr%r'coeffs r/rKznbinom_gen._logpmfcsJac U1Q3Z'%(2qQx'//!aR"888r2cFt|}tj|||Sr4)r rM _nbinom_cdfrSs r/rTznbinom_gen._cdfgs !Hq!Q''r2cJt|}tj|||\}}}|j|||}|dkD}d}|}tjd5|||||||||<tj ||||<ddd|S#1swY|SxYw)N?cdtjtj|dz|d|z  SrD)r+rrbetainc)rIr%r's r/f1znbinom_gen._logcdf..f1ps)88W__QUAq1u==> >r2ignore)divide)r r+broadcast_arraysrTerrstater) r.rHr%r'rIcdfcondrlogcdfs r/_logcdfznbinom_gen._logcdfks !H%%aA.1aii1a Sy ? [[ ) /agqw$8F4LFF3u:.FD5M /  / s 4BB"cFt|}tj|||Sr4)r rM _nbinom_sfrSs r/rXznbinom_gen._sfzrUr2ctjd5tj|||cdddS#1swYyxYwNrover)r+rrM _nbinom_isfrOs r/r[znbinom_gen._isf~3 [[h ' ,??1a+ , , , 8Actjd5tj|||cdddS#1swYyxYwr)r+rrM _nbinom_ppfr_s r/raznbinom_gen._ppfrrctj||tj||tj||tj||fSr4)rM _nbinom_mean_nbinom_variance_nbinom_skewness_nbinom_kurtosis_excessr=s r/rsznbinom_gen._statssL   Q "  A &  A &  ' '1 -   r2rc)r~rrrr0r9r>rPrKrTrrXr[rarsrr2r/rrs?9t>:,(9( ',, r2rnbinomc8eZdZdZdZd dZdZdZdZd dZ y) betanbinom_genaKA 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. %(after_notes)s References ---------- .. [1] https://en.wikipedia.org/wiki/Beta_negative_binomial_distribution .. versionadded:: 1.12.0 See Also -------- betabinom : Beta binomial distribution %(example)s ctdddtjfdtdddtjfdtdddtjfdgSrr*r-s r/r0zbetanbinom_gen._shape_inforr2NcN|j|||}|j|||Sr4)rrrs r/r9zbetanbinom_gen._rvss+   aD )--aD99r2c<|dk\t|z|dkDz|dkDzSrr<rs r/r>zbetanbinom_gen._argcheckrr2ct|}tj||z t||dzz }|t||z||zzt||z SrD)r r+rrrs r/rKzbetanbinom_gen._logpmfsS !H66!a%=.6!QU#33Aq1u--q! <.meansq5AF# #r2r fill_valuecN||z||zdz z||zdz z|dz |dz dzzz S)Nrrerrs r/rkz"betanbinom_gen._stats..varsAEQURZ(AEBJ7B1r6B,.0 1r2rrccd|z|zdz d|z|zdz z|dz z t||z||zdz z||zdz z|dz z z S)Nrr@rerrs r/skewz#betanbinom_gen._stats..skewslUQY^A B72v!%a!eq1urz&:a!ebj&I2v'"   !r2rdrcz|dz }|dz dz|dz|d|zdz zzd|dz z|zzzd|dzz|dz|dzz|dz|dz z|zzd|dz dzzzzzd|dz z|z|dz|dzz|dz|dz z|zzd|dz dzzzzz}|d z |dz z|z|z||zdz z||zdz z}||z|z dz S) Nrerrrfr@rrg@r)r%rArtermterm_2 denominators r/kurtosisz'betanbinom_gen._stats..kurtosissNFD2vlaea1q52:.>&>a"f )'*+QU q2vB&6!b&R:!#$:%'%')QVaK'7'899QV q(b&ArE)QVB,?!,CCa"fr\)*+ +FFq2v.2Q6!ebj*-.URZ9K&=;.3 3r2rIxpx apply_wherer+r,) r.r%rArrirrjrkrlrmrrs r/rszbetanbinom_gen._statss $ __QUQ1It G 1ooa!eaAYGB ! '>QAq 4BFFKB 4 '>QAq 8OB3Br2rcr{) r~rrrr0r9r>rKrPrsrr2r/rrs'#HE :== -!r2r betanbinomcTeZdZdZdZddZdZdZdZdZ d Z d Z d Z d Z d Zy)geom_gena5A 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. Note that when drawing random samples, the probability of observations that exceed ``np.iinfo(np.int64).max`` increases rapidly as $p$ decreases below $10^{-17}$. For $p < 10^{-20}$, almost all observations would exceed the maximum ``int64``; however, the output dtype is always ``int64``, so these values are clipped to the maximum. %(after_notes)s See Also -------- planck %(example)s c tddddgSrrr-s r/r0zgeom_gen._shape_inforr2Nc|j||}tj|jj}tj |dk||S)Nr7r) geometricr+iinformaxwhere)r.r'r7r8resmax_ints r/r9z geom_gen._rvssH$$QT$2((399%))xxa#..r2c|dk|dkDzSNrrrrs r/r>zgeom_gen._argchecksQ1q5!!r2c@tjd|z |dz |zSrD)r+powerr.rIr's r/rPz geom_gen._pmf!s xx!QqS!A%%r2cNtj|dz | t|zSrD)rrGrrs r/rKzgeom_gen._logpmf$s"q1uqb)CF22r2cJt|}tt| |z Sr4)r rrr.rHr'rIs r/rTz geom_gen._cdf's# !HeQBik"""r2cLtj|j||Sr4)r+r_logsfrs r/rXz geom_gen._sf+svvdkk!Q'((r2c6t|}|t| zSr4)r rrs r/rzgeom_gen._logsf.s !Hr{r2ctt| t| z }|j|dz |}tj||k\|dkDz|dz |Sr)r rrTr+r )r.r`r'rytemps r/raz geom_gen._ppf2sUE1"Iqb )*yya#xxtax0$q&$??r2cd|z }d|z }||z |z }d|z t|z }tjgd|d|z z }||||fS)Nrre)rir)rr+polyval)r.r'rjqrrkrlrms r/rszgeom_gen._stats7sZ U U1fqj!etBx  ZZ A &A .3Br2cntj| tj| d|z z|z z Sr)r+rrrs r/rzzgeom_gen._entropy?s/q zBHHaRLCE2Q666r2rc)r~rrrr0r9r>rPrKrTrXrrarsrzrr2r/rrs@B>/"&3#)@ 7r2rgeomz A geometric)rArlongnamecZeZdZdZdZddZdZdZdZdZ d Z d Z d Z d Z d ZdZy) 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)!}. This distribution uses routines from the Boost Math C++ library for the computation of the ``pmf``, ``cdf``, ``sf`` and ``stats`` methods. [1]_ %(after_notes)s References ---------- .. [1] The Boost Developers. "Boost C++ Libraries". https://www.boost.org/. 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 ctdddtjfdtdddtjfdtdddtjfdgS)NMTrr&r%Nr*r-s r/r0zhypergeom_gen._shape_infoP3q"&&k=A3q"&&k=A3q"&&k=AC Cr2Nc2|j|||z ||SNr )hypergeometric)r.r&r%r'r7r8s r/r9zhypergeom_gen._rvss **1ac14*@@r2cftj|||z z dtj||fSrr+maximumminimum)r.r&r%r's r/rBzhypergeom_gen._get_supports+zz!QqS'1%rzz!Q'777r2c|dkD|dk\z|dk\z}|||k||kzz}|t|t|zt|zz}|Srr<)r.r&r%r'rs r/r>zhypergeom_gen._argchecksYA!q&!Q!V, aAF## AQ/+a.@@ r2c||}}||z }t|dzdt|dzdzt||z dz|dzzt|dz||z dzz t||z dz||z |zdzz t|dzdz }|SrDr) r.rIr&r%r'totgoodbadresults r/rKzhypergeom_gen._logpmfsqTDja#fSUA&66Aa19MM1d1fQh'(*01QAa *BCQ"# r2c2tj||||Sr4)rM_hypergeom_pmfr.rIr&r%r's r/rPzhypergeom_gen._pmf!!!Q1--r2c2tj||||Sr4)rM_hypergeom_cdfr9s r/rTzhypergeom_gen._cdfr:r2c~d|zd|zd|z}}}||z }||dzzd|z||z zz d|z|zz }||dz |z|zz}|d|z|z||z z|zd|zdz zz }|||z||z z|z|dz z|dz zz}tj|||tj|||tj||||fS)Nrrrfrrrer)rM_hypergeom_mean_hypergeom_variance_hypergeom_skewness)r.r&r%r'mrms r/rszhypergeom_gen._statssq&"q&"q&a1 E!a%[26QU+ +b1fqj 8 q1ukAo b1fqjAE"Q&"q&1*55 a!eq1uo!QV,B77   1a (  # #Aq! ,  # #Aq! ,    r2ctj|||z z t||dz}|j||||}tjt |dS)Nrrru)r+rwminpmfrxr)r.r&r%r'rIrys r/rzzhypergeom_gen._entropysM EE!q1u+c!Qi!m ,xx1a#vvd4jq))r2c2tj||||Sr4)rM _hypergeom_sfr9s r/rXzhypergeom_gen._sfs  Aq!,,r2c g}ttj||||D]\}}}} |dz|dzz|dz | dz zkr7|jt t |j ||||  Vtj|dz| dz} |jt|j| ||| tj|S)Nrr) zipr+rappendrrrarangerrKasarray r.rIr&r%r'rquantr3r4drawk2s r/rzhypergeom_gen._logsfs&)2+>+>q!Q+J&K I "E3d c *dSjTCZ-HH 5#dkk%dD&I"J!JKLYYuqy$(3 9T\\"c4%FGH Izz#r2c g}ttj||||D]\}}}} |dz|dzz|dz | dz zkDr7|jt t |j ||||  Vtjd|dz} |jt|j| ||| tj|S)Nrrr) rHr+rrIrrlogsfrJrrKrKrLs r/rzhypergeom_gen._logcdfs&)2+>+>q!Q+J&K I "E3d c *dSjTCZ-HH 5#djjT4&H"I!IJKYYq%!), 9T\\"c4%FGH Izz#r2rc)r~rrrr0r9rBr>rKrPrTrsrzrXrrrr2r/r$r$FsGHRC A8 .. "* -  r2r$ hypergeomc<eZdZdZdZdZdZd dZdZdZ d Z y) 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 ctdddtjfdtdddtjfdtdddtjfdgS)Nr&Trr&r%rr*r-s r/r0znhypergeom_gen._shape_infoIr(r2c d|fSrr)r.r&r%rVs r/rBznhypergeom_gen._get_supportNrr2c|dk\||kz|dk\z|||z kz}|t|t|zt|zz}|Srr<)r.r&r%rVrs r/r>znhypergeom_gen._argcheckQsOQ16"a1f-ac: AQ/+a.@@ r2Nc:tfd}||||||S)Nc& j|||\}}tj||dz} j||||}t ||dd} | |j |j t} || jS| S)Nrnext extrapolate)kindrr ) supportr+rJrr uniformrintitem) r&r%rVr7r8rArksrppfrvsr.s r/_rvs1z"nhypergeom_gen._rvs.._rvs1Xs<<1a(DAq1ac"B((2q!Q'C3MJCl***56==cBC|xxz!Jr2rr)r.r&r%rVr7r8res` r/r9znhypergeom_gen._rvsVs* #  $ Q14lCCr2cPtj|dk7|dk7z||||fddS)Nrct|dz| t||zdzt||z dz||z |z dzz t||z |z dzdzt|dz||z dzzt|dzdz SrDr2)rIr&r%rVs r/z(nhypergeom_gen._logpmf..is1a.6!A#q>1!A#a%1Qq)*,21Q3q57A,>?!A#qs1u%&(.qsA7r2r)rrr.rIr&r%rVs r/rKznhypergeom_gen._logpmffs7 !VQ !Q1 8  r2c<t|j||||Sr4rrks r/rPznhypergeom_gen._pmfos4<<1a+,,r2cd|zd|zd|z}}}||z||z dzz }||dzz|z||z dz||z dzzz d|||z dzz z z}d\}}||||fS)Nrrrrcr)r.r&r%rVrjrkrlrms r/rsznhypergeom_gen._statstsQ$1bda1 qSAaCE]1gaiAaCEAaCE?+q1!A;?B3Br2rc) r~rrrr0rBr>r9rKrPrsrr2r/rTrTs.bHC  D - r2rT nhypergeomc0eZdZdZdZddZdZdZdZy) logser_genaA 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 tddddgSrrr-s r/r0zlogser_gen._shape_inforr2Nc(|j||Sr*) logseriesrs r/r9zlogser_gen._rvss%%ad%33r2c|dkD|dkzSr;rrs r/r>zlogser_gen._argchecksA!a%  r2cjtj|| dz|z tj| z Sr)r+rrrrs r/rPzlogser_gen._pmfs.A$q(7==!+<< MM1"  !c']Q rAvS1 $RUlrAvQ37Q,.QrT$Y2q5( 288C% %rAv 1Q3(NQqSAEA:- -!A1q0@ @BQtVBY42-"a%7 36\C 3Br2rc) r~rrrr0r9r>rPrsrr2r/rprps 0>4 != r2rplogserz A logarithmiccHeZdZdZdZdZd dZdZdZdZ d Z d Z d Z y) poisson_genaA 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@tdddtjfdgS)NrjFrr&r*r-s r/r0zpoisson_gen._shape_infos4BFF ]CDDr2c |dk\Srr)r.rjs r/r>zpoisson_gen._argchecks Qwr2Nc&|j||Sr4poisson)r.rjr7r8s r/r9zpoisson_gen._rvss##B--r2cVtj||t|dzz |z }|SrD)rrFrE)r.rIrjPks r/rKzpoisson_gen._logpmfs) ]]1b !E!a%L 02 5 r2c8t|j||Sr4r)r.rIrjs r/rPzpoisson_gen._pmfs4<<2&''r2cDt|}tj||Sr4)r rpdtrr.rHrjrIs r/rTzpoisson_gen._cdfs !H||Ar""r2cDt|}tj||Sr4)r rpdtrcrs r/rXzpoisson_gen._sfs !H}}Q##r2cttj||}tj|dz d}tj ||}tj ||k\||Sr)r rpdtrikr+r.rr )r.r`rjryvals1rs r/razpoisson_gen._ppfsRGNN1b)* 4!8Q'||E2&xx 5$//r2c|}tj|}|dkD}tj||dtj}tj||dtj}||||fS)Nrctd|z SrrrHs r/riz$poisson_gen._stats..sSU r2rc d|z Srrrs r/riz$poisson_gen._stats..s Ar2)r+rKrrr,)r.rjrktmp mu_nonzerorlrms r/rszpoisson_gen._statss_jjn1W __Z.CPRPVPV W __Zo"&& Q3Br2rc) r~rrrr0r>r9rKrPrTrXrarsrr2r/rrs50E.(#$0 r2rrz A Poisson)rr"cNeZdZdZdZdZdZdZdZdZ dZ d d Z d Z d Z y ) planck_genaA 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@tdddtjfdgS)Nlambda_Frrr*r-s r/r0zplanck_gen._shape_info"s9ea[.IJJr2c |dkDSrr)r.rs r/r>zplanck_gen._argcheck%s {r2c<t|  t| |zzSr4)rr)r.rIrs r/rPzplanck_gen._pmf(s whWHQJ//r2c>t|}t| |dzz SrD)r rr.rHrrIs r/rTzplanck_gen._cdf+s# !Hwh!n%%%r2c8t|j||Sr4)rr)r.rHrs r/rXzplanck_gen._sf/s4;;q'*++r2c*t|}| |dzzSrDr rs r/rzplanck_gen._logsf2s !Hx1~r2ctd|z t| zdz }|dz j|j|}|j ||}t j ||k\||S)Nr)r rcliprBrTr+r )r.r`rryrrs r/razplanck_gen._ppf6sfDL5!9,Q./a  1 1' :<yy(xx 5$//r2NcHt|  }|j||dz S)Nr r)rr )r.rr7r8r's r/r9zplanck_gen._rvs<s+ G8_ %%ad%3c99r2cdt|z }t| t| dzz }dt|dz z}ddt|zz}||||fS)Nrrrer)rrr)r.rrjrkrlrms r/rszplanck_gen._statsAs^ uW~ 7(mUG8_q00 tGCK  qg 3Br2cXt|  }|t| z|z t|z Sr4)rrr)r.rCs r/rzzplanck_gen._entropyHs/ G8_ sG8}$Q&Q//r2rc)r~rrrr0r>rPrTrXrrar9rsrzrr2r/rrs:6K0&,0 : 0r2rplanckzA discrete exponential c:eZdZdZdZdZdZdZdZdZ dZ y ) boltzmann_genaA 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 cztdddtjfdtdddtjfdgS)NrFrrr'Tr*r-s r/r0zboltzmann_gen._shape_infofs:9ea[.I3q"&&k>BD Dr2c0|dkD|dkDzt|zSrr<r.rr's r/r>zboltzmann_gen._argcheckjs! A&Q77r2c$|j|dz fSrDr@rs r/rBzboltzmann_gen._get_supportmsvvq1u}r2cjdt| z dt| |zz z }|t| |zzSrDr)r.rIrr'facts r/rPzboltzmann_gen._pmfps<#wh-!C O"34C O##r2cht|}dt| |dzzz dt| |zz z SrD)r r)r.rHrr'rIs r/rTzboltzmann_gen._cdfvs9 !H#wh!n%%#whqj/(9::r2c |dt| |zz z}td|z td|z zdz }|dz jdtj }|j |||}t j||k\||S)Nrrrj)rr rrr+r,rTr )r.r`rr'qnewryrrs r/razboltzmann_gen._ppfzs|!C O#$DL3qv;.q01a c266*yy+xx 5$//r2ct| }t| |z}|d|z z ||zd|z z z }|d|z dzz ||z|zd|z dzz z }d|z d|z z }||dzz||z|zz }|d|zz|dzz|dz|zd|zzz } | |dzz } |dd|zz||zzz|dzz|dz|zdd|zz||zzzz } | |z |z } ||| | fS)Nrrrrrwrr) r.rr'zzNrjrktrmtrm2rlrms r/rszboltzmann_gen._statss( M '!_ AYqtQrT{ "Q lQqSVQrTAI--tacl#q&1Q3r6! !WS!V^ad2gqtn , $+  !A#ac ]36 !AqD2Iq2vbe|$< < $Y 3Br2N) r~rrrr0r>rBrPrTrarsrr2r/rrPs+*D8$ ;0 r2r boltzmannz!A truncated discrete exponential )rrAr"cHeZdZdZdZdZdZdZdZdZ dZ d d Z d Z y ) randint_genaA 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) ctddtj tjfdtddtj tjfdgS)NlowTrhighr*r-s r/r0zrandint_gen._shape_infosH5$"&&"&&(9>J64266'266):NKM Mr2c<||kDt|zt|zSr4r<r.rrs r/r>zrandint_gen._argchecks s k#..T1BBBr2c||dz fSrDrrs r/rBzrandint_gen._get_supportsDF{r2ctj|tj|tj|z z }tj||k\||kz|dS)Nrrj)r+ ones_likerKint64r )r.rIrrr's r/rPzrandint_gen._pmfsH LLOrzz$bhh?#E Fxxca$h/B77r2c4t|}||z dz||z z Srr)r.rHrrrIs r/rTzrandint_gen._cdfs" !HC" ,,r2ct|||z z|zdz }|dz j||}|j|||}tj||k\||SrD)r rrTr+r )r.r`rrryrrs r/razrandint_gen._ppfs\A$s*+a/T*yyT*xx 5$//r2ctj|tj|}}||zdz dz }||z }||zdz dz }d}d||zdzz||zdz z } |||| fS)Nrrrg(@rjg333333)r+rK) r.rrm2m1rjdrkrlrms r/rszrandint_gen._statss{D!2::c?B2gmq  GsQw$  1s #qsSy 13Br2Nctj|jdk(r1tj|jdk(rt||||S|,tj||}tj||}tj t t|tjtg}|||S)z=An array of *size* random integers >= ``low`` and < ``high``.rr )otypes) r+rKr7r broadcast_to vectorizerrr`)r.rrr7r8randints r/r9zrandint_gen._rvss ::c?  1 $D)9)>)>!)C c4dC C   //#t,C??4.D,,w|\B')xx}o7sD!!r2ct||z Sr4)rrs r/rzzrandint_gen._entropys4#:r2rc) r~rrrr0r>rBrPrTrarsr9rzrr2r/rrs7=~MC8 -0 ""r2rrz#A discrete uniform (random integer)c0eZdZdZdZddZdZdZdZy) zipf_genaA 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@tdddtjfdgS)NrAFrrr*r-s r/r0zzipf_gen._shape_info;3266{NCDDr2Nc(|j||Sr*)zipf)r.rAr7r8s r/r9z zipf_gen._rvs>s   ..r2c |dkDSrDrr.rAs r/r>zzipf_gen._argcheckAs 1u r2c|jtj}dtj|dz || zz}|SNrr)rr+float64rr )r.rIrArs r/rPz zipf_gen._pmfDs9 HHRZZ  7<<1% %A2 - r2cbtj||dzkD||fdtjS)Nrcbtj||z dtj|dz SrD)rr )rAr%s r/riz zipf_gen._munp..Ms%a!eQ/',,q!2DDr2rr)r.r%rAs r/_munpzzipf_gen._munpJs. AI1v Dvv r2rc) r~rrrr0r9r>rPrrr2r/rrs")VE/ r2rrzA Zipfc:t|dt||dzz S)z"Generalized harmonic number, a > 1r)r r%rAs r/_gen_harmonic_gt1rTs 1:Q! $$r2cZtj|s|Stj|}tj|t}tj |ddtD]}||k}||xxd|||zz z cc<tj |tj|<|S)z#Generalized harmonic number, a <= 1rrr)r+r7nanmax zeros_likefloatrJnanisnan)r%rAn_maxoutimasks r/_gen_harmonic_leq1rZs 771: IIaLE -- 'C YYua5 1"Av D Qq!D'z\! "vvC  Jr2c~tj||\}}tj|dkD||ftt S)zGeneralized harmonic numberr)r+rrrrrrs r/ _gen_harmonicrhs7  q! $DAq ??1q51a&*;=O PPr2c:eZdZdZdZdZdZdZdZdZ dZ y ) zipfian_genaA 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 cztdddtjfdtdddtjfdgS)NrAFrr&r%Trr*r-s r/r0zzipfian_gen._shape_infos:3266{MB3q"&&k>BD Dr2cV|dk\|dkDz|tj|tk(zS)Nrr)r+rKr`r.rAr%s r/r>zzipfian_gen._argchecks*Q1q5!Q"**Qc*B%BCCr2c d|fSrDrrs r/rBzzipfian_gen._get_supportrr2cl|jtj}dt||z || zzSr)rr+rrr.rIrAr%s r/rPzzipfian_gen._pmfs1 HHRZZ ]1a((1qb500r2c^tj|}t||t||z Sr4r+r rrs r/rTzzipfian_gen._cdfs( HHQKQ"]1a%888r2ctj|dz}||zt||t||z zdz||zt||zz SrDrrs r/rXzzipfian_gen._sfsT HHQUOA}Q*]1a-@@AAEa4 a++- .r2ct||}t||dz }t||dz }t||dz }t||dz }||z }||z|dzz } |dz} | | z } ||z d|z|z|dzz z d|dzz|dzz z| dzz } |dz|zd|dzz|z|zz d|z|dzz|zzd|dzzz | dzz } | dz} || | | fS)Nrrrrrwr)r)r.rAr%HnaHna1Hna2Hna3Hna4mu1mu2nmu2dmu2rlrms r/rszzipfian_gen._statss&Aq!Q!$Q!$Q!$Q!$3hS47"AvTk3h4S!V++aaiQ.>>c J1fTkAc1fHTM$..3tQwt1CC$' !1W% aCRr2N) r~rrrr0r>rBrPrTrXrsrr2r/rrns-,\DD19.  r2rzipfianz A Zipfianc<eZdZdZdZdZdZdZdZdZ d d Z y) dlaplace_genaLA 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@tdddtjfdgS)NrAFrrr*r-s r/r0zdlaplace_gen._shape_inforr2cPt|dz t| t|zzSNre)rrabs)r.rIrAs r/rPzdlaplace_gen._pmfs$AcE{S!c!f---r2c^t|}d}d}tj|dk\||f||S)NcDdt| |zt|dzz z SrrrIrAs r/rzdlaplace_gen._cdf..f1s$aR!VA 33 3r2cBt||dzzt|dzz SrDrrs r/f2zdlaplace_gen._cdf..f2s"qAE{#s1vz2 2r2r)r rr)r.rHrArIrrs r/rTzdlaplace_gen._cdfs4 !H 4 3qAv1vr266r2c ,dt|z}ttj|ddt| zz kt ||z|z dz t d|z |z |z }|dz }tj|j |||k\||S)Nrr)rr r+r rrT)r.r`rAconstryrs r/razdlaplace_gen._ppfsCF BHHQCG !44 5\A-1!1Q3%-001467qxx %+q0%>>r2ct|}d|z|dz dzz }d|z|dzd|zzdzz|dz dzz }d|d||dzz dz fS)Nrerrg$@rrjrr)r.rAearr|s r/rszdlaplace_gen._statssg VeRUQJeRU3r6\"_%B 23CQJO++r2cN|t|z tt|dz z Sr )rrrrs r/rzzdlaplace_gen._entropys"47{Sae---r2Nctjtj|  }|j||}|j||}||z Sr*)r+rrKr )r.rAr7r8 probOfSuccessrHys r/r9zdlaplace_gen._rvssR 2::a=.11  " "=t " <  " "=t " <1u r2rc) r~rrrr0rPrTrarsrzr9rr2r/r r s+,E. 7?, .r2r dlaplacezA discrete LaplaciancHeZdZdZdZdZddddZdZdZd Z d Z d Z y) poisson_binom_genulA Poisson Binomial discrete random variable. %(before_notes)s See Also -------- binom Notes ----- The probability mass function for `poisson_binom` is: .. math:: f(k; p_1, p_2, ..., p_n) = \sum_{A \in F_k} \prod_{i \in A} p_i \prod_{j \in A^C} 1 - p_j where :math:`k \in \{0, 1, \dots, n-1, n\}`, :math:`F_k` is the set of all subsets of :math:`k` integers that can be selected :math:`\{0, 1, \dots, n-1, n\}`, and :math:`A^C` is the complement of a set :math:`A`. `poisson_binom` accepts a single array argument ``p`` for shape parameters :math:`0 ≤ p_i ≤ 1`, where the last axis corresponds with the index :math:`i` and any others are for batch dimensions. Broadcasting behaves according to the usual rules except that the last axis of ``p`` is ignored. Instances of this class do not support serialization/unserialization. %(after_notes)s References ---------- .. [1] "Poisson binomial distribution", Wikipedia, https://en.wikipedia.org/wiki/Poisson_binomial_distribution .. [2] Biscarri, William, Sihai Dave Zhao, and Robert J. Brunner. "A simple and fast method for computing the Poisson binomial distribution function". Computational Statistics & Data Analysis 122 (2018) 92-100. :doi:`10.1016/j.csda.2018.01.007` %(example)s cgSr4rr-s r/r0zpoisson_binom_gen._shape_infoHs  r2cttj|d}d|k|dkz}tj|dSNrrur)r+stackall)r.argsr'condss r/r>zpoisson_binom_gen._argcheckMs5 HHT "aAF#vve!$$r2Nrc*tj|d}| |jn&tj|r|dfn t |dz}tj |j|}t j|||jdS)Nrrur)rr) r+r!shapeisscalartuplebroadcast_shapesrr9rx)r.r7r8r#r's r/r9zpoisson_binom_gen._rvsRs{ HHT # <[[.q E$K$4F ""177D1~~ad~FJJPRJSSr2cdt|fSr)len)r.r#s r/rBzpoisson_binom_gen._get_support\s#d)|r2ctj|jtj}tj|g|^}}tj |tj }t||dS)NrrDr+ atleast_1drrrrKrr r.rIr#s r/rPzpoisson_binom_gen._pmf_[ MM!  # #BHH -&&q040Dzz$bjj1au--r2ctj|jtj}tj|g|^}}tj |tj }t||dS)Nrrr-r/s r/rTzpoisson_binom_gen._cdfer0r2ctj|d}tj|d}tj|d|z zd}||ddfSr )r+r!rx)r.r#kwdsr'rrks r/rszpoisson_binom_gen._statsksI HHT "vvaa ffQ!A#YQ'c4&&r2c t|g|i|Sr4)poisson_binomial_frozen)r.r#r3s r/__call__zpoisson_binom_gen.__call__qs&t;d;d;;r2) r~rrrr0r>r9rBrPrTrsr6rr2r/rrs8'P % $$T. . ' }s# QA& 'c4 77r2cJttj|dd|d|fSr:r;)r.r'r=ris r/_parse_args_statsr@s# QA& 'c7 ::r2cHttj|dd|dfSr:r;)r.r'r=s r/ _parse_argsrBs! QA& 'c 11r2ceZdZdZddZy)r5c||_||_|jdi|j|_t j tt|j_tj tt|j_ tj tt|j_ |jj|i|\}}}|jj|\|_ |_ y)Nr)r#r3 __class___updated_ctor_paramdistr>__get___pb_obj_pb_clsr@rBrBrAr)r.rGr#r3r8_s r/__init__z poisson_binomial_frozen.__init__s  #DNN@T%=%=%?@ %4$;$;GW$M !&7&?&?&Q # + 3 3GW E ,tyy,,d;d; 1///8r2Nc |jj|ji|j\}}}|jj||j||||fi|Sr4)rGrBr#r3expect) r.funclbub conditionalr3rAr=scales r/rNzpoisson_binomial_frozen.expectsW- --tyyFDIIF 3 tyydiib"kRTRRr2)NNNF)r~rrrLrNrr2r/r5r5s  9Sr2r5c0eZdZdZdZddZdZdZdZy) skellam_genaA 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 cztdddtjfdtdddtjfdgS)NrFrrrr*r-s r/r0zskellam_gen._shape_infos:5%!RVVnE5%!RVVnEG Gr2NcP|}|j|||j||z Sr4r)r.rrr7r8r%s r/r9zskellam_gen._rvss1 $$S!,$$S!,- .r2c "tjd5tj|dktjd|zdd|z zd|zdztjd|zdd|zzd|zdz}ddd|S#1swYSxYw)Nrrrrr)r+rr rM _ncx2_pdfr.rHrrpxs r/rPzskellam_gen._pmfs [[h ' B!a%--#q!A#w#>q@--#q!A#w#>q@BB B   B  s A#BBc ,t|}tjd5tj|dkt j d|zd|zd|zdt j d|zd|dzzd|zz }ddd|S#1swYSxYw)Nrrrrr)r r+rr rM _ncx2_cdfrZs r/rTzskellam_gen._cdfs !H [[h ' D!a%--#r!tQsU;cmmAcE1ac7AcEBBDB D  D s AB  BcN||z }||z}|t|dzz }d|z }||||fS)Nrrr)r.rrrrkrlrms r/rszskellam_gen._statss>SyCi D#N " WS"b  r2rc) r~rrrr0r9rPrTrsrr2r/rUrUs!:G. !r2rUskellamz A SkellamcHeZdZdZdZd dZdZdZdZdZ d Z d Z d Z y) yulesimon_genaA 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@tdddtjfdgS)NalphaFrrr*r-s r/r0zyulesimon_gen._shape_info s7EArvv;GHHr2Nc |j|}|j|}t| tt| |z  z }|Sr4)standard_exponentialr rr)r.rdr7r8E1E2anss r/r9zyulesimon_gen._rvs sK  . .t 4  . .t 4B3RC%K 00112 r2c:|tj||dzzSrDrrr.rHrds r/rPzyulesimon_gen._pmfsw||Auqy111r2c |dkDSrr)r.rds r/r>zyulesimon_gen._argchecks  r2cLt|tj||dzzSrDrrrrls r/rKzyulesimon_gen._logpmfs 5zGNN1eai888r2c@d|tj||dzzz SrDrkrls r/rTzyulesimon_gen._cdfs!1w||Auqy1111r2c:|tj||dzzSrDrkrls r/rXzyulesimon_gen._sfs7<<519---r2cLt|tj||dzzSrDrorls r/rzyulesimon_gen._logsf!s 1vq%!)444r2ctj|dktj||dz z }tj|dkD|dz|dz |dz dzzz tj}tj|dktj|}tj|dkDt |dz |dzdzz||dz zz tj}tj|dktj|}tj|dkD|dzd|dzzd|zz dz ||dz z|dz zz ztj}tj|dktj|}||||fS) Nrrrerr 1)r+r r,rr)r.rdrjrrlrms r/rszyulesimon_gen._stats$s[ XXeqj"&&%519*= >hhuqyaxECKEAI>#ABvvhhuz2663/ XXeai519oQ6%519:MNffXXeqj"&&" - XXeaiaiBMBJ$>$C$)UQY$7519$E$GHffXXeqj"&&" -3Br2rc) r~rrrr0r9rPr>rKrTrXrrsrr2r/rbrbs6 BI 292.5r2rb yulesimon)rrAc@eZdZdZdZdZdZdZdZd dZ dZ d dZ y) _nchypergeom_genzA noncentral hypergeometric discrete random variable. For subclassing by nchypergeom_fisher_gen and nchypergeom_wallenius_gen. Nc tdddtjfdtdddtjfdtdddtjfdtdddtjfd gS) Nr&Trr&r%r'oddsFrr*r-s r/r0z_nchypergeom_gen._shape_infoCsf3q"&&k=A3q"&&k=A3q"&&k=A651bff+~FH Hr2c~|||}}}||z }tjd||z }tj||}||fSrr-) r.r&r%r'r{rrx_minx_maxs r/rBz_nchypergeom_gen._get_supportIsFaq2 V 1a"f% 1b!e|r2c(tj|tj|}}tj|tj|}}tj||jt|k(z|dk\z}tj||jt|k(z|dk\z}tj||jt|k(z|dk\z}|dkD}||k} ||k} ||z|z|z| z| zSr)r+rKrrr`) r.r&r%r'r{cond1cond2cond3cond4cond5cond6s r/r>z_nchypergeom_gen._argcheckPszz!}bjjm1**Q-D!14((1+!((3-1"45a@((1+!((3-1"45a@((1+!((3-1"45a@qQQu}u$u,u4u<._rvs1]sxx{RXXa[(288A;6wwtRVV,,WWT]F#%CS$--0FAq$ =C++d#CJr2rrf)r.r&r%r'r{r7r8res` r/r9z_nchypergeom_gen._rvs[s, #  $ Q1dLIIr2ctj|||||\}}}}}|jdk(rtj|Stjfd}||||||S)Nrctj|tj|ztj|ztj|zrtjSj||||d}|j |SNg-q=)r+rrrG probability)rHr&r%r'r{rr.s r/_pmf1z$_nchypergeom_gen._pmf.._pmf1pscxx{RXXa[(288A;6!Dvv ))Aq!T51C??1% %r2)r+rr7 empty_liker)r.rHr&r%r'r{rs` r/rPz_nchypergeom_gen._pmfjsk..q!Q4@1aD 66Q;==# #  &  & Q1a&&r2cztjfd}d|vsd|vr |||||nd\}}d\} } ||| | fS)Nctj|tj|ztj|zr tjtjfSj||||d}|j Sr)r+rrrGri)r&r%r'r{rr.s r/ _moments1z*_nchypergeom_gen._stats.._moments1{s\xx{RXXa[(288A;6vvrvv~%))Aq!T51C;;= r2rAvrc)r+r) r.r&r%r'r{rirrArrdrIs ` r/rsz_nchypergeom_gen._statsysX  !  ! .1G^sg~ !Q4(! 11!Qzr2rcr{) r~rrrrrGr0rBr>r9rPrsrr2r/ryry9s3 H DH  = J ' r2ryceZdZdZdZeZy)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)r~rrrrrrGrr2r/rrsGRH %Dr2rnchypergeom_fisherz$A Fisher's noncentral hypergeometricceZdZdZdZeZy)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)r~rrrrrrGrr2r/rrsGRH 'Dr2rnchypergeom_walleniusz&A Wallenius' noncentral hypergeometric)rN)rr|)r)m functoolsrscipyr scipy.specialrrrrrEr scipy._lib._utilr scipy._lib.array_api_extra_libarray_api_extrarscipy.interpolater numpyr r rrrrrrrrr+_distn_infrastructurerrrrrr _biasedurnrrr_stats_pythranr scipy.special._ufuncs_ufuncsrMr"rrrrrrrrrrr!r$rRrTrnrpr}rrrrrrrrrrrrrrrr r,rrr7r>r@rBrIrJrHr5rUr`rbrwryrrrrlistglobalscopyitemspairs _distn_names_distn_gen_names__all__rr2r/rs II)((&MMM88,,+##]* ]*@ w>#I>#B AK 0 OKOd { + r r j  "Z[Zz . N7{N7b!&=9XKXv { + [[[| . 55p ah A?+?D 9{ ;D0D0N ah1J K<K<~ {a#F H t+tn 90) * ?{?D!&84% Q W +W t  K @M;M` 266''2H JS< S