Reputation: 65
What is the best and correct way to sample from a delayed geometric distribution (geometric distribution for positive integers greater 0) in matlab?
To my understanding the delayed geometric distribution is defined as follows, where f
is the probability mass function:
f(k)= (1-p)*p^(k-1) for k=1,2,3... and 0<p<1
My current approach to sample from that distribution is to first solve for k and defining a function that returns a random value:
function k = georndDel(p)
f = rand;
k = log(-(f*p)./(p-1))./log(p); %for log(p)<>0, 0<p<1
end
However, this returns a continuous value, sometimes negative, where I require only positive integers to be returned.
Is there amn obvious error in inverting the probability mass function? And how do I restrict the random values to positive ingegers?
Upvotes: 0
Views: 240
Reputation: 4684
You can generate the numbers manually:
cdf_val = rand();
k = log(1-cdf_val)/log(1-p);
k = round(k+0.5);
Or as Luis already mentioned using the geornd() plus 1.
Here is an example:
p = 0.1;
n = 20000;
arr1=zeros(n, 1);
arr2=zeros(n, 1);
for i=1:n
% manual approach
cdf_val = rand();
k = log(1-cdf_val)/log(1-p);
arr1(i) = round(k+0.5);
% using geornd
arr2(i) = geornd(p)+1;
end
histogram(arr1, 0:60);
hold on;
histogram(arr2, 0:60);
hold off;
legend('manual', 'geornd+1')
Upvotes: 2