Module: Enumerable

Included in:
Exctl::Commands
Defined in:
lib/shorthand.rb

Instance Method Summary collapse

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

#averageObject Also known as: avg, mean



52
# File 'lib/shorthand.rb', line 52

def average() self.sum/self.length.to_f end

#mean_adObject



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

#medianObject



53
# File 'lib/shorthand.rb', line 53

def median()    self.dup.to_a.sort[self.size/2] end

#median_adObject



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_madObject



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

#midObject



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

#q20Object



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

#q80Object



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_adObject



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_ad2Object



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_avgObject



78
# File 'lib/shorthand.rb', line 78

def robust_avg() (self.q20 + self.mid + self.q80) / 3.0 end

#robust_avgmObject



79
# File 'lib/shorthand.rb', line 79

def robust_avgm() (self.q20 + self.mid + self.mid + self.q80) / 4.0 end

#sample_varianceObject 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_deviationObject Also known as: stddev



85
# File 'lib/shorthand.rb', line 85

def standard_deviation() Math.sqrt(self.sample_variance) end

#sumObject



51
# File 'lib/shorthand.rb', line 51

def sum() self.inject(0){|acc,i|acc + i} end

#summarize_runsObject



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