KDEPLOT Computes and plots a kernel density estimate of PDF and optionally compares it with density g. CALL: f = kdeplot(data,g,plotflag,h); f = kernel density estimate of data, two column matrix. data = data vector. g = pdf, two column matrix (optional). plotflag = 0 no plotting 1 plot pdf (default) h = user specified bandwidth (optional) if not specified kdefun uses a two-stage direct plug-in method (see hldpi). Example: R = wgumbrnd(2,12,[],1,100); x = linspace(5,30,200); kdeplot(R,[x;wgumbpdf(x,2,12)]') See also kdefun, hldpi
L-stage Direct Plug-In estimate of smoothing parameter. | |
Kernel Density Estimator. | |
Display message and abort function. | |
Hold current graph. | |
Linearly spaced vector. | |
Linear plot. | |
Graph title. | |
X-axis label. | |
Y-axis label. |
001 function [fx] = kdeplot(data,g,plotflag,h) 002 %KDEPLOT Computes and plots a kernel density estimate of PDF 003 % and optionally compares it with density g. 004 % 005 % CALL: f = kdeplot(data,g,plotflag,h); 006 % 007 % f = kernel density estimate of data, two column matrix. 008 % data = data vector. 009 % g = pdf, two column matrix (optional). 010 % plotflag = 0 no plotting 011 % 1 plot pdf (default) 012 % h = user specified bandwidth (optional) 013 % if not specified kdefun uses a two-stage 014 % direct plug-in method (see hldpi). 015 % 016 % Example: 017 % R = wgumbrnd(2,12,[],1,100); 018 % x = linspace(5,30,200); 019 % kdeplot(R,[x;wgumbpdf(x,2,12)]') 020 % 021 % See also kdefun, hldpi 022 023 % Tested on: Matlab 5.3 024 % History: 025 % revised pab oct2005 026 % -changed call to kdefun due to new options structure 027 % revised pab 4 nov. 2004 028 % -kernel used in the call to hldpi changed from 'epan' to 'gauss' 029 % because gauss kernel is the only supported one. 030 % added ms 2000.08.22 031 032 error(nargchk(1,4,nargin)) 033 data=data(:); 034 035 if nargin<3|isempty(plotflag), 036 plotflag=1; 037 end 038 039 if nargin<2 040 g=[]; 041 end 042 043 kernel = 'gauss'; 044 if nargin<4 045 h=hldpi(data,kernel); 046 end 047 048 049 x=linspace(min(data)-h,max(data)+h,200); 050 051 options = { 'kernel', kernel,'hs', h}; 052 f = kdefun(data,options,x); 053 054 if plotflag 055 plot(x,f) 056 title('PDF') 057 ylabel(['f(x)']) 058 xlabel('x') 059 if ~isempty(g), 060 hold on, 061 plot(g(:,1),g(:,2),'r--'),hold off 062 end 063 end 064 065 x=x(:); 066 f=f(:); 067 if nargout>0 068 fx=[x f]; 069 end 070 071 072
Comments or corrections to the WAFO group