L i?@ddlZddlZddlmZmZddlmZm Z ddl m Z ddl mZddlmZmZdZd Zd Zdd Zdd Zdd ZddZddZdZdZy)N)fftifft)ndtrndtri)primes_from_2_to)_bvnu) _qmvn_inner _qmvt_innerct}tttj|dzD]*}||zs|j |||z}||zs|dk(s*n|dk7r|j |t |S)zLReturn a sorted list of the unique prime factors of a positive integer. r )setrintnpsqrtaddsorted)nfactorsps X/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/scipy/stats/_qmvnt.py_factorize_intr.s~eG c"''!*o1 2q5 KKN !GAq5 6    Av A '?c|dz }t|}t|}d}d}||krH|||z}tt|t|t|}|dk(r|dz }d}n|dz }||krH|S)zCompute a primitive root of the prime number `p`. Used in the CBC lattice construction. References ---------- .. [1] https://en.wikipedia.org/wiki/Primitive_root_modulo_n r r)rlenpowr)rpmrrrkdrds r_primitive_rootr">s QBR G G A A A a% '!*  QQQ ( 7 FAA FA a% Hrc&t|dz}|d}tj|}tjddtj|dz zg}d}d}tjd|dz}|dz dz}t |} tj|t } t|dz D]} | | | z|z| | dz<tj|| z | } | |z } | | z| z dz} t| }td|D]}tj| d |dzd d d| |dz|d d dg}|||dz ||dz |zzz}t|t|zjj}| |||<||z }||fS) aCompute a QMC lattice generator using a Fast CBC construction. Parameters ---------- n_dim : int > 0 The number of dimensions for the lattice. n_qmc_samples : int > 0 The desired number of QMC samples. This will be rounded down to the nearest prime to enable the CBC construction. Returns ------- q : float array : shape=(n_dim,) The lattice generator vector. All values are in the open interval ``(0, 1)``. actual_n_qmc_samples : int The prime number of QMC samples that must be used with this lattice, no more, no less. References ---------- .. [1] Nuyens, D. and Cools, R. "Fast Component-by-Component Construction, a Reprise for Different Kernels", In H. Niederreiter and D. Talay, editors, Monte-Carlo and Quasi-Monte Carlo Methods 2004, Springer-Verlag, 2006, 371-385. r ?g?rrdtypegUUUUUU?N) rroneshstackaranger"rrangeminimumrrrealargmin)n_dim n_qmc_samplesprimesbtgmqwzmgpermjpncfcs reordereds r _cbc_latticer@Ys8ma/ 0F2JM B C %!) 445 6B A A !UQYA  q A &A 771C D 1q5\447{m3QU 4 ::md*D 1D B R" wA QB 1e_II dqsGDbDM ac!HTrTN   AaC2ac7Y.. / c!f  " " ) ) +Aw! MA m rc t|}d} |dk(r&t||dzz t||dzz z } d} n|dk(rt|||} d} nt||dz} d} d} d} | |kDry| |krtt t j d| z} || |||fd |i|\}} }| |z } dd| | z dzzz }| ||| z zz } t j || z} | |kDr| |krt| | | fS) aAutomatically rerun the integration to get the required error bound. Parameters ---------- func : callable Either :func:`_qmvn` or :func:`_qmvt`. covar, low, high : array As specified in :func:`_qmvn` and :func:`_qmvt`. rng : Generator, optional default_rng(), yada, yada error : float > 0 The desired error bound. limit : int > 0: The rough limit of the number of integration points to consider. The integration will stop looping once this limit has been *exceeded*. **kwds : Other keyword arguments to pass to `func`. When using :func:`_qmvt`, be sure to include ``nu=`` as one of these. Returns ------- prob : float The estimated probability mass within the bounds. est_error : float 3 times the standard error of the batch estimates. n_samples : int The number of integration points actually used. rr g?gV瞯 n_batches The number of points to sample. This number will be divided into `n_batches` batches that apply random offsets of the sampling lattice for each batch in order to estimate the error. covar : (n, n) float array Possibly singular, positive semidefinite symmetric covariance matrix. low, high : (n,) float array The low and high integration bounds. rng : Generator, optional default_rng(), yada, yada lattice : 'cbc' or callable The type of lattice rule to use to construct the integration points. n_batches : int > 0, optional The number of QMC batches to apply. Returns ------- prob : float The estimated probability mass within the bounds. est_error : float 3 times the standard error of the batch estimates. rr size) _permuted_choleskyflags c_contiguouscopyshaper@maxrandomr r)r7rIrJrKrClattice n_batchescholohirr4r0rndmrPrQrOs r_qmvnrhs6%UC6KCR 99 ! !hhj ! A#AE3qI~q+ABA} ::Iq>: *D!, 4]#S^S"b"D)Y I %%rc t|||\ jd dz d}t d|z }t d|z }| | z  fd}|fS)a~Transform the multivariate normal integration into a QMC integrand over a unit hypercube. The dimensionality of the resulting hypercube integration domain is one less than the dimensionality of the original integrand. Note that this transformation subsumes the integration bounds in order to account for infinite bounds. The QMC integration one does with the returned integrand should be on the unit hypercube. Parameters ---------- covar : (n, n) float array Possibly singular, positive semidefinite symmetric covariance matrix. low, high : (n,) float array The low and high integration bounds. use_tent : bool, optional If True, then use tent periodization. Only helpful for lattice rules. Returns ------- integrand : Callable[[NDArray], NDArray] The QMC-integrable integrand. It takes an ``(n_qmc_samples, ndim_integrand)`` array of QMC samples in the unit hypercube and returns the ``(n_qmc_samples,)`` evaluations of at these QMC points. ndim_integrand : int The dimensionality of the integrand. Equal to ``n-1``. rr rrcLt|}ttj|d}|k(sJtj||f}tj| }tj|}|j }t dD]}rtd||dz zdz }n||dz }t|||zz||dz ddf< |d|f|d|ddfz} ||f} t|| z | z }t|| z | z } | |z }||z}|S)Nrr r) rr atleast_1dzerosfullr^r+absphinvrD)zsndim_qmcr0yr<dcpvixr>ctr rdcidcirfrerndim_integranduse_tents r integrandz%_mvn_qmc_integrand..integrandsBr7BMM"Q%01 >))) HHh . / GGM2 & WW]C ( WWYq! ABqsG a(qsGAF +Aa!eQhKArrE Qrr1uX%AQTBRUQY"$%ARUQY"$%AQBbB  r)r[r_rD)rIrJrKr|rxr<r r}rdryrzrfrerr{s ` @@@@@@@r_mvn_qmc_integrandr~s:%UC6KCR ! AUN TB BqEBJA BqEBJA B b&C. n $$rc tdtj|}tj|tj}tj|tj}t |||z ||z \} } } | j d} t| t||zd\} }|j|| f}t| |t|t|| | | t|\}}}|||fS)a5Multivariate t integration over box bounds. Parameters ---------- m : int > n_batches The number of points to sample. This number will be divided into `n_batches` batches that apply random offsets of the sampling lattice for each batch in order to estimate the error. nu : float >= 0 The shape parameter of the multivariate t distribution. covar : (n, n) float array Possibly singular, positive semidefinite symmetric covariance matrix. low, high : (n,) float array The low and high integration bounds. rng : Generator, optional default_rng(), yada, yada lattice : 'cbc' or callable The type of lattice rule to use to construct the integration points. n_batches : int > 0, optional The number of QMC batches to apply. Returns ------- prob : float The estimated probability mass within the bounds. est_error : float 3 times the standard error of the batch estimates. n_samples : int The number of samples actually used. r%r&rr rY) r`rrasarrayfloat64r[r_r@rar rfloat)r7nurIrJrKrCrbrcsnrdrerfrr4r0rgrPrQrOs r_qmvtr6s> S"''"+ B **S +C ::d"** -D$UC"HdRi@KCR ! A#As1 >1'=>A} ::Iq>: *D!, 4]#S^S"b%)"D)Y I %%rc >tj|tj}tj|tj}tj|tj}|jd}|j||fk7r t d|j|fk7s|j|fk7r t dtj tj tj|d}d||dk(<||z}||z}||z}||ddtjfz}tj|} tj dtjz} t|D]} | d z|z} | } d}d}d}d}d}t| |D]x}|||f|kDstj |||f}|dkDr||d| f| d| z}|||z |z }|||z |z }t|t|z }||kso|}|}|}|}|} z| | kDr|| | f|| | f<t|tj| d| ftj| d| ft|tj| d zd| ftj| d zd| ft|tj| d z| | ftj| | d z| ft|| | t|| | || kDr||| | f<d|| | d zdf<t| d z|D]=}||| fxx|zcc<||| d z|d zfxx||| f|| d z|d z| fzzcc<?t||kDrCtj | |zdz tj | |zdz z | |zz | | <n ||zdz | | <|d kr|| | <n |d kDr|| | <|| d| d zfxx|zcc<|| xx|zcc<|| xx|zcc<d|| d| f<|| || zdz | | <|||fS) a{Compute a scaled, permuted Cholesky factor, with integration bounds. The scaling and permuting of the dimensions accomplishes part of the transformation of the original integration problem into a more numerically tractable form. The lower-triangular Cholesky factor will then be used in the subsequent integration. The integration bounds will be scaled and permuted as well. Parameters ---------- covar : (n, n) float array Possibly singular, positive semidefinite symmetric covariance matrix. low, high : (n,) float array The low and high integration bounds. tol : float, optional The singularity tolerance. Returns ------- cho : (n, n) float array Lower Cholesky factor, scaled and permuted. new_low, new_high : (n,) float array The scaled and permuted low and high integration bounds. r&rz!expected a square symmetric arrayzLexpected integration boundaries the same dimensions as the covariance matrixrBr%Nrr i )rarrayrr_ ValueErrorrmaximumdiagnewaxisrmrTr+rD _swap_slicess_roexp)rIrJrKtolrdnew_lonew_hirrtrssqtprepkimckdemr>lo_mhi_mrvrylo_ihi_ides rr[r[bs]4 ((5 +C XXc ,F XXd"** -F ! A yyQF<== ||tv||t3 '  BGGCL#. /BBrSyM bLF bLF2IC2am C  A 771ruu9 D 1X2/1um   q! A1a4y3WWSAY'q5ArrE QrU*Aq A +q A +YT*9BCDDB  6ad)CBK beeBFmRUU1bqb5\ : beeBFGRK0"%%Q 2C D beeAE"HaK0"%%AE"H 2E F B ' B ' 8C1I C1q56 N1q5!_ GAqD R Aq1uQU{N#s1a4y3q1uQU{A~3F'FF# G3x#~ q 01BFFD54>r2qAQQA RaA Hr)gMbP?i')cbcr)F)g|=)rnumpyr scipy.fftrr scipy.specialrrDrrpscipy.stats._qmcrscipy.stats._stats_pythranr _qmvnt_cyr r rr"r@rWrhr~rr[rrErrrsVF 5-,/   68v2&j'&Z=%@)&XcL  r