Class: Array

Inherits:
Object show all
Defined in:
lib/darkext/array.rb,
lib/darkext/statistics.rb

Instance Method Summary collapse

Instance Method Details

#ci(opts = { }) ⇒ Object

Generates a confidence interval

Raises:

  • (ArgumentError)


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_meanObject Also known as: g_mean

Raises:

  • (ArgumentError)


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_meanObject Also known as: h_mean

Raises:

  • (ArgumentError)


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

#histogramObject

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

#meanObject Also known as: average, ave

Finds the mean of the array

Raises:

  • (ArgumentError)


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

#medianObject

Finds the median of the array

Raises:

  • (ArgumentError)


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

#modeObject

Finds the mode of the array

Raises:

  • (ArgumentError)


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

#productObject

Finds the product of the array



23
24
25
# File 'lib/darkext/array.rb', line 23

def product
  self.inject(&:*)
end

#randomObject 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

#randomizeObject

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

#squaresObject

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_deviationObject Also known as: stddev

Standard deviation

Raises:

  • (ArgumentError)


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

#standardizeObject

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

#sumObject

Sums the array



18
19
20
# File 'lib/darkext/array.rb', line 18

def sum
  self.inject(&:+)
end

#sum_of_squaresObject

Raises:

  • (ArgumentError)


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

#varianceObject

Variance

Raises:

  • (ArgumentError)


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