Module: Experiment::Stats::Descriptive
- Included in:
- Experiment::Stats
- Defined in:
- lib/experiment/stats/descriptive.rb
Instance Method Summary collapse
- #mean(ar = self) ⇒ Object
- #median(ar = self) ⇒ Object
- #range(ar = self) ⇒ Object
- #standard_deviation(ar = self) ⇒ Object
- #sum(ar = self, &block) ⇒ Object
- #variance(ar = self) ⇒ Object
- #z_score(ar = self, x) ⇒ Object
- #z_scores(ar = self) ⇒ Object
Instance Method Details
#mean(ar = self) ⇒ Object
30 31 32 |
# File 'lib/experiment/stats/descriptive.rb', line 30 def mean(ar = self) sum(ar) / ar.count end |
#median(ar = self) ⇒ Object
34 35 36 37 38 39 40 41 |
# File 'lib/experiment/stats/descriptive.rb', line 34 def median(ar = self) a = ar.sort if ar.count.odd? a[(ar.count-1)/2] else (a[ar.count/2 - 1] + a[ar.count/2]) / 2.0 end end |
#range(ar = self) ⇒ Object
26 27 28 |
# File 'lib/experiment/stats/descriptive.rb', line 26 def range(ar = self) ar.max - ar.min end |
#standard_deviation(ar = self) ⇒ Object
14 15 16 |
# File 'lib/experiment/stats/descriptive.rb', line 14 def standard_deviation(ar = self) Math.sqrt(variance(ar)) end |
#sum(ar = self, &block) ⇒ Object
5 6 7 |
# File 'lib/experiment/stats/descriptive.rb', line 5 def sum(ar = self, &block) ar.reduce(0.0) {|asum, a| (block_given? ? yield(a) : a) + asum} end |
#variance(ar = self) ⇒ Object
9 10 11 12 |
# File 'lib/experiment/stats/descriptive.rb', line 9 def variance(ar = self) v = sum(ar) {|x| (mean(ar) - x)**2.0 } v/(ar.count - 1.0) end |
#z_score(ar = self, x) ⇒ Object
22 23 24 |
# File 'lib/experiment/stats/descriptive.rb', line 22 def z_score(ar = self, x) (x - mean(ar)) / standard_deviation(ar) end |
#z_scores(ar = self) ⇒ Object
18 19 20 |
# File 'lib/experiment/stats/descriptive.rb', line 18 def z_scores(ar = self) ar.map {|x| z_score(ar, x)} end |