Class: PostRunner::LinearPredictor

Inherits:
Object
  • Object
show all
Defined in:
lib/postrunner/LinearPredictor.rb

Overview

For now we use a trivial adaptive linear predictor that just uses the average of past values to predict the next value.

Instance Method Summary collapse

Constructor Details

#initialize(n) ⇒ LinearPredictor

Create a new LinearPredictor object.

Parameters:

  • n (Fixnum)

    The number of coefficients the predictor should use.



21
22
23
24
25
# File 'lib/postrunner/LinearPredictor.rb', line 21

def initialize(n)
  @values = []
  @size = n
  @next = nil
end

Instance Method Details

#insert(value) ⇒ Object

Tell the predictor about the actual next value.

Parameters:

  • value (Float)

    next value



29
30
31
32
33
34
35
36
37
38
# File 'lib/postrunner/LinearPredictor.rb', line 29

def insert(value)
  @values << value

  if @values.length >= @size
    @values.shift
  end

  @next = @values.reduce(:+) / @values.size
  $stderr.puts "insert(#{value})  next: #{@next}"
end

#predictFloat

Returns The predicted value of the next sample.

Returns:

  • (Float)

    The predicted value of the next sample.



41
42
43
# File 'lib/postrunner/LinearPredictor.rb', line 41

def predict
  @next
end