Kernel Density for 2D data

I have two series of data(both of type double). I want to generate a kernel density plot from these. Pls help. My coding is given below.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
curs1 = exec(conn, 'select sp_x, sp_y from road_part6_trajectories_oneway2_new_segments_cartesian2');
format long;
curs1 = fetch(curs1);
AA = curs1.Data;
x = [AA{:,1}]';
y = [AA{:,2}]';
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
I want to generate the kernel density or any other suitable density plot for x and y. pls advise.

 Accepted Answer

The function ksdensity() will do kernel density estimation. It's not clear to me what your x and y represent, so it's hard to give more specific advice than that.
In response to comments, here is some code with an example of 2-d kernel density estimation, with a plot of the results. Important note: this assumes that x and y are independent from each other.
% Generate some normally distributed data
x = randn(50,1);
y = randn(50,1);
% Estimate a continuous pdf from the discrete data
[pdfx xi]= ksdensity(x);
[pdfy yi]= ksdensity(y);
% Create 2-d grid of coordinates and function values, suitable for 3-d plotting
[xxi,yyi] = meshgrid(xi,yi);
[pdfxx,pdfyy] = meshgrid(pdfx,pdfy);
% Calculate combined pdf, under assumption of independence
pdfxy = pdfxx.*pdfyy;
% Plot the results
mesh(xxi,yyi,pdfxy)
set(gca,'XLim',[min(xi) max(xi)])
set(gca,'YLim',[min(yi) max(yi)])

9 Comments

I have a table that contains two columns called sp_x and sp_y. The values of sp_x and sp_y corresponds to coordinates of positions in cartesian space (hence both columns contain double precision values). I downloaded these two columns to matlab (pls see the code) and assign the values to x and y (x contains all values in sp_x column and y contains of all values of sp_y column). I want to construct kernel density plot using both x and y. I guess this is a bivariate case as coordinates exist as (x,y) pairs. Please advise
Can you assume (or test?) that x and y are independent and uncorrelated? If they are, then you could use ksdensity() separately for x and for y, then multiply the two resulting functions.
no. they are related. for example, (x1, y1) defines one position(location/point) in cartesian plane , (x2, y2) another position likewise. therefore, they are related. if kernel density is not suitable for this kind of density calculation, do you know any other way to do that? please explain.
I'm not sure what you want to get the density of. Can't you just plot them as an image and look for a pattern that would indicate some correlation between different rows or different x and y? There are a variety of ways of determining uniformity of a 2D array.
yes. exactly that's what I want. These (xi,yi)points are expected to exhibit some patterns. For example, in some locations, the point density is high. I want to see these areas where point density is high.
I tried to use mvnpdf to find the probability density. Please see the following code. But, I get a series of zeros as the result of p.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
curs1 = exec(conn, 'select sp_x, sp_y from road_part6_trajectories_oneway2_new_segments_cartesian2');
format long;
curs1 = fetch(curs1);
r=rows(curs1);
AA = curs1.Data;
x = [AA{:,1}]';
y = [AA{:,2}]';
for i=1:5
xy(i,1)=x(i);
xy(i,2)=y(i);
end;
p=mvnpdf(xy)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
From everything you have written so far, I can no longer tell for sure if you are trying to simply plot what the 2-d frequency distribution looks like, or if you are trying to do kernel density estimation (http://en.wikipedia.org/wiki/Kernel_density_estimation). I'll shortly edit my answer to give an example of the latter.
yes. that's right. I want to plot the 2-d frequency distribution. I have plot the histogram (using hist3) but I want a plot which clearly shows the dense areas. I initially thought that a kernel density would be suitable for this
Then you should start with a two-dimensional histogram. There are several contributions with such tools on the file exchange. To use the cyclist's code you have to show that the variables are independent. A quick look with a 2-D histogram will help you get a look of that.

Sign in to comment.

More Answers (1)

Tunde
Tunde on 20 Oct 2016
Edited: Tunde on 20 Oct 2016
Very helpful. After changing x and y to variables from my data. I have this diagram
Is there any way to change the z-axis to normalized density estimates or probabilities?

Tags

Community Treasure Hunt

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

Start Hunting!