randomCauchy

Generates pseudorandom numbers from a Cauchy distribution.

Synopsis

randomCauchy (nRandom)

Required Arguments

int nRandom (Input)
Number of random numbers to generate.

Return Value

An array of length nRandom containing the random Cauchy deviates.

Description

Function randomCauchy generates pseudorandom numbers from a Cauchy distribution. The probability density function is

\[f(x) = \frac{S}{\pi \left[S^2 + (x-T)^2\right]}\]

where T is the median and \(T-S\) is the first quartile. This function first generates standard Cauchy random numbers (\(T=0\) and \(S=1\)) using the technique described below, and then scales the values using T and S.

Use of the inverse CDF technique would yield a Cauchy deviate from a uniform (0, 1) deviate, u, as \(\tan\left[\pi(u-0.5)\right]\). Rather than evaluating a tangent directly, however, randomCauchy generates two uniform (−1, 1) deviates, \(x_1\) and \(x_2\). These values can be thought of as sine and cosine values. If

\[x_1^2 + x_2^2\]

is less than or equal to 1, then \(x_1/x_2\) is delivered as the unscaled Cauchy deviate; otherwise, \(x_1\) and \(x_2\) are rejected and two new uniform (−1, 1) deviates are generated. This method is also equivalent to taking the ration of two independent normal deviates.

Example

In this example, randomCauchy generates five pseudorandom Cauchy numbers. The generator used is a simple multiplicative congruential with a multiplier of 16807.

from __future__ import print_function
from numpy import *
from pyimsl.stat.randomCauchy import randomCauchy
from pyimsl.stat.randomSeedSet import randomSeedSet

n_random = 5
randomSeedSet(123457)
r = randomCauchy(n_random)
print("Cauchy random deviates: %8.4f%8.4f%8.4f%8.4f%8.4f" %
      (r[0], r[1], r[2], r[3], r[4]))

Output

Cauchy random deviates:   3.5765  0.9353 15.5797  2.0815 -0.1333