06 8 / 2014
Basic Stats in a Modular Domain
Part of my job is (well, was) to make sense of lousy sensors.
In this case, I was using a cheap 3-axis accelerometer to do two things: 1) Measure the tilt of the equipment it was mounted in (roll, pitch and net tilt), and 2) get the horizontal component of a cheap 3-axis magnetometer (in other words, determine the magnetic heading).
Getting the magnetic heading was straightforward. I won’t explain the details here, but briefly, you use the accelerometer to find the gravity vector (AKA “down”), then find the projection of the magnetometer vector into the plane normal to the gravity vector. (Google “accelerometer magnetometer heading”.) The roll and pitch can be derived by knowing little more than how the sensor is mounted in the equipment.
Once I had the roll, pitch and heading values, the next question is tougher What is the resolution? What is the accuracy? The precision? The repeatability? These are the questions that must be asked and answered in order to turn a sensor into an instrument (albeit a cheap one).
The most basic first step is to take lots of data from the stationary instrument, then look at the mean (average) and standard deviation of the data set. While this is easy for most kinds of data, it is a different world for angles. For example, what is the average of the following two angles: 2 degrees and 358 degrees?
Let’s see: (2 + 358) / 2 = 180. That can’t be right: It is obvious that 0 (zero) degrees is smack in between 2 and 358: 180 is absolutely the wrong answer. How do we mathematically get the right answer?
Clearly, angles aren’t “normal” numbers. Unlike, say, temperature, angles can’t take on a wide range of values: Angles are limited to the positive range from 0 to 360, where 0 and 360 mean the same thing. (Yes, there are other possible angular values outside this range, but they are all equivalent to taking that value modulo 360. Unless you are talking about cumulative angular displacement or angular paths, which we most certainly are not!)
That’s what angles aren’t. So, what are they?
If I asked you to draw an angle, what would you do? You’d probably make a big “+” on a piece of paper, call the horizontal line “X” and the vertical line “Y”, and the point where they cross the “origin”. Then you’d draw another line from the origin that has the desired angle to the X axis.
What you just drew would be called a “vector”. But vectors have not only an angle, but also a length. What is the length of our angle vector?
Since we are concerned with only the angle, we can call the length 1 (one) for convenience. (It doesn’t really matter: the length is always the same for all angle vectors, and it cancels out when we use it later.)
Now, remember that the average of a bunch of values is just their sum divided by the number of values. What is the average of a bunch of vectors? In this case, we “add” the vectors, then divide by the number of vector times the vector length. (Now you see why 1 is a convenient length: We are dividing by the number of vectors, which is the number of values.)
But how do we add vectors? We simply put them head to tail! When the last vector is moved to the end, you can draw a new vector from the origin to the tip of the last vector added. And guess what: The angle of this big, long vector is PRECISELY the average of all the individual angle vectors!
Well, that was almost too cute to be true, but it is! The average of the angle vectors is just the angle of their sum.
Surely the standard deviation must be MUCH more difficult! If you look up the definition of standard deviation, you get a complicated equation that sums the squares of the differences between each value squared and the mean squared, then takes the square root of that sum. Whew!
Guess what: The standard deviation of a bunch of angles is EASIER!
If you look at the plot of all those angle vectors laid end-to-end, then draw the big vector from the origin to the tip, you will see that the chain of individual vectors will wiggle back and forth across the big vector. The distance between any little vector and the big victor is a “deviation”, and the norm of those tiny deviations is the “standard” deviation.
Nice words, but what do they mean?
Notice this: The length of the big vector will be LESS than the sum of the lengths of the individual angle vectors (assuming you don’t have a perfect sensor, which we most certainly do not). How much shorter the big vector is, is DIRECTLY related to the standard deviation!
I won’t go into the hows and the whys, but here is a link that does: http://en.wikipedia.org/wiki/Directional_statistics and another one that may be easier to grasp: http://ncss.wpengine.netdna-cdn.com/wp-content/themes/ncss/pdf/Procedures/NCSS/Circular_Data_Analysis.pdf
OK, let’s cut to the chase: How many lines of code will it take for me to calculate the average and standard deviation of a bunch of angles?
In Python, it’s just one line for the average, and one line for the standard deviation: http://math.stackexchange.com/questions/887351/average-and-standard-deviation-for-angular-values
Man, whodathunkit?