Class: Array
Instance Method Summary collapse
-
#ci(opts = { }) ⇒ Object
Generates a confidence interval.
- #geometric_mean ⇒ Object (also: #g_mean)
- #harmonic_mean ⇒ Object (also: #h_mean)
-
#histogram ⇒ Object
Generates a histogram hash for the array.
-
#mean ⇒ Object
(also: #average, #ave)
Finds the mean of the array.
-
#median ⇒ Object
Finds the median of the array.
-
#mode ⇒ Object
Finds the mode of the array.
-
#product ⇒ Object
Finds the product of the array.
-
#random ⇒ Object
(also: #pick)
Picks a random value from the array.
-
#randomize ⇒ Object
Randomizes the array.
-
#randomize! ⇒ Object
Destructively randomizes.
-
#rotate(n = 1) ⇒ Object
Rotates the array left by n elements.
-
#rotate_reverse(n = 1) ⇒ Object
Rotates the array right by n elements.
-
#sample(n = 1) ⇒ Object
Randomly samples n elements.
-
#squares ⇒ Object
Collects the squares of each value in the array.
-
#squares! ⇒ Object
Destructively collects the squares.
-
#standard_deviation ⇒ Object
(also: #stddev)
Standard deviation.
-
#standardize ⇒ Object
Standardizes the array.
-
#standardize! ⇒ Object
Destructive standardize.
-
#sum ⇒ Object
Sums the array.
- #sum_of_squares ⇒ Object
-
#variance ⇒ Object
Variance.
Instance Method Details
#ci(opts = { }) ⇒ Object
Generates a confidence interval
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/darkext/statistics.rb', line 74 def ci(opts = { }) raise ArgumentError.new('Array size must be > 0') if self.size.zero? opts = { :percent => 0.95, :rho => 1, :type => :center }.merge(opts) percent = opts[:percent] rho = opts[:rho] m = self.mean ret = Array.new div = (opts[:type] == :center ? 2 : 1) i = ((Darkext::Statistics::zscore((1 - percent) / div) * rho) / self.size.sqrt).abs case opts[:type] when :center ret << m - i ret << m + i when :upper ret << m + i when :lower ret << m - i end return ret end |
#geometric_mean ⇒ Object Also known as: g_mean
22 23 24 25 |
# File 'lib/darkext/statistics.rb', line 22 def geometric_mean raise ArgumentError.new('Array size must be > 0') if self.size.zero? self.product.root(self.size) end |
#harmonic_mean ⇒ Object Also known as: h_mean
16 17 18 19 |
# File 'lib/darkext/statistics.rb', line 16 def harmonic_mean raise ArgumentError.new('Array size must be > 0') if self.size.zero? self.size.to_f / self.map { |i| 1 / i.to_f }.sum end |
#histogram ⇒ Object
Generates a histogram hash for the array
40 41 42 43 44 45 |
# File 'lib/darkext/statistics.rb', line 40 def histogram self.sort.inject({}) do |a,x| a[x] = a[x].to_i + 1 a end end |
#mean ⇒ Object Also known as: average, ave
Finds the mean of the array
9 10 11 12 |
# File 'lib/darkext/statistics.rb', line 9 def mean raise ArgumentError.new('Array size must be > 0') if self.size.zero? self.sum / self.size.to_f end |
#median ⇒ Object
Finds the median of the array
29 30 31 32 33 34 35 36 37 |
# File 'lib/darkext/statistics.rb', line 29 def median raise ArgumentError.new('Array size must be > 0') if self.size.zero? case self.size % 2 when 0 return self.sort[(self.size / 2) - 1, 2].mean when 1 return self.sort[self.size / 2] end end |
#mode ⇒ Object
Finds the mode of the array
48 49 50 51 52 53 |
# File 'lib/darkext/statistics.rb', line 48 def mode raise ArgumentError.new('Array size must be > 0') if self.size.zero? map = self.histogram max = map.values.max map.keys.select { |x| map[x] == max } end |
#product ⇒ Object
Finds the product of the array
23 24 25 |
# File 'lib/darkext/array.rb', line 23 def product self.inject(&:*) end |
#random ⇒ Object Also known as: pick
Picks a random value from the array
38 39 40 |
# File 'lib/darkext/array.rb', line 38 def random self[rand(self.size)] end |
#randomize ⇒ Object
Randomizes the array
44 45 46 |
# File 'lib/darkext/array.rb', line 44 def randomize self.sort_by { rand } end |
#randomize! ⇒ Object
Destructively randomizes
49 50 51 |
# File 'lib/darkext/array.rb', line 49 def randomize! self.replace(self.randomize) end |
#rotate(n = 1) ⇒ Object
Rotates the array left by n elements
6 7 8 9 |
# File 'lib/darkext/array.rb', line 6 def rotate(n = 1) return if self.size.zero? n.times { self.push(self.shift) } end |
#rotate_reverse(n = 1) ⇒ Object
Rotates the array right by n elements
12 13 14 15 |
# File 'lib/darkext/array.rb', line 12 def rotate_reverse(n = 1) return if self.size.zero? n.times { self.unshift(self.pop) } end |
#sample(n = 1) ⇒ Object
Randomly samples n elements
69 70 71 |
# File 'lib/darkext/statistics.rb', line 69 def sample(n = 1) (1..n).collect { self[rand(self.size)] } end |
#squares ⇒ Object
Collects the squares of each value in the array
28 29 30 |
# File 'lib/darkext/array.rb', line 28 def squares self.map(&:square) end |
#squares! ⇒ Object
Destructively collects the squares
33 34 35 |
# File 'lib/darkext/array.rb', line 33 def squares! self.map!(&:square) end |
#standard_deviation ⇒ Object Also known as: stddev
Standard deviation
62 63 64 65 |
# File 'lib/darkext/statistics.rb', line 62 def standard_deviation raise ArgumentError.new('Array size must be > 0') if self.size.zero? self.variance.abs.sqrt end |
#standardize ⇒ Object
Standardizes the array
97 98 99 |
# File 'lib/darkext/statistics.rb', line 97 def standardize self.clone.standardize! end |
#standardize! ⇒ Object
Destructive standardize
102 103 104 105 106 |
# File 'lib/darkext/statistics.rb', line 102 def standardize! m = self.mean.to_f rho = self.standard_deviation.to_f self.map! { |v| (v.to_f - m) / rho } end |
#sum ⇒ Object
Sums the array
18 19 20 |
# File 'lib/darkext/array.rb', line 18 def sum self.inject(&:+) end |
#sum_of_squares ⇒ Object
108 109 110 111 112 |
# File 'lib/darkext/statistics.rb', line 108 def sum_of_squares raise ArgumentError.new('Array size must be > 0') if self.size.zero? m = self.mean self.map { |v| v - m }.squares.sum end |
#variance ⇒ Object
Variance
56 57 58 59 |
# File 'lib/darkext/statistics.rb', line 56 def variance raise ArgumentError.new('Array size must be > 0') if self.size.zero? self.sum_of_squares.to_f / (self.size).to_f end |