I need to draw random numbers from a log-cauchy distribution which has density: $$f(x;\mu,\sigma)=\frac{1}{x\pi\sigma\left[1+\left(\frac{ln(x)-\mu}{\sigma}\right)^2\right]}\qquad x>0.$$ Can anyone help me out or point me to a book/paper that could show me how?
1 Answer
A variable $X$ has a log-cauchy distribution if $\log(X)$ has a cauchy distribution. So, we just need to generate cauchy random variables and exponentiate them to get something that is log-cauchy distributed.
We can generate from the cauchy distribution using inverse transform sampling, which says that if you plug random uniforms into the inverse CDF of a distribution, then what you get out has that distribution. The cauchy distribution with location $\mu$ and scale $\sigma$ has CDF:
$$ F(x) = \frac{1}{\pi} \arctan\left(\frac{x-\mu}{\sigma}\right)+\frac{1}{2} $$
it is straightforward to invert this function to find that
$$ F^{-1}(y) =\mu + \sigma\,\tan\left[\pi\left(y-\tfrac{1}{2}\right)\right] $$
Therefore if $U \sim {\rm Uniform}(0,1)$ then $Y = \mu + \sigma\,\tan\left[\pi\left(U-\tfrac{1}{2}\right)\right]$ has a cauchy distribution with location $\mu$ and scale $\sigma$ and $\exp(Y)$ has a log-cauchy distribution. Some R
code to generate from this distribution (without using rcauchy
:))
rlogcauchy <- function(n, mu, sigma)
{
u = runif(n)
x = mu + sigma*tan(pi*(u-.5))
return( exp(x) )
}
Note: since the cauchy distribution is very long tailed, when you exponentiate them on a computer you may get values that are numerically "infinite". I'm not sure there's anything to be done about that.
Also note that if you were to do inverse transform sampling using the log-cauchy quantile function directly, you'd have the same problem, since, after doing the calculation, you actually end up with the act same thing - $\exp \left( \mu + \sigma\,\tan\left[\pi\left(U-\tfrac{1}{2}\right)\right] \right)$