lib/least_squares.rb in least_squares-0.1.0 vs lib/least_squares.rb in least_squares-0.1.1

- old
+ new

@@ -37,16 +37,14 @@ # @example # xs = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] # ys = [9, 1, 0, 5, 4, 7, 7, 0, 9, 3] # Math.pearson(xs,ys) #=> 0.0581327470763432 def Math.pearson(xs, ys) - numerator = 0 xs_mean = Math.mean(xs) ys_mean = Math.mean(ys) - (0...xs.size).each do |i| - numerator += (xs[i] - xs_mean) * (ys[i] - ys_mean) - end - numerator / (Math.sqrt(xs.inject(0){|s,x| s + (x - xs_mean) ** 2}) * Math.sqrt(ys.inject(0){|s,y| s + (y - ys_mean) ** 2})) + numerator = xs.zip(ys).inject(0){|s,n| s + ((n[0] - xs_mean) * (n[1] - ys_mean))} + denominator = Math.sqrt(xs.inject(0){|s,x| s + (x - xs_mean) ** 2}) * Math.sqrt(ys.inject(0){|s,y| s + (y - ys_mean) ** 2}) + numerator / denominator end ## # Takes two arrays of numbers and returns the Least Squares Regression Line # as a Proc.