lib: add standard derivation computation to counter
authorAlexandre Terrasa <alexandre@moz-code.org>
Thu, 6 Mar 2014 17:55:44 +0000 (12:55 -0500)
committerAlexandre Terrasa <alexandre@moz-code.org>
Fri, 7 Mar 2014 06:17:17 +0000 (01:17 -0500)
Signed-off-by: Alexandre Terrasa <alexandre@moz-code.org>

lib/counter.nit

index 4c7b6d6..1c2b9ed 100644 (file)
@@ -162,6 +162,16 @@ class Counter[E: Object]
                end
                return sum.to_f / values.length.to_f
        end
+
+       # The standard derivation of the counter values
+       fun std_dev: Float do
+               var avg = self.avg
+               var sum = 0.0
+               for value in map.values do
+                       sum += (value.to_f - avg).pow(2.0)
+               end
+               return (sum / map.length.to_f).sqrt
+       end
 end
 
 private class CounterSorter[E: Object]