The world’s leading publication for data science, AI, and ML professionals.

Bayesian Price Optimization with PyMC3

PyMC3, Killer Visualizations, and Probabilistic Decision Making

Credit: Pexels.com
Credit: Pexels.com

In this article, we’re going to explore price optimization from the Bayesian perspective. So what is price optimization?

It’s optimizing the price of a good or service given costs and revenue. Revenue is generally subject to the "demand curve," which is simply a relation between price and units demanded by consumers. A price too low would attract anyone and everyone who’s in the market but wouldn’t bring in enough revenue to outweigh the costs. But the inverse is also true. If the price is too high, so few customers (if any) will be attracted; so costs could outweigh revenue all the same. Hence the task of optimization.

As we’ll see the costs could be any function of the units demanded. This could be purchase, storage, transportation or any basically any cost related to the units demanded. In this case, we’ll make a simple assumption for illustrative purposes, but the function could be changed to meet any arbitrary cost that needs to be accounted for.

So why Bayesian?

The Frequentist approach uses confidence intervals. This means we’re X% confident that the true value of some parameter falls in a given interval. But what about our confidence within that interval? The Frequentist approach doesn’t help us much here. And to make matters worse, parameters are often correlated with one another.

The Bayesian approach, on the other hand, models the joint distribution over all parameters. Sure, we can make an X% credible intervals – but our analysis gives us the shape that the parameters’ joint distribution follows. When we want to simulate how revenue, cost, and profits respond to changes in price, confidence intervals just aren’t transparent enough for out purposes.

Data Simulation

Let’s say we’re working a lemonade stand on a hot summer day and we’re selling quarts of lemonade. We’ve got a cooler, which we rented for a fixed fee of 50 dollars and it costs us 1.5 dollars in ice for each quart of lemonade – that’s ridiculous, I know. But it’s a cost constraint, which we’ll implement later for illustrative purpose so that you can swap it out for a cost function of your own.

First, we need a demand curve; we’ll simulate our own for simplicity. There are multiple approaches to demand curve modeling. One that tends to work really well incorporates exponential decay. As the price approaches zero, the predicted demand would be infinitely high, whereas if the price were arbitrarily (infinitely) high, the demand would asymptotically approach zero.

How did we get this curve? Well, I’ve just spilled the beans, it’s simulated! But let’s say that you’ve offered quarts of lemonade at various prices and you’ve observed the following pattern. In actuality, using discounts is a science of its own. You get clarity on the demand curve as well as estimating efficacy of Marketing channels. We won’t get into discounts and pricing experiments here, hence the simulation.

Credit: Image by author
Credit: Image by author

Bayesian Linear Model

We’re essentially just uncovering the very parameters that we set. A linear relationship exists between price and log units demanded (since demand exhibits exponential decay.)

Why a Bayesian model? Rather than inferring confidence intervals around the individual parameters, intercept and slope, we can infer the joint distribution over them. This is excellent! It allows us to infer distributions over things like revenue, costs, and profits.

A Frequentist approach would use a single slope and intercept, yielding point estimates on revenue, cost, or profits. This would leave us unable to articulate our certainty on optimal decision making.

As you can see, PyMC3 syntax is super user friendly and easy to follow along. If you’re already familiar with Bayesian analysis, you’ll note the priors I’ve placed on m, b, and s, our slope, intercept, and noise term. These effectively regularize the search space. The slope and intercept could be positive or negative, so I’ve used normal priors. However, the noise (error) can never be negative, hence I’ve chosen the exponential distribution.

Posterior Predictive Plot

Let’s see the Bayesian approach in action with the posterior predictive distribution. One of the really useful features of Bayesian analysis is the ability to generate a posterior predictive distribution. This means we can visualize what our model thinks is (and what isn’t!) likely. We sample from the joint distribution over slopes and intercepts, infer predictions over units demanded given an array of prices, and plot. The plotted lines succinctly capture the variance around our price-demand relationship!

Credit: Image by author
Credit: Image by author

Revenue and Profit

The last step(s) boil down to defining a cost function and inferring predictions over demand, revenue, cost, and profit, which we treat as random variables with distributions of their own.

Revenue is simple, it’s the price multiplied by the units demanded. And I’ve decided arbitrarily that the cost is $50 overhead plus $1.5/unit. However, this cost function could be swapped out for any function of the units demanded.

Credit: Image by author
Credit: Image by author

Above, we can see that the price $5.5/unit corresponds to the maximized profits. Another way visualization below captures this relationship in 2D. The hot center shows that $5.5 price could bring in $95–105 total profit given demand, revenue and costs.

Credit: Image by author
Credit: Image by author

This last graph shows the relationship between price and profit, which is the penultimate relationship of interest in price optimization. We now know that we can sell some good or service for about 5.5 dollars, generating 95–105 dollars in profit.

There you have it! I hope I’ve persuaded you to at least try the Bayesian approach. A little extra upfront work gives you a more robust framework to understand your decision space.

Parting notes

Dear reader, I’ve noticed a pattern where users add my stories to their lists but don’t clap or subscribe. Please – if you like my content, recognize it as useful by clapping, commenting, and subscribing. This (1) helps me prioritize what content to create next for you and (2) helps other Medium users find my content. Thank you!


Related Articles