Optimizing product price using regression | by Mahbubul Alam | Towards Data Science

Price and quantity are two fundamental measures that determine the bottom line of every business, and setting the right price is one of the most important decisions a company can make. Under-pricing hurts the company’s revenue if consumers are willing to pay more and, on the other hand, over-pricing can hurt in a similar fashion if consumers are less inclined to buy the product at a higher price.
So given the tricky relationship between price and sales, where is the sweet spot — the optimum price — that maximizes product sales and earns most profit?
The purpose of this article is to answer this question by implementing a combination of the economic theory and a regression algorithm in Python environment.
We are optimizing a future price based on the relationship between historical price and sales, so the first thing we need is the past data on these two indicators. For this exercise, I’m using a time series data on historical beef sales and corresponding unit prices.
# load dataimport pandas as pdbeef = pd# view first few rowsbeef.tail(5
The dataset contains a total of 91 observations of quantity-price pairs reported on a quarterly basis.
It is customary in data science to do exploratory data analysis (EDA), but I’m skipping that part to focus on modeling. Nevertheless, I strongly encourage taking this extra step to make sure you understand the data before building models.
We need to import libraries for three reasons: manipulating data, building the model, and visualizing the functions.
We are importing numpy and pandas for creating and manipulating table, mtplotlib and seaborn for visualization and statsmodels API to build and run the regression model.
import numpy as npfrom pandas import DataFrameimport matplotlib.pyplot as pltimport seaborn as snsfrom statsmodels.formula.api import ols%matplotlib inline
We know that revenue depends on the quantity sold and the unit price of products.
We also know that profit is calculated by netting out costs from revenue.
Putting these two together we get the following equations:
# revenuerevenue = quantity * price # eq (1)# profitprofit = revenue - cost # eq (2)
We can rewrite the profit function by combining eq. #1 and 2 as follows:
# revised profit functionprofit = quantity * price - cost # eq (3)
Eq #3 tells us that we need three pieces of information to calculate profit: quantity, price and cost.
We first need to establish the relationship between quantity and price — the demand function. This demand function is estimated from a “demand curve” based on the linear relationship between price and quantity.
# demand curvesns.lmplot(x = "Price", y = "Quantity", data = beef, fig_reg = True, size = 4)
To find that demand curve we will fit an Ordinary Least Square (OLS) regression model.
# fit OLS modelmodel = ols("Quantity ~ Price", data = beef).fit()# print model summaryprint(model.summary())
The following are the regression results with the necessary coefficients needed for further analysis.
The coefficient we are looking for is coming from the regression model above — the intercept and the price coefficient — to measure the corresponding sales quantity. We can now plug these values into equation 3.
# plugging regression coefficientsquantity = 30.05 - 0.0465 * price # eq (5)# the profit function in eq (3) becomesprofit = (30.05 - 0.0465 * price) * price - cost # eq (6)
The next step is to find the price we are looking for from a range of options. The codes below should be intuitive, but basically what we are doing here is calculating revenue for each price and the corresponding quantity sold.
# a range of diffferent prices to find the optimum onePrice = [320, 330, 340, 350, 360, 370, 380, 390]# assuming a fixed costcost = 80Revenue = []for i in Price:   quantity_demanded = 30.05 - 0.0465 * i      # profit function   Revenue.append((i-cost) * quantity_demanded)# create data frame of price and revenueprofit = pd.DataFrame({"Price": Price, "Revenue": Revenue})#plot revenue against priceplt.plot(profit["Price"], profit["Revenue"])
If price and revenue are plotted, we can visually identify the peak of the revenue and find the price that makes the revenue at the highest point on the curve.
So we find that the maximum revenue at different price levels is reached at $3,726 when the price is set at $360.
# price at which revenue is maximumprofit[profit['Revenue'] == profit[['Revenue'].max()]
The purpose of this article was to demonstrate how to find the price at which the revenue or profit is maximized using a combination of economic theory and statistical modeling. In the initial steps we defined the demand and profit functions, and then ran a regression to find the parameter values needed to feed into the profit/revenue function. And finally, we checked revenues under different price levels to get the price for the corresponding maximum revenue.