
    zj                        d dl mZ d dlm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rd d	lmZ d d
lmZmZ  G d dej                  ZdS )    )annotations)TYPE_CHECKINGN)distribution)
check_typeconvert_dtype)Variable)exponential_family)in_dynamic_mode)Sequence)Tensordtypec                       e Zd ZU dZded<   ded<   ded<   d fd	Zedd
            Zedd            ZddZ	ddZ
ddZg fddZg fddZddZddZddZ xZS ) Gammaa  
    Gamma distribution parameterized by :attr:`concentration` (aka "alpha") and :attr:`rate` (aka "beta").

    The probability density function (pdf) is

    .. math::

        f(x; \alpha, \beta, x > 0) = \frac{\beta^{\alpha}}{\Gamma(\alpha)} x^{\alpha-1}e^{-\beta x}

        \Gamma(\alpha)=\int_{0}^{\infty} x^{\alpha-1} e^{-x} \mathrm{~d} x, (\alpha>0)

    Args:
        concentration (float|Tensor): Concentration parameter. It supports broadcast semantics.
            The value of concentration must be positive. When the parameter is a tensor,
            it represents multiple independent distribution with
            a batch_shape(refer to :ref:`api_paddle_distribution_Distribution`).
        rate (float|Tensor): Rate parameter. It supports broadcast semantics.
            The value of rate must be positive. When the parameter is tensor,
            it represent multiple independent distribution with
            a batch_shape(refer to :ref:`api_paddle_distribution_Distribution`).

    Example:
        .. code-block:: python

            >>> import paddle

            >>> # scale input
            >>> gamma = paddle.distribution.Gamma(0.5, 0.5)
            >>> print(gamma.mean)
            Tensor(shape=[], dtype=float32, place=Place(gpu:0), stop_gradient=True,
                   1.)

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

            >>> print(gamma.entropy())
            Tensor(shape=[], dtype=float32, place=Place(gpu:0), stop_gradient=True,
                   0.78375685)

            >>> # tensor input with broadcast
            >>> gamma = paddle.distribution.Gamma(paddle.to_tensor([0.2, 0.4]), paddle.to_tensor(0.6))
            >>> print(gamma.mean)
            Tensor(shape=[2], dtype=float32, place=Place(gpu:0), stop_gradient=True,
                   [0.33333331, 0.66666663])

            >>> print(gamma.variance)
            Tensor(shape=[2], dtype=float32, place=Place(gpu:0), stop_gradient=True,
                   [0.55555552, 1.11111104])

            >>> print(gamma.entropy())
            Tensor(shape=[2], dtype=float32, place=Place(gpu:0), stop_gradient=True,
                   [-1.99634242,  0.17067254])
    r   concentrationrater   float | TensorreturnNonec                $   t                      s\t          |dt          t          t          j        j        fd           t          |dt          t          t          j        j        fd           |                     ||          r(|| _        || _	        t          |j                  | _        n;|                     ||          \  | _        | _	        t	          j                    | _        t                                          | j        j                   d S )Nr   r   r   )r
   r   floatr   paddlepirValue_validate_argsr   r   r   r   
_to_tensorget_default_dtypesuper__init__shape)selfr   r   	__class__s      i/lsinfo/ai/hellotax_ai/data_center/backend/venv/lib/python3.11/site-packages/paddle/distribution/gamma.pyr   zGamma.__init__\   s        	&*"23	   &*"23	   }d33 	4!.DDI&}':;;DJJ.2oot/ /+T  133DJ+122222    c                     | j         | j        z  S )zVMean of gamma distribution.

        Returns:
            Tensor: mean value.
        r   r   r    s    r"   meanz
Gamma.meanz   s     !DI--r#   c                F    | j         | j                            d          z  S )z^Variance of gamma distribution.

        Returns:
            Tensor: variance value.
           )r   r   powr&   s    r"   variancezGamma.variance   s      !DIMM!$4$444r#   valuec                P    t          j        |                     |                    S )zProbability density function evaluated at value

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

        Returns:
            Tensor: Probability.
        )r   explog_probr    r,   s     r"   probz
Gamma.prob   s      z$--..///r#   c                    | j         t          j        | j                  z  | j         dz
  t          j        |          z  z   | j        |z  z
  t          j        | j                   z
  S )zLog probability density function evaluated at value

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

        Returns:
            Tensor: Log probability.
           )r   r   logr   lgammar0   s     r"   r/   zGamma.log_prob   se     DI!6!66!A%E):)::;i%  mD.//0	
r#   c                    | j         t          j        | j                  z
  t          j        | j                   z   d| j         z
  t          j        | j                   z  z   S )zUEntropy of gamma distribution

        Returns:
            Tensor: Entropy.
        g      ?)r   r   r4   r   r5   digammar&   s    r"   entropyzGamma.entropy   s]     j##$mD.//0 T''6>$:L+M+MMN	
r#   r   Sequence[int]c                    t          j                    5  |                     |          cddd           S # 1 swxY 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   no_gradrsampler    r   s     r"   samplezGamma.sample   s     ^ 	' 	'<<&&	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	's   6::c                    t           j                            | |          }t          j        | j                            |                    | j                            |          z  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.
        )sample_shape)r   Distribution_extend_shaper   standard_gammar   expandr   r=   s     r"   r<   zGamma.rsample   si     )77u 8 
 
 $%%e,,
 
IU##$ 	$r#   otherc                   t          |t                    st          dt          |                     |j        t          j        | j        |j        z            z  }t          j        |j                  t          j        | j                  z
  }| j        |j        z
  t          j	        | j                  z  }|j        | j        z
  | j        | j        z  z  }||z   |z   |z   S )zThe KL-divergence between two gamma distributions.

        Args:
            other (Gamma): instance of Gamma.

        Returns:
            Tensor: kl-divergence between two gamma distributions.
        z/Expected type of other is Exponential, but got )

isinstancer   	TypeErrortyper   r   r4   r   r5   r7   )r    rE   t1t2t3t4s         r"   kl_divergencezGamma.kl_divergence   s     %'' 	O$u++OO    6:di%*.D#E#EE]5.//&-3
 3
 
  5#66&.;
 ;
 
 j49$);di)GHBw|b  r#   c                &    | j         dz
  | j         fS Nr3   r%   r&   s    r"   _natural_parameterszGamma._natural_parameters   s    "Q&
33r#   xyc                    t          j        |dz             |dz   t          j        |                                           z  z   S rP   )r   r5   r4   
reciprocal)r    rR   rS   s      r"   _log_normalizerzGamma._log_normalizer   s8    }QU##q1u
ALLNN?0K0K&KKKr#   )r   r   r   r   r   r   )r   r   )r,   r   r   r   )r   r9   r   r   )rE   r   r   r   )rR   r   rS   r   r   r   )__name__
__module____qualname____doc____annotations__r   propertyr'   r+   r1   r/   r8   r>   r<   rN   rQ   rV   __classcell__)r!   s   @r"   r   r       sf        5 5n LLLLLL3 3 3 3 3 3< . . . X. 5 5 5 X5	0 	0 	0 	0
 
 
 
 
 
 
 
 -/ 
' 
' 
' 
' 
' .0 $ $ $ $ $ ! ! ! !04 4 4 4L L L L L L L Lr#   r   )
__future__r   typingr   r   r   paddle.base.data_feederr   r   paddle.base.frameworkr   paddle.distributionr	   paddle.frameworkr
   collections.abcr   r   r   ExponentialFamilyr    r#   r"   <module>rg      s   # " " " " "                    = = = = = = = = * * * * * * 2 2 2 2 2 2 , , , , , , %(((((($$$$$$$$LL LL LL LL LL0 LL LL LL LL LLr#   