Random sample without replacement
Show older comments
Hi,all,
Does anybody know how to do random sample without replacement? The randsample function in matlab only supports sampling with replacement.
I made codes on my own, and it is really weird sometimes it works, but sometimes it shows error (Error using ==> randsample at 94 W must have length equal to N.):
function C=randsample_WithoutReplacement(m,n,A1,A2)
%A1:population
%A2:probability
B=zeros(m,1);
C=zeros(n,m);
s=transpose(1:1:length(A1));
ut=0;
loc=0;
A=A2;
for j=1:n
A=A2;
s=transpose(1:1:length(A1));
for i=1:m
B(i)=randsample(s,1,true,A);
[ut, loc] = ismember(B(i), s);
s(loc)=[];
A(loc)=[];
end
for i=1:m
C(j,i)=A1(B(i));
end
end
3 Comments
Andrew Newell
on 30 Jan 2012
What version of MATLAB do you have? In R2011a, randsample supports sampling with or without replacement.
Andrew Newell
on 30 Jan 2012
Oh - you mean it doesn't support weighted sampling without replacement.
Andrew Newell
on 31 Jan 2012
I can't reproduce your error.
Accepted Answer
More Answers (2)
Peter Perkins
on 31 Jan 2012
1 vote
As Andrew pointed out, randsample absolutely does do sampling without replacement, just not with weights. It looks like that's what you're asking for.
If you have access to R2011b, you can use the new datasample function in the Statistics Toolbox (a replacement for randsample, though randsample continues to work) for sampling with and without replacement, weighted or unweighted:
1 Comment
Qinpeng Wang
on 31 Jan 2012
Derek O'Connor
on 31 Jan 2012
If you don't have access to R2011b and randsample, then the function below is reasonably fast on my Dell Precision 690, 2.33GHz, 16GB ram, Windows 7 Professional, Matlab R2008b 64-bit.
It uses a rejection loop to call DiscITBS, which generates a single sample from a discrete distribution by doing a binary search on the CDF, which, by definition, is sorted in ascending order.
Membership in S is tested by the byte-array member. This is a bit expensive (of memory) but is fast and simple. If you have lots of memory, then use it.
The expected value of the running time is Ns*Ew*log(Np), where Ew = E(nw) is the expected number of trips around the rejection loop.
If Np is small then it doesn't matter what method you use. If Np = 10^6, and Ns < 0.25*Np then this method is quite fast because Ew will be small and log(Np) of binary search takes care of the large Np.
For example:
with Np = 10^6 and Ns = 10^3, nw = 3 and t = 0.05 secs.
with Np = 10^6 and Ns = 10^4, nw = 129 and t = 0.23 secs.
% -------------------------------------------------------------
function [S,nw] = DiscSampRej(x,p,Ns);
% -------------------------------------------------------------
% Generate a random sample of size Ns from x(1:Np) with prob
% p(1:Np), without replacement. Derek O'Connor 31 Jan 2012
% -------------------------------------------------------------
S = zeros(1,Ns);
Np = length(x);
member(1:Np) = false;
cdf = cumsum(p);
nw=0;
for k = 1:Ns
idx = DiscITBS(cdf);
while member(idx)
idx = DiscITBS(cdf);nw=nw+1;
end
S(k) = x(idx);
member(idx) = true;
end % function
% -------------------------------------------------------------
function idx = DiscITBS(cdf);
% -------------------------------------------------------------
% Uses the discrete Inverse Transform method with Binary Search
% This greatly reduces the number of iterations of the while-loop
% Time Complexity: O(log n)
u = rand;
L = 1; H = length(cdf);
while L <= H
m = floor(L/2+H/2);
if u < cdf(m)
H = m-1;
else
L = m+1;
end
end
idx = m;
% end function
Categories
Find more on Stable Distribution 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!