
    zj                        d dl mZ d dlZd dlmZ d dlZd dlmZmZ erd dl	m
Z
 d dlmZ  G d dej                  ZdS )	    )annotationsN)TYPE_CHECKING)	dirichletexponential_family)Sequence)Tensorc                       e Zd ZU dZded<   ded<   d fdZedd	            Zedd
            ZddZ	ddZ
g fddZddZedd            ZddZ xZS )Betaa
  
    Beta distribution parameterized by alpha and beta.

    In probability theory and statistics, the beta distribution is a family of
    continuous probability distributions defined on the interval [0, 1]
    parameterized by two positive shape parameters, denoted by alpha and beta,
    that appear as exponents of the random variable and control the shape of
    the distribution. The generalization to multiple variables is called a
    Dirichlet distribution.

    The probability density function (pdf) is

    .. math::

        f(x; \alpha, \beta) = \frac{1}{B(\alpha, \beta)}x^{\alpha-1}(1-x)^{\beta-1}

    where the normalization, B, is the beta function,

    .. math::

        B(\alpha, \beta) = \int_{0}^{1} t^{\alpha - 1} (1-t)^{\beta - 1}\mathrm{d}t


    Args:
        alpha (float|Tensor): Alpha parameter. It supports broadcast semantics.
            The value of alpha must be positive. When the parameter is a tensor,
            it represents multiple independent distribution with
            a batch_shape(refer to ``Distribution`` ).
        beta (float|Tensor): Beta parameter. It supports broadcast semantics.
            The value of beta must be positive(>0). When the parameter is tensor,
            it represent multiple independent distribution with
            a batch_shape(refer to ``Distribution`` ).

    Examples:

        .. code-block:: python

            >>> import paddle

            >>> # scale input
            >>> beta = paddle.distribution.Beta(alpha=0.5, beta=0.5)
            >>> print(beta.mean)
            Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True,
            0.50000000)

            >>> print(beta.variance)
            Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True,
            0.12500000)

            >>> print(beta.entropy())
            Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True,
            -0.24156499)

            >>> # tensor input with broadcast
            >>> beta = paddle.distribution.Beta(alpha=paddle.to_tensor([0.2, 0.4]), beta=0.6)
            >>> print(beta.mean)
            Tensor(shape=[2], dtype=float32, place=Place(cpu), stop_gradient=True,
            [0.25000000, 0.40000001])

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

            >>> print(beta.entropy())
            Tensor(shape=[2], dtype=float32, place=Place(cpu), stop_gradient=True,
            [-1.91923141, -0.38095081])
    r   alphabetafloat | TensorreturnNonec                   t          |t          j                  rt          j        g |          }t          |t          j                  rt          j        g |          }t          j        ||g          \  | _        | _        t          j	        t          j
        | j        | j        gd                    | _        t                                          | j        j                   d S )N)shape
fill_value)
isinstancenumbersRealpaddlefullbroadcast_tensorsr   r   r   	Dirichletstack
_dirichletsuper__init___batch_shape)selfr   r   	__class__s      h/lsinfo/ai/hellotax_ai/data_center/backend/venv/lib/python3.11/site-packages/paddle/distribution/beta.pyr   zBeta.__init__d   s    eW\** 	<KbU;;;EdGL)) 	:;RD999D & 8% G G
DI#-L$*di0"55
 
 	566666    c                0    | j         | j         | j        z   z  S )zMean of beta distribution.r   r   r    s    r"   meanz	Beta.means   s     zTZ$)344r#   c                v    | j         | j        z   }| j         | j        z  |                    d          |dz   z  z  S )zVariance of beat distribution      )r   r   pow)r    sums     r"   variancezBeta.variancex   s9     j49$zDI%sQw)?@@r#   valuec                P    t          j        |                     |                    S )zProbability density function evaluated at value

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

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

        Args:
            value (Tensor): Value to be evaluated

        Returns:
            Tensor: Log probability.
        g      ?r   )r   r1   r   r   r2   s     r"   r1   zBeta.log_prob   s/     ''eS5[5I2(N(NOOOr#   r   Sequence[int]c                    t          |t                    r|nt          |          }t          j        | j                            |          d         d          S )zSample from beta distribution with sample shape.

        Args:
            shape (Sequence[int], optional): Sample shape.

        Returns:
            Tensor, Sampled data with shape `sample_shape` + `batch_shape` + `event_shape`.
        ).r   r   )axis)r   tupler   squeezer   sample)r    r   s     r"   r:   zBeta.sample   sL     $E511CuU||~do44U;;FC"MMMMr#   c                4    | j                                         S )zYEntropy of dirichlet distribution

        Returns:
            Tensor: Entropy.
        )r   entropyr&   s    r"   r<   zBeta.entropy   s     &&(((r#   tuple[Tensor, Tensor]c                    | j         | j        fS Nr%   r&   s    r"   _natural_parameterszBeta._natural_parameters   s    
DI&&r#   xyc                    t          j        |          t          j        |          z   t          j        ||z             z
  S r?   )r   lgamma)r    rA   rB   s      r"   _log_normalizerzBeta._log_normalizer   s4    }Q&-"2"22V]1q55I5IIIr#   )r   r   r   r   r   r   )r   r   )r.   r   r   r   )r   r5   r   r   )r   r=   )rA   r   rB   r   r   r   )__name__
__module____qualname____doc____annotations__r   propertyr'   r-   r3   r1   r:   r<   r@   rE   __classcell__)r!   s   @r"   r
   r
      sC        B BH MMMLLL7 7 7 7 7 7 5 5 5 X5 A A A XA
	0 	0 	0 	0	P 	P 	P 	P -/ 
N 
N 
N 
N 
N) ) ) ) ' ' ' X'J J J J J J J Jr#   r
   )
__future__r   r   typingr   r   paddle.distributionr   r   collections.abcr   r   ExponentialFamilyr
    r#   r"   <module>rS      s    # " " " " "               = = = = = = = = ((((((QJ QJ QJ QJ QJ/ QJ QJ QJ QJ QJr#   