%%%%%%%%正态分布的概率密度函数估计
function myparzen02()
clc;
close all;
clear all;
N = 20480; %调整N的值可以改进精度
x = linspace(-3, 3, 100);
f = randn(N, 1); %%产生标准正态分布
X = zeros(N, 1);
X=f;
p = Parzen(x, X, 0.25, 1);
subplot(3,3,1);plot(x, p);title('N =1, h1 = 0.25');axis([-3, 3, 0, 1.5]);grid on;
p = Parzen(x, X, 1, 1);
subplot(3,3,2);plot(x, p);title('N =1, h1 = 1');axis([-3, 3, 0, 1.5]); grid on;
p = Parzen(x, X, 4, 1);
subplot(3,3,3);plot(x, p);title('N =1, h1 = 4');axis([-3, 3, 0, 1.5]); grid on;
p = Parzen(x, X, 0.25, 16);
subplot(3,3,4);plot(x, p);title('N =16, h1 = 0.25');axis([-3, 3, 0, 1.5]); grid on;
p = Parzen(x, X, 1, 16);
subplot(3,3,5);plot(x, p);title('N =16, h1 = 1');axis([-3, 3, 0, 1.5]); grid on;
p = Parzen(x, X, 4, 16);
subplot(3,3,6);plot(x, p);title('N =16, h1 = 4');axis([-3, 3, 0, 1.5]); grid on;
p = Parzen(x, X, 0.25, N);
subplot(3,3,7);plot(x, p);title('N =256, h1 = 0.25');axis([-3, 3, 0, 1.5]); grid on;
p = Parzen(x, X, 1, N);
subplot(3,3,8);plot(x, p);title('N =256, h1 = 1');axis([-3, 3, 0, 1.5]); grid on;
p = Parzen(x, X, 4,N);
subplot(3,3,9);plot(x, p);title('N =256, h1 = 4');axis([-3, 3, 0, 1.5]); grid on;
grid on;
end
function p = Parzen(x, X, h1, N) % x为横坐标; X为样本; h1用来调节窗宽h; N为样本个数.
h = h1 / sqrt(N);
% h为窗宽.
sum = zeros(1, 100);
for i = 1:N
sum = sum + normpdf((x - X(i))/h, 0, 1); %用正态窗函数作Parzen窗估计
end
p = sum/(N * h);
end