L iN&ddlZddlZddlmcmZddlm Z dZ ejejde Z ejejde Zejdej zZej$dej zZdZejdZej dzZej dzZej d zZgd Zd Zd Zdd ZddZdZddZddZ ddZ!dZ"dZ#ddZ$dZ%ddZ&y)N) _derivativei<)gSˆBgAAz?g}<ٰj_g#+K?g88CgJ?gllfgUUUUUU?cd|z }tj|dz |z tdz z|tjt||z zzS)N?r)nplog_LOG_2PIpolyval_STIRLING_COEFFS)nrns Z/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/scipy/stats/_ksstats.py_log_nfactorial_div_n_pow_nr]sF QB 66!9Q;?XaZ '"rzz:JBqD/Q*Q QQc0tj|ddS)z%clips a probability to range 0<=p<=1.r )r clip)ps r _clip_probrgs 771c3 rcFtj|||}t|S)z>Selects either the CDF or SF, and then clips to range 0<=p<=1.)r wherer)cdfprobsfprobcdfrs r_select_and_clip_probr ls gv&A a=rc|dk\r tdd|S||z}|dkr tdd|Sttj|}||z }d|zdz }tj||g}tj d|dz}d||zz } tj |} d} |D]} | | | dz <| | z} | | dz xx| zcc<!td|zdz d|zd||zzz } d| z| z| d<td|D]}| d||z dz||dz d|f<| |dddf<tj| d |dddf<tjtj|d}|}d}d}|dkDr|dzrtj||}||z }tj||}|dz}tj||dz |dz ftkDr|tz}|tz }|dz}|dkDr||dz |dz f}td|dzD]9}||z|z }tj|t ks(|tz}|tz};|dk7rtj"||}t|d|z |S) zComputes the Kolmogorov CDF: Pr(D_n <= d) using the MTW approach to the Durbin matrix algorithm. Durbin (1968); Marsaglia, Tsang, Wang (2003). [1], [3]. r r?rrrN)axis)r intr ceilzerosarangeemptymaxrangeflipeyeshapematmulabs_EP128_E128_EM128ldexp)rdrndkhmHintmvwfacjttiHpwrnnexpntHexpntrs r _kolmogn_DMTWrFrs Cx$S#s33 QB Sy$S#s33 BGGBKA BA A A !QA 99QA D a4iA  A C !a% q !a%C QUS[! a !AqD& (B 2X AbE 1a[%!a%!)}!a%&!) %AadGwwqq!Ab!eH 66"((1+a. !D B E F q& 699T1%D VOE IIaO!  66!AE1q5L/ "V + KA eOF 1W q& QUAE\A1a!e_ EAI 66!9v  KA UNE  z HHQ  CE3 //rc8|dk(r| |z dz ||zdz }}nit|dzd\}}|dk(r<||dzk(r||z |z dz ||z|zdz }}n3|dz |z |z dz ||zdz |zdz }}n|dz |z dz ||z|zdz }}t|dzdt||fS)z0Compute the endpoints of the interval for row i.rrr)divmodr*min) rArllceilfroundfj1j2ip1div2ip1mod2s r_pomeranz_compute_j1j2rQsAvuq"u*q.B"!a%+ a<!a%R%!+QVe^a-?B 1r)F2Q6" q8H58PST8TBq[2%)7R<&+@1+DB rAvq>3r1: %%rc^||z}ttj|}d||z z}t|d|z }|dkDrdnd}|dkDrdnd}d|dzz} tj| } tj| } tj| } d| d<d| d<d| d<d} ||z d|z|z dd|zz |z }}}t d| D]5}| |dz |z|z | |<| |dz |z|z | |<| |dz |z|z | |<7tj | g}tj | g}d|d<d\}}td||||\}}t dd|zdzD]}|}||}}||}}|jdt|||||\}}|dk(s |d|zdzk(r| }n |dzr| n| }||z dz}|dkDsZtj|||z ||z |z|d|}||z }||z dz}||||z|d|dtj|cxkr tkrnn|tz}| tz} ||z|z }|||z }t d|dzD]5}tj|tkDr|tz}| tz } ||z}7| dk7rtj|| }t!|d|z |}|S) z[Computes Pr(D_n <= d) using the Pomeranz recursion algorithm. Pomeranz (1974) [2] r rrr"r)rrrN)r%r floorrIr)r+r'rQfillconvolver*r3r1r2r0r4r ) rxrtrJfgrKrLnpwrsgpower twogpower onem2gpowerrDg_over_n two_g_over_none_minus_two_g_over_nr9V0V1V0sV1srMrNrAk1pwrsln2conv conv_startconv_lenanss r_kolmogn_Pomeranzrls$$ AA RXXa[ B q2vA AsQwAa%QQEs7aF aLE XXe_FI((5/KF1IIaLKN E56qS!A#a%!ac'12lH 1e_I1q5MH,q0q  Q',6: ! $QU+.DDqH AI 5' B 5' B BqEHC #Aq"eV :D#JGDbJBw{H J,ABByM266":&&f (R-C+ 0 QW+C 1a!e_ 66#;  6MC UNE q  zhhsE" S3Y 4C Jrc |dkrtdd|S|dk\rtdd|Stj||z}|dz|dz|dz|dzf\}}}}t dz |z }|tkrtdd|Stj |} | } tdz } d|zd|zz} d|zd |zz tzdz } t d d|zz zd z }td d |zz zd z }t d|zd|zzzd z }td|zd|zz zdz }d|zd|dzzz }tjd}ttjd |ztjz }t|ddD]y}d|zd z }|dz|dz|dz}}}tj| d|z}tjd| | |zz| | |zz||zz|||zz||zz||zzg}||z}||z }{|| z}|tz}|tj|d|zd|dzzd|dzzgz}tj t dz |z } tj |dd}|dz}t"|z}tj|z}| |z} tj$|| z}!|!ttzd|zz z}!|dxx|!z cc<tj$||z||z z|z| z}"|"ttzd|zz z}"|dxx|"z cc<tj|dztj t'|dz }#||#z}|s|dz}|dxxd z cc<t%|}$|$S)aPComputes the Pelz-Good approximation to Prob(Dn <= x) with 0<=x<=1. Start with Li-Chien, Korolyuk approximation: Prob(Dn <= x) ~ K0(z) + K1(z)/sqrt(n) + K2(z)/n + K3(z)/n**1.5 where z = x*sqrt(n). Transform each K_(z) using Jacobi theta functions into a form suitable for small z. Pelz-Good (1976). [6] rr rrrrr r@i`iZrr#HiP i@)r r sqrt _PI_SQUARED_MIN_LOGexp_PI_FOUR_PI_SIXr'r%r&pir+powerarray_SQRT2PIr(_SQRT3sumlen)%rrVrzzsquaredzthreezfourzsixqlogqk1ak1bk2ak2bk2ck3dk3ck3bk3aK0to3maxkr7r9msquaredmfourmsixqpowercoeffsksksquaredsqrt3zkspiqpwersk2extrak3extra powers_of_nKsums% r_kolmogn_PelzGoodr#s Cx$S#377Cx$S#377  QA$%qD!Q$1ad$:!HfeT >E  |a(*+A 4B BQwH aZF 552:D (]FffX&'G {X%sV|44G !HHffftm 6AFJKG {X%sTz22G !HH((1s7BIIc%j$9C$?@K [E    aA  u:D Krctj|r|St||k7s|dkrtjS|dk\rt dd|S|dkrt dd|S||z}|dkr|dkrt dd|S|dkr$Qt4D(sTzsC C5==((A..$S4Z3??     u}},,Q22Dd# #D fQVs*1$/#Aqd3 #-S AArctjrStk7sdkrtjS|dk\s|dkry|z}|dkr|dkrydkr9tjtj ddz zd|zdz z}n@tj tdz tjd|zdz zz}|dzdzzS|dz k\rdd|z dz zzzS|dk\r-dtjjj|zS|dz }t||dz z }t|d|z }fd }t|||d S) zvComputes the PDF for the two-sided Kolmogorov-Smirnov statistic. x must be of type float, n of type integer. rr r"rrrrg@ct|SN)kolmogn)_xrs r_kkz_kolmogn_p.._kksq"~rrp)dxorder)r rr%rrr(rrr rstatsksonepdfrIr)rrVrWprddeltars` r _kolmogn_prsl  xx{ 1v{a1fvv Cx16 AACx 8 8''"))Aq/S1W5QCDC&&4Q71Q3"&&QQRBS:SSTCQwA~AEzC!G1%%))Cx5;;$$((A... KE q3q5y !E sQw E sA%q 11rctjrStk7sdkrtjSdkrdz S|dkrytjtj t jjdzz z }|dz kr |dz zdz Stjtj |dz z  }|ddz z k\r|Stjtjz }t|ddz z }fd}t jj|dz |dS) zeComputes the PPF/ISF of kolmogn. n of type integer, n>= 1 p is the CDF, q the SF, p+q=1 rr rrr|c"t|z Sr)r)rVrrs r_fz_kolmogni.._fs1~!!rg+=)xtol)r rr%rrr rrloggammaexpm1scu _kolmogcir}rIoptimizebrentq)rrrrrVx1rs`` r _kolmognirs'  xx{ 1v{a1fvv Av1u Av FFBFF1I 6 6qs ;;Q> ?E A~a1$$ "&&3-/ ""AAAI~ q "''!* $B Rs1u B" >> SUBU ;;rcttj|||dgdgdtjtjtjg}|D]X\}}}}tj|r||d<#t ||k7rt d|tt ||||d<Z|jd}|S)aComputes the CDF for the two-sided Kolmogorov-Smirnov distribution. The two-sided Kolmogorov-Smirnov distribution has as its CDF Pr(D_n <= x), for a sample of size n drawn from a distribution with CDF F(t), where :math:`D_n &= sup_t |F_n(t) - F(t)|`, and :math:`F_n(t)` is the Empirical Cumulative Distribution Function of the sample. Parameters ---------- n : integer, array_like the number of samples x : float, array_like The K-S statistic, float between 0 and 1 cdf : bool, optional whether to compute the CDF(default=true) or the SF. Returns ------- cdf : ndarray CDF (or SF it cdf is False) at the specified locations. The return value has shape the result of numpy broadcasting n and x. N zerosize_ok)flags op_dtypes.n is not integral: rnr#) r nditerfloat64bool_rr% ValueErrorroperands) rrVrit_nr_cdfrresults rrrs0 Aq#t$]O"BJJ"**E GB1Ba 88B<AcF  r7b=22$78 8#b'240# 1[[_F Mrctj||dg}|D]U\}}}tj|r||d<"t||k7rt d|t t|||d<W|j d}|S)aComputes the PDF for the two-sided Kolmogorov-Smirnov distribution. Parameters ---------- n : integer, array_like the number of samples x : float, array_like The K-S statistic, float between 0 and 1 Returns ------- pdf : ndarray The PDF at the specified locations The return value has shape the result of numpy broadcasting n and x. N.rr#)r rrr%rrr)rrVrrrrrs rkolmognprs" Aq$< B) B 88B<AcF  r7b=22$78 8CGR(# )[[_F Mrc4tj|||dg}|D]j\}}}}tj|r||d<#t||k7rt d||r|d|z fnd|z |f\}} t t||| |d<l|j d} | S)aComputes the PPF(or ISF) for the two-sided Kolmogorov-Smirnov distribution. Parameters ---------- n : integer, array_like the number of samples q : float, array_like Probabilities, float between 0 and 1 cdf : bool, optional whether to compute the PPF(default=true) or the ISF. Returns ------- ppf : ndarray PPF (or ISF if cdf is False) at the specified locations The return value has shape the result of numpy broadcasting n and x. N.rrr#)r rrr%rrr) rrrrr_qrr_pcdf_psfrs rkolmognir;s& Aq#t$ %B1Ba 88B<AcF  r7b=22$78 8$(r1R4jqtRj t3r7E40#1[[_F Mr)T)'numpyr scipy.specialrscipy.special._ufuncsr_ufuncsrscipy.stats._finite_differencesrr2r4 longdoubler1r3r}rrr rrrr~rrrrrr rFrQrlrrrrrrrrrrsH##7  -"--"E * -"--"UF + 2771ruu9  266!bee)   eeqj 55A: %%1*I R  H0V&$QhPf9Bx'2T<:"J:r