Kernel Density for 2D data

124 visualizaciones (últimos 30 días)
Devinya Herath
Devinya Herath el 15 de Oct. de 2011
Comentada: Joseph Byrnes el 19 de Abr. de 2022
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.

Respuesta aceptada

the cyclist
the cyclist el 15 de Oct. de 2011
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 comentarios
Devinya Herath
Devinya Herath el 17 de Oct. de 2011
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
Bjorn Gustavsson
Bjorn Gustavsson el 24 de Oct. de 2011
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.

Iniciar sesión para comentar.

Más respuestas (1)

Tunde
Tunde el 20 de Oct. de 2016
Editada: Tunde el 20 de Oct. de 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?
  1 comentario
Joseph Byrnes
Joseph Byrnes el 19 de Abr. de 2022
normalize the area under the curve to 1

Iniciar sesión para comentar.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by