
    קg                     d    d Z ddlZddl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dS )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                   z     e Zd ZdZdej        iZej        Zd	 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
    concentration      ?Nc                 d   |dk     rt          d| d          || _        t          |          \  | _        | j                                        }t          j        ||f          }| j        d| j        dz
  z  z   }t          j        | j        dz
  | j        j        | j        j	                  }t          j
        |                    d          |g          }|dz   }|                    d          d|z  z
  }	t          ||	          | _        t                                          |||           d S )	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             \/var/www/html/ai-engine/env/lib/python3.11/site-packages/torch/distributions/lkj_cholesky.pyr   zLKJCholesky.__init__?   s5   77]WZ]]]    -m < <	(--//j#s,,*SDHqL-AAHqL$*%,
 
 

 F,,T22F;<<c\
",,R003<?
*j11
k=AAAAA    c                    |                      t          |          }t          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)   r-   zLKJCholesky.expandU   s    ((i@@j--( .55kBBJ%%kTXK&?@@	k3(() 	) 	
 	
 	
 "0
r*   c                 ~   | j                             |                              d          }t          j        |                     |          |j        |j                                      d          }||	                    dd          z  }|ddd d f         
                    d           t          j        |          |z  }t          j        |j                  j        }t          j        dt          j        |d	z  d
          z
  |                                          }|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)   r5   zLKJCholesky.samplea   s    Jl++55b99;  ..agah
 
 

$r(( 	 !8==R=#F#FFc1aaai &&s+++JqMMM)k!'""'[UYq!t%<%<%<!<#FFFKKMM
	Uj)))r*   c                 h   | j         r|                     |           |                    dd          ddd f         }t          j        d| j        dz   | j        j                  }d| j        dz
                      d          z  | j        z   |z
  }t          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   r3   r   )r.   _validate_samplediagonalr   r   r   r	   r   r   r?   loglgammamvlgammamathpi)r    valuerG   orderunnormalized_log_pdfdm1alphadenominator	numeratorpi_constantnormalize_terms              r)   log_probzLKJCholesky.log_probv   s2     	)!!%(((^^"^55c122g>
Q1T5G5NOOOT'!+66r:::TXEM$y1A1A)ArJJJhl"S3Y.l5))C/N53;44	 Ci$(47"3"33$y0;>#n44r*   )r
   N)N)__name__
__module____qualname____doc__r   positivearg_constraintscorr_choleskysupportr   r-   r   r   r5   r\   __classcell__)r(   s   @r)   r   r      s        $ $J '(<=O'GB B B B B B,
 
 
 
 
 
 #-%*,,    *5 5 5 5 5 5 5r*   )r`   rQ   r   torch.distributionsr   r    torch.distributions.distributionr   torch.distributions.utilsr   __all__r    r*   r)   <module>rk      s       1 1 1 1 1 1 1 1 9 9 9 9 9 9 3 3 3 3 3 3 /z5 z5 z5 z5 z5, z5 z5 z5 z5 z5r*   