views:

898

answers:

12

I want to calculate the average of a set of angles, which represents source bearing (0 to 360 deg) - (similar to wind-direction)

I know it has been discussed before (several times). The accepted answer was Compute unit vectors from the angles and take the angle of their average.

However this answer defines the average in a non intuitive way. The average of 0, 0 and 90 will be atan( (sin(0)+sin(0)+sin(90)) / (cos(0)+cos(0)+cos(90)) ) = atan(1/2)= 26.56 deg

I would expect the average of 0, 0 and 90 to be 30 degrees.

So I think it is fair to ask the question again: How would you calculate the average, so such examples will give the intuitive expected answer.

Edit:

My practical usage: I had collected measurements for some physical quantity, at a resolution of between 5 and 10 degrees. Now I want to interpolate my measurements for some arbitrary angle. I found out that the interpolation is more accurate when using the 'intuitive' average. Actually I'll do a 1st-order interpolation, but the general case troubles me on the theoretical level.

Edit2:

Let's say that we are measuring wind direction (Only its direction, and not its speed). Moreover, let's assume that the wind direction is constant, and I get different measurements only because of inaccuracy of my equipment (X degrees RMS). Our 3 measurements were 0, 0, and 90 degrees. Since all measurements are equivalently reliable, why shouldn't our best estimate of the wind direction be 30 degrees? setting it to 25.56 degrees is a bias toward 0...

A: 

What is wrong with taking the set of angles as real values and just computing the arithmetic average of those numbers? Then you would get the intuitive (0+0+90)/3 = 30 deg.

Edit: Thanks for useful comments and pointing out that angles may exceed 360. I believe the answer could be the normal arithmetic average reduced "modulo" 360: we sum all the values, divide by the number of angles and then subtract/add a multiple of 360 so that the result lies in the interval [0..360).

Krystian
Then what happens if you want the average of 360 + 10 + 20, clearly the answer should not be 130..
James
The problem is when the sum is greater than 360. For example, the average of 30 and 350 should be 10, not 190). – Lior Kogan 0 secs ago
Lior Kogan
Then just take the sum, modulo 360, before dividing? ((30 + 350) % 360)/2
SimonJ
@SimonJ: ((340+345+350) % 360)/3 = 105. It should be 345 of course.
Lior Kogan
@Lior: Why not take the sum of the moduli instead of the modulus of the sum, e.g. `((340%360) + (345%360) + (350%360))/3 = 345`
ThisSuitIsBlackNot
@ThisSuitIsBlackNot: Fails for { 180, 180 }. (Average by any reasonable definition should be 180, but sum of moduli results in 360.)
Jason
@Jason: You should have looked at my example. You take the *average* of the sum of the moduli, so for {180, 180}: `((180%360) + (180%360))/2 = 180`
ThisSuitIsBlackNot
+3  A: 

This is incorrect on every level.

Vectors add according to the rules of vector addition. The "intuitive, expected" answer might not be that intuitive.

Take the following example. If I have one unit vector (1, 0), with origin at (0,0) that points in the +x-direction and another (-1, 0) that also has its origin at (0,0) that points in the -x-direction, what should the "average" angle be?

If I simply add the angles and divide by two, I can argue that the "average" is either +90 or -90. Which one do you think it should be?

If I add the vectors according to the rules of vector addition (component by component), I get the following:

(1, 0) + (-1, 0) = (0, 0)

In polar coordinates, that's a vector with zero magnitude and angle zero.

So what should the "average" angle be? I've got three different answers here for a simple case.

I think the answer is that vectors don't obey the same intuition that numbers do, because they have both magnitude and direction. Maybe you should describe what problem you're solving a bit better.

Whatever solution you decide on, I'd advise you to base it on vectors. It'll always be correct that way.

duffymo
The average of x, x+180, or of x,x+120,x+240 and so on is a patological case. whether you use vectors or numbers - the answer is not defined.
Lior Kogan
I say the only thing that makes sense is vector arithmetic, not dealing directly with angles at all. Do you agree?
duffymo
@duffymo: It is not a question of correctness. It a question of definition. My question is how to define the circular average function such that my examples will hold.
Lior Kogan
What is definition about besides correctness? Gotta take the mod nature into account. I'd also advise a 0 to 360 or 0 to 2π scale if you must go this way.
duffymo
"pathological"? I don't see why that answer isn't defined. If I take a vector view of things it most certainly is defined. What's your basis for saying this, besides "I said so"? A mathematical citation would be helpful.
duffymo
A: 

You could do this: Say you have a set of angles in an array angle, then to compute the array first do: angle[i] = angle[i] mod 360, now perform a simple average over the array. So when you have 360, 10, 20, you are averaging 0, 10 and 20 - the results are intuitive.

aip.cd.aish
Intutive, but wrong.
peter.murray.rust
+1  A: 
cobbal
+1 I like this. Not sure why it got a downvote.
e.James
I wonder if the result depends on the order of the angles in the list. That seems undesirable.
Jason Orendorff
only on edge cases I think, since the weighting is in place, this is still far from a perfect solution. All 3 of the test cases I have up there work for different orderings.
cobbal
This is interesting. I'll run some tests... Thank you.
Lior Kogan
Sorry, it won't work in the general case. Example: 10,170,190,270,350. The average should be 270, but it is 198.
Lior Kogan
try the new one, it gets 270
cobbal
the problem with the original was that it didn't handle opposite angles well
cobbal
I tried the new one. ((10+170)/2)*(2/5) + ((190+270+350)/3)*(3/5)= 198
Lior Kogan
the python works though, although I just realized case 10, 170, 270 isn't ideal
cobbal
This is crazy, you may as well write special code for every case.
Kugel
+6  A: 

What are you trying to accomplish?

It makes no sense to average degrees. One example for all:

What is the average of 0 and 180? Is it 90? 270? Or somthing else?

I would say just sum up the vectors and calculate the angle from the result.

Kugel
The average of x, x+180, or of x,x+120,x+240 and so on is a patological case. whether you use vectors or numbers - the answer is not defined.
Lior Kogan
if the resulting vector is (0, 0), the angle is undefined of course.
Kugel
Vectors seem the best way to go to me too, also wouldn't (0,0) suggest there are 2 suitable angles, which I agree would be difficult tackle.
Jamie
+2  A: 

In my opinion, this is about angles, not vectors. For that reason the average of 360 and 0 is truly 180. The average of one turn and no turns should be half a turn.

Styggentorsken
(i) circular average of two angles is defined according to the smaller arc between them. (ii) 0=360. Average(0,360)=Average(0,0)=0.
Lior Kogan
A: 

I think the problem stems from how you treat angles greater than 180 (and those greater than 360 as well). If you reduce the angles to a range of +180 to -180 before adding them to the total, you get something more reasonable:

int AverageOfAngles(int angles[], int count)
{
    int total = 0;
    for (int index = 0; index < count; index++)
    {
        int angle = angles[index] % 360;
        if (angle > 180) { angle -= 360; }
        total += angle;
    }

    return (int)((float)total/count);
}
e.James
then you get the problem of `average(-170, 170) = 0`
cobbal
@cobbal: isn't that correct?
e.James
I would want it to be 180 personally
cobbal
+13  A: 

[Note the OP's question (but not title) appears to have changed to a rather specialised question ("...the average of a SEQUENCE of angles where each successive addition does not differ from the running mean by more than a specified amount." ) - see @MaR comment and mine. My following answer addresses the OP's title and the bulk of the discussion and answers related to it.]

This is not a question of logic or intuition, but of definition. This has been discussed on SO before without any real consensus. Angles should be defined within a range (which might be -PI to +PI, or 0 to 2*PI or might be -Inf to +Inf. The answers will be different in each case.

The world "angle" causes confusion as it means different things. The angle of view is an unsigned quantity (and is normally PI > theta > 0. In that cases "normal" averages might be useful. Angle of rotation (e.g. total rotation if an ice skater) might or might not be signed and might include theta > 2*PI and theta < -2*PI.

What is defined here is angle = direction whihch requires vectors. If you use the word "direction" instead of "angle" you will have captured the OP's (apparent original) intention and it will help to move away from scalar quantities.

Wikipedia shows the correct approach when angles are defined circularly such that

theta = theta+2*PI*N = theta-2*PI*N

The answer for the mean is NOT a scalar but a vector. The OP may not feel this is intuitive but it is the only useful correct approach. We cannot redefine the square root of -4 to be -2 because it's more initutive - it has to be +-2*i. Similarly the average of bearings -90 degrees and +90 degrees is a vector of zero length, not 0.0 degrees.

Wikipedia (http://en.wikipedia.org/wiki/Mean%5Fof%5Fcircular%5Fquantities) has a special section and states (The equations are LaTeX and can be seen rendered in Wikipedia):

Most of the usual means fail on circular quantities, like angles, daytimes, fractional parts of real numbers. For those quantities you need a mean of circular quantities.

Since the arithmetic mean is not effective for angles, the following method can be used to obtain both a mean value and measure for the variance of the angles:

Convert all angles to corresponding points on the unit circle, e.g., α to (cosα,sinα). That is convert polar coordinates to Cartesian coordinates. Then compute the arithmetic mean of these points. The resulting point will lie on the unit disk. Convert that point back to polar coordinates. The angle is a reasonable mean of the input angles. The resulting radius will be 1 if all angles are equal. If the angles are uniformly distributed on the circle, then the resulting radius will be 0, and there is no circular mean. In other words, the radius measures the concentration of the angles.

Given the angles \alpha_1,\dots,\alpha_n the mean is computed by

M \alpha = \operatorname{atan2}\left(\frac{1}{n}\cdot\sum_{j=1}^n

\sin\alpha_j, \frac{1}{n}\cdot\sum_{j=1}^n \cos\alpha_j\right)

using the atan2 variant of the arctangent function, or

M \alpha = \arg\left(\frac{1}{n}\cdot\sum_{j=1}^n

\exp(i\cdot\alpha_j)\right)

using complex numbers.

Note that in the OP's question an angle of 0 is purely arbitrary - there is nothing special about wind coming from 0 as opposed to 180 (except in this hemisphere it's colder on the bicycle). Try changing 0,0,90 to 289, 289, 379 and see how the simple arithmetic no longer works.

(There are some distributions where angles of 0 and PI have special significance but they are not in scope here).

Here are some intense previous discussions which mirror the current spread of views :-)

http://mathforum.org/library/drmath/view/53924.html

http://stackoverflow.com/questions/491738/how-do-you-calculate-the-average-of-a-set-of-angles

http://forums.xkcd.com/viewtopic.php?f=17&amp;t=22435

http://www.allegro.cc/forums/thread/595008

peter.murray.rust
the wikipedia method is the one that the OP rejected because it resulted in `average(0, 0, 90) = 26.56`
cobbal
+3  A: 

What does it even mean to average source bearings? Start by answering that question, and you'll get closer to being to define what you mean by the average of angles.

In my mind, an angle with tangent equal to 1/2 is the right answer. If I have a unit force pushing me in the direction of the vector (1, 0), another force pushing me in the direction of the vector (1, 0) and third force pushing me in the direction of the vector (0, 1), then the resulting force (the sum of these forces) is the force pushing me in the direction of (1, 2). These the the vectors representing the bearings 0 degrees, 0 degrees and 90 degrees. The angle represented by the vector (1, 2) has tangent equal to 1/2.

Responding to your second edit:

Let's say that we are measuring wind direction. Our 3 measurements were 0, 0, and 90 degrees. Since all measurements are equivalently reliable, why shouldn't our best estimate of the wind direction be 30 degrees? setting it to 25.56 degrees is a bias toward 0...

Okay, here's an issue. The unit vector with angle 0 doesn't have the same mathematical properties that the real number 0 has. Using the notation 0v to represent the vector with angle 0, note that

0v + 0v = 0v

is false but

0 + 0 = 0

is true for real numbers. So if 0v represents wind with unit speed and angle 0, then 0v + 0v is wind with double unit speed and angle 0. And then if we have a third wind vector (which I'll representing using the notation 90v) which has angle 90 and unit speed, then the wind that results from the sum of these vectors does have a bias because it's traveling at twice unit speed in the horizontal direction but only unit speed in the vertical direction.

Jason
I look at it this way: let's say that my measurement tool have some error, of X degrees RMS. this is why I'm getting different results for each measurement. The fact that the measurement represent physical direction is less important, as far as I can tell
Lior Kogan
Actually, I don't measure speed. Only direction. Also, let's assume the the wind has a constant direction, but the error is only in my measurements.
Lior Kogan
@Lior Kogan: Please see my edit.
Jason
@Lior Kogan: Directions are typically represented by unit-length vectors.
Jason
+4  A: 

Thank you all for helping me see my problem more clearly.

I found what I was looking for. It is called Mitsuta method.

The inputs and output are in the range [0..360).

This method is good for averaging data that was sampled using constant sampling intervals.

The method assumes that the difference between successive samples is less than 180 degrees (which means that if we won't sample fast enough, a 330 degrees change in the sampled signal would be incorrectly detected as a 30 degrees change in the other direction and will insert an error into the calculation). Nyquist–Shannon sampling theorem anybody ?

Here is a c++ code:

double AngAvrg(const vector<double>& Ang)
{
    vector<double>::const_iterator iter= Ang.begin();

    double fD   = *iter;
    double fSigD= *iter;

    while (++iter != Ang.end())
    {
        double fDelta= *iter - fD;

             if (fDelta < -180.) fD+= fDelta + 360.;
        else if (fDelta >  180.) fD+= fDelta - 360.;
        else                     fD+= fDelta       ;

        fSigD+= fD;
    }

    double fAvrg= fSigD / Ang.size();

    if (fAvrg >= 360.) return fAvrg -360.;
    if (fAvrg <  0.  ) return fAvrg +360.;
                       return fAvrg      ;
}

It is explained on page 51 at http://www.epa.gov/scram001/guidance/met/mmgrma.pdf

Thank you MaR for sending the link as a comment.

If the sampled data is constant, but our sampling device has an inaccuracy with a Von Mises distribution, a unit-vectors calculation will be appropriate.

Lior Kogan
A: 

Maybe you could represent angles as quaternions and take average of these quaternions and convert it back to angle.

I don't know If it gives you what you want because quaternions are rather rotations than angles. I also don't know if it will give you anything different from vector solution.

Quaternions in 2D simplify to complex numbers so I guess It's just vectors but maybe some interesting quaternion averaging algorithm like http://ntrs.nasa.gov/archive/nasa/casi.ntrs.nasa.gov/20070017872%5F2007014421.pdf when simplified to 2D will behave better than just vector average.

Kamil Szot
+1  A: 

Here's the answer I gave to this same question:

http://stackoverflow.com/questions/491738/how-do-you-calculate-the-average-of-a-set-of-angles/3651941#3651941

It gives answers inline with what the OP says he wants, but attention should be paid to this:

"I would also like to stress that even though this is a true average of angles, unlike the vector solutions, that does not necessarily mean it is the solution you should be using, the average of the corresponding unit vectors may well be the value you actually should to be using."

Nimble
Your suggested method is excellent for the case were the measurement-device is accurate, and you are measuring independant discrete events.
Lior Kogan