How can I calculate the empircal CDF from an empirical PDF when dimensions are greater than 2?

I would like to calculate the empirical cumulative distribution of a data set with more than 2 dimensions. The only examples I have been able to find are for bivariate data (using hist3 and cumsum). Below is an example 3 dimensional PDF. I would like a robust solution that can accommodate any number of dimensions, if possible. Any help would be very much appreciated. Thank you.
sPDF(1,1,1) = 1;
sPDF(2,1,1) = 2;
sPDF(1,2,1) = 3;
sPDF(2,2,1) = 4;
sPDF(1,1,2) = 5;
sPDF(2,1,2) = 6;
sPDF(1,2,2) = 7;
sPDF(2,2,2) = 8;

 Accepted Answer

See if the mvncdf function and its friends do what you want.

10 Comments

Thank you for your response, but this is not what I am asking for. This calculates the CDF for continuous multivariate normal distributions. I am looking for a solution that calculates the empirical CDF based on a multidimensional PDF (this is sometimes referred to as the Probability Mass Function).
For a bivariate example, the following works great to find the joint cumulative distribution function (the discrete form, which is all I need):
CDF = cumsum(cumsum(PDF,1),2);
Unfortunately, cumsum doesn't support > 2 dimensions. Perhaps I should be asking whether anyone can provide me an N-Dimensional cumsum function?
I would use the cumtrapz function. You will probably have to apply it in as many dimensions as you have in your matrix. (It will produce a result that is one less dimension than its argument.) I doubt there is anything else in core MATLAB that will do what you want. I’ve used cumtrapz to integrate volumes, but not beyond three dimensions.
Thanks again Star, I'll read through that function. Also, after much googling, I happened to come across the open source program FreeMat, and they have a CumSum function that supports multiple dimensions. This probably warrants a new question, but don`t suppose anyone is able to replicate it in Matlab easily? It produces the result I am expecting.
I used it as follows for a 3 dimensional example:
--> CDF = cumsum(sPDF,1);
--> CDF = cumsum(CDF,2);
--> CDF = cumsum(CDF,3);
--> CDF = CDF./36 ...where 36 is the sum of CDF
My pleasure!
The cumsum function can do that. From the cumsum documentation:
  • B = cumsum(A,dim) returns the cumulative sum of the elements along dimension dim . For example, if A is a matrix, then cumsum(A,2) returns the cumulative sum of each row.
From the cumtrapz documentation, it can do that as well:
  • Z = cumtrapz(_,dim) integrates across the dimension of Y specified by scalar dim , using any of the input arguments in the previous syntaxes. The length of X must be the same as size(Y,dim) .
You simply have to ask them!
Wow. Well that is embarrassing. I thought I tried that earlier... Thank you very much! Recursively calling cumsum is the answer. Thank you for your time and help Star.
My pleasure!
I didn’t completely understand what you wanted (my fault) or I’d have suggested it earlier.
What if I want to calculate the empirical cdf from an empirical pdf. Basically the question above but with data containing NaN's. I've tried cumsum but I get NaN's for every value after the first NaN value
You could try interpolating the NaN values first. Something like the following: inpaint This one may also work: inpainting Or you could try fitting a neural network to the data and predict those NaN values.

Sign in to comment.

More Answers (0)

Asked:

on 28 Jul 2014

Commented:

on 23 Feb 2018

Community Treasure Hunt

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

Start Hunting!