Sampling some elements without replacement out of big population, based on a probability distribution
Show older comments
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
More Answers (1)
Clarisha Nijman
on 20 Oct 2018
0 votes
5 Comments
Clarisha Nijman
on 20 Oct 2018
Bruno Luong
on 20 Oct 2018
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.
Clarisha Nijman
on 21 Oct 2018
Bruno Luong
on 21 Oct 2018
Also you might incorporate the last edited version of algorithm without KEEP but set P(D) = 0. It will be faster.
Clarisha Nijman
on 22 Oct 2018
Categories
Find more on Uniform Distribution (Continuous) in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!