4.5

4.5 | 2 ratings Rate this file 31 downloads (last 30 days) File Size: 28.7 KB File ID: #14469

Performing random numbers generator from a generic discrete distribution

by Gianluca Dorini

 

29 Mar 2007 (Updated 03 Apr 2007)

No BSD License  

This function extracts random numbers distributed over a discrete set; the PDF is user-defined

Download Now | Watch this File

File Information
Description

This function extracts a scalar/vector/matrix of random numbers with discrete Probability Distribution Function.
The PDF is specified by the user as a input vector.

This function is designed to be fast, and it is implemented within a .mex file

Following Olivier B. comments (that I acknowledge for his comments), I performed cross-comparisons with randp. gDiscrPdfRnd is faster with a ratio that increases with the number of number, i.e. for about 3 times faster for 10^6 numbers to over 40 times faster for 10^7 numbers.
Moreover, for large random arrays, randp seriously surcharges the RAM memory, whereas gDiscrPdfRnd limits thememory use to what is essential (tanksto the coding). In what follows the details of thecomparison are given.

>> tic;R = randp([1 3 2],1000000,1);toc

elapsed_time =

    0.4840

>> tic;R = gDiscrPdfRnd([1 3 2],1000000,1);toc

elapsed_time =

    0.1570

>> tic;R = randp([1 3 2],10000000,1);toc

elapsed_time =

   68.5780

>> tic;R = gDiscrPdfRnd([1 3 2],10000000,1);toc

elapsed_time =

    1.6410

>> 68.5780/1.6410

ans =

   41.7904

Required Products MATLAB Compiler
MATLAB release MATLAB 6.5 (R13)
Zip File Content  
Other Files gDiscrPdfRnd.dll,
gDiscrPdfRnd.m,
gDiscrPdfRnd.c
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (5)
03 Apr 2007 Olivier B.

There is no c file ?

03 Apr 2007 Jos x@y.z

Look at RANDP for a fast more matlabish approach:
http://www.mathworks.com/matlabcentral/fileexchange/loadFile.do?objectId=8891&objectType=FILE

10 Apr 2007 Olivier B.

Nice, works pretty fast. I will use it for MC simulation based on empirical distribution.

11 Sep 2009 Francesco Pozzi

I've tried this small experiment:

N = 100000; % sample size
w = 1:100; % quite steep distribution
w = w / sum(w); % distribution (pdf)
Y_std = sqrt(w .* (1 - w)); % Standard deviations associated to each probability

%%%%%%%%%%%%%%%%%

Y1 = randp(w, N, 1); % Try Jos function
Y1_w = histc(Y1, [1:100]); % absolute frequencies table
Y1_w = Y1_w / N; % transform into relative frequencies
Y1_w_std = sqrt(Y1_w .* (1 - Y1_w)); % Empirical standard deviations associated to each probability

%%%%%%%%%%%%%%%%%

Y2 = gDiscrPdfRnd(w, N, 1); % Try Gianluca Dorini's function
Y2_w = histc(Y2, [1:100]); % absolute frequencies table
Y2_w = Y2_w / N; % transform into relative frequencies
Y2_w_std = sqrt(Y2_w .* (1 - Y2_w)); % Empirical standard deviations associated to each probability

%%%%%%%%%%%%%%%%%

% Check if empirical standard deviations are coherent with theretical ones
plot(Y_std - Y1_w_std', '.b') % plot empirical differences for Jos function
hold on
plot(Y_std - Y2_w_std', '.r') % plot empirical differences for Gianluca Dorini's function

%%%%%%%%%%%%%%%%%

It looks as though both functions statistically behave very well. They are both excellent functions. Thank you.

13 Oct 2009 Christian Dorion

Hi,
  Thanks for sharing this file. I suggest you replace the hardcoded 32767 by RAND_MAX... Otherwise it works like a charm.

Please login to add a comment or rating.
Updates
02 Apr 2007

screenshot replacement

03 Apr 2007

1) the attached file was uncorrect. 2) performed comparisons with randp, as suggested by Olivier B., that I thanks for his review

Tag Activity for this File
Tag Applied By Date/Time
statistics Gianluca Dorini 22 Oct 2008 09:06:17
probability Gianluca Dorini 22 Oct 2008 09:06:17
random numbers Gianluca Dorini 22 Oct 2008 09:06:17
discrete pdf Gianluca Dorini 22 Oct 2008 09:06:17
 

MATLAB Central Terms of Use

NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Terms prior to use.

Contact us at files@mathworks.com