Ë
    óÍ:jœ  ã                   óx   — d Z ddlZddlmZmZ ddlZddlmZ ddlmZm	Z	 ddl
mZ ddlmZ dgZ G d	„ de«      Zy)
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)ÚOptionalÚUnion)ÚTensor)ÚBetaÚconstraints)ÚDistribution)Úbroadcast_allÚLKJCholeskyc            	       ó¸   ‡ — e Zd ZdZdej
                  iZej                  Z	 	 dde	de
eef   dee   ddfˆ fd„Zdˆ fd„	Z ej$                  «       fd	„Zd
„ Zˆ xZS )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                 ób  •— |dk  rt        d|› d�«      ‚|| _        t        |«      \  | _        | j                  j	                  «       }t        j                  ||f«      }| j                  d| j                  dz
  z  z   }t        j                  | j                  dz
  | j                  j                  | j                  j                  ¬«      }t        j                  |j                  d«      |g«      }|dz   }|j                  d«      d|z  z
  }	t        ||	«      | _        t        ‰
| �A  |||«       y )	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             €úu/home/mcse/projects/srt_converter/srt-converter-venv/lib/python3.12/site-packages/torch/distributions/lkj_cholesky.pyr#   zLKJCholesky.__init__B   s  ø€ ð �Š7ÜØVÐWZÐV[Ð[\Ð]óð ð ˆŒÜ -¨mÓ <ÑˆÔ	Ø×(Ñ(×-Ñ-Ó/ˆÜ—j‘j # s Ó,ˆà×*Ñ*¨S°D·H±H¸q±LÑ-AÑAˆÜ—‘Ø�H‰H�q‰LØ×$Ñ$×*Ñ*Ø×%Ñ%×,Ñ,ô
ˆô
 —‘˜F×,Ñ,¨TÓ2°FÐ;Ó<ˆØ˜c‘\ˆ
Ø"×,Ñ,¨RÓ0°3¸±<Ñ?ˆ
Ü˜* jÓ1ˆŒ
Ü‰Ñ˜ k°=ÕAó    c                 ó~  •— | j                  t        |«      }t        j                  |«      }| j                  |_        | j
                  j                  |«      |_        | j                  j                  || j                  fz   «      |_        t        t        |�'  || j                  d¬«       | j                  |_        |S )NF)r   )Ú_get_checked_instancer
   r   r   r   r   Úexpandr!   r"   r#   r&   Ú_validate_args)r$   r%   Ú	_instanceÚnewr+   s       €r,   r0   zLKJCholesky.expand]   sž   ø€ Ø×(Ñ(¬°iÓ@ˆÜ—j‘j Ó-ˆØ—(‘(ˆŒØ ×.Ñ.×5Ñ5°kÓBˆÔØ—J‘J×%Ñ% k°T·X±X°KÑ&?Ó@ˆŒ	ÜŒk˜3Ñ(Ø˜×)Ñ)¸ð 	)ô 	
ð "×0Ñ0ˆÔØˆ
r-   c                 ó~  — | j                   j                  |«      j                  d«      }t        j                  | j                  |«      |j                  |j                  ¬«      j                  d«      }||j                  dd¬«      z  }|ddd d …f   j                  d«       t        j                  |«      |z  }t        j                  |j                  «      j                  }t        j                  dt        j                  |d	z  d¬
«      z
  |¬«      j                  «       }|t        j                   |«      z  }|S )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,   r8   zLKJCholesky.samplei   sú   € ð �J‰J×Ñ˜lÓ+×5Ñ5°bÓ9ˆÜ—;‘;Ø× Ñ  Ó.°a·g±gÀaÇhÁhô
ç
‰$ˆr‹(ð 	ð ! 8§=¡=°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                 ó¤  — | j                   r| j                  |«       |j                  dd¬«      ddd …f   }t        j                  d| j
                  dz   | j                  j                  ¬«      }d| j                  dz
  j                  d«      z  | j
                  z   |z
  }t        j                  ||j                  «       z  d¬«      }| j
                  dz
  }| j                  d	|z  z   }t        j                  |«      |z  }t        j                  |d	z
  |«      }d	|z  t        j                  t        j                  «      z  }	|	|z   |z
  }
||
z
  S )
Nr   éþÿÿÿ)Údim1Údim2.r   r   )r   r6   r   )r1   Ú_validate_sampleÚdiagonalr   r   r   r   r   r    rB   ÚlogÚlgammaÚmvlgammaÚmathÚpi)r$   ÚvaluerJ   ÚorderÚunnormalized_log_pdfÚdm1ÚalphaÚdenominatorÚ	numeratorÚpi_constantÚnormalize_terms              r,   Úlog_probz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ˆØ$ yÑ0°;Ñ>ˆØ# nÑ4Ð4r-   )g      ð?N)N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   ÚpositiveÚarg_constraintsÚcorr_choleskyÚsupportÚintr   r   Úfloatr   Úboolr#   r0   r   r   r8   r_   Ú__classcell__)r+   s   @r,   r
   r
      s‡   ø„ ñ$ðL '¨×(<Ñ(<Ð=€OØ×'Ñ'€Gð
 /2Ø(,ñ	BàðBð ˜V U˜]Ñ+ðBð   ‘~ð	Bð
 
õBõ6
ð #- %§*¡*£,ó ö*5r-   )rc   rT   Útypingr   r   r   r   Útorch.distributionsr   r   Ú torch.distributions.distributionr   Útorch.distributions.utilsr	   Ú__all__r
   © r-   r,   ú<module>rr      s8   ðñó ß "ã Ý ß 1Ý 9Ý 3ð ˆ/€ô@5�,õ @5r-   