As part of an assignment, I have been asked to write an Octave function that will generate m pseudorandom outcomes of a geometric RV X with parameter p = 0.55 in 2 separate ways:
- Directly using the cdf of the RV
- Using the fact that the variable is produced using independent Bernoulli experiments.
For 2, my thoughts are:
- create a loop of m iterations
- create an inner loop that repeats until there is a successful trial
- have a counting variable in the inner loop that counts how many failures there are before a success, and store this count for each of the m iterations of the outer loop.
However, I am quite lost with part one, and am struggling to even understand what the question means by "directly using the cdf of the random variable".
EDIT
Here is what I have done so far:
function x = generate_geometric_cdf(p, m)
% generate geometric RV X with parameter p, m times using the cdf
emperical = zeros(1,m); % allocate array for empirical results of m simulations
for i = 1:m % iterate over m simulations
u = rand; % generate a random number between 0-1
emperical(i) = geocdf(u, p); % store the outcome of the geometric distribution at u
end
end
EDIT 2
geoinv
was the function I needed as opposed to geocdf
. geocdf(u, p)
returns the probability from the cdf for of value u
, with parameter p
whereas geoinv(u, p)
does the inverse and returns a random value given the probability u
and parameter p
.