
    zj|*                        d dl mZ d dlZd dlmZ d dlZd dl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S )
    )annotationsN)TYPE_CHECKING)	framework)distribution)Sequence)Tensorc                       e Zd ZU dZded<   d fdZedd            Zedd	            Zedd
            Z	ddZ
ddZg fddZg fddZddZddZddZ xZS )	GeometricaM  
    Geometric distribution parameterized by probs.

    In probability theory and statistics, the geometric distribution is one of
    discrete probability distributions, parameterized by one positive shape parameter, denoted by probs.
    In n Bernoulli trials, it takes k+1 trials to get the probability of success for the first time.
    In detail, it is: the probability that the first k times failed and the kth time succeeded.
    The geometric distribution is a special case of the Pascal distribution when r=1.

    The probability mass function (pmf) is

    .. math::
            Pr(Y=k)=(1-p)^kp

    where k is number of trials failed before seeing a success, and p is probability of success for each trial and k=0,1,2,3,4..., p belong to (0,1].

    Args:
        probs (Real|Tensor): Probability parameter.
            The value of probs must be positive. When the parameter is a tensor, probs is probability of success for each trial.

    Returns:
        Geometric distribution for instantiation of probs.

    Examples:

        .. code-block:: python

            >>> import paddle
            >>> from paddle.distribution import Geometric

            >>> geom = Geometric(0.5)

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

            >>> print(geom.variance)
            Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True,
            2.)

            >>> print(geom.stddev)
            Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True,
            1.41421354)
    r   probsfloat | TensorreturnNonec                   t          |t          j        t          j        t
          j        t          j        j        f          rt          |t          j                  r!t          j	        d|t          j
                  }t          j	        |j        d|j                  }t          j	        |j        d|j                  }t          j	        |j        dt                    }||k    }||k    }nt          dt          |                     t!          |j                  }|| _        t%                                          |           d S )N )shape
fill_valuedtype   r   FzOExpected type of probs is Number.Real|Tensor|framework.Variable|Value, but got )
isinstancenumbersRealpaddler   r   VariablepirValuefullfloat32r   r   bool	TypeErrortypetupler   super__init__)	selfr   all_ones	all_zeros	all_false
lessthen_0
morethen_1batch_shape	__class__s	           m/lsinfo/ai/hellotax_ai/data_center/backend/venv/lib/python3.11/site-packages/paddle/distribution/geometric.pyr#   zGeometric.__init__P   sC   \6=)*<fj>NO
 
 	 %.. fn   {kau{  H kau{  I ke4  I )+J)JJ obfglbmbmoo   EK((
%%%%%    c                    d| j         z  dz
  S )zMean of geometric distribution.      ?)r   r$   s    r,   meanzGeometric.meanq   s     TZ#%%r-   c                h    t          j        d| j        z  dz
  | j        z  | j        j                  S )z#Variance of geometric distribution.r/   r   )r   	to_tensorr   r   r0   s    r,   variancezGeometric.variancev   s<     4:#tz1*"
 
 
 	
r-   c                4    t          j        | j                  S )z-Standard deviation of Geometric distribution.)r   sqrtr5   r0   s    r,   stddevzGeometric.stddev~   s     {4=)))r-   kint | Tensorc                    t          |t          j        t          j        t
          j        j        f          r%t          j        d| j	        z
  |          | j	        z  S t          dt          |                     )aI  Probability mass function evaluated at k.

        .. math::

            P(X=k) = (1-p)^{k} p, \quad k=0,1,2,3,\ldots

        Args:
            k (int): Value to be evaluated.

        Returns:
            Tensor: Probability.

        Examples:

            .. code-block:: python

                >>> import paddle
                >>> from paddle.distribution import Geometric

                >>> geom = Geometric(0.5)
                >>> print(geom.pmf(2))
                Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True,
                0.12500000)
        r/   DExpected type of k is number.Real|framework.Variable|Value, but got r   r   Integralr   r   r   r   r   powr   r   r    r$   r9   s     r,   pmfzGeometric.pmf   sv    2  )"4fj6FG
 
 	 :sTZ/!44tzAA`W[\]W^W^``  r-   c                    t          |t          j        t          j        t
          j        j        f          r't          j        | 	                    |                    S t          dt          |                     )aF  Log probability mass function evaluated at k.

        .. math::
            \log P(X = k) = \log(1-p)^k p

        Args:
            k (int): Value to be evaluated.

        Returns:
            Tensor: Log probability.

        Examples:

            .. code-block:: python

                >>> import paddle
                >>> from paddle.distribution import Geometric

                >>> geom = Geometric(0.5)
                >>> print(geom.log_pmf(2))
                Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True,
                -2.07944131)
        r<   )r   r   r>   r   r   r   r   r   logrA   r   r    r@   s     r,   log_pmfzGeometric.log_pmf   sp    0  )"4fj6FG
 
 	 :dhhqkk***`W[\]W^W^``  r-   r   Sequence[int]c                    t          j                    5  |                     |          cddd           S # 1 swxY w Y   dS )a  Sample from Geometric distribution with sample shape.

        Args:
            shape (Sequence[int]): Sample shape.

        Returns:
            Sampled data with shape `sample_shape` + `batch_shape` + `event_shape`.

        Examples:

            .. code-block:: python

                >>> import paddle
                >>> from paddle.distribution import Geometric

                >>> paddle.seed(2023)
                >>> geom = Geometric(0.5)
                >>> print(geom.sample((2,2)))
                Tensor(shape=[2, 2], dtype=float32, place=Place(cpu), stop_gradient=True,
                [[0., 0.],
                 [1., 0.]])
        N)r   no_gradrsample)r$   r   s     r,   samplezGeometric.sample   s    . ^ 	' 	'<<&&	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	's   6::c                T   t           j                            | |          }t          j        |t          t          j        d          j                  d| j	        j
                  }t          j        t          j        |          t          j        | j	                   z            S )a  Generate samples of the specified shape.

        Args:
            shape(Sequence[int]): The shape of generated samples.

        Returns:
            Tensor: A sample tensor that fits the Geometric distribution.

        Examples:

            .. code-block:: python

                >>> import paddle
                >>> from paddle.distribution import Geometric

                >>> paddle.seed(2023)
                >>> geom = Geometric(0.5)
                >>> print(geom.rsample((2,2)))
                Tensor(shape=[2, 2], dtype=float32, place=Place(cpu), stop_gradient=True,
                [[0., 0.],
                 [1., 0.]])

        )sample_shaper   r3   r/   )r   minmaxr   )r   Distribution_extend_shaper   uniformfloatnpfinfotinyr   r   floorrC   log1p)r$   r   rP   s      r,   rH   zGeometric.rsample   s    0 )77u 8 
 
 .bhY///455*"	
 
 
 |FJw//&,}2M2MMNNNr-   c                    d| j         z
  t          j        d| j         z
            z  }| j         t          j        | j                   z  }||z    | j         z  S )a  Entropy of dirichlet distribution.

        .. math::

            H(X) = -\left[\frac{1}{p} \log p + \frac{1-p}{p^2} \log (1-p) \right]

        Returns:
            Tensor: Entropy.

        Examples:

            .. code-block:: python

                >>> import paddle
                >>> from paddle.distribution import Geometric

                >>> geom = Geometric(0.5)
                >>> print(geom.entropy())
                Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True,
                1.38629425)
        r/   )r   r   rC   )r$   xys      r,   entropyzGeometric.entropy  sQ    , 4:C$*,<!=!==JDJ///Qx$*$$r-   c                    t          |t          j        t          j        t
          j        j        f          r#dt          j        d| j	        z
  |dz             z
  S t          dt          |                     )aD  Cdf of geometric distribution.

        .. math::

            F(X \leq k) = 1 - (1-p)^(k+1), \quad k=0,1,2,\ldots

        Args:
            k: The number of trials performed.

        Returns:
            Tensor: Entropy.

        Examples:

            .. code-block:: python

                >>> import paddle
                >>> from paddle.distribution import Geometric

                >>> geom = Geometric(0.5)
                >>> print(geom.cdf(4))
                Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True,
                0.96875000)
        r/   r   r<   r=   r@   s     r,   cdfzGeometric.cdf   sx    2  )"4fj6FG
 
 	 S4:%5A>>>>`W[\]W^W^``  r-   otherc                   t          |t                    rL| j        |j        }}|t          j        ||z            z  d|z
  t          j        d|z
  d|z
  z            z  z   S t          dt          |                     )a  Calculate the KL divergence KL(self || other) with two Geometric instances.

        .. math::

            KL(P \| Q) = \frac{p}{q} \log \frac{p}{q} + \log (1-p) - \log (1-q)

        Args:
            other (Geometric): An instance of Geometric.

        Returns:
            Tensor: The kl-divergence between two geometric distributions.

        Examples:

            .. code-block:: python

                >>> import paddle
                >>> from paddle.distribution import Geometric

                >>> geom_p = Geometric(0.5)
                >>> geom_q = Geometric(0.1)
                >>> print(geom_p.kl_divergence(geom_q))
                Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True,
                0.51082563)
        r/   z6Exacted type of other is geometric.Geometric, but got )r   r
   r   r   rC   r   r    )r$   r]   pqs       r,   kl_divergencezGeometric.kl_divergenceB  s    4 eY'' 	:u{qAvz!a%(((C!GvzqS1W%8 8 ,   VeVV  r-   )r   r   r   r   )r   r   )r9   r:   r   r   )r   rE   r   r   )r]   r
   r   r   )__name__
__module____qualname____doc____annotations__r#   propertyr1   r5   r8   rA   rD   rI   rH   rZ   r\   ra   __classcell__)r+   s   @r,   r
   r
       s_        + +Z MMM& & & & & &B & & & X& 
 
 
 X
 * * * X*       D   B -/ ' ' ' ' '4 .0 #O #O #O #O #OJ% % % %6       D" " " " " " " "r-   r
   )
__future__r   r   typingr   numpyrR   r   paddle.baser   paddle.distributionr   collections.abcr   r   rN   r
   r   r-   r,   <module>ro      s    # " " " " "                   ! ! ! ! ! ! , , , , , , ((((((D D D D D) D D D D Dr-   