Properties of ellipses by matrix coefficients – I – Two defining matrices and eigenvalues

This post requires Javascript to display formulas!

For my two current post series on multivariate normal distributions [MNDs] and on shear operations

Multivariate Normal Distributions – II – random vectors and their covariance matrix

Fun with shear operations and SVD

some geometrical and algebraic properties of ellipses are of interest.

Geometrically, we think of an ellipse in terms of their perpendicular two principal axes, focal points, ellipticity and rotation angles with respect to a coordinate system. As elliptic data appear in many contexts of physics, chaos theory, engineering, optics … ellipses are well studied mathematical objects. So, why a post about ellipses in the Machine Learning section of a blog?

In my present working context ellipses appear as a side result of statistical multivariate normal distributions [MNDs]. The projections of multidimensional contour hyper-surfaces of a MND within the ℝn onto coordinate planes of an Cartesian Coordinate System [CCS] result in 2-dimensional ellipses. These ellipses are typically rotated against the axes of the CCS – and their rotation angles reflect data correlations. The general relations of statistical vector data with projections of multidimensional MNDs are somewhat intricate.

Data produced in numerical experiments, e.g. in a Machine Learning context, most often do not give you the geometrical properties of ellipses, which some theory may have predicted, directly. Instead you may get numerical values of statistical vector distributions which correspond to algebraic coefficients. Examples of such coefficients are e.g. correlation coefficients. These coefficients can often be regarded as elements of a matrix. In case of an underlying MND of your statistical variables these matrices indirectly and approximately describe contour surfaces – namely ellipsoids. Or regarding projections of the data on 2-dim coordinate planes “ellipses”.

Ellipsoids/ellipses can in general be defined by matrices operating on position vectors. In particular: Coefficients of quadratic polynomial expressions used to describe ellipses as conic sections correspond to the coefficients of a matrix operating on position vectors.

So, when I became confronted with multidimensional MNDs and their projections on coordinate planes the following questions became interesting:

  • How can one derive the lengths σ1, σ2 of the perpendicular principal axes of an ellipse from data for the coefficients of a matrix which defines the ellipse by a polynomial expression?
  • By which formula do the matrix coefficients provide the inclination angle of the ellipse’s primary axes with the x-axis of a chosen coordinate system?

You may have to dig a bit to find correct and reproducible answers in your math books. Regarding the resulting mathematical expressions I have had some bad experiences with ChatGPT. But as a former physicist I take the above questions as a welcome exercise in solving quadratic equations and doing some linear algebra. So, for those of my readers who are a bit interested in elementary math I want to answer the posed questions step by step and indicate how one can derive the respective formulas. The level is moderate – you need some knowledge in trigonometry and/or linear algebra.

Centered ellipses and two related matrices

Below I regard ellipses whose centers coincide with the origin of a chosen CCS. For our present purpose we thus get rid of some boring linear terms in the equations we have to solve. We do not loose much of general validity by this step: Results of an off-center ellipse follow from applying a simple translation operation to the resulting vector data. But I admit: Your (statistical) data must give you some relatively precis information about the center of your ellipse. We assume that this is the case.

Our ellipses can be rotated with respect to a chosen CCS. I.e., their longer principal axes may be inclined by some angle φ towards the x-axis of our CCS.

There are actually two different ways to define a centered ellipse by a matrix:

  • Alternative 1: We define the (rotated) ellipse by a matrix AE which results from the (matrix) product of two simpler matrices: AE = RφDσ1, σ2.
    Dσ1, σ2 corresponds to a scaling operation applied to position vectors for points located on a centered unit circle. Aφ describes a subsequent rotation. AE summarizes these geometrical operations in a compact form.
  • Alternative 2: We define the (rotated) ellipse by a matrix Aq which combines the x- and y-elements of position vectors in a polynomial equation with quadratic terms in the components (see below). The matrix defines a so called quadratic form. Geometrically interpreted, a quadratic form describes an ellipse as a special case of a conic section. The coefficients of the polynomial and the matrix must, of course, fulfill some particular properties.

While it is relatively simple to derive the matrix elements from known values for σ1, σ2 and φ it is a bit harder to derive the ellipse’s properties from the elements of either of the two defining matrices. I will cover both matrices in this post.

For many practical purposes the derivation of central elliptic properties from given elements of Aq is more relevant and thus of special interest in the following discussion.

Matrix AE of a centered and rotated ellipse: Scaling of a unit circle followed by a rotation

Our starting point is a unit circle C whose center coincides with our CCS’s origin. The components of vectors vc to points on the circle C fulfill the following conditions:

CC:{vvc=(xcyc)=(cos(ψ)sin(ψ)),0ψ2π}

and

x2c+y2c=1.

We define an ellipse E1, σ2) by the application of two linear operations to the vectors of the unit circle:

EEσ1,σ2:{vvE=(xEyE)=RRϕDDEvvc,vcvcCC}.

DE is a diagonal matrix which describes a stretching of the circle along the CCS-axes, and Rφ is an orthogonal rotation matrix. The stretching (or scaling) of the vector-components is done by

DDE=(σ100σ2),
DD1E=(1/σ1001/σ2).

The coefficients σ1, σ2 obviously define the lengths of the principal axes of the yet unrotated ellipse. To be more precise: σ1 is half of the diameter in x-direction, σ1 is half of the diameter in y-direction. I.e. σ1 and σ1 refer to the half-axes of the ellipse.

The subsequent rotation by an angle φ against the x-axis of the CCS is done by

RRϕ=(cos(ϕ)sin(ϕ)sin(ϕ)cos(ϕ))=(u1u2u2u1),
RRTϕ=RR1ϕ=RRϕ.

The combined linear transformation results in a matrix AE with coefficients ((a, b), (c, d)):

AAE=RRϕDDE=(σ1u1σ2u2σ1u2σ2u1)=:(abcd).

These is the first set of matrix coefficients we are interested in.

Note:

AA1E=(1σ1u11σ1u21σ2u21σ2u1),
vvE=(xEyE)=AAE(xcyc),
vvc=(xcyc)=AA1E(xEyE).

We use

u1=cos(ϕ),u2=sin(ϕ),u21+u22=1,(1/λ1):=σ21,(1/λ2):=σ22

and find

a=σ1u1,b=σ2u2,c=σ1u2,d=σ2u1,
det(AAE)=adbc=σ1σ2.

σ1 and σ2 are factors which give us the lengths of the principal axes of the ellipse. σ1 and σ2 have positive values. We, therefore, can safely assume:

det(AAE)=adbc>0

Ok, we have defined an ellipse via an invertible matrix AE, whose coefficients are directly based on geometrical properties.

But as said: Often an ellipse is described by a an equation with quadratic terms in x and y coordinates of data points. The quadratic form has its background in algebraic properties of conic sections. As a next step we derive such a quadratic equation and relate the coefficients of the quadratic polynomial with the elements of our matrix AE. The result will in turn define another very useful matrix Aq.

Quadratic forms – Case 1: Centered ellipse, principal axes aligned with CCS-axes

We start with a simple case. We take a so called axis-parallel ellipse which results from a scaling matrix DE, only. I.e., in this case, the rotation matrix is assumed to be just the identity matrix. We can omit it from further calculations:

RRϕ=(1001)=II,u1=1,u2=0,ϕ=0.

We need an expression in terms of (xE, yE).

To get quadratic terms of vector components it often helps to invoke a scalar product. The scalar product of a vector with itself gives us the squared norm or length of a vector. In our case the norms of the inversely re-scaled vectors obviously have to fulfill:

[DD1E(xEyE)]T[DD1E(xEyE)]=1.

(The bullet represents the scalar product of the vectors.) This directly results in:

1σ21x2E+1σ22y2E=1.

We eliminate the denominator to get a convenient quadratic form:

λ1x2E+λ2y2E=1.

If we were given the quadratic form more generally by coefficients α, β and γ

αx2E+βxEyE+γy2E:=1,

we could directly relate these coefficients with the geometrical properties of our ellipse:

Axis-parallel ellipse:

α=1/a2=1/σ21=λ1,γ=1/d2=1/σ22=λ2,β=b=c=0,ϕ=0.

I.e., we can directly derive σ1, σ2 and φ from the coefficients of the quadratic form. But an axis-parallel ellipse is a very simple ellipse. Things get more difficult for a rotated ellipse.

Quadratic forms – Case 2: General centered and rotated ellipse

We perform the same trick with the vectors vE to get a quadratic polynomial for a rotated ellipse:

[AA1E(xEyE)]T[AA1E(xEyE)]=1.

I skip the lengthy, but simple algebraic calculation. We get (with our matrix elements a, b, c, d):

1σ21σ22[(c2+d2)x2E2(ac+bd)xEyE+(a2+b2)y2E]=1.

The rotation has obviously lead to mixing of components in the polynomial. The coefficient for xE * yE is > 0 for the non-trivial case.

Quadratic form: A matrix equation to define an ellipse

We rewrite our equation again with general coefficients α, β and γ

αx2E+βxEyE+γy2E=1.

These are coefficients which may come from some theory or from averages of numerical data.

The quadratic polynomial can in turn be reformulated as a matrix operation with a symmetric matrix Aq:

vvTEAAqvvE=1,

with

AAq=(αβ/2β/2γ),
AAq=1σ21σ22(c2+d2ac+bdac+bda2+b2).

This means

α=1σ21σ22(c2+d2)=λ2u22+λ1u21,γ=1σ21σ22(a2+b2)=λ2u21+λ1u22,β=21σ21σ22(ac+bd)=2(λ2λ1)u1u2.

With

u1=cosϕ,u2=sinϕ

it follows that

α=λ2sin2ϕ+λ1cos2ϕ,γ=λ2cos2ϕ+λ1sin2ϕ,β=2(λ2λ1)cosϕsinϕ.

These terms are intimately related to the geometrical data; expect them to play a major role in further considerations.

With the help of the coefficients of AE we can also show that det(Aq) > 0:

det(AAq)=(αγ14β2)=1σ21σ22(bcad)2>0.

Thus Aq is an invertible matrix if AE is invertible. For standard conditions (σ1 >0, σ2 > 0) this is the case (see above). Furthermore, Aq is symmetric and thus its own transposed matrix.

Above we have got α, β, γ as some relatively simple functions of a, b, c, d. The inversion is not so trivial and we do not even try it here.

Instead we focus on how we can express σ1, σ2 and φ as functions of either (a, b, c, d) or (α, β, γ).

How to derive σ1, σ2 and φ from the coefficients of AE or Aq in the general case?

Let us assume we have (numerical) data for the coefficients of the quadratic form. Then we may want to calculate values for the length of the principal axes and the rotation angle φ of the corresponding ellipse. There are two ways to derive respective formulas:

  • Approach 1: Use trigonometric relations to directly solve the equation system.
  • Approach 2: Use an eigenvector decomposition of Aq.

Both ways are fun!

Direct derivation of σ1, σ2 and φ from Aq by using trigonometric relations

Trigonometric relations which we can use are:

sin(2ϕ)=2cos(ϕ)sin(ϕ),cos(2ϕ)=2cos2(ϕ)1=12sin2(ϕ)=cos2(ϕ)sin2(ϕ).

Without loosing generality we assume

λ2λ1.

In the end results would only differ by a rotation of π/2, if we had chosen otherwise.

By combining the above relations for the Aq-coefficients we find

α+γ=λ1+λ2,γα=(λ2λ1)cos(2ϕ),β=(λ2λ1)sin(2ϕ).

By squaring and adding the last two equations we further get

λ2+λ1=α+γ,λ2λ1=[β2+(γα)2]1/2.

Eventually we get:

λ1=12[(α+γ)[β2+(γα)2]1/2],λ2=12[(α+γ)+[β2+(γα)2]1/2]..

For λ2 ≥ λ1 the rotation angle is given by

ϕ=12arcsin(β[β2+(γα)2]1/2)

Example:

A typical example for a matrix would be one that appears in the context of a bivariate normal distribution with some correlation imposed onto the statistical variables:

AAq=11ρ2(1ρρ1),

with ρ > 0 being the Pearson correlation coefficient. For this case we get for the half axes of a respective ellipse and the angle :

σ1=(1+ρ),σ2=(1ρ),ϕ=π/4.

Direct derivation of σ1, σ2 and φ from AE by using trigonometric relations

We set

ϵ1=σ21=1λ1,ϵ2=σ22=1λ2.

We have:

a2+b2=ϵ1cos2ϕ+ϵ2sin2ϕ,c2+d2=ϵ1sin2ϕ+ϵ2cos2ϕ,ac+bd=(ϵ1ϵ2)cosϕsinϕ.

This leads to

2(a2+b2)=ϵ1(1+cos(2ϕ))+ϵ2(1cos(2ϕ)),2(c2+d2)=ϵ2(1+cos(2ϕ))+ϵ1(1cos(2ϕ)),2(ac+bd)=(ϵ1ϵ2)sin(2ϕ).

We rearrange terms and get:

ϵ1+ϵ2+(ϵ1ϵ2)cos(2ϕ)=+2(a2+b2),ϵ1ϵ2+(ϵ1ϵ2)cos(2ϕ)=2(c2+d2),(ϵ1ϵ2)sin(2ϕ)=+2(ac+bd).

Let us define some further variables before we add and subtract the first two of the above equations:

r=12(a2+b2+c2+d2),s1=12(a2+b2c2d2),s2=(ac+bd),ss=(s1s2),s=s21+s22.

Then adding two of the equations with the sin2φ and cos2φ above and using the third one results in:

12(ϵ1ϵ2)(cos(2ϕ)sin(2ϕ))=(s1s2)

Taking the vector norm on both sides (with ε1 ≥ ε2) and adding two of the equations above results in:

ϵ1ϵ2=2s,ϵ1+ϵ2=2r.

This gives us:

σ21=ϵ1=r+s,σ22=ϵ2=rs.

In terms of the coefficients a, b, c, d:

σ21=1λ1=12[a2+b2+c2+d2+[4(ac+bd)2+(c2+d2a2b2)2]1/2],σ22=1λ2=12[a2+b2+c2+d2[4(ac+bd)2+(c2+d2a2b2)2]1/2].

Who said that life had to be easy?

But, it is relatively easy to prove:

σ21σ22=1λ21λ22=(adbc )2=[det(AAq)]2.

So, we can indeed confirm :

α=1(adbc)2(c2+d2),γ=1(adbc)2(a2+b2),β=21(adbc)2(ac+bd).

Determination of the inclination angle φ

For the determination of the angle φ we use:

(cos(2ϕ)sin(2ϕ))=1s(s1s2).

We choose

π/2<ϕπ/2

and get:

ϕ=12arctan(s2s1)=12arctan(2(ac+bd)(a2+b2c2d2))

Note: All in all there are four different solutions. The reason is that we alternatively could have requested λ2 ≥ λ1 and also chosen the angle π + φ. So, the ambiguity is due to a selection of the considered principal axis and rotational symmetries.

2nd way to a solution for σ1, σ2 and φ via eigendecomposition

For our second way of deriving formulas for σ1, σ2 and φ we use some linear algebra. This way is interesting for two reasons: It indicates how we can use the Python “linalg”-package together with Numpy to get results numerically. In addition we get familiar with a representation of the ellipse in a properly rotated CCS.

Above we have written down a symmetric matrix Aq describing an operation on the position vectors of points on our rotated ellipse:

vvTEAAqvvE=1.

We know from linear algebra that every symmetric matrix can be decomposed into a product of orthogonal matrices O, OT and a diagonal matrix. This reflects the so called eigendecomposition of a symmetric matrix. It is a unique decomposition in the sense that it has a uniquely defined solution in terms of the coefficients of the following matrices:

AAq=OOΔΔdiagOOT

with

DDdiag=(λu00λd)

The coefficients λu and λd are eigenvalues of both Ddiag and Aq.

Reason: Orthogonal matrices do not change eigenvalues of a transformed matrix. So, the diagonal elements of Ddiag are the eigenvalues of Aq. Linear algebra also tells us that the columns of the matrix O are given by the components of the normalized eigenvectors of Aq.

We can interpret O as a rotation matrix Rψ for some angle ψ:

vvTEAAqvvE=vvTERRψDDdiagRRTψvvE=1.

This means

[RRψvvE]TDDdiag[RRψvvE]=1.

The whole operation tells us a simple truth, which we are already familiar with. By our construction procedure for a rotated ellipse we know that a rotated CCS exists, in which the ellipse can be described as the result of a scaling operation (along the coordinate axes of the rotated CCS) applied to a unit circle. (This CCS is, of course, rotated by an angle φ against our working CCS in which the ellipse appears rotated.)

Above we had found

[AA1E(xEyE)]T[AA1E(xEyE)]=1

With our matrices Rφ and scaling matrix DE we can rewrite this as

[(xEyE)]T[DD1ERRϕ]T[DD1ERRϕ](xEyE)=1.

So:

[(xEyE)]T[RRϕ]T[DD1E]TDD1ERRϕ(xEyE)=1.

Remembering that a diagonal matrix is its own transposed matrix and that the inverse of aan orthogonal matrix (rotation) is its inverse we get:

[(xEyE)]T[RRϕ][DD1EDD1E]RRTϕ(xEyE)=1.

Comparing expressions, we find

DDdiag=[DD1EDD1E].

And thus

λu=λ1=1σ21,λd=λ2=1σ22.

The eigenvalues of our symmetric matrix Aq are just λ1 and λ2.

Mathematically, a lengthy calculation (see below) will indeed reveal that the eigenvalues of a symmetric matrix Aq with coefficients α, 1/2*β and γ have the following form:

λ1/2=λu/d=12[(α+γ)[β2+(γα)2]1/2]

This is, of course, exactly what we have found some minutes ago by directly solving the equations with the trigonometric terms.

We will prove the fact that these indeed are valid eigenvalues in a minute. Let us first look at respective eigenvectors ξ1/2. To get them we must solve the equations resulting from

((αβ/2β/2γ)(λ1/200λ1/2))ξ1/2ξ1/2=00,

with

ξ1ξ1=(ξ1,xξ1,y),ξ2ξ2=(ξ2,xξ2,y)

Below we will show that the following vectors fulfill the conditions (up to a common factor in the components):

λ1:ξ1ξ1=(1β((αγ)[β2+(γα)2]1/2),1)T,λ2:ξ2ξ2=(1β((αγ)+[β2+(γα)2]1/2),1)T.

for the eigenvalues

λ1=12((α+γ)[β2+(γα)2]1/2),λ2=12((α+γ)+[β2+(γα)2]1/2).

As usual, the T at the formulas for the vectors symbolizes a transposition operation.

Note that the vector components given above are not normalized. This is important for performing numerical checks as Numpy and linear algebra programs would typically give you normalized eigenvectors with a length = 1. But you can easily compensate for this by working with

λ1:ξn1ξn1=1ξ1ξ1ξ1ξ1,λ2:ξn2ξn2=1ξ2ξ2ξ2ξ2.

Proof for the eigenvalues and eigenvector components

We just prove that the eigenvector conditions are e.g. fulfilled for the components of the first eigenvector ξ1 and λ1 = λu.

(αλ1)ξ1,x+12βξ1,y=012βξ1,x+(γλ1)ξ2,y=0

(The steps for the second eigenvector are completely analogous).

We start with the condition for the first component:

(α12[(α+γ)[β2+(αγ)2]1/2])1β[(αγ)[β2+(αγ)2]1/2]+β2=0
12[(αγ)+[β2+(αγ)2]1/2]1β[(αγ)[β2+(αγ)2]1/2]+β2=0.
12β[(αγ)2β2(αγ)2]+β2=0

The last relation is obviously true.

You can perform a similar calculation for the other eigenvector component:

12β1β[(αγ)[β2+(αγ)2]1/2]+(γ12[(α+γ)[β2+(αγ)2]1/2])1=0

Thus:

12(αγ)12[β2+(αγ)2]1/212(αγ)+12[β2+(αγ)2]1/2=0

True, again.

In a very similar exercise one can show that the scalar product of the eigenvectors is equal to zero:

1β2[(αγ)2β2+(αγ)2/]+11β2β21=0.

I.e. andindeed

ξ1ξ1ξ1ξ1=(ξ1,x,ξ1,y)(ξ2,xξ2,y)=0.

The eigenvectors are perpendicular to each other. Exactly, what we expect for the orientations of the principal axes of an ellipse against each other.

Rotation angle from coefficients of Aq

We still need a formula for the rotation angle(s). From linear algebra results related to an eigendecomposition we know that the orthogonal (rotation) matrices consist of columns of the normalized eigenvectors. With the components given in terms of our un-rotated CCS, in which we basically work. These vectors point along the principal axes of our ellipse. Thus the components of these eigenvectors define our aspired rotation angles of the ellipse’s principal axes against the x-axis of our CCS.

Let us prove this. By assuming

cos(ϕ1)=ξn1,x,sin(ϕ1)=ξn1,y

and using

sin(2ϕ1)=2sin(ϕ1)cos(ϕ1),

we get:

sin(2ϕ1)=2ξ1,xξ1,y[ξ21,x+ξ21,y].

Thus

sin(2ϕ1)=21β((αγ)[β2+(γα)2]1/2)1[(1β((αγ)[β2+(γα)2]1/2))2+12]=21β(tz)1β2][β2+(tz)2]

with

t=(αγ),z=[β2+(γα)2]1/2.

This looks very differently from the simple expression we got above. And a direct approach is cumbersome. The trick is to multiply nominator and denominator by a convenience factor

(t+z),

and exploit

(tz)(t+z)=t2z2,(tz)(t+z)=β2

to get

2β(tz)(t+z)[β2+(tz)2](t+z)=2ββ2β2(t+z)β2(tz)=β[β2+(αγ)2]1/2

This means that our 2nd approach gives us the result

sin(2ϕ1)=β[β2+(αγ)2]1/2,

which is of course identical to the result we got with our first solution approach. It is clear that the second axis has an inclination by φ +- π / 2:

ϕ2=ϕ1±π/2.

In general the angles have a natural ambiguity of π.

Conclusion

In this post I have shown how one can derive essential properties of centered, but rotated ellipses from matrix-based representations. Such calculations become relevant when e.g. experimental or numerical data only deliver the coefficients of a quadratic form for the ellipse.

We have first established the relation of the coefficients of a matrix that defines an ellipse by a combined scaling and rotation operation with the coefficients of a matrix which defines an ellipse as a quadratic form of the components of position vectors. In addition we have shown how the coefficients of both matrices are related to quantities like the lengths of the principal axes of the ellipse and the inclination of these axes against the x-axis of the Cartesian coordinate system in which the ellipse is described via position vectors. So, if one of the matrices is given we can numerically calculate the ellipse’s main properties.

In the next post of this mini-series

Properties of ellipses by matrix coefficients – II – coordinates of points with extremal y-values

we have a look at the x- and y-coordinates of points on an ellipse with extremal y-values. All in terms of the matrix coefficients we are now familiar with.