Class: BigBench::PostProcessor::Environment::PolynomialRegression

Inherits:
Object
  • Object
show all
Defined in:
lib/bigbench/post_processor/environment.rb

Overview

This class performs the actual regression for a specfied degree and timebase. As x it returns the timebase values for the corresponding timebase - e.g. the seconds - and as y it returns the corresponding regression values. Additionally it offers the derivations for the regressions with the deviation method.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(x, y, degree) ⇒ PolynomialRegression

Returns a new instance of PolynomialRegression.



429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
# File 'lib/bigbench/post_processor/environment.rb', line 429

def initialize x, y, degree
  @x, @degree, @derivations, @formulas = x, degree, [], []
  raise "Regression is not possible for a single time value, choose a smaller timebase" if @x.size <= 1
  
  # Perform regression
  x_data = x.map { |xi| (0..degree).map { |pow| (xi**pow).to_f } }
  mx = ::Matrix[*x_data]
  my = ::Matrix.column_vector(y)
  
  
  # Calculate coefficients
  @coefficients = ((mx.t * mx).inv * mx.t * my).transpose.to_a[0]
  
  # Setup functions that map the actual polynom
  @derivations << lambda { |x| (0..@degree).to_a.inject(0) { |result, d| result + (@coefficients[d] * x**d)           }}
  @derivations << lambda { |x| (1..@degree).to_a.inject(0) { |result, d| result + (d * (@coefficients[d] * x**(d-1))) }}
  
  # Store formulas for printing
  @formulas << (0..@degree).to_a.map { |d| d == 0 ? @coefficients[d] : "#{@coefficients[d]}x^#{d}" }
  @formulas << (1..@degree).to_a.map { |d| d == 1 ? @coefficients[d] : "#{d}*#{@coefficients[d]}x^#{d-1}" }
end

Instance Attribute Details

#coefficientsObject (readonly)

Returns the coefficients calculated for the regression and the degree like

[3.428734, 1.176235]


427
428
429
# File 'lib/bigbench/post_processor/environment.rb', line 427

def coefficients
  @coefficients
end

#degreeObject (readonly)

Returns the value of attribute degree.



421
422
423
# File 'lib/bigbench/post_processor/environment.rb', line 421

def degree
  @degree
end

#xObject (readonly)

An array with the seconds in the timebase

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]


420
421
422
# File 'lib/bigbench/post_processor/environment.rb', line 420

def x
  @x
end

Instance Method Details

#derivation(derivation = 0) ⇒ Object

Returns an array with the computed y-values for the corresponding derivation. The default derivation is the

  1. derivation which is the original regression that is equal to the y method. The result looks like this:

[2.5, 2.6, 2.7, 2.8, 3.0, 3.2, 3.4, 3.8, 4.0, 4.9]


456
457
458
# File 'lib/bigbench/post_processor/environment.rb', line 456

def derivation(derivation = 0)
  @x.map{ |x| @derivations[derivation].call(x) }
end

#formula(derivation = 0) ⇒ Object

Returns the printed formula of this polynom



469
470
471
# File 'lib/bigbench/post_processor/environment.rb', line 469

def formula(derivation = 0)
  @formulas[derivation].join " + "
end

#yObject

Returns an array with the regression values like this:

[2.5, 2.6, 2.7, 2.8, 3.0, 3.2, 3.4, 3.8, 4.0, 4.9]


464
465
466
# File 'lib/bigbench/post_processor/environment.rb', line 464

def y
  derivation(0)
end