www.gusucode.com > stats 源码程序 matlab案例代码 > stats/FitaKernelDistributionObjecttoDataExample.m

    %% Fit a Kernel Distribution Object to Data  

%% 
% Load the sample data. Visualize the patient weight data using a histogram. 
load hospital
histogram(hospital.Weight)    

%%
% The histogram shows that the data has two modes, one for female patients
% and one for male patients.  

%% 
% Create a probability distribution object by fitting a kernel distribution
% to the patient weight data. 
pd_kernel = fitdist(hospital.Weight,'Kernel')  

%% 
% For comparison, create another probability distribution object by fitting
% a normal distribution to the patient weight data. 
pd_normal = fitdist(hospital.Weight,'Normal')  

%% 
% Define the x values and compute the pdf of each distribution. 
x = 50:1:250;
pdf_kernel = pdf(pd_kernel,x);
pdf_normal = pdf(pd_normal,x);  

%% 
% Plot the pdf of each distribution. 
plot(x,pdf_kernel,'Color','b','LineWidth',2);
hold on;
plot(x,pdf_normal,'Color','r','LineStyle',':','LineWidth',2);
legend('Kernel Distribution','Normal Distribution','Location','SouthEast');
hold off;    

%%
% Fitting a kernel distribution instead of a unimodal distribution such
% as the normal reveals the separate modes for the female and male patients.