
    Αi                        S SK Jr  S SKJr  S SKJr  S SKrS SKJrJ	r	  \(       a  S SKJ
r
   " S S\	R                  5      rS	 rS
 rg)    )annotations)Iterable)TYPE_CHECKINGN)categoricaldistribution)Tensorc                     ^  \ rS rSr% SrS\S'   S\S'   SU 4S jjr\SS j5       r\SS	 j5       r	SS
 jr
SS jr/ 4SS jjrSS jrSS jrSrU =r$ )Multinomial   a  
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   probsc                n  > [        U[        5      (       a  US:  a  [        S5      eUR                  5       S:  a  [        S5      eX"R	                  SSS9-  U l        Xl        [        R                  " U R                  U5      S9U l
        [        TU ]1  UR                  S S UR                  SS  5        g )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      _/var/www/html/banglarbhumi/venv/lib/python3.13/site-packages/paddle/distribution/multinomial.pyr   Multinomial.__init__M   s    +s++{QT  99;?K  YYr4Y88
&'33((/
 	Sb)5;;rs+;<    c                4    U R                   U R                  -  $ )zDmean of multinomial distribution.

Returns:
    Tensor: mean value.
)r   r   r   s    r    meanMultinomial.mean`   s     zzD,,,,r"   c                T    U R                   U R                  -  SU R                  -
  -  $ )zLvariance of multinomial distribution.

Returns:
    Tensor: variance value.
r   )r   r   r$   s    r    varianceMultinomial.variancei   s&     $**,DJJ??r"   c                L    [         R                  " U R                  U5      5      $ )probability mass function evaluated at value.

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

Returns:
    Tensor: probability of value.
)paddleexplog_prob)r   values     r    probMultinomial.probr   s     zz$--.//r"   c                   [         R                  " U5      (       a*  [         R                  " XR                  R                  5      n[         R
                  " [         R                  " U R                  5      U/5      u  p![         R                  " 5       (       a  SX!S:H  [         R                  " U5      -  '   O:[         R                  R                  X!S:H  [         R                  " U5      -  S5      n[         R                  " UR                  S5      S-   5      [         R                  " US-   5      R                  S5      -
  X-  R                  S5      -   $ )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.   Multinomial.log_prob}   s     U##KKzz'7'78E00ZZ

#U+
 !!##<=FQJ6<<#789]]**!V(<=qF
 MM%))B-!+,mmEAI&**2./~""2&'	
r"   c                   [        U[        5      (       d  [        S5      eU R                  R	                  U R
                  /[        U5      Q5      n[        R                  R                  R                  X R                  R                  S   5      R                  U R                  R                  5      R                  S5      $ )z~draw 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   r4   r5   r   )r   r   sampless      r    r@   Multinomial.sample   s     %**CDD##**D,<,<+KtE{+KLII  ((**2B2B22FGT$**""#SV	
r"   c                R   [         R                  " / U R                  U R                  R                  S9n[         R
                  " U R                  S-   U R                  R                  S9R                  SS[        U R                  R                  5      -  -   5      SS n[         R                  " U R                  X5      5      nXR                  R                  5       -  [         R                  " US-   5      -
  U[         R                  " US-   5      -  R                  SS/5      -   $ )	zHentropy of multinomial distribution

Returns:
    Tensor: entropy value
)r   
fill_valuer5   r   r5   )r   )r   Nr   r   )r,   fullr   r   r5   arangereshapelenr   r-   _binomial_logpmfr   entropyr<   r   )r   nsupportbinomial_pmfs       r    rO   Multinomial.entropy   s     KK!1!19I9I
 --q 

(8(8

'%$TZZ%5%5!666
7< zz$"7"7"CD%%--//&--A2FFFMM'A+66;;QGD
 	
r"   c           	        U R                  U R                  SS9n[        R                  " US-   5      n[        R                  " US-   5      n[        R                  " X-
  S-   5      nU[	        U5      -  U[        R
                  " [        R                  " [        R                  " U5      * 5      5      -  -   U-
  nX#-  U-
  U-
  U-
  $ )NT)	is_binaryr   )r   r   r,   r<   _clip_by_zerolog1pr-   abs)r   countr/   r   factor_nfactor_k
factor_nmknorms           r    rN   Multinomial._binomial_logpmf   s    &&tzzT&B==+==+]]5=1#45
 M&))fll6::vzz&/A.A#BCCD 	 ~(:5<<r"   )r   r   r   )r   r   r   r   returnNone)r_   r   )r/   r   r_   r   )r   zIterable[int]r_   r   )rY   r   r/   r   r_   r   )__name__
__module____qualname____firstlineno____doc____annotations__r   propertyr%   r(   r0   r.   r@   rO   rN   __static_attributes____classcell__)r   s   @r    r
   r
      sh    -^ M=& - - @ @	0
8 -/ 
 
&= =r"   r
   c                2    [         R                  " U S-   US9$ )Nr   rI   )r,   rK   )rY   r5   s     r    _binomial_supportrk      s    ==%00r"   c                L    U R                  SS9U -   U R                  SS9-
  S-  $ )Nr   )min)max   )clip)xs    r    rV   rV      s+    FFqFMA1-22r"   )
__future__r   collections.abcr   typingr   r,   paddle.distributionr   r   r   Distributionr
   rk   rV    r"   r    <module>rx      s;    # $    9o=,++ o=d13r"   