MATLAB Answers

Kernel density estimation Problem graph ?

2 views (last 30 days)
Lakshit Chugh
Lakshit Chugh on 3 May 2020
Answered: Shubh Sahu on 6 May 2020
l = linspace(-4,4,500);
fhat = zeros(size(l));
h = 1.06*n^(-1/5);
hold on
for i=1:n % get each kernel function evaluated at x% centered at data
f = exp(-(1/(2*h^2))*(l-x(i)).^2)/sqrt(2*pi)/h;
plot(l,f/(n*h));
fhat = fhat+f/(n);
end
plot(l,fhat);
hold off
Thats my code for kernel density estimation where x(i) is dataset of mixture of gaussian and beta distribution but the graph i am getting is value so small that even i cant see it and i am not sure is it right?

Answers (1)

Shubh Sahu
Shubh Sahu on 6 May 2020
See this piece of code:
f = exp(-(1/(2*h^2))*(l-x(i)).^2)/sqrt(2*pi)/h;
plot(l,f/(n*h));
In this f is a point and l is a vector. So in first place you are plotting point against a vector so it will be very difficult to see. Make fhat an array.
fhat(i)= fhat(i-1)+f/(n);
Note: I will strongly recommend you to see this link. Never plot anything inside for loop until it's very necessary to do that.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by