Statistics - How to Draw Probability Density Function in MatLab

3
9/18/2014 statistics - How to draw probability density function in MatLab? - Stack Overflow https://stackoverflow.com/questions/5757387/how-to-draw-probability-density-function-in-matlab 1/3 Take the 2-minute tour × Haozhun 2,096 1 12 35 3 Answers 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 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 You can generate a discrete probability distribution for your integers using the function HIST: data = [1 2 3 3 4]; %# Sample data xRange = 0:10; %# Range of integers to compute a probability for N = hist(data,xRange); %# Bin the data plot(xRange,N./numel(data)); %# Plot the probabilities for each integer xlabel('Integer value'); ylabel('Probability'); And here's the resulting plot: Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required. How to draw probability density function in MatLab? sign up log in tour help careers 2.0 add a comment

description

This details how to draw Probability Density Function in the MatLab environment. Useful for engineering or statictics, data science.

Transcript of Statistics - How to Draw Probability Density Function in MatLab

Page 1: Statistics - How to Draw Probability Density Function in MatLab

9/18/2014 statistics - How to draw probability density function in MatLab? - Stack Overflow

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

Take the 2-minute tour ×

Haozhun2,096 1 12 35

3 Answers

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 bargraph. (I would have millions of integers)Sorry, update again. My data are integers, but actually they represents time(I expect several quite high peakat exact same value while other value should look like as if they are not discrete). I'm actually starting towonder if this is actually not discrete integers inherently. CDF would definitely work, but when coming toPDF, 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

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

You can generate a discrete probability distribution for your integers using the function HIST:

data = [1 2 3 3 4]; %# Sample dataxRange = 0:10; %# Range of integers to compute a probability forN = hist(data,xRange); %# Bin the dataplot(xRange,N./numel(data)); %# Plot the probabilities for each integerxlabel('Integer value');ylabel('Probability');

And here's the resulting plot:

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

How to draw probability density function in MatLab?

sign up

log in

tour

help

careers 2.0

add a comment

Page 2: Statistics - How to Draw Probability Density Function in MatLab

9/18/2014 statistics - How to draw probability density function in MatLab? - Stack Overflow

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

gnovice71.8k 8 137 206

edited Apr 22 '11 at 17:13 answered Apr 22 '11 at 16:22

4 @gnovice: just a minor point that you should, in general, divide by the area of the histogram and not thenumber of data points to get a pdf. So the last line should read bar(X,N/trapz(X,N)) . Since in thisexample, the bin points are integers and unit spaced, both numel and trapz give the same answer, 4 , butif 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 probabilitydistribution) 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. Willthis 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

1 @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 anda spike of 0.4 when x equals 400. As an alternative way to display your data, you may want to try a STEM plotinstead 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 offaxis([-4 8 0 1])title('')

Which outputs this.

Page 3: Statistics - How to Draw Probability Density Function in MatLab

9/18/2014 statistics - How to draw probability density function in MatLab? - Stack Overflow

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

FriskyGrub81 6

Ali31 1

The CumulativeDistribution Function is on the bottom, the Kernel Density Estimate on the top.

answered Oct 19 '13 at 2:00

type "ksdensity" in matlab help and you will find out the function that will give you the continuous form ofPDF. I guess this is exactly what you are looking for.

answered May 18 '13 at 2:24

Not the answer you're looking for? Browse other questions tagged matlab statistics

plot or ask your own question.

add a comment

add a comment