How to create 3D joint density plot MATLAB? - matlab

I 'm having a problem with creating a joint density function from data. What I have is queue sizes from a stock as two vectors saved as:
X = [askQueueSize bidQueueSize];
I then use the hist3-function to create a 3D histogram. This is what I get:
http://dl.dropbox.com/u/709705/hist-plot.png
What I want is to have the Z-axis normalized so that it goes from [0 1].
How do I do that? Or do someone have a great joint density matlab function on stock?
This is similar (How to draw probability density function in MatLab?) but in 2D.
What I want is 3D with x:ask queue, y:bid queue, z:probability.
Would greatly appreciate if someone could help me with this, because I've hit a wall over here.

I couldn't see a simple way of doing this. You can get the histogram counts back from hist3 using
[N C] = hist3(X);
and the idea would be to normalise them with:
N = N / sum(N(:));
but I can't find a nice way to plot them back to a histogram afterwards (You can use bar3(N), but I think the axes labels will need to be set manually).
The solution I ended up with involves modifying the code of hist3. If you have access to this (edit hist3) then this may work for you, but I'm not really sure what the legal situation is (you need a licence for the statistics toolbox, if you copy hist3 and modify it yourself, this is probably not legal).
Anyway, I found the place where the data is being prepared for a surf plot. There are 3 matrices corresponding to x, y, and z. Just before the contents of the z matrix were calculated (line 256), I inserted:
n = n / sum(n(:));
which normalises the count matrix.
Finally once the histogram is plotted, you can set the axis limits with:
xlim([0, 1]);
if necessary.

With help from a guy at mathworks forum, this is the great solution I ended up with:
(data_x and data_y are values, which you want to calculate at hist3)
x = min_x:step:max_x; % axis x, which you want to see
y = min_y:step:max_y; % axis y, which you want to see
[X,Y] = meshgrid(x,y); *%important for "surf" - makes defined grid*
pdf = hist3([data_x , data_y],{x y}); %standard hist3 (calculated for yours axis)
pdf_normalize = (pdf'./length(data_x)); %normalization means devide it by length of
%data_x (or data_y)
figure()
surf(X,Y,pdf_normalize) % plot distribution
This gave me the joint density plot in 3D. Which can be checked by calculating the integral over the surface with:
integralOverDensityPlot = sum(trapz(pdf_normalize));
When the variable step goes to zero the variable integralOverDensityPlot goes to 1.0
Hope this help someone!

There is a fast way how to do this with hist3 function:
[bins centers] = hist3(X); % X should be matrix with two columns
c_1 = centers{1};
c_2 = centers{2};
pdf = bins / (sum(sum(bins))*(c_1(2)-c_1(1)) * (c_2(2)-c_2(1)));
If you "integrate" this you will get 1.
sum(sum(pdf * (c_1(2)-c_1(1)) * (c_2(2)-c_2(1))))

Related

Draw 3D model of more than one curve in matlab with vectors

I'm doing a project that involves making a 3D model of the cornea in matlab. I have 6 plot3 in the same graph to draw one cornea
but now i want a surface plot.
Don't mind the curve orientation.
Note that all the plot3 have x, y and z that are vectors
Thanks in advance
If I were you I would use the Surf command doku surf. It is used to display [x,y,z] data. Since you have not have as many touples of data (just 6) you will have to interpolate all the other values. Therefore I would use the scattered interpolant function doku scattered interpolant.
!!!!!!!!!!!!!!Take care all this is pseudocode!!!!!!!!!!!!!!!!
F = scatteredInterpolant(x_existing,y_existing,z_existing);
generates a scattered interpolant object. You do already feed your already existing data in there. Afterwards you generate the points at which you want to interpolate:
%generates samples from -4 t0 4 in 0.05 steps
[x_sample,y_sample] = meshgrid(-4:0.05:4,-4:0.05:4);
Now you calculate the fitted z values using the scattered interpolant obj
z_interpolated=F(x_sample,y_sample) %interpolates
surf(x_sample,y_sample,z_interpolated) %plots with surf between -4 and 4
!!!!!!!!!!!!!!!From here working code!!!!!!!!!!!!!!!!!!!!!
%serialiasation of data (special for this usecase)
x_data=[h0(30:632,6);(a30(28:408,3))+0.527;(a60(276:632,3));(a90(26:575,3))+3.417;(a120(188:586,3))-0.6625;(a150(16:380,3))+1.173];
y_data=[(h0(30:632,5));((a30(28:408,2))-0.9128);(a60(276:632,2));(a90(26:575,2));(a120(188:586,2))-0.3825;((a150(16:380,2))+2.032)];
z_data=[yA0;yA30+0.162;yA60;yA90+0.837;yA120+0.135;yA150+0.135];
% cleaning the data of nan values
x_data=x_data(~isnan(z_data));
y_data=y_data(~isnan(z_data));
z_data=z_data(~isnan(z_data));%random for the looks
%interpolating
F=scatteredInterpolant(x_data,y_data,z_data);
%read yourself what this does
F.Method = 'natural';
F.ExtrapolationMethod = 'none';
%choosing sample points
[x_sample,y_sample] = meshgrid(-6:0.05:6,-6:0.05:6);
%interpolation
z_interpolated=F(x_sample,y_sample);
%plot
surf(x_sample,y_sample,z_interpolated)
I hope I was able to help you. If you try it and it works it would be very nice of you to post the working code here so that in the future here stands a working solution.

Find volume of 3d peaks in matlab

right now I have a 3d scatter plot with peaks that I need to find the volumes for. My data is from an image, so the x- and y- values indicate the pixel positions on the xy-plane, and the z value is the pixel value for each pixel.
Here's my scatter plot:
scatter3(x,y,z,20,z,'filled')
I am trying to find the "volume" of the peaks of the data, like drawn below:
I've tried findpeaks() but it gives me many local maxima without the the two prominent peaks that I'm looking for. In addition, I'm really stuck on how to establish the "base" of my peaks, because my data is from a scatter plot. I've also tried the convex hull and a linear surface fit, and get this:
But I'm still stuck on how to use any of these commands to establish an automated peak "base" and volume. Please let me know if you have any ideas or code segments to help me out, because I am stumped and I can't find anything on Stack Overflow. Sorry in advance if this is really unclear! Thank you so much!
Here is a suggestion for dealing with this problem:
Define a threshold for z height, or define in any other way which points from the scatter are relevant (the black plane in the leftmost figure below).
Within the resulted points, find clusters on the X-Y plane, to define the different regions to calculate. You will have to define manually how many clusters you want.
for each cluster, perform a Delaunay triangulation to estimate its volume.
Here is an example code for all that:
[x,y,z] = peaks(30); % some data
subplot 131
scatter3(x(:),y(:),z(:),[],z(:),'filled')
title('The original data')
th = 2.5; % set a threshold for z values
hold on
surf([-3 -3 3 3],[-4 4 -4 4],ones(4)*th,'FaceColor','k',...
'FaceAlpha',0.5)
hold off
ind = z>th; % get an index of all values of interest
X = x(ind);
Y = y(ind);
Z = z(ind);
clustNum = 3; % the number of clusters should be define manually
T = clusterdata([X Y],clustNum);
subplot 132
gscatter(X,Y,T)
title('A look from above')
subplot 133
hold on
c = ['rgb'];
for k = 1:max(T)
valid = T==k;
% claculate a triangulation of the data:
DT = delaunayTriangulation([X(valid) Y(valid) Z(valid)]);
[K,v] = convexHull(DT); % get the convex hull indices
% plot the volume:
ts = trisurf(K,DT.Points(:,1),DT.Points(:,2),DT.Points(:,3),...
'FaceColor',c(k));
text(mean(X(valid)),mean(Y(valid)),max(Z(valid))*1.3,...
num2str(v),'FontSize',12)
end
hold off
view([-45 40])
title('The volumes')
Note: this code uses different functions from several toolboxes. In any case that something does not work, first make sure that you have the relevant toolbox, there are alternatives to most of them.
Having already a mesh, maybe you could use the process described in https://se.mathworks.com/matlabcentral/answers/277512-how-to-find-peaks-in-3d-mesh .
If not, making a linear regression on (x,z) or (y,z) plane could make a base for you to find the peaks.
Out of experience in data with plenty of noise, selecting the peaks manually is often faster if you have small set of data to make the analysis. Just plot every peak with its number from findpeaks() and select the ones that are relevant to you. An interpolation to a smoother data can help to solve the problem in the long term (but creates a problem by itself).
Other option will be searching for peaks in the (x,z) and (y,z) planes, then having the amplitude of each peak in an (x) [or (y)] interval and from there make a integration for every area.

3d plot with ksdensity in matlab

I have a problem in matlab.
I used a ksdensity function on a vector of deltaX, which was my computed X minus actual X.
And I did the same on deltaY.
Then I used plot on that data. This gave me two 2d plots.
As I have two plots showing how (in)accurate was my system in computing X and Y (something like gaussian bell it was). Now I would like to have one plot but in 3d.
The code was just like that:
[f,xi] = ksdensity(deltaX);
figure;
plot(xi,f)
Ok what I'm about to show is probably not the correct way to visualize your problem, mostly because I'm not quite sure I understand what you're up to. But this will show you an example of how to make the Z matrix as discussed in the comments to your question.
Here's the code:
x = wgn(1000,1,5);%create x and y variables, just noise
y = wgn(1000,1,10);
[f,xi] = ksdensity(x);%compute the ksdensity (no idea if this makes real-world sense)
[f2,xi2] = ksdensity(y);
%create the Z matrix by adding together the densities at each x,y pair
%I doubt this makes real-world sense
for z=1:length(xi)
for zz = 1:length(xi2)
Z(z,zz) = f(z)+f2(zz);
end
end
figure(1)
mesh(xi,xi2,Z)
Here's the result:
I leave it up to you to determine the correct way to visualize your density functions in 3D, this is just how you could make the Z matrix. In short, the Z matrix contains the plot elevation at each x,y coordinate. Hope this helps a little.

matlab: cdfplot of relative error

The figure shown above is the plot of cumulative distribution function (cdf) plot for relative error (attached together the code used to generate the plot). The relative error is defined as abs(measured-predicted)/(measured). May I know the possible error/interpretation as the plot is supposed to be a smooth curve.
X = load('measured.txt');
Xhat = load('predicted.txt');
idx = find(X>0);
x = X(idx);
xhat = Xhat(idx);
relativeError = abs(x-xhat)./(x);
cdfplot(relativeError);
The input data file is a 4x4 matrix with zeros on the diagonal and some unmeasured entries (represent with 0). Appreciate for your kind help. Thanks!
The plot should be a discontinuous one because you are using discrete data. You are not plotting an analytic function which has an explicit (or implicit) function that maps, say, x to y. Instead, all you have is at most 16 points that relates x and y.
The CDF only "grows" when new samples are counted; otherwise its value remains steady, just because there isn't any satisfying sample that could increase the "frequency".
You can check the example in Mathworks' `cdfplot1 documentation to understand the concept of "empirical cdf". Again, only when you observe a sample can you increase the cdf.
If you really want to "get" a smooth curve, either 1) add more points so that the discontinuous line looks smoother, or 2) find any statistical model of whatever you are working on, and plot the analytic function instead.

How can I find equation of a plot connecting data points in Matlab?

I have various plots (with hold on) as show in the following figure:
I would like to know how to find equations of these six curves in Matlab. Thanks.
I found interactive fitting tool in Matlab simple and helpful, though somewhat limited in scope:
The graph above seems to be linear interpolation. Given vectors X and Y of data, where X contains the arguments and Y the function points, you could do
f = interp1(X, Y, x)
to get the linearly interpolated value f(x). For example if the data is
X = [0 1 2 3 4 5];
Y = [0 1 4 9 16 25];
then
y = interp1(X, Y, 1.5)
should give you a very rough approximation to 1.5^2. interp1 will match the graph exactly, but you might be interested in fancier curve-fitting operations, like spline approximations etc.
Does rxns stand for reactions? In that case, your curves are most likely exponential. An exponential function has the form: y = a*exp(b * x) . In your case, y is the width of mixing zone, and x is the time in years. Now, all you need to do is run exponential regression in Matlab to find the optimal values of parameters a and b, and you'll have your equations.
The advice, though there might be better answer, from me is: try to see the rate of increase in the curve. For example, cubic is more representative than quadratic if the rate of increase seems fast and find the polynomial and compute the deviation error. For irregular curves, you might try spline fitting. I guess there is also a toolbox in matlab for spline fitting.
There is a way to extract information with the current figure handle (gcf) from you graph.
For example, you can get the series that were plotted in a graph:
% Some figure is created and data are plotted on it
figure;
hold on;
A = [ 1 2 3 4 5 7] % Dummy data
B = A.*A % Some other dummy data
plot(A,B);
plot(A.*3,B-1);
% Those three lines of code will get you series that were plotted on your graph
lh=findall(gcf,'type','line'); % Extract the plotted line from the figure handle
xp=get(lh,'xdata'); % Extract the Xs
yp=get(lh,'ydata'); % Extract the Ys
There must be other informations that you can get from the "findall(gcf,...)" methods.