lib/benchmark/trend.rb
def fit(xs, ys, tran_x: ->(x) { x }, tran_y: ->(y) { y })
      eps    = (10 ** -10)
      n      = 0
      sum_x  = 0.0
      sum_x2 = 0.0
      sum_y  = 0.0
      sum_y2 = 0.0
      sum_xy = 0.0

      xs.zip(ys).each do |x, y|
        n        += 1
        sum_x    += tran_x.(x)
        sum_y    += tran_y.(y)
        sum_x2   += tran_x.(x) ** 2
        sum_y2   += tran_y.(y) ** 2
        sum_xy   += tran_x.(x) * tran_y.(y)
      end

      txy = n * sum_xy - sum_x * sum_y
      tx  = n * sum_x2 - sum_x ** 2
      ty  = n * sum_y2 - sum_y ** 2

      is_linear = tran_x.(Math::E) * tran_y.(Math::E) == Math::E ** 2

      if tx.abs < eps # no variation in xs
        raise ArgumentError, "No variation in data #{xs}"
      elsif ty.abs < eps && is_linear # no variation in ys - constant fit
        slope = 0
        intercept = sum_y / n
        residual_sq = 1 # doesn't exist
      else
        slope       = txy / tx
        intercept   = (sum_y - slope * sum_x) / n
        residual_sq = (txy ** 2) / (tx * ty)
      end

      [slope, intercept, residual_sq]
    end
def fit(xs, ys, tran_x: ->(x) { x }, tran_y: ->(y) { y })
      eps    = (10 ** -10)
      n      = 0
      sum_x  = 0.0
      sum_x2 = 0.0
      sum_y  = 0.0
      sum_y2 = 0.0
      sum_xy = 0.0

      xs.zip(ys).each do |x, y|
        n        += 1
        sum_x    += tran_x.(x)
        sum_y    += tran_y.(y)
        sum_x2   += tran_x.(x) ** 2
        sum_y2   += tran_y.(y) ** 2
        sum_xy   += tran_x.(x) * tran_y.(y)
      end

      txy = n * sum_xy - sum_x * sum_y
      tx  = n * sum_x2 - sum_x ** 2
      ty  = n * sum_y2 - sum_y ** 2

      is_linear = tran_x.(Math::E) * tran_y.(Math::E) == Math::E ** 2

      if tx.abs < eps # no variation in xs
        raise ArgumentError, "No variation in data #{xs}"
      elsif ty.abs < eps && is_linear # no variation in ys - constant fit
        slope = 0
        intercept = sum_y / n
        residual_sq = 1 # doesn't exist
      else
        slope       = txy / tx
        intercept   = (sum_y - slope * sum_x) / n
        residual_sq = (txy ** 2) / (tx * ty)
      end

      [slope, intercept, residual_sq]
    end
def fit ( xs , ys , tran_x : -> ( x ) { x } , tran_y : -> ( y ) { y } ) eps = ( 10 ** - 10 ) n = 0 sum_x = 0.0 sum_x2 = 0.0 sum_y = 0.0 sum_y2 = 0.0 sum_xy = 0.0 xs . zip ( ys ) . each do | x , y | n += 1 sum_x += tran_x . ( x ) sum_y += tran_y . ( y ) sum_x2 += tran_x . ( x ) ** 2 sum_y2 += tran_y . ( y ) ** 2 sum_xy += tran_x . ( x ) * tran_y . ( y ) end txy = n * sum_xy - sum_x * sum_y tx = n * sum_x2 - sum_x ** 2 ty = n * sum_y2 - sum_y ** 2 is_linear = tran_x . ( Math :: E ) * tran_y . ( Math :: E ) == Math :: E ** 2 if tx . abs < eps # no variation in xs raise ArgumentError , "No variation in data #{xs}" elsif ty . abs < eps && is_linear # no variation in ys - constant fit slope = 0 intercept = sum_y / n residual_sq = 1 # doesn't exist else slope = txy / tx intercept = ( sum_y - slope * sum_x ) / n residual_sq = ( txy ** 2 ) / ( tx * ty ) end [ slope , intercept , residual_sq ] end
Fit the performance measurements to construct a model with
 slope and intercept parameters that minimize the error.

 @param [Array[Numeric]] xs
   the data points along X axis

 @param [Array[Numeric]] ys
   the data points along Y axis

 @return [Array[Numeric, Numeric, Numeric]
   returns slope, intercept and model's goodness-of-fit

 @api public
Fit the performance measurements to construct a model with slope and intercept parameters that minimize the error .