Class: CorrectHorseBatteryStaple::StatisticalArray
- Inherits:
-
Object
- Object
- CorrectHorseBatteryStaple::StatisticalArray
show all
- Defined in:
- lib/correct_horse_battery_staple/statistical_array.rb
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(array, sorted = false) ⇒ StatisticalArray
Returns a new instance of StatisticalArray.
4
5
6
7
|
# File 'lib/correct_horse_battery_staple/statistical_array.rb', line 4
def initialize(array, sorted=false)
@obj = array
@sorted = sorted
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args, &block) ⇒ Object
29
30
31
|
# File 'lib/correct_horse_battery_staple/statistical_array.rb', line 29
def method_missing(name, *args, &block)
@obj.__send__(name, *args, &block)
end
|
Class Method Details
.cast(array, sorted = false) ⇒ Object
Instance Method Details
#index_range_for_percentile(range) ⇒ Object
61
62
63
64
65
66
67
|
# File 'lib/correct_horse_battery_staple/statistical_array.rb', line 61
def index_range_for_percentile(range)
range = Range.new(range - 0.5, range + 0.5) if range.is_a?(Numeric)
sort!
(percentile_index(range.begin, false).floor ..
percentile_index(range.end, false).ceil)
end
|
#mean ⇒ Object
Also known as:
average
33
34
35
|
# File 'lib/correct_horse_battery_staple/statistical_array.rb', line 33
def mean
inject(0) { |sum, x| sum += x } / size.to_f
end
|
#mean_and_standard_deviation ⇒ Object
52
53
54
|
# File 'lib/correct_horse_battery_staple/statistical_array.rb', line 52
def mean_and_standard_deviation
return m=mean, standard_deviation(m)
end
|
#percentile_index(percentile, round = true) ⇒ Object
56
57
58
59
|
# File 'lib/correct_horse_battery_staple/statistical_array.rb', line 56
def percentile_index(percentile, round=true)
r = percentile.to_f/100 * length + 0.5
round ? r.round : r
end
|
#select_percentile(range) ⇒ Object
69
70
71
|
# File 'lib/correct_horse_battery_staple/statistical_array.rb', line 69
def select_percentile(range)
slice(index_range_for_percentile(range))
end
|
#sort(&block) ⇒ Object
42
43
44
45
|
# File 'lib/correct_horse_battery_staple/statistical_array.rb', line 42
def sort(&block)
return super(&block) if block || !@sorted
self
end
|
#sort! ⇒ Object
17
18
19
20
21
|
# File 'lib/correct_horse_battery_staple/statistical_array.rb', line 17
def sort!
@obj = @obj.sort unless @sorted
@sorted = true
self
end
|
#sort_by!(&block) ⇒ Object
23
24
25
26
27
|
# File 'lib/correct_horse_battery_staple/statistical_array.rb', line 23
def sort_by!(&block)
@obj = @obj.sort_by(&block)
@sorted = true
self
end
|
#standard_deviation(m = mean) ⇒ Object
47
48
49
50
|
# File 'lib/correct_horse_battery_staple/statistical_array.rb', line 47
def standard_deviation(m = mean)
variance = inject(0) { |v, x| v += (x - m) ** 2 }
return Math.sqrt(variance/(size-1))
end
|
#sum ⇒ Object
38
39
40
|
# File 'lib/correct_horse_battery_staple/statistical_array.rb', line 38
def sum
reduce(:+)
end
|