L i5ddlZddlZddlmZmZddlZddlmZmZddlm Z ddl m Z ddl m Z ddlmZddlmZmZmZd gZej*d Zd ed ed efdZd ed efdZGdd e Zy)N)OptionalUnion)nanTensor) constraints)ExponentialFamily)_precision_to_scale_tril) lazy_property)_Number_sizeNumberWishartxpreturncp|j|dz dz jsJdtj|j dtj ||j |jjdj|jdzz jdS)Nrz/Wrong domain for multivariate digamma function.dtypedevicer) gtalltorchdigamma unsqueezearangerrdivexpandshapesum)rrs a/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/torch/distributions/wishart.py _mvdigammar%s 44Q!  "U$UU " == B ,,q 9 = =a @ G GRW X Y  c"gct|jtj|jjS)N)min)clamprfinforeps)rs r$_clamp_above_epsr,s& 77u{{177+//7 00r&c eZdZdZej ZdZdZe dZ dde e e fdee dee d ee d eed df fd Zdfd Zed e fdZed e fdZed e fdZe d e fdZe d e fdZe d e fdZej4fdZej4dfded e fdZdZdZe d e e e ffdZ!dZ"xZ#S)ra Creates a Wishart distribution parameterized by a symmetric positive definite matrix :math:`\Sigma`, or its Cholesky decomposition :math:`\mathbf{\Sigma} = \mathbf{L}\mathbf{L}^\top` Example: >>> # xdoctest: +SKIP("FIXME: scale_tril must be at least two-dimensional") >>> m = Wishart(torch.Tensor([2]), covariance_matrix=torch.eye(2)) >>> m.sample() # Wishart distributed with mean=`df * I` and >>> # variance(x_ij)=`df` for i != j and variance(x_ij)=`2 * df` for i == j Args: df (float or Tensor): real-valued parameter larger than the (dimension of Square matrix) - 1 covariance_matrix (Tensor): positive-definite covariance matrix precision_matrix (Tensor): positive-definite precision matrix scale_tril (Tensor): lower-triangular factor of covariance, with positive-valued diagonal Note: Only one of :attr:`covariance_matrix` or :attr:`precision_matrix` or :attr:`scale_tril` can be specified. Using :attr:`scale_tril` will be more efficient: all computations internally are based on :attr:`scale_tril`. If :attr:`covariance_matrix` or :attr:`precision_matrix` is passed instead, it is only used to compute the corresponding lower triangular matrices using a Cholesky decomposition. 'torch.distributions.LKJCholesky' is a restricted Wishart distribution.[1] **References** [1] Wang, Z., Wu, Y. and Chu, H., 2018. `On equivalence of the LKJ distribution and the restricted Wishart distribution`. [2] Sawyer, S., 2007. `Wishart Distributions and Inverse-Wishart Sampling`. [3] Anderson, T. W., 2003. `An Introduction to Multivariate Statistical Analysis (3rd ed.)`. [4] Odell, P. L. & Feiveson, A. H., 1966. `A Numerical Procedure to Generate a SampleCovariance Matrix`. JASA, 61(313):199-203. [5] Ku, Y.-C. & Bloomfield, P., 2010. `Generating Random Wishart Matrices with Fractional Degrees of Freedom in OX`. Trctjtjtjtj|jddz dS)Nrr)covariance_matrixprecision_matrix scale_trildf)rpositive_definitelower_cholesky greater_than event_shapeselfs r$arg_constraintszWishart.arg_constraintsGsG"-!>!> + = =%44**4+;+;B+?!+CD   r&Nr2r/r0r1 validate_argsrc(|du|duz|duzdk(sJdtd|||fD}|jdkr tdt|trTt j |jdd}t j||j|j|_ nCt j|jdd|j}|j||_ |jdd}|jj|ddz jrtd |d |ddz d ||j|d z|_n7||j|d z|_n||j|d z|_|jj'|djrt)j*d t, |]|||t1t3|j4D cgc]} | dz  c} |_|||_n7|%t j:j=||_nt?||_t j@jBjE|jjGdt jH|jJd|j8j|j8jj|dzz |_&ycc} w)NrzTExactly one of covariance_matrix or precision_matrix or scale_tril may be specified.c3$K|]}|| ywN).0rs r$ z#Wishart.__init__..^s }  srzSscale_tril must be at least two-dimensional, with optional leading batch dimensionsrrz Value of df=z( expected to be greater than ndim - 1 = .)rrz]Low df values detected. Singular samples are highly likely to occur for ndim - 1 < df < ndim.r:rr2)'nextdim ValueError isinstancer rSizer"tensorrrr2broadcast_shapesr!leanyr1r/r0ltwarningswarnsuper__init__rangelen _batch_shape _batch_dims_unbroadcasted_scale_trillinalgcholeskyr distributionschi2Chi2rr _event_shape _dist_chi2) r8r2r/r0r1r:param batch_shaper6r __class__s r$rRzWishart.__init__Ps"-*D2HI D (   c   ')9:F   99;?e  b' "**U[["%56Kll2U[[NDG00Sb1A288LKii ,DGkk"#& 77::k"o) * . . 0rd"J;WY?]^K^J__`a   !#ll;+ABDO  *%*\\+2H%ID "  )$)LLx1G$HD ! 77::k"o & * * , MMo  kO.3C8I8I4J.KLa!eHL  !-7D *  *-2\\-B-BCT-UD *-EFV-WD * --2277!!"%,,%%b)88>>99@@&u,- .8  Ms Lc"|jt|}tj|}||jz}|j j ||_|jj ||_tt|Dcgc]}|dz  c}|_ d|jvr |jj ||_ d|jvr |jj ||_ d|jvr |jj ||_tjj j#|jj%dtj&|jd|j j(|j j*j |dzz |_t.t|c||jd |j2|_|Scc}w) Nrr/r1r0rrrrDFrC)_get_checked_instancerrrIr6rWr!r2rSrTrV__dict__r/r1r0rZr[r\rrrrr^rQrR_validate_args)r8r` _instancenew cov_shaperras r$r!zWishart.expands(()<jj- $"2"22 (,(F(F(M(Mi(X% ,-23{3C-DEQU8E $-- /$($:$:$A$A)$LC ! 4== (!__33I>CN  .#'#8#8#?#? #JC ,,1166  $,,$$R(77==88??&u,- .7   gs$[$2B2BRW$X!00 /Fs H cf|jj|j|jzSr=)rWr!rUr]r7s r$r1zWishart.scale_trils/--44    1 1 1  r&c|j|jjddzj|j|jzS)NrAr)rW transposer!rUr]r7s r$r/zWishart.covariance_matrixsH  * *,,66r2> ? &""T%6%66 7 8r&c(tj|jd|jj|jj }tj ||jj|j|jzS)Nr)rr) reyer]rWrrcholesky_solver!rU)r8identitys r$r0zWishart.precision_matrixsx99   b !11880066  ##Hd.L.LMTT    1 1 1  r&cl|jj|jdz|jzS)Nrr)r2viewrUr/r7s r$meanz Wishart.means+ww||D--67$:P:PPPr&c|j|jjdz dz }t||dk<|j |j dz|jzS)Nrrrrq)r2r/r"rrrrU)r8factors r$modez Wishart.modesY41177;;a?!v{{{4,,v569O9OOOr&c|j}|jdd}|jj|jdz|j dt jd||zzS)NrArdim1dim2rqrz...i,...j->...ij)r/diagonalr2rrrUpowreinsum)r8Vdiag_Vs r$variancezWishart.variancesb  " ""-ww||D--67 EE!Hu||$6G G  r&c|jd}t|jj|j j dd}t j||d\}}t jt j||jzt||dz zdz fz|j|j|d||f<|j|z}||jddzS) NrrArx)offsetrrr.)r]r,r^rsamplesqrt diag_embedr tril_indicesrandnrIrUintrrrWrk)r8 sample_shapernoiseijchols r$_bartlett_samplingzWishart._bartlett_samplings   b !! OO # #L 1 6 6 8 *"2* & !!!Qr21 ;; JJ| $t'8'8 8CQU a*>t?O?O*P7FK 1 1 34&??,   r&c4|jr|j||j}|jd}| |tzdz |j j ddjjdzztj|dz |z ||z dz dz tjj|jzztj||j j ddjddz z S)NrrrArxrr)rF)re_validate_sampler2r]_log_2rWr{logr#rmvlgammarXslogdet logabsdetrn)r8valuenurs r$log_probzWishart.log_prob's     ! !% ( WW   b ! CF Q0099r9KR nnR!Vq) *AvzQ!5!5e!r&r$rsz " +<L3.. + !&SV1161 u1u1r&