Unlock Price Optimization Potential with Python—Modelling Linear and Non-linear Price Elasticity

Price Elasticity Formulas and Modelling Codes for Building Effective Price Strategy

ChenDataBytes
8 min readOct 31, 2023
Photo by Rajiv Perera on Unsplash

Price elasticity of Demand is a metric used to determine the degree to which demand for a product or service responds to fluctuations in its price. Specifically, it quantifies the percentage change in the quantity of the good or service demanded as a function of a corresponding percentage change in its price. In essence, price elasticity captures the magnitude of the responsiveness of quantity demanded for changes in price. The pricing elasticity formula is provided below in Fig.1 :

Fig 1. Price elasticity formula

Q is the quantity of the good demanded, and ΔQ is how much the quantity changed. P is the price of the good demanded, and Δ P is how much the price changed.

Data

We use the following price data for modelling.

import pandas as pd
df = pd.read_csv('./Data/price_data.csv')
df.plot(x="Quantity", y="Price",kind="scatter")
Fig 2. Scatter plot for price and quantity
df.describe()

The median price is $187, and the median quantity is 700. The basic statistics show we have extreme values in prices and quantity.

Linear Price Elasticity Model

Linear price elasticity can be calculated from a linear regression demand function (Fig.3 ). ΔQ/ΔP is the coefficient (b) from the linear regression fit. P is the price and Q is the quantity for a given product or service. If we want to get the overall elasticity of a product or service, we use the mean value of price and quantity as the initial value for P and Q.

Fig 3. Linear Price Elasticity Function

While the relationship between price and quantity is not necessarily linear (from the scatter plot in Fig.2), we explore a linear regression demand function to examine how well the linear model performs. By utilizing Python’s Statsmodels for Ordinary Least Squares (OLS) Estimation, we analyze the coefficient (b) to determine the linear price elasticity.

import statsmodels.api as sm
from statsmodels.tools.tools import add_constant

x_value = df['Price']
y_value = df['Quantity']
X = add_constant(x_value)
model = sm.OLS(y_value, X)
result = model.fit()

print(result.summary())

The small P values (0.000) indicate that we can reject the null hypothesis that Price does not affect Quantity. A low R-squared (0.049) indicates that our model cannot explain a lot of the response variability.

Despite the linear model’s limitations in capturing the complexity of pricing data, we still employ the linear price elasticity formula for calculation purposes. In this case, linear price elasticity is -1.77. Now the linear demand function of this product can be written as Quantity = 2889.2504 + (-6.6389)*Price.

coefficient_pvalue = result.f_pvalue
intercept,slope = result.params

#Linear Price elasticity Formula
price_elasticity = round((slope)*(mean_price/mean_quantity),2)

print('Linear Price Elasticity {} with pvalue {}'\
.format(price_elasticity, coefficient_pvalue))

Non-Linear Price Elasticity Model

Observing that the price curve exhibits a power curve pattern in Fig.2, we utilize a non-linear model derived from a power regression demand function (Fig. 4). By applying logarithmic functions and taking total differentials, we can determine the elasticity as -b.

Fig 4. Non-linear Price Elasticity Function
import statsmodels.api as sm
from statsmodels.tools.tools import add_constant
import numpy as np

x_value = np.log(df['Price'])
y_value = np.log(df['Quantity'])
X = add_constant(x_value)
model = sm.OLS(y_value, X)
result = model.fit()

print(result.summary())

The small P values (0.000) indicate that we can reject the null hypothesis that Price does not affect Quantity. We have an improved R-squared (0.132), but it still indicates that our model has room for further enhancement. In this exercise, we focus on applying the non-linear price elasticity concept, rather than building a perfect model.

Similarly, we can get b from the coefficient (b) of the linear regression fit, which is the non-linear price elasticity. The linear price elasticity is -1.07.

coefficient_pvalue = result.f_pvalue
intercept,slope = result.params

#Non_linear Price elasticity Formula
price_elasticity = round((slope),2)
print('Non_linear Price Elasticity {} with pvalue {}'.format(price_elasticity, coefficient_pvalue))

Remember we fit the regression using a log function, so the intercept from the regression is log(a). To get the non-linear demand function, we apply the exp() method returns the value of a.

import math

round(math.exp(12.1370),0)

Now the non-linear demand function of this product can be written as Quantity = 186652 * Price**-1.07

How to interpret elasticity

Let’s look into different types of elasticity.

Perfectly inelastic demand (E = 0)

When demand is perfectly inelastic, changes in the price of the good do not affect its quantity demanded, resulting in a vertical straight-line demand curve.

2. Relatively inelastic demand (-1 < E < 0)

When demand is relatively inelastic, a change in price results in a smaller percentage change in quantity demanded, leading to a downward-sloping demand curve.

3. Unit elastic demand (E = -1)

When demand is unit elastic, the percentage change in quantity demanded equals that in price, resulting in a constant demand curve with a slope of -1.

4. Relatively elastic demand (-∞ < E < -1)

When demand is relatively elastic, a change in price leads to a larger percentage change in quantity demanded, resulting in a demand curve that slopes more steeply downward.

5. Perfectly elastic demand (E is -∞)

When demand is perfectly elastic, any increase in the price of the good, no matter how small, results in the quantity demanded dropping to zero, resulting in a horizontal straight-line demand curve.

So, the price elasticity of the product in our exercise is -1.07 (we used a non-linear one due to a better model fit), and we classify it as relatively elastic demand. This finding suggests that a 1% increase in price results in a 1.07% decrease in demand.

Price optimization based on elasticity

The ultimate goal is to recommend the optimal price point for the business. We explore various price points to understand how demand and revenue fluctuate based on elasticity and the demand function.

By using a price range between 15 and 1000, we see that the optimised price for max revenue is $15, which is the lowest price point.

import numpy as np

# Set price elasticity range
elasticity = -1.05

# Set price range
price = list(np.linspace(15, 1000, num=100))
demand = 186652 * np.power(price,elasticity)
revenue = price * demand
max_revenue = np.max(revenue)
max_index = np.argmax(revenue)
price[max_index]

We can also see from the revenue and price chart (Fig. 5), the highest revenue is when the price is lowest. The percentage difference between the highest revenue ($154,420) and the lowest revenue ($115,088) is 34%, and we see a smaller difference after raising the price to $200.

price_df.plot(kind='line', y='Revenue', x="Price")
plt.ylim(0,170000)
Fig 5. Revenue vs Price

Price strategy based on elasticity

Once we have the price elasticity from the modelling, how does the business use this measure for its price strategy?

  • Elastic Demand: If a product has a high price elasticity of demand, meaning that consumers are highly sensitive to changes in price, businesses may need to adopt a lower pricing strategy to attract more customers and increase sales. This could involve lowering the price of the product or offering discounts, coupons, or other promotional offers.
  • Inelastic Demand: If a product has a low price elasticity of demand, meaning that consumers are relatively insensitive to changes in price, businesses may be able to adopt a higher pricing strategy without significantly reducing demand. This could involve raising the price of the product or offering premium features or services to justify a higher price point.

Based on the dataset, we observe that the median price is $187. It is important to note that prices exceeding $200 do not contribute to any revenue growth. However, by adopting a lower price strategy, businesses can capitalize on the relatively elastic demand for the product to increase overall revenue.

Furthermore, I experimented with a simulation exercise with various non-linear price elasticity metrics, such as incorporating -0.8 for inelasticity and -2 for perfect elasticity, while keeping the intercept constant. This exploration (Fig 6.) allowed me to examine how revenue curves are affected. It becomes evident that our suggested strategy holds: that when greater elasticity is present, a higher discount is required to achieve revenue growth. On the other hand, inelasticity demonstrates revenue growth even when raising the price points.

#add -0.8 inelastic 
elasticity1 = -0.8
Set price range
price = list(np.linspace(15, 1000, num=100))
demand1 = 186652 * np.power(price,elasticity1)
revenue1 = price * demand1
max_revenue1 = np.max(revenue1)
max_index1 = np.argmax(revenue1)
price[max_index1]

data1 = {'Price': price, 'Revenue': revenue1, 'Quantity': demand1}
price_df1 = pd.DataFrame(data1)

#add -2 perpectly elastic
elasticity2 = -2
price = list(np.linspace(15, 1000, num=100))
demand2 = 186652 * np.power(price,elasticity2)
revenue2 = price * demand2
max_revenue2 = np.max(revenue2)
max_index2 = np.argmax(revenue2)
price[max_index2]

data2 = {'Price': price, 'Revenue': revenue2, 'Quantity': demand2}
price_df2 = pd.DataFrame(data2)

final_price_df = price_df1.merge(price_df, left_on='Price', right_on='Price')\
.merge(price_df2, left_on='Price', right_on='Price')

final_price_df = final_price_df.rename(columns={'Revenue_x':'-0.8_elasticity_rev',\
'Revenue':'-2_elasticity_rev',\
'Revenue_y':'-1.07_elasticity_rev',
})[['Price','-0.8_elasticity_rev','-1.07_elasticity_rev','-2_elasticity_rev']].set_index('Price')

final_price_df.plot()
Fig 6. Different elasticity metrics against revenue

End note:

While this article focuses on a specific method, such as power regression, to model non-linear price elasticity, it is important to analyze data and consider other regression forms, such as polynomial regression, to capture the complexity of price elasticity. Leveraging Python for modelling price elasticity bridges economic concepts with the data science realm, empowering businesses to make informed pricing decisions.

--

--