3

I have a data which is 100x1 vector. How can I display its empirical pdf in Matlab? Also, if I want to compare the pdf of three vectors on the same graph, then how to do that?

Right now I am using pdfplot.m file to plot my empirical pdf, however when I want to compare the 3 distributions by using 'hold on', then firstly its not working and secondly all the distributions are in same color. Thanks!

EDIT: I don't want to plot cdf.

Amro
  • 123,847
  • 25
  • 243
  • 454
  • 2
    You realize that pdfplot.m simply plots a normalized histogram with 20 bins? So - what aspect of pdfplot is important for you that isn't among the default options of hist? – Jonas Sep 29 '10 at 19:11
  • Jonas: _"You realize that pdfplot.m simply plots a normalized histogram with 20 bins"_ - I didn't realize that fact :P –  Sep 29 '10 at 19:19

2 Answers2

12

What you are looking for is Kernel density estimation (also known as Parzen windows). Its implemented in KSDENSITY function in the Statistics toolbox:

data = randn(100,1);
ksdensity(data)

alt text

The Wikipedia entry above has a MATLAB example using a function submission on FEX

Amro
  • 123,847
  • 25
  • 243
  • 454
4

hist:

hist(data)

or, if you want more control over how it is presented, use:

[n,x] = hist(data);
plot(x,n,'rx-'); %# just an example, plot the pdf with red x's and a line, 
                 %# instead of bars
figure;
plot(x, cumsum(n)/sum(n)); %# plot the CDF
Ofri Raviv
  • 24,375
  • 3
  • 55
  • 55
  • Ofri: Is there a way that I can retain my all 100 values in the vector and plot them as hist (or pdf)? –  Sep 29 '10 at 19:10
  • 1
    try hist(data,min(data):max(data)) or [n,x]=hist(data,min(data):max(data)) – Ofri Raviv Sep 29 '10 at 19:26
  • _"try hist(data,min(data):max(data)) or [n,x]=hist(data,min(data):max(data))"_ - It doesn't work. Gives _out of memory_ error. –  Sep 29 '10 at 20:59