Clear Filters
Clear Filters

How to fit multivariate pdf and cdf from data

19 views (last 30 days)
I have a set of simulated data from a Monte Carlo simulation which gives me a bivariate distribution. I can plot the results using histogram2, and I expect the results to be bivariate gaussian. How can I properly fit this 'empirical' data to get a normalized pdf and cdf which I can then integrate over to get some confidence intervals?

Accepted Answer

Jeff Miller
Jeff Miller on 1 Jul 2018
You don't need a bivariate histogram to fit the bivariate normal--just use the sample means and covariance matrix. Here's an example:
% Let's say your data are in an n,2 matrix called xy.
% Here is one randomly generated to use in the example.
muXY = [100, 200];
sigmaXY = [15^2, 5^2; 5^2, 20^2];
xy = mvnrnd(muXY,sigmaXY,10000);
% Here is your bivariate histogram:
figure; histogram2(xy(:,1),xy(:,2));
% Now estimate the parameters of the best-fitting Gaussian:
xybar = mean(xy);
xycovar=cov(xy);
% Plot the best-fitting bivariate pdf:
xsteps = min(xy(:,1)):1:max(xy(:,1)); % Adjust with step sizes appropriate for your
ysteps = min(xy(:,2)):1:max(xy(:,2)); % x and y values.
[X,Y] = meshgrid(xsteps,ysteps);
F = mvnpdf([X(:) Y(:)],xybar,xycovar); % Note that xybar and xycovar are used here.
F = reshape(F,length(ysteps),length(xsteps));
figure; surf(xsteps,ysteps,F);
caxis([min(F(:))-.5*range(F(:)),max(F(:))]);
xlabel('x'); ylabel('y'); zlabel('Probability Density');
  2 Comments
supernoob
supernoob on 2 Jul 2018
Edited: supernoob on 2 Jul 2018
Thanks, this is really helpful. Once I have the pdf I need to integrate in polar coordinates over a chosen area. Once I have that done, this problem is solved!
Tim Fulcher
Tim Fulcher on 24 Dec 2021
Many thanks for this Jeff. Very useful.

Sign in to comment.

More Answers (1)

dpb
dpb on 30 Jun 2018
Edited: dpb on 30 Jun 2018
They're in the BinCounts property of the object or you can just use the old histcounts2.
ADDENDUM
Ah, ok. I've not tried in Matlab, seems a definite lack of no prepared function indeed...
The Answer_108846 implies one can do it with MLE using the supplied functions for pdf/cdf.
Attach your data and I'll try to see if I can give it a go later on...btw, you'll probably get much better fit using the raw data than histogram bin counts.
  1 Comment
supernoob
supernoob on 30 Jun 2018
Thanks. I realized this shortly after posting the question and deleted that part, sorry for the confusion. I still can't figure out how to fit these values to get a bivariate gaussian pdf. gmdistribution is not the correct choice.

Sign in to comment.

Products


Release

R2017b

Community Treasure Hunt

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

Start Hunting!