2013-12-12 30 views
1

我使用matlab来估计概率密度函数(pdf)。matlab pdf估计(ksdensity)不起作用

的代码是这样的

xi = -2:0.1:2; 
a1 = normpdf(xi, 1, 0.3); 
a2 = normpdf(xi, -1, 0.3); 
subplot(211); 
plot(xi, a1+a2); 
[f, xs] = ksdensity(a1+a2); 
subplot(212); 
plot(xs, f); 

和图片这样

Plot result

你看,估计是不工作的。

那么这里有什么问题?顺便说一句,在matlab中还有其他的pdf估计方法吗?

回答

3

这是更接近你期待什么?

ksdensity函数需要一个来自分布的样本向量,而您正在给它提供概率密度函数的值。

>> xi = -3:0.1:3; 
>> p1 = normpdf(xi, 1, 0.3); 
>> p2 = normpdf(xi,-1, 0.3); 
>> subplot(211) 
>> plot(xi, 0.5*p1+0.5*p2) 
>> a1 = 1 + 0.3 * randn(10000,1); % construct the same distribution 
>> a2 = -1 + 0.3 * randn(10000,1); % construct the same distribution 
>> [f, xs] = ksdensity([a1;a2]); 
>> subplot(212) 
>> plot(xs, f) 

enter image description here

+0

这是惊人的,非常有助于弄清楚我在想什么。 – SolessChong

1

ksdensity为您提供输入值的概率分布(默认为100分)。您的输入值a1 + a2的值介于0和1.5之间,其中大部分接近0,较小部分接近1.5。你看到的第二个情节反映了这种分布。

如果你想看到两个类似的情节,把作为输入ksdensity与集中在靠近-1元素的向量和1

+0

你还要点。 – SolessChong