wisp
 
(Arne Babenhauserheide)
2017-04-28: add calculation of the unbiased std

add calculation of the unbiased std

diff --git a/examples/unbiased-std.w b/examples/unbiased-std.w
new file mode 100644
--- /dev/null
+++ b/examples/unbiased-std.w
@@ -0,0 +1,26 @@
+define-module : example unbiased-std
+              . #:export : std
+
+define factors⁻¹
+  ' ;; from https://en.wikipedia.org/wiki/Unbiased_estimation_of_standard_deviation#Results_for_the_normal_distribution
+    2  . 0.7978845608
+    3  . 0.8862269255
+    4  . 0.9213177319
+    5  . 0.9399856030
+    6  . 0.9515328619
+    7  . 0.9593687891
+    8  . 0.9650304561
+    9  . 0.9693106998
+    10 . 0.9726592741
+
+define : std . vals
+  let* 
+    : len (length vals)
+      mean (/ (apply + vals) len)
+      factor (or (assoc-ref factors⁻¹ len) 1)
+    * (/ 1 factor) : sqrt : * (/ 1 (- len 1)) : apply + : map (λ(x) (expt (- x mean) 2)) vals .
+
+;; quick test
+let : : res : std 0 0 3
+  when : not : > 0.01 : abs : - res : * 1.129 : sqrt 3 ;; calculated by hand
+         format #t "Bug: (std 0 0 3) gives ~a instead of 1.995\n" res