Dynamic Pricing With Causal Machine Learning And Optimization On Amazon SageMaker
Dynamic Pricing with Causal Machine Learning and Optimization on Amazon SageMaker
This notebook's CI test result for us-west-2 is as follows. CI test results in other regions can be found at the end of the notebook.

This notebook describes and implements a dynamic pricing experiment on Amazon SageMaker.
Table of contents
- Generate synthetic data
- Input parameters
- Shapes
- Data Generation Process (DGP)
- References
- Estimation of cross-price elasticity with Double Machine Learning (DML)
- Introduction
- DML experiment
- CATE interpretation
- Forecast expected quantities
- Optimization
- Objective functions
- Revenue
- Profit
- Optimization constraints
- Linear constraints
- Bounds
- Solution
- Objective functions
- Simulation
- Procedure
The Python libraries used in this post are:
- NumPy, Pandas for data manipulation.
- Matplotlib for data visualization.
- SciPy for optimization.
- EconML and scikit-learn for Causal Machine Learning.
To install EconML, type inside a notebook cell:
!pip install econml
as follows:
1. Generate synthetic data
In our Causal Machine Learning experiment:
- The price represents the treatment T.
- The volume represents the outcome Y.
- Confounders W are features with an effect on both treatment and outcome. Some examples:
- Discounts
- Coupon
- Stores
- Seasonality
- Inflation
- Cost of energy or materials
- Number of customers
- Social media reactions (sentiment, likes),
- ...
- We generate synthetic data so that:
- Both price and volume are function of a set of confounders.
- Price elasticities are negative to mimic the law of supply and demand.
1.1 Input parameters
- Number of products:
- Number of observations (days):
- Number of confounders:
- Price offset:
- Cost offset:
- Cost fraction (to introduce a proportionality between cost and price):
- Minimum and maximum values for the price elasticity:
- Minimum and maximum values for the cross-price elasticity:
- Error magnitude:
1.2 Shapes
1.3 Data Generation Process (DGP)
Assuming:
Price-elasticity: the effect of change in price in the i-th product over the units sold of the same i-th product:
Cross-price-elasticity: the effect of change in price in the i-th product over the units sold of a different j-th product:
Price (treatment) of the i-th product:
Cost of the i-th product:
Volume (outcome) for the i-th product:
Where:
1.4 References
- The shape of the control variables is inspired by Oprescu et al. [2018].
- The shapes of the treatment and outcome satisfy the structural assumptions of Double Machine Learning as per Chernozhukov [2016].
- The choice of the uniform distrubution is based on the principle of maximum entropy as per Guiasu [1985].
2. Estimation of cross-price elasticity with Double Machine Learning (DML)
2.1 Introduction
Price elasticity represents the relationship between price and demand sales volume for a given product.
Under the assumption of a linear relationship, elasticity is defined as:
In the previous formula:
- : change in volume (demand, units sold) caused by the change in price.
- : change in price.
- : proportionality coefficient known as price elasticity. More information on the topic can be found in Anderson et al. (1997).
We estimate price elasticity through Double Machine Learning (DML) as in Chernozhukov (2016).
In brief, Double Machine Learning is a method for estimating heterogeneous treatment effects over a target variable called outcome, when all the confounders are observed.
Confounders are features that affect both treatment and outcome.
In dynamic pricing, the treatment is the conscious manipulation of product’s price towards profit or revenue maximization, while the outcome is the demand or volume for that product.
Confounders may be, for instance, the application of discounts, coupons, seasonality, and any information that may affect both the price and the demand for a product.
The structural assumptions of the model are as follows:
where:
- : outcomes. In our case, the demand/volume.
- : treatment. In our case, the prices.
- : features that only affect the outcome, namely variables.
- : features that affect both outcome and treatment, namely confounders.
- : marginal conditional average treatment effect (CATE). In our case, the price elasticity K.
- and represent error terms.
- : conditional expectation function.
2.2 DML experiment
2.3 CATE interpretation
The above estimate suggests that increasing the price of product n° 1 by +10% () results in:
- sales drop of 30% for product n°1 (), and
- sales increase of 60% for product n°2 ().
The cross-price elasticity is not the final result of our solution, but rather a necessary intermediate step. Nevertheless, it could still be leveraged independently from this solution as a powerful information to:
- Determine whether and how products compete with one another, then assess this competition using quantifiable and understandable metrics.
- Considering the product's competitors, make an educated guess as to the potential impact of discounts or marketing initiatives.
By doing this, we assume that we are in elastic conditions, meaning that a price variation corresponds to a proportional change in the demand.
This is true for most businesses, from retail to large distribution.
Nevertheless, not all businesses work under this assumption. They are referred to as inelastic. In those cases, this strategy is not applicable.
3. Forecast expected quantities
While DML gives us an estimate of the causal effect θ(X) of price manipulation over demand, our final goal is the prediction of the set of prices that will maximize the business objectives.
In Causal Machine Learning, these prices are referred to as counterfactual, as they are directly produced by the intervention.
As we cannot predict directly the counterfactual prices and demand, we can write them as a combination of observational quantities and a contribution due to the intervention:
where:
- : counterfactual volume. The subscripts stands for quantities.
- : observational volume. The subscripts stands for quantities.
- : additional effect due to the treatment.
Therefore, the next step in our strategy is forecasting the observational quantities.
Unlike counterfactual ones, observational quantities are based entirely on previous values. They refer to a condition in which the treatment is not applied, therefore no price manipulation is assumed.
As a result, we can use forecasting techniques to predict the observational quantities.
In a real dynamic pricing application, we should forecast demands and prices for multiple products. For this reason, we could leverage the advantages provided by DeepAR by Salinas et al. (2019) from Amazon Research.
Indeed, in situations with many similar time series across a cross-sectional unit, such as the demand for a set of products over the past 12 months, one can benefit from training a single model jointly over all of the time series. DeepAR uses this approach, and, in such scenarios, it outperforms the standard ARIMA and ETS methods.
Conveniently, DeepAr is already available as Amazon SageMaker estimator. The detailed implementation for the forecast of observational quantities is not the main scope of this notebook. For this reason, we will limit ourselves in considering the last price and volume values in our time series as example. Still, DeepAR would provide significant advantages where the demand and prices of multiple products should be predicted. One may find a detailed, end-to-end tutorial on using DeepAR in a SageMaker Studio Notebook here.
4. Optimization
We previously defined the counterfactual quantities as a combination of observational ones and an intervention effect:
By using the definition of price elasticity, we can rewrite this expression for the i-th product as:
In particular:
- : counterfactual volume/demand of the i-th product.
- : observational volume/demand of the i-th product (known from forecasting).
- : effect of the variation of price of the i-th product over the demand of the j-th product (cross-price elasticity known from DML).
- : counterfactual price of the j-th product (known from forecasting).
- : observational price of the j-th product (known from forecasting).
The previous analytical expression can be plugged in any objective function such as profit and optimized with respect to the counterfactual prices.
Furthermore, we can impose constraints to the optimization problem modifying the feasible region in which the counterfactual prices are evaluated. Custom policies, regulations, and business rules can all be used as optimization constraints. Our framework provides the flexibility to accommodate any type and number of constraints.
The following sections provide the complete derivation for popular objective functions: profit and revenue.
4.1 Objective functions
4.1.1 Revenue
Define revenue as:
Formulate the problem of finding the set of prices that maximize the revenue as an optimization task:
In matrix form, posed as minimization problem:
In conclusion, the problem can be framed as follow:
- Objective function :
- Gradient :
- Hessian :
4.1.2 Profit
Define profit as:
Assumption: cost is independent from any potential modification of price:
Formulate the problem of finding the set of prices that maximize the profit as an optimization task:
In matrix form, posed as minimization problem:
In conclusion, the problem can be framed as follow:
- Objective function :
- Gradient :
- Hessian :
4.2 Optimization constraints
4.2.1 Linear constraints
We need to ensure that the feasible region will only account for positive counterfactual volumes. This can be achieved with a linear constraint, imposing:
Through price elasticity, we can formulate this condition as:
Therefore, we can write the following linear constraint:
where:
4.2.2 Bounds
Bounds allow to estimate the objective function only between a specified range for the input variables.
In our case, we want the feasible region to be described by minimum and maximum prices.
The application of such a perimeter allows to avoid sudden and highly different prices in short spans of time, in case the optimum price is too different from the one currently applied.
4.3 Solution
We found the set of counterfactual prices that maximize revenue.
We can observe the feasible region and the found minimizer:
The following code snippet plots the objective function with respect to the counterfactual prices of two products.
When reproducing this notebook, please consider that a 3-D plot of the objective function is only possible when considering two products, one for each axis.
If more than two products have been generated in the previous passages, the following plot is not possible.
Let us estimate counterfactual volumes and compare counterfactual and observational revenue:
5. Simulation
Optimization provides a set of prices that maximizes a certain business goal (profit, revenue, ..). Instead, simulation works backwards.
Simulation allows to apply any custom price, and verify what would happen to the business goal. In this way, business users are free to experiment different scenarios and further adapt the solution.
In conclusion, the simulation utility provides flexibility to business users answering the question: What would happen to revenue or profit if I applied these prices?
5.1 Procedure
Decide, for each product, the price to be applied during the simulation:
Estimate the volumes resulting from this application:
Calculate resulting counterfactual profit :
Compare it with the expected (observational profit) and verify a gain or loss:
Notebook CI Test Results
This notebook was tested in multiple regions. The test results are as follows, except for us-west-2 which is shown at the top of the notebook.














