Thursday, February 29, 2024

Dynamic Pricing with Multi-Armed Bandit: Studying by Doing | by Massimiliano Costacurta | Aug, 2023

Must read


Making use of Reinforcement Studying methods to real-world use instances, particularly in dynamic pricing, can reveal many surprises

Towards Data Science
Photograph by Markus Spiske on Unsplash

Within the huge world of decision-making issues, one dilemma is especially owned by Reinforcement Studying methods: exploration versus exploitation. Think about strolling right into a on line casino with rows of slot machines (often known as “one-armed bandits”) the place every machine pays out a unique, unknown reward. Do you discover and play every machine to find which one has the best payout, or do you stick to at least one machine, hoping it’s the jackpot? This metaphorical situation underpins the idea of the Multi-armed Bandit (MAB) downside. The target is to discover a technique that maximizes the rewards over a sequence of performs. Whereas exploration affords new insights, exploitation leverages the data you already possess.

Now, transpose this precept to dynamic pricing in a retail situation. Suppose you might be an e-commerce retailer proprietor with a brand new product. You aren’t sure about its optimum promoting worth. How do you set a worth that maximizes your income? Must you discover completely different costs to know buyer willingness to pay, or must you exploit a worth that has been performing nicely traditionally? Dynamic pricing is basically a MAB downside in disguise. At every time step, each candidate worth level might be seen as an “arm” of a slot machine and the income generated from that worth is its “reward.” One other solution to see that is that the target of dynamic pricing is to swiftly and precisely measure how a buyer base’s demand reacts to various worth factors. In less complicated phrases, the intention is to pinpoint the demand curve that finest mirrors buyer conduct.

On this article, we’ll discover 4 Multi-armed Bandit algorithms to guage their efficacy in opposition to a well-defined (although not easy) demand curve. We’ll then dissect the first strengths and limitations of every algorithm and delve into the important thing metrics which are instrumental in gauging their efficiency.

Historically, demand curves in economics describe the connection between the worth of a product and the amount of the product that buyers are prepared to purchase. They typically slope downwards, representing the frequent statement that as worth rises, demand usually falls, and vice-versa. Consider widespread merchandise corresponding to smartphones or live performance tickets. If costs are lowered, extra folks have a tendency to purchase, but when costs skyrocket, even the ardent followers may assume twice.

But in our context, we’ll mannequin the demand curve barely in another way: we’re placing worth in opposition to likelihood. Why? As a result of in dynamic pricing situations, particularly digital items or providers, it’s typically extra significant to assume when it comes to the probability of a sale at a given worth than to invest on actual portions. In such environments, every pricing try might be seen as an exploration of the probability of success (or buy), which might be simply modeled as a Bernoulli random variable with a likelihood p relying on a given take a look at worth.

Right here’s the place it will get significantly attention-grabbing: whereas intuitively one may assume the duty of our Multi-armed Bandit algorithms is to unearth that ideally suited worth the place the likelihood of buy is highest, it’s not fairly so easy. In truth, our final objective is to maximise the income (or the margin). This implies we’re not looking for the worth that will get the most individuals to click on ‘purchase’ — we’re looking for the worth that, when multiplied by its related buy likelihood, offers the best anticipated return. Think about setting a excessive worth that fewer folks purchase, however every sale generates important income. On the flip facet, a really low worth may appeal to extra patrons, however the whole income may nonetheless be decrease than the excessive worth situation. So, in our context, speaking in regards to the ‘demand curve’ is considerably unconventional, as our goal curve will primarily signify the likelihood of buy somewhat than the demand immediately.

Now, attending to the mathematics, let’s begin by saying that shopper conduct, particularly when coping with worth sensitivity, isn’t all the time linear. A linear mannequin may recommend that for each incremental enhance in worth, there’s a continuing decrement in demand. In actuality, this relationship is usually extra complicated and nonlinear. One solution to mannequin this conduct is through the use of logistic features, which may seize this nuanced relationship extra successfully. Our chosen mannequin for the demand curve is then:

Right here, a denotes the utmost achievable likelihood of buy, whereas b modulates the sensitivity of the demand curve in opposition to worth adjustments. The next worth of b means a steeper curve, approaching extra quickly to decrease buy chances as the worth will increase.

4 examples of demand curves with completely different mixtures of parameters a and b

For any given worth level, we’ll be then capable of get hold of an related buy likelihood, p. We are able to then enter p right into a Bernoulli random variable generator to simulate the response of a buyer to a specific worth proposal. In different phrases, given a worth, we will simply emulate our reward operate.

Subsequent, we will multiply this operate by the worth with the intention to get the anticipated income for a given worth level:

Unsurprisingly, this operate doesn’t attain its most in correspondence with the best likelihood. Additionally, the worth related to the utmost doesn’t rely on the worth of the parameter a, whereas the utmost anticipated return does.

Anticipated income curves with associated maxima

With some recollection from calculus, we will additionally derive the formulation for the by-product (you’ll want to make use of a mix of each the product and the chain rule). It’s not precisely a soothing train, however it’s nothing too difficult. Right here is the analytical expression of the by-product of the anticipated income:

This by-product permits us to search out the precise worth that maximizes our anticipated income curve. In different phrases, through the use of this particular formulation in tandem with some numerical algorithms, we will simply decide the worth that units it to 0. This, in flip, is the worth that maximizes the anticipated income.

And that is precisely what we want, since by fixing the values of a and b, we’ll instantly know the goal worth that our bandits must discover. Coding this in Python is a matter of some strains of code:

For our use case, we’ll set a = 2 and b = 0.042, which can give us a goal worth of about 30.44, related to an optimum likelihood of 0.436 ( → optimum common reward is 30.44*0.436=13.26). This worth is clearly unknown basically and it’s precisely the worth that our Multi-armed Bandit algorithms will search.

Now that we’ve recognized our targets, it’s time to discover numerous methods for testing and analyzing their efficiency, strengths, and weaknesses. Whereas a number of algorithms exist in MAB literature, in relation to real-world situations, 4 main methods (together with their variations) predominantly type the spine. On this part, we’ll present a short overview of those methods. We assume the reader has a foundational understanding of them; nevertheless, for these excited by a extra in-depth exploration, references are offered on the finish of the article. After introducing every algorithm, we’ll additionally current its Python implementation. Though every algorithm possesses its distinctive set of parameters, all of them generally make the most of one key enter: the arm_avg_reward vector. This vector denotes the common reward garnered from every arm (or motion/worth) as much as the present time step t. This crucial enter guides all of the algorithms in making knowledgeable choices in regards to the subsequent worth setting.

The algorithms I’m going to use to our dynamic pricing downside are the next:

Grasping: This technique is like all the time going again to the machine that gave you probably the most cash the primary few instances you performed. After attempting out every machine a bit, it sticks with the one which appeared the very best. However there could be an issue. What if that machine was simply fortunate firstly? The Grasping technique may miss out on higher choices. On the brilliant facet, the code implementation is actually easy:

It’s important to distinguish the preliminary situation (when all rewards are 0) from the common one. Typically, you’ll discover solely the ‘else’ half carried out, which certainly works even when all rewards are at 0. But, this method can result in a bias towards the primary component. When you make this oversight, you may find yourself paying that bias, significantly if the optimum reward occurs to be tied to the primary arm (sure, I’ve been there). The Grasping method is often the least-performing one and we’ll primarily use it as our efficiency baseline.

ϵ-greedy: The ε-greedy (epsilon-greedy) algorithm is a modification to deal with the primary disadvantage of the grasping method. It introduces a likelihood ε (epsilon), usually a small worth, to pick a random arm, selling exploration. With a likelihood 1−ε, it chooses the arm with the best estimated reward, favoring exploitation. By balancing between random exploration and exploitation of identified rewards, the ε-greedy technique goals to attain higher long-term returns in comparison with purely grasping strategies. Once more, the implementation is instant, it’s merely an extra ‘if’ on high of the Grasping code.

UCB1 (Higher Confidence Sure): The UCB1 technique is sort of a curious explorer looking for the very best restaurant in a brand new metropolis. Whereas there’s a favourite spot they’ve loved, the attract of doubtless discovering a good higher place grows with every passing day. In our context, UCB1 combines the rewards of identified worth factors with the uncertainty of these much less explored. Mathematically, this steadiness is achieved by a formulation: the common reward of a worth level plus an “uncertainty bonus” based mostly on how lengthy because it was final tried. This bonus is calculated as

and represents the “rising curiosity” in regards to the untried worth. The hyperparameter C controls the steadiness between exploitation and exploration, with greater values of C encouraging extra exploration of less-sampled arms. By all the time choosing the worth with the best mixed worth of identified reward and curiosity bonus, UCB1 ensures a mixture of sticking to what’s identified and venturing into the unknown, aiming to uncover the optimum worth level for optimum income. I’ll begin with the by-the-book implementation of this method, however we’ll quickly see that we have to tweak it a bit.

Thompson Sampling: This Bayesian method addresses the exploration-exploitation dilemma by probabilistically choosing arms based mostly on their posterior reward distributions. When these rewards adhere to a Bernoulli distribution, representing binary outcomes like success/failure, Thompson Sampling (TS) employs the Beta distribution as a conjugate prior (see this desk for reference). Initiating with a non-informative Beta(1,1) prior for each arm, the algorithm updates the distribution’s parameters upon observing rewards: a hit will increase the alpha parameter, whereas a failure augments the beta. Throughout every play, TS attracts from the present Beta distribution of every arm and opts for the one with the highest sampled worth. This technique permits TS to dynamically modify based mostly on gathered rewards, adeptly balancing between the exploration of unsure arms and the exploitation of these identified to be rewarding. In our particular situation, though the foundational reward operate follows a Bernoulli distribution (1 for a purchase order and 0 for a missed buy), the precise reward of curiosity is the product of this primary reward and the present worth beneath take a look at. Therefore, our implementation of TS will want a slight modification (which will even introduce some surprises).

The change is definitely fairly easy: to find out probably the most promising subsequent arm, samples extracted from the posterior estimates are multiplied by their respective worth factors (line 3). This modification ensures choices are anchored on the anticipated common income, shifting the main target from the best buy likelihood.

At this level, having gathered all the important thing substances to assemble a simulation evaluating the efficiency of the 4 algorithms in our dynamic pricing context, we should ask ourselves: what precisely will we be measuring? The metrics we select are pivotal, as they may information us within the means of each evaluating and enhancing the algorithm implementation. On this endeavor, I’m zeroing in on three key indicators:

  1. Remorse: This metric measures the distinction between the reward obtained by the chosen motion and the reward that might have been obtained by taking the very best motion. Mathematically, remorse at time t is given by: Remorse(t)=Optimum Reward(t)−Precise Reward(t). Remorse, when amassed over time, gives perception into how a lot we’ve “misplaced” by not all the time selecting the very best motion. It’s most well-liked over cumulative reward as a result of it gives a clearer indication of the algorithm’s efficiency relative to the optimum situation. Ideally, a remorse worth near 0 signifies proximity to optimum decision-making.
  2. Reactivity: This metric gauges the pace at which an algorithm approaches a goal common reward. Primarily, it’s a measure of the algorithm’s adaptability and studying effectivity. The faster an algorithm can obtain the specified common reward, the extra reactive it’s, implying a swifter adjustment to the optimum worth level. In our case the goal reward is about at 95% of the optimum common reward, which is 13.26. Nonetheless, preliminary steps can exhibit excessive variability. As an example, a fortunate early alternative may lead to a hit from a low likelihood arm related to a excessive worth, shortly reaching the brink. As a result of such fluctuations, I’ve opted for a stricter definition of reactivity: the variety of steps required to realize 95% of the optimum common reward ten instances, excluding the preliminary 100 steps.
  3. Arms Allocation: This means the frequency with which every algorithm makes use of the accessible arms. Offered as a share, it reveals the algorithm’s propensity to pick every arm over time. Ideally, for probably the most environment friendly pricing technique, we’d need an algorithm to allocate 100% of its decisions to the best-performing arm and 0% to the remainder. Such an allocation would inherently result in a remorse worth of 0, denoting optimum efficiency.

Evaluating MAB algorithms poses challenges because of the extremely stochastic nature of their outcomes. Because of this due to the inherent randomness in figuring out portions, the outcomes can tremendously range from one run to a different. For a sturdy analysis, the simplest method is to execute the goal simulation a number of instances, accumulate the outcomes and metrics from every simulation, after which compute the common.

The preliminary step includes making a operate to simulate the decision-making course of. This operate will implement the suggestions loop represented within the under picture.

Suggestions loop carried out within the simulation operate

That is the implementation of the simulation loop:

The inputs to this operate are:

  • costs: An inventory of candidate costs we want to take a look at (primarily our “arms”).
  • nstep: The entire variety of steps within the simulation.
  • technique: The algorithm we intention to check for making choices on the subsequent worth.

Lastly, we have to write the code for the outer loop. For each goal technique, this loop will name run_simulation a number of instances, gather and combination the outcomes from every execution, after which show the outcomes.

For our evaluation, we’ll use the next configuration parameters:

  • costs: Our worth candidates → [20, 30, 40, 50, 60]
  • nstep: Variety of time steps for each simulation → 10000
  • nepoch: Variety of simulation executions → 1000

Moreover, by setting our worth candidates, we will promptly get hold of the related buy chances, that are (roughly) [0.60, 0.44, 0.31, 0.22, 0.15].

After working the simulation we’re lastly capable of see some outcomes. Let’s begin from the plot of the cumulative remorse:

From the graph, we will see that TS is the winner when it comes to imply cumulative remorse, however it takes round 7,500 steps to surpass ε-greedy. However, we’ve a transparent loser, which is UCB1. In its primary configuration, it primarily performs on par with the grasping method (we’ll get again to this later). Let’s attempt to perceive the outcomes higher by exploring the opposite accessible metrics. In all 4 instances, the reactivity reveals very giant customary deviations, so we’ll concentrate on the median values as an alternative of the means, as they’re extra immune to outliers.

The preliminary statement from the plots reveals that whereas TS surpasses ε-greedy when it comes to the imply, it barely lags behind when it comes to the median. Nonetheless, its customary deviation is smaller. Significantly attention-grabbing is the reactivity bar plot, which reveals how TS struggles to quickly obtain a good common reward. At first, this was counterintuitive to me, however the mechanism behind TS on this situation clarified issues. We beforehand talked about that TS estimates buy chances. But, choices are made based mostly on the product of those chances and the costs. Having information of the true chances (that, as talked about, are [0.60, 0.44, 0.31, 0.22, 0.15]) permits us to calculate the anticipated rewards TS is actively navigating: [12.06, 13.25, 12.56, 10.90, 8.93]. In essence, though the underlying chances differ significantly, the anticipated income values are comparatively shut from its perspective, particularly in proximity to the optimum worth. This implies TS requires extra time to discern the optimum arm. Whereas TS stays the top-performing algorithm (and its median finally drops under that of the ε-greedy one if the simulation is extended), it calls for an extended interval to establish the very best technique on this context. Beneath, the arm allocation pies present how TS and ε-greedy do fairly nicely in figuring out the very best arm (worth=30) and utilizing it more often than not in the course of the simulation.

Now let’s get again to UCB1. Remorse and reactivity verify that it’s mainly performing as a totally exploitative algorithm: fast to get degree of common reward however with huge remorse and excessive variability of the end result. If we take a look at the arm allocations that’s much more clear. UCB1 is barely barely smarter than the Grasping method as a result of it focuses extra on the three arms with greater anticipated rewards (costs 20, 30, and 40). Nonetheless, it primarily doesn’t discover in any respect.

Enter hyperparameter tuning. It’s clear that we have to decide the optimum worth of the load C that balances exploration and exploitation. Step one is to switch the UCB1 code.

On this up to date code, I’ve included the choice to normalize the common reward earlier than including the “uncertainty bonus”, which is weighted by the hyperparameter C. The rationale for that is to permit for a constant search vary for the very best hyperparameter (say 0.5–1.5). With out this normalization, we might obtain related outcomes, however the search interval would wish changes based mostly on the vary of values we’re coping with every time. I’ll spare you the boredom of discovering the very best C worth; it may be simply decided by a grid search. It seems that the optimum worth is 0.7. Now, let’s rerun the simulation and study the outcomes.

That’s fairly the plot twist, isn’t it? Now, UCB1 is clearly the very best algorithm. Even when it comes to reactivity, it has solely barely deteriorated in comparison with the earlier rating.

Moreover, from the attitude of arm allocation, UCB1 is now the undisputed chief.

  • Concept vs. Expertise: Beginning with book-based studying is a necessary first step when delving into new subjects. Nonetheless, the earlier you immerse your self in hands-on experiences, the quicker you’ll remodel info into information. The nuances, subtleties, and nook instances you encounter when making use of algorithms to real-world use instances will supply insights far past any knowledge science ebook you may learn.
  • Know Your Metrics and Benchmarks: When you can’t measure what you’re doing, you’ll be able to’t enhance it. By no means start any implementations with out understanding the metrics you plan to make use of. Had I solely thought-about remorse curves, I might need concluded, “UCB1 doesn’t work.” By evaluating different metrics, particularly arm allocation, it turned evident that the algorithm merely wasn’t exploring sufficiently.
  • No One-Measurement-Suits-All options: Whereas UCB1 emerged because the best choice in our evaluation, it doesn’t indicate it’s the common answer in your dynamic pricing problem. On this situation, tuning was easy as a result of we knew the optimum worth we sought. In actual life, conditions are by no means so clear-cut. Do you possess sufficient area information or the means to check and modify your exploration issue for the UCB1 algorithm? Maybe you’d lean in direction of a reliably efficient choice like ε-greedy that guarantees instant outcomes. Or, you could be managing a bustling e-commerce platform, showcasing a product 10000 instances per hour, and also you’re prepared to be affected person, assured that Thompson Sampling will attain the utmost cumulative reward finally. Yeah, life ain’t simple.

Lastly, let me say that if this evaluation appeared daunting, sadly, it already represents a really simplified scenario. In real-world dynamic pricing, costs and buy chances don’t exist in a vacuum — they really exist in ever-changing environments and so they’re influenced by numerous components. For instance, it’s extremely inconceivable that buy likelihood stays constant all year long, throughout all buyer demographics and areas. In different phrases, to optimize pricing choices, we should take into account our prospects’ contexts. This consideration would be the point of interest of my subsequent article, the place I’ll delve deeper into the issue by integrating buyer info and discussing Contextual Bandits. So, keep tuned!



Supply hyperlink

More articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest article