如何在MatLab中绘制概率密度函数?

时间:2011-04-22 16:08:35

标签: matlab statistics plot

x = [1 2 3 3 4]
cdfplot(x)

谷歌搜索后,我发现上面的代码将在Matlab中为我绘制累积分布函数 是否有一种简单的方法来绘制概率密度函数?

澄清。我需要一个具有均匀分布的x轴的图形。我希望它看起来不像条形图。 (我会有数百万的整数)
对不起,请再次更新。我的数据是整数,但实际上它们代表时间(我期望几个非常高的峰值在完全相同的值,而其他值应该看起来好像它们不是离散的)。我实际上开始想知道这实际上是不是离散整数本身。 CDF肯定会奏效,但是当谈到PDF时,它似乎比我预期的要复杂得多。

4 个答案:

答案 0 :(得分:7)

您可以使用函数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');

这是由此产生的情节:

enter image description here


UPDATE:

在较新版本的MATLAB中,不再推荐使用hist功能。相反,您可以像这样使用histcounts函数来生成与上面相同的数字:

data = [1 2 3 3 4];
N = histcounts(data, 'BinLimits', [0 10], 'BinMethod', 'integers', 'Normalization', 'pdf');
plot(N);
xlabel('Integer value');
ylabel('Probability');

答案 1 :(得分:7)

如果您需要连续分发功能,请尝试此操作。

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

哪个输出这个。 enter image description here

累积分布函数位于底部,核心密度估计位于顶部。

答案 2 :(得分:2)

在matlab帮助中键入“ksdensity”,您将找到能够为您提供连续PDF形式的函数。我想这正是你要找的。

答案 3 :(得分:0)

除了ksdensity(x)获得的平滑PDF外,您还可以使用ksdensity(x,'function','cdf')绘制平滑的CDF图。

enter image description here

相关问题