Ë
    ˆ\;joE  ã                   ó^   — 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dˆ fd„	Zed„ «       Zed„ «       Zed„ «       Zdd„Z	dd„Z
d„ Zd	„ Zd
„ Zd„ Zd„ Zˆ xZS )ÚCauchyu"  Cauchy distribution is also called Cauchyâ€“Lorentz distribution. It is a continuous probability distribution named after Augustin-Louis Cauchy and Hendrik Lorentz. It has a very wide range of applications in natural sciences.

    The Cauchy distribution has the probability density function (PDF):

    .. math::

        { f(x; loc, scale) = \frac{1}{\pi scale \left[1 + \left(\frac{x - loc}{ scale}\right)^2\right]} = { 1 \over \pi } \left[ {  scale \over (x - loc)^2 +  scale^2 } \right], }

    Args:
        loc (float|Tensor): Location of the peak of the distribution. The data type is float32 or float64.
        scale (float|Tensor): The half-width at half-maximum (HWHM). The data type is float32 or float64. Must be positive values.
        name (str, optional): Name for the operation (optional, default is None). For more information, please refer to :ref:`api_guide_Name`.

    Examples:

        .. code-block:: python

            >>> import paddle
            >>> from paddle.distribution import Cauchy

            >>> # init Cauchy with float
            >>> rv = Cauchy(loc=0.1, scale=1.2)
            >>> print(rv.entropy())
            Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True,
                    2.71334577)

            >>> # init Cauchy with N-Dim tensor
            >>> rv = Cauchy(loc=paddle.to_tensor(0.1), scale=paddle.to_tensor([1.0, 2.0]))
            >>> print(rv.entropy())
            Tensor(shape=[2], dtype=float32, place=Place(cpu), stop_gradient=True,
                    [2.53102422, 3.22417140])
    c                 óþ  •— |�|nd| _         t        |t        j                  t        j
                  f«      st        dt        |«      › �«      ‚t        |t        j                  t        j
                  f«      st        dt        |«      › �«      ‚t        |t        j                  «      rt        j                  d|¬«      }t        |t        j                  «      rt        j                  d|¬«      }|j                  |j                  k7  r%t        j                  ||g«      \  | _        | _        n||c| _        | _        | j                  j                  | _        t        ‰| �A  | j                  j                  d¬«       y )Nr   z/Expected type of loc is Real|Variable, but got z1Expected type of scale is Real|Variable, but got © )ÚshapeÚ
fill_value)Úbatch_shapeÚevent_shape)ÚnameÚ
isinstanceÚnumbersÚRealr   ÚVariableÚ	TypeErrorÚtypeÚpaddleÚfullr	   Úbroadcast_tensorsÚlocÚscaleÚdtypeÚsuperÚ__init__)Úselfr   r   r   Ú	__class__s       €úcG:\00. PROJECTS\API\Inventory\templateJSON\kerjaOCR\Lib\site-packages\paddle/distribution/cauchy.pyr   zCauchy.__init__:   s  ø€ Ø Ð,‘D°(ˆŒ	ä˜#¤§¡¬i×.@Ñ.@ÐAÔBÜØAÄ$ÀsÃ)ÀÐMóð ô ˜%¤'§,¡,´	×0BÑ0BÐ!CÔDÜØCÄDÈÃKÀ=ÐQóð ô �cœ7Ÿ<™<Ô(Ü—+‘+ B°3Ô7ˆCä�eœWŸ\™\Ô*Ü—K‘K b°UÔ;ˆEà�9‰9˜Ÿ™Ò#Ü#)×#;Ñ#;¸SÀ%¸LÓ#IÑ ˆDŒH�d•jà#&¨Ð ˆDŒH�d”jà—X‘X—^‘^ˆŒ
ä‰Ñ T§X¡X§^¡^ÀÐÕDó    c                 ó   — t        d«      ‚)zMean of Cauchy distribution.z Cauchy distribution has no mean.©Ú
ValueError©r   s    r   ÚmeanzCauchy.meanU   s   € ô Ð;Ó<Ð<r   c                 ó   — t        d«      ‚)z Variance of Cauchy distribution.z$Cauchy distribution has no variance.r!   r#   s    r   ÚvariancezCauchy.varianceZ   s   € ô Ð?Ó@Ð@r   c                 ó   — t        d«      ‚)z*Standard Deviation of Cauchy distribution.z"Cauchy distribution has no stddev.r!   r#   s    r   ÚstddevzCauchy.stddev_   s   € ô Ð=Ó>Ð>r   c                 ó    — |�|n| j                   dz   }t        j                  «       5  | j                  ||«      cddd«       S # 1 sw Y   yxY w)a  Sample from Cauchy distribution.

        Note:
            `sample` method has no grad, if you want so, please use `rsample` instead.

        Args:
            shape (Sequence[int]): Sample shape.
            name (str, optional): Name for the operation (optional, default is None). For more information, please refer to :ref:`api_guide_Name`.

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

        Examples:

            .. code-block:: python

                >>> import paddle
                >>> from paddle.distribution import Cauchy

                >>> # init Cauchy with float
                >>> rv = Cauchy(loc=0.1, scale=1.2)
                >>> print(rv.sample([10]).shape)
                [10]

                >>> # init Cauchy with 0-Dim tensor
                >>> rv = Cauchy(loc=paddle.full((), 0.1), scale=paddle.full((), 1.2))
                >>> print(rv.sample([10]).shape)
                [10]

                >>> # init Cauchy with N-Dim tensor
                >>> rv = Cauchy(loc=paddle.to_tensor(0.1), scale=paddle.to_tensor([1.0, 2.0]))
                >>> print(rv.sample([10]).shape)
                [10, 2]

                >>> # sample 2-Dim data
                >>> rv = Cauchy(loc=0.1, scale=1.2)
                >>> print(rv.sample([10, 2]).shape)
                [10, 2]

                >>> rv = Cauchy(loc=paddle.to_tensor(0.1), scale=paddle.to_tensor([1.0, 2.0]))
                >>> print(rv.sample([10, 2]).shape)
                [10, 2, 2]
        NÚ_sample)r   r   Úno_gradÚrsample)r   r	   r   s      r   ÚsamplezCauchy.sampled   s>   € ðX Ð'‰t¨d¯i©i¸)Ñ.CˆÜ�^‰^ÕØ—<‘<  tÓ,÷ ×Òús   ¨AÁAc                 ón  — |�|n| j                   dz   }t        |t        j                  t        j
                  t        t        f«      st        dt        |«      › �«      ‚t        |t        «      r|n
t        |«      }| j                  |«      }| j                  j                  |«      }| j                  j                  |«      }t        j                  || j                   ¬«      }t        j"                  |t        j$                  |t        j&                  t        j(                  |dz
  z  «      «      |¬«      S )aÄ  Sample from Cauchy distribution (reparameterized).

        Args:
            shape (Sequence[int]): Sample shape.
            name (str, optional): Name for the operation (optional, default is None). For more information, please refer to :ref:`api_guide_Name`.

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

        Examples:

            .. code-block:: python

                >>> import paddle
                >>> from paddle.distribution import Cauchy

                >>> # init Cauchy with float
                >>> rv = Cauchy(loc=0.1, scale=1.2)
                >>> print(rv.rsample([10]).shape)
                [10]

                >>> # init Cauchy with 0-Dim tensor
                >>> rv = Cauchy(loc=paddle.full((), 0.1), scale=paddle.full((), 1.2))
                >>> print(rv.rsample([10]).shape)
                [10]

                >>> # init Cauchy with N-Dim tensor
                >>> rv = Cauchy(loc=paddle.to_tensor(0.1), scale=paddle.to_tensor([1.0, 2.0]))
                >>> print(rv.rsample([10]).shape)
                [10, 2]

                >>> # sample 2-Dim data
                >>> rv = Cauchy(loc=0.1, scale=1.2)
                >>> print(rv.rsample([10, 2]).shape)
                [10, 2]

                >>> rv = Cauchy(loc=paddle.to_tensor(0.1), scale=paddle.to_tensor([1.0, 2.0]))
                >>> print(rv.rsample([10, 2]).shape)
                [10, 2, 2]
        Ú_rsamplez1Expected type of shape is Sequence[int], but got ©r   ç      à?©r   )r   r   ÚnpÚndarrayr   r   ÚlistÚtupler   r   Ú_extend_shaper   Úexpandr   r   Úrandr   ÚaddÚmultiplyÚtanÚpi)r   r	   r   r   r   Úuniformss         r   r,   zCauchy.rsample”   sí   € ðR Ð'‰t¨d¯i©i¸*Ñ.Dˆä˜%¤"§*¡*¬i×.@Ñ.@Ä$ÌÐ!NÔOÜØCÄDÈÃKÀ=ÐQóð ô $ E¬5Ô1‘´u¸U³|ˆØ×"Ñ" 5Ó)ˆà�h‰h�o‰o˜eÓ$ˆØ—
‘
×!Ñ! %Ó(ˆÜ—;‘;˜u¨D¯J©JÔ7ˆÜ�z‰zØÜ�O‰O˜E¤6§:¡:¬b¯e©e°xÀ#±~Ñ.FÓ#GÓHØô
ð 	
r   c                 óÄ   — | j                   dz   }t        |t        j                  «      st	        dt        |«      › �«      ‚| j                  |«      j                  |¬«      S )a/  Probability density function(PDF) evaluated at value.

        .. math::

            { f(x; loc, scale) = \frac{1}{\pi scale \left[1 + \left(\frac{x - loc}{ scale}\right)^2\right]} = { 1 \over \pi } \left[ {  scale \over (x - loc)^2 +  scale^2 } \right], }

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

        Returns:
            Tensor: PDF evaluated at value.

        Examples:

            .. code-block:: python

                >>> import paddle
                >>> from paddle.distribution import Cauchy

                >>> # init Cauchy with float
                >>> rv = Cauchy(loc=0.1, scale=1.2)
                >>> print(rv.prob(paddle.to_tensor(1.5)))
                Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True,
                        0.11234467)

                >>> # broadcast to value
                >>> rv = Cauchy(loc=0.1, scale=1.2)
                >>> print(rv.prob(paddle.to_tensor([1.5, 5.1])))
                Tensor(shape=[2], dtype=float32, place=Place(cpu), stop_gradient=True,
                        [0.11234467, 0.01444674])

                >>> # init Cauchy with N-Dim tensor
                >>> rv = Cauchy(loc=paddle.to_tensor([0.1, 0.1]), scale=paddle.to_tensor([1.0, 2.0]))
                >>> print(rv.prob(paddle.to_tensor([1.5, 5.1])))
                Tensor(shape=[2], dtype=float32, place=Place(cpu), stop_gradient=True,
                        [0.10753712, 0.02195240])

                >>> # init Cauchy with N-Dim tensor with broadcast
                >>> rv = Cauchy(loc=paddle.to_tensor(0.1), scale=paddle.to_tensor([1.0, 2.0]))
                >>> print(rv.prob(paddle.to_tensor([1.5, 5.1])))
                Tensor(shape=[2], dtype=float32, place=Place(cpu), stop_gradient=True,
                        [0.10753712, 0.02195240])
        Ú_probú,Expected type of value is Variable, but got r2   )r   r   r   r   r   r   Úlog_probÚexp)r   Úvaluer   s      r   ÚprobzCauchy.probÐ   s]   € ðX �y‰y˜7Ñ"ˆä˜%¤×!3Ñ!3Ô4ÜØ>¼tÀE»{¸mÐLóð ð �}‰}˜UÓ#×'Ñ'¨TÐ'Ó2Ð2r   c                 ó¸  — | j                   dz   }t        |t        j                  «      st	        dt        |«      › �«      ‚| j                  | j                  |«      }t        j                  | j                  | j                  |g«      \  }}}t        j                  t        j                  t        j                  t        j                  ||«      |«      «      j                  «        t        j                  t        j                   |j"                  t%        j&                  t$        j(                  «      | j*                  ¬«      |j'                  «       «      |¬«      S )a‚  Log of probability densitiy function.

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

        Returns:
            Tensor: Log of probability densitiy evaluated at value.

        Examples:

            .. code-block:: python

                >>> import paddle
                >>> from paddle.distribution import Cauchy

                >>> # init Cauchy with float
                >>> rv = Cauchy(loc=0.1, scale=1.2)
                >>> print(rv.log_prob(paddle.to_tensor(1.5)))
                Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True,
                        -2.18618369)

                >>> # broadcast to value
                >>> rv = Cauchy(loc=0.1, scale=1.2)
                >>> print(rv.log_prob(paddle.to_tensor([1.5, 5.1])))
                Tensor(shape=[2], dtype=float32, place=Place(cpu), stop_gradient=True,
                        [-2.18618369, -4.23728657])

                >>> # init Cauchy with N-Dim tensor
                >>> rv = Cauchy(loc=paddle.to_tensor([0.1, 0.1]), scale=paddle.to_tensor([1.0, 2.0]))
                >>> print(rv.log_prob(paddle.to_tensor([1.5, 5.1])))
                Tensor(shape=[2], dtype=float32, place=Place(cpu), stop_gradient=True,
                        [-2.22991920, -3.81887865])

                >>> # init Cauchy with N-Dim tensor with broadcast
                >>> rv = Cauchy(loc=paddle.to_tensor(0.1), scale=paddle.to_tensor([1.0, 2.0]))
                >>> print(rv.log_prob(paddle.to_tensor([1.5, 5.1])))
                Tensor(shape=[2], dtype=float32, place=Place(cpu), stop_gradient=True,
                        [-2.22991920, -3.81887865])
        Ú	_log_probrA   r0   r2   )r   r   r   r   r   r   Ú_check_values_dtype_in_probsr   r   r   r   ÚsubtractÚsquareÚdivideÚlog1pr:   r   r	   r3   Úlogr=   r   ©r   rD   r   r   r   s        r   rB   zCauchy.log_prob  s   € ðP �y‰y˜;Ñ&ˆä˜%¤×!3Ñ!3Ô4ÜØ>¼tÀE»{¸mÐLóð ð ×1Ñ1°$·(±(¸EÓBˆÜ"×4Ñ4Ø�X‰X�t—z‘z 5Ð)ó
ÑˆˆU�Eô �‰ä—‘œfŸm™m¬F¯O©O¸EÀ3Ó,GÈÓOÓPß‰e‹gðô �J‰JÜ—‘˜CŸI™I¤r§v¡v¬b¯e©e£}¸D¿J¹JÔGØ—	‘	“óð ô	
ð 		
r   c                 óÀ  — | j                   dz   }t        |t        j                  «      st	        dt        |«      › �«      ‚| j                  | j                  |«      }t        j                  | j                  | j                  |g«      \  }}}t        j                  t        j                  t        j                  ||«      |«      |¬«      t        j                  z  dz   S )aÌ  Cumulative distribution function(CDF) evaluated at value.

        .. math::

            { \frac{1}{\pi} \arctan\left(\frac{x-loc}{ scale}\right)+\frac{1}{2}\! }

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

        Returns:
            Tensor: CDF evaluated at value.

        Examples:

            .. code-block:: python

                >>> import paddle
                >>> from paddle.distribution import Cauchy

                >>> # init Cauchy with float
                >>> rv = Cauchy(loc=0.1, scale=1.2)
                >>> print(rv.cdf(paddle.to_tensor(1.5)))
                Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True,
                        0.77443725)

                >>> # broadcast to value
                >>> rv = Cauchy(loc=0.1, scale=1.2)
                >>> print(rv.cdf(paddle.to_tensor([1.5, 5.1])))
                Tensor(shape=[2], dtype=float32, place=Place(cpu), stop_gradient=True,
                        [0.77443725, 0.92502367])

                >>> # init Cauchy with N-Dim tensor
                >>> rv = Cauchy(loc=paddle.to_tensor([0.1, 0.1]), scale=paddle.to_tensor([1.0, 2.0]))
                >>> print(rv.cdf(paddle.to_tensor([1.5, 5.1])))
                Tensor(shape=[2], dtype=float32, place=Place(cpu), stop_gradient=True,
                        [0.80256844, 0.87888104])

                >>> # init Cauchy with N-Dim tensor with broadcast
                >>> rv = Cauchy(loc=paddle.to_tensor(0.1), scale=paddle.to_tensor([1.0, 2.0]))
                >>> print(rv.cdf(paddle.to_tensor([1.5, 5.1])))
                Tensor(shape=[2], dtype=float32, place=Place(cpu), stop_gradient=True,
                        [0.80256844, 0.87888104])
        Ú_cdfrA   r2   r1   )r   r   r   r   r   r   rH   r   r   r   r   ÚatanrK   rI   r3   r=   rN   s        r   Úcdfz
Cauchy.cdfD  sÅ   € ðX �y‰y˜6Ñ!ˆä˜%¤×!3Ñ!3Ô4ÜØ>¼tÀE»{¸mÐLóð ð ×1Ñ1°$·(±(¸EÓBˆÜ"×4Ñ4Ø�X‰X�t—z‘z 5Ð)ó
ÑˆˆU�Eô
 �K‰KÜ—‘œfŸo™o¨e°SÓ9¸5ÓAÈôô �e‰eñð ñ	ð	
r   c           	      ó0  — | j                   dz   }t        j                  t        j                  | j                  j
                  t        j                  dt        j                  z  «      | j                  ¬«      | j                  j                  «       |¬«      S )a}  Entropy of Cauchy distribution.

        .. math::

            { \log(4\pi scale)\! }

        Returns:
            Tensor: Entropy of distribution.

        Examples:

            .. code-block:: python

                >>> import paddle
                >>> from paddle.distribution import Cauchy

                >>> # init Cauchy with float
                >>> rv = Cauchy(loc=0.1, scale=1.2)
                >>> print(rv.entropy())
                Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True,
                        2.71334577)

                >>> # init Cauchy with N-Dim tensor
                >>> rv = Cauchy(loc=paddle.to_tensor(0.1), scale=paddle.to_tensor([1.0, 2.0]))
                >>> print(rv.entropy())
                Tensor(shape=[2], dtype=float32, place=Place(cpu), stop_gradient=True,
                        [2.53102422, 3.22417140])

        Ú_entropyé   r0   r2   )r   r   r:   r   r   r	   r3   rM   r=   r   r   )r   r   s     r   ÚentropyzCauchy.entropy„  sa   € ð< �y‰y˜:Ñ%ˆÜ�z‰zÜ�K‰K˜Ÿ™Ÿ™¬¯©¨q´2·5±5©yÓ(9ÀÇÁÔLØ�J‰J�N‰NÓØô
ð 	
r   c           	      ó4  — | j                   dz   }t        |t        «      st        dt	        |«      › �«      ‚| j
                  }|j
                  }| j                  }|j                  }t        j                  t        j                  t        j                  ||«      d«      t        j                  t        j                  ||«      d«      «      j                  «       }dt        j                  ||«      z  j                  «       }t        j                  |||¬«      S )u]  The KL-divergence between two Cauchy distributions.

        Note:
            [1] FrÃ©dÃ©ric Chyzak, Frank Nielsen, A closed-form formula for the Kullback-Leibler divergence between Cauchy distributions, 2019

        Args:
            other (Cauchy): instance of Cauchy.

        Returns:
            Tensor: kl-divergence between two Cauchy distributions.

        Examples:

            .. code-block:: python

                >>> import paddle
                >>> from paddle.distribution import Cauchy

                >>> rv = Cauchy(loc=0.1, scale=1.2)
                >>> rv_other = Cauchy(loc=paddle.to_tensor(1.2), scale=paddle.to_tensor([2.3, 3.4]))
                >>> print(rv.kl_divergence(rv_other))
                Tensor(shape=[2], dtype=float32, place=Place(cpu), stop_gradient=True,
                        [0.19819736, 0.31532931])
        Ú_kl_divergencez*Expected type of other is Cauchy, but got é   rU   r2   )r   r   r   r   r   r   r   r   r:   ÚpowrI   rM   r;   )	r   Úotherr   Úa_locÚb_locÚa_scaleÚb_scaleÚt1Út2s	            r   Úkl_divergencezCauchy.kl_divergence©  sÝ   € ð2 �y‰yÐ+Ñ+ˆä˜%¤Ô(ÜØ<¼TÀ%»[¸MÐJóð ð —‘ˆØ—	‘	ˆà—*‘*ˆØ—+‘+ˆä�Z‰ZÜ�J‰J”v—z‘z '¨7Ó3°QÓ7Ü�J‰J”v—‘ u¨eÓ4°aÓ8ó
÷ ‰#‹%ð 	ð ”&—/‘/ '¨7Ó3Ñ3×8Ñ8Ó:ˆä�‰˜r 2¨DÔ1Ð1r   )N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   Úpropertyr$   r&   r(   r-   r,   rE   rB   rR   rV   rb   Ú__classcell__)r   s   @r   r   r      sv   ø„ ñõBEð6 ñ=ó ð=ð ñAó ðAð ñ?ó ð?ó.-ó`:
òx33òj=
ò~>
ò@#
öJ,2r   r   )
r   Únumpyr3   r   Úpaddle.baser   Úpaddle.distributionr   ÚDistributionr   r   r   r   Ú<module>rm      s*   ðó ã ã Ý !Ý ,ô}2ˆ\×&Ñ&õ }2r   