Code covered by the BSD License  

Highlights from
Generation of Random Variates

image thumbnail

Generation of Random Variates

by

 

generates random variates from over 870 univariate distributions

gaus_ppf(xv, p_value);
% gaus_ppf.m - numerically estimate Gaussian (normal) Point Percent Function.
%
% created by:  J. Huntley,  09/15/04.
%

function [fppf,xppf] = gaus_ppf(xv, p_value);

rand('state',0);
m = 0;
sigma = 1;
nsamples = 1000;
delta = 1/(nsamples-1);
xv = 0:delta:1;
itmax = 100;
ftol = 1e-7;

for js = 1:nsamples                
    urd1 = rand(1);    			% uniform random deviate (0 : 1)
    % Sample the distribution's PPF by using MFP to numerically find the zeros of 
    % the function: CDF - URD.  PPF is evaluated explictly for each iteration.
    x0 = 0;
    xn = 1;   
    n = 0;
    f0 = gaus_cdf(x0, m, sigma) - urd1; 
    xi = xn;
    fi = gaus_cdf(xn, m, sigma) - urd1;
    while (abs(fi) > ftol & n <= itmax)  
        xn = xi;
        fn = fi;
        xi = (x0*fn - xn*f0) / (fn - f0);
        fi = gaus_cdf(xi, m, sigma) - urd1;
        if(abs(fi) <= ftol)
            break
        end
        n = n + 1;
        if(n > itmax)
            WARNING = 'n > itmax, gaus_ppf'
            break 
        end
        if((fi * fn) > 0)
            f0 = 0.5 * f0;
        else
            x0 = xn;
            f0 = fn;
        end
    end % while Loop
    xhold(js) = xi;
    fhold(js) = gaus_cdf(xi, m, sigma);
end % for loop

[xsort, index] = sort(xhold);
for js = 1:nsamples
    fsort(js) = fhold(index(js));
end

figure(3);
plot(xsort,fsort,'b');                  % plot CDF.
drawnow;

% Interpolate PPF values to uniform probability spacing, xi.
sxv = size(xv,2);
delta = 1/(sxv+1);
xppf = delta:delta:1-delta;
fppf = interp1(fsort,xsort,xppf,'spline');

figure(5);
plot(xppf,fppf,'b');                    % plot PPF.
drawnow;

return

Contact us