# Optimal Predictors

This post is about a neat question I was asked many moons ago in an interview. I was quite pleased with myself for getting it right, especially under pressure. I’ve changed some bits of the problem statement in order to protect the innocent, however.

**Problem** Suppose we have a car engine and wish to know its current temperature . We could use a temperature sensor to obtain measurements of the temperature. However, these readings aren’t precise. One minute we might get a value, the next minute a slightly different value. For this reasons, regulation says that we’re supposed to use at least two temperature sensors, from two different vendors, in order to get a more reliable reading. The rules are fuzzy however about how to go about combining sensors. We want to do a good job however and seek to find the best possible way to combine measurements.

For argument’s sake, I’m going to focus on the simplest case, that of two sensors.

A first hurdle is correctly describing the problem in the language of mathematics. We can model the measurement obtained from the first sensor by a random variable and the one from the second sensor by a random variable . I’m also going to make the assumption that the sensors are good, and actually measure the temperature, without any bias, such that and . Finally, I’m going to assume that the random variables are fully characterized by their variance and , which is a measure of the quality of the sensor. This variance is known, and in real life it would be provided by the manufacturer.

We can formulate the problem in a nicer fashion now. Our goal is to find with such that a given quality metric is maximized. Since we’ve declared that a given sensor is of higher quality if its variance is low, we can use the variance of the resulting estimator as the quality metric, thus . The problem then becomes:

We’ve made tremendous progress on modeling the problem. However, the form of is still very fuzzy. How should it look? We could use or , that is, defaulting to either one or the other sensor, or even , but that feels like a cop-out and is basically against regulation.

In principle, can look like anything. You’ll agree with me, however, that an “arbitrary” functions such as looks kind of funky. The most natural approach is perhaps to average the two measurements, like . This is a step in the right direction, however, is does not take the different qualities of the sensors into account. A better approach would be - a linear combination of the two. The goal could then be modified such that we choose and in order to minimize the variance. There is a slight problem however. The alphas can be negative or very big etc. Again, it seems unnatural to subtract a measurement. We should restrict the coefficients to be positive. Better yet, we should force them to be a convex combination, such that their sum is always . Thus the final measurement is a little bit of the measurement of sensor and a little bit of the measurement of sensor . The problem then becomes:

In the first line we used a property of the variance, namely that, , as well as the “almost”-linearity of the variance of independent random variables. The problem is an equality constrained convex optimization problem. A standard tool for solving such problems is that of Lagrange multipliers. The optimization problem can be posed as:

This form can be attacked through regular tools for unconstrained convex optimization problems. The minimum will be the point for which the gradients along , and are zero. If we look at , we have:

Applying the same procedure for , we obtain . Replacing with the previous expression yields:

We thus have the form for in terms of quantities we know ( and ). Since and must add up to , we have the final form for them:

This is definitely a very nice and satisfying result. For non-negative and , the s are unitary values. Since the s are standard deviations, we can naturally expect them to meet this criteria. It’s also interesting how the dependence works. If the variance for one sensor is large relative to the one for the other sensor, then one coefficient will be small while the coefficient of the other sensor will be large. There are some edge-cases which are worth mentioning. If , we have , which is something we expected. If , we have , that is, if one sensor provides perfect information, we don’t bother with the other sensor. As a side-note, the model breaks down when both sensors are perfect, though it at least indicates that the coefficients should be the same.

We’ve started with a practical problem, and through a series of modeling decisions and assumptions we’ve come up with a very nice way to combine the sensors, complete with analytical forms for its parameters. While this is a toy problem, it’s still an interesting application of statistical modeling and convex optimization. It can also be naturally extended to the case of sensors, but that’s a problem for another time.