% 几何分布X~G(0,6)的代码如下
p = 0.6;
k = 1:5;
p_Xk = (1-p).^(k-1).*p;
figure %画图
stem(k,p_Xk);hold on;
xlabel('k');ylabel('P(X=k)');
title('几何分布的分布律,p=0.6');
xlim([1,length(k)]);
set(gca,'xtick',1:length(k))
hold off
% 二项分布X~B(26,0.6)的代码如下
n = 26;
p = 0.6;
k = 0:26;
p_Xk = zeros(1,length(k));%初始化概率
for i = 1:length(k)
p_Xk(i) = nchoosek(n,k(i))*p^k(i)*(1-p)^(n-k(i));
end
figure %画图
stem(k,p_Xk);hold on;
xlabel('k');ylabel('P(X=k)');
title('二项分布的分布律,n=26,p=0.6');
xlim([0,length(k)-1]);
set(gca,'xtick',0:(length(k)-1))
hold off
lamda = 3;
sum = 0;
for k=0:5
sum = sum+lamda^k/factorial(k)*exp(-lamda);
end
disp(sum);
例6代码:
lamda = 6;
sum = 0;
k=0;
while( sum <0.95 )
sum = sum+lamda^k/factorial(k)*exp(-lamda);
k=k+1;
end
disp(sum);
disp(2*k);
例7的计算代码:
disp(1-exp(-5)-5*exp(-5));
% 超几何分布X~h(n,N,M)=h(4,20,5)的代码如下
n = 4;
N = 20;
M = 5;
k = 0:min(n,M);
P_k = zeros(1,length(k));%使用0矩阵初始化概率
for i = 1:length(k)
P_k(i) = nchoosek(M,k(i))*nchoosek(N-M,n-k(i))/nchoosek(N,n);
end
figure %画图
stem(k,P_k);hold on;
xlabel('k');ylabel('P(X=k)');
title('超几何分布的分布律,X~h(4,20,5)');
xlim([0-0.2,length(k)-1]);
set(gca,'xtick',0:(length(k)-1))
for i=1:length(k)
text(k(i)-0.1,P_k(i)+0.02,num2str(P_k(i)));%将概率值显示出来
end
hold off
例9的计算代码:
disp(exp(-2)+2*exp(-2));
% 指数分布X~E(λ)
lamda = 1;
lamda1 = 2;
lamda2 = 3;
x = 0:0.01:5;
%- 概率密度函数
f_x = lamda*exp(-lamda*x);
f_x1 = lamda1*exp(-lamda1*x);
f_x2 = lamda2*exp(-lamda2*x);
%- 分布函数
F_x = 1-exp(-lamda*x);
F_x1 = 1-exp(-lamda1*x);
F_x2 = 1-exp(-lamda2*x);
figure %画概率密度函数图
plot(x,f_x);hold on;
plot(x,f_x1);plot(x,f_x2);
xlabel('x');ylabel('f(x)');
title('指数分布的概率密度函数,X~E(λ)');
xlim([min(x),max(x)]);
legend('λ=1','λ_1=2','λ_2=3');
hold off
figure %画分布函数图
plot(x,F_x);hold on;
plot(x,F_x1);plot(x,F_x2);
xlabel('x');ylabel('F(x)');
title('指数分布的分布函数,X~E(λ)');
xlim([min(x),max(x)]);
legend('λ=1','λ_1=2','λ_2=3');
hold off
% 正态分布X~N(u,σ²)
u = 0;
u1 = -2;
u2 = 1;
sigma_square = 1;
sigma_square1 = 4;
sigma_square2 = 9;
x = -6:0.01:6;
%- 概率密度函数
f_x = 1/sqrt( 2*pi*sigma_square )*exp( -(x-u).^2/(2*sigma_square) );
f_x1 = 1/sqrt( 2*pi*sigma_square )*exp( -(x-u1).^2/(2*sigma_square) );
f_x2 = 1/sqrt( 2*pi*sigma_square )*exp( -(x-u2).^2/(2*sigma_square) );
f_x3 = 1/sqrt( 2*pi*sigma_square1 )*exp( -(x-u).^2/(2*sigma_square1) );
f_x4 = 1/sqrt( 2*pi*sigma_square2 )*exp( -(x-u).^2/(2*sigma_square2) );
figure %画图,改变u
plot(x,f_x);hold on;
plot(x,f_x1);plot(x,f_x2);
xlabel('x');ylabel('f(x)');
title('正态分布的概率密度函数,X~N(u,σ²),改变u');
xlim([min(x),max(x)]);
legend('u=0,σ^2=1','u=-2,σ^2=1','u=1,σ^2=1');
hold off
figure %画图,改变σ²
plot(x,f_x);hold on;
plot(x,f_x3);plot(x,f_x4);
xlabel('x');ylabel('f(x)');
title('正态分布的概率密度函数,X~N(u,σ²),改变σ²');
xlim([min(x),max(x)]);
legend('u=0,σ^2=1','u=0,σ^2=4','u=0,σ^2=9');
hold off
% 以标准正态分布X~N(0,1)为例(使用非标准正态分布也能画),画σ、2σ、3σ、6σ准则图
u = 0;
sigma_square = 1;
x = -10:0.01:10;
%- 概率密度函数
f_x = 1/sqrt( 2*pi*sigma_square )*exp( -(x-u).^2/(2*sigma_square) );
plot(x,f_x);
hold on;
% 标注u-σ、u+σ
plot([u-sqrt(sigma_square),u-sqrt(sigma_square)],[0,1/sqrt(2*pi*sigma_square)*exp( -(u-sqrt( sigma_square )-u).^2/(2*sigma_square) )],'--');
text(u-sqrt(sigma_square)+0.01,0+0.01,'u-σ');
plot([u+sqrt(sigma_square),u+sqrt(sigma_square)],[0,1/sqrt(2*pi*sigma_square)*exp( -(u+sqrt(sigma_square)-u).^2/(2*sigma_square) )],'--');
text(u+sqrt(sigma_square)+0.01,0+0.01,'u+σ');
% 标注u-2σ、u+2σ
plot([u-2*sqrt(sigma_square),u-2*sqrt(sigma_square)],[0,1/sqrt(2*pi*sigma_square)*exp( -(u-2*sqrt( sigma_square )-u).^2/(2*sigma_square) )],'--');
text(u-2*sqrt(sigma_square)+0.01,0+0.01,'u-2σ' );
plot([u+2*sqrt(sigma_square),u+2*sqrt(sigma_square)],[0,1/sqrt(2*pi*sigma_square)*exp( -(u+2*sqrt(sigma_square)-u).^2/(2*sigma_square) )],'--');
text(u+2*sqrt(sigma_square)+0.01,0+0.01,'u+2σ' );
% 标注u-3σ、u+3σ
plot([u-3*sqrt(sigma_square),u-3*sqrt(sigma_square)],[0,1/sqrt(2*pi*sigma_square)*exp( -(u-3*sqrt( sigma_square )-u).^2/(2*sigma_square) )],'--');
text(u-3*sqrt(sigma_square)+0.01,0+0.01,'u-3σ' );
plot([u+3*sqrt(sigma_square),u+3*sqrt(sigma_square)],[0,1/sqrt(2*pi*sigma_square)*exp( -(u+3*sqrt(sigma_square)-u).^2/(2*sigma_square) )],'--');
text(u+3*sqrt(sigma_square)+0.01,0+0.01,'u+3σ' );
% 标注u-6σ、u+6σ
plot([u-6*sqrt(sigma_square),u-6*sqrt(sigma_square)],[0,1/sqrt(2*pi*sigma_square)*exp( -(u-6*sqrt( sigma_square )-u).^2/(2*sigma_square) )],'--');
text(u-6*sqrt(sigma_square)+0.01,0+0.01,'u-6σ' );
plot([u+6*sqrt(sigma_square),u+6*sqrt(sigma_square)],[0,1/sqrt(2*pi*sigma_square)*exp( -(u+6*sqrt(sigma_square)-u).^2/(2*sigma_square) )],'--');
text(u+6*sqrt(sigma_square)+0.01,0+0.01,'u+6σ' );
xlabel('x');ylabel('f(x)');
title('标准正态分布的几大σ准则');
xlim([min(x),max(x)]);
hold off
伽马分布代码:
% 伽马分布,X~Ga(α,λ)
alpha = 0.5;
alpha1 = 1;
alpha2 = 1.4;
alpha3 = 5;
lambda = 2;
x=0:0.01:5;
f_x = lambda^alpha/gamma(alpha)*x.^(alpha-1).*exp(-lambda*x);
f_x1 = lambda^alpha1/gamma(alpha1)*x.^(alpha1-1).*exp(-lambda*x);
f_x2 = lambda^alpha2/gamma(alpha2)*x.^(alpha2-1).*exp(-lambda*x);
f_x3 = lambda^alpha3/gamma(alpha3)*x.^(alpha3-1).*exp(-lambda*x);
figure %画图
plot(x,f_x,'r');hold on;
plot(x,f_x1,'m');plot(x,f_x2,'b');plot(x,f_x3,'g');
xlabel('x');ylabel('f(x)');
title('伽马分布,X~Ga(α,λ)');
xlim([min(x),max(x)]);
legend('α=0.5,λ=2','α=1,λ=2','α=1.4,λ=2','α=5,λ=2');
hold off