{{ message }}

Instantly share code, notes, and snippets.

# rweald/denominator-snippet.rb

Created Aug 29, 2012
Code Snippets for Simple Linear Regression Using Ruby Blog Post
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
 denominator = @xs.reduce(0) do |sum, x| sum + ((x - x_mean) ** 2) end
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
 class SimpleLinearRegression def initialize(xs, ys) @xs, @ys = xs, ys if @xs.length != @ys.length raise "Unbalanced data. xs need to be same length as ys" end end def y_intercept mean(@ys) - (slope * mean(@xs)) end def slope x_mean = mean(@xs) y_mean = mean(@ys) numerator = (0...@xs.length).reduce(0) do |sum, i| sum + ((@xs[i] - x_mean) * (@ys[i] - y_mean)) end denominator = @xs.reduce(0) do |sum, x| sum + ((x - x_mean) ** 2) end (numerator / denominator) end def mean(values) total = values.reduce(0) { |sum, x| x + sum } Float(total) / Float(values.length) end end
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
 x_mean = mean(xs) y_mean = mean(ys) numerator = (0...@xs.length).reduce(0) do |sum, i| sum + ((@xs[i] - x_mean) * (@ys[i] - y_mean)) end
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
 data <- read.csv("path to csv") lm(Number.of.Views ~ Days.Online, data = data)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
 require_relative 'simple-linear-regression' data_file = File.open(ARGV) data_file.readline # strip off the header xs, ys = [], [] data_file.each do |line| x, y = line.split(",") xs << Float(x) ys << Float(y) end linear_model = SimpleLinearRegression.new(xs, ys) puts "Model generated with" puts "Slope: #{linear_model.slope}" puts "Y-Intercept: #{linear_model.y_intercept}" puts "\n" puts "Estimated Linear Model:" puts "Y = #{linear_model.y_intercept} + (#{linear_model.slope} * X)"
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
 class SimpleLinearRegression def mean(values) total = values.reduce(0) { |sum, x| x + sum } Float(total) / Float(values.length) end end
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
 class SimpleLinearRegression def initialize(xs, ys) @xs, @ys = xs, ys if @xs.length != @ys.length raise "Unbalanced data. xs need to be same length as ys" end end def slope x_mean = mean(@xs) y_mean = mean(@ys) numerator = (0...@xs.length).reduce(0) do |sum, i| sum + ((@xs[i] - x_mean) * (@ys[i] - y_mean)) end denominator = @xs.reduce(0) do |sum, x| sum + ((x - x_mean) ** 2) end (numerator / denominator) end #Code truncated for clarity end
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
 def y_intercept mean(@ys) - (slope * mean(@xs)) end