Class: Array

Inherits:
Object
  • Object
show all
Defined in:
lib/expcalc/array_expansion.rb

Instance Method Summary collapse

Instance Method Details

#get_quantiles(position = 0.5, decreasing_sort = false) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/expcalc/array_expansion.rb', line 19

def get_quantiles(position=0.5, decreasing_sort = false) 
  sorted_array = self.sort
  sorted_array.reverse! if decreasing_sort
  quantile_value = nil

  n_items = self.length
  quantile_coor = (n_items - 1) * position
  f_qcoor = quantile_coor.floor.to_i
  c_qcoor = quantile_coor.ceil.to_i
  if f_qcoor == c_qcoor
    quantile_value = sorted_array[f_qcoor]
  else
    quantile_value = (sorted_array[f_qcoor] + sorted_array[c_qcoor]).fdiv(2)
  end
  return quantile_value
end

#meanObject



3
4
5
# File 'lib/expcalc/array_expansion.rb', line 3

def mean
  return self.inject(0){|sum, n | sum + n}.fdiv(self.length)
end

#standard_deviation(population = false) ⇒ Object



15
16
17
# File 'lib/expcalc/array_expansion.rb', line 15

def standard_deviation(population = false)
  return Math.sqrt(self.variance(population))
end

#variance(population = false) ⇒ Object



7
8
9
10
11
12
13
# File 'lib/expcalc/array_expansion.rb', line 7

def variance(population=false)
  x_mean = self.mean
  size = self.length
  size -= 1 if !population
  variance = self.inject(0){|sum, n | sum + (n - x_mean)**2 }.fdiv(size)
  return variance
end