Domanda

I'm estimating a probability density function (pdf) using matlab.

The code is like this

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

and pics like this

Plot result

You see the estimation is not working at all.

So what's wrong here? BTW is there other pdf estimation methods in matlab?

È stato utile?

Soluzione

Is this closer to what you expect?

The ksdensity function expects a vector of samples from the distribution, whereas you were feeding it the values of the probability density function.

>> 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

Altri suggerimenti

ksdensity gives you the probability distribution (100 points by default) of the input values. Your input value a1+a2 has values that range between 0 and 1.5, with a large potion of those close to 0 and a smaller portion near 1.5. The second plot you see reflects this distribution.

If you want to see two similar plots, put as an input to ksdensity a vector with elements concentrated near -1 and 1.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top