
    zj                         d dl mZ d dlZd dlZd dlmZ d dlZd dlZd dl	m
Z
 d dlmZ erd dlmZ d dlmZ d dlmZmZ  G d	 d
e          ZdS )    )annotationsN)TYPE_CHECKING)	framework)TransformedDistribution)Sequence)Tensor)	TransformUniformc                       e Zd ZU dZded<   ded<   ded<   ded<   d fdZedd            Zedd            Zedd            Z	ddZ
ddZddZddZg fddZg fddZ xZS )Gumbela  The Gumbel distribution with location `loc` and `scale` parameters.

    Mathematical details

    The probability density function (pdf) is

    .. math::

        pdf(x; mu, sigma) = exp(-(x - mu) / sigma - exp(-(x - mu) / sigma)) / sigma


    In the above equation:

    * :math:`loc = \mu`: is the mean.
    * :math:`scale = \sigma`: is the std.

    Args:
        loc(int|float|tensor): The mean of gumbel distribution.The data type is int, float, tensor.
        scale(int|float|tensor): The std of gumbel distribution.The data type is int, float, tensor.

    Examples:
        .. code-block:: python

            >>> import paddle
            >>> from paddle.distribution.gumbel import Gumbel

            >>> # Gumbel distributed with loc=0, scale=1
            >>> dist = Gumbel(paddle.full([1], 0.0), paddle.full([1], 1.0))

            >>> # doctest: +SKIP
            >>> print(dist.sample([2]))
            Tensor(shape=[2, 1], dtype=float32, place=Place(cpu), stop_gradient=True,
            [[0.40484068],
            [3.19400501]])

            >>> print(dist.rsample([2]))
            Tensor(shape=[2, 1], dtype=float32, place=Place(cpu), stop_gradient=True,
            [[-0.95093185],
            [ 0.32422572]])

            >>> # doctest: -SKIP
            >>> value = paddle.full([1], 0.5)
            >>> print(dist.prob(value))
            Tensor(shape=[1], dtype=float32, place=Place(cpu), stop_gradient=True,
            [0.33070430])

            >>> print(dist.log_prob(value))
            Tensor(shape=[1], dtype=float32, place=Place(cpu), stop_gradient=True,
            [-1.10653067])

            >>> print(dist.cdf(value))
            Tensor(shape=[1], dtype=float32, place=Place(cpu), stop_gradient=True,
            [0.54523921])

            >>> print(dist.entropy())
            Tensor(shape=[1], dtype=float32, place=Place(cpu), stop_gradient=True,
            [1.57721567])
    r   locscaler
   	base_distztuple[Transform, ...]
transformsfloat | TensorreturnNonec           
     *   t          |t          j        t          j        t
          j        j        f          st          dt          |                     t          |t          j        t          j        t
          j        j        f          st          dt          |                     t          |t          j                  rt          j
        d|          }t          |t          j                  rt          j
        d|          }|j        |j        k    r$t          j        ||g          \  | _        | _        n||c| _        | _        t          j        d          }t
          j                            t          j        | j        t)          |j                            t          j        | j        t)          d|j        z
                                | _        d| _        t3                                          | j        | j                   d S )Nz5Expected type of loc is Real|Variable|Value, but got z7Expected type of scale is Real|Variable|Value, but got  )shape
fill_valuefloat32)dtype   )
isinstancenumbersRealr   VariablepaddlepirValue	TypeErrortypefullr   broadcast_tensorsr   r   npfinfodistributionr
   	full_likefloattinyepsr   r   super__init__)selfr   r   r'   	__class__s       j/lsinfo/ai/hellotax_ai/data_center/backend/venv/lib/python3.11/site-packages/paddle/distribution/gumbel.pyr.   zGumbel.__init__c   s   ',	 2FJ4DE
 
 	 SS		SS   GL)"4fj6FG
 
 	 W$u++WW   c7<(( 	8+B3777CeW\** 	<KbU;;;E9###)#;S%L#I#I DHdjj#& DHdjy))),44TXuUZ'8'899TXuQ]';';<<
 

 99999    c                :    | j         | j        t          j        z  z   S )u`  Mean of distribution

        The mean is

        .. math::

            mean = \mu + \sigma * γ

        In the above equation:

        * :math:`loc = \mu`: is the location parameter.
        * :math:`scale = \sigma`: is the scale parameter.
        * :math:`γ`: is the euler's constant.

        Returns:
            Tensor: mean value.

        )r   r   r&   euler_gammar/   s    r1   meanzGumbel.mean   s    ( x$*r~555r2   c                    t          j        | j        j        t          j        t          j        z  | j        j                  }t          j        | j        d          |z  dz  S )a  Variance of distribution.

        The variance is

        .. math::

            variance = \sigma^2 * \pi^2 / 6

        In the above equation:

        * :math:`scale = \sigma`: is the scale parameter.

        Returns:
            Tensor: The variance value.

        r   r   r         )	r   r$   r   r   mathpir   r   pow)r/   temps     r1   variancezGumbel.variance   sU    $ {(.w(*"
 
 
 z$*a((4/!33r2   c                4    t          j        | j                  S )a  Standard deviation of distribution

        The standard deviation is

        .. math::

            stddev = \sqrt{\sigma^2 * \pi^2 / 6}

        In the above equation:
        * :math:`scale = \sigma`: is the scale parameter.

        Returns:
            Tensor: std value
        )r   sqrtr?   r5   s    r1   stddevzGumbel.stddev   s      {4=)))r2   valuec                ,   | j         |                    | j         j                  z
  | j                            | j         j                  z  }t	          j        |t	          j        |          z
            | j                            |j                  z  S )zProbability density/mass function

        Args:
            value (Tensor): The input tensor.

        Returns:
            Tensor: probability.The data type is same with value.

        )r   astyper   r   r   exp)r/   rC   ys      r1   probzGumbel.prob   sv     XTX^444
8I8IHN9
 9
 
 z!fjmm+,,tz/@/@/I/IIIr2   c                P    t          j        |                     |                    S )zLog probability density/mass function.

        Args:
            value (Tensor): The input tensor.

        Returns:
            Tensor: log probability.The data type is same with value.

        )r   logrH   r/   rC   s     r1   log_probzGumbel.log_prob   s      z$))E**+++r2   c                    t          j        t          j        || j                            |j                  z
   | j                            |j                  z                       S )zCumulative distribution function.
        Args:
            value (Tensor): value to be evaluated.

        Returns:
            Tensor: cumulative probability of value.

        )r   rF   r   rE   r   r   rK   s     r1   cdfz
Gumbel.cdf   sa     zZ$(//%+6667*##EK001  
 
 	
r2   c                T    t          j        | j                  dz   t          j        z   S )z`Entropy of Gumbel distribution.

        Returns:
            Entropy of distribution.

        r   )r   rJ   r   r&   r4   r5   s    r1   entropyzGumbel.entropy   s"     z$*%%)BN::r2   r   Sequence[int]c                    t          j                    5  |                     |          cddd           S # 1 swxY w Y   dS )zSample from ``Gumbel``.

        Args:
            shape (Sequence[int], optional): The sample shape. Defaults to [].

        Returns:
            Tensor: A tensor with prepended dimensions shape.The data type is float32.

        N)r   no_gradrsample)r/   r   s     r1   samplezGumbel.sample   s     ^ 	' 	'<<&&	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	's   6::c                "   t           j                                        }t           j                            t          j        | j        j        d| j        j                  t          j	        | j                             }t           j                            | j        | j                   }|
                    |                    |
                    |                    | j                            |                                                  S )a   reparameterized sample
        Args:
            shape (Sequence[int], optional): 1D `int32`. Shape of the generated samples. Defaults to [].

        Returns:
            Tensor: A tensor with prepended dimensions shape.The data type is float32.

        r   r8   )r   r(   ExpTransformAffineTransformr$   r   r   r   r   	ones_likeforwardinverse_baserU   )r/   r   	exp_transaffine_trans_1affine_trans_2s        r1   rT   zGumbel.rsample
  s     '4466	,<<Kj&1DHN   dj)))	
 
  ,<<Htzk
 
 %%&&%%dj&7&7&>&>??  
 
 	
r2   )r   r   r   r   r   r   )r   r   )rC   r   r   r   )r   rQ   r   r   )__name__
__module____qualname____doc____annotations__r.   propertyr6   r?   rB   rH   rL   rN   rP   rU   rT   __classcell__)r0   s   @r1   r   r   "   sg        9 9v KKKMMM%%%%!: !: !: !: !: !:F 6 6 6 X6* 4 4 4 X42 * * * X*"J J J J 
, 
, 
, 
,
 
 
 
 ; ; ; ; -/ ' ' ' ' ' .0 
 
 
 
 
 
 
 
 
r2   r   )
__future__r   r;   r   typingr   numpyr&   r   paddle.baser   ,paddle.distribution.transformed_distributionr   collections.abcr   r   paddle.distributionr	   r
   r   r   r2   r1   <module>rn      s    # " " " " "                    ! ! ! ! ! ! P P P P P P 7((((((66666666B
 B
 B
 B
 B
$ B
 B
 B
 B
 B
r2   