Clear Filters
Clear Filters

Determining 1, 2 and 3 sigma radii for a bivariate Gaussian histogram

8 views (last 30 days)
Hi all,
I've a 2d scatter plot which I've converted into a 2d histogram using scatter_kde (credit: Nils). I'd like to know whether it's possible to determine the 1, 2 and 3 sigma radii for the 2d histogram? It would also be useful if I could quantify the "roundness" of those circles.
Thanks and compliments of the season.
Tim

Accepted Answer

Star Strider
Star Strider on 18 Dec 2021
If the point positions are in matrix format (or can be interpolated to matrix format using griddata or a similar function), the contour function would likely be appropriate. Set the contours to be whatever values are desired.
Example —
xv = linspace(-30, 30);
yv = linspace(-40, 40);
[Xm,Ym] = ndgrid(xv, yv);
zfcn = @(x,y) exp(-(x/15).^2) + exp(-(y/20).^2);
Zm = zfcn(Xm,Ym) + randn(size(Xm))*0.25;
Xm(Zm < 1) = NaN;
Ym(Zm < 1) = NaN;
Zm(Zm < 1) = NaN;
Zmm = mean(Zm(:),'omitnan');
sgma1 = std(Zm,1,1,'omitnan');
sgma1m = mean(sgma1);
sgma2 = std(Zm,1,2,'omitnan');
sgma2m = mean(sgma2);
NoNaN = ~isnan(Zm);
Xmn = Xm(NoNaN);
Ymn = Ym(NoNaN);
Zmn = Zm(NoNaN);
xvi = linspace(-30, 30, 25);
yvi = linspace(-40, 40, 25);
[Xi,Yi] = ndgrid(xvi, yvi);
Zi = griddata(Xmn(:), Ymn(:), Zmn(:), Xi, Yi);
figure
scatter3(Xm(:), Ym(:), Zm(:), [], Zm(:), 'Marker','.')
xlabel('X')
ylabel('Y')
hold on
surf(Xi, Yi, Zi, 'FaceAlpha',0.75, 'EdgeColor','k')
contour3(Xi, Yi, Zi, Zmm+[0.15 0.30 0.45], '-r', 'LineWidth',5)
hold off
grid on
figure
scatter3(Xm(:), Ym(:), Zm(:), [], Zm(:), 'Marker','.')
xlabel('X')
ylabel('Y')
view(0,90)
hold on
contour3(Xi, Yi, Zi, Zmm+[0.15 0.30 0.45], '-r', 'LineWidth',2.5)
hold off
grid on
This is definitely not a perfect illustration of the data, however it illustrates the approach.
.
  2 Comments
Tim Fulcher
Tim Fulcher on 20 Dec 2021
Thanks Star Strider. I've not finished implementation yet but so far...so good.
Regards and compliments of the season.

Sign in to comment.

More Answers (0)

Categories

Find more on Statistics and Machine Learning Toolbox in Help Center and File Exchange

Products


Release

R2019b

Community Treasure Hunt

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

Start Hunting!