
    [Th                     \    S r SSKrSSK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)Betaconstraints)Distribution)broadcast_allLKJCholeskyc                      ^  \ rS rSrSrS\R                  0r\R                  r	S	U 4S 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
concentrationc                 \  > 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	[        X5      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   )
ValueErrordimr   r
   sizetorchSizearanger   r   cat	new_zeros	unsqueezer   _betasuper__init__)selfr   r
   validate_argsbatch_shapeevent_shapemarginal_concoffset
beta_conc1
beta_conc0	__class__s             X/var/www/auris/envauris/lib/python3.13/site-packages/torch/distributions/lkj_cholesky.pyr   LKJCholesky.__init__@   s   7VWZV[[\]   -m <	((--/jj#,**SDHHqL-AAHHqL$$**%%,,

 F,,T2F;<c\
",,R03<?
*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)   r.   LKJCholesky.expandV   s    ((i@jj-(( ..55kBJJ%%kXXK&?@	k3()) 	) 	
 "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)   r7   LKJCholesky.sampleb   s     JJl+55b9;;  .aggahh

$r( 	 !==R=#FFc1ai &&s+JJqMM)kk!''"''[[UYYq!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   r5   r   )r/   _validate_samplediagonalr   r   r   r
   r   r   rA   loglgammamvlgammamathpi)r    valuerI   orderunnormalized_log_pdfdm1alphadenominator	numeratorpi_constantnormalize_terms              r)   log_probLKJCholesky.log_probw   s"    !!%(^^"^5c12g>
Q1T5G5G5N5NOT''!+66r::TXXEM$yy1A)ArJhhl""S3Y.ll5)C/NN53;4	 Ci$((477"33$0;>#44r+   )r   r
   r   )g      ?N)N)__name__
__module____qualname____firstlineno____doc__r   positivearg_constraintscorr_choleskysupportr   r.   r   r   r7   r_   __static_attributes____classcell__)r(   s   @r)   r   r      sK    $L '(<(<=O''GB,
 #(**, *5 5r+   )re   rT   r   torch.distributionsr   r    torch.distributions.distributionr   torch.distributions.utilsr   __all__r    r+   r)   <module>rq      s2      1 9 3 /{5, {5r+   