Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: how to randomly sample from nonstandard Cauchy distribution, hence with different parameters?

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?

like image 738
Euler_Salter Avatar asked Jul 17 '17 08:07

Euler_Salter


People also ask

How do you generate a random sample from a Cauchy distribution?

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.

What is Cauchy random variable?

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.

Is Cauchy distribution a normal distribution?

The Cauchy distribution has no finite moments, i.e., mean, variance etc, but it can be normalized and that's it.

How do you calculate Cauchy distribution?

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.


2 Answers

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
like image 174
MB-F Avatar answered Nov 03 '22 01:11

MB-F


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

like image 24
user3389669 Avatar answered Nov 03 '22 01:11

user3389669