核密度估计(parzen窗密度估计)

摘要:
matlab中提供了核平滑密度估计函数ksdensity:[f,xi]=ksdensity返回矢量或两列矩阵x中的样本数据的概率密度估计f。该估计基于高斯核函数,并且在等间隔的点xi处进行评估,覆盖x中的数据范围。ksdensity估计单变量数据的100点密度,或双变量数据的900点密度。ksdensity适用于连续分布的样本。也可以指定评估点:[f,xi]=ksdensityspecifiespointstoevaluatef.Here,xiandptscontainidenticalvalues。

matlab中提供了核平滑密度估计函数ksdensity(x):

[f, xi] = ksdensity(x)

返回矢量或两列矩阵x中的样本数据的概率密度估计f。 该估计基于高斯核函数,并且在等间隔的点xi处进行评估,覆盖x中的数据范围。

ksdensity估计单变量数据的100点密度,或双变量数据的900点密度。

ksdensity适用于连续分布的样本。

也可以指定评估点:

[f,xi] = ksdensity(x,pts)specifies points (pts) to evaluatef. Here,xiandptscontain identical values。

核密度估计方法:

核密度估计(parzen窗密度估计)第1张

代码实现,对比:

clear all;
clc;
n=100;
% 生成一些正态分布的随机数
x=normrnd(0,1,1,n);
minx = min(x);
maxx = max(x);
dx = (maxx-minx)/n;
x1 = minx:dx:maxx-dx;
h=0.5;
f=zeros(1,n);
for j = 1:n
    for i=1:n
        f(j)=f(j)+exp(-(x1(j)-x(i))^2/2/h^2)/sqrt(2*pi);
    end
    f(j)=f(j)/n/h;
end
plot(x1,f);
%用系统函数计算比较
[f2,x2] = ksdensity(x);
hold on;
plot(x2,f2,'r'); %红色为参考

核密度估计(parzen窗密度估计)第2张

核密度估计(parzen窗密度估计)第3张

PRML上核密度估计(parzen窗密度估计)的实现:

%% Demonstrate a non-parametric (parzen) density estimator in 1D  
%% 
% This file is from pmtk3.googlecode.com
function parzenWindowDemo2
% setSeed(2);
%[data, f] = generateData;
%  http://en.wikipedia.org/wiki/Kernel_density_estimation
data = [-2.1 -1.3 -0.4 1.9 5.1 6.2]';
f = @(x) 0;
n = numel(data);
domain = -5:0.001:10;
kernels = {'gauss', 'unif'};
for kk=1:2
  kernel = kernels{kk};
  switch kernel
    case 'gauss', hvals = [1, 2];
    case 'unif', hvals = [1, 2];
  end
  for i=1:numel(hvals)
    h = hvals(i);
    figure;
    hold on
    handle=plot(data, 0.01*ones(1,n), 'x', 'markersize', 14);
    set(handle,'markersize',14,'color','k');
    g = kernelize(h, kernel, data);
    plot(domain,g(domain'),'-b');
    if strcmp(kernel, 'gauss')
      for j=1:n
        k = @(x) (1/h)*gaussProb(x,data(j),h^2);
        plot(domain, 0.1*k(domain'), ':r');
      end
    end
    title(sprintf('%s, h=%5.3f', kernel, h), 'fontsize', 16);
    %printPmtkFigure(sprintf('parzen-%s-%d', kernel, h));
  end
  %placeFigures('nrows',3,'ncols',1,'square',false);
end
end
function [data, f] = generateData
mix = [0.35,0.65];
sigma = [0.015,0.01];
mu = [0.25,0.75];
n = 50;
%% The true function, we are trying to recover
f = @(x)mix(1)*gaussProb(x, mu(1), sigma(1)) + mix(2)*gaussProb(x, mu(2), sigma(2));
%Generate data from a mixture of gaussians.
model1 = struct('mu', mu(1), 'Sigma', sigma(1));
model2 = struct('mu', mu(2), 'Sigma', sigma(2));
pdf1 = @(n)gaussSample(model1, n);
pdf2 = @(n)gaussSample(model2, n);
data = rand(n,1);
nmix1 = data <= mix(1);
data(nmix1) = pdf1(sum(nmix1));
data(~nmix1) = pdf2(sum(~nmix1));
end
function g = kernelize(h,kernel,data)
%Use one gaussian kernel per data point with smoothing parameter h.
n = size(data,1);
g = @(x)0;
%unif = @(x, a, b)exp(uniformLogprob(structure(a, b), x));
for i=1:n
  switch kernel
    case 'gauss', g = @(x)g(x) + (1/(h*n))*gaussProb(x,data(i),h^2);
    %case 'unif', g = @(x)g(x) + (1/n)*unif(x,data(i)-h/2, data(i)+h/2);
    case 'unif', g = @(x)g(x) + (1/(h*n))*unifpdf(x,data(i)-h/2, data(i)+h/2);
  end
end
end
function setupFig(h)
figure;
hold on;
axis([0,1,0,5]);
set(gca,'XTick',0:0.5:1,'YTick',[0,5],'box','on','FontSize',16);
title(['h = ',num2str(h)]);
scrsz = get(0,'ScreenSize');
left =  20;   right = 20;
lower = 50;   upper = 125;
width = scrsz(3)-left-right;
height = (scrsz(4)-lower-upper)/3;
set(gcf,'Position',[left,scrsz(4)/2,width, height]);
end
function p = gaussProb(X, mu, Sigma)
% Multivariate Gaussian distribution, pdf
% X(i,:) is i'th case
% *** In the univariate case, Sigma is the variance, not the standard
% deviation! ***
% This file is from pmtk3.googlecode.com
d = size(Sigma, 2);
X  = reshape(X, [], d);  % make sure X is n-by-d and not d-by-n
X = bsxfun(@minus, X, rowvec(mu));
logp = -0.5*sum((X/(Sigma)).*X, 2); 
logZ = (d/2)*log(2*pi) + 0.5*logdet(Sigma);
logp = logp - logZ;
p = exp(logp);        
end
function x = rowvec(x)
% Reshape a matrix into a row vector
% Return x as a row vector. This function is useful when a function returns a
% column vector or matrix and you want to immediately reshape it in a functional
% way. Suppose f(a,b,c) returns a column vector, matlab will not let you write
% f(a,b,c)(:)' - you would have to first store the result. With this function you
% can write rowvec(f(a,b,c)) and be assured that the result is a row vector.   
% This file is from pmtk3.googlecode.com
    x = x(:)';
end
function y = logdet(A)
% Compute log(det(A)) where A is positive-definite
% This is faster and more stable than using log(det(A)).
%
%PMTKauthor Tom Minka
% (c) Microsoft Corporation. All rights reserved.
% This file is from pmtk3.googlecode.com
try
    U = chol(A);
    y = 2*sum(log(diag(U)));
catch % #ok
    y = 0;
    warning('logdet:posdef', 'Matrix is not positive definite');
end
end

核密度估计(parzen窗密度估计)第4张

核密度估计(parzen窗密度估计)第5张

核密度估计(parzen窗密度估计)第6张

核密度估计(parzen窗密度估计)第7张

对比发现与MATLAB自带的函数的估计结果仍有一定的差异?

核密度估计(parzen窗密度估计)第8张

核密度估计(parzen窗密度估计)第9张

参考:

https://ww2.mathworks.cn/help/stats/ksdensity.html

https://zhidao.baidu.com/question/176640010444765324.html

免责声明:文章转载自《核密度估计(parzen窗密度估计)》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇漏洞扫描工具Nessu的安装和简单使用Equinox OSGi系列之 创建自己的OSGi应用项目下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

关于核空间与像空间的专题讨论

$f命题:$设${W_1},{W_2}$是线性空间$V$的任意两个子空间,则 $(1)$${W_1}$一定是$V$的某个线性变换的核 $(2)$${W_1}$一定是$V$的某个线性变换的像 $(3)$若$dim{W_1}{ m{ + }}dim{W_2}{ m{ = }}n$,则一定存在$sigma in Lleft( V ight)$,使得${W_1}{...

paper 62:高斯混合模型(GMM)参数优化及实现

高斯混合模型(GMM)参数优化及实现 (< xmlnamespace prefix ="st1" ns ="urn:schemas-microsoft-com:office:smarttags" />2010-11-13) 1 高斯混合模型概述< xmlnamespace prefix ="o" ns ="urn:schemas-micr...

Coursera 机器学习 第8章(下) Dimensionality Reduction 学习笔记

8 Dimensionality Reduction8.3 Motivation8.3.1 Motivation I: Data Compression第二种无监督问题:维数约简(Dimensionality Reduction)。通过维数约简可以实现数据压缩(Data Compression),数据压缩可以减少计算机内存使用,加快算法运算速度。什么是维数...

雷达方程分析

雷达方程分析 雷达方程是设计雷达系统的基础。雷达方程如下:  一般情况,雷达系统设计已知雷达需要探测距离R,所以雷达方程常进行变换应用,例如根据《雷达系统设计MATLAB仿真》中的: 其中,G为天线增益,λ为波长,σ为目标截面积,Pt为峰值功率,k为玻尔兹曼常数,Te有效温度,B带宽,F噪声系数,L雷达损失。  研究最小可检测信噪比SNR和不同探测距离...