以下利用Matlab program來分別以Binomial RV及Bernoulli RV
%來試驗中央極限定理(Central Limit Theorem)
%Binomial RVs
%-------------MATLAB Script-------------------------------------
% file binomial_rv
n=input('Number of Bernoulli trials n=');
p=input('Parameter of Bernoulli RV p=');
m=input('Number of sample mean m=');
for k=1:m
i=0;
f=(1.0-p)^n;
q=f;
u=rand();
while f<=u
q=q*(p*(n-i)/((i+1.0)*(1.0-p)));
f=f+q;
i=i+1;
end
Binomial(k,1)=i;
end
mean=sum(Binomial)/m;
x=[1:m]';
Samples=[x Binomial];
% fprintf('\n\t Result of Binomial RVs\n')
% disp(Samples);
fprintf('The mean is = %f \n', mean);
---------------------------------------------------------------------------
>> binomial_rv
Number of Bernoulli trials n=6
Parameter of Bernoulli RV p=0.3
Number of sample mean m=10000
The mean is = 1.791700
>>
>> binomial_rv
Number of Bernoulli trials n=6
Parameter of Bernoulli RV p=0.3
Number of sample mean m=100000
The mean is = 1.801410
>>
The E[X]=np=1.8
As the number of samples approaches infinity, the sample mean approaches the E[X].
------------------------------------------------------
%以Bernoulli RV來測試CLT
%Bernoulli RVs
-------------MATLAB Script-------------------------------------
% file bernoulli_rv.m
n=input('Number of Bernoulli trials n=');
p=input('Parameter of Bernoulli RV p=');
B=rand(n,1)<=p;
mean=sum(B)/n;
x=[1:n]';
Samples=[x B];
% fprintf('\n\t Result of Binomial RVs\n')
% disp(Samples);
fprintf('The mean is = %f \n', mean);
----------------------------------------------------------------------
>> bernoulli_rv
Number of Bernoulli trials n=10000
Parameter of Bernoulli RV p=0.4
The mean is = 0.388700
>>
>> bernoulli_rv
Number of Bernoulli trials n=100000
Parameter of Bernoulli RV p=0.4
The mean is = 0.399340
>>
The E[X]=p=0.4.
It is very obvious that as the trials approaches infinity, the sample mean will approach E[x].
%來試驗中央極限定理(Central Limit Theorem)
%Binomial RVs
%-------------MATLAB Script-------------------------------------
% file binomial_rv
n=input('Number of Bernoulli trials n=');
p=input('Parameter of Bernoulli RV p=');
m=input('Number of sample mean m=');
for k=1:m
i=0;
f=(1.0-p)^n;
q=f;
u=rand();
while f<=u
q=q*(p*(n-i)/((i+1.0)*(1.0-p)));
f=f+q;
i=i+1;
end
Binomial(k,1)=i;
end
mean=sum(Binomial)/m;
x=[1:m]';
Samples=[x Binomial];
% fprintf('\n\t Result of Binomial RVs\n')
% disp(Samples);
fprintf('The mean is = %f \n', mean);
---------------------------------------------------------------------------
>> binomial_rv
Number of Bernoulli trials n=6
Parameter of Bernoulli RV p=0.3
Number of sample mean m=10000
The mean is = 1.791700
>>
>> binomial_rv
Number of Bernoulli trials n=6
Parameter of Bernoulli RV p=0.3
Number of sample mean m=100000
The mean is = 1.801410
>>
The E[X]=np=1.8
As the number of samples approaches infinity, the sample mean approaches the E[X].
------------------------------------------------------
%以Bernoulli RV來測試CLT
%Bernoulli RVs
-------------MATLAB Script-------------------------------------
% file bernoulli_rv.m
n=input('Number of Bernoulli trials n=');
p=input('Parameter of Bernoulli RV p=');
B=rand(n,1)<=p;
mean=sum(B)/n;
x=[1:n]';
Samples=[x B];
% fprintf('\n\t Result of Binomial RVs\n')
% disp(Samples);
fprintf('The mean is = %f \n', mean);
----------------------------------------------------------------------
>> bernoulli_rv
Number of Bernoulli trials n=10000
Parameter of Bernoulli RV p=0.4
The mean is = 0.388700
>>
>> bernoulli_rv
Number of Bernoulli trials n=100000
Parameter of Bernoulli RV p=0.4
The mean is = 0.399340
>>
The E[X]=p=0.4.
It is very obvious that as the trials approaches infinity, the sample mean will approach E[x].