U e5d @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_finiteAttributeErrormathZisfinite)xr&r&r' _isfinites rBcCs||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) r2rEras_integer_ratio numerator denominatorr? OverflowError ValueErrorrFformatr#)rArHr&r&r'r.s r.cCspt||kr|St|tr(|jdkr(t}z ||WStk rjt|trd||j||jYSYnXdS)Nr()r2rDr1rKrErFrrJ)valuer:r&r&r'_converts   rPcCs.t||}|t|kr&|||kr&|StdSr))rlenrM)arAir&r&r' _find_lteq s rTcCs>t|||d}|t|dkr6||d|kr6|dStdS)N)lor()rrQrM)rRlrArSr&r&r' _find_rteqs rWnegative valueccs$|D]}|dkrt||VqdS)Nr)r)r<errmsgrAr&r&r' _fail_negsrZcCsHt||krt|}t|}|dkr,tdt|\}}}t|||S)Nr(z%mean requires at least one data point)iterlistrQrr>rP)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)iterablerAr,r&r'r9Oszfmean..countz&fmean requires at least one data point)rQrFr ZeroDivisionErrorr)r7r9r=r&r_r'rAs    cCs8ztttt|WStk r2tddYnXdS)NzHgeometric mean requires a non-empty dataset containing positive numbers)rrr3rrMr)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*rAr&r&r'r-sz harmonic_mean..) r[r\rQr isinstancenumbersZRealrrFr>rZr`rP)r7rYr,rAr:r=r9r&r&r'ros$  "cCs\t|}t|}|dkr td|ddkr8||dS|d}||d||dSdSNrno median for empty datar(r5rQr)r7r,rSr&r&r'rs   cCsLt|}t|}|dkr td|ddkr8||dS||ddSdSrdrgr7r,r&r&r'r s   cCs,t|}t|}|dkr td||dS)Nrrerfrgrhr&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)Nrrer(rfzexpected number but got %r) r5rQrrbstrbytesrFrErTrW) r7Zintervalr,rAobjLl1l2Zcffr&r&r'rs&      cCsHt|}t|d}z|ddWStk rBtddYnXdS)Nr(rzno mode for empty data)r[r" most_common IndexErrorr)r7Zpairsr&r&r'r s cCs@tt|}tt|tdddgf\}}tttd|S)Nr()keyr)r"r[rpnextrr!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 1rfz"must have at least two data pointsZ inclusiveruzUnknown method: )rr5rQrangeappendrM) r7r,rvZldmresultrSjZdeltaZ interpolatedr&r&r'rls4 $   $$ csdk r,tfdd|D\}}}||fSt|tfdd|D\}}}tfdd|D\}}}||dt|8}||fS)Nc3s|]}|dVqdSrfNr&racr&r'r-sz_ss..c3s|]}|dVqdSr|r&rar}r&r'r-sc3s|]}|VqdSr)r&rar}r&r'r-srf)r>rrQ)r7r~r:r=r9UZtotal2Zcount2r&r}r'_sssrcCsLt||krt|}t|}|dkr,tdt||\}}t||d|S)Nrfz*variance requires at least two data pointsr(r[r\rQrrrP)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)prsigmaqrZnumZdenrAr&r&r'_normal_dist_inv_cdf9sd      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)rrErr)selfrrr&r&r'__init__s zNormalDist.__init__cCs.t|ttfst|}t|}||t||Sr))rbr\tuplerr)clsr7rr&r&r' from_samplesszNormalDist.from_samplesN)seedcsB|dkrtjn t|j|j|jfddt|DS)Ncsg|]}qSr&r&r*rSgaussrrr&r' sz&NormalDist.samples..)randomrZRandomrrrw)rr,rr&rr'samplesszNormalDist.samplescCs<|jd}|stdt||jdd|tt|S)N@z$pdf() not defined when sigma is zerog)rrrrrr)rrArr&r&r'pdfs zNormalDist.pdfcCs2|jstdddt||j|jtdS)Nz$cdf() not defined when sigma is zerorrr)rrrrr)rrAr&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_cdfrtcsfddtdDS)Ncsg|]}|qSr&)rrr,rr&r'rsz(NormalDist.quantiles..r()rw)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) rbrrFrrrrrrrrr) rotherXYZX_varZY_varZdvZdmrRbx1x2r&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)rbrrrrrrr&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__5szNormalDist.__pos__cCst|j |jSr)rrr&r&r'__neg__9szNormalDist.__neg__cCs || Sr)r&rr&r&r'__rsub__?szNormalDist.__rsub__cCs&t|tstS|j|jko$|j|jkSr))rbrNotImplementedrrrr&r&r'__eq__Es zNormalDist.__eq__cCst|j|jfSr))hashrrrr&r&r'__hash__KszNormalDist.__hash__cCs t|jd|jd|jdS)Nz(mu=z, sigma=))r2r#rrrr&r&r'__repr__OszNormalDist.__repr__)rr)rt)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_closesriI/g`@@cCsg|] }|tqSr&srar&r&r'rsrcCsg|] }|tqSr&rrar&r&r'rscCsg|] }|tqSr&rrar&r&r'rscCsg|] }|tqSr&rrar&r&r'rscCsg|]\}}||qSr&r&r*rAyr&r&r'rscCsg|]\}}||qSr&r&rr&r&r'rs)r)rX)r()N)N)N)N)N)Q__all__r@rcrZ fractionsrZdecimalr itertoolsrZbisectrrrrrrrrrr operatorr! collectionsr"rMrr>rBr0r.rPrTrWrZrrrrrr r rr r rrrrrr rrZ _statistics ImportErrorr#rrrrrrZdoctestZg1Zg2r,rrrfuncprintrr3ZconstrrrrrGzipZtestmodr&r&r&r'Ts   (   :  / 779  / ,  JQ