Module: PROIEL::Statistics
- Defined in:
- lib/proiel/statistics.rb
Class Method Summary collapse
-
.least_squares(x, y) ⇒ Array(Float, Float)
Computes the line of best fit using the least-squares method.
Class Method Details
.least_squares(x, y) ⇒ Array(Float, Float)
Computes the line of best fit using the least-squares method.
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/proiel/statistics.rb', line 22 def self.least_squares(x, y) raise ArgumentError unless x.is_a?(Array) raise ArgumentError unless y.is_a?(Array) raise ArgumentError, 'array lengths differ' unless x.size == y.size x_mean = x.reduce(&:+).to_f / x.size y_mean = y.reduce(&:+).to_f / y.size x_sqsum = x.reduce(0.0) { |sum, n| sum + n**2 } xy_sum = x.zip(y).reduce(0.0) { |sum, (m, n)| sum + m * n } sxy = xy_sum - x.length * x_mean * y_mean sx2 = x_sqsum - x.length * (x_mean**2) beta = sxy / sx2 alfa = y_mean - beta * x_mean [alfa, beta] end |