close

以下利用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> 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)> 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].


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

    變化是生活的調味料

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