How to estimate 95% probability contour in bivariate kernel density function MATLAB

19 views (last 30 days)
Hello:
I have fitted bivariate kernel density function using ksdensity in MATLAB and obtained contour as the method described here: https://stackoverflow.com/questions/44398251/matlab-contour-data-for-2d-normal-cumulative-probability-density
I want to show the area encompassed 95% probability field and related contour for the bivariate density field. I am looking for some thing like this as implemented in R : https://stackoverflow.com/questions/23437000/how-to-plot-a-contour-line-showing-where-95-of-values-fall-within-in-r-and-in
Does MATLAB has such facility to show 95% contours for the joint probability fields?

Answers (1)

Bruno Luong
Bruno Luong on 27 Sep 2023
Edited: Bruno Luong on 27 Sep 2023
I'm not sure if there is a standard definition of area of 95 confidence in 2D pdf since it like asking what if the shape of 95% of the cake, one can cut in many way provide the remaning is 95%.
Assuming one cut on a level of pdf, here is one way
% Invent some fake 2D pdf
pdf=abs(peaks(100));
pdf=pdf-min(pdf,[],'all');
maxp = max(pdf,[],'all');
minp = 0;
smin = 1;
smax = 0;
spdf = sum(pdf,'all');
starget = 0.95;
sold = NaN;
while true
l = (minp+maxp)/2;
i = find(pdf >= l);
s = sum(pdf(i)) / spdf
if s > starget
minp = l;
else
maxp = l;
end
if s==sold || ... % stuck due to quantification of pixels
abs(smax-smin) < 0.001
break
end
sold = s;
end
s = 0.3217
s = 0.6847
s = 0.8667
s = 0.9439
s = 0.9769
s = 0.9607
s = 0.9525
s = 0.9488
s = 0.9504
s = 0.9494
s = 0.9498
s = 0.9501
s = 0.9500
s = 0.9499
s = 0.9500
s = 0.9500
close all
figure;
surf(pdf+10)
hold on
contourf(pdf,[l l])
Note that estimation of the integration
%i = find(pdf >= l);
%s = sum(pdf(i)) / spdf
is very crude. One can do much better but a it cumbersome to implement more precise integration.

Categories

Find more on Contour Plots in Help Center and File Exchange

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!