Module: Enumerable
- Included in:
- Exctl::Commands
- Defined in:
- lib/shorthand.rb
Instance Method Summary collapse
- #amap(m, *a, &b) ⇒ Object
- #amap!(m, *a, &b) ⇒ Object
- #average ⇒ Object (also: #avg, #mean)
- #mean_ad ⇒ Object
- #median ⇒ Object
- #median_ad ⇒ Object
- #median_mad ⇒ Object
- #median_madr(r) ⇒ Object
- #medianr(r) ⇒ Object
- #mid ⇒ Object
- #q20 ⇒ Object
- #q80 ⇒ Object
- #robust_ad ⇒ Object
- #robust_ad2 ⇒ Object
- #robust_avg ⇒ Object
- #robust_avgm ⇒ Object
- #sample_variance ⇒ Object (also: #var)
- #standard_deviation ⇒ Object (also: #stddev)
- #sum ⇒ Object
- #summarize_runs ⇒ Object
Instance Method Details
#amap(m, *a, &b) ⇒ Object
49 |
# File 'lib/shorthand.rb', line 49 def amap(m,*a,&b) self.map {|i|i.send(m,*a,&b)} end |
#amap!(m, *a, &b) ⇒ Object
50 |
# File 'lib/shorthand.rb', line 50 def amap!(m,*a,&b)self.map!{|i|i.send(m,*a,&b)} end |
#average ⇒ Object Also known as: avg, mean
52 |
# File 'lib/shorthand.rb', line 52 def average() self.sum/self.length.to_f end |
#mean_ad ⇒ Object
58 |
# File 'lib/shorthand.rb', line 58 def mean_ad() m = self.average; self.map{|v| (v.to_f - m.to_f).abs}.average end |
#median ⇒ Object
53 |
# File 'lib/shorthand.rb', line 53 def median() self.dup.to_a.sort[self.size/2] end |
#median_ad ⇒ Object
55 |
# File 'lib/shorthand.rb', line 55 def median_ad() m = self.median; self.map{|v| (v.to_f - m.to_f).abs}.average end |
#median_mad ⇒ Object
56 |
# File 'lib/shorthand.rb', line 56 def median_mad() m = self.median; self.map{|v| (v.to_f - m.to_f).abs}.median end |
#median_madr(r) ⇒ Object
57 |
# File 'lib/shorthand.rb', line 57 def median_madr(r) m = self.median; self.map{|v| (v.to_f - m.to_f).abs}.medianr(r) end |
#medianr(r) ⇒ Object
54 |
# File 'lib/shorthand.rb', line 54 def medianr(r) d = self.dup.to_a.sort; d[self.size/2-r..self.size/2+r].average end |
#mid ⇒ Object
59 |
# File 'lib/shorthand.rb', line 59 def mid() srt=self.sort; self.size % 2 == 0 ? (srt[self.size/2] + srt[self.size/2-1]).to_f/2.0 : srt[self.size/2] end |
#q20 ⇒ Object
62 63 64 65 66 67 68 69 |
# File 'lib/shorthand.rb', line 62 def q20() fifth = self.size.to_f / 5.0 return self[0] if fifth < 1.0 i1,i2 = [fifth.floor-1, fifth.ceil-1] srt = self.sort return srt[i1].to_f if i1 == i2 return srt[i1].to_f * (fifth.ceil - fifth) + srt[i2] * (fifth - fifth.floor) end |
#q80 ⇒ Object
70 71 72 73 74 75 76 77 |
# File 'lib/shorthand.rb', line 70 def q80() fifth = self.size.to_f / 5.0 return self.last if fifth < 1.0 i1,i2 = [self.size - fifth.floor, self.size - fifth.ceil] srt = self.sort return srt[i1].to_f if i1 == i2 return srt[i1].to_f * (fifth.ceil - fifth) + srt[i2] * (fifth - fifth.floor) end |
#robust_ad ⇒ Object
60 |
# File 'lib/shorthand.rb', line 60 def robust_ad() r = self.robust_avg; self.map{|v| (v.to_f - r).abs}.average end |
#robust_ad2 ⇒ Object
61 |
# File 'lib/shorthand.rb', line 61 def robust_ad2() r = self.robust_avgm; self.map{|v| (v.to_f - r).abs}.average end |
#robust_avg ⇒ Object
78 |
# File 'lib/shorthand.rb', line 78 def robust_avg() (self.q20 + self.mid + self.q80) / 3.0 end |
#robust_avgm ⇒ Object
79 |
# File 'lib/shorthand.rb', line 79 def robust_avgm() (self.q20 + self.mid + self.mid + self.q80) / 4.0 end |
#sample_variance ⇒ Object Also known as: var
80 81 82 83 84 |
# File 'lib/shorthand.rb', line 80 def sample_variance avg=self.average sum=self.inject(0){|acc,i|acc + (i-avg)**2} sum.to_f/self.length.to_f end |
#standard_deviation ⇒ Object Also known as: stddev
85 |
# File 'lib/shorthand.rb', line 85 def standard_deviation() Math.sqrt(self.sample_variance) end |
#sum ⇒ Object
51 |
# File 'lib/shorthand.rb', line 51 def sum() self.inject(0){|acc,i|acc + i} end |
#summarize_runs ⇒ Object
86 87 88 89 90 91 |
# File 'lib/shorthand.rb', line 86 def summarize_runs return [] if size == 0 self.slice_before([self[0]]){|e,c| e==c[0] ? false : (c[0]=e; true)}.map do |chk| [chk.size, chk[0]] end end |