Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 712
Haozhun Avatar asked Apr 22 '11 16:04

Haozhun


People also ask

How do you write probability density in Matlab?

y = pdf( name , x , A ) returns the probability density function (pdf) for the one-parameter distribution family specified by name and the distribution parameter A , evaluated at the values in x .

How do you construct a probability density function?

To get the probability from a probability density function, we need to integrate the area under the curve for a certain interval. The probability= Area under the curve = density X interval length. In our example, the interval length = 131-41 = 90 so the area under the curve = 0.011 X 90 = 0.99 or ~1.

How do you create a distribution in Matlab?

pd = makedist( distname ) creates a probability distribution object for the distribution distname , using the default parameter values. pd = makedist( distname , Name,Value ) creates a probability distribution object with one or more distribution parameter values specified by name-value pair arguments.

What does Normpdf do in Matlab?

Description. y = normpdf( x ) returns the probability density function (pdf) of the standard normal distribution, evaluated at the values in x . y = normpdf( x , mu ) returns the pdf of the normal distribution with mean mu and the unit standard deviation, evaluated at the values in x .


1 Answers

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:

enter image description here


UPDATE:

In newer versions of MATLAB the hist function is no longer recommended. Instead, you can use the histcounts function like so to produce the same figure as above:

data = [1 2 3 3 4];
N = histcounts(data, 'BinLimits', [0 10], 'BinMethod', 'integers', 'Normalization', 'pdf');
plot(N);
xlabel('Integer value');
ylabel('Probability');
like image 69
gnovice Avatar answered Sep 23 '22 00:09

gnovice