
    Α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  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)convert_dtype)distribution)Tensor)_DTypeLiteralc                     ^  \ rS rSr% Sr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$ )Poisson   a!  
The Poisson distribution with occurrence rate parameter: `rate`.

In probability theory and statistics, the Poisson distribution is the most basic discrete probability
distribution defined on the nonnegative integer set, which is used to describe the probability distribution of the number of random
events occurring per unit time.

The probability mass function (pmf) is

.. math::

    pmf(x; \lambda) = \frac{e^{-\lambda} \cdot \lambda^x}{x!}

In the above equation:

* :math:`rate = \lambda`: is the mean occurrence rate.

Args:
    rate(int|float|Tensor): The mean occurrence rate of Poisson distribution which should be greater than 0, meaning the expected occurrence
        times of an event in a fixed time interval. If the input data type is int or float, the data type of `rate` will be converted to a
        1-D Tensor with paddle global default dtype.

Examples:
    .. code-block:: python

        >>> import paddle
        >>> from paddle.distribution import Poisson
        >>> paddle.set_device('cpu')
        >>> paddle.seed(100)
        >>> rv = Poisson(paddle.to_tensor(30.0))

        >>> print(rv.sample([3]))
        Tensor(shape=[3], dtype=float32, place=Place(cpu), stop_gradient=True,
        [32., 27., 25.])

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

        >>> print(rv.entropy())
        Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True,
        3.11671519)

        >>> rv1 = Poisson(paddle.to_tensor([[30.,40.],[8.,5.]]))
        >>> rv2 = Poisson(paddle.to_tensor([[1000.,40.],[7.,10.]]))
        >>> print(rv1.kl_divergence(rv2))
        Tensor(shape=[2, 2], dtype=float32, place=Place(cpu), stop_gradient=True,
        [[864.80499268, 0.          ],
         [0.06825146  , 1.53426409  ]])
r   rater	   dtypec                   > [         R                  " 5       U l        U R                  U5      U l        U R                  R
                  n[        TU ]  U5        g N)paddleget_default_dtyper   
_to_tensorr   shapesuper__init__)selfr   batch_shape	__class__s      [/var/www/html/banglarbhumi/venv/lib/python3.13/site-packages/paddle/distribution/poisson.pyr   Poisson.__init__S   s=    --/
OOD)	iioo%    c                    [        U[        [        45      (       a"  [        R                  " U/U R
                  S9nU$ [        UR
                  5      U l        U$ )zQConvert the input parameters into tensors.

Returns:
    Tensor: converted rate.
r   )
isinstancefloatintr   	to_tensorr   r   )r   r   s     r   r   Poisson._to_tensorZ   sK     dUCL))##TF$**=D  'tzz2DJr   c                    U R                   $ )z@Mean of poisson distribution.

Returns:
    Tensor: mean value.
r   r   s    r   meanPoisson.meang        yyr   c                    U R                   $ )zHVariance of poisson distribution.

Returns:
    Tensor: variance value.
r%   r&   s    r   variancePoisson.variancep   r)   r   c                f   [        U[        5      (       d  [        S5      e[        U5      n[        U R                  5      n[        X-   5      n[
        R                  " U R                  US9n[
        R                  " 5          [
        R                  " U5      sSSS5        $ ! , (       d  f       g= f)a  Generate poisson 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`.
z%sample shape must be Sequence object.)r   N)
r   r   	TypeErrortupler   r   broadcast_tor   no_gradpoisson)r   r   r   output_shapeoutput_rates        r   samplePoisson.sampley   s{     %**CDDeD,,-U01))$))<H^^>>+. s   B""
B0c           
        U R                  U R                  5      R                  SS[        U R                  5      -  -   5      nU R                  U5      n[        R                  " U5      U-  R                  S5      * n[        R                  " [        R                  " U R                  [        R                  " SU R                  S95      U R                  S9n[        R                  " X45      $ )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 poisson distribution. The data type is the same as `rate`.
   r   g        r   )_enumerate_bounded_supportr   reshapelenr   log_probr   expsumcast	not_equalr"   r   multiply)r   valuesr?   proposedmasks        r   entropyPoisson.entropy   s      00;CCD3t//000
 ==(ZZ)H499!<<{{		6++CtzzB **	
 x..r   c           
       ^ ^ [         R                  R                  5       (       a  [         R                  " [         R                  " T5      [         R
                  " ST R                  S95      (       a*  [         R                  " [         R                  " T5      5      O[         R                  " TT R                  S9n[         R                  " [         R                  " TSU-  -   SS95      n[         R                  " SUT R                  S9nU$ U4S jnU 4S jn[         R                  R                  R                  [         R                  " [         R                  " T5      [         R
                  " ST R                  S95      UU5      n[         R                  " [         R                  " TSU-  -   SS95      n[         R                  " SUT R                  S9nU$ )aB  Generate a bounded approximation of the support. Approximately view Poisson r.v. as a
Normal r.v. with mu = rate and sigma = sqrt(rate). Then by 30-sigma rule, generate a bounded
approximation of the support.

Args:
    rate (float): rate of one poisson r.v.

Returns:
    Tensor: the bounded approximation of the support
      ?r      int32r   c                 X   > [         R                  " [         R                  " T 5      5      $ r   )r   sqrtmaxr%   s   r   	true_func5Poisson._enumerate_bounded_support.<locals>.true_func   s    {{6::d#344r   c                 B   > [         R                  " ST R                  S9$ )NrK   r   )r   r"   r   r&   s   r   
false_func6Poisson._enumerate_bounded_support.<locals>.false_func   s    ''4::>>r   )r   	frameworkin_dynamic_modegreater_equalrP   r"   r   rO   	ones_likerB   arangestaticnncond)r   r   s_maxupperrE   rQ   rT   s   ``     r   r<   "Poisson._enumerate_bounded_support   sY    ++-- ''JJt$f&6&6s$**&M  FJJt,- %%d$**=  JJv{{4"u*+<GLME]]1e4::>FM5? MM$$))$$JJt$f&6&6s$**&M E JJv{{4"u*+<GLME]]1e4::>FMr   c                d   [         R                  " XR                  S9n[         R                  " U R                  R                  5      R
                  n[         R                  " U R                  * U[         R                  " U R                  5      -  -   [         R                  " US-   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 `rate`.
r   r;   )neginf)	r   rB   r   finfor   eps
nan_to_numloglgamma)r   valuerd   s      r   r?   Poisson.log_prob   s     E4ll499??+//  
&**TYY//0--	*+ 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 `rate`.
)r   r@   r?   )r   rh   s     r   probPoisson.prob   s     zz$--.//r   c                >   U R                   UR                   :w  a  [        S5      e[        R                  " [        R                  " U R
                  UR
                  5      5      nU R                  U5      n[        R                  " U5      n[        R                  " SX@R                  S9R                  SS[        U R                   5      -  -   5      nU R                  U5      nUR                  U5      n[        R                  " U5      Xg-
  -  R                  S5      $ )a  The KL-divergence between two poisson distributions with the same `batch_shape`.

The probability density function (pdf) is

.. math::

    KL\_divergence\lambda_1, \lambda_2) = \sum_x p_1(x) \log{\frac{p_1(x)}{p_2(x)}}

.. math::

    p_1(x) = \frac{e^{-\lambda_1} \cdot \lambda_1^x}{x!}

.. math::

    p_2(x) = \frac{e^{-\lambda_2} \cdot \lambda_2^x}{x!}

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

Returns:
    Tensor, kl-divergence between two poisson distributions. The data type is the same as `rate`.

zOKL divergence of two poisson distributions should share the same `batch_shape`.r   r   r8   r:   )r   
ValueErrorr   rP   maximumr   r<   rZ   r   r=   r>   r?   r@   rA   )r   otherrate_maxsupport_maxa_maxcommon_support
log_prob_1
log_prob_2s           r   kl_divergencePoisson.kl_divergence   s    2 u000a  ::fnnTYY

CD55h?

;'q%zzBJJD3t//000
 ]]>2
^^N3


:&**ABGGJJr   )r   r   )r   float | TensorreturnNone)r   ry   rz   r   )rz   r   )r   zSequence[int]rz   r   )rh   r   rz   r   )rp   r   rz   r   )__name__
__module____qualname____firstlineno____doc____annotations__r   r   propertyr'   r+   r5   rH   r<   r?   rk   rw   __static_attributes____classcell__)r   s   @r   r   r      ss    1f L&     -/ /(/:'R
(	0&K &Kr   r   )
__future__r   collections.abcr   typingr   r   paddle.base.data_feederr   paddle.distributionr   r   paddle._typing.dtype_liker	   Distributionr    r   r   <module>r      s8    # $    1 ,7|Kl'' |Kr   