standard deviation
angles
statistics
angular measurement
mathematical calculations

Calculating Standard Deviation of Angles?

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

Introduction

Angles are circular data, so ordinary standard deviation can be misleading. The problem is that 0 degrees and 360 degrees represent the same direction, but ordinary linear statistics treat them as far apart. To measure spread correctly, you need circular statistics rather than a plain standard deviation formula on the raw degree values.

Why the Linear Formula Fails

Consider two directions: 1 degree and 359 degrees. A normal arithmetic mean would suggest something near 180, which is obviously wrong because the two directions are almost identical.

That same wraparound problem breaks an ordinary standard deviation calculation. Circular data has to be treated as points on the unit circle, not as plain numbers on a line.

Convert Angles to Unit Vectors

The standard approach is to convert each angle into its cosine and sine components, average those components, and then use the mean resultant length to measure concentration.

python
1import math
2
3angles_deg = [350, 10, 15]
4angles_rad = [math.radians(a) for a in angles_deg]
5
6xs = [math.cos(a) for a in angles_rad]
7ys = [math.sin(a) for a in angles_rad]
8
9mean_x = sum(xs) / len(xs)
10mean_y = sum(ys) / len(ys)
11
12print(mean_x, mean_y)

This moves the problem from "numbers with wraparound" to ordinary vector arithmetic.

Compute the Circular Mean

Once you have the mean vector components, compute the mean direction with atan2.

python
1import math
2
3mean_angle = math.atan2(mean_y, mean_x)
4mean_angle_deg = math.degrees(mean_angle) % 360
5print(mean_angle_deg)

Using atan2 matters because it picks the correct quadrant. A plain arctangent can produce the wrong direction.

Circular Standard Deviation Formula

The key quantity is the mean resultant length R:

  • 'R = sqrt(mean_x^2 + mean_y^2)'

If the directions are tightly clustered, R is close to 1. If they are spread uniformly around the circle, R is closer to 0.

A common circular standard deviation is:

  • 's = sqrt(-2 ln R)'

That value is in radians. Convert it to degrees if needed.

python
1import math
2
3R = math.sqrt(mean_x ** 2 + mean_y ** 2)
4std_rad = math.sqrt(-2 * math.log(R))
5std_deg = math.degrees(std_rad)
6
7print(R)
8print(std_deg)

This gives a meaningful measure of angular spread that respects the circular geometry.

Full Runnable Example

python
1import math
2
3
4def circular_std_deg(angles_deg):
5    angles_rad = [math.radians(a) for a in angles_deg]
6    mean_x = sum(math.cos(a) for a in angles_rad) / len(angles_rad)
7    mean_y = sum(math.sin(a) for a in angles_rad) / len(angles_rad)
8    R = math.sqrt(mean_x ** 2 + mean_y ** 2)
9    std_rad = math.sqrt(-2 * math.log(R))
10    return math.degrees(std_rad)
11
12
13sample = [350, 10, 15]
14print(circular_std_deg(sample))

This is the right kind of computation for wind direction, headings, bearings, and other circular measurements.

Interpret the Result Carefully

Circular standard deviation is not identical to linear standard deviation, even though the name is similar. It reflects concentration around a direction, not spread on an infinite line.

Also note that if directions are nearly uniform around the circle, R can become very small and the resulting deviation becomes large. That is expected: the data is not concentrated around any one direction.

When Unwrapping May Be Acceptable

If you know all the angles lie in a narrow interval that does not cross the wraparound boundary, you can sometimes unwrap them first and then use ordinary statistics. For example, 20, 22, and 25 degrees behave linearly enough.

But once the sample may cross 0 and 360, circular statistics are the safer general solution.

Common Pitfalls

The most common mistake is applying ordinary mean and standard deviation directly to raw degree values.

Another mistake is forgetting to convert degrees to radians before using sin, cos, and atan2 in most programming languages.

A third problem is using atan instead of atan2, which can put the mean direction in the wrong quadrant.

Summary

  • Angles are circular data, so ordinary standard deviation is often wrong
  • Convert angles to sine and cosine components on the unit circle
  • Use atan2 to compute the mean direction correctly
  • Use the mean resultant length R to derive a circular standard deviation
  • This approach is appropriate for headings, bearings, wind direction, and other directional measurements

Related reading
Course
Intermediate
27 lessons
15 hours
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 course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

All Rights Reserved.