Class: StreamStat::V

Inherits:
Object
  • Object
show all
Defined in:
lib/stream_stat/v.rb

Overview

Accumrator.

This holds :avg, :variance & :sd.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(count = 0, avg = 0.0, square_avg = 0.0, min = Float::INFINITY, max = -Float::INFINITY)) ⇒ V

Returns a new instance of V.



10
11
12
13
14
15
16
# File 'lib/stream_stat/v.rb', line 10

def initialize(count = 0, avg = 0.0, square_avg = 0.0, min = Float::INFINITY, max = -Float::INFINITY)
  @count = count
  @avg = avg
  @square_avg = square_avg
  @min = min
  @max = max
end

Instance Attribute Details

#avgObject (readonly)

Returns the value of attribute avg.



8
9
10
# File 'lib/stream_stat/v.rb', line 8

def avg
  @avg
end

#maxObject (readonly)

Returns the value of attribute max.



8
9
10
# File 'lib/stream_stat/v.rb', line 8

def max
  @max
end

#minObject (readonly)

Returns the value of attribute min.



8
9
10
# File 'lib/stream_stat/v.rb', line 8

def min
  @min
end

Instance Method Details

#next(item) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/stream_stat/v.rb', line 18

def next(item)
  item = item.to_f
  next_length = @count + 1
  self.class.new(
    next_length,
    next_avg(next_length, item),
    next_square_avg(next_length, item),
    [@min, item].min,
    [@max, item].max
  )
end

#sdObject



34
35
36
# File 'lib/stream_stat/v.rb', line 34

def sd
  Math.sqrt variance
end

#varianceObject



30
31
32
# File 'lib/stream_stat/v.rb', line 30

def variance
  @square_avg - @avg**2
end