nic hoffs

Expectation, Covariance, and Optimal Estimators

Expected Value and Variance

X can take a range of values. The distribution of outcomes is dependent on the underlying pdf f(x). It has a mean value, also known as the expected value. The mean and expected value are denoted by μx and E[X], respectively.

We can calculate the expected value by doing a weighted sum of the outcome of a random value and its corresponding probability:

E[X]=\infin\infinxf(x)dx

Now we also want a measure of the spread -- how much the outcomes typically deviate from the expected value.

σx2=\infin\infin(xE[x])2f(x)dx

σ is the standard deviation and σ2 is the variance.

Covariance for Xd

Now what if we have a multivariate random variable? Say, for instance, the position of a robot on a 2D Grid: X=[xy].

The expected value for the vector can be found simply by taking the expected value element-wise:

E[X]=[\infin\infin(xE[x])2f(x)dx\infin\infin(yE[y])2f(y)dy]

Variance is actually trickier. The features are related in some way (that's why they're in a single vector) so taking the element-wise will leave valuable information out. We can categorize the relation between the two features into two groups:

But what do we mean by big or small? Well, the difference between the random variable and its expected value is a good measure! It's small if XE[X]<0 and big if XE[X]>0.

Note that it's not possible to have this configuration:

If this were the case, it'd mean that y is always big! This isn't possible because then the mean would be bigger and there would be more small y values.

The relation between x and y is really what defines the covariance. It tells us a lot about the "shape" of the distribution. But how do we calculate it? Well, we only really care about whether or not the variables change together or not. It doesn't matter if they're both small or both big -- this is one group. We want a positive covariance if they're directly proportional and a negative if they're inversely proportional. Consider the formula for the expected value and it should feel quite intuitive:

Cov(X,Y)=E[(XE[X])(YE[Y])]

If both values are small or big (negative * negative = positive), the value inside the expected value will be positive. If they're different, it will be negative. The expected value gives us a notion of the average result.

A nice property of covariance is that taking the covariance of a random variable with itself is simply the variance.

Cov(X,X)=E[(XE[X])(XE[X])]Cov(X,X)=E[(XE[X])2]=\infin\infin(XE[X])2f(x)dx

So for our multidimensional random variable, the covariance matrix would look like this:

ΣX=[Cov(x,x)Cov(x,y)Cov(y,x)Cov(y,y)]=[Var(x)Cov(x,y)Cov(y,x)Var(y)]

And for a random vector with D dimensions:

ΣX=[Var(X1)Cov(X1,X2)Cov(X1,Xd)Cov(X2,X1)Var(X2)Cov(X2,Xd)Cov(Xd,X1)Cov(Xd,X2)Var(Xd)]

This can actually be calculated using a matrix product as follows:

ΣX=[E[(X1E[X1])(X1E[X1])]E[(X1E[X1])(X2E[X2])]E[(X1E[X1])(XdE[Xd])]E[(X2E[X2])(X1E[X1])]E[(X2E[X2])(X2E[X2])]E[(X2E[X2])(XdE[Xd])]E[(XdE[Xd])(X1E[X1])]E[(XdE[Xd])(X2E[X2])]E[(XdE[Xd])(XdE[Xd])]]Xc=[X1E[X1]X2E[X2]XdE[Xd]]XcT=[X1E[X1]X2E[X2]XdE[Xd]]ΣX=E[XcXcT]

Feel free to verify this product yourself.

Properties of Expectation and Covariance

Linearity

We want to show:

E[aX+bY]=aE[X]+bE[y]

First, note that, using marginalization: \infin\infinf(x,y)dy=f(x). With that, we can proceed with the derivation:

E[aX+bY]=\infin\infin(aX+bY)f(x,y)dxdy=\infin\infin(aX)f(x,y)dxdy+\infin\infin(bY)f(x,y)dxdy=a\infin\infin(X)f(x)dy+b\infin\infin(Y)f(x)dy=aE[X]+bE[y]

Cov(X+Y)=Cov(X)+Cov(X,Y)+Cov(Y,X)+Cov(Y)

Cov(X+Y)=E[(X+YE[X+Y])(X+YE[X+Y])T]=E[(X+YE[X]E[Y])(X+YE[X]E[Y])T]=E[(XE[X])(XE[X])T+(XE[X])(YE[Y])T+(YE[Y])(XE[X])T+(YE[Y])(YE[Y])T]=E[(XE[X])(XE[X])T]+E[(XE[X])(YE[Y])T]+E[(YE[Y])(XE[X])T]+E[(YE[Y])(YE[Y])T]=Var(X)+Cov(X,Y)+Cov(Y,X)+Var(Y)=Var(X)+2Cov(X,Y)+Var(Y)

Cov(AX)

Cov(AX)=E[(AXE[AX])(AXE[AX])T]=E[(AXAE[X])(AXAE[X])T]=E[A(XE[X])(XE[X])TAT]=AE[(XE[X])(XE[X])T]AT=ACov(X)AT

Note that, when I say Cov(X), this is the covariance of each feature of the random variables with respect to the other features. It's the same case as I explored earlier with X=[xy].

Estimation

Imagine Xd is a hidden state which we can't observe. Since it's unobservable, we want an estimator X^. The estimator may be noisy (ideally not), but it definitely shouldn't have bias. An unbiased estimator refers to an estimator which, in the long run, averages out to the value of the hidden state. In other words, despite the noise, the estimator is centered around the hidden states value.

To put it mathematically, we want E[X^]=X.

The error of our estimator is X~=X^X. The expected value of our estimator should be zero, because the bias is 0 and, on the average, the noise is displaced from the mean evenly on either side:

E[X~]=E[X^X]=E[X^]E[X]=XX=0.

The covariance of the estimator is ΣX^.

Combining Estimators

Oftentimes we have two separate estimators of a single hidden state: X^1,X^2. How can we combine them to achieve an estimator which is better than either estimator on its own? Well, if we're looking for an optimal combination, we need some measure of optimality to optimize for. The trace of a matrix is the sum of its diagonals. In a covariance matrix, the diagonals are the variances for each of the features of the random variable. Since we really care more about the spread of the random variable, and not really the covariance between features, this seems like a good measurement for the overall noise level.

With that measure of optimality, we can formalize our task:

X^=f(X^1,X^2) s.t. tr(ΣX^) is minimized and E[X^]=X

So, we assure the estimator is unbiased and then minimze the trace of the covariance.

Consider the example of a linear combination of two 1D Gaussians:

X^=f(X^1,X^2)=k1X^1+k2X^2

We want it to be unbiased:

E[X^]=E[k1X^1+k2X^2]=k1E[X^1]+k2E[X^2]=Xk1X+k2X=X=k1+k2=1k2=1k1

We assume the two estimators are independent so the covariance is 0. For independent RV, the following identity applies: Var(aX+bY)=a2Var(X)2+b2Var(Y)2.

Var(X^)=Var(k1X^1+k2X^2)=k12σ12+k22σ22=k12σ12+(1k1)2σ22=k12σ12+(12k1+k12)σ22=k12σ12+σ222k1σ22+k12σ22

Now we want to minimize the variance w.r.t k1:

k1(k12σ12+σ222k1σ22+k12σ22)=02k1σ122σ22+2k1σ22=0k1σ12σ22+k1σ22=0k1=σ22σ12+σ22

Solving for k2:

k2=1σ22σ12+σ22=σ12+σ22σ12+σ22σ22σ12+σ22=σ12σ12+σ22

Replacing k1,k2 with the critical values we calculated:

X^=σ22σ12+σ22X^1+σ12σ12+σ22X^2

This should intuitively make sense. If \hat X_1 has a lot of noise (σ1 is big), X^2 will contribute more to the estimator.

Combining Estimators for multidimensional RV

Now we have X,X^1,X^2,X^d and K1,K2d×d:

X^=K1X^1+K2X^2

First, we must ensure the estimator is unbiased:

E[X^]=E[K1X^1+K2X^2]=K1E[X^1]+K2E[X^2]=K1X+K2X=XK1+K2=IK2=IK1

This condition will ensure the new estimator is unbiased. Onto the minimization of covariance:

ΣX^=Cov(X^)=Cov(K1X^1+K2X^2)=Cov(K1X^1)+Cov(K2X^2)=K1ΣX^1K1T+K2ΣX^2K2T=K1ΣX^1K1T+(IK1)ΣX^2(IK1)T=K1ΣX^1K1T+K1ΣX^2K1TK1ΣX^2ΣX^2K1T+ΣX^2Tr(ΣX^)=Tr(K1ΣX^1K1T)+Tr(K1ΣX^2K1T)Tr(K1ΣX^2)Tr(ΣX^2K1T)+Tr(ΣX^2)=Tr(K1ΣX^1K1T)+Tr(K1ΣX^2K1T)Tr(K1ΣX^2)Tr(K1ΣX^2)+Tr(ΣX^2)=Tr(K1ΣX^1K1T)+Tr(K1ΣX^2K1T)2Tr(K1ΣX^2)+Tr(ΣX^2)=

Now taking the partial derivative w.r.t K1:

K1(Tr(K1ΣX^1K1T)+Tr(K1ΣX^2K1T)2Tr(K1ΣX^2)+Tr(ΣX^2))=0K1(Tr(K1ΣX^1K1T)+Tr(K1ΣX^2K1T)2Tr(K1ΣX^2))=02K1ΣX^1+2K1ΣX^2+K1(2Tr(K1ΣX^2))=02K1ΣX^1+2K1ΣX^22ΣX^2=0K1ΣX^1+K1ΣX^2ΣX^2=0K1(ΣX^1+ΣX^2)=ΣX^2K1=ΣX^2(ΣX^1+ΣX^2)1

Solving for K2:

K2=IK1=IΣX^2(ΣX^1+ΣX^2)1K2=(ΣX^1+ΣX^2)(ΣX^1+ΣX^2)1ΣX^2(ΣX^1+ΣX^2)1K2=(ΣX^1+ΣX^2ΣX^2)(ΣX^1+ΣX^2)1K2=(ΣX^1)(ΣX^1+ΣX^2)1

Rewriting our original expression for the estimator:

X^=ΣX^2(ΣX^1+ΣX^2)1X^1+(ΣX^1)(ΣX^1+ΣX^2)1X^2

This looks really similar to the 1-D case and still aligns with intuition. If X^1 has high covariance, X^2 will be large and dominate in the new estimator.

This choice of K1 and K2 is actually optimal for all estimators given that the estimators, as the function we're optimizing is convex. I don't know that much about this, so I won't go into great detail. My understanding is that the convexity basically means optimizing the function will definitely give us a global minimum -- there aren't several local minima we could get stuck in.

The method of optimizing with respect to the trace of the covariance is foundational to the derivation for the Kalman Filter, so understanding this prerequisite is critical.