U AT^c@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_ratioZget_coerceinttypermapZsumsortedZitems) datastartcountr+r*ZpartialsZ partials_getTZtypvaluestotalr%r%r&_sum{s$  r9cCs.z |WStk r(t|YSXdSr()Z is_finiteAttributeErrormathZisfinite)xr%r%r& _isfinites r=cCs||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)r/Zbool issubclassrfloat TypeErrorr")r6Smsgr%r%r&r.s(     r.c Cszrt|tkst|tkr$|WSz|j|jfWWStk rnz|WYWStk rhYnXYnXWn ttfk r|dfYSXd}t | t|j dS)Nz0can't convert type '{}' to numerator/denominator) r0r?rZas_integer_ratio numerator denominatorr:Z OverflowError ValueErrorr@Zformatr")r<rBr%r%r&r-s r-cCspt||kr|St|tr(|jdkr(t}z ||WStk rjt|trd||j||jYSYnXdS)Nr')r0r>r/rDr?r@rrC)Zvaluer6r%r%r&_converts   rFcCs.t||}|t|kr&|||kr&|StdSr()rlenrE)ar<ir%r%r& _find_lteq s rJcCs>t|||d}|t|dkr6||d|kr6|dStdS)N)Zlor')rrGrE)rHZlr<rIr%r%r& _find_rteqs rKnegative valueccs$|D]}|dkrt||VqdS)Nr)r)r7errmsgr<r%r%r& _fail_negsrNcCsHt||krt|}t|}|dkr,tdt|\}}}t|||S)Nr'z%mean requires at least one data point)iterlistrGrr9rF)r3r+r6r8r5r%r%r&r's cstz t|Wn0tk r<dfdd}t||}Yn Xt|}z |WStk rntddYnXdS)Nrc3s t|ddD]\}|Vq dS)Nr')r4)Z enumerate)Ziterabler<r+r%r&r5Oszfmean..countz&fmean requires at least one data point)rGr@rZeroDivisionErrorr)r3r5r8r%rQr&rAs    cCs8ztttt|WStk r2tddYnXdS)NzHgeometric mean requires a non-empty dataset containing positive numbers)rrr1rrEr)r3r%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)r<r%r%r&r,z harmonic_mean..) rOrPrGr isinstancenumbersZRealrr@r9rNrRrF)r3rMr+r<r6r8r5r%r%r&ros$  "cCs\t|}t|}|dkr td|ddkr8||dS|d}||d||dSdSNrno median for empty datar'r2rGr)r3r+rIr%r%r&rs   cCsLt|}t|}|dkr td|ddkr8||dS||ddSdSrWrZr3r+r%r%r&r s   cCs,t|}t|}|dkr td||dS)NrrXrYrZr[r%r%r&rs r'c Cst|}t|}|dkr"tdn|dkr2|dS||d}||fD]}t|ttfrFtd|qFz||d}Wn(tk rt|t|d}YnXt||}t |||}|}||d} |||d|| S)NrrXr'rYzexpected number but got %r) r2rGrrUZstrZbytesr@r?rJrK) r3Zintervalr+r<ZobjZLZl1Zl2ZcfZfr%r%r&rs&      cCsHt|}t|d}z|ddWStk rBtddYnXdS)Nr'rzno mode for empty data)rOr! most_commonZ IndexErrorr)r3Zpairsr%r%r&r s cCs@tt|}tt|tdddgf\}}tttd|S)Nr')Zkeyr)r!rOr\Znextrr rPr1)r3ZcountsZmaxcountZ 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 1rYz"must have at least two data pointsZ inclusiver^zUnknown method: )rr2rGrangeZappendrE) r3r+r_ZldZmZresultrIZjZdeltaZ interpolatedr%r%r&rls4 $   $$ csddkrt|tfdd|D\}}}tfdd|D\}}}||dt|8}||fS)Nc3s|]}|dVqdS)rYNr%rScr%r&r,rTz_ss..c3s|]}|VqdSr(r%rSrar%r&r,rTrY)rr9rG)r3rbr6r8r5ZUZtotal2Zcount2r%rar&_sss rccCsLt||krt|}t|}|dkr,tdt||\}}t||d|S)NrYz*variance requires at least two data pointsr'rOrPrGrrcrF)r3xbarr+r6ssr%r%r&rs& cCsHt||krt|}t|}|dkr,tdt||\}}t|||S)Nr'z*pvariance requires at least one data pointrd)r3mur+r6rfr%r%r&r s# cC8t||}z |WStk r2t|YSXdSr()rrr:r;)r3revarr%r%r&r  cCrhr()r rr:r;)r3rgrir%r%r&r $rjcCs|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)prgsigmaZqZrZnumZdenr<r%r%r&_normal_dist_inv_cdf7sd      rpc@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_sigmarmrlcCs(|dkrtdt||_t||_dS)Nrmzsigma must be non-negative)rr?rqrr)selfrgror%r%r&__init__s zNormalDist.__init__cCs.t|ttfst|}t|}||t||Sr()rUrPZtuplerr)Zclsr3rer%r%r& from_samplesszNormalDist.from_samplesN)seedcsB|dkrtjn t|j|j|jfddt|DS)Ncsg|]}qSr%r%r)rIgaussrgror%r& z&NormalDist.samples..)randomryZRandomrqrrr`)rsr+rvr%rxr&samplesszNormalDist.samplescCs<|jd}|stdt||jdd|tt|S)N@z$pdf() not defined when sigma is zerog)rrrrrqrr)rsr<rr%r%r&pdfs zNormalDist.pdfcCs2|jstdddt||j|jtdS)Nz$cdf() not defined when sigma is zerorkrlr~)rrrrrqr)rsr<r%r%r&cdfszNormalDist.cdfcCs:|dks|dkrtd|jdkr*tdt||j|jS)Nrmrlz$p must be in the range 0.0 < p < 1.0z-cdf() not defined when sigma at or below zero)rrrrprq)rsrnr%r%r&inv_cdfs  zNormalDist.inv_cdfr]csfddtdDS)Ncsg|]}|qSr%)rrwr+rsr%r&rzr{z(NormalDist.quantiles..r')r`)rsr+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 zerorlr~) rUrr@rrrqrrrrrrr) rsZotherXYZX_varZY_varZdvZdmrHZbx1x2r%r%r&overlaps"   (  zNormalDist.overlapcC|jSr(rqrsr%r%r&rzNormalDist.meancCrr(rrr%r%r&rrzNormalDist.mediancCrr(rrr%r%r&r szNormalDist.modecCrr(rrrr%r%r&rrzNormalDist.stdevcCs |jdS)Nr~rrr%r%r&rrzNormalDist.variancecCs8t|tr&t|j|jt|j|jSt|j||jSr(rUrrqrrrrrr%r%r&__add__ zNormalDist.__add__cCs8t|tr&t|j|jt|j|jSt|j||jSr(rrr%r%r&__sub__rzNormalDist.__sub__cCst|j||jt|Sr(rrqrrrrr%r%r&__mul__#zNormalDist.__mul__cCst|j||jt|Sr(rrr%r%r& __truediv__+rzNormalDist.__truediv__cCst|j|jSr(rrqrrrr%r%r&__pos__3zNormalDist.__pos__cCst|j |jSr(rrr%r%r&__neg__7rzNormalDist.__neg__cCs || Sr(r%rr%r%r&__rsub__=rzNormalDist.__rsub__cCs&t|tstS|j|jko$|j|jkSr()rUrZNotImplementedrqrrrr%r%r&__eq__Cs zNormalDist.__eq__cCst|j|jfSr()Zhashrqrrrr%r%r&__hash__IrzNormalDist.__hash__cCs t|jd|jd|jdS)Nz(mu=z, sigma=z))r0r"rqrrrr%r%r&__repr__MszNormalDist.__repr__)rmrl)r])r"r#r$Z __slots__rtZ classmethodrur}rrrrrZpropertyrrr rrrrrrrrZ__radd__rZ__rmul__rrrr%r%r%r&rsD   "     )rpZ__main__)isclose)addsubmultruediv)repeati iiiiz Test z with another NormalDist:i z with a constant:iz Test constant with z:cCsdSr(r%)G1G2r%r%r& assert_close}rriiIii/g`@@cCsg|] }|tqSr%srSr%r%r&rzr{rzcCsg|] }|tqSr%rrSr%r%r&rzr{cCsg|] }|tqSr%rrSr%r%r&rzr{cCsg|] }|tqSr%rrSr%r%r&rzr{cCsg|]\}}||qSr%r%r)r<Zyr%r%r&rzcCsg|]\}}||qSr%r%rr%r%r&rzr)r)rL)r'r(r(r(r(r()QZ__all__r;rVr|Z fractionsrZdecimalrZ itertoolsrZbisectrrrrrrrrrrZoperatorr Z collectionsr!rErr9r=r.r-rFrJrKrNrrrrrr rrr r rrcrr rr rprZ _statisticsZ ImportErrorr"rrrrrrZdoctestZg1Zg2r+r}rrZfuncZprintrur1ZconstrrrrrAZzipZtestmodr%r%r%r&ZTs   (   :  / 779  / ,  JQ