Class: HomeQ::Histogram
- Inherits:
-
Object
- Object
- HomeQ::Histogram
- Defined in:
- lib/homeq/base/histogram.rb
Instance Attribute Summary collapse
-
#f ⇒ Object
readonly
array of frequency.
-
#max ⇒ Object
readonly
Returns the value of attribute max.
-
#min ⇒ Object
readonly
Returns the value of attribute min.
-
#num ⇒ Object
readonly
Returns the value of attribute num.
-
#s1 ⇒ Object
readonly
Returns the value of attribute s1.
-
#s2 ⇒ Object
readonly
Returns the value of attribute s2.
-
#s3 ⇒ Object
readonly
Returns the value of attribute s3.
-
#s4 ⇒ Object
readonly
Returns the value of attribute s4.
-
#w ⇒ Object
readonly
class width.
Instance Method Summary collapse
- #avg ⇒ Object
- #bar(x, unit = 1) ⇒ Object
- #cv ⇒ Object
- #h_class(x) ⇒ Object
-
#initialize(min, max, width) ⇒ Histogram
constructor
A new instance of Histogram.
- #kurtosis ⇒ Object
- #push(x) ⇒ Object (also: #<<)
- #report(unit = 1) ⇒ Object
- #sd ⇒ Object
-
#size ⇒ Object
statistics.
- #skewness ⇒ Object
- #sum ⇒ Object
- #to_s(unit = 1) ⇒ Object
- #variance ⇒ Object
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
#f ⇒ Object (readonly)
array of frequency
33 34 35 |
# File 'lib/homeq/base/histogram.rb', line 33 def f @f end |
#max ⇒ Object (readonly)
Returns the value of attribute max.
31 32 33 |
# File 'lib/homeq/base/histogram.rb', line 31 def max @max end |
#min ⇒ Object (readonly)
Returns the value of attribute min.
30 31 32 |
# File 'lib/homeq/base/histogram.rb', line 30 def min @min end |
#num ⇒ Object (readonly)
Returns the value of attribute num.
35 36 37 |
# File 'lib/homeq/base/histogram.rb', line 35 def num @num end |
#s1 ⇒ Object (readonly)
Returns the value of attribute s1.
36 37 38 |
# File 'lib/homeq/base/histogram.rb', line 36 def s1 @s1 end |
#s2 ⇒ Object (readonly)
Returns the value of attribute s2.
37 38 39 |
# File 'lib/homeq/base/histogram.rb', line 37 def s2 @s2 end |
#s3 ⇒ Object (readonly)
Returns the value of attribute s3.
38 39 40 |
# File 'lib/homeq/base/histogram.rb', line 38 def s3 @s3 end |
#s4 ⇒ Object (readonly)
Returns the value of attribute s4.
39 40 41 |
# File 'lib/homeq/base/histogram.rb', line 39 def s4 @s4 end |
#w ⇒ Object (readonly)
class width
32 33 34 |
# File 'lib/homeq/base/histogram.rb', line 32 def w @w end |
Instance Method Details
#avg ⇒ Object
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 (x,unit=1) n = (x.to_f.nan? ? 0 : x.to_f / unit).ceil ("----+" * (n/5+1))[0,n] end |
#cv ⇒ Object
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 |
#kurtosis ⇒ Object
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 |
#sd ⇒ Object
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 |
#size ⇒ Object
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 |
#skewness ⇒ Object
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 |
#sum ⇒ Object
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, (percentage / 1.8, unit), percentage.nan? ? 0 : percentage, @f[i]) end s end |
#variance ⇒ Object
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 |