0
votes

I am trying to simulate a simple bernuli simulation and also a simple geometric simulation on matlab and since I am new to matlab it seems a bit difficult. I have been using this to understand it better http://www.academia.edu/1722549/Useful_distributions_using_MATLAB but I Havent been able to make a good simulation so far.Can some help me or show me a good tutorial. thank you.

NEW EDIT: answer from here: this is my own asnwer that I try to com up with is it correct:

If we want to simulate Bernoulli distribution in Matlab, we can simply use random number generator rand to simulate a Bernoulli experiment. In this case we try to simulate tossing a coin 4 times with p = 0.5:

>> p = 0.5;
>> rand(1,4) < p
ans =
     1     1     1     0

Using function rand, it returns values distributed between 0 and 1. By using “ < “, every value that is less than 0.5 is a success and therefore it prints 1 for that value; and for values equal or greater than 0.5 is a failure and therefore it prints 0 for that value.
Our ans is: 1 1 1 0. Which means that 3 times we have value less than 0.5 and 1 times we had values greater or equal to 0.5.

2
I have updated my answer to manually simulate the pdf. Take into account that the precision will be determined by the parameter m. - tashuhka

2 Answers

1
votes

rand(1,n) < p will give count of tails in n Bernoulli trails assuming 1 is head. Alternatively, you can use binornd(n,p) function in MATLAB to simulate Bernoulli trial for n=1. One small caveat is that using rand(1,n) < p is quite faster as compared to binornd(n,p).

1
votes

From the Wikipedia and your link, you can reply the question on your own:

The Binomial distribution is the discrete probability distribution of the number of successes (n) in a sequence of n independent yes/no experiments. The Bernoulli distribution is a special case of the Binomial distribution where n=1.

function pdf = binopdf_(k,n,p)
    m = 10000;
    idx = 0;
    for ii=1:m
        idx = idx + double(nnz(rand(n,1) < p)==k);
    end
    pdf = idx/m;
end

For example, if I toss a fair coin (p=0.5) 20 times, how many tails will I get?

k = 0:20;
y_pdf = binopdf_(k,20,0.5);
y_cdf = cumsum(y_pdf);

figure;
subplot(1,2,1);
stem(k,y_pdf);
title('PDF');
subplot(1,2,2);
stairs(k,y_cdf);
axis([0 20 0 1]);
title('CDF');

If you see the PDF, the mean value of tails we will see is 10.

enter image description here

The geometric distribution probability distribution of the number X of Bernoulli trials needed to get one success.

function pdf = geopdf_(k,p)
    m = 10000;
    pdf = zeros(numel(k));
    for jj=1:numel(k)
        idx = 0;
        for ii=1:m
            idx = idx + double(nnz(rand(jj,1) < p) < 1);
        end
        pdf(jj) = idx/m;
    end
end

For example, how many times we have to toss a fair coin (p=0.5) to get one tail?

k = 0:20;
y_pdf = geopdf_(k,0.5);
y_cdf = cumsum(y_pdf);

figure;
subplot(1,2,1);
stem(k,y_pdf)
title('PDF');
subplot(1,2,2);
stairs(k,y_cdf);
axis([0 20 0 1]);
title('CDF');

If you see the PDF, we have 0.5 possibilities of getting a tail in the first trial, 0.75 possibilities of getting a tail in the first two trials, etc. enter image description here