>> x = [randn(30,1); 5+randn(30,1)];
>> [f,xi] = ksdensity(x);
>> sum(f)

ans =


I ran the ksdensity function in MATLAB above. After getting the density values, I summed them up. I've got 5.5376, but I expected 1. What am I missing here? Is there any good introductory book that explains the mechanics of the kernel density estimation in line with the ksdensity function?

  • $\begingroup$ This question seems to come down to a misunderstanding of densities, which is addressed at stats.stackexchange.com/questions/4220. Does that solve your problem? $\endgroup$ – whuber Oct 6 '14 at 23:32
  • $\begingroup$ Thank you very much, whuber. Confusing, but I will dig in. $\endgroup$ – Bill TP Oct 6 '14 at 23:44
  • $\begingroup$ You appear to be summing the height of the density at some collection of values. Why would that sum to 1? $\endgroup$ – Glen_b Oct 7 '14 at 1:55

it integrates to one, not sums to one.

trapz(xi, f) should return something close to 1.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.