Ë
    ˆ\;jÔ*  ã                   ó^   — d dl Z d dlZd dlZd dlmZ d dlmZ  G d„ dej                  «      Z	y)é    N)Ú	framework)Údistributionc                   ó€   ‡ — e Zd ZdZˆ fd„Zed„ «       Zed„ «       Zed„ «       Zd„ Z	d„ Z
dd„Zdd	„Zd
„ Zd„ Z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)
    c                 óþ  •— t        |t        j                  t        j                  t
        j                  f«      rÉt        |t        j                  «      r&t        j                  d|t        j                  ¬«      }t        j                  |j                  d|j                  ¬«      }t        j                  |j                  d|j                  ¬«      }t        j                  |j                  dt        ¬«      }||k  }||kD  }nt        dt        |«      › �«      ‚t        j                  ||«      r,t        j                  ||«      rt        |j                  «      }nt!        d«      ‚|| _        t$        ‰| �M  |«       y )N© )ÚshapeÚ
fill_valueÚdtypeé   r   FzIExpected type of probs is Number.Real|Tensor|framework.Variable, but got zvExpected parameter probs of distribution Geometric to satisfy theconstraint Interval(lower_bound=0.0, upper_bound=1.0))Ú
isinstanceÚnumbersÚRealÚpaddleÚTensorr   ÚVariableÚfullÚfloat32r	   r   ÚboolÚ	TypeErrorÚtypeÚ	equal_allÚtupleÚ
ValueErrorÚprobsÚsuperÚ__init__)	Úselfr   Úall_onesÚ	all_zerosÚ	all_falseÚ
lessthen_0Ú
morethen_1Úbatch_shapeÚ	__class__s	           €úfG:\00. PROJECTS\API\Inventory\templateJSON\kerjaOCR\Lib\site-packages\paddle/distribution/geometric.pyr   zGeometric.__init__F   s6  ø€ Ü�eœgŸl™l¬F¯M©M¼9×;MÑ;MÐNÔOÜ˜%¤§¡Ô.ÜŸ™Ø¨´f·n±nô�ô —{‘{Ø—k‘k¨a°u·{±{ôˆHô Ÿ™Ø—k‘k¨a°u·{±{ôˆIô Ÿ™Ø—k‘k¨e¼4ôˆIð  )Ñ+ˆJØ Ñ)‰Jô Ø[Ô\`ÐafÓ\gÐ[hÐióð ô ×Ñ˜J¨	Ô2´v×7GÑ7GØ˜	ô8
ô   §¡Ó,‰KäðHóð ð
 ˆŒ
Ü‰Ñ˜Õ%ó    c                 ó&   — d| j                   z  dz
  S )zMean of geometric distribution.ç      ð?)r   ©r   s    r&   ÚmeanzGeometric.meanl   s   € ð �T—Z‘ZÑ #Ñ%Ð%r'   c                 ó’   — t        j                  d| j                  z  dz
  | j                  z  | j                  j                  ¬«      S )z#Variance of geometric distribution.r)   ©r   )r   Ú	to_tensorr   r   r*   s    r&   ÚvariancezGeometric.varianceq   s>   € ô ×ÑØ�4—:‘:Ñ Ñ# t§z¡zÑ1Ø—*‘*×"Ñ"ô
ð 	
r'   c                 ó@   — t        j                  | j                  «      S )z-Standard deviation of Geometric distribution.)r   Úsqrtr/   r*   s    r&   ÚstddevzGeometric.stddevy   s   € ô �{‰{˜4Ÿ=™=Ó)Ð)r'   c                 óä   — t        |t        j                  t        j                  f«      r0t        j                  d| j                  z
  |«      | j                  z  S t        dt        |«      › �«      ‚)aI  Probability mass funciotn 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)   ú>Expected type of k is number.Real|framework.Variable, but got ©
r   r   ÚIntegralr   r   r   Úpowr   r   r   ©r   Úks     r&   ÚpmfzGeometric.pmf~   sa   € ô2 �aœ'×*Ñ*¬I×,>Ñ,>Ð?Ô@Ü—:‘:˜s T§Z¡ZÑ/°!Ó4°t·z±zÑAÐAäØPÔQUÐVWÓQXÐPYÐZóð r'   c                 óÌ   — t        |t        j                  t        j                  f«      r$t        j                  | 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)
        r4   )
r   r   r6   r   r   r   Úlogr:   r   r   r8   s     r&   Úlog_pmfzGeometric.log_pmfž   sU   € ô0 �aœ'×*Ñ*¬I×,>Ñ,>Ð?Ô@Ü—:‘:˜dŸh™h q›kÓ*Ð*äØPÔQUÐVWÓQXÐPYÐZóð r'   c                 óx   — t        j                  «       5  | j                  |«      cddd«       S # 1 sw Y   yxY w)a‰  Sample from Geometric distribution with sample shape.

        Args:
            shape (tuple(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   •0°9c                 ó~  — t         j                  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(tuple(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   r-   r)   )r	   ÚminÚmaxr   )r   ÚDistributionÚ_extend_shaper   ÚuniformÚfloatÚnpÚfinfoÚtinyr   r   Úfloorr<   Úlog1p)r   r	   rH   s      r&   r@   zGeometric.rsample×   sŒ   € ô0 ×)Ñ)×7Ñ7Ø˜uð 8ó 
ˆô —.‘.ØÜ”b—h‘h YÔ/×4Ñ4Ó5ØØ—*‘*×"Ñ"ô	
ˆô �|‰|œFŸJ™J wÓ/´&·,±,ÀÇÁ¸}Ó2MÑMÓ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   r<   )r   ÚxÚys      r&   ÚentropyzGeometric.entropyü   sY   € ð, �4—:‘:Ñ¤§¡¨C°$·*±*Ñ,<Ó!=Ñ=ˆØ�J‰JœŸ™ D§J¡JÓ/Ñ/ˆà�Q‘ˆx˜$Ÿ*™*Ñ$Ð$r'   c                 óÖ   — t        |t        j                  t        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   r4   r5   r8   s     r&   ÚcdfzGeometric.cdf  sa   € ô2 �aœ'×*Ñ*¬I×,>Ñ,>Ð?Ô@ØœŸ™ S¨4¯:©:Ñ%5¸¸A¹Ó>Ñ>Ð>äØPÔQUÐVWÓQXÐPYÐZóð r'   c                 ó   — t        |t        «      rX| 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)   z6Exected type of other is geometric.Geometric, but got )r   r   r   r   r<   r   r   )r   ÚotherÚpÚqs       r&   Úkl_divergencezGeometric.kl_divergence7  s�   € ô4 �eœYÔ'Ø—:‘:˜uŸ{™{ˆqˆAØ”v—z‘z ! a¡%Ó(Ñ(¨C°!©G´v·z±zØ�q‘˜S 1™WÑ%ó8ñ ,ñ ð ô ØHÌÈeËÈÐVóð r'   )r   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   Úpropertyr+   r/   r2   r:   r=   rA   r@   rR   rT   rY   Ú__classcell__)r%   s   @r&   r   r      sr   ø„ ñ+ôZ$&ðL ñ&ó ð&ð ñ
ó ð
ð ñ*ó ð*òò@ó>'ó4#OòJ%ò6ö@"r'   r   )
r   ÚnumpyrJ   r   Úpaddle.baser   Úpaddle.distributionr   rF   r   r   r'   r&   Ú<module>rc      s*   ðó ã ã Ý !Ý ,ôA�×)Ñ)õ Ar'   