Sha256: fdd3f490785f72df936137b1c65bca9c8fd82b52df33454eb5b9956e8fee6e13
Contents?: true
Size: 1.12 KB
Versions: 4
Compression:
Stored size: 1.12 KB
Contents
module Experiment module Stats module Descriptive def sum(ar = self, &block) ar.reduce(0.0) {|asum, a| (block_given? ? yield(a) : a) + asum} end def variance(ar = self) v = sum(ar) {|x| (mean(ar) - x)**2.0 } v/(ar.count - 1.0) end def standard_deviation(ar = self) Math.sqrt(variance(ar)) end def z_scores(ar = self) ar.map {|x| z_score(ar, x)} end def z_score(ar = self, x) (x - mean(ar)) / standard_deviation(ar) end def range(ar = self) ar.max - ar.min end def mean(ar = self) sum(ar) / ar.count end def median(ar = self) a = ar.sort if ar.count.odd? a[(ar.count-1)/2] else (a[ar.count/2 - 1] + a[ar.count/2]) / 2.0 end end end class << self # Monkey pathces the Array class to accept the methods in this class # as it's own - so instead of `Stats::variance([1, 2, 3])` # you can call [1, 2, 3].variance def monkey_patch! Array.send :include, Descriptive end include Descriptive end end end
Version data entries
4 entries across 4 versions & 1 rubygems