Scale ksdensity

Hello everyone.. I have data on my X-axis I import it from a file and I am trying to estimate the kernel function for it.. I believe the code for the kernel is right but I only can see my data(red) and not the kernel but I can see a blue line on the axes and I think its the kernel but not scaled .. I did the scaling for the data but don't know how to scale the ksdensity. I don't know how can you scale two different plots using axes? or is there another way I can make the kernel visible in my plot?
work = importdata('data.dat');
x = work(:,1);
X = (x).';
a= min(X);
b=max(X);
[xi,f]=ksdensity(X);
plot(f,xi);line(repmat(X,2,1),repmat([0;1],1,length(X)),'color','r' );axis([a b 0 40]);

 Accepted Answer

the cyclist
the cyclist on 12 Aug 2011
The output of ksdensity() is scaled so that the area under the curve is equal to 1, which is conventional for a probability density function. Using your notation, this means that
sum(xi.*diff([0,f]))
will be approximately 1.
If you want your red lines to be about the same height as the curve, you will need to multiply one or the other before plotting. For example, use the following line to use shorter red lines. (I also commented out your manual rescaling.)
plot(f,xi);line(repmat(X,2,1),repmat([0;0.01],1,length(X)),'color','r' );%axis([a b 0 40]);
If you really want to use two different scales, then you can use the plotyy() command.

5 Comments

Susan
Susan on 12 Aug 2011
When I run my code, I only get the red lines and the reason I asked to scale the ksdesnity its because it is not visible so I thought I should scale it but with your code .. I only can still see the red line, the ksdensity is not plotted ???
the cyclist
the cyclist on 13 Aug 2011
Sorry. I forgot that I used some fake data (since I don't know what your data look like). My general point is that you should probably keep the probability density function unscaled, and change the height of your red lines. Maybe you could scale the height of your red lines to be a fixed factor less than the height of the highest part of the density?
repmat([0;0.1*max(xi)],1,length(X))
Susan
Susan on 15 Aug 2011
work = importdata('data.dat');
x = work(:,1);
X = (x).';
a= min(X);
b=max(X);
[xi,f]=ksdensity(X);
repmat([0;0.1*max(xi)],1,length(X))
plot(f,xi);
line(repmat(X,2,1),repmat([0;1],1,length(X)),'color','r' );
axis([a b 0 40]);
I tried to make sure the red lines are shorter than the blue ksdensity but I can still only see the red ones or I can change it around and see the blue one but both of them are not plotted together? I don't understand where I am going wrong??
the cyclist
the cyclist on 15 Aug 2011
I meant that you should use my repmat() command INSTEAD OF YOURS:
work = importdata('data.dat');
x = work(:,1);
X = (x).';
a= min(X);
b=max(X);
[xi,f]=ksdensity(X);
plot(f,xi);
line(repmat(X,2,1),repmat([0;0.1*max(xi)],1,length(X)),'color','r' );
% axis([a b 0 40]);
Susan
Susan on 15 Aug 2011
Yeah It worked... Thank you :)

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!