开发者

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 antici开发者_Go百科pated.


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:

How to draw probability density function in MatLab?


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');


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.

How to draw probability density function in MatLab?

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


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.


In addition to the smooth PDF obtained by ksdensity(x), you can also plot a smooth CDF plot using ksdensity(x,'function','cdf').

How to draw probability density function in MatLab?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜