Code covered by the BSD License  

Highlights from
RANDP

5.0

5.0 | 7 ratings Rate this file 29 Downloads (last 30 days) File Size: 1.98 KB File ID: #8891

RANDP

by Jos (10584)

 

01 Nov 2005 (Updated 25 Mar 2009)

Random integers with given probabilities ( (v2.0)

| 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 (7)
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.

28 Jul 2010 Martin

What an elegant piece of code! Thanks for this.

22 Feb 2011 Reza Fathzadeh  
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 (10584) 22 Oct 2008 08:05:12
probability Cristina McIntire 10 Dec 2008 16:45:13
rand Cristina McIntire 10 Dec 2008 16:45:13
distribution Cristina McIntire 10 Dec 2008 16:45:13
randomization Cristina McIntire 10 Dec 2008 16:45:13
random Cristina McIntire 10 Dec 2008 16:45:13
select Cristina McIntire 10 Dec 2008 16:45:13
pdf Jos (10584) 18 Feb 2009 04:20:28
cdf Jos (10584) 18 Feb 2009 04:20:31
function Jos (10584) 18 Feb 2009 04:20:41
density Jos (10584) 18 Feb 2009 04:20:41
distribution Jos (10584) 25 Mar 2009 15:56:40
user Jos (10584) 25 Mar 2009 15:56:41
density Minchul Shin 30 Jul 2009 00:17:16
probability Jos (10584) 14 Jan 2011 04:03:42
rand Jos (10584) 14 Jan 2011 04:03:48
empirical distribution Jos (10584) 14 Jan 2011 06:21:33
user defined distribution Jos (10584) 14 Jan 2011 06:21:41

Contact us at files@mathworks.com