# A Unique Intersection of Game Theory and Data Science: E-Commerce Product Pricing

## Price Elasticity, Cross Elasticity and Nash Equilibrium

9 min readJun 18, 2021

--

# Contents:

1. Introduction
2. Concept of Elasticity
3. Measuring Elasticity: Linear Regression
4. Example: Data & Code
5. Dynamic Pricing in Competition: Game Theory
6. Nash Equilibrium

# Introduction

One of the biggest challenges in e-commerce is to utilize data mining methods for the improvement of their dynamic pricing policies. Usually these products in the e-commerce face severe competition either from same products hosted in different e-commerce website or from products from different brand.

In this blog, I will try to model this severe competition as a formal 2 player strategic game and apply one of the most famous game theoretic solution concept called Nash Equilibrium. We will take product’s price-demand data from E-commerce website across time and write a python code to solve the Game theory mathematical equations.

By the end of this blog, you can understand how game theory can be used to solve dynamic pricing problems in competitions setup.

# Concept of Elasticity

It is very important for an e-commerce product pricing systems to understand.

1. The change in product demand in response to change in price.
Example: If the price of a particular laptop decreases on Amazon , the sales of that same laptop increases on Amazon
2. The change in product demand when its contender changes the price.
Example 1: If the price of a particular laptop decreases on Amazon, same laptop sales decreases on Flipkart as people tend to buy laptops from amazon.
Example 2: If the price of particular HP laptop decreases on amazon, then the sales of Lenovo laptop with same specs decreases as people tend to buy HP laptop.

It is very important to quantify these effect of prices on demand. Let’s burrow the concept of elasticity here.

## Price Elasticity:

Price elasticity of demand is a measurement of the change in consumption of a product in relation to a change in its price. Expressed mathematically, it is:[1]

Inelastic: That is, a reduction in price does not increase demand much, and an increase in price does not hurt demand either.

Elastic: price changes for these goods cause substantial changes in their demand or their supply.

As we can clearly see that price elasticity clearly quantifies 1. The change in product demand in response to change in price.

## Cross Elasticity:

The cross elasticity of demand is an economic concept that measures the responsiveness in the quantity demanded of one good when the price for competitor changes.

As we can clearly see that Cross elasticity clearly quantifies: 2. The change in product demand when its contender changes the price.

# Measuring Elasticity: Linear Regression !

For Measuring elasticity, we will use the most famous regression concept that’s first introduced to every data scientist aspirant: Linear Regression.

In linear regression, the coefficient provides an estimate of the slope of a straight line that is assumed be the relationship between the dependent variable and at least one independent variable.

From the calculus, the slope of the line is the first derivative and tells us the magnitude of the impact of a one unit change in the variable “X” upon the value of the variable measured in the units of the variable “Y”.

So, we can calculate the slope 𝓭Q/𝓭P. if we use the a linear regression with dependent variable as sales(Q) and independent variable as price(P) and coefficient gives the slope.

## Elasticity vs Slope ( 𝓭Q/𝓭P )

Please note that elasticity is % Change in Quantity Demanded / % Change in Price. where Slope is coefficient of Linear Regression: 𝓭Q/𝓭P (unit change in quantity/ unit change in price).

There are two ways we can extract “ELASTICITY” from Linear regression.

1. Directly use demand(Q) as dependent variable(Y) and price(p) as independent variable(X).
Elasticity = slope* (P/Q)
This is called level-level transformation
2. Use log of demand(Q) as dependent variable(Y) and log of price(p) as independent variable(X).
Elasticity = slope
This is called log-log transformation

This chart explains the usage of level-level and log-log method to calculate price elasticities.
X = Price
Y = Quantity/demand/sales

# Example: Data & Code

Let’s take price and demand data for example and compute the elasticities.

As you can clearly see as price increases the sales are decreasing. Let’s now try to fit a Linear Regression on log(sales) and log(price) which directly outputs the slope as elasticity. The outliers are very clear and need to be removed for fitting the LR models, we can remove outlier using the Z-score

## 1. Price Elasticity

`import pandas as pdimport statsmodels.api as smdf = pd.read_csv('data.csv')df['AveragePrice'] = np.log(df['AveragePrice'].values)df['UnitsSold'] = np.log(df['UnitsSold'].values)X = df[‘AveragePrice’]Y = df[‘UnitsSold’]X = sm.add_constant(X)model = sm.OLS(Y,X)results = model.fit()print(results.summary())df.plot.scatter(x = ‘AveragePrice’, y = ‘UnitsSold’)import matplotlib.pyplot as pltfig = plt.figure(figsize=(10,5))fig = sm.graphics.plot_partregress_grid(results, fig=fig)`

## Interpreting Output

1. Price Elasticity: -3.6
2. If Price increases by 10%, Sales will be reduced by 36%
3. P-Value < 0.05 implies that the coefficient is Significant outcome

## 2. Cross Elasticity

We just need to replace the price of itself to price of competitor and we will get the Cross elasticity.

1. Cross Elasticity: 0.44
2. If Price of coopetitor decreased by 10%, Sales will be reduced by 4.4%

In the next section, we will discuss the concepts of game theory and how its applicable here. Please refer to my game theory series on medium

# Dynamic Pricing in Competition: Game Theory

Let’s setup this problem as a static game. Let’s assume company ‘X’ and company ‘Y’ are in competition and each has choice to increase of decrease the prices.

## Intuition of Game

As we saw in the above discussion price increase in ‘X’ does decrease the sales of ‘X’ which is quantified by price elasticity. Also, we saw that price increase in competitor ‘Y’ does increase the sales of ‘X’ which is quantified by cross elasticity.

So if any company choose to increase or decrease price, it has impact on it’s own sales/profit and also it’s competitor's sales/profit. In order to maximize your well being in this competitive environment, company’s dynamic pricing systems not only think of your actions but also guess what competitors are doing, in order to maximize your reward.

How to guess competitor’s actions? Simple your competitor is also no less rational than you, they take decisions in a similar way. Your competitors also know what can happen to their profits if you increase or decrease your price.

## Pay-off matrix

Pay-off matrix is one way to define a game.

Rows: Represent players X strategies
Columns: Represents player Y strategies
Matrix Entries: payoff of X/Y

As the plot explains we will need to get the pay-off or profit of each company is different actions taken by company ‘X’ and company ‘Y’.

# Nash Equilibrium

A most famous solution concept which is as a profile of strategies for which each player is choosing a best response to the strategies of all other players.

Each strategy in a Nash equilibrium is a best response to all other strategies in that equilibrium.

It is named after John Nash, who got the Nobel Prize for it in 1994. The concept represents an outcome within the game, at which point no player can increase payoff by changing their strategic decisions. We use it to attempt to explain and understand the decision-making process and strategy between rational individuals.

Mathematically, let’s look at the application of Nash Equilibrium to our dynamic pricing problem. We will code it up at the end of this section. Let’s assume player 1 and player 2 are in product pricing systems of product 1 and Product 2 and they are in competition.

## Step 1: Define Elasticities

We can compute 2- price elasticities and 2- cross elasticities as mentioned in the above section.

## Step 2: Define Pay-off function

Here it is very important to define the pay-off function. For different companies this payoff can differ as some companies prefer customer satisfaction over profit. For now we will simply take immediate profit as pay-off function.

We will define payoff/profit for player 1 & player , if player 1 changes price x1 and player changes price x2.

## Step 3: Nash Equilibrium Solving

In this step, we find the Nash Equilibrium for both the players and find out the price changes for player 1 and player 2 at the equilibrium. This is the point where no player can increase payoff by changing their strategic decisions.

## Step 4: Example & Code

We will assume % profit as the payoff as it make more intuitive sense. We will take a scenario where 2 head phones on a e-commerce market are in severe competition. The price elasticity and cross elasticities are defined as follows:

As we discusses in the price elasticity, The price changes have parabolic effect on it’s own % profit. We will understand cross elasticity in our next plot:

The effect of cross- elasticity is very clear in the above plot. If the price of “Head Phones 2 ” increased, then the sales of it’s competitor (Head Phones 1 )increases and the profits of it’s competitor increases.

Here is the pay-off matrix for both head-phones. We can clearly see how the price changes of both products have discernable impact on the profits of the both the products.

Now let’s code Nash Equilibrium setup and understand the outcome.

`from sympy import symbols, Eq, solveelasticity_ = [[-2.3 ,1],[2,-4]]s1 = 210p1 = 54s2 = 200p2 = 60x, y = symbols(‘x,y’)a1 = elasticity_[0][0]*s1 + s1a2 = elasticity_[1][1]*s2 + s2b1 = (elasticity_[0][0]*2*s1)/p1b2= (elasticity_[1][1]*2*s2)/p2c1= (elasticity_[0][1]*s1)/p2c2= (elasticity_[1][0]*s2)/p1eq1 = Eq((a1+x*b1+y*c1), 0)eq2 = Eq((a2+y*b2+x*c2), 0)solution = solve((eq1, eq2), (x, y))print(‘x1: ‘, solution[x],’,x2: ‘,solution[y])X1 = solution[x]X2 = solution[y]print(‘% Profit of head Phones 1’, 100*((s1+elasticity_[0][0]*X1*s1/p1+elasticity_[0][1]*X2*s1/p2)*(p1+X1) — p1*s1)/(p1*s1) )print(‘% Profit of head Phones 2’, 100*((s2+elasticity_[1][1]*X2*s1/p2+elasticity_[1][0]*X1*s2/p1)*(p2+X2) — p2*s2)/ (p2*s2) )`

## Step 5: Understand the Outcome

Let’s try to understand the Equilibrium. When both the products are in severe competition, the price reduced by head-phones 1 is 20 \$ and the price reduced by the head-phones 2 is 28 \$.
Despite reducing the prices, head phones 1 still faces the losses. In the solution space outside the Nash Equilibrium, head phones 1 will have more losses than 13%.

Let’s try to understand this Equilibrium by fixing the head-phones 2 price change at - 28 \$ and plotting different solutions for head-phones 1.

As we can see clearly in the plot, head phones 1need to reduce its prices by 20 \$ to minimize its loss.

This scenario can be different if both the products are not be severe competition. Note that cross-elasticity serves as an approximate quantification of competition.

# Summary

In this blog, we learnt how to calculate the impact of prices on demand and also understood the impact of competitor’s prices. We introduced the concept of Nash Equilibrium and successfully applied it to the E-commerce competition scenario.

I hope my readers enjoyed reading this blog.

--

--

RL | ML | ALGO TRADING | TRANSPORTATION | GAME THEORY