Method: Minitest::Benchmark#fit_exponential
- Defined in:
- lib/minitest/benchmark.rb
permalink #fit_exponential(xs, ys) ⇒ Object
To fit a functional form: y = ae^(bx).
Takes x and y values and returns [a, b, r^2].
See: mathworld.wolfram.com/LeastSquaresFittingExponential.html
235 236 237 238 239 240 241 242 243 244 245 246 247 248 |
# File 'lib/minitest/benchmark.rb', line 235 def fit_exponential xs, ys n = xs.size xys = xs.zip ys sxlny = sigma(xys) { |x, y| x * Math.log(y) } slny = sigma(xys) { |_, y| Math.log(y) } sx2 = sigma(xys) { |x, _| x * x } sx = sigma xs c = n * sx2 - sx ** 2 a = (slny * sx2 - sx * sxlny) / c b = ( n * sxlny - sx * slny ) / c return Math.exp(a), b, fit_error(xys) { |x| Math.exp(a + b * x) } end |