
    Αi3                        S SK Jr  S SKrS SKJr  S SK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)
    )annotationsN)TYPE_CHECKING)	framework)distribution)Sequence)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5       r
    SS
 jrSS jrSS jrSS jrSS jr/ 4SS jjr/ 4SS jjrSS jrSS jrSrU =r$ )Laplace   a@  
Creates a Laplace distribution parameterized by :attr:`loc` and :attr:`scale`.

Mathematical details

The probability density function (pdf) is

.. math::
    pdf(x; \mu, \sigma) = \frac{1}{2 * \sigma} * e^{\frac{-|x - \mu|}{\sigma}}

In the above equation:

* :math:`loc = \mu`: is the location parameter.
* :math:`scale = \sigma`: is the scale parameter.

Args:
    loc (scalar|Tensor): The mean of the distribution.
    scale (scalar|Tensor): The scale of the distribution.

Examples:
    .. code-block:: python

        >>> import paddle
        >>> paddle.seed(2023)
        >>> m = paddle.distribution.Laplace(paddle.to_tensor(0.0), paddle.to_tensor(1.0))
        >>> m.sample()  # Laplace distributed with loc=0, scale=1
        Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True,
            1.31554604)

r   locscalec                  > [        U[        R                  [        R                  [
        R                  R                  45      (       d  [        S[        U5       35      e[        U[        R                  [        R                  [
        R                  R                  45      (       d  [        S[        U5       35      e[        U[        R                  5      (       a  [
        R                  " SUS9n[        U[        R                  5      (       a  [
        R                  " SUS9n[        UR                  5      S:  d  [        UR                  5      S:  a?  UR                  UR                  :X  a%  [
        R                  " X/5      u  U l        U l        OXsU l        U l        ["        TU ]I  U R                  R                  5        g )Nz/Expected type of loc is Real|Variable, but got z1Expected type of scale is Real|Variable, but got  shape
fill_valuer   )
isinstancenumbersRealr   VariablepaddlepirValue	TypeErrortypefulllenr   dtypebroadcast_tensorsr   r   super__init__)selfr   r   	__class__s      [/var/www/html/banglarbhumi/venv/lib/python3.13/site-packages/paddle/distribution/laplace.pyr!   Laplace.__init__B   sK   ',,	 2 2FJJ4D4DE
 
 A$s)M  GLL)"4"4fjj6F6FG
 
 CDK=Q  c7<<((++B37CeW\\**KKbU;Eq C		NQ$6II$#)#;#;SL#I DHdj#& DHdj(    c                    U R                   $ )z<Mean of distribution.

Returns:
    Tensor: The mean value.
)r   r"   s    r$   meanLaplace.mean`   s     xxr&   c                     SU R                   -  $ )zStandard deviation.

The stddev is

.. math::
    stddev = \sqrt{2} * \sigma

In the above equation:

* :math:`scale = \sigma`: is the scale parameter.

Returns:
    Tensor: The std value.
g;f?)r   r(   s    r$   stddevLaplace.stddevi   s      $**$$r&   c                8    U R                   R                  S5      $ )zVariance of distribution.

The variance is

.. math::
    variance = 2 * \sigma^2

In the above equation:

* :math:`scale = \sigma`: is the scale parameter.

Returns:
    Tensor: The variance value.
   )r,   powr(   s    r$   varianceLaplace.variance{   s      {{q!!r&   c                \   [        U[        R                  5      (       a  [        R                  " SUS9nUR
                  U R                  R
                  :w  a*  [        R                  " XR                  R
                  5      n[        U R                  R                  5      S:  d<  [        U R                  R                  5      S:  d  [        UR                  5      S:  a1  [        R                  " U R                  U R                  U/5      u  p#nOU R                  U R                  p2X#U4$ )a  Argument dimension check for distribution methods such as `log_prob`,
`cdf` and `icdf`.

Args:
  value (Tensor|Scalar): The input value, which can be a scalar or a tensor.

Returns:
  loc, scale, value: The broadcasted loc, scale and value, with the same dimension and data type.
r   r   r   )r   r   r   r   r   r   r   castr   r   r   r   )r"   valuer   r   s       r$   _validate_valueLaplace._validate_value   s     eW\\**KKbU;E;;$*****KKzz'7'78E

  !A%488>>"Q&5;;!# & 8 84::u-!C 4::5  r&   c                    U R                  U5      u  p#n[        R                  " SU-  5      * nU[        R                  " X-
  5      U-  -
  $ )a  Log probability density/mass function.

The log_prob is

.. math::
    log\_prob(value) = \frac{-log(2 * \sigma) - |value - \mu|}{\sigma}

In the above equation:

* :math:`loc = \mu`: is the location parameter.
* :math:`scale = \sigma`: is the scale parameter.

Args:
  value (Tensor|Scalar): The input value, can be a scalar or a tensor.

Returns:
  Tensor: The log probability, whose data type is same with value.

Examples:
    .. code-block:: python

        >>> import paddle

        >>> m = paddle.distribution.Laplace(paddle.to_tensor(0.0), paddle.to_tensor(1.0))
        >>> value = paddle.to_tensor(0.1)
        >>> m.log_prob(value)
        Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True,
                -0.79314721)

r/   )r6   r   logabs)r"   r5   r   r   	log_scales        r$   log_probLaplace.log_prob   sI    > !007EZZE	**	6::ek2U:::r&   c                N    S[         R                  " SU R                  -  5      -   $ )a  Entropy of Laplace distribution.

The entropy is:

.. math::
    entropy() = 1 + log(2 * \sigma)

In the above equation:

* :math:`scale = \sigma`: is the scale parameter.

Returns:
    The entropy of distribution.

Examples:
    .. code-block:: python

        >>> import paddle

        >>> m = paddle.distribution.Laplace(paddle.to_tensor(0.0), paddle.to_tensor(1.0))
        >>> m.entropy()
        Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True,
                1.69314718)
   r/   )r   r9   r   r(   s    r$   entropyLaplace.entropy   s     2 6::a$**n---r&   c                    U R                  U5      u  p#nSX-
  R                  5       -  [        R                  " X-
  R	                  5       * U-  5      -  nSU-
  $ )a  Cumulative distribution function.

The cdf is

.. math::
    cdf(value) = 0.5 - 0.5 * sign(value - \mu) * e^\frac{-|(\mu - \sigma)|}{\sigma}

In the above equation:

* :math:`loc = \mu`: is the location parameter.
* :math:`scale = \sigma`: is the scale parameter.

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

Returns:
    Tensor: The cumulative probability of value.

Examples:
    .. code-block:: python

        >>> import paddle

        >>> m = paddle.distribution.Laplace(paddle.to_tensor(0.0), paddle.to_tensor(1.0))
        >>> value = paddle.to_tensor(0.1)
        >>> m.cdf(value)
        Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True,
                0.54758132)
      ?)r6   signr   expm1r:   )r"   r5   r   r   items        r$   cdfLaplace.cdf   sd    < !007E{  "#llU[--//%789 	 Tzr&   c                    U R                  U5      u  p#nUS-
  nX#UR                  5       -  [        R                  " SUR	                  5       -  5      -  -
  $ )a  Inverse Cumulative distribution function.

The icdf is

.. math::
    cdf^{-1}(value)= \mu - \sigma * sign(value - 0.5) * ln(1 - 2 * |value-0.5|)

In the above equation:

* :math:`loc = \mu`: is the location parameter.
* :math:`scale = \sigma`: is the scale parameter.

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

Returns:
    Tensor: The cumulative probability of value.

Examples:
    .. code-block:: python

        >>> import paddle
        >>> m = paddle.distribution.Laplace(paddle.to_tensor(0.0), paddle.to_tensor(1.0))
        >>> value = paddle.to_tensor(0.1)
        >>> m.icdf(value)
        Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True,
                -1.60943794)
rC   )r6   rD   r   log1pr:   )r"   r5   r   r   terms        r$   icdfLaplace.icdf  sN    : !007Es{d[[]*V\\"txxz/-JJJJr&   c                    [        U[        5      (       a  UO
[        U5      n[        R                  " 5          U R	                  U5      sSSS5        $ ! , (       d  f       g= f)a7  Generate samples of the specified shape.

Args:
    shape(Sequence[int], optional): The shape of generated samples.
        Defaults to [].

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

Examples:
    .. code-block:: python

        >>> import paddle
        >>> paddle.seed(2023)
        >>> m = paddle.distribution.Laplace(paddle.to_tensor(0.0), paddle.to_tensor(1.0))
        >>> m.sample()  # Laplace distributed with loc=0, scale=1
        Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True,
            1.31554604)
N)r   tupler   no_gradrsample)r"   r   s     r$   sampleLaplace.sample2  s=    ( $E511uU|^^<<& s   A
A!c           	        U R                  5       nU R                  U5      n[        R                  " U[	        [
        R                  " SS5      5      US-  -   SUS-  -
  U R                  R                  S9nU R                  U R                  UR                  5       -  [        R                  " UR                  5       * 5      -  -
  $ )a7  Reparameterized sample.

Args:
    shape(Sequence[int], optional): The shape of generated samples.
        Defaults to [].

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

Examples:
    .. code-block:: python

        >>> import paddle
        >>> paddle.seed(2023)
        >>> m = paddle.distribution.Laplace(paddle.to_tensor([0.0]), paddle.to_tensor([1.0]))
        >>> m.rsample((1,))  # Laplace distributed with loc=0, scale=1
        Tensor(shape=[1, 1], dtype=float32, place=Place(cpu), stop_gradient=True,
            [[1.31554604]])
r?   r/   g      ?)r   minmaxr   )_get_eps_extend_shaper   uniformfloatnp	nextafterr   r   r   rD   rK   r:   )r"   r   epsr[   s       r$   rR   Laplace.rsampleJ  s    * mmo""5)..bll2q)*S1W4cAg((..	
 xx$**w||~5[[]N9
 
 
 	
r&   c                    SnU R                   R                  [        R                  :X  d(  U R                   R                  [        R                  :X  a  SnU$ )z
Get the eps of certain data type.

Note:
    Since paddle.finfo is temporarily unavailable, we
    use hard-coding style to get eps value.

Returns:
    Float: An eps value by different data types.
gy>gǝ <)r   r   r   float64
complex128)r"   r_   s     r$   rY   Laplace._get_epsk  s=     HHNNfnn,xx~~!2!22C
r&   c                J   UR                   U R                   -  n[        R                  " U R                  UR                  -
  5      nU R                   [        R                  " U* U R                   -  5      -  U-   UR                   -  n[        R
                  " U5      nXE-   S-
  $ )a  Calculate the KL divergence KL(self || other) with two Laplace instances.

The kl_divergence between two Laplace distribution is

.. math::
    KL\_divergence(\mu_0, \sigma_0; \mu_1, \sigma_1) = 0.5 (ratio^2 + (\frac{diff}{\sigma_1})^2 - 1 - 2 \ln {ratio})

.. math::
    ratio = \frac{\sigma_0}{\sigma_1}

.. math::
    diff = \mu_1 - \mu_0

In the above equation:

* :math:`loc = \mu`: is the location parameter of self.
* :math:`scale = \sigma`: is the scale parameter of self.
* :math:`loc = \mu_1`: is the location parameter of the reference Laplace distribution.
* :math:`scale = \sigma_1`: is the scale parameter of the reference Laplace distribution.
* :math:`ratio`: is the ratio between the two distribution.
* :math:`diff`: is the difference between the two distribution.

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

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

Examples:
    .. code-block:: python

        >>> import paddle

        >>> m1 = paddle.distribution.Laplace(paddle.to_tensor([0.0]), paddle.to_tensor([1.0]))
        >>> m2 = paddle.distribution.Laplace(paddle.to_tensor([1.0]), paddle.to_tensor([0.5]))
        >>> m1.kl_divergence(m2)
        Tensor(shape=[1], dtype=float32, place=Place(cpu), stop_gradient=True,
                [1.04261160])
r?   )r   r   r:   r   expr9   )r"   other	var_ratiotterm1term2s         r$   kl_divergenceLaplace.kl_divergence  s|    R KK$**,	JJtxx%))+,fjj!djj99A=L

9%}q  r&   )r   r   )r   float | Tensorr   rn   returnNone)ro   r   )r5   rn   ro   ztuple[Tensor, Tensor, Tensor])r5   rn   ro   r   )r   zSequence[int]ro   r   )ro   r\   )rg   r
   ro   r   )__name__
__module____qualname____firstlineno____doc____annotations__r!   propertyr)   r,   r1   r6   r<   r@   rG   rM   rS   rR   rY   rl   __static_attributes____classcell__)r#   s   @r$   r
   r
      s    > 
KM)<   % %" " ""!#!	&!:";H.6%N KD -/ '0 .0 
B(.! .!r&   r
   )
__future__r   r   typingr   numpyr]   r   paddle.baser   paddle.distributionr   collections.abcr   r   Distributionr
   r   r&   r$   <module>r      s9    #      ! ,(N!l'' N!r&   