Created
July 23, 2020 09:12
-
-
Save intrip/f6760ce2a4dcdd7dbc7a73f304f7fc13 to your computer and use it in GitHub Desktop.
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 'matrix' | |
require 'ruby_linear_regression' | |
ROWS_PER_OPTION = 1_000 | |
POSITION_VALUES_MAPPING = { | |
top_left: { value: [1, 1], consent_rate: 60 }, | |
bottom_left: { value: [1000, 1], consent_rate: 30 }, | |
top_right: { value: [1, 1000], consent_rate: 45 }, | |
bottom_right: { value: [1000, 1000], consent_rate: 20 }, | |
} | |
generated_array = [] | |
POSITION_VALUES_MAPPING.each do |position, values_map| | |
consent_rate = values_map[:consent_rate] | |
lower_bound = consent_rate - 0.1 * consent_rate | |
upper_bound = consent_rate + 0.1 * consent_rate | |
ROWS_PER_OPTION.times do | |
random_consent_rate = rand(lower_bound..upper_bound) | |
generated_array << [values_map[:value], random_consent_rate] | |
end | |
end | |
generated_array.shuffle! | |
x_data = [] | |
y_data = [] | |
POSITION_MAPPING = { | |
top_left: [1, 1], | |
bottom_left: [1000, 1], | |
top_right: [1, 1000], | |
bottom_right: [1000, 1000], | |
} | |
# Load data from CSV file into two arrays - one for independent variables X (x_data) and one for the dependent variable y (y_data) | |
generated_array.each do |row| | |
# Each row contains option value and expected consent rate | |
x_data.push( [row[0][0].to_i, row[0][1].to_i]) | |
y_data.push( row[1].to_i ) | |
end | |
# Create regression model | |
linear_regression = RubyLinearRegression.new | |
# Load training data | |
linear_regression.load_training_data(x_data, y_data) | |
# Train the model using gradient descent | |
# p linear_regression.train_gradient_descent(0.0001, 1000, false) | |
# Train using least squares | |
linear_regression.train_normal_equation | |
# Output the cost | |
puts "Trained model with the following cost fit #{linear_regression.compute_cost}" | |
# Predictions | |
POSITION_MAPPING.each do |key, value| | |
predicted_consent_rate = linear_regression.predict([value[0], value[1]]) | |
puts "Prediction for #{key} position is #{predicted_consent_rate}" | |
end | |
#Trained model with the following cost fit 3.6977304062500846 | |
# | |
# Prediction for top_left position is 58.2812499999997 | |
# Prediction for bottom_left position is 30.821750000000435 | |
# Prediction for top_right position is 45.663750000000164 | |
# Prediction for bottom_right position is 18.204250000000897 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment