Class: Pwrake::Stat

Inherits:
Object
  • Object
show all
Defined in:
lib/pwrake/report/stat.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ Stat

Returns a new instance of Stat.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/pwrake/report/stat.rb', line 5

def initialize(data)
  @data = data
  @n = data.size
  if @n>0
    @min = data.min
    @max = data.max
    @sum = data.inject(0){|s,x| s+x}
    @mean = @sum/@n
    @median = _median
    @mean_absolute_deviation = data.inject(0){|s,x| (x-@mean).abs} / @n
    if @n>1
      @variance = data.inject(0){|s,x| y=x-@mean; y**2} / (@n-1)
      @sdev = Math.sqrt(@variance)
      @skew = data.inject(0){|s,x| y=(x-@mean)/@sdev; y**3} / @n
      @kurt = data.inject(0){|s,x| y=(x-@mean)/@sdev; y**4} / @n - 3
    end
  end
end

Instance Attribute Details

#binObject (readonly)

Returns the value of attribute bin.



28
29
30
# File 'lib/pwrake/report/stat.rb', line 28

def bin
  @bin
end

#histObject (readonly)

Returns the value of attribute hist.



28
29
30
# File 'lib/pwrake/report/stat.rb', line 28

def hist
  @hist
end

#hist_maxObject (readonly)

Returns the value of attribute hist_max.



28
29
30
# File 'lib/pwrake/report/stat.rb', line 28

def hist_max
  @hist_max
end

#hist_minObject (readonly)

Returns the value of attribute hist_min.



28
29
30
# File 'lib/pwrake/report/stat.rb', line 28

def hist_min
  @hist_min
end

#kurtObject (readonly)

Returns the value of attribute kurt.



27
28
29
# File 'lib/pwrake/report/stat.rb', line 27

def kurt
  @kurt
end

#maxObject (readonly)

Returns the value of attribute max.



25
26
27
# File 'lib/pwrake/report/stat.rb', line 25

def max
  @max
end

#meanObject (readonly)

Returns the value of attribute mean.



25
26
27
# File 'lib/pwrake/report/stat.rb', line 25

def mean
  @mean
end

#mean_absolute_deviationObject (readonly)

Returns the value of attribute mean_absolute_deviation.



26
27
28
# File 'lib/pwrake/report/stat.rb', line 26

def mean_absolute_deviation
  @mean_absolute_deviation
end

#medianObject (readonly)

Returns the value of attribute median.



25
26
27
# File 'lib/pwrake/report/stat.rb', line 25

def median
  @median
end

#minObject (readonly)

Returns the value of attribute min.



25
26
27
# File 'lib/pwrake/report/stat.rb', line 25

def min
  @min
end

#nObject (readonly)

Returns the value of attribute n.



24
25
26
# File 'lib/pwrake/report/stat.rb', line 24

def n
  @n
end

#sdevObject (readonly)

Returns the value of attribute sdev.



27
28
29
# File 'lib/pwrake/report/stat.rb', line 27

def sdev
  @sdev
end

#skewObject (readonly)

Returns the value of attribute skew.



27
28
29
# File 'lib/pwrake/report/stat.rb', line 27

def skew
  @skew
end

#sumObject (readonly)

Returns the value of attribute sum.



25
26
27
# File 'lib/pwrake/report/stat.rb', line 25

def sum
  @sum
end

#varianceObject (readonly)

Returns the value of attribute variance.



27
28
29
# File 'lib/pwrake/report/stat.rb', line 27

def variance
  @variance
end

Class Method Details

.html_thObject



97
98
99
100
# File 'lib/pwrake/report/stat.rb', line 97

def self.html_th
  "<th>%s</th><th>%s</th><th>%s</th><th>%s</th><th>%s</th><th>%s</th>" %
  %w[n mean median min max sdev]
end

Instance Method Details

#_medianObject



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/pwrake/report/stat.rb', line 56

def _median
  if @n==1
    @data[0]
  elsif @n==2
    @mean
  else
    case @n%2
    when 1
      i = (@n-1)/2
      @data.sort[i]
    else
      i = @n/2
      s = @data.sort
      (s[i]+s[i+1])/2
    end
  end
end

#hist_eachObject



44
45
46
47
48
49
50
51
52
53
54
# File 'lib/pwrake/report/stat.rb', line 44

def hist_each
  if @hist
    n = @hist.size
    n.times do |i|
      x1 = 10**(@bin*(i+@i_min))
      x2 = 10**(@bin*(i+1+@i_min))
      y  = @hist[i]
      yield x1,x2,y
    end
  end
end

#html_tdObject



92
93
94
95
# File 'lib/pwrake/report/stat.rb', line 92

def html_td
  "<td>%i</td><td>%g</td><td>%g</td><td>%g</td><td>%g</td><td>%g</td>" %
    [@n, @mean, @median, @min, @max, @sdev||0]
end

#make_logx_histogram(bin) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/pwrake/report/stat.rb', line 30

def make_logx_histogram(bin)
  @bin = bin # 1.0/10
  @i_max = (Math.log10(@max)/@bin).floor
  @i_min = (Math.log10(@min)/@bin).floor
  @hist_min = 10**(@i_min * @bin)
  @hist_max = 10**((@i_max+1) * @bin)
  @hist = Array.new(@i_max-@i_min+1,0)
  @data.each do |x|
    i = (Math.log10(x)/@bin-@i_min).floor
    raise "invalid index i=#{i}" if i<0 || i>@i_max-@i_min
    @hist[i] += 1
  end
end

#reportObject



74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/pwrake/report/stat.rb', line 74

def report
  case @n
  when 0
    "no data"
  when 1
    "n=1 mean=%g"%@mean
  else
    "n=%i mean=%g median=%g min=%g max=%g sdev=%g skew=%g kurt=%g" %
      [@n, @mean, @median, @min, @max, @sdev||0, @skew||0, @kurt||0]
    "n=%i mean=%g median=%g min=%g max=%g sdev=%g" %
      [@n, @mean, @median, @min, @max, @sdev||0]
  end
end

#report2Object



102
103
104
105
106
107
108
109
110
111
112
# File 'lib/pwrake/report/stat.rb', line 102

def report2
  case @n
  when 0
    "  no data"
  when 1
    "  n=1\n  mean=%g"%@mean
  else
    "  n=%i\n  mean=%g\n  median=%g\n  min=%g\n  max=%g\n  sdev=%g\n  skew=%g\n  kurt=%g" %
      [@n, @mean, @median, @min, @max, @sdev||0, @skew||0, @kurt||0]
  end
end

#stat_arrayObject



88
89
90
# File 'lib/pwrake/report/stat.rb', line 88

def stat_array
  [@n, @mean, @median, @min, @max, @sdev||0]
end