How to estimate 95% probability contour in bivariate kernel density function MATLAB
17 views (last 30 days)
Show older comments
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?
0 Comments
Answers (1)
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
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.
1 Comment
See Also
Categories
Find more on Contour Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!