How to generate a Cauchy random variable

How do I calculate a Cauchy random variable and how do I calculate the probability mass function to show it is "heavy tailed"


In general, the fundamental result you need is that for any random variable $X$ with distribution function $F$, the random variable $Y=F(X)$ has a uniform distribution on $[0,1]$. Consequently, if you can invert $F$, then you can use a uniform density to simulate back your random variable $X$, since $X = F^{-1}(Y)$.'

In your case, the cdf of a standard Cauchy is $$F=\dfrac{1}{\pi}\arctan(x)+\dfrac{1}{2}$$ and therefore if you let $$y= \dfrac{1}{\pi}\arctan(x)+\dfrac{1}{2}$$ you immediately get $$x = \tan{\left(\pi \left(y - \dfrac{1}{2}\right)\right)}$$

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 $\pi$, and apply the tangent function. Repeat a bunch of times to get your sample.

Another interesting way to simulate a Cauchy variable is based upon the observation that the ratio of two Normal distributions is Cauchy distributed. Hence you can generate two standard independent Normal arrays and do their ratio term by term. The resulting array will be Cauchy distributed.


The expected value of a random variable with density $f$ is equal to $$ \int_{-\infty}^\infty xf(x)\,dx $$ provided $$ \int_{-\infty}^\infty |x|f(x)\,dx<\infty. $$ If that last condition fails to hold, then some strange things can happen. One of those is that the conclusion of the strong law of large numbers will fail. With the Cauchy distribution, one has $$ \int_{-\infty}^\infty |x|f(x)\,dx=\infty. $$ Consequently things like the following can happen: \begin{align} & \int_{-a}^a x f(x)\,dx \to 0\text{ as } a\to\infty \\[8pt] \text{ but } & \int_{-a}^{2a} xf(x)\,dx\to\text{a nonzero number as }a\to\infty. \end{align} Work out the integrals and you'll see that. That's the "heavy-tailed" nature of the thing.

Now suppose $X$ is uniformly distributed between $-\pi/2$ and $\pi/2$. Then \begin{align} f_{\tan X}(w) & =\frac{d}{dw} F_{\tan X}(w) = \frac{d}{dw}\Pr(\tan X\le w) \\[8pt] & = \frac{d}{dw} \Pr(X\le\arctan w) \\[8pt] & = \frac{d}{dw}\int_{-\pi/2}^{\arctan w} \frac{dx}\pi \\[8pt] & = \frac1\pi\cdot\frac{d}{dz} \left( \arctan w +\frac\pi2 \right) \\[8pt] & = \frac{1}{\pi(1+w^2)}. \end{align}

So there you have a way to simulate a Cauchy-distributed random variable: First simulate a random variable uniformly distributed between $\pm\pi/2$. Then take its tangent (in radians).