-
Notifications
You must be signed in to change notification settings - Fork 0
/
discrete_sampler.m
41 lines (29 loc) · 936 Bytes
/
discrete_sampler.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
function samples_out=discrete_sampler(density,num_samples,replacement_option)
%
% Function that draws samples from a discrete density
%
% density - discrete probability density (should sum to 1)
% num_samples - number of samples to draw
% replacement_option: 1 for sampling with replacment, 0 for no replacment
samples_out = zeros(1,num_samples);
%% Get CDF
cum_density = cumsum(density);
%% Draw samples from uniform distribution
uni_samples = rand(1,num_samples);
a=1;
while (a<=num_samples)
binary = uni_samples(a)>cum_density;
highest = find(binary);
if isempty(highest)
samples_out(1,a) = 1;
else
samples_out(1,a) = highest(end)+1;
end
if ((~replacement_option) & (a>1)) %% if we aren't doing replacement
if (sum(samples_out(1,a)==samples_out(1,1:a-1))>0)
uni_samples(1,a)=rand; %% gen. new uniform sample
a=a-1; %% redo this sample
end
end
a=a+1;
end