o
    Met                     @   s6   d dl Z d dlZd dlmZmZ G dd dejZdS )    N)	dirichletexponential_familyc                       sn   e Zd ZdZ fddZedd Zedd Zdd	 Zd
d Z	dddZ
dd Zedd Zdd Z  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=[1], dtype=float32, place=CUDAPlace(0), stop_gradient=True,
            #        [0.50000000])
            print(beta.variance)
            # Tensor(shape=[1], dtype=float32, place=CUDAPlace(0), stop_gradient=True,
            #        [0.12500000])
            print(beta.entropy())
            # Tensor(shape=[1], dtype=float32, place=CUDAPlace(0), stop_gradient=True,
            #        [0.12500000])

            # 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=CUDAPlace(0), stop_gradient=True,
            #        [0.25000000, 0.40000001])
            print(beta.variance)
            # Tensor(shape=[2], dtype=float32, place=CUDAPlace(0), stop_gradient=True,
            #        [0.10416666, 0.12000000])
            print(beta.entropy())
            # Tensor(shape=[2], dtype=float32, place=CUDAPlace(0), stop_gradient=True,
            #        [-1.91923141, -0.38095069])
    c                    s   t |tjrtjdg|d}t |tjrtjdg|d}t||g\| _| _t	t
| j| jgd| _tt| | jj d S )N   )shapeZ
fill_value)
isinstancenumbersRealpaddlefullZbroadcast_tensorsalphabetar   Z	Dirichletstack
_dirichletsuperr   __init__Z_batch_shape)selfr   r   	__class__ HD:\Projects\ConvertPro\env\Lib\site-packages\paddle/distribution/beta.pyr   U   s   zBeta.__init__c                 C   s   | j | j | j  S )z#Mean of beta distribution.
        r   r   r   r   r   r   meanc   s   z	Beta.meanc                 C   s*   | j | j }| j | j |d|d   S )z&Variance of beat distribution
           r   )r   r   pow)r   sumr   r   r   variancei   s   zBeta.variancec                 C   s   t | |S )zProbability density funciotn evaluated at value

        Args:
            value (Tensor): Value to be evaluated.
        
        Returns:
            Tensor: Probability.
        )r   explog_probr   valuer   r   r   probp   s   	z	Beta.probc                 C   s   | j t|d| gdS )zLog probability density funciton evaluated at value

        Args:
            value (Tensor): Value to be evaluated
        
        Returns:
            Tensor: Log probability.
        g      ?r   )r   r    r   r   r!   r   r   r   r    {   s   	zBeta.log_probr   c                 C   s0   t |tr|nt|}tj| j|d ddS )zSample from beta distribution with sample shape.

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

        Returns:
            Sampled data with shape `sample_shape` + `batch_shape` + `event_shape`.
        ).r   r   )Zaxis)r   tupler   Zsqueezer   sample)r   r   r   r   r   r%      s   	zBeta.samplec                 C   s
   | j  S )zYEntropy of dirichlet distribution

        Returns:
            Tensor: Entropy.
        )r   entropyr   r   r   r   r&      s   
zBeta.entropyc                 C   s   | j | jfS Nr   r   r   r   r   _natural_parameters   s   zBeta._natural_parametersc                 C   s"   t |t | t ||  S r'   )r   lgamma)r   xyr   r   r   _log_normalizer   s   "zBeta._log_normalizer)r   )__name__
__module____qualname____doc__r   propertyr   r   r#   r    r%   r&   r(   r,   __classcell__r   r   r   r   r      s    @



r   )r	   r   Zpaddle.distributionr   r   ZExponentialFamilyr   r   r   r   r   <module>   s   