Method: Minitest::Benchmark#fit_logarithmic

Defined in:
lib/minitest/benchmark.rb

#fit_logarithmic(xs, ys) ⇒ Object

To fit a functional form: y = a + b*ln(x).

Takes x and y values and returns [a, b, r^2].

See: mathworld.wolfram.com/LeastSquaresFittingLogarithmic.html


257
258
259
260
261
262
263
264
265
266
267
268
269
270
# File 'lib/minitest/benchmark.rb', line 257

def fit_logarithmic xs, ys
  n     = xs.size
  xys   = xs.zip ys
  slnx2 = sigma(xys) { |x, _| Math.log(x) ** 2 }
  slnx  = sigma(xys) { |x, _| Math.log(x)      }
  sylnx = sigma(xys) { |x, y| y * Math.log(x)  }
  sy    = sigma(xys) { |_, y| y                }

  c = n * slnx2 - slnx ** 2
  b = ( n * sylnx - sy * slnx ) / c
  a = (sy - b * slnx) / n

  return a, b, fit_error(xys) { |x| a + b * Math.log(x) }
end