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, default = nil) ⇒ 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, default = nil)
  @values = Array.new(n, default)
  @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
# File 'lib/postrunner/LinearPredictor.rb', line 29

def insert(value)
  @values << value

  if @values.length > @size
    @values.shift
    @next = @values.reduce(:+) / @size
  end
end

#predictFloat

Returns The predicted value of the next sample.

Returns:

  • (Float)

    The predicted value of the next sample.



39
40
41
# File 'lib/postrunner/LinearPredictor.rb', line 39

def predict
  @next
end