Module: MoreCoreExtensions::MathSlope::ClassMethods

Defined in:
lib/more_core_extensions/core_ext/math/slope.rb

Instance Method Summary collapse

Instance Method Details

#linear_regression(*coordinates) ⇒ Object

Finds the linear regression of the given coordinates. Coordinates should be given as x, y pairs.

Returns the slope of the line, the y intercept, and the R-squared value.

Math.linear_regression([1.0, 1.0], [2.0, 2.0]) # => [1.0, 0.0, 1.0]


31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/more_core_extensions/core_ext/math/slope.rb', line 31

def linear_regression(*coordinates)
  return if coordinates.empty?

  x_array, y_array = coordinates.transpose
  sum_x  = x_array.sum
  sum_x2 = x_array.map(&:square).sum
  sum_y  = y_array.sum
  sum_y2 = y_array.map(&:square).sum
  sum_xy = coordinates.map { |x, y| x * y }.sum

  n = coordinates.size.to_f
  slope = (n * sum_xy - sum_x * sum_y) / (n * sum_x2 - sum_x.square)
  return if slope.nan?

  y_intercept = (sum_y - slope * sum_x) / n
  r_squared   = (n * sum_xy - sum_x * sum_y) / Math.sqrt((n * sum_x2 - sum_x.square) * (n * sum_y2 - sum_y.square)) rescue nil

  return slope, y_intercept, r_squared
end

#slope_x_intercept(y, slope, y_intercept) ⇒ Object

Finds the x coordinate given y, slope of the line and the y intercept

‘x = (y - b) / m` Where `m` is the slope of the line and `b` is the y intercept

Math.slope_x_intercept(1.5, 0.5, 1) # => 1.0


22
23
24
# File 'lib/more_core_extensions/core_ext/math/slope.rb', line 22

def slope_x_intercept(y, slope, y_intercept)
  (y - y_intercept) / slope.to_f
end

#slope_y_intercept(x, slope, y_intercept) ⇒ Object

Finds the y coordinate given x, slope of the line and the y intercept

‘y = mx + b` Where `m` is the slope of the line and `b` is the y intercept

Math.slope_y_intercept(1, 0.5, 1) # => 1.5


12
13
14
# File 'lib/more_core_extensions/core_ext/math/slope.rb', line 12

def slope_y_intercept(x, slope, y_intercept)
  slope * x + y_intercept
end