I was looking here: numpy
And I can see you can use the command np.random.standard_cauchy()
specifying an array, to sample from a standard Cauchy.
I need to sample from a Cauchy which might have x_0 != 0
and gamma != 1
, i.e. might not be located at the origin, nor have scale equal to 1.
How can I do this?
Hence, to generate a standardized Cauchy, use the rand function in Matlab to generate a uniform [0,1] variate subtract 1/2 from it, multiply the result by π, and apply the tangent function. Repeat a bunch of times to get your sample.
The Cauchy distribution is the distribution of the x-intercept of a ray issuing from. with a uniformly distributed angle. It is also the distribution of the ratio of two independent normally distributed random variables with mean zero.
The Cauchy distribution has no finite moments, i.e., mean, variance etc, but it can be normalized and that's it.
The standard Cauchy distribution and the standard uniform distribution are related as follows: If U has the standard uniform distribution then X=G−1(U)=tan[π(U−12)] has the standard Cauchy distribution. If X has the standard Cauchy distribution then U=G(X)=12+1πarctan(X) has the standard uniform distribution.
If you have scipy, you can use scipy.stats.cauchy
, which takes a location (x0) and a scale (gamma) parameter. It exposes the rvs
method to draw random samples:
x = stats.cauchy.rvs(loc=100, scale=2.5, size=1000) # draw 1000 samples
You may avoid the dependency on SciPy, since the Cauchy distribution is part of the location-scale family. That means, if you draw a sample x
from Cauchy(0, 1)
, just shift it by x_0
and multiply with gamma
and x' = x_0 + gamma * x
will be distributed according to Cauchy(x_0, gamma)
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With