Statistics - How To Draw Probability Density Function in MatLab
Statistics - How To Draw Probability Density Function in MatLab
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
statistics
plot
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
https://stackoverflow.com/questions/5757387/how-to-draw-probability-density-function-in-matlab
1/3
9/18/2014
@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
https://stackoverflow.com/questions/5757387/how-to-draw-probability-density-function-in-matlab
2/3
9/18/2014
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