ó
    Eñi–  ã                   óh   • S r SSKrSSKrSSKJr  SSKJrJr  SSKJr  SSK	J
r
  S/r " S S\5      rg)	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)ÚTensor)ÚBetaÚconstraints)ÚDistribution)Úbroadcast_allÚLKJCholeskyc            	       óÈ   ^ • \ rS rSrSrS\R                  0r\R                  r	  SS\
S\\-  S\S-  SS4U 4S jjjrSU 4S	 jjr\R"                  " 5       4S
 jrS rSrU =r$ )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
ÚconcentrationNÚdimÚvalidate_argsÚreturnc                 ó\  >• US:  a  [        SU S35      eXl        [        U5      u  U l        U R                  R	                  5       n[
        R                  " X45      nU R                  SU R                  S-
  -  -   n[
        R                  " U R                  S-
  U R                  R                  U R                  R                  S9n[
        R                  " UR                  S5      U/5      nUS-   nUR                  S5      SU-  -
  n	[        X‰5      U l        [        T
U ]A  XEU5        g )	Né   zDExpected dim to be an integer greater than or equal to 2. Found dim=Ú.ç      à?é   ©ÚdtypeÚdevice)r   éÿÿÿÿ)Ú
ValueErrorr   r   r   ÚsizeÚtorchÚSizeÚaranger   r   ÚcatÚ	new_zerosÚ	unsqueezer   Ú_betaÚsuperÚ__init__)Úselfr   r   r   Úbatch_shapeÚevent_shapeÚmarginal_concÚoffsetÚ
beta_conc1Ú
beta_conc0Ú	__class__s             €Ú]/home/mande/repo/quber/.venv/lib/python3.13/site-packages/torch/distributions/lkj_cholesky.pyr"   ÚLKJCholesky.__init__B   s  ø€ ð �‹7ÜØVÐWZÐV[Ð[\Ð]óð ð ŒÜ -¨mÓ <ÑˆÔ	Ø×(Ñ(×-Ñ-Ó/ˆÜ—j’j # Ó,ˆà×*Ñ*¨S°D·H±H¸q±LÑ-AÑAˆÜ—’Ø�H‰H�q‰LØ×$Ñ$×*Ñ*Ø×%Ñ%×,Ñ,ñ
ˆô
 —’˜F×,Ñ,¨TÓ2°FÐ;Ó<ˆØ˜c‘\ˆ
Ø"×,Ñ,¨RÓ0°3¸±<Ñ?ˆ
Ü˜*Ó1ˆŒ
Ü‰Ñ˜°=ÕAó    c                 óv  >• U R                  [        U5      n[        R                  " U5      nU R                  Ul        U R
                  R                  U5      Ul        U R                  R                  XR                  4-   5      Ul        [        [        U]'  XR                  SS9  U R                  Ul        U$ )NF)r   )Ú_get_checked_instancer   r   r   r   r   Úexpandr    r!   r"   r%   Ú_validate_args)r#   r$   Ú	_instanceÚnewr*   s       €r+   r0   ÚLKJCholesky.expand]   sš   ø€ Ø×(Ñ(¬°iÓ@ˆÜ—j’j Ó-ˆØ—(‘(ˆŒØ ×.Ñ.×5Ñ5°kÓBˆÔØ—J‘J×%Ñ% k·X±X°KÑ&?Ó@ˆŒ	ÜŒk˜3Ñ(Ø×)Ñ)¸ð 	)ñ 	
ð "×0Ñ0ˆÔØˆ
r-   c                 óp  • U R                   R                  U5      R                  S5      n[        R                  " U R                  U5      UR                  UR                  S9R                  S5      nX3R                  SSS9-  nUSSS S 24   R                  S5        [        R                  " U5      U-  n[        R                  " UR                  5      R                  n[        R                  " S[        R                  " US	-  SS
9-
  US9R                  5       nU[        R                   " U5      -  nU$ )Nr   r   T)r   Úkeepdim.r   g        r   r   ©r   )Úmin)r    Úsampler   r   ÚrandnÚ_extended_shaper   r   ÚtrilÚnormÚfill_ÚsqrtÚfinfoÚtinyÚclampÚsumÚ
diag_embed)r#   Úsample_shapeÚyÚu_normalÚu_hypersphereÚwÚepsÚ
diag_elemss           r+   r9   ÚLKJCholesky.samplei   sø   € ð �J‰J×Ñ˜lÓ+×5Ñ5°bÓ9ˆÜ—;’;Ø× Ñ  Ó.°a·g±gÀaÇhÁhñ
ç
‰$ˆr‹(ð 	ð !§=¡=°RÀ =Ð#FÑFˆà�c˜1ša�iÑ ×&Ñ& sÔ+Ü�JŠJ�q‹M˜MÑ)ˆä�kŠk˜!Ÿ'™'Ó"×'Ñ'ˆÜ—[’[ ¤U§Y¢Y¨q°!©t¸Ñ%<Ñ!<À#ÑF×KÑKÓMˆ
Ø	ŒU×Ò˜jÓ)Ñ)ˆØˆr-   c                 ó   • U R                   (       a  U R                  U5        UR                  SSS9SSS 24   n[        R                  " SU R
                  S-   U R                  R                  S9nSU R                  S-
  R                  S5      -  U R
                  -   U-
  n[        R                  " X2R                  5       -  SS9nU R
                  S-
  nU R                  S	U-  -   n[        R                  " U5      U-  n[        R                  " US	-
  U5      nS	U-  [        R                  " [        R                  5      -  n	X˜-   U-
  n
XJ-
  $ )
Nr   éþÿÿÿ)Údim1Údim2.r   r   )r   r7   r   )r1   Ú_validate_sampleÚdiagonalr   r   r   r   r   r   rC   ÚlogÚlgammaÚmvlgammaÚmathÚpi)r#   ÚvaluerK   ÚorderÚunnormalized_log_pdfÚdm1ÚalphaÚdenominatorÚ	numeratorÚpi_constantÚnormalize_terms              r+   Úlog_probÚLKJCholesky.log_prob~   s"  € ð ××Ø×!Ñ! %Ô(Ø—^‘^¨°"�^Ð5°c¸1¹2°gÑ>ˆ
Ü—’˜Q §¡¨1¡°T×5GÑ5G×5NÑ5NÑOˆØ�T×'Ñ'¨!Ñ+×6Ñ6°rÓ:Ñ:¸T¿X¹XÑEÈÑMˆÜ$Ÿyšy¨·±Ó1AÑ)AÀrÑJÐà�h‰h˜‰lˆØ×"Ñ" S¨3¡YÑ.ˆÜ—l’l 5Ó)¨CÑ/ˆÜ—N’N 5¨3¡;°Ó4ˆ	ð ˜C‘i¤$§(¢(¬4¯7©7Ó"3Ñ3ˆØ$Ñ0°;Ñ>ˆØ#Ñ4Ð4r-   )r    r   r   )g      ð?N)N)Ú__name__Ú
__module__Ú__qualname__Ú__firstlineno__Ú__doc__r   ÚpositiveÚarg_constraintsÚcorr_choleskyÚsupportÚintr   ÚfloatÚboolr"   r0   r   r   r9   ra   Ú__static_attributes__Ú__classcell__)r*   s   @r+   r   r      s‹   ø† ñ$ðN '¨×(<Ñ(<Ð=€OØ×'Ñ'€Gð
 ),Ø%)ñ	BàðBð  ‘~ðBð ˜d‘{ð	Bð
 
÷Bð B÷6
ð #(§*¢*£,ô ÷*5ð 5r-   )rg   rV   r   r   Útorch.distributionsr   r   Ú torch.distributions.distributionr   Útorch.distributions.utilsr   Ú__all__r   © r-   r+   Ú<module>rv      s5   ðñó ã Ý ß 1Ý 9Ý 3ð ˆ/€ôA5�,õ A5r-   