
    Αi?                        S SK Jr  S SKrS SKJr  S SKJr  S SKrS SKJ	r	  S SK
Jr  \(       a  S SKJr  S SKJr   " S	 S
\R                  5      rSS jrSS jrg)    )annotationsN)Sequence)TYPE_CHECKING)convert_dtype)distribution)Tensor)_DTypeLiteralc                     ^  \ rS rSr% SrS\S'   S\S'   S\S'   S\S'   S	\S
'      S       SU 4S jjjr\SS j5       r\SS j5       r	/ 4SS jjr
/ 4SS jjrSS jrSS jrSS jrSS jrSrU =r$ )MultivariateNormal   a4  The Multivariate Normal distribution is a type multivariate continuous distribution defined on the real set, with parameter: `loc` and any one
of the following parameters characterizing the variance: `covariance_matrix`, `precision_matrix`, `scale_tril`.

Mathematical details

The probability density function (pdf) is

.. math::

    p(X ;\mu, \Sigma) = \frac{1}{\sqrt{(2\pi)^k |\Sigma|}} \exp(-\frac{1}{2}(X - \mu)^{\intercal} \Sigma^{-1} (X - \mu))

In the above equation:

* :math:`X`: is a k-dim random vector.
* :math:`loc = \mu`: is the k-dim mean vector.
* :math:`covariance_matrix = \Sigma`: is the k-by-k covariance matrix.

Args:
    loc(int|float|Tensor): The mean of Multivariate Normal distribution. If the input data type is int or float, the data type of `loc` will be
        convert to a 1-D Tensor the paddle global default dtype.
    covariance_matrix(Tensor|None): The covariance matrix of Multivariate Normal distribution. The data type of `covariance_matrix` will be convert
        to be the same as the type of loc.
    precision_matrix(Tensor|None): The inverse of the covariance matrix. The data type of `precision_matrix` will be convert to be the same as the
        type of loc.
    scale_tril(Tensor|None): The cholesky decomposition (lower triangular matrix) of the covariance matrix. The data type of `scale_tril` will be
        convert to be the same as the type of loc.

Examples:
    .. code-block:: python

        >>> import paddle
        >>> from paddle.distribution import MultivariateNormal
        >>> paddle.set_device("cpu")
        >>> paddle.seed(100)

        >>> rv = MultivariateNormal(loc=paddle.to_tensor([2.,5.]), covariance_matrix=paddle.to_tensor([[2.,1.],[1.,2.]]))

        >>> print(rv.sample([3, 2]))
        Tensor(shape=[3, 2, 2], dtype=float32, place=Place(cpu), stop_gradient=True,
        [[[-0.00339603,  4.31556797],
          [ 2.01385283,  4.63553190]],
         [[ 0.10132277,  3.11323833],
          [ 2.37435842,  3.56635118]],
         [[ 2.89701366,  5.10602522],
          [-0.46329355,  3.14768648]]])

        >>> print(rv.mean)
        Tensor(shape=[2], dtype=float32, place=Place(cpu), stop_gradient=True,
        [2., 5.])

        >>> print(rv.variance)
        Tensor(shape=[2], dtype=float32, place=Place(cpu), stop_gradient=True,
        [1.99999988, 2.        ])

        >>> print(rv.entropy())
        Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True,
        3.38718319)

        >>> rv1 = MultivariateNormal(loc=paddle.to_tensor([2.,5.]), covariance_matrix=paddle.to_tensor([[2.,1.],[1.,2.]]))
        >>> rv2 = MultivariateNormal(loc=paddle.to_tensor([-1.,3.]), covariance_matrix=paddle.to_tensor([[3.,2.],[2.,3.]]))
        >>> print(rv1.kl_divergence(rv2))
        Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True,
        1.55541301)
r   locTensor | Nonecovariance_matrixprecision_matrix
scale_trilr	   dtypec                  > [         R                  " 5       U l        [        U[        [
        45      (       a!  [         R                  " U/U R                  S9nO[        UR                  5      U l        UR                  5       S:  a  UR                  S5      nS U l
        S U l        S U l        US LUS L-   US L-   S:w  a  [        S5      eUb  UR                  5       S:  a  [        S5      e[         R                  " X@R                  S9n[         R                  " UR                   S S UR                   S S 5      nUR#                  / UQUR                   S   PUR                   S   P5      U l        GOLUb  UR                  5       S:  a  [        S	5      e[         R                  " X R                  S9n[         R                  " UR                   S S UR                   S S 5      nUR#                  / UQUR                   S   PUR                   S   P5      U l
        OUR                  5       S:  a  [        S
5      e[         R                  " X0R                  S9n[         R                  " UR                   S S UR                   S S 5      nUR#                  / UQUR                   S   PUR                   S   P5      U l        UR#                  / UQSP5      U l        U R$                  R                   SS  nUb  X@l        O8Ub%  [         R(                  R+                  U5      U l        O[-        U5      U l        [.        TU ]a  XV5        g )Nr      )r   zTExactly one of covariance_matrix or precision_matrix or scale_tril may be specified.   zZscale_tril matrix must be at least two-dimensional, with optional leading batch dimensionszZcovariance_matrix must be at least two-dimensional, with optional leading batch dimensionszYprecision_matrix must be at least two-dimensional, with optional leading batch dimensions)paddleget_default_dtyper   
isinstancefloatint	to_tensorr   dimreshaper   r   r   
ValueErrorcastbroadcast_shapeshapeexpandr   _unbroadcasted_scale_trillinalgcholeskyprecision_to_scale_trilsuper__init__)selfr   r   r   r   batch_shapeevent_shape	__class__s          g/var/www/html/banglarbhumi/venv/lib/python3.13/site-packages/paddle/distribution/multivariate_normal.pyr+   MultivariateNormal.__init__e   sE    --/
cE3<((""C5

;C&syy1DJ779q=++d#C!% $T)j.DED(
 f  !~~!# =   ZzzBJ 00  "%syy"~K )//J+Jz//3JZ5E5Eb5IJDO * $$&* =  !',=ZZ P 00!'',ciinK &7%=%= %++B/ &++B/&D"  ##%) =   &{{+;::N 00 &&s+SYYs^K %5$;$; $**2. %**2.%D! ::00R01hhnnRS)!-7**-3]]-C-C!.D* .E .D* 	2    c                    U R                   $ )zLMean of Multivariate Normal distribution.

Returns:
    Tensor: mean value.
)r   r,   s    r0   meanMultivariateNormal.mean   s     xxr2   c                    [         R                  " U R                  5      R                  S5      R	                  U R
                  U R                  -   5      $ )zTVariance of Multivariate Normal distribution.

Returns:
    Tensor: variance value.
r   )r   squarer&   sumr%   _batch_shape_event_shaper4   s    r0   varianceMultivariateNormal.variance   s?     MM$889SWVD%%(9(99:	
r2   c                    [         R                  " 5          U R                  U5      sSSS5        $ ! , (       d  f       g= f)a  Generate Multivariate Normal samples of the specified shape. The final shape would be ``sample_shape + batch_shape + event_shape``.

Args:
    shape (Sequence[int], optional): Prepended shape of the generated samples.

Returns:
    Tensor, Sampled data with shape `sample_shape` + `batch_shape` + `event_shape`. The data type is the same as `self.loc`.
N)r   no_gradrsample)r,   r$   s     r0   sampleMultivariateNormal.sample   s&     ^^<<& s   1
?c                ^   [        U[        5      (       d  [        S5      eU R                  U5      n[        R
                  " [        R                  " US9U R                  S9nU R                  [        R                  " U R                  UR                  S5      5      R                  S5      -   $ )r?   z%sample shape must be Sequence object.)r$   r   r   )r   r   	TypeError_extend_shaper   r"   normalr   r   matmulr&   	unsqueezesqueeze)r,   r$   output_shapeepss       r0   rA   MultivariateNormal.rsample   s     %**CDD))%0kk&--l;4::Nxx&--**CMM",=

'"+ 	r2   c                t   [         R                  " XR                  S9nXR                  -
  n[	        U R
                  U5      nU R
                  R                  SSS9R                  5       R                  S5      nSU R                  S   [        R                  " S[        R                  -  5      -  U-   -  U-
  $ )zLog probability density function.

Args:
  value (Tensor): The input tensor.

Returns:
  Tensor: log probability. The data type is the same as `self.loc`.
r   r   r   axis1axis2g      r   r   )r   r"   r   r   batch_mahalanobisr&   diagonallogr9   r;   mathpi)r,   valuediffMhalf_log_dets        r0   log_probMultivariateNormal.log_prob   s     E4xxd<<dC**33"B3GSUSW 	 D%%a(488AK+@@1DE	
r2   c                L    [         R                  " U R                  U5      5      $ )zProbability density function.

Args:
    value (Tensor): The input tensor.

Returns:
    Tensor: probability. The data type is the same as `self.loc`.
)r   expr[   )r,   rW   s     r0   probMultivariateNormal.prob  s     zz$--.//r2   c                \   U R                   R                  SSS9R                  5       R                  S5      nSU R                  S   -  S[
        R                  " S[
        R                  -  5      -   -  U-   n[        U R                  5      S:X  a  U$ UR                  U R                  5      $ )aB  Shannon entropy in nats.

The entropy is

.. math::

    \mathcal{H}(X) = \frac{n}{2} \log(2\pi) + \log {\det A} + \frac{n}{2}

In the above equation:

* :math:`\Omega`: is the support of the distribution.

Returns:
    Tensor, Shannon entropy of Multivariate Normal distribution. The data type is the same as `self.loc`.
r   r   rO         ?r   g      ?r   )
r&   rS   rT   r9   r;   rU   rV   lenr:   r%   )r,   rZ   Hs      r0   entropyMultivariateNormal.entropy  s    " **33"B3GSUSW 	 $##A&&#TWW0E*EF 	
 t  !Q&H88D--..r2   c                   U R                   UR                   :w  a%  U R                  UR                  :w  a  [        S5      eU R                  R	                  SSS9R                  5       R                  S5      nUR                  R	                  SSS9R                  5       R                  S5      n[        [        [        U R                  R                  5      5      5      nUS   US   sUS'   US'   [        R                  " U R                  U R                  R                  U5      5      n[        R                  " UR                  UR                  R                  U5      5      n[        R                  R                  Xe5      R	                  SSS9R                  S5      nU[!        UR                  U R"                  UR"                  -
  5      -  nUU-
  SXpR                  S   -
  -  -   $ )a  The KL-divergence between two poisson distributions with the same `batch_shape` and `event_shape`.

The probability density function (pdf) is

.. math::

    KL\_divergence(\lambda_1, \lambda_2) = \log(\det A_2) - \log(\det A_1) -\frac{n}{2} +\frac{1}{2}[tr [\Sigma_2^{-1} \Sigma_1] + (\mu_1 - \mu_2)^{\intercal} \Sigma_2^{-1}  (\mu_1 - \mu_2)]

Args:
    other (MultivariateNormal): instance of Multivariate Normal.

Returns:
    Tensor, kl-divergence between two Multivariate Normal distributions. The data type is the same as `self.loc`.

zmKL divergence of two Multivariate Normal distributions should share the same `batch_shape` and `event_shape`.r   r   rO   rb   r   )r:   r;   r!   r&   rS   rT   r9   listrangerc   r$   r   rH   	transposer'   solverR   r   )r,   otherhalf_log_det_1half_log_det_2new_perm	cov_mat_1	cov_mat_2expectations           r0   kl_divergence MultivariateNormal.kl_divergence/  s   " !3!33!!U%7%77  **33"B3GSUSW 	 ++442R4HSUSW 	
 c$"@"@"F"FGHI%-b\8B<"hrlMM****44X>
	 MM++++55h?
	
 MM	5XBbX)SW 	
 	(++TXX		-A
 	
 [#4#4Q#7789	
r2   )r&   r   r   r   r   r   )NNN)r   zfloat | Tensorr   r   r   r   r   r   )returnr   )r$   zSequence[int]ru   r   )rW   r   ru   r   )rl   r   ru   r   )__name__
__module____qualname____firstlineno____doc____annotations__r+   propertyr5   r<   rB   rA   r[   r_   re   rs   __static_attributes____classcell__)r/   s   @r0   r   r      s    ?B 
K$$##
 ,0*.$(U3U3 )U3 (	U3
 "U3 U3n   

 

 -/ 
' .0 "
.	0/<7
 7
r2   r   c                   [         R                  R                  [         R                  " U S5      5      n[         R                  " US5      n[	        [        [        UR                  5      5      5      nUS   US   sUS'   US'   [         R                  " X#5      n[         R                  " U R                  S   U R                  S9n[         R                  R                  XESS9nU$ )zConvert precision matrix to scale tril matrix

Args:
    P (Tensor): input precision matrix

Returns:
    Tensor: scale tril matrix
)r   r   r   r   r   Fupper)r   r'   r(   fliprh   ri   rc   r$   rj   eyer   triangular_solve)PLftmpro   L_invIdLs          r0   r)   r)   i  s     
		Ax 8	9B
++b(
#CE#cii.)*H!)"x|HRL(2,S+E	AGGBKqww	/B&&u&>AHr2   c                   UR                   S   nUR                   SS n[        U5      nU R                  5       S-
  nXE-
  nXe-   nUSU-  -   nUR                   SU n	[        U R                   SS UR                   US 5       H  u  pXU
-  U
4-  n	M     X4-  n	UR	                  U	5      n[        [        U5      5      [        [        XhS5      5      -   [        [        US-   US5      5      -   U/-   nUR                  U5      nU R	                  SX"45      nUR	                  SUR                   S   U45      nUR                  S5      n[        R                  R                  XSS	9R                  S5      R                  S5      nUR                  5       nUR	                  UR                   SS 5      n[        [        U5      5      n[        U5       H  nUUU-   UU-   /-  nM     UR                  U5      nUR	                  U5      $ )
aA  
Computes the squared Mahalanobis distance of the Multivariate Normal distribution with cholesky decomposition of the covariance matrix.
Accepts batches for both bL and bx.

Args:
    bL (Tensor): scale trial matrix (batched)
    bx (Tensor): difference vector(batched)

Returns:
    Tensor: squared Mahalanobis distance
r   Nr   r   r   r   )r   r   r   Fr   )r$   rc   r   zipr    rh   ri   rj   r   r'   r   powr9   t)bLbxnbx_batch_shapebx_batch_dimsbL_batch_dimsouter_batch_dimsold_batch_dimsnew_batch_dimsbx_new_shapesLsxpermute_dimsflat_Lflat_xflat_x_swapM_swaprY   
permuted_Mpermute_inv_dimsi
reshaped_Ms                         r0   rR   rR   |  s    	AXXcr]N 'MFFHqLM$4%5N%M(99N88--.LbhhsmRXX.>r%BCr2& DDL	L	!B 	U#$%
u%q9
:	;
u%)>1=
>	? 
	  
l	#BZZQ
#FZZV\\!_a01F""9-K&&v%&H	Q	R 
 	
A 288CR=)JE"234=!-1>A3EFF "%%J n--r2   )r   r   ru   r   )r   r   r   r   ru   r   )
__future__r   rU   collections.abcr   typingr   r   paddle.base.data_feederr   paddle.distributionr   r   paddle._typing.dtype_liker	   Distributionr   r)   rR    r2   r0   <module>r      sD    #  $    1 ,7I
22 I
X
&7.r2   