Sampling some elements without replacement out of big population, based on a probability distribution

Hello,
Given population U=1:20, The associated probability series is related to the importance of each element: p=[0.01 0.05 0.11 0.08 0.02 0.02 0.01 0.055 0.1 0.2 0.05 0.05 0.01 0.03 0.025 0.06 0.04 0.05 0.01 0.02] Based on this probability distribution I want to sample 10 numbers from population U without replacement (one number should be selected at most once).
The best codes I could composed is this one, but it is a code that results in sampling with replacement U = 1:20; p=[0.01 0.05 0.11 0.08 0.02 0.02 0.01 0.055 0.1 0.2 0.05 0.05 0.01 0.03 0.025 0.06 0.04 0.05 0.01 0.02]; n = 10; c = cumsum([0,p(:).']); c = c/c(end); [~,i] = histc(rand(1,n),c); r = v(i); % map to v values
A next code I tried is U = 1:20; p=[0.01 0.05 0.11 0.08 0.02 0.02 0.01 0.055 0.1 0.2 0.05 0.05 0.01 0.03 0.025 0.06 0.04 0.05 0.01 0.02]; %to get the cumulative distribution in reversed order q=flip(p); r=cumsum(q); s=[0 r];
n = 10;%number of columns that should be selected X = zeros(n,1);%initiate list to save selected columns
%draw 10 uniform numbers, each one represents an arbitrary cumulative distribution x = rand(n,1);
%link the cdf value depending on its range to the right element of the populatione for j=1:size(U,2)-1 X(x>=s(j) & x<s(j+1))=20-j; end
But this code gives me problems at the last line and I do not know how to cover the situation when cum probabiliy equals one.
Can somebody give me some feedback/suggestion?
Thank you in advance!

 Accepted Answer

Your question is mathematically inconsistent. Take a very simple example, a population of 2: [1,2], and you want to generate 1 with probability 3/4 and 2 with probability 1/4.

If you draw 2 samples without replacement you either get [1,2] or [2,1], so the prescribed probabilities of 3/4 and 1/4 never meet.

Now you could simulate the real-life drawing by a loop:

U = 1:20; 
p=[0.01 0.05 0.11 0.08 0.02 0.02 0.01 ...
   0.055 0.1 0.2 0.05 0.05 0.01 0.03 ...
   0.025 0.06 0.04 0.05 0.01 0.02]; 
n = 10; 
nset = 10000;
% without replacement
ik = zeros(nset,n);
for i=1:nset
    pk = p(:).';
    for k=1:n
        c = cumsum([0,pk]);
        c = c/c(end);
        [~,d] = histc(rand,c);
        ik(k) = d;
        pk(d) = 0;
    end
end
wor = U(ik);

This give one set of drawing n element without replacement. Repeat it as long as you like. But there is no warranty the appearance is the prescribed P.

For example the element #10 has prescribed probability of 0.2, however it can appears at mots once in when drawing a sequence of 10 without replacement, so the probability the element #10 appears can never goes above 1/10 = 0.1, which is not 0.2 you want it to be. (The histogram plot in the subsequent comment bellow also confirms this, look at bin #10)

1 Comment

If you run this code with the above data, you'll see the probability without replacement is distorted, it get equalized among elements (brown on the left). This is not the case with replacement (blue on the left)
U = 1:20;
p=[0.01 0.05 0.11 0.08 0.02 0.02 0.01 ...
0.055 0.1 0.2 0.05 0.05 0.01 0.03 ...
0.025 0.06 0.04 0.05 0.01 0.02];
n = 10;
nset = 10000;
% without replacement
ik = zeros(nset,n);
for i=1:nset
pk = p(:).';
for k=1:n
c = cumsum([0,pk]);
c = c/c(end);
[~,d] = histc(rand,c);
ik(i,k) = d;
pk(d) = 0;
end
end
wor = U(ik);
% with replacement
c = cumsum([0,p]);
c = c/c(end);
[~,d] = histc(rand(nset,n),c);
wr = U(d);
close all
subplot(1,2,1);
h2 = histogram(wr(:),20);
hold on
h1 = histogram(wor(:),20);
legend([h1,h2],'Without replacement','with replacement');
subplot(1,2,2);
bar(p)
title('prescribed probability');

Sign in to comment.

More Answers (1)

Bruno, the answer is very very clear, thanks a lot!

5 Comments

Hello,
Applying your suggestions helped a lot and with a small set I do not get any problems. Now I used the real set U, and the real probability distribution. Gradually I am decreasing the number of to be selected numbers in an iteration.
Uptill now I received two errors: 1) Index exceeds matrix dimensions. wor = U(ik); 2) Subscript indices must either be real positive integers or logicals. ik(i,k) = keep(d);
Now I can't figure out which dimension, when and how it is exeeded. For the second error: I do not understand the logic of that error at all.
This is the code: U = 1:1000;%number of columns to be selected
%create probabilities for simplicity aa=rand(1,5); p=aa./sum(aa,2);
nset=10000; %number of iterations n=1000;%number of columns to be sampled in each iteration
while n>=1
% without replacement ik = zeros(nset,n);%inititate matrix to save sample number nset for i=1:nset pk = p(:).'; keep = 1:length(p); for k=1:n c = cumsum([0,pk]); c = c/c(end); [~,d] = histc(rand,c); ik(i,k) = keep(d); %instead of clearing the supplied variables, it keeps them and deletes the rest. pk(d) = []; keep(d) = []; end end
wor = U(ik);
AScetched=A(:,wor);
AOccupRate=sum(A,1)/1000;
AScetchedOccupRate=sum(AScetched,2);
d=AOccupRate-AScetchedOccupRate;
error=[error norm(d,2)/size(A,1)];
aantalCol=[aantalCol n];
n=n/1.1;
n=floor(n);
end
You have 1000 objects (U), but you provide just the probability array p of length 5????
How you can expect it can work?
Please take care also the formatting of your post.
Also you might incorporate the last edited version of algorithm without KEEP but set P(D) = 0. It will be faster.

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!