Cauchy

class paddle.distribution. Cauchy ( loc, scale, name=None ) [source]

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):

\[{ 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], }\]
Parameters
  • 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 Name.

Examples

>>> 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])
property mean

Mean of Cauchy distribution.

property variance

Variance of Cauchy distribution.

property stddev

Standard Deviation of Cauchy distribution.

sample ( shape, name=None )

sample

Sample from Cauchy distribution.

Note

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

Parameters
  • shape (Sequence[int]) – Sample shape.

  • name (str, optional) – Name for the operation (optional, default is None). For more information, please refer to Name.

Returns

Sampled data with shape sample_shape + batch_shape + event_shape.

Return type

Tensor

Examples

>>> 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]
rsample ( shape, name=None )

rsample

Sample from Cauchy distribution (reparameterized).

Parameters
  • shape (Sequence[int]) – Sample shape.

  • name (str, optional) – Name for the operation (optional, default is None). For more information, please refer to Name.

Returns

Sampled data with shape sample_shape + batch_shape + event_shape.

Return type

Tensor

Examples

>>> 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]
prob ( value )

prob

Probability density function(PDF) evaluated at value.

\[{ 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], }\]
Parameters

value (Tensor) – Value to be evaluated.

Returns

PDF evaluated at value.

Return type

Tensor

Examples

>>> 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])
log_prob ( value )

log_prob

Log of probability densitiy function.

Parameters

value (Tensor) – Value to be evaluated.

Returns

Log of probability densitiy evaluated at value.

Return type

Tensor

Examples

>>> 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])
cdf ( value )

cdf

Cumulative distribution function(CDF) evaluated at value.

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

value (Tensor) – Value to be evaluated.

Returns

CDF evaluated at value.

Return type

Tensor

Examples

>>> 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])
property batch_shape

Returns batch shape of distribution

Returns

batch shape

Return type

Sequence[int]

entropy ( )

entropy

Entropy of Cauchy distribution.

\[{ \log(4\pi scale)\! }\]
Returns

Entropy of distribution.

Return type

Tensor

Examples

>>> 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])
property event_shape

Returns event shape of distribution

Returns

event shape

Return type

Sequence[int]

probs ( value )

probs

Probability density/mass function.

Note

This method will be deprecated in the future, please use prob instead.

kl_divergence ( other ) [source]

kl_divergence

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

Parameters

other (Cauchy) – instance of Cauchy.

Returns

kl-divergence between two Cauchy distributions.

Return type

Tensor

Examples

>>> 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])