Class: LEWT::MetaMath
- Inherits:
-
Object
- Object
- LEWT::MetaMath
- Defined in:
- lib/extensions/metastat/metamath.rb
Overview
MetaMath is a base class for developing statistics routines for use with the Metastat LEWT extension It provides some convienience methods for developing new routines, as well as acting as a container for common mathematical proceedures.
Direct Known Subclasses
Instance Method Summary collapse
-
#descriptive_stats(ar) ⇒ Object
- Return the descriptive statistics for an array of values [mean, media, mode] ar
-
Array of values.
-
#mean(ar) ⇒ Object
Takes an array of numeric values and returns there mean.
-
#median(ar) ⇒ Object
- Takes an array of values and returns the median ar
-
Array of values.
-
#mode(ar) ⇒ Object
Takes an array of values and returns there mode.
Instance Method Details
#descriptive_stats(ar) ⇒ Object
Return the descriptive statistics for an array of values [mean, media, mode]
- ar
-
Array of values
41 42 43 44 45 46 47 48 |
# File 'lib/extensions/metastat/metamath.rb', line 41 def descriptive_stats(ar) raise TypeError "Expected an array" if not ar.kind_of?(Array) return { :mean => mean(ar), :median => median(ar), :mode => mode(ar) } end |
#mean(ar) ⇒ Object
Takes an array of numeric values and returns there mean.
- ar
-
Array of values
17 18 19 20 21 |
# File 'lib/extensions/metastat/metamath.rb', line 17 def mean(ar) raise TypeError "Expected an array" if not ar.kind_of?(Array) total = ar.reduce(0) { |sum, x| x + sum } return Float(total)/Float(ar.length) end |
#median(ar) ⇒ Object
Takes an array of values and returns the median
- ar
-
Array of values
33 34 35 36 37 |
# File 'lib/extensions/metastat/metamath.rb', line 33 def median(ar) raise TypeError "Expected an array" if not ar.kind_of?(Array) mid = ar.length / 2 return (mid % 1 != 0) ? mean( [ ar[(mid).floor], ar[(mid).round ]] ) : ar[mid] end |
#mode(ar) ⇒ Object
Takes an array of values and returns there mode.
- ar
-
Array of values
25 26 27 28 29 |
# File 'lib/extensions/metastat/metamath.rb', line 25 def mode(ar) raise TypeError "Expected an array" if not ar.kind_of?(Array) freq = ar.inject(Hash.new(0)) { |h,v| h[v] += 1; h } return ar.max_by { |v| freq[v] } end |