
    zj                        d dl mZ d dlmZ d dlmZ d dlZd dlmZm	Z	 erd dlm
Z
  G d de	j                  Zd	 Zd
 ZdS )    )annotations)Iterable)TYPE_CHECKINGN)categoricaldistribution)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ddZ xZS )Multinomiala$  
    Multinomial distribution parameterized by :attr:`total_count` and
    :attr:`probs`.

    In probability theory, the multinomial distribution is a generalization of
    the binomial distribution, it models the probability of counts for each side
    of a k-sided die rolled n times. When k is 2 and n is 1, the multinomial is
    the bernoulli distribution, when k is 2 and n is grater than 1, it is the
    binomial distribution, when k is grater than 2 and n is 1, it is the
    categorical distribution.

    The probability mass function (PMF) for multinomial is

    .. math::

        f(x_1, ..., x_k; n, p_1,...,p_k) = \frac{n!}{x_1!...x_k!}p_1^{x_1}...p_k^{x_k}

    where, :math:`n` is number of trials, k is the number of categories,
    :math:`p_i` denote probability of a trial falling into each category,
    :math:`{\textstyle \sum_{i=1}^{k}p_i=1}, p_i \ge 0`, and :math:`x_i` denote
    count of each category.

    Args:
        total_count (int): Number of trials.
        probs (Tensor): Probability of a trial falling into each category. Last
            axis of probs indexes over categories, other axes index over batches.
            Probs value should between [0, 1], and sum to 1 along last axis. If
            the value over 1, it will be normalized to sum to 1 along the last
            axis.

    Examples:

    .. code-block:: python

        >>> import paddle
        >>> paddle.seed(2023)
        >>> multinomial = paddle.distribution.Multinomial(10, paddle.to_tensor([0.2, 0.3, 0.5]))
        >>> print(multinomial.sample((2, 3)))
        Tensor(shape=[2, 3, 3], dtype=float32, place=Place(cpu), stop_gradient=True,
            [[[1., 5., 4.],
              [0., 4., 6.],
              [1., 3., 6.]],
            [[2., 2., 6.],
              [0., 6., 4.],
              [3., 3., 4.]]])
    inttotal_countr   probsreturnNonec                   t          |t                    r|dk     rt          d          |                                dk     rt          d          ||                    dd          z  | _        || _        t          j        | 	                    |                    | _
        t                                          |j        d d         |j        dd                     d S )N   zBinput parameter total_count must be int type and grater than zero.z9probs parameter should not be none and over one dimensionT)keepdim)logits)
isinstancer   
ValueErrordimsumr   r   r   Categorical_probs_to_logits_categoricalsuper__init__shape)selfr   r   	__class__s      o/lsinfo/ai/hellotax_ai/data_center/backend/venv/lib/python3.11/site-packages/paddle/distribution/multinomial.pyr   zMultinomial.__init__M   s    +s++ 	{QT   99;;??K   UYYr4Y888
&'3((//
 
 
 	SbS)5;rss+;<<<<<    c                     | j         | j        z  S )z\mean of multinomial distribution.

        Returns:
            Tensor: mean value.
        )r   r   r   s    r!   meanzMultinomial.mean`   s     zD,,,r"   c                6    | j         | j        z  d| j        z
  z  S )zdvariance of multinomial distribution.

        Returns:
            Tensor: variance value.
        r   )r   r   r$   s    r!   variancezMultinomial.variancei   s     $*,DJ??r"   valuec                P    t          j        |                     |                    S )probability mass function evaluated at value.

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

        Returns:
            Tensor: probability of value.
        )paddleexplog_prob)r   r(   s     r!   probzMultinomial.probr   s      z$--..///r"   c                |   t          j        |          rt          j        || j        j                  }t          j        t          j        | j                  |g          \  }}t          j                    rd||dk    t          j        |          z  <   n:t           j	        
                    ||dk    t          j        |          z  d          }t          j        |                    d          dz             t          j        |dz                                 d          z
  ||z                      d          z   S )r*   r   r   r   )r+   
is_integercastr   dtypebroadcast_tensorslogin_dynamic_modeisinfstaticsetitemlgammar   )r   r(   r   s      r!   r-   zMultinomial.log_prob}   s    U## 	9Ktz'788E0Z
##U+
 
 !## 	<=FEQJ6<#7#7899]**!V(<(<=q F
 M%))B--!+,,mEAI&&**2../v~""2&&'	
r"   r   Iterable[int]c                r   t          |t                    st          d          | j                            | j        gt          |                    }t          j        j	        
                    || j        j        d                                       | j        j                                      d          S )zdraw sample data from multinomial distribution

        Args:
            sample_shape (list|tuple, optional): [description]. Defaults to [].
        z%sample shape must be Iterable object.r   r   )r   r   	TypeErrorr   sampler   listr+   nn
functionalone_hotr   r   r1   r2   r   )r   r   sampless      r!   r=   zMultinomial.sample   s     %** 	ECDDD#**D,<+KtE{{+KLLI (($*2B22FGGT$*"##SVV	
r"   c                &   t          j        g | j        | j        j                  }t          j        | j        dz   | j        j                                      ddt          | j        j                  z  z             dd         }t          j	        | 
                    ||                    }|| j                                        z  t          j        |dz             z
  |t          j        |dz             z                      ddg          z   S )	z`entropy of multinomial distribution

        Returns:
            Tensor: entropy value
        )r   
fill_valuer2   r   r2   )r   )r   Nr   r   )r+   fullr   r   r2   arangereshapelenr   r,   _binomial_logpmfr   entropyr9   r   )r   nsupportbinomial_pmfs       r!   rK   zMultinomial.entropy   s     K!19I
 
 
 -q 
(8
 
 

'%$TZ%5!6!666
7
7< z$"7"77"C"CDDD%--///&-A2F2FFFM'A+666;;QGDD
 	
r"   countc           	        |                      | j        d          }t          j        |dz             }t          j        |dz             }t          j        ||z
  dz             }|t	          |          z  |t          j        t          j        t          j        |                               z  z   |z
  }||z  |z
  |z
  |z
  S )NT)	is_binaryr   )r   r   r+   r9   _clip_by_zerolog1pr,   abs)r   rO   r(   r   factor_nfactor_k
factor_nmknorms           r!   rJ   zMultinomial._binomial_logpmf   s    &&tzT&BB=++=++]55=1#455
 M&)))fl6:vz&/A/A.A#B#BCCCD 	 v~(:5<<r"   )r   r   r   r   r   r   )r   r   )r(   r   r   r   )r   r:   r   r   )rO   r   r(   r   r   r   )__name__
__module____qualname____doc____annotations__r   propertyr%   r'   r.   r-   r=   rK   rJ   __classcell__)r    s   @r!   r
   r
      s        - -^ MMM= = = = = =& - - - X- @ @ @ X@	0 	0 	0 	0
 
 
 
8 -/ 
 
 
 
 
 
 
 
 
&= = = = = = = =r"   r
   c                4    t          j        | dz   |          S )Nr   rE   )r+   rG   )rO   r2   s     r!   _binomial_supportra      s    =%0000r"   c                h    |                      d          | z   |                      d          z
  dz  S )Nr   )min)max   )clip)xs    r!   rR   rR      s/    FFqFMMA1-22r"   )
__future__r   collections.abcr   typingr   r+   paddle.distributionr   r   r   Distributionr
   ra   rR    r"   r!   <module>rn      s    # " " " " " $ $ $ $ $ $              9 9 9 9 9 9 9 9 o= o= o= o= o=,+ o= o= o=d1 1 13 3 3 3 3r"   