
    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 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  G d
 dej                  ZdddZdS )    )annotationsN)TYPE_CHECKING)check_variable_and_dtype)LayerHelper)exponential_family)in_dynamic_or_pir_mode)Sequence)Tensorc                       e Zd ZU dZded<   d fdZedd            Zedd            Zg fddZ	ddZ
ddZddZedd            ZddZ xZS )	Dirichletaa  
    Dirichlet distribution with parameter "concentration".

    The Dirichlet distribution is defined over the `(k-1)-simplex` using a
    positive, length-k vector concentration(`k > 1`).
    The Dirichlet is identically the Beta distribution when `k = 2`.

    For independent and identically distributed continuous random variable
    :math:`\boldsymbol X \in R_k` , and support
    :math:`\boldsymbol X \in (0,1), ||\boldsymbol X|| = 1` ,
    The probability density function (pdf) is

    .. math::

        f(\boldsymbol X; \boldsymbol \alpha) = \frac{1}{B(\boldsymbol \alpha)} \prod_{i=1}^{k}x_i^{\alpha_i-1}

    where :math:`\boldsymbol \alpha = {\alpha_1,...,\alpha_k}, k \ge 2` is
    parameter, the normalizing constant is the multivariate beta function.

    .. math::

        B(\boldsymbol \alpha) = \frac{\prod_{i=1}^{k} \Gamma(\alpha_i)}{\Gamma(\alpha_0)}

    :math:`\alpha_0=\sum_{i=1}^{k} \alpha_i` is the sum of parameters,
    :math:`\Gamma(\alpha)` is gamma function.

    Args:
        concentration (Tensor): "Concentration" parameter of dirichlet
            distribution, also called :math:`\alpha`. When it's over one
            dimension, the last axis denotes the parameter of distribution,
            ``event_shape=concentration.shape[-1:]`` , axes other than last are
            consider batch dimensions with ``batch_shape=concentration.shape[:-1]`` .

    Examples:

        .. code-block:: python

            >>> import paddle
            >>> dirichlet = paddle.distribution.Dirichlet(paddle.to_tensor([1., 2., 3.]))
            >>> print(dirichlet.entropy())
            Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True,
            -1.24434423)

            >>> print(dirichlet.prob(paddle.to_tensor([.3, .5, .6])))
            Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True,
            10.80000019)
    r
   concentrationreturnNonec                   |                                 dk     st          j        |j                  dk    rt	          d          || _        t                                          |j        d d         |j        dd                     d S )N   r   z:`concentration` parameter must be at least one dimensional)dimmathprodshape
ValueErrorr   super__init__)selfr   	__class__s     m/lsinfo/ai/hellotax_ai/data_center/backend/venv/lib/python3.11/site-packages/paddle/distribution/dirichlet.pyr   zDirichlet.__init__S   s    ""di0C&D&D&I&IL   +,SbS1=3Frss3KLLLLL    c                J    | j         | j                             dd          z  S )zbMean of Dirichlet distribution.

        Returns:
            Mean value of distribution.
        r   Tkeepdim)r   sumr   s    r   meanzDirichlet.mean]   s(     !D$6$:$:2t$:$L$LLLr   c                    | j                             dd          }| j         || j         z
  z  |                    d          |dz   z  z  S )zjVariance of Dirichlet distribution.

        Returns:
            Variance value of distribution.
        r   Tr      r   )r   r!   pow)r   concentration0s     r   variancezDirichlet.variancef   sW     +//D/AA"nt7I&IJq!!^a%78
 	
r   r   Sequence[int]c                    t          |t                    r|nt          |          }t          | j                            |                     |                              S )zSample from dirichlet distribution.

        Args:
            shape (Sequence[int], optional): Sample shape. Defaults to empty list.
        )
isinstancetuple
_dirichletr   expand_extend_shape)r   r   s     r   samplezDirichlet.sampler   sO     $E511CuU||$,33D4F4Fu4M4MNNOOOr   valuec                P    t          j        |                     |                    S )zProbability density function(PDF) evaluated at value.

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

        Returns:
            PDF evaluated at value.
        )paddleexplog_probr   r1   s     r   probzDirichlet.prob{   s      z$--..///r   c                   t          j        |          | j        dz
  z                      d          t          j        | j                            d                    z   t          j        | j                                      d          z
  S )zoLog of probability density function.

        Args:
            value (Tensor): Value to be evaluated.
              ?r   )r3   logr   r!   lgammar6   s     r   r5   zDirichlet.log_prob   sw     Z$"4s":;@@DDmD.22266778mD.//33B778	
r   c                   | j                             d          }| j         j        d         }t          j        | j                                       d          t          j        |          z
  ||z
  t          j        |          z  z
  | j         dz
  t          j        | j                   z                      d          z
  S )zbEntropy of Dirichlet distribution.

        Returns:
            Entropy of distribution.
        r   r9   )r   r!   r   r3   r;   digamma)r   r'   ks      r   entropyzDirichlet.entropy   s     +//33$R(M$,--11"55mN++,>!V^N%C%CCD #c)V^D<N-O-OOc"gg	
r   tuple[Tensor]c                    | j         fS N)r   r"   s    r   _natural_parameterszDirichlet._natural_parameters   s    "$$r   xc                    |                                                     d          t          j         |                    d                    z
  S )Nr   )r;   r!   r3   )r   rD   s     r   _log_normalizerzDirichlet._log_normalizer   s5    xxzz~~b!!FM!%%))$<$<<<r   )r   r
   r   r   )r   r
   )r   r)   r   r
   )r1   r
   r   r
   )r   r@   )rD   r
   r   r
   )__name__
__module____qualname____doc____annotations__r   propertyr#   r(   r0   r7   r5   r?   rC   rF   __classcell__)r   s   @r   r   r       s5        . .` M M M M M M M M M XM 	
 	
 	
 X	
 -/ P P P P P	0 	0 	0 	0

 

 

 


 
 
 
" % % % X%= = = = = = = =r   r   r   r
   name
str | Noner   c                .   t                      rt          j                            |           S d}t	          | dg d|           t          |fi t                      }|                    | j                  }|	                    |d| id|ii            |S )N	dirichletr   )float16float32float64uint16)dtypeAlphaOut)typeinputsoutputsattrs)
r   r3   _C_opsrQ   r   r   locals"create_variable_for_type_inferencerV   	append_op)r   rN   op_typehelperouts        r   r-   r-      s     }&&}555 777		
 	
 	
 W111177% 8 
 
 	]+CL	 	 	
 	
 	
 
r   rB   )r   r
   rN   rO   r   r
   )
__future__r   r   typingr   r3   paddle.base.data_feederr   paddle.base.layer_helperr   paddle.distributionr   paddle.frameworkr   collections.abcr	   r
   ExponentialFamilyr   r-    r   r   <module>rm      s   # " " " " "               < < < < < < 0 0 0 0 0 0 2 2 2 2 2 2 3 3 3 3 3 3 ((((((H= H= H= H= H="4 H= H= H=V      r   