Construct i.i.d. Bernoulli variables with MATLAB
Show older comments
Hello,
As known from the probability laws, the following formula is true for i.i.d. variables
: 

On the following code, I am trying to construct 5 i.i.d. Bernoulli variables with parameter p, and verify the above formula from a large number of experiments. However, as can be seen from the printed matrices on the end of the script, the above formula fails.
I know that the reason is the lack of independence between the random variables, however I have searched a lot and I have not found any solutions. Instead, I found many answers suggesting exactly the method I use here, i.e. "diag( rand(n,1) < p )". Where am I wrong?
rng(1);
n = 4;
p = 0.1;
experiments = 100000;
for e = 1 : experiments %e different draws of every random variable, in order to obtain enough samples
for ell=1:5 %5 different Psi variables
Psi_matrix(:,ell,e) = ( rand(n,1) < p );
end
end
mean1 = mean(Psi_matrix, 3);
prod1 = mean1(:,1).*mean1(:,2).*mean1(:,3).*mean1(:,4).*mean1(:,5); %product of expectations
prod0 = zeros(n,experiments);
for e=1:experiments
prod0(:,e) = Psi_matrix(:,1,e).*Psi_matrix(:,2,e).*Psi_matrix(:,3,e).*Psi_matrix(:,4,e).*Psi_matrix(:,5,e);
end
mean0 = mean(prod0, 2); %expectation of product
prod1 %Check the printed matrices - prod1 is unequal to mean0
mean0
Accepted Answer
More Answers (0)
Categories
Find more on Univariate Discrete Distributions 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!