5.0

5.0 | 5 ratings Rate this file 159 downloads (last 30 days) File Size: 1.98 KB File ID: #8891

RANDP

by Jos

 

01 Nov 2005 (Updated 25 Mar 2009)

Code covered by BSD License  

Random integers with given probabilities ( (v2.0)

Download Now | Watch this File

File Information
Description

  RANDP - pick random values with relative probability
 
      R = RANDP(PROB,..) returns integers in the range from 1 to
      NUMEL(PROB) with a relative probability, so that the value X is
      present approximately (PROB(X)./sum(PROB)) times in the matrix R.
 
      All values of PROB should be equal to or larger than 0.
 
      RANDP(PROB,N) is an N-by-N matrix, RANDP(PROB,M,N) and
      RANDP(PROB,[M,N]) are M-by-N matrices. RANDP(PROB, M1,M2,M3,...) or
      RANDP(PROB,[M1,M2,M3,...]) generate random arrays.
      RANDP(PROB,SIZE(A)) is the same size as A.
 
      Examples:
        R = randp([1 3 2],1,10000) ;
        % return a row vector with 10000 values with about 16650 two's
        histc(R,1:3) ./ numel(R)
 
        R = randp([1 1 0 0 1],10,1)
        % 10 samples evenly drawn from [1 2 5]
 
        % randomly select 100 elements according to a specific distribution
        V = {'Red','Green','Blue'} ;
        ind = randp([80 10 10],200,1) ;
        R = V(ind) ; % should contain about 160 'Red'
 
      Also see RAND, RANDPERM
               RANDPERMBREAK, RANDINTERVAL, RANDSWAP (MatLab File Exchange)

version 2.0, feb 2009

MATLAB release MATLAB 6.5 (R13)
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (5)
02 Nov 2005 urs (us) schwarz

nicely coded snippet
two (unimportant) comments:
1) why not put the validity test for P BEFORE the (potentially consuming) generation of all the random numbers...
2) why not add a pointer to RANDSAMPLE for those who own the stats tbx...
us

16 Aug 2008 Reza Farrahi Moghaddam

Very useful when dealing with user-defined PDFs.

21 Jun 2009 Carlos Baiz

Works great!

04 Sep 2009 Francesco Pozzi

Wow, it really is a nice function, very fast and efficient. It is exactly what I was looking for. Thank you.

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.

Please login to add a comment or rating.
Updates
03 Nov 2005

Slightly modified using hints from <us>

21 Aug 2006

fixed error when called with scalar size argument

05 Mar 2009

MATLAB Central test

25 Mar 2009

implementation of faster algorithm using histc

Tag Activity for this File
Tag Applied By Date/Time
statistics Jos 22 Oct 2008 08:05:12
distribution Cristina McIntire 10 Dec 2008 16:45:13
rand Cristina McIntire 10 Dec 2008 16:45:13
probability Cristina McIntire 10 Dec 2008 16:45:13
randomization Cristina McIntire 10 Dec 2008 16:45:13
select Cristina McIntire 10 Dec 2008 16:45:13
random Cristina McIntire 10 Dec 2008 16:45:13
pdf Jos 18 Feb 2009 04:20:28
cdf Jos 18 Feb 2009 04:20:31
density Jos 18 Feb 2009 04:20:41
function Jos 18 Feb 2009 04:20:41
distribution Jos 25 Mar 2009 15:56:40
user Jos 25 Mar 2009 15:56:41
density Minchul Shin 30 Jul 2009 00:17:16
 

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