3

Is there any way to compute a covariance matrix out of a confidence/uncertainty/error ellipse? I know how it's done the other way around, using a 2x2 covariance matrix to compute an confidence ellipse (e.g. described here: http://www.visiondummy.com/2014/04/draw-error-ellipse-representing-covariance-matrix/).

Is this even possible or is necessary information missing?

My confidence ellipse is described by the length of both axis and the angle of ellipse rotation.

My approach so far: The axis lengths correspond to the two eigenvalues of the covariance matrix and defining the "spread". An ellipse angle of 0 means, there's no correlation between x & y. Covariance matrix without correlation

I created a new blank 2x2 matrix and assumed the angle is zero, e.g. I used the first eigenvalue and set it to var_xx. the same with the second eigenvalue and var_yy. Now I have a diagonal matrix, which describes the variance, but no rotation (correlation).

Now I used a 2D rotation matrix and the ellipse angle to rotate the previous created matrix.

This approach seems wrong, because the matrix isn't symmetric anymore. Unfortunately a covariance matrix has to be symmetric.

Any ideas?

Ganapathy
  • 545
  • 1
  • 6
  • 23
Thomas
  • 88
  • 2
  • 11

4 Answers4

1

Thanks for raising this issue in public since I needed to do the similar transformation - transform from 2d standard deviation ellipsoid to 2x2 co-variance matrix. There are numerous references for the other way around but the only reference I found is below which brings me to the conclusion that you made a slight mistake, but your derivation brought more clarity. Compare here http://simbad.u-strasbg.fr/Pages/guide/errell.htx

We know that for uncorrelated random values the co-variance matrix is diagonal and has the individual variances in its diagonal element, which are the squared standard deviations (sigma).

 [varX1,   0]    (so your eigen values should be)   eVal1 = longAxis*longAxis;
 [0,   varX2]                                       eVal2 = shortAxis*shortAxis;

Since the transformation from the eigen basis u*u^T / u^T*u creates a new normalized basis, your set of eigen vectors could also be set up as eVec1 = R * [1; 0]; eVec2 = R * [0; 1]; (The length is in the eigen values).

If I did it right multiplying out your code gives varX1 = longAxis * cos(phi)² + shortAxis * sin(phi)² which is missing the squares

Setting up the eigen values right (Var[X] = sigma²) gives the correct results

varX1 = majorAxis² * cos(phi)² + minorAxis² * sin(phi)²
varX2 = majorAxis² * sin(phi)² + minorAxis² * cos(phi)²
cov12 = (majorAxis² - minorAxis²) * sin(phi) * cos(phi)

In accordance to the reference I provided and you can easily see that the uncorrelated case is recovered by setting phi = 0;

Thomas
  • 88
  • 2
  • 11
daku
  • 43
  • 6
  • I think there is an error in your last line. As @k c already mentioned, your ``sin(phi)² * cos(phi)²`` should **not** be squared. The reference you provided (http://simbad.u-strasbg.fr/Pages/guide/errell.htx) says the same. – Thomas Jan 07 '20 at 08:05
1

Daku's answer seems to give nearly the right result, but on the co-variance term there shouldn't be a square on the sin and cosine.

It should be:

varX1 = semiMajorAxis² * cos(phi)² + semiMinorAxis² * sin(phi)²
varX2 = semiMajorAxis² * sin(phi)² + semiMinorAxis² * cos(phi)²
cov12 = (semiMajorAxis² - semiMinorAxis²) * sin(phi) * cos(phi) 
k c
  • 88
  • 8
  • You're right. It should not be squared I guess. Your version fits to the results daku was referring to: http://simbad.u-strasbg.fr/Pages/guide/errell.htx – Thomas Jan 07 '20 at 08:00
  • 2
    Why is it `majorAxis` and not half of it? Shouldn't sigma be half of the width of the ellipse? – mcamurri Jan 07 '20 at 08:58
  • 2
    The submitter didn't state what sort of confidence ellipse was given. The above will be correct for a 2-sigma ellipse. While 2-sigma of a 1D gaussian distribution covers roughly a 95% interval, the same is NOT true for 2D gaussian. Those follow the chi-squared distribution of 2nd degree, so if you have a 95% confidence interval ellipse, you need to divide all of the above by the appropriate chi-square factor of 5.991 to get the correct covariance matrix. Eg, the opposite of what was done in the vision-dummy link in the question. – pavon Apr 02 '20 at 21:40
  • @mcamurri - good point - I have clarified my answer now that the ellipse dimensions are the semi-major and semi-minor axes. – k c Nov 16 '20 at 14:40
0

It seems like I have a correct and working solution. The solution is derived from an answer in another forum: https://math.stackexchange.com/a/1119677

Matlab Example:

% ellipse param
longAxis = 20;
shortAxis = 10;
phi = 0;

% eigenvalues (this may vary from usecase to usecase)
eVal1 = longAxis;
eVal2 = shortAxis;

% compute eigenvectors
R = [cosd(phi), -sind(phi);
     sind(phi), cosd(phi)]; 
eVec1 = R * [eVal1; 0];
eVec2 = R * [0; eVal2];

% compute covariance matrix
% derived from: https://math.stackexchange.com/a/1119677
coVar = eVal1*(eVec1*eVec1')/(eVec1'*eVec1) + eVal2*(eVec2*eVec2')/(eVec2'*eVec2)
Community
  • 1
  • 1
Thomas
  • 88
  • 2
  • 11
0

There is an easy way to approach this for general transformations applied to a covariance.

If I have some linear mapping, A, and some covariance C, I can compute the covariance after transformation by C_new = A * C * A^T.

Solution

So for your problem, you can compute the rotated covariance by computing C = R C R^T

Intuition

This makes sense if you think of C as a being composed of cholesky factors C = L * L^T, where the cholesky factor (or whatever your preferred square-root is) tells us how to deform the unit circle to get the 1-sigma uncertainty ellipse.

When we transform a distribution (what you are trying to do above by transforming the covariance), we want to change the unit circle is deformed. We can do this by transforming L.

So: L_new = A * L. Then C_new = (A * L) * (A * L)^T = (A * L) * (L^T * A^T)

And since we know that C = L * L^T C_new = A * C * A^T.

Jacob Panikulam
  • 1,196
  • 1
  • 9
  • 12