Introduction

A Matlab code to generate random numbers following multi-dimensional Cauchy distributions is provided here.


Citation

The following publication contains mathematical details of the method and the code, which should be cited whenever relevant.


Code

cauchyn.m

function [x, r] = cauchyn(t, y, T, m, n)
% x - n-dimensional cauchy random numbers
% r - norm of x
% [t, y] - lookup table
% T - Cauchy parameter
% m - number of random numbers
% n - random number dimension

u = rand(m,1);
c = find(y < 0);
if isempty(c)
    y1 = y;
    t1 = t;
else
    y1 = y(c(size(c,1))+1:size(y,1),:);
    t1 = t(c(size(c,1))+1:size(t,1),:);
end
theta = interp1(y1, t1, u);
r = T * tan(theta);
x = randn(m,n);
x = repmat(r, [1,size(x,2)]) .* x ./ repmat(sqrt(sum(x .* x, 2)), [1,size(x, 2)]);

cauchyn_gen.m

function [t,y] = cauchyn_gen(n, s)
% n - dimension
% s - size of lookup table
[t,y] = ode45(@cauchyn_dydt, [pi/(2*s):pi/(2*s):pi/2], 0, 'AbsTol', n);

cauchyn_dydt.m

function dydt = cauchyn_dydt(t, y, n)
dydt = sin(t)^(n-1);

Usage

  1. Determine the number of random numbers (m), their dimension (n), and the Cauchy parameter (T).
  2. Get the n-dimensional CDF lookup table having a size of s. s=20000 is a reasonable choice.
    [t, y] = cauchyn_gen(n, s)
  3. Generate n-dimensional Cauchy random numbers x.
    [x, r] = cauchyn(t, y, T, m, n)

Back to top