We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 80d60d5 commit a4757adCopy full SHA for a4757ad
tools/awk/stdev.awk
@@ -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