Wednesday, March 30, 2011

Matlab Plot: Multiple KDEs

Below is a script for plotting kernel density estimates (KDEs) in Matlab

%ScriptGraphCombo
%subplot, putting the two on the same graph.

%I. Start
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
clear

%load in the data;
load data/NB25.mat

%work with  
%X         5000x5             200000  double             
%Y         5000x5             200000  double     

linenu = 2;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%Note we can force the support:
%%%%%%%%%%%%%%%%%%%%%
%        x = [randn(30,1); 5+randn(30,1)];
%        xi = linspace(-10,15,201);
%        f = ksdensity(x,xi,'function','cdf');
%%%%%%%%%%%%%%%%%%%%%

%II. Graph - Combined DATA
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%this is if you simply use a normal density - keep away from zero
ep=.01;
Y = max(Y,ep);
[fA,xA] = ksdensity(Y(:,1));
[fB,xB] = ksdensity(Y(:,2));
[fC,xC] = ksdensity(Y(:,3));
[fD,xD] = ksdensity(Y(:,4));
[fE,xE] = ksdensity(Y(:,5));

xvector=[xA;xB;xC;xD;xE]';
fvector=[fA; fB; fC; fD; fE]';

plot(xvector, fvector, 'LineWidth',linenu);
legend('avg-1','avg-2', 'avg-3', 'avg-4', 'avg-5')
xlabel('Egg Count', 'fontsize',14)
ylabel('Probability Density', 'fontsize',14)




%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.