o
    "j                     @   sL   d dl Z d dlZd dlZd dlZd dlmZ d dlmZ G dd deZ	dS )    N)	framework)TransformedDistributionc                       st   e Zd ZdZ fd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dd Zdd Z  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])
    c              	      s
  t |tjtjfstdt| t |tjtjfs$tdt| t |tjr1tjd|d}t |tjr>tjd|d}|j	|j	krPt
||g\| _| _n||| _| _tjdd}tjt| jt|jt| jtd|j | _d| _t | j| j d S )Nz/Expected type of loc is Real|Variable, but got z1Expected type of scale is Real|Variable, but got  )shape
fill_valueZfloat32)dtype   )
isinstancenumbersRealr   Variable	TypeErrortypepaddlefullr   Zbroadcast_tensorslocscalenpfinfodistributionZUniformZ	full_likefloatZtinyepsZ	base_distZ
transformssuper__init__)selfr   r   r   	__class__r   [/var/www/html/Deteccion_Ine/venv/lib/python3.10/site-packages/paddle/distribution/gumbel.pyr   U   s,   zGumbel.__init__c                 C   s   | j | jtj  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   r   r   r   meant   s   zGumbel.meanc                 C   s6   t j| jjtjtj | jjd}t | jd| d 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   tempr   r   r   variance   s   
zGumbel.variancec                 C   s   t | 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)   r    r   r   r   stddev   s   zGumbel.stddevc                 C   s*   | j | | j }t|t| | j S )zProbability density/mass function

        Args:
            value (Tensor): The input tensor.

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

        )r   r   r   exp)r   valueyr   r   r   prob   s   
zGumbel.probc                 C   s   t | |S )zLog probability density/mass function.

        Args:
            value (Tensor): The input tensor.

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

        )r   logr/   r   r-   r   r   r   log_prob   s   
zGumbel.log_probc                 C   s    t t || j  | j  S )zCumulative distribution function.
        Args:
            value (Tensor): value to be evaluated.

        Returns:
            Tensor: cumulative probability of value.

        )r   r,   r   r   r1   r   r   r   cdf   s    	z
Gumbel.cdfc                 C   s   t | jd tj S )z`Entropy of Gumbel distribution.

        Returns:
            Entropy of distribution.

        r	   )r   r0   r   r   r   r    r   r   r   entropy   s   zGumbel.entropyc                 C   s6   t   | |W  d   S 1 s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   Zno_gradrsample)r   r   r   r   r   sample   s   

$zGumbel.samplec                 C   sn   t j }t jt j| jjd| jjdt 	| j }t j| j| j }|
||
|| j|S )zreparameterized sample
        Args:
            shape (Sequence[int]): 1D `int32`. Shape of the generated samples.

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

        r   r"   )r   r   ZExpTransformZAffineTransformr   r   r   r   r   Z	ones_likeforwardZinverseZ_baser6   )r   r   Z	exp_transZaffine_trans_1Zaffine_trans_2r   r   r   r5      s"   
	
zGumbel.rsample)__name__
__module____qualname____doc__r   propertyr!   r)   r+   r/   r2   r3   r4   r6   r5   __classcell__r   r   r   r   r      s    ;


	r   )
r%   r   numpyr   r   Zpaddle.baser   Z,paddle.distribution.transformed_distributionr   r   r   r   r   r   <module>   s   