parzen 窗的matlab实现

用一下程序简单实现使用parzen窗对正态分布的概率密度估计:

(其中核函数选用高斯核)

%run for parzen 
close all;clear all;clc;
x=normrnd(0,1,1,10000);%从正态分布中产生样本
f=-5:0.01:5;%确定横坐标范

% N=100   h= 0.25 , 1, 4
p1=Parzen(x,0.25,10,f); 
p2 = Parzen(x,1,10,f);
p3 = Parzen(x,4,10,f);
subplot(331)
plot(f,p1)
subplot(332)
plot(f,p2)
subplot(333)
plot(f,p3)

hold on
% N=100   h= 0.25 , 1, 4
p1=Parzen(x,0.25,100,f); 
p2 = Parzen(x,1,100,f);
p3 = Parzen(x,4,100,f);
subplot(334)
plot(f,p1)
subplot(335)
plot(f,p2)
subplot(336)
plot(f,p3)

hold on
% N=1000   h= 0.25 , 1, 4
p1=Parzen(x,0.25,1000,f); 
p2 = Parzen(x,1,1000,f);
p3 = Parzen(x,4,1000,f);
subplot(337)
plot(f,p1)
subplot(338)
plot(f,p2)
subplot(339)
plot(f,p3)

 

function  p =  Parzen(x,h,N,f)
%  高斯函数Parzen 窗  统计落在parzen窗内的估计概率
%  h - 窗长度
%  N -采样点数
b=0;
h1 =  h/sqrt(N);
for i=1:length(f)
    for j=1:N
    b= b+ exp(((f(i)-x(j))/h1).^2/(-2))/sqrt(2*pi)/h1;
    end
    p(i) =  b/N;
    b=0;
end
end

 

posted @ 2016-04-06 17:10  司马_羽鹤  阅读(2091)  评论(0编辑  收藏  举报