Sei sulla pagina 1di 3

9/18/2014

statistics - How to draw probability density function in MatLab? - Stack Overflow


sign up

Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no
registration required.

log in

tour

help

careers 2.0

Take the 2-minute tour

How to draw probability density function in MatLab?


x = [1 2 3 3 4]
cdfplot(x)
After Googling, I find the above code will draw a cumulative distribution function for me in Matlab.
Is there a simple way to draw a probability density function?
To Clarify. I need a graph that has an evenly distributed x-axis. And I would prefer it does not look like a bar
graph. (I would have millions of integers)
Sorry, update again. My data are integers, but actually they represents time(I expect several quite high peak
at exact same value while other value should look like as if they are not discrete). I'm actually starting to
wonder if this is actually not discrete integers inherently. CDF would definitely work, but when coming to
PDF, it seems it's more complicated than I anticipated.
matlab

statistics

plot

edited Apr 22 '11 at 19:19

asked Apr 22 '11 at 16:08


Haozhun
2,096 1 12 35

What do you mean by "evenly distributed x-axis"? gnovice Apr 22 '11 at 16:54
@gnovice As you've done in the new answer. Haozhun Apr 22 '11 at 17:17
Have a look at the ksdensity function. It is an implementation of the Kernel density estimation.
mathworks.com.au/help/toolbox/stats/ksdensity.html user1127125 Jan 3 '12 at 3:04
add a comment

3 Answers
You can generate a discrete probability distribution for your integers using the function HIST:
data = [1 2 3 3 4];
xRange = 0:10;
N = hist(data,xRange);
plot(xRange,N./numel(data));
xlabel('Integer value');
ylabel('Probability');

%#
%#
%#
%#

Sample data
Range of integers to compute a probability for
Bin the data
Plot the probabilities for each integer

And here's the resulting plot:

https://stackoverflow.com/questions/5757387/how-to-draw-probability-density-function-in-matlab

1/3

9/18/2014

statistics - How to draw probability density function in MatLab? - Stack Overflow

edited Apr 22 '11 at 17:13

answered Apr 22 '11 at 16:22


gnovice
71.8k 8 137 206

@gnovice: just a minor point that you should, in general, divide by the area of the histogram and not the
number of data points to get a pdf. So the last line should read bar(X,N/trapz(X,N)) . Since in this
example, the bin points are integers and unit spaced, both numel and trapz give the same answer, 4 , but
if this is not the case, they will be different. r.m. Apr 22 '11 at 16:57
@yoda: You are correct, but Gene mentioned having to do this for integer values (i.e. a discrete probability
distribution) so I thought I'd keep it simple. gnovice Apr 22 '11 at 17:03
Thank you for your answer, I've got one more question, gnovice. @yoda's comment raised my concern. Will
this still work correctly if x=[100 200 400 400 550] Haozhun Apr 22 '11 at 17:20
I'll try both on my actual data. Thank you all! Haozhun Apr 22 '11 at 17:24

@Gene: If you had data = [100 200 400 400 550]; and specified a range of integers like xRange =
0:600; , you would get a plot that was mostly 0 except for spikes of 0.2 when x equals 100, 200, and 550 and
a spike of 0.4 when x equals 400. As an alternative way to display your data, you may want to try a STEM plot
instead of a regular line plot. It may look better. gnovice Apr 22 '11 at 17:33

show 3 more comments

If you want a continuous distribution function, try this.


x = [1 2 3 3 4]
subplot(2,1,1)
ksdensity(x)
axis([-4 8 0 0.4])
subplot(2,1,2)
cdfplot(x)
grid off
axis([-4 8 0 1])
title('')
Which outputs this.

https://stackoverflow.com/questions/5757387/how-to-draw-probability-density-function-in-matlab

2/3

9/18/2014

statistics - How to draw probability density function in MatLab? - Stack Overflow

The Cumulative
Distribution Function is on the bottom, the Kernel Density Estimate on the top.
answered Oct 19 '13 at 2:00
FriskyGrub
81 6
add a comment

type "ksdensity" in matlab help and you will find out the function that will give you the continuous form of
PDF. I guess this is exactly what you are looking for.
answered May 18 '13 at 2:24
Ali
31 1
add a comment

Not the answer you're looking for? Browse other questions tagged matlab statistics
plot or ask your own question.

https://stackoverflow.com/questions/5757387/how-to-draw-probability-density-function-in-matlab

3/3

Potrebbero piacerti anche