How do you calculate the average of a set of circular data?
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
When dealing with circular data, such as angles, directions, or times of day, calculating the average can be notably different from computing the mean of linear data. Circular data wrap around such that the end and start are effectively the same point (e.g., 0 degrees is the same as 360 degrees). This unique characteristic implies that straightforward averaging might not yield meaningful results. For instance, if we have angles of 10 degrees and 350 degrees, their mean isn't 180 degrees, even though numerically (10 + 350)/2 = 180. Instead, the true mean lies around 0 degrees. Here's how you can compute a meaningful average for circular data.
Calculating the Average of Circular Data
Technical Explanation
The key to averaging circular data is to use vector addition. Each measurement is treated as a vector around the unit circle. This approach consists of these straightforward steps:
- Convert Angles to Radians: Circular data are typically in degrees or radians. It's essential to work in radians for trigonometric functions in a programming environment. For conversion, use
Radians = Degrees × π / 180. - Compute Unit Vectors: For each angle, compute the corresponding point on the unit circle using sine and cosine functions:
x_i = cos(θ_i)andy_i = sin(θ_i). - Average the Vectors: Sum all the x-components and y-components separately. Divide each by the number of data points
N:x̄ = (∑_{i=1}^{N} x_i) / Nandȳ = (∑_{i=1}^{N} y_i) / N. - Calculate the Average Angle: The mean direction (or average angle) can then be obtained using the arc tangent function:
θ̄ = atan2(ȳ, x̄). Here,atan2is advantageous because it returns the angle in the correct quadrant, whereas a simple arc tangent might not. - Convert Back to Degrees (optional): For interpretability, convert the result back to degrees:
Degrees = θ̄ × 180 / π.
Example Calculation
Consider a set of angles: 10°, 350°, and 30°.
- Convert Angles to Radians:
- 10°:
10 × π / 180 = 0.1745radians - 350°:
350 × π / 180 = 6.1087radians - 30°:
30 × π / 180 = 0.5236radians
- Compute Unit Vectors:
- For 10°:
(x₁, y₁) = (cos(0.1745), sin(0.1745)) - For 350°:
(x₂, y₂) = (cos(6.1087), sin(6.1087)) - For 30°:
(x₃, y₃) = (cos(0.5236), sin(0.5236))
- Average the Vectors:
x̄ = (x₁ + x₂ + x₃) / 3ȳ = (y₁ + y₂ + y₃) / 3
- Calculate the Average Angle:
θ̄ = atan2(ȳ, x̄)
- Convert Back to Degrees:
- Degrees =
θ̄ × 180 / π
Summary Table
| Step | Description |
| Conversion to Radians | Multiply degrees by π / 180. |
| Compute Unit Vectors | Use cos(θ) and sin(θ). |
| Average the Vectors | Average x-components and y-components separately. |
| Calculate Average Angle | Use atan2 to compute θ̄. |
| Convert to Degrees | Multiply radians by 180 / π. |
Additional Considerations
- Anisotropy: Some circular data might be inherently biased toward certain directions (e.g., wind directions often aren't uniformly distributed). Understanding the distribution of data can be crucial in accurately interpreting the average.
- Circular Variance: Beyond just the average, it's often useful to compute the circular variance, which provides insight into how spread out data points are around the circle. It's defined as
1 - R, whereRis the mean resultant length, calculated asR = sqrt(x̄² + ȳ²). - Applications: Circular statistics find applications across various fields, including meteorology (wind directions), geology (geological strike directions), and chronobiology (time of day).
Understanding the correct methodology for computing averages of circular data is crucial for accurate analysis and decision-making in contexts where such data are prevalent. By using trigonometric methods and careful interpretation, we ensure the results are both mathematically and contextually valid.
Related reading
- How do you decode one-hot labels in Tensorflow?
- How do you decode one-hot labels in Tensorflow?
- How do you edit an existing Tensorboard Training `Loss` summary?
- How do you get the magnitude of a vector in Numpy?
- How do you find a point at a given perpendicular distance from a line?
- How do you round UP a number?
- How do you create custom asynchronous functions in node.js?
- How do you debug React Native?

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.