Class: HomeQ::Histogram

Inherits:
Object
  • Object
show all
Defined in:
lib/homeq/base/histogram.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(min, max, width) ⇒ Histogram

Returns a new instance of Histogram.



49
50
51
52
# File 'lib/homeq/base/histogram.rb', line 49

def initialize(min,max,width)
  @min=min; @max=max; @w=width; @f=Array::new(h_class(@max)+1,0)
  @num=0; @s1=0.0; @s2=0.0; @s3=0.0; @s4=0.0;
end

Instance Attribute Details

#fObject (readonly)

array of frequency



33
34
35
# File 'lib/homeq/base/histogram.rb', line 33

def f
  @f
end

#maxObject (readonly)

Returns the value of attribute max.



31
32
33
# File 'lib/homeq/base/histogram.rb', line 31

def max
  @max
end

#minObject (readonly)

Returns the value of attribute min.



30
31
32
# File 'lib/homeq/base/histogram.rb', line 30

def min
  @min
end

#numObject (readonly)

Returns the value of attribute num.



35
36
37
# File 'lib/homeq/base/histogram.rb', line 35

def num
  @num
end

#s1Object (readonly)

Returns the value of attribute s1.



36
37
38
# File 'lib/homeq/base/histogram.rb', line 36

def s1
  @s1
end

#s2Object (readonly)

Returns the value of attribute s2.



37
38
39
# File 'lib/homeq/base/histogram.rb', line 37

def s2
  @s2
end

#s3Object (readonly)

Returns the value of attribute s3.



38
39
40
# File 'lib/homeq/base/histogram.rb', line 38

def s3
  @s3
end

#s4Object (readonly)

Returns the value of attribute s4.



39
40
41
# File 'lib/homeq/base/histogram.rb', line 39

def s4
  @s4
end

#wObject (readonly)

class width



32
33
34
# File 'lib/homeq/base/histogram.rb', line 32

def w
  @w
end

Instance Method Details

#avgObject



69
70
71
# File 'lib/homeq/base/histogram.rb', line 69

def avg
  @s1/@num # average or arithmetic mean
end

#bar(x, unit = 1) ⇒ Object



44
45
46
47
# File 'lib/homeq/base/histogram.rb', line 44

def bar(x,unit=1)
  n = (x.to_f.nan? ? 0 : x.to_f / unit).ceil
  ("----+" * (n/5+1))[0,n]
end

#cvObject



82
83
84
# File 'lib/homeq/base/histogram.rb', line 82

def cv
  sd/avg # coefficient of variation
end

#h_class(x) ⇒ Object



41
42
43
# File 'lib/homeq/base/histogram.rb', line 41

def h_class(x)
  ((x-@min)/@w).truncate
end

#kurtosisObject



91
92
93
94
# File 'lib/homeq/base/histogram.rb', line 91

def kurtosis
  # kurtosis E((x-m)^4)/s^4=(E(x^4)-4mE(x^3)+6m^2E(x^2)-3m^4)/s^4
  m=avg; v=variance; ((@s4-4*m*@s3+6*m*m*@s2)/@num-3*m**4)/(v*v)
end

#push(x) ⇒ Object Also known as: <<



54
55
56
57
# File 'lib/homeq/base/histogram.rb', line 54

def push(x)
  @num+=1; @s1+=x; @s2+=(x**2); @s3+=(x**3); @s4+=(x**4); 
  @f[h_class([[@min,x].max, @max].min)]+=1
end

#report(unit = 1) ⇒ Object



110
111
112
113
114
# File 'lib/homeq/base/histogram.rb', line 110

def report(unit=1)
  str = to_s(unit)
  str <<
    "number: %d, average: %4.2f, std dev: %4.2f\n" % [size, avg, sd]
end

#sdObject



77
78
79
80
# File 'lib/homeq/base/histogram.rb', line 77

def sd
  return 0 if variance.nan?
  Math::sqrt(variance) # standard deviation.
end

#sizeObject

statistics



61
62
63
# File 'lib/homeq/base/histogram.rb', line 61

def size
  @num # s=0; @f.each{|n| s+=n}; return s
end

#skewnessObject



86
87
88
89
# File 'lib/homeq/base/histogram.rb', line 86

def skewness
  # skewness E((x-m)^3)/s^3=(E(x^3)-3*m*E(x^2) + 2 m ^3)/s^3
  m=avg; s=sd; ((@s3-3*m*@s2)/@num+2*m*m*m)/(s*s*s)
end

#sumObject



65
66
67
# File 'lib/homeq/base/histogram.rb', line 65

def sum
  @s1
end

#to_s(unit = 1) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/homeq/base/histogram.rb', line 96

def to_s(unit=1)
  form=sprintf("%%%d.1f-.:%%s %%2.1f%%%% %%d\n", @max.to_s.size + 2)
  s = ''
  for i in 0..h_class(@max)
    percentage = @f[i].to_f / size * 100.0
    s << sprintf(form,
                 @min+i*@w,
                 bar(percentage / 1.8, unit),
                 percentage.nan? ? 0 : percentage,
                 @f[i])
  end
  s
end

#varianceObject



73
74
75
# File 'lib/homeq/base/histogram.rb', line 73

def variance
  (@s2/@num)-(@s1/@num)**2 # v=E((x-m)^2)=E(x^2)-m^2  where m=E(X).
end