close

%使用Matlab來產生Bernoulli Random Variable序列之和的樣本空間 
%------------------------------MATLAB Script-------------------------------------- 
% file bernoulli_rvs 
% Sequence of Bernoulli Random variables 
% X_i are Bernoulli RVs with i=1,...,n 
n=input('Number of RVs='); 
m=input('Number of samples of the sum=');
 p=input('Parameter of Bernoulli RV='); 

%Sn=X_1+X_2+...+X_100 
B=rand(n,m)<=p; 
for i=1:m 
SUMOFBRV(i,1)=sum(B(:,i)); 
end k=[1:m]'; 
Samples=[k SUMOFBRV]; 
fprintf('\n\tSome samples of the sum of these %d Bernoulli RVs',n);
 fprintf('\n\t i \t S(i)\n'); 
disp(Samples); 
-------------------------------------------------------------------------- 
>> bernoulli_rvs Number of RVs n=100 
Number of samples of the sum m=10
Parameter of Bernoulli RV p=0.3 
>>
 random_sum 
Some samples of the sum of these 100 RVs
 i S(i) 
1 25 
2 34 
3 36
4 36 
5 36 
6 32 
7 32 
8 29 
9 31 
10 39 The probability density function of the sum of these n Bernoulli RVs can be approached by directly counting the number of relative frequencies of all possible numbers under a large number of trails. The following matlab program executes 100000 trials and counts the frequencies of all possible numbers. 
-------------------------------------MATLAB Script-------------------------------------
% pdf_of_the_sum.m 
n=input('Number of RVs n='); 
m=input('Number of samples of the sum m='); 
p=input('Parameter of Bernoulli RV p='); 
B=rand(n,m)<=p; 
for i=1:m 
SUMOFBRV(i,1)=sum(B(:,i)); 
end 
x=1:100; 
SEQ=hist(SUMOFBRV,x); 
SEQ1=SEQ/m; 
plot(x,SEQ1); 
xlabel('s') ylabel('pdf fS(s)') 
title('Probability density function of the sum of the 100 Bernoulli RVs') 
>> pdf_of_the_sum 
Number of RVs n=100 
Number of samples of the sum m=100000 
Parameter of Bernoulli RV p=0.3 
>> 

  



There is another way to approach the pdf of the sum of the 100 Bernoulli RVs. Since Sn=X1+X2+…+X100 is the sum of the 100 Bernoulli RVs, the value of Sn equals to the number of successes of Binomial random variables. Thus, the pdf of the sum of the 100 RVs can be approached via Binomial distribution. The following Matlab program draws the pdf of the sum via Binomial distribution.
 
-------------MATLAB Script-------------------------------------
function y=combination(n,k)
% Compute binomial coefficient (n,k)
% k may be a matrix of integers between 0 and n
% result is a matrix of the same dimension
y=round(gamma(n+1)./(gamma(k+1).*gamma(n+1-k)));
 
-------------MATLAB Script-------------------------------------
% File binomial_dist.m
% Draw pdf or pmf of Binomial random variable
% version 11/5/2007
n=input('Number of RVs n=');
p=input('Parameter of Bernoulli RV p=');
for i=1:n
P(i)=combination(n,i)*(p^i)*(1-p)^(n-i);
end
x=1:n
plot(x,P);
xlabel('s')
ylabel('pdf fS(s)')
title('Probability density function of the sum of the 100 Bernoulli RVs')


arrow
arrow
    全站熱搜
    創作者介紹
    創作者 Irwinlin 的頭像
    Irwinlin

    變化是生活的調味料

    Irwinlin 發表在 痞客邦 留言(0) 人氣()