Skip to content

Commit a4757ad

Browse files
committed
Add AWK script to compute the corrected sample standard deviation
1 parent 80d60d5 commit a4757ad

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

tools/awk/stdev.awk

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#!/usr/bin/env awk -f
2+
#
3+
# Computes the corrected sample standard deviation.
4+
#
5+
# Usage: stdev
6+
#
7+
# Input:
8+
# - a column of numbers
9+
#
10+
# Output:
11+
# - corrected sample standard deviation
12+
#
13+
# Notes:
14+
# - Uses [Welford's method][1].
15+
#
16+
# [1]: https://en.wikipedia.org/wiki/Algorithms_for_calculating_variance#Online_algorithm
17+
18+
BEGIN {
19+
delta = 0
20+
mean = 0
21+
M2 = 0
22+
N = 0
23+
}
24+
{
25+
N += 1
26+
delta = $1 - mean
27+
mean += delta / N
28+
M2 += delta * ($1 - mu)
29+
}
30+
END {
31+
if (N < 2) {
32+
print 0
33+
} else {
34+
print sqrt(M2 / (N-1))
35+
}
36+
}

0 commit comments

Comments
 (0)