www.gusucode.com > stats 源码程序 matlab案例代码 > stats/KernelDistributionParametersExample.m
%% Kernel Distribution Parameters % This example shows an illustration of kernel density estimators, % smoothing functions, and bandwidth. %% Discrete Kernels SixMPG = [13;15;23;29;32;34]; figure histogram(SixMPG) %% Smoothing Function from Normals figure pdSix = fitdist(SixMPG,'Kernel','BandWidth',4); x = 0:.1:45; ySix = pdf(pdSix,x); plot(x,ySix,'k-','LineWidth',2) % Plot each individual pdf and scale its appearance on the plot hold on for i=1:6 pd = makedist('Normal','mu',SixMPG(i),'sigma',4); y = pdf(pd,x); y = y/6; plot(x,y,'b:') end hold off %% Available Kernel Shapes % Set plot specifications hname = {'normal' 'epanechnikov' 'box' 'triangle'}; colors = {'r' 'b' 'g' 'm'}; lines = {'-','-.','--',':'}; % Generate a sample of each kernel smoothing function and plot data = [0]; figure for j=1:4 pd = fitdist(data,'kernel','Kernel',hname{j}); x = -3:.1:3; y = pdf(pd,x); plot(x,y,'Color',colors{j},'LineStyle',lines{j}) hold on; end legend(hname{:}) hold off %% Kernel Shape Examples load carbig % Set plot specifications hname = {'normal' 'epanechnikov' 'box' 'triangle'}; colors = {'r' 'b' 'g' 'm'}; lines = {'-','-.','--',':'}; % Generate kernel distribution objects and plot figure for j=1:4 pd = fitdist(MPG,'kernel','Kernel',hname{j}); x = -10:1:60; y = pdf(pd,x); plot(x,y,'Color',colors{j},'LineStyle',lines{j}) hold on end legend(hname{:}) hold off %% Bandwidth Illustrations % Create kernel distribution objects pd1 = fitdist(MPG,'kernel'); pd2 = fitdist(MPG,'kernel','BandWidth',1); pd3 = fitdist(MPG,'kernel','BandWidth',5); % Compute each pdf x = -10:1:60; y1 = pdf(pd1,x); y2 = pdf(pd2,x); y3 = pdf(pd3,x); % Plot each pdf plot(x,y1,'Color','r','LineStyle','-') hold on plot(x,y2,'Color','k','LineStyle',':') plot(x,y3,'Color','b','LineStyle','--') legend({'BandWidth = Default','BandWidth = 1','BandWidth = 5'}) hold off