Kernel estimates using Gaussian.. Its working with histogram at the moment !!

Hey all...
I am trying to get the kernel work using Gaussian now.. I have it with histogram, I tried to find any relevant articles regarding the Gaussian technique in MATLAB but could not find.. I made it in different subplot to see what I am doing.. in this link it will show what I mean by Gaussian on each data plot(red dashes) rather than histogram..
https://www.ifas.jku.at/Portale/Institute/SOWI_Institute/ifas/content/e2550/e2770/e6038/files6234/JKU07HP.pdf?preview=preview
here is what I have and Its working for the histogram..
function Sa = trial(lambdaMax,lambda,T)
t = 0;
I = 0;
Sa = [];
u = rand;
t = t - log(u)/lambdaMax;
while t <= T
if (u <= lambda(t)/lambdaMax)
I = I+1;
Sa(I) = t;
end
u = rand;
t = t - log(u)/lambdaMax;
u=rand;
end
Script to run it,
lambdaMax=50;
T=20;
lambda =@(Sa) lambdaMax*(cos(Sa));
Sa = trial(lambdaMax,lambda,T);
figure
hold on
plot(Sa,lambda(Sa))
xlabel('t')
ylabel ('cos(x)')
X = linspace(min(Sa),max(Sa),100);
Y = pchip(Sa,lambda(Sa),X);
subplot(2,2,1)
plot(X,Y)
line(repmat(Sa,2,1),repmat([0;1],1,length(Sa)),'color','r' )
[f,xi] = ksdensity(Y);
subplot(2,2,2)
hist(Y)
subplot(2,2,3)
plot(xi,sum(Y)*f/sum(f),'g.-')

 Accepted Answer

In the line where you call ksdensity, it looks like you have the wrong input. Does this give what you expect?
[f,xi] = ksdensity(Sa,'width',0.2);

4 Comments

my problem is mainly with the histogram.. I want it to be gaussian on top of each data..
in this link the images there is what I mean by the gaussian plot that I am looking for,,
https://www.ifas.jku.at/Portale/Institute/SOWI_Institute/ifas/content/e2550/e2770/e6038/files6234/JKU07HP.pdf?preview=preview
There are many images in that document. Can you please specify exactly which plot on which page you want? If you substitute my version of the ksdensity() function into your code, then your green line will be the equivalent of the black line from the page "Graphs of Kernel Density Estimates for varying h" in that document.
If what you want is actually all the little red gaussians (which are an intermediate step), then you will need to find the gaussian parameters that are calculated inside ksdensity(), and plot those gaussians yourself.
Thanks your reply, It actually doing the right thing now, Can you explain to me though the line you recommended, what is 0.2 , width of what? and why 0.2?.. Thank you
That is the width of the gaussian kernel used for the estimation. This is explained in the help file for the ksdensity() function. You might also look at the section "Bandwidth Selection" on this page: http://en.wikipedia.org/wiki/Kernel_density_estimation
[Please accept this answer if you found it useful. That might help future users who have questions on kernel density estimation.]

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!