U AT^@sjdddddddddd d d d d dddgZddlZddlZddlZddlmZddlmZddlm Z ddl m Z m Z ddlm Z mZmZmZmZmZmZmZddlmZddlmZGdddeZdcddZddZdd Zd!d"Zd#d$Zd%d&Z d'd(Z!ddd*d+Z"d,dZ#d-dZ$d.dZ%d/dZ&d0dZ'd1d Z(d2dZ)ded4dZ*d5d Z+d6d Z,d7d8d9d:dZ-dfd;d<Z.dgd=dZ/dhd>d Z0did?dZ1djd@d Z2dAdBZ3GdCddZ4zddDl5m3Z3Wne6k rYnXe7dEkrfddFlm8Z8ddGlm9Z9m:Z:m;Z;mZ>e4dIdJZ?e4dKdLZ@dMZAe?BeAZCe@BeAZDe9e:fD]PdS)k NormalDistStatisticsErrorfmeangeometric_mean harmonic_meanmeanmedianmedian_grouped median_high median_lowmode multimodepstdev pvariance quantilesstdevvarianceNFraction)Decimal)groupby) bisect_left bisect_right)hypotsqrtfabsexperftaulogfsum) itemgetter)Counterc@s eZdZdS)rN)__name__ __module__ __qualname__r&r&"/usr/lib64/python3.8/statistics.pyrusc Csd}t|\}}||i}|j}ttt|}t|tD]@\}} t||}tt| D]"\}}|d7}||d|||<qRq6d|kr|d} ntddt| D} || |fS)Nrcss|]\}}t||VqdSNr).0dnr&r&r' sz_sum..) _exact_ratioget_coerceinttypermapsumsorteditems) datastartcountr,r+ZpartialsZ partials_getTtypvaluestotalr&r&r'_sum{s$  r>cCs.z |WStk r(t|YSXdSr))Z is_finiteAttributeErrormathisfinite)xr&r&r' _isfinites rCcCs||kr |S|tks|tkr |S|tkr,|St||r:|St||rH|St|trV|St|trd|St|tr|t|tr||St|trt|tr|Sd}t||j|jfdS)Nz"don't know how to coerce %s and %s)r1bool issubclassrfloat TypeErrorr#)r:Smsgr&r&r'r0s(     r0c Cszrt|tkst|tkr$|WSz|j|jfWWStk rnz|WYWStk rhYnXYnXWn ttfk r|dfYSXd}t | t|j dS)Nz0can't convert type '{}' to numerator/denominator) r2rFras_integer_ratio numerator denominatorr? OverflowError ValueErrorrGformatr#)rBrIr&r&r'r.s r.cCspt||kr|St|tr(|jdkr(t}z ||WStk rjt|trd||j||jYSYnXdS)Nr()r2rEr1rLrFrGrrK)valuer:r&r&r'_converts   rQcCs.t||}|t|kr&|||kr&|StdSr))rlenrN)arBir&r&r' _find_lteq s rUcCs>t|||d}|t|dkr6||d|kr6|dStdS)N)lor()rrRrN)rSlrBrTr&r&r' _find_rteqs rXnegative valueccs$|D]}|dkrt||VqdS)Nr)r)r<errmsgrBr&r&r' _fail_negsr[cCsHt||krt|}t|}|dkr,tdt|\}}}t|||S)Nr(z%mean requires at least one data point)iterlistrRrr>rQ)r7r,r:r=r9r&r&r'r's cstz t|Wn0tk r<dfdd}t||}Yn Xt|}z |WStk rntddYnXdS)Nrc3s t|ddD]\}|Vq dS)Nr()r8) enumerate)iterablerBr,r&r'r9Oszfmean..countz&fmean requires at least one data point)rRrGr ZeroDivisionErrorr)r7r9r=r&r`r'rAs    cCs8ztttt|WStk r2tddYnXdS)NzHgeometric mean requires a non-empty dataset containing positive numbers)rrr3rrNr)r7r&r&r'r\s cCst||krt|}d}t|}|dkr2tdn<|dkrn|d}t|tjtfrf|dkrbt||Stdz"t ddt ||D\}}}Wnt k rYdSXt |||S)Nz.harmonic mean does not support negative valuesr(z.harmonic_mean requires at least one data pointrzunsupported typecss|]}d|VqdS)r(Nr&r*rBr&r&r'r-sz harmonic_mean..) r\r]rRr isinstancenumbersZRealrrGr>r[rarQ)r7rZr,rBr:r=r9r&r&r'ros$  "cCs\t|}t|}|dkr td|ddkr8||dS|d}||d||dSdSNrno median for empty datar(r5rRr)r7r,rTr&r&r'rs   cCsLt|}t|}|dkr td|ddkr8||dS||ddSdSrerhr7r,r&r&r'r s   cCs,t|}t|}|dkr td||dS)Nrrfrgrhrir&r&r'r s r(c Cst|}t|}|dkr"tdn|dkr2|dS||d}||fD]}t|ttfrFtd|qFz||d}Wn(tk rt|t|d}YnXt||}t |||}|}||d} |||d|| S)Nrrfr(rgzexpected number but got %r) r5rRrrcstrbytesrGrFrUrX) r7intervalr,rBobjLl1l2cffr&r&r'rs&      cCsHt|}t|d}z|ddWStk rBtddYnXdS)Nr(rzno mode for empty data)r\r" most_common IndexErrorr)r7pairsr&r&r'r s cCs@tt|}tt|tdddgf\}}tttd|S)Nr()keyr)r"r\rsnextrr!r]r3)r7ZcountsZmaxcountZ mode_itemsr&r&r'r 5s  exclusive)r,methodc CsL|dkrtdt|}t|}|dkr0td|dkr|d}g}td|D]N}|||}||||}||||||d||} || qN|S|dkr:|d}g}td|D]r}|||}|dkrdn||dkr|dn|}||||}||d||||||} || q|Std|dS)Nr(zn must be at least 1rgz"must have at least two data pointsZ inclusiveryzUnknown method: )rr5rRrangeappendrN) r7r,rzZldmresultrTjZdeltaZ interpolatedr&r&r'rls4 $   $$ csddkrt|tfdd|D\}}}tfdd|D\}}}||dt|8}||fS)Nc3s|]}|dVqdS)rgNr&rbcr&r'r-sz_ss..c3s|]}|VqdSr)r&rbrr&r'r-srg)rr>rR)r7rr:r=r9UZtotal2Zcount2r&rr'_sss rcCsLt||krt|}t|}|dkr,tdt||\}}t||d|S)Nrgz*variance requires at least two data pointsr(r\r]rRrrrQ)r7xbarr,r:ssr&r&r'rs& cCsHt||krt|}t|}|dkr,tdt||\}}t|||S)Nr(z*pvariance requires at least one data pointr)r7mur,r:rr&r&r'rs# cCs8t||}z |WStk r2t|YSXdSr))rrr?r@)r7rvarr&r&r'rs  cCs8t||}z |WStk r2t|YSXdSr))rrr?r@)r7rrr&r&r'r $s  cCs|d}t|dkrd||}d|d|d|d|d|d |d |d |}d |d |d|d|d|d|d|d}||}|||S|dkr|nd|}tt| }|dkr^|d}d|d|d|d|d|d|d|d}d|d |d!|d"|d#|d$|d%|d}n|d}d&|d'|d(|d)|d*|d+|d,|d-}d.|d/|d0|d1|d2|d3|d4|d}||}|dkr| }|||S)5N?g333333?gQ?g^}o)@gE.kR@g Ul@g*u>l@gN@g"]Ξ@gnC`@gu @giK~j@gv|E@gd|1@gfRr@gu.2@g~y@gn8(E@?g@g?g鬷ZaI?ggElD?g7\?guSS?g=. @gj%b@gHw@gjRe?g9dh? >g('߿A?g~z ?g@3?gɅ3?g3fRx?gIFl@gt>g*Yn>gESB\T?gN;A+?gUR1?gEF?gPn@g&>@gigtcI,\>gŝI?g*F2v?gC4?gO1?)rrr)prsigmaqrnumZdenrBr&r&r'_normal_dist_inv_cdf7sd      rc@seZdZdddZd7ddZedd Zd d d d ZddZddZ ddZ d8ddZ ddZ e ddZe ddZe ddZe dd Ze d!d"Zd#d$Zd%d&Zd'd(Zd)d*Zd+d,Zd-d.ZeZd/d0ZeZd1d2Zd3d4Zd5d6Zd S)9rz(Arithmetic mean of a normal distributionz+Standard deviation of a normal distribution)_mu_sigmarrcCs(|dkrtdt||_t||_dS)Nrzsigma must be non-negative)rrFrr)selfrrr&r&r'__init__s zNormalDist.__init__cCs.t|ttfst|}t|}||t||Sr))rcr]tuplerr)clsr7rr&r&r' from_samplesszNormalDist.from_samplesN)seedcsB|dkrtjn t|j|j|jfddt|DS)Ncsg|]}qSr&r&r*rTgaussrrr&r' sz&NormalDist.samples..)randomrRandomrrr{)rr,rr&rr'samplesszNormalDist.samplescCs<|jd}|stdt||jdd|tt|S)N@z$pdf() not defined when sigma is zerog)rrrrrr)rrBrr&r&r'pdfs zNormalDist.pdfcCs2|jstdddt||j|jtdS)Nz$cdf() not defined when sigma is zerorrr)rrrrr)rrBr&r&r'cdfszNormalDist.cdfcCs:|dks|dkrtd|jdkr*tdt||j|jS)Nrrz$p must be in the range 0.0 < p < 1.0z-cdf() not defined when sigma at or below zero)rrrr)rrr&r&r'inv_cdfs  zNormalDist.inv_cdfrxcsfddtdDS)Ncsg|]}|qSr&)rrr,rr&r'rsz(NormalDist.quantiles..r()r{)rr,r&rr'rs zNormalDist.quantilesc Cst|tstd||}}|j|jf|j|jfkr>||}}|j|j}}|rT|s\td||}t|j|j}|sdt|d|jt dS|j||j|}|j|jt |d|t ||} || |} || |} dt| | | | t| | | | S)Nz$Expected another NormalDist instancez(overlap() not defined when sigma is zerorr) rcrrGrrrrrrrrr) rotherXYZX_varZY_varZdvZdmrSbx1x2r&r&r'overlaps"   (  zNormalDist.overlapcCs|jSr)rrr&r&r'rszNormalDist.meancCs|jSr)rrr&r&r'rszNormalDist.mediancCs|jSr)rrr&r&r'r szNormalDist.modecCs|jSr)rrr&r&r'rszNormalDist.stdevcCs |jdS)Nrrrr&r&r'rszNormalDist.variancecCs8t|tr&t|j|jt|j|jSt|j||jSr)rcrrrrrrr&r&r'__add__s zNormalDist.__add__cCs8t|tr&t|j|jt|j|jSt|j||jSr)rrr&r&r'__sub__s zNormalDist.__sub__cCst|j||jt|Sr)rrrrrr&r&r'__mul__#szNormalDist.__mul__cCst|j||jt|Sr)rrr&r&r' __truediv__+szNormalDist.__truediv__cCst|j|jSr)rrrrr&r&r'__pos__3szNormalDist.__pos__cCst|j |jSr)rrr&r&r'__neg__7szNormalDist.__neg__cCs || Sr)r&rr&r&r'__rsub__=szNormalDist.__rsub__cCs&t|tstS|j|jko$|j|jkSr))rcrNotImplementedrrrr&r&r'__eq__Cs zNormalDist.__eq__cCst|j|jfSr))hashrrrr&r&r'__hash__IszNormalDist.__hash__cCs t|jd|jd|jdS)Nz(mu=z, sigma=))r2r#rrrr&r&r'__repr__MszNormalDist.__repr__)rr)rx)r#r$r% __slots__r classmethodrrrrrrrpropertyrrr rrrrrrrr__radd__r__rmul__rrrr&r&r&r'rsD   "     )r__main__)isclose)addsubmultruediv)repeat iz Test z with another NormalDist: z with a constant:z Test constant with :cCsdSr)r&)G1G2r&r&r' assert_close}sriI/g`@@cCsg|] }|tqSr&srbr&r&r'rsrcCsg|] }|tqSr&rrbr&r&r'rscCsg|] }|tqSr&rrbr&r&r'rscCsg|] }|tqSr&rrbr&r&r'rscCsg|]\}}||qSr&r&r*rByr&r&r'rscCsg|]\}}||qSr&r&rr&r&r'rs)r)rY)r()N)N)N)N)N)Q__all__r@rdrZ fractionsrZdecimalr itertoolsrbisectrrrrrrrrrr operatorr! collectionsr"rNrr>rCr0r.rQrUrXr[rrrrrr r rr r rrrrrr rrZ _statistics ImportErrorr#rrrrrrdoctestZg1Zg2r,rrrfuncprintrr3constrrrrrHziptestmodr&r&r&r'Ts   (   :  / 779  / ,  JQ