Class: StatisticalSpread
- Inherits:
-
Struct
- Object
- Struct
- StatisticalSpread
- Defined in:
- lib/flash_math/modules/statistics/statistical_spread.rb
Instance Attribute Summary collapse
-
#values ⇒ Object
Returns the value of attribute values.
Instance Method Summary collapse
- #kurtosis ⇒ Object
- #max ⇒ Object
- #mean ⇒ Object
- #median ⇒ Object
- #min ⇒ Object
- #mode ⇒ Object
- #percentile_from_value(value) ⇒ Object
- #population_standard_deviation ⇒ Object
- #population_variance ⇒ Object
- #range ⇒ Object
- #relative_standard_deviation ⇒ Object
- #skewness ⇒ Object
- #standard_deviation ⇒ Object
- #sum(identity = 0, &block) ⇒ Object
- #value_from_percentile(percentile) ⇒ Object
- #variance ⇒ Object
Instance Attribute Details
#values ⇒ Object
Returns the value of attribute values
1 2 3 |
# File 'lib/flash_math/modules/statistics/statistical_spread.rb', line 1 def values @values end |
Instance Method Details
#kurtosis ⇒ Object
103 104 105 106 107 |
# File 'lib/flash_math/modules/statistics/statistical_spread.rb', line 103 def kurtosis return if values.length == 0 return 0 if values.length == 1 StatisticalSpread.new(values).sum_quarted_deviation / ((values.length - 1) * StatisticalSpread.new(values).quarted_standard_deviation.to_f) end |
#max ⇒ Object
46 47 48 49 |
# File 'lib/flash_math/modules/statistics/statistical_spread.rb', line 46 def max return if values.length < 1 values.sort.last end |
#mean ⇒ Object
11 12 13 14 |
# File 'lib/flash_math/modules/statistics/statistical_spread.rb', line 11 def mean return if values.length < 1 sum / values.length.to_f end |
#median ⇒ Object
16 17 18 19 20 21 22 23 24 25 |
# File 'lib/flash_math/modules/statistics/statistical_spread.rb', line 16 def median return if values.length < 1 sorted = values.sort if values.length % 2 == 0 (sorted[(values.length / 2) -1] + sorted[values.length / 2]) / 2.0 else sorted[values.length / 2] end end |
#min ⇒ Object
51 52 53 54 |
# File 'lib/flash_math/modules/statistics/statistical_spread.rb', line 51 def min return if values.length < 1 values.sort.first end |
#mode ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/flash_math/modules/statistics/statistical_spread.rb', line 27 def mode return if values.length < 1 frequency_distribution = values.inject(Hash.new(0)) { |hash, value| hash[value] += 1; hash } top_2 = frequency_distribution.sort { |a,b| b[1] <=> a[1] } .take(2) if top_2.length == 1 top_2.first.first elsif top_2.first.last == top_2.last.last nil else top_2.first.first end end |
#percentile_from_value(value) ⇒ Object
56 57 58 59 |
# File 'lib/flash_math/modules/statistics/statistical_spread.rb', line 56 def percentile_from_value(value) return if values.length < 1 (values.sort.index(value) / values.length.to_f * 100).ceil end |
#population_standard_deviation ⇒ Object
92 93 94 95 |
# File 'lib/flash_math/modules/statistics/statistical_spread.rb', line 92 def population_standard_deviation return if values.length < 1 Math.sqrt(StatisticalSpread.new(values).population_variance) end |
#population_variance ⇒ Object
74 75 76 77 78 79 |
# File 'lib/flash_math/modules/statistics/statistical_spread.rb', line 74 def population_variance return if values.length < 1 precalculated_mean = StatisticalSpread.new(values).mean sum = values.inject(0) { |accumulator, value| accumulator + (value - precalculated_mean) ** 2 } sum / values.length.to_f end |
#range ⇒ Object
40 41 42 43 44 |
# File 'lib/flash_math/modules/statistics/statistical_spread.rb', line 40 def range return if values.length < 1 sorted = values.sort sorted.last - sorted.first end |
#relative_standard_deviation ⇒ Object
86 87 88 89 90 |
# File 'lib/flash_math/modules/statistics/statistical_spread.rb', line 86 def relative_standard_deviation return if values.length < 1 precalculated_mean = StatisticalSpread.new(values).mean (StatisticalSpread.new(values).population_standard_deviation / precalculated_mean) * 100.0 end |
#skewness ⇒ Object
97 98 99 100 101 |
# File 'lib/flash_math/modules/statistics/statistical_spread.rb', line 97 def skewness return if values.length == 0 return 0 if values.length == 1 StatisticalSpread.new(values).sum_cubed_deviation / ((values.length - 1) * StatisticalSpread.new(values).cubed_standard_deviation.to_f) end |
#standard_deviation ⇒ Object
81 82 83 84 |
# File 'lib/flash_math/modules/statistics/statistical_spread.rb', line 81 def standard_deviation return if values.length < 2 Math.sqrt(StatisticalSpread.new(values).variance) end |
#sum(identity = 0, &block) ⇒ Object
3 4 5 6 7 8 9 |
# File 'lib/flash_math/modules/statistics/statistical_spread.rb', line 3 def sum(identity=0, &block) if block_given? StatisticalSpread.new(values.map(&block)).sum(identity) else values.inject(:+) || identity end end |
#value_from_percentile(percentile) ⇒ Object
61 62 63 64 65 |
# File 'lib/flash_math/modules/statistics/statistical_spread.rb', line 61 def value_from_percentile(percentile) return if values.length < 1 value_index = (percentile.to_f / 100 * values.length).ceil values.sort[value_index] end |
#variance ⇒ Object
67 68 69 70 71 72 |
# File 'lib/flash_math/modules/statistics/statistical_spread.rb', line 67 def variance return if values.length < 1 precalculated_mean = StatisticalSpread.new(values).mean sum = values.inject(0) { |accumulator, value| accumulator + (value - precalculated_mean) ** 2 } sum / (values.length.to_f - 1) end |