matlab中二项式分布的拉普拉斯近似
Laplace approximation for binomial distribution in matlab
我使用 bionrnd()
函数生成随机向量和拉普拉斯近似公式来近似二项分布。但拉普拉斯直方图与二项分布直方图不同。
我的编码错误在哪里?
请帮助我,这是我的代码:
clear
clc
close all
n = input ('Please Enter Number of Exams : ');
p = input ('Please Enter Probability : ');
eta=n*p;
sigma=sqrt(n*p*(1-p));
for x=1:n*100;
a=rand(1,n);
b=0;
for i=1:n
if(a(i)>=1-p)
b=b+1;
end
end
c(x)=b;
d(x)=binornd(n , p);
e(x)=(1./(sqrt(2*pi*x*p*(1-p)))).* exp(-(((x+1)*p-(x*p)).^2)./(2*x*p*(1-p)));
end
E=sigma*e+eta;
histfit(c);
axis([0 n 0 inf])
M = mean(c);
varc=var(c);
disp(['var is : ', num2str(varc)]);
disp(['Mean is : ', num2str(M)]);
figure
histfit(d);
axis([0 n 0 inf])
M2 = mean(d);
varc2=var(d);
disp(['var is : ', num2str(varc2)]);
disp(['Mean is : ', num2str(M2)]);
figure
histfit(e,100);
axis([0 n 0 inf])
M3 = mean(e);
varc3=var(e);
disp(['var is : ', num2str(M3)]);
disp(['Mean is : ', num2str(M3)]);
e(x)就是这个link中拉普拉斯近似的公式:De Moivre Laplace theorem formula
输出是:
第三个图一定要给别人点赞。
问题 #1:c(x)
和 d(x)
是具有给定概率分布的随机数列表。当您绘制 c(x)
或 d(x)
的直方图时,您绘制的是每个数字的出现频率。这个频率刚好等于分布。
e(x)
是一个完全不同的对象。您已将其编码为概率分布 本身 ,而不是一组样本值。将 e(x)
中的值与 c(x)
或 d(x)
中的值进行比较。前者是浮点数列表,而后者是整数列表。将 e(x)
的出现频率绘制为直方图没有意义。
解决此问题的最简单方法可能是将 e
重写为函数,在分布 e
上生成一组随机样本,然后将它们绘制为直方图。然后你将以与其他分布相同的方式对待 e
。查看 http://au.mathworks.com/matlabcentral/fileexchange/26003-random-numbers-from-a-user-defined-distribution 以了解执行此操作的方法。
问题 #2:我不认为
e(y)=(1./(sqrt(2*pi*y*p*(1-p)))).*exp(-(((y+1)*p-(y*p)).^2)./(2*y*p*(1-p)));
是分布公式的正确翻译。你应该
e(y) = (1./(sqrt(2*pi*n*p*(1-p)))).*exp(-((y-n*p)).^2)./(2*n*p*(1-p)));
请注意,我已将公式编写为新变量 y
的函数,因为出于上述原因您无论如何都需要这样做。
我使用 bionrnd()
函数生成随机向量和拉普拉斯近似公式来近似二项分布。但拉普拉斯直方图与二项分布直方图不同。
我的编码错误在哪里?
请帮助我,这是我的代码:
clear
clc
close all
n = input ('Please Enter Number of Exams : ');
p = input ('Please Enter Probability : ');
eta=n*p;
sigma=sqrt(n*p*(1-p));
for x=1:n*100;
a=rand(1,n);
b=0;
for i=1:n
if(a(i)>=1-p)
b=b+1;
end
end
c(x)=b;
d(x)=binornd(n , p);
e(x)=(1./(sqrt(2*pi*x*p*(1-p)))).* exp(-(((x+1)*p-(x*p)).^2)./(2*x*p*(1-p)));
end
E=sigma*e+eta;
histfit(c);
axis([0 n 0 inf])
M = mean(c);
varc=var(c);
disp(['var is : ', num2str(varc)]);
disp(['Mean is : ', num2str(M)]);
figure
histfit(d);
axis([0 n 0 inf])
M2 = mean(d);
varc2=var(d);
disp(['var is : ', num2str(varc2)]);
disp(['Mean is : ', num2str(M2)]);
figure
histfit(e,100);
axis([0 n 0 inf])
M3 = mean(e);
varc3=var(e);
disp(['var is : ', num2str(M3)]);
disp(['Mean is : ', num2str(M3)]);
e(x)就是这个link中拉普拉斯近似的公式:De Moivre Laplace theorem formula
输出是:
第三个图一定要给别人点赞。
问题 #1:c(x)
和 d(x)
是具有给定概率分布的随机数列表。当您绘制 c(x)
或 d(x)
的直方图时,您绘制的是每个数字的出现频率。这个频率刚好等于分布。
e(x)
是一个完全不同的对象。您已将其编码为概率分布 本身 ,而不是一组样本值。将 e(x)
中的值与 c(x)
或 d(x)
中的值进行比较。前者是浮点数列表,而后者是整数列表。将 e(x)
的出现频率绘制为直方图没有意义。
解决此问题的最简单方法可能是将 e
重写为函数,在分布 e
上生成一组随机样本,然后将它们绘制为直方图。然后你将以与其他分布相同的方式对待 e
。查看 http://au.mathworks.com/matlabcentral/fileexchange/26003-random-numbers-from-a-user-defined-distribution 以了解执行此操作的方法。
问题 #2:我不认为
e(y)=(1./(sqrt(2*pi*y*p*(1-p)))).*exp(-(((y+1)*p-(y*p)).^2)./(2*y*p*(1-p)));
是分布公式的正确翻译。你应该
e(y) = (1./(sqrt(2*pi*n*p*(1-p)))).*exp(-((y-n*p)).^2)./(2*n*p*(1-p)));
请注意,我已将公式编写为新变量 y
的函数,因为出于上述原因您无论如何都需要这样做。