+
    &j                     j    R t ^ RIt^ RIt^ RIHt ^ RIHtHt ^ RIHt ^ RI	H
t
 R.t ! R R]4      tR# )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                      a a ] tR t^t oRtR]P                  /t]P                  t	R	V3R lV 3R lllt
R
V 3R llt]P                  ! 4       3R ltR tRtVtV ;t# )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                N   < V ^8  d   QhRS[ RS[S[,          RS[R,          RR/# )   dimr	   validate_argsNreturn)intr   floatbool)format__classdict__s   "x/Users/jameslopez/projects/CWCArchive/cwc-podcast/.venv/lib/python3.14/site-packages/torch/distributions/lkj_cholesky.py__annotate__LKJCholesky.__annotate__B   sA     B BB ~B d{	B
 
B    c                  < V^8  d   \        RV R24      hWn        \        V4      w  V n        V P                  P	                  4       p\
        P                  ! W34      pV P                  RV P                  ^,
          ,          ,           p\
        P                  ! V P                  ^,
          V P                  P                  V P                  P                  R7      p\
        P                  ! VP                  R4      V.4      pVR,           pVP                  R4      RV,          ,
          p	\        W4      V n        \        S
V `A  WEV4       R# )r   zDExpected dim to be an integer greater than or equal to 2. Found dim=.      ?dtypedeviceN)   )
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   &&&&      r   r*   LKJCholesky.__init__B   s    7VWZV[[\]   -m <	((--/jj#,**SDHHqL-AAHHqL$$**%%,,

 F,,T2F;<c\
",,R03<?
*1
=Ar   c                  < V P                  \        V4      p\        P                  ! V4      pV P                  Vn        V P
                  P                  V4      Vn        V P                  P                  WP                  3,           4      Vn        \        \        V`'  WP                  R R7       V P                  Vn        V# )F)r   )_get_checked_instancer   r"   r#   r   r	   expandr(   r)   r*   r-   _validate_args)r+   r,   	_instancenewr2   s   &&& r   r6   LKJCholesky.expand]   s    ((i@jj-(( ..55kBJJ%%kXXK&?@	k3()) 	) 	
 "00
r   c                   V P                   P                  V4      P                  R4      p\        P                  ! V P                  V4      VP                  VP                  R7      P                  R4      pW3P                  RRR7      ,          pVR,          P                  R4       \        P                  ! V4      V,          p\        P                  ! VP                  4      P                  p\        P                  ! ^\        P                  ! V^,          RR7      ,
          VR7      P                  4       pV\        P                   ! V4      ,          pV# )	r   r   T)r   keepdimg        r   )minr   ).    :NNN)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   r@   LKJCholesky.samplei   s     JJl+55b9;;  .aggahh

$r( 	 !==R=#FFi &&s+JJqMM)kk!''"''[[UYYq!t%<!<#FKKM
	Uj))r   c                4   V P                   '       d   V P                  V4       VP                  RRR7      R,          p\        P                  ! ^V P
                  ^,           V P                  P                  R7      p^V P                  ^,
          P                  R4      ,          V P
                  ,           V,
          p\        P                  ! W2P                  4       ,          RR7      pV P
                  ^,
          pV P                  RV,          ,           p\        P                  ! V4      V,          p\        P                  ! VR,
          V4      pRV,          \        P                  ! \        P                  4      ,          p	W,           V,
          p
WJ,
          # )r   )dim1dim2)r   r=   r   r   ).:r   NN)r7   _validate_samplediagonalr"   r$   r   r	   r   r'   rJ   loglgammamvlgammamathpi)r+   valuerR   orderunnormalized_log_pdfdm1alphadenominator	numeratorpi_constantnormalize_terms   &&         r   log_probLKJCholesky.log_prob~   s    !!%(^^"^5g>
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*   r6   r"   r#   r@   rh   __static_attributes____classdictcell____classcell__)r2   r   s   @@r   r   r      sS     $N '(<(<=O''GB B6
 #(**, *5 5r   )rn   r]   r"   r   torch.distributionsr   r    torch.distributions.distributionr   torch.distributions.utilsr   __all__r    r   r   <module>r{      s5       1 9 3 /A5, A5r   