L ixdZddlZddlmZmZddlZddlmZddlmZm Z ddl m Z ddl m Z dgZGd de Zy) z This closely follows the implementation in NumPyro (https://github.com/pyro-ppl/numpyro). Original copyright notice: # Copyright: Contributors to the Pyro project. # SPDX-License-Identifier: Apache-2.0 N)OptionalUnion)Tensor)Beta constraints) Distribution) broadcast_all LKJCholeskyc eZdZdZdej iZejZ d de de e e fde eddffd Zd fd Zej$fd Zd ZxZS) r a) LKJ distribution for lower Cholesky factor of correlation matrices. The distribution is controlled by ``concentration`` parameter :math:`\eta` to make the probability of the correlation matrix :math:`M` generated from a Cholesky factor proportional to :math:`\det(M)^{\eta - 1}`. Because of that, when ``concentration == 1``, we have a uniform distribution over Cholesky factors of correlation matrices:: L ~ LKJCholesky(dim, concentration) X = L @ L' ~ LKJCorr(dim, concentration) Note that this distribution samples the Cholesky factor of correlation matrices and not the correlation matrices themselves and thereby differs slightly from the derivations in [1] for the `LKJCorr` distribution. For sampling, this uses the Onion method from [1] Section 3. Example:: >>> # xdoctest: +IGNORE_WANT("non-deterministic") >>> l = LKJCholesky(3, 0.5) >>> l.sample() # l @ l.T is a sample of a correlation 3x3 matrix tensor([[ 1.0000, 0.0000, 0.0000], [ 0.3516, 0.9361, 0.0000], [-0.1899, 0.4748, 0.8593]]) Args: dimension (dim): dimension of the matrices concentration (float or Tensor): concentration/shape parameter of the distribution (often referred to as eta) **References** [1] `Generating random correlation matrices based on vines and extended onion method` (2009), Daniel Lewandowski, Dorota Kurowicka, Harry Joe. Journal of Multivariate Analysis. 100. 10.1016/j.jmva.2009.04.008 concentrationNdim validate_argsreturncb|dkrtd|d||_t|\|_|jj }t j ||f}|jd|jdz zz}t j|jdz |jj|jj}t j|jd|g}|dz}|jdd|zz } t|| |_t |A|||y) NzDExpected dim to be an integer greater than or equal to 2. Found dim=.?dtypedevice)r) ValueErrorr r r sizetorchSizearangerrcat new_zeros unsqueezer_betasuper__init__) selfr r r batch_shape event_shape marginal_concoffset beta_conc1 beta_conc0 __class__s f/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/torch/distributions/lkj_cholesky.pyr#zLKJCholesky.__init__Bs 7VWZV[[\]  -m < ((--/ jj#s, **SDHHqL-AA  HHqL$$**%%,,  F,,T2F;<c\ ",,R03<? *j1  k=Ac~|jt|}tj|}|j|_|j j ||_|jj ||jfz|_tt|'||jd|j|_ |S)NF)r) _get_checked_instancer rrr r expandr!r"r#r&_validate_args)r$r% _instancenewr+s r,r0zLKJCholesky.expand]s((i@jj- (( ..55kBJJ%%kTXXK&?@  k3( )) ) "00 r-c~|jj|jd}tj|j ||j |jjd}||jddz }|ddddfjdtj||z}tj|j j}tjdtj|d zd z | j}|tj |z }|S) NrrT)r keepdim.rgrrr )min)r!sampler rrandn_extended_shaperrtrilnormfill_sqrtfinfotinyclampsum diag_embed)r$ sample_shapeyu_normal u_hypersphereweps diag_elemss r,r8zLKJCholesky.sampleis JJ  l + 5 5b 9;;   .aggahh $r( !8==R=#FF c1ai &&s+ JJqMM )kk!''"''[[UYYq!t%  Q1 T5G5G5N5NOT''!+66r::TXXEM$yy1A)ArJhhl""S3Y.ll5)C/ NN53;4 Ci$((477"33 $y0;>#n44r-)g?N)N)__name__ __module__ __qualname____doc__rpositivearg_constraints corr_choleskysupportintrrfloatrboolr#r0rrr8r_ __classcell__)r+s@r,r r s$L' (<(<=O''G /2(, B BVU]+B ~ B  B6 #-%**,*5r-)rcrTtypingrrrrtorch.distributionsrr torch.distributions.distributionrtorch.distributions.utilsr __all__r r-r,rrs8 " 193 /@5,@5r-