
    ΑiH                        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KJrJr   " S	 S
\R$                  5      rg)    )annotationsN)TYPE_CHECKING)	framework)distribution)Sequence)Never)Tensordtypec                    ^  \ rS rSr% SrS\S'   S\S'   S\S'   S\S'    S       SU 4S
 jjjr\SS j5       r\SS j5       r	\SS j5       r
/ S	4     SS jjr/ S	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$ )Cauchy"   u  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|None, 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])
r	   locscaler
   strnameNc                ~  > Ub  UOSU l         [        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                  UR                  :w  a%  [        R                  " X/5      u  U l        U l        OXsU l        U l        U R                  R                   U l        ["        TU ]I  U R                  R                  SS9  g )Nr   z5Expected type of loc is Real|Variable|Value, but got z7Expected type of scale is Real|Variable|Value, but got  )shape
fill_value)batch_shapeevent_shape)r   
isinstancenumbersRealr   VariablepaddlepirValue	TypeErrortypefullr   broadcast_tensorsr   r   r
   super__init__)selfr   r   r   	__class__s       Z/var/www/html/banglarbhumi/venv/lib/python3.13/site-packages/paddle/distribution/cauchy.pyr$   Cauchy.__init__I   sO    !,D(	',,	 2 2FJJ4D4DE
 
 GS	{S  GLL)"4"4fjj6F6FG
 
 I$u+W  c7<<((++B37CeW\\**KKbU;E99##)#;#;SL#I DHdj#& DHdjXX^^
TXX^^D    c                    [        S5      e)zMean of Cauchy distribution.z Cauchy distribution has no mean.
ValueErrorr%   s    r'   meanCauchy.meanm   s     ;<<r)   c                    [        S5      e)z Variance of Cauchy distribution.z$Cauchy distribution has no variance.r+   r-   s    r'   varianceCauchy.variancer   s     ?@@r)   c                    [        S5      e)z*Standard Deviation of Cauchy distribution.z"Cauchy distribution has no stddev.r+   r-   s    r'   stddevCauchy.stddevw   s     =>>r)   c                    Ub  UOU R                   S-   n[        R                  " 5          U R                  X5      sSSS5        $ ! , (       d  f       g= f)a  Sample from Cauchy distribution.

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

Args:
    shape (Sequence[int], optional): Sample shape.
    name (str|None, 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:: pycon

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

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

        >>> # init Cauchy with 0-Dim tensor
        >>> rv = Cauchy(loc=paddle.full((), 0.1), scale=paddle.full((), 1.2))
        >>> print(rv.sample([10]).shape)
        paddle.Size([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)
        paddle.Size([10, 2])

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

        >>> rv = Cauchy(
        ...     loc=paddle.to_tensor(0.1),
        ...     scale=paddle.to_tensor([1.0, 2.0]),
        ... )
        >>> print(rv.sample([10, 2]).shape)
        paddle.Size([10, 2, 2])
N_sample)r   r   no_gradrsample)r%   r   r   s      r'   sampleCauchy.sample|   s<    h 'tdii).C^^<<, s   A
Ac                   Ub  UOU R                   S-   n[        U[        R                  [        R
                  [        R                  R                  [        [        45      (       d  [        S[        U5       35      e[        U[        5      (       a  UO
[        U5      nU R                  U5      nU R                  R                  U5      nU R                   R                  U5      n[        R"                  " XR$                  S9n[        R&                  " U[        R(                  " U[        R*                  " [        R,                  US-
  -  5      5      US9$ )a  Sample from Cauchy distribution (reparameterized).

Args:
    shape (Sequence[int], optional): Sample shape.
    name (str|None, 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:: pycon

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

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

        >>> # init Cauchy with 0-Dim tensor
        >>> rv = Cauchy(loc=paddle.full((), 0.1), scale=paddle.full((), 1.2))
        >>> print(rv.rsample([10]).shape)
        paddle.Size([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)
        paddle.Size([10, 2])

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

        >>> rv = Cauchy(
        ...     loc=paddle.to_tensor(0.1),
        ...     scale=paddle.to_tensor([1.0, 2.0]),
        ... )
        >>> print(rv.rsample([10, 2]).shape)
        paddle.Size([10, 2, 2])
_rsamplez1Expected type of shape is Sequence[int], but got r
         ?r   )r   r   npndarrayr   r   r   r   r   listtupler   r    _extend_shaper   expandr   randr
   addmultiplytanpi)r%   r   r   r   r   uniformss         r'   r9   Cauchy.rsample   s   b 'tdii*.DZZ++VZZ-=-=tUK
 
 CDK=Q  $E511uU|""5)hhooe$

!!%(;;uJJ7zzOOE6::beex#~.F#GH
 	
r)   c                    U R                   S-   n[        U[        R                  [        R
                  R                  45      (       d  [        S[        U5       35      eU R                  U5      R                  US9$ )a7  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])
_prob5Expected type of value is Variable or Value, but got r@   )r   r   r   r   r   r   r   r   r    log_probexp)r%   valuer   s      r'   probCauchy.prob   so    X yy7"%)"4"4fjj6F6F!GHHGU}U  }}U#''T'22r)   c                   U R                   S-   n[        U[        R                  [        R
                  R                  45      (       d  [        S[        U5       35      eU R                  U R                  U5      n[        R                  " U R                  U R                  U/5      u  p4n[        R                  " [        R                  " [        R                  " [        R                  " X5      U5      5      R!                  5       * [        R"                  " [        R$                  " UR&                  [(        R*                  " [(        R,                  5      U R.                  S9UR+                  5       5      US9$ )a  Log of probability density function.

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

Returns:
    Tensor: Log of probability density 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_probrP   r>   r@   )r   r   r   r   r   r   r   r   r    _check_values_dtype_in_probsr   r"   r   subtractsquaredividelog1prH   r!   r   rA   logrK   r
   r%   rS   r   r   r   s        r'   rQ   Cauchy.log_prob0  s   P yy;&%)"4"4fjj6F6F!GHHGU}U  11$((EB"44XXtzz5)
E fmmFOOE,GOPeg JJCIIrvvbee}DJJG		 	
 		
r)   c                   U R                   S-   n[        U[        R                  [        R
                  R                  45      (       d  [        S[        U5       35      eU R                  U R                  U5      n[        R                  " U R                  U R                  U/5      u  p4n[        R                  " [        R                  " [        R                  " X5      U5      US9[         R"                  -  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])
_cdfrP   r@   r?   )r   r   r   r   r   r   r   r   r    rX   r   r"   r   atanr[   rY   rA   rK   r^   s        r'   cdf
Cauchy.cdfo  s    X yy6!%)"4"4fjj6F6F!GHHGU}U  11$((EB"44XXtzz5)
E
 KKfooe95A ee 		
r)   c           	     *   U R                   S-   n[        R                  " [        R                  " U R                  R
                  [        R                  " S[        R                  -  5      U R                  S9U R                  R                  5       US9$ )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   r>   r@   )r   r   rH   r!   r   r   rA   r]   rK   r
   r   )r%   r   s     r'   entropyCauchy.entropy  sa    < yy:%zzKKq255y(9LJJNN
 	
r)   c           	     >   U R                   S-   n[        U[        5      (       d  [        S[	        U5       35      eU R
                  nUR
                  nU R                  nUR                  n[        R                  " [        R                  " [        R                  " XV5      S5      [        R                  " [        R                  " X45      S5      5      R                  5       nS[        R                  " XV5      -  R                  5       n[        R                  " XxUS9$ )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    rg   r@   )r   r   r   r   r    r   r   r   rH   powrY   r]   rI   )	r%   otherr   a_locb_loca_scaleb_scalet1t2s	            r'   kl_divergenceCauchy.kl_divergence  s    2 yy++%((<T%[MJ  		**++ZZJJvzz'3Q7JJvu4a8
 #% 	 &//'3388:rD11r)   )r
   r   r   r   )N)r   float | Tensorr   rw   r   
str | NonereturnNone)ry   r   )r   zSequence[int]r   rx   ry   r	   )rS   r	   ry   r	   )ry   r	   )rn   r   ry   r	   )__name__
__module____qualname____firstlineno____doc____annotations__r$   propertyr.   r1   r4   r:   r9   rT   rQ   rc   rh   ru   __static_attributes____classcell__)r&   s   @r'   r   r   "   s	   B 
KML
I  	"E"E "E 	"E
 
"E "EH = = A A ? ?
 &(D6-"6-/96-	6-r &(DE
"E
/9E
	E
N33j=
~>
@#
J,2 ,2r)   r   )
__future__r   r   typingr   numpyrA   r   paddle.baser   paddle.distributionr   collections.abcr   typing_extensionsr   r	   r
   Distributionr   r   r)   r'   <module>r      s<    #      ! ,('$^2\&& ^2r)   