
    Α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  \(       a  S SKJ	r	  S SK
Jr   " S S	\R                  5      rg)
    )annotations)Sequence)TYPE_CHECKINGN)distribution)Tensor)_DTypeLiteralc                     ^  \ rS rSr% SrS\S'   S\S'   S\S'         SU 4S jjr      SS	 jr\SS
 j5       r	\SS j5       r
/ 4SS jjrSS jrSS jrSS jrSS jrSS jrSrU =r$ )Binomial   ao  
The Binomial distribution with size `total_count` and `probs` parameters.

In probability theory and statistics, the binomial distribution is the most basic discrete probability distribution defined on :math:`[0, n] \cap \mathbb{N}`,
which can be viewed as the number of times a potentially unfair coin is tossed to get heads, and the result
of its random variable can be viewed as the sum of a series of independent Bernoulli experiments.

The probability mass function (pmf) is

.. math::

    pmf(x; n, p) = \frac{n!}{x!(n-x)!}p^{x}(1-p)^{n-x}

In the above equation:

* :math:`total\_count = n`: is the size, meaning the total number of Bernoulli experiments.
* :math:`probs = p`: is the probability of the event happening in one Bernoulli experiments.

Args:
    total_count(int|Tensor): The size of Binomial distribution which should be greater than 0, meaning the number of independent bernoulli
        trials with probability parameter :math:`p`. The data type will be converted to 1-D Tensor with paddle global default dtype if the input
        :attr:`probs` is not Tensor, otherwise will be converted to the same as :attr:`probs`.
    probs(float|Tensor): The probability of Binomial distribution which should reside in [0, 1], meaning the probability of success
        for each individual bernoulli trial. If the input data type is float, it will be converted to a 1-D Tensor with paddle global default dtype.

Examples:
    .. code-block:: python

        >>> import paddle
        >>> from paddle.distribution import Binomial
        >>> paddle.set_device('cpu')
        >>> paddle.seed(100)
        >>> rv = Binomial(100, paddle.to_tensor([0.3, 0.6, 0.9]))

        >>> print(rv.sample([2]))
        Tensor(shape=[2, 3], dtype=float32, place=Place(cpu), stop_gradient=True,
        [[31., 62., 93.],
         [29., 54., 91.]])

        >>> print(rv.mean)
        Tensor(shape=[3], dtype=float32, place=Place(cpu), stop_gradient=True,
        [30.00000191, 60.00000381, 90.        ])

        >>> print(rv.entropy())
        Tensor(shape=[3], dtype=float32, place=Place(cpu), stop_gradient=True,
        [2.94053698, 3.00781751, 2.51124287])
r   dtyper   total_countprobsc                   > [         R                  " 5       U l        U R                  X5      u  U l        U l        U R                  R                  n[        TU ]!  U5        g )N)	paddleget_default_dtyper   
_to_tensorr   r   shapesuper__init__)selfr   r   batch_shape	__class__s       \/var/www/html/banglarbhumi/venv/lib/python3.13/site-packages/paddle/distribution/binomial.pyr   Binomial.__init__Q   sK     --/
'+{'J$$*&&,,%    c                ^   [        U[        5      (       a  [        R                  " X R                  S9nOUR                  U l        [        U[
        5      (       a  [        R                  " XR                  S9nO[        R                  " XR                  S9n[        R                  " X/5      $ )zConvert the input parameters into Tensors if they were not and broadcast them

Returns:
    list[Tensor]: converted total_count and probs.
r   )
isinstancefloatr   	to_tensorr   intcastbroadcast_tensors)r   r   r   s      r   r   Binomial._to_tensorZ   sy     eU##$$U**=EDJk3'' **;jjIK ++kDK ''(<==r   c                4    U R                   U R                  -  $ )zAMean of binomial distribution.

Returns:
    Tensor: mean value.
r   r   r   s    r   meanBinomial.meano   s     $**,,r   c                T    U R                   U R                  -  SU R                  -
  -  $ )zIVariance of binomial distribution.

Returns:
    Tensor: variance value.
   r&   r'   s    r   varianceBinomial.variancex   s&     $**,DJJ??r   c                   [        U[        5      (       d  [        S5      e[        R                  " S5         [        U5      n[        U R                  5      n[        X-   5      n[        R                  " U R                  US9n[        R                  " U R                  US9n[        R                  " [        R                  " USS9U5      n[        R                  " X`R                  5      sSSS5        $ ! , (       d  f       g= f)a6  Generate binomial samples of the specified shape. The final shape would be ``shape+batch_shape`` .

Args:
    shape (Sequence[int], optional): Prepended shape of the generated samples.

Returns:
    Tensor: Sampled data with shape `sample_shape` + `batch_shape`. The returned data type is the same as `probs`.
z%sample shape must be Sequence object.F)r   int32r   N)r   r   	TypeErrorr   set_grad_enabledtupler   broadcast_tor   r   binomialr"   r   )r   r   r   output_shapeoutput_sizeoutput_probsamples          r   r8   Binomial.sample   s     %**CDD$$U+%LE 0 01K !45L --  K !--djjMK__Kw7F ;;vzz2 ,++s   B5C66
Dc                    U R                  5       nU R                  U5      n[        R                  " U5      U-  R	                  S5      * $ )a%  Shannon entropy in nats.

The entropy is

.. math::

    \mathcal{H}(X) = - \sum_{x \in \Omega} p(x) \log{p(x)}

In the above equation:

* :math:`\Omega`: is the support of the distribution.

Returns:
    Tensor: Shannon entropy of binomial distribution. The data type is the same as `probs`.
r   )_enumerate_supportlog_probr   expsum)r   valuesr<   s      r   entropyBinomial.entropy   sB      ((*==(H%055a888r   c                    [         R                  " S[         R                  " U R                  5      -   U R                  S9nUR                  SS[        U R                  5      -  -   5      nU$ )zvReturn the support of binomial distribution [0, 1, ... ,n]

Returns:
    Tensor: the support of binomial distribution
r+   r   ))r+   )r   arangemaxr   r   reshapelenr   )r   r?   s     r   r;   Binomial._enumerate_support   sY     

4++,,DJJ
 s43C3C/D(D DEr   c                h   [         R                  " XR                  S9n[         R                  " U R                  S-   5      [         R                  " U R                  U-
  S-   5      -
  [         R                  " US-   5      -
  n[         R
                  " U R                  R                  5      R                  n[         R                  " U R                  USU-
  S9n[         R                  " UU[         R                  " U5      -  -   U R                  U-
  [         R                  " SU-
  5      -  -   U* S9$ )zLog probability density/mass function.

Args:
    value (Tensor): The input tensor.

Returns:
    Tensor: log probability. The data type is the same as `probs`.
r   g      ?r+   )minrE   )neginf)r   r"   r   lgammar   finfor   epsclip
nan_to_numlog)r   valuelog_combrN   r   s        r   r<   Binomial.log_prob   s     E4 MM$**S01mmD,,u4s:;<mmECK() 	
 ll4::++,00DJJCQW=  &**U++,##e+vzz!e)/DDE 4
 	
r   c                L    [         R                  " U R                  U5      5      $ )zProbability density/mass function.

Args:
    value (Tensor): The input tensor.

Returns:
    Tensor: probability. The data type is the same as `probs`.
)r   r=   r<   )r   rR   s     r   probBinomial.prob   s     zz$--.//r   c                   U R                  5       nU R                  U5      nUR                  U5      n[        R                  " [        R                  " U5      [        R
                  " X45      5      R                  S5      $ )a  The KL-divergence between two binomial distributions with the same :attr:`total_count`.

The probability density function (pdf) is

.. math::

    KL\_divergence(n_1, p_1, n_2, p_2) = \sum_x p_1(x) \log{\frac{p_1(x)}{p_2(x)}}

.. math::

    p_1(x) = \frac{n_1!}{x!(n_1-x)!}p_1^{x}(1-p_1)^{n_1-x}

.. math::

    p_2(x) = \frac{n_2!}{x!(n_2-x)!}p_2^{x}(1-p_2)^{n_2-x}

Args:
    other (Binomial): instance of ``Binomial``.

Returns:
    Tensor: kl-divergence between two binomial distributions. The data type is the same as `probs`.

r   )r;   r<   r   multiplyr=   subtractr>   )r   othersupport
log_prob_1
log_prob_2s        r   kl_divergenceBinomial.kl_divergence   sb    0 ))+]]7+
^^G,
OO

:&8 #a&	r   )r   r   r   )r   int | Tensorr   float | TensorreturnNone)r   ra   r   rb   rc   zlist[Tensor])rc   r   )r   zSequence[int]rc   r   )rR   r   rc   r   )r[   r
   rc   r   )__name__
__module____qualname____firstlineno____doc____annotations__r   r   propertyr(   r,   r8   r@   r;   r<   rV   r_   __static_attributes____classcell__)r   s   @r   r
   r
      s    .` M&'&0>&	&>'>0>>	>* - - @ @ -/ 329(

:	0   r   r
   )
__future__r   collections.abcr   typingr   r   paddle.distributionr   r   paddle._typing.dtype_liker   Distributionr
    r   r   <module>ru      s3    # $    ,7f|(( fr   