正态分布与参数估计
预处理
clear
rng(6331);
mu = 1;
sigma = 1;
真实概率密度曲线:
fplot(@(x) exp(-(x-mu).^2./(2*sigma))./(sqrt(2*pi)*sigma) )
title("真实概率密度曲线")
四个样本直方图
n_1 = 10;
n_2 = 50;
n_3 = 500;
n_4 = 1000;
y_1 = sigma.*randn(n_1,1) + mu;
y_2 = sigma.*randn(n_2,1) + mu;
y_3 = sigma.*randn(n_3,1) + mu;
y_4 = sigma.*randn(n_4,1) + mu;
histogram(y_1,10)
title("n=10")
histogram(y_2,10)
title("n=50")
histogram(y_3,20)
title("n=500")
histogram(y_4,20)
title("n=1000")
四个样本参数估计值
矩估计==最大似然估计
[mu_, sigma_] =estimate(y_1 , n_1)
mu_ = 0.6865
sigma_ = 0.2972
[mu_, sigma_] =estimate(y_2 , n_2)
mu_ = 0.7853
sigma_ = 1.0036
[mu_, sigma_] =estimate(y_3 , n_3)
mu_ = 0.9213
sigma_ = 1.0244
[mu_, sigma_] =estimate(y_4 , n_4)
mu_ = 0.9908
sigma_ = 1.0422
参数估计误差随样本容量增大的变化曲线
矩估计==最大似然估计
样本容量取 10~1000
n = 10:1:1000;
dsigma = zeros(length(n),1);
dmu = zeros(length(n),1);
for i = 1:length(n)
y = sigma.*randn(n(i),1) + mu;
[mu_, sigma_] =estimate(y , n(i));
dsigma(i) = abs(sigma - sigma_);
dmu(i) = abs(mu - mu_);
end
plot(n,dsigma)
hold on
plot(n,dmu)
legend('△σ','△μ')
hold off
矩估计、最大似然估计函数
function [mu_, sigma_] = estimate(y , n)
mu_ = sum(y)/n;
sigma_ = sum((y-mu_).^2)/n;
end