Class: Array
- Inherits:
-
Object
- Object
- Array
- Defined in:
- lib/statistiks/array/sd.rb,
lib/statistiks/array/sum.rb,
lib/statistiks/array/mean.rb,
lib/statistiks/array/variance.rb,
lib/statistiks/array/percentile.rb
Instance Method Summary collapse
- #mean(round = 5) ⇒ Object
- #median ⇒ Object
- #percentile(p) ⇒ Object
- #quartiles ⇒ Object
- #sd(round = 5) ⇒ Object
- #sum(round = 5) ⇒ Object
- #variance(round = 5) ⇒ Object
Instance Method Details
#mean(round = 5) ⇒ Object
2 3 4 |
# File 'lib/statistiks/array/mean.rb', line 2 def mean(round = 5) (self.sum.to_f/self.length).round(round) end |
#median ⇒ Object
10 11 12 |
# File 'lib/statistiks/array/percentile.rb', line 10 def median self.percentile(50) end |
#percentile(p) ⇒ Object
2 3 4 5 6 7 8 |
# File 'lib/statistiks/array/percentile.rb', line 2 def percentile(p) if p < 0 || p > 100 nil else self.sort[((p.to_f / 100.0) * self.length.to_f) - 0.5] end end |
#quartiles ⇒ Object
14 15 16 |
# File 'lib/statistiks/array/percentile.rb', line 14 def quartiles [self.percentile(25), self.percentile(50), self.percentile(75)] end |
#sd(round = 5) ⇒ Object
2 3 4 |
# File 'lib/statistiks/array/sd.rb', line 2 def sd(round = 5) Math.sqrt(self.variance).round(round) end |
#sum(round = 5) ⇒ Object
2 3 4 |
# File 'lib/statistiks/array/sum.rb', line 2 def sum(round = 5) self.inject(:+).round(round) end |
#variance(round = 5) ⇒ Object
2 3 4 5 |
# File 'lib/statistiks/array/variance.rb', line 2 def variance(round = 5) mean = self.mean ((1.0/(self.length - 1))*self.map{ |x| (x - mean)**2 }.sum).round(round) end |