2
$\begingroup$

Assume I have a multidimensional Gaussian distribution defined by a mean $\mu$ and covariance matrix $\Sigma$. I want to calculate an iso contour / ellipsoid that is aligned with the shape of the PDF.

i.e like this:

enter image description here

Or like the projected region in this image:

enter image description here

In more general terms, is there an easy way to define the isocontour of a Gaussian function for plotting?

$\endgroup$
6
  • $\begingroup$ Do you want to plot in higher dimensions ?? $\endgroup$
    – user65203
    May 14, 2020 at 6:55
  • $\begingroup$ Primary goal is the 2D case, but I would not mind higher dimensions $\endgroup$
    – Makogan
    May 14, 2020 at 6:56
  • $\begingroup$ What kind of representation are you thinking of for 3D, 4D and above ? $\endgroup$
    – user65203
    May 14, 2020 at 6:58
  • $\begingroup$ For the specific use case I am working on I need to just plot a 2D ellipse in the plane on top of the gaussian gradient. But I'd love, if possible, to know how to do a 3D isocontour as well $\endgroup$
    – Makogan
    May 14, 2020 at 6:59
  • $\begingroup$ What kind of representation are you thinking of for 3D isocontours ? $\endgroup$
    – user65203
    May 14, 2020 at 7:00

2 Answers 2

5
$\begingroup$

In $n$ dimensions, the $\text{pdf}$ is an exponential function of

$$(p-\mu)^T\Sigma^{-1}(p-\mu)$$ and the isocontours are the (hyper)ellipses of equation

$$(p-\mu)^T\Sigma^{-1}(p-\mu)=l.$$

By diagonalizing $\Sigma^{-1}$,

$$(p-\mu)^TP\Lambda P^{-1}(p-\mu)=l,$$ where $P$ should be taken orthogonal to correspond to a pure rotation.

In the transformed coordinates,

$$t^T\Lambda t=l,$$

leading to the classical 2D case,

$$\frac{\lambda_u}lu^2+\frac{\lambda_v}lv^2=1.$$

This can be drawn by the parametric equations

$$\begin{cases}u=\dfrac l{\sqrt{\lambda_u}}\cos\theta,\\v=\dfrac l{\sqrt{\lambda_v}}\sin\theta,\end{cases}$$ then reverting to the original coordinates.

For the 3D case, you have the option of using spherical coordinates

$$\begin{cases}u=\dfrac l{\sqrt{\lambda_u}}\cos\theta\sin\phi,\\v=\dfrac l{\sqrt{\lambda_v}}\sin\theta\sin\phi,\\w=\dfrac l{\sqrt{\lambda_w}}\cos\phi\end{cases},$$ giving a system of meridians and parallels.

You also have the option of freezing one coordinate at a time to obtain a triple network of elliptical cross-sections of equation

$$\frac{\lambda_u}lu^2+\frac{\lambda_v}lv^2=1-\frac{\lambda_w}lw^2,$$ i.e.

$$\begin{cases}x'=\dfrac{l\sqrt{1-\dfrac{\lambda_w}lw^2}}{\sqrt{\lambda_u}}\cos\theta,\\y'=\dfrac{l\sqrt{1-\dfrac{\lambda_w}lw^2}}{\sqrt{\lambda_v}}\sin\theta,\\z'=w\end{cases}$$ and similar for the other axis.

For the 3D representation, the points undergo both the diagonalizing and the viewing transformations. The latter is usually made of a rotation, a translation and possibly a perspective projection, and the two rotations and translations can be combined.

Bonus:

For 4D, projecting a wireframe from 4D to 2D will be completely unreadable. You can think of using time as the fourth dimension, and consider constant-time cross-sections, such that the fourth coordinate of $p$ is held constant.

Here things get a little more complicated as the above condition translates to a hyperplane equation in the diagonalized coordinates, and you will have to construct the intersection(s) of a hyperellipsoid and this hyperplane obtained by sweeping over time. This will result in a set of 3D ellipsoids that you can render by the above method. Beware anyway, that the center is not fixed.

The movie will show an ellipsoid inflating from a single point, with its center moving along a line segment (not necessarily a main axis). After meeting the middle point, it will deflate symmetrically and vanish.

In the 4D diagonalized coordinates, the hyperellipsoid will look like $$au^2+bv^2+cw^2+dt^2=1$$ and the constant-time constraint will be write $$pu+qv+rw+st=1.$$

Hence by eliminating a coordinate, say $t$, we get the ellipsoid $$au^2+bv^2+cw^2+d\left(\frac{1-pu-qv-rw}s\right)^2=1.$$

$\endgroup$
6
  • $\begingroup$ The angle for the 2D ellipse should be modified according to the rotation! $\endgroup$ Mar 12, 2021 at 11:33
  • 1
    $\begingroup$ @Euler_Salter: Diagonalized coordinates were implied. I have changed the notation. $\endgroup$
    – user65203
    Mar 12, 2021 at 11:47
  • $\begingroup$ Thank you! I am struggling really hard to plot the ellipse corresponding to a contour of a 2D Gaussian. How would you find the angle of rotation? $\endgroup$ Mar 12, 2021 at 11:48
  • $\begingroup$ @Euler_Salter: by diagonalizing $\Sigma$. $\endgroup$
    – user65203
    Mar 12, 2021 at 12:44
  • 1
    $\begingroup$ @Euler_Salter users.telenet.be/jci/math/reduc.htm $\endgroup$
    – user65203
    Mar 12, 2021 at 12:52
2
$\begingroup$

The axes are the eigenvectors of the covariance matrix; the semi-axes are (proportional to) the reciprocals of the eigenvalues of the covariance matrix.

$\endgroup$
2
  • 1
    $\begingroup$ Another user replied to your post and then deleted it, sorry. $\endgroup$
    – Makogan
    May 14, 2020 at 7:24
  • $\begingroup$ My bad, commented at the wrong place. $\endgroup$
    – user65203
    May 14, 2020 at 7:27

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .