o
    * i                     @  s   d dl mZ d dlmZ d dlZd dlZd dlmZ d dlm	Z	m
Z
 d dlmZ d dlmZ d dlmZ erDd d	lmZ d d
lmZmZ G dd dejZdS )    )annotations)TYPE_CHECKINGN)distribution)
check_typeconvert_dtype)Variable)exponential_family)in_dynamic_mode)Sequence)Tensordtypec                      s   e Zd ZU dZded< ded< d( fdd	Zed)d
dZed)ddZg fd*ddZ	g fd*ddZ
d+ddZd+ddZd)ddZd+ddZd+ddZd,d d!Zed-d#d$Zd.d&d'Z  ZS )/Exponentiala  
    Exponential distribution parameterized by :attr:`rate`.

    The probability density function (pdf) is

    .. math::

        f(x; \theta) =  \theta e^{- \theta x },  (x \ge 0) $$

    In the above equation:

    * :math:`rate = \theta`: is the rate parameter.

    Args:
        rate (float|Tensor): Rate parameter. The value of rate must be positive.

    Example:
        .. code-block:: python

            >>> import paddle

            >>> expon = paddle.distribution.Exponential(paddle.to_tensor([0.5]))
            >>> print(expon.mean)
            Tensor(shape=[1], dtype=float32, place=Place(gpu:0), stop_gradient=True,
                   [2.])

            >>> print(expon.variance)
            Tensor(shape=[1], dtype=float32, place=Place(gpu:0), stop_gradient=True,
                   [4.])

            >>> print(expon.entropy())
            Tensor(shape=[1], dtype=float32, place=Place(gpu:0), stop_gradient=True,
                   [1.69314718])
    r   rater   float | TensorreturnNonec                   sh   t  st|dtttjjfd | |r|| _t	|j
| _
n| |\| _t | _
t | jj d S )Nr   r   )r	   r   floatr   paddleZpirValueZ_validate_argsr   r   r   Z
_to_tensorZget_default_dtypesuper__init__shape)selfr   	__class__ k/home/app/PaddleOCR-VL-test/.venv_paddleocr/lib/python3.10/site-packages/paddle/distribution/exponential.pyr   I   s   

zExponential.__init__c                 C  s
   | j  S )z\Mean of exponential distribution.

        Returns:
            Tensor: mean value.
        )r   Z
reciprocalr   r   r   r   mean\   s   
zExponential.meanc                 C  s   | j dS )zdVariance of exponential distribution.

        Returns:
            Tensor: variance value.
        )r   powr   r   r   r   variancee   s   zExponential.variancer   Sequence[int]c                 C  s6   t   | |W  d   S 1 sw   Y  dS )zGenerate samples of the specified shape.

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

        Returns:
            Tensor, A tensor with prepended dimensions shape. The data type is float32.
        N)r   Zno_gradrsample)r   r   r   r   r   samplen   s   
	$zExponential.samplec                 C  sF   t jj| |d}tj|ttjddjd| j	j
d}t| | j	 S )a  Generate reparameterized samples of the specified shape.

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

        Returns:
            Tensor: A tensor with prepended dimensions shape. The data type is float32.
        )Zsample_shapeZfloat32)r         ?)r   minmaxr   )r   DistributionZ_extend_shaper   uniformr   npZfinfoZtinyr   r   log)r   r   r)   r   r   r   r#   z   s   	zExponential.rsamplevaluec                 C  s   | j t| j  |  S )a	  Probability density function evaluated at value.

        .. math::

            { f(x; \theta) = \theta e^{- \theta x}, (x \ge 0 ) }

        Args:
            value (float|Tensor): Value to be evaluated.

        Returns:
            Tensor: Probability.
        )r   r   expr   r,   r   r   r   prob   s   zExponential.probc                 C  s   t | j| j|  S )zLog probability density function evaluated at value.

        Args:
            value (float|Tensor): Value to be evaluated

        Returns:
            Tensor: Log probability.
        r   r+   r   r.   r   r   r   log_prob   s   	zExponential.log_probc                 C  s   dt | j S )z\Entropy of exponential distribution.

        Returns:
            Tensor: Entropy.
        r%   r0   r   r   r   r   entropy   s   zExponential.entropyc                 C  s   dt | j |  S )a  Cumulative distribution function(CDF) evaluated at value.

        .. math::


            { cdf(x; \theta) = 1 - e^{- \theta x }, (x \ge 0) }

        Args:
            value (float|Tensor): Value to be evaluated.

        Returns:
            Tensor: CDF evaluated at value.
        r%   )r   r-   r   r.   r   r   r   cdf   s   zExponential.cdfc                 C  s   t |  | j S )a2  Inverse cumulative distribution function(CDF) evaluated at value.

        .. math::


            { icdf(x; \theta) = -\frac{ 1 }{ \theta } ln(1 + x), (x \ge 0) }

        Args:
            value (float|Tensor): Value to be evaluated.

        Returns:
            Tensor: CDF evaluated at value.
        )r   log1pr   r.   r   r   r   icdf   s   zExponential.icdfotherc                 C  s@   t |tstdt| |j| j }t| }|| d S )zThe KL-divergence between two exponential distributions.

        Args:
            other (Exponential): instance of Exponential.

        Returns:
            Tensor: kl-divergence between two exponential distributions.
        z/Expected type of other is Exponential, but got    )
isinstancer   	TypeErrortyper   r   r+   )r   r6   Z
rate_ratiot1r   r   r   kl_divergence   s   
	zExponential.kl_divergencetuple[Tensor]c                 C  s
   | j  fS N)r   r   r   r   r   _natural_parameters   s   
zExponential._natural_parametersxc                 C  s   t |  S r>   )r   r+   )r   r@   r   r   r   _log_normalizer   s   zExponential._log_normalizer)r   r   r   r   )r   r   )r   r"   r   r   )r,   r   r   r   )r6   r   r   r   )r   r=   )r@   r   r   r   )__name__
__module____qualname____doc____annotations__r   propertyr   r!   r$   r#   r/   r1   r2   r3   r5   r<   r?   rA   __classcell__r   r   r   r   r   "   s(   
 #





r   )
__future__r   typingr   numpyr*   r   r   Zpaddle.base.data_feederr   r   Zpaddle.base.frameworkr   Zpaddle.distributionr   Zpaddle.frameworkr	   collections.abcr
   r   r   ZExponentialFamilyr   r   r   r   r   <module>   s   