Building a Price Optimization Tool with Ruby

Much like many other development languages, Ruby can be ably used to develop data optimization tools. Price optimization falls within that realm of possibility.

A price optimization model is a mathematical, AI-powered tool that business developers and analysts use to predict demand variance at different price points. In other words, it predicts how many people will buy your super-cool product at $20 vs. how many will buy it at $30.

Knowing the answer to that, or the predicted answer, helps businesses set prices and achieve the profit margins they aim for. Having a price optimization tool is essential for modern companies, and you can use Ruby to create one.

Ruby is a popular programming language. Without it, we wouldn’t have popular sites like Twitch and Soundcloud. You can do a lot of things with Ruby since it is reasonably flexible. Applications range from database work, and content management systems to online scheduling and time management tools. However, today we are going to look specifically into building a price optimization tool.

Price Optimization

Before we touch on Ruby specifically, it’s essential to understand how price optimization works.

Price optimization uses machine learning to analyze all the factors that drive a product’s price. Then it will align product and price relationships in a rational way. It can also give statistical insight so that you can learn what drives price response in your particular market.

Clearly, price optimization tools are invaluable to modern business owners. Setting too high of a price means you won’t sell the optimal amount, and setting too low of a price means leaving profit on the table.

Though you can optimize prices with a pad and pencil, plus an inordinate amount of time, it’s far easier to put a programming language like Ruby to work.

How To Build a Price Optimization Tool with Ruby

Though there are more advanced price optimization techniques, one of the easiest ways to optimize prices is through linear regression.

In linear regression, you use a continuous dependent variable, in our case, price, to predict an independent variable, in our case, sales.

Step One: Dataset

The first step to building any price optimization tool is to collect data. Historical data is great, and if you’ve been in business for any length of time, you probably have plenty of it. Keeping track of price points in relation to volume sold, your customer’s geographic location, and their characteristics like age or education level will help you optimize prices. Alternatively, be sure to scrape a clear set of data to feed into your model.

Since we’re keeping this simple, we’ll pretend to look at a data set that shows the product price vs. the number of products sold to U.S. customers.

Once your data is formatted correctly on a .CSV file, you’re ready to use Ruby.

Step Two: Put Ruby to Work!

To begin with Ruby, you’ll need to install the ruby_linear_regression gem. Then, create a new file and add requires as follows:


require “ruby_linear_regression”

require “csv”


After that, make sure you delete the header row in your .CSV file if you included one. Then set-up two arrays to hold your independent (x-value) and dependent (y-value) data points as follows:


x_data = []

y_data = []


Once that’s done, you can iterate with the .each method. That will allow you to pull your data on sales and price. It’ll look like this:


csv.each do |row|

  x_data.push( [row[1].to_i] )

  y_data.push( row[2].to_i )


Finally, you’ll put the ruby_linear_regression gem to use to:


To do so, type in the following:


linear_regression =

linear_regression.load_training_data(x_data, y_data)



Once you have all of that complete, you should run the mean square error or MSE. The MSE measures the variance between observed and predicted values. Ideally, your MSE will be low because, in our case, that means more accurate price predictions.


To check MSE, use:


puts “Trained model with the following cost fit #{linear_regression.compute_cost}”


Now it’s time to use this simple price optimization tool.


As an example, let’s say you want to know how many super soft tee-shirts you’ll sell at $20 per piece in the U.S. It would look something like this:


prediction_data = [20]

predicted_popularity = linear_regression.predict(prediction_data)

puts “Predicted sales: #{predicted_sales.round}”


Let the program run, and you’re all set! Ruby should spit out a predicted number of soft tee-shirts sold at a $20 price point.

Of course, there are more advanced ways to use linear regression and Ruby to build a price optimization tool that takes more into account. Linear trees, for example, use decision trees with linear regression to model complicated price structures. They’re useful when you have several distinct customer segments all buying your product for different reasons and in different ways.

For that, it’s best to hire a Ruby specialist. But even if linear regression is too simple for your business needs, understanding the concept will make working with a specialist to build a price optimization tool that much easier.

Final Thoughts

Once you build your price optimization tool, whether it’s something super simple like the above, or something more complicated, you can use Ruby performance testing to check for any speed or memory problems in your code.

From there, it’s a matter of letting the code run. With price optimization models in place, you can be sure your business is making the profits it should, and thanks to Ruby, it takes very little work.

About author:

Christoph is a code-loving father of two beautiful children. He is a full-stack developer and a committed team member at – a subsidiary of When he isn’t building software, Christoph can be found spending time with his family or training for his next marathon.

Zenscrape’s Social Media Handles – github:



Exit mobile version