How can we decide on a product or service price using observed data over time? For example, a retailer may have changed the price for a specific product in multiple time slots of a week, tested the demanded quantity (sales) based on different prices, and now wants to know the optimal price to set for the next week. Notably, the optimal price should maximize the total revenue of the retailer. This article describes how the optimal price can be determined using Gekko in Python. It can be one of the possible cases in which optimization and machine learning can be integrated!

## Modeling and solving the pricing problem in Python

At first, I code the decision problem according to the following assumptions and the elements of the decision making environment:

*The dataset*:

- Consists of Quantity-Price observations over business days of the previous week.

*The demand function*:

- Is estimated via linear regression.

*The product/service*:

- Can be of any type, any kind, with any characteristic.
- Has had a specific amount of sales during the previous week.
- Has experienced different levels of prices during the previous week.

Consider a dataset as follows:

Weekday | Sales | Price |

Monday | 80 | 20 |

Tuesday | 150 | 22 |

Wednesday | 200 | 18 |

Thursday | 400 | 25 |

Friday | 145 | 55 |

Saturday | 350 | 15 |

Sunday | 409 | 21 |

Herein, I provide a simple code that models the decision problem, satisfying the mentioned assumptions based on this dataset:

import gekko as op import itertools as it #Developer: @KeivanTafakkori, 12 Mar 2022 def model (U,T,a,b,solve="y"): m = op.GEKKO(remote=False, name='LinearRegressionProblem') x = {i: m.Var(lb=None, ub=None) for i in U} z = m.Var(lb=None, ub=None) g = {t: m.Var(lb=None,ub=None) for t in T} n_a = {(t,i): a[t][i] for t,i in it.product(T,U)} n_b = {t: b[t] for t in T} objs = {0: (2*len(T))**(-1)*sum((g[t]-n_b[t])**2 for t in T)} cons = {0: {t: (g[t] == sum(n_a[(t,i)]*x[i] for i in U) + z) for t in T}} m.Minimize(objs[0]) for keys1 in cons: for keys2 in cons[keys1]: m.Equation(cons[keys1][keys2]) if solve == "y": m.options.SOLVER=1 m.solve(disp=True) for keys in x: x[keys] = x[keys].value[0] print(f"x[{keys}]", x[keys]) z = z.value[0] print("z", z) return m,x,z # Monday Tuesday Wednesday Thursday Friday Saturday Sunday a = [ [80], [150], [200], [400], [145], [350], [409]] #Sales b = [ 20, 22, 18, 25, 55, 15, 21] #Price U = range(len(a[0])) #Set of input features T = range(len(b)) #Set of the training points m,x,z = model(U,T,a,b) #Model and solve the problem

This code fits a linear function (we call it linear demand function) and finds a slope and intercept to minimize the prediction error. Accordingly, it results to the following output:

x[0] -0.02980603901 z 32.526238806

Next, we want to set an optimal price for our product/service with a predicted linear demand function at hand to maximize revenue. Considering that revenue is generated by the quantity of sales times the price set, the following optimization model should be solved to find the optimal price:

#Developer: @KeivanTafakkori, 12 March 2022 def model (x,z): m = op.GEKKO(remote=False, name='PricingProblem') q = m.Var(lb=0, ub=None) objs = {0: z*q+x*(q**2)} m.Maximize(objs[0]) m.options.SOLVER=2 m.solve(disp=True) print("total revenue: ", z*q.value[0]+x*(q.value[0]**2), "$") print("marginal revenue: ", z+2*x*q.value[0], "$") print("optimal price (maximum willingness to pay): ", z+x*q.value[0], "$") print("optimal sales (consumption level): ", q.value[0] , "units") return m x = x[0] #predicted slope of the demand function using linear regression z = z #predicted intercept of the demand function using linear regression m = model(x,z) #Model and solve the problem

Solving the above model results to the following output:

total revenue: 8873.673305852495 $ marginal revenue: -5.7734276026621956e-08 $ optimal price (maximum willingness to pay): 16.263119374132863 $ optimal sales (consumption level): 545.63168982 units

Therefore, it is suggested to set the price at 16.26 $ to generate maximum revenue possible (i.e., 8873.67$)!

## Concluding remarks

In this article, I described how a machine learning model can be integrated with an optimization model to set an optimal price for our product/service. The interested readers can learn more about applications of **optimization in machine learning**, or **solvers **and **interfaces **in Python by visiting the linked references. If you find these contents insightful, let us know by commenting below this article! You may also contact us at this link.

If this article is going to be used in research or other publishing methods, you can cite it as Tafakkori (2022) (in text) and refer to it as follows: Tafakkori, K. (2022). Pricing with linear demand function using Gekko in Python. *Supply Chain Data Analytics*. url: https://www.supplychaindataanalytics.com/pricing-with-linear-demand-function-using-gekko-in-python/

Industrial engineer focusing on leveraging optimization methods and artificial intelligence technologies using multiple programming languages to empower a business to achieve its goals!

## 2 thoughts on “Pricing with linear demand function using Gekko in Python”