Thompson Sampling for Contextual Multi-arm bandit
Multi-arm Bandit problem
Multi-arm bandit is a colorful name for a problem we daily face in our lives given choices. The problem is how to choose given multitude of options. Lets make the problem concrete. Assume that its Friday evening and you are planning to go to a fancy restaurant. Should you try your favorite restaurant or try some new restaurant. If you go for your favorite restaurant then you are
exploiting your past knowledge and if you go for a new restaurant, then you are
exploring which may be useful in future to find a better restaurant than your present restaurant. Unfortunately, it is also possible that you may come across some very bad restaurants during this exploration. The main question is how to balance the tradeoff between exploration and exploitation.
Thompson Sampling essentially provides a way to solve this problem.
Assume that there are two restaurants and . Assume that restaurant on an average times align to your taste and restaurant on an average times align to your taste. In other words, if you go to restaurant , times, then you will like the experience times and if you go to restaurants , times, then you will like the experience times where and are some numbers between and . Unfortunately these numbers and are unknown to you so you cannot know which restaurant you should choose. You go to restaurant very often and you want to maximize your chances of having good experiences in the restaurants during your life time. How should you choose these restaurants based on your experiences so far? Thompson Sampling comes to rescue here.
The idea behind Thompson Sampling is inspired by Bayesian Inference. Lets try to present the main idea behind Thompson Sampling as succinctly as possible below:
- Lets assume that we have priors on unknown parameters that affects the reward for our bandit problem. In our restaurant example, the parameters are and . The reward for bandit problem is essentially and which are the measure of goodness of the restaurant.
- We sample a value of unknown parameters from this prior distribution.
- We compute the reward from these sampled parameters.
- We choose the actions which gives the highest reward.
- We observe the actual reward gathered by taking our action.
- We update the priors on the parameters using the observed reward.
- We repeat the above procedure using the new posterior distribution.
Thompson Sampling in action
Now lets apply the Thompson Sampling to our restaurant hunting problem and see how does it help us in resolving the problem.
Since we don’t know anything about the restaurants initially, we can assume that the and parameters are uniformly distributed between and . Note that we can also write a uniform distribution between and as a beta-distribution . Please read more about the beta-distribution here which is going to be helpful in understanding the upcoming text.
We sample a value for and a value for from their prior distribution. We choose the restaurant having the higher values.
Assume that you chose the restaurant in the previous step. You go to the restaurant and if you have the good experience in the restaurant, you give it reward otherwise you would give it a reward.
- Now we need to update the distribution for based on the reward that we observed in the previous step. Since our prior distribution was the beta-distribution, it is easy to do as described below:
If your prior distribution for is you receive the reward for going to restaurant then the posterior probability distribution for is .
- Now to choose a restaurant second time, you sample new values for and but you use the updated probability distribution for the restaurant that was chosen in the first trial and you keep going on like that to choose restaurants.
Now lets see how does Thompson Sampling helps us in finding the best restaurant. We will simulate this behavior. For this simulation, we will assume that the true value of is and true value of is . We would like to see that as time progress, Thompson Sampling would recommend us restaurant more and more.
In the following figure, we plot how Thompson sampling chooses between restaurant and restaurant . Each point is telling us the fraction of time restaurant and restaurant were chosen by Thompson sampling in the previous trials. It is interesting to see that in this case Thompson sampling figures out pretty quickly (approximately trials) that it should stick with restaurant for better experience.
It is interesting to note that Thompson Sampling is doing a good job in finding the best restaurant when we have to find the best restaurants only among two restaurants. An obvious question comes, is Thompson Sampling easily extendable. In other words, can it gives us a similar performance when we had to find the best restaurant among restaurants where . To investigate this question, we did a simulation as you can see in the following figure.
For the above simulation, we assume that the true thetas parameter of Thompson Sampling are and for restaurant A, B, C, and D respectively. As you can see in the above figure, Thompson Sampling is able to recommend the best restaurant consistently although it is taking approximately trials for Thompson Sampling to consistently make this claim.
At this juncture of this blog, we can ask some important questions as following:
- Can we change Thompson Sampling somehow to increase the convergence speed of Thompson Sampling?
- Can we utilize some side information (such as recipe of the restaurants, information about chefs etc) to decrease the learning time for Thompson Sampling?
Contextual Thompson Sampling is precisely the answer of the above questions that will be the focus of the rest of the blog.
Contextual Thompson Sampling
Following the previous case (Simple Thompson Sampling), we will first use a mathematical abstraction to explain Contextual Thompson Sampling. Further, we will elaborate this concept with the help of our ongoing restaurant search problem.
As is suggested in the name, in Contextual Thompson Sampling there is a context that we will use to select arms in a multi-arm bandit problem. The context vector encapsulates all the side information that we think can be useful for determining the best arm. Lets denote a context vector by the symbol . For each arm, we want to determine a set of parameters that can tell us how to choose an arm given a context. We will assume that we also know a function that tells us the relationship between parameter vector , context vector , and expected reward of the arm. In essence,
You may say that it is difficult to know the function but we can always choose to be a high capacity neural network model that can model even very complex relationship between and . Although training time of such a high capacity neural network model can be long because it would require lot of data.
The training process of Contextual Thompson Sampling is similar to Simple Thompson Sampling.
- At time , we observe a context .
- For each arm , we assume that is distributed according to the probability distribution where are the parameters of the distribution.
- For each arm, we will start with some initial value of . Lets call this initial value .
- We will sample according to the distribution for each arm .
- We will compute the expected reward for each arm.
- We choose the arm that has the maximum value of
- We observe the actual reward for playing this arm.
- We repeat the above process times.
- Subsequently, we update the parameters for each arm using the maximum likelihood estimator of after observing the reward .
- We keep repeating the above process until some predefined convergence criterion is met.
Contextual Thompson Sampling for Restaurant Searching Problem
Lets first consider the context vector. Assume that the context vector consists of following elements: day of the week, and weather. Consider the following property of each of the context field:
Day of week: This feature represents the day that we visit the restaurant. We are assuming that if there is some impact of day of week on us liking the restaurant that Thompson Sampling will be successfully able to capture this impact.
Weather: This feature represents the weather we visit the restaurant. Assume that this field can be of one of two types: hot weather or cold weather.
Now lets make the most important assumption. Assume that we like restaurant when the weather is hot and we like restaurant when weather is cold. In other words, only the weather field in the context vector that gives us the significant information about us liking a restaurant.
In Thompson Sampling, the context vector consists of day of week and weather. We use the one-hot encoded representation to represent this context vector. So our context vector for the restaurant search problem is dimensional vector. The first seven dimension out of these dimensions represent day of week. The next dimension represent the weather. For example, if the context vector is , then it implies that we are visiting the restaurant in a hot day where day of week is Monday.
We further assume that the expected reward of a restaurant is a linear function of context vector and the restaurant parameters ( and ). In mathematical terms,
We further assume that initial distribution of parameters and for restaurant and restaurant are normally distributed with and respectively. Now our goal is to find the posterior distribution for and after looking at our observations. Fortunately, it is easy to do with the help of Laplace Approximation. In short, we are applying the Bayesian Logistic Regression in a sequence. This is a good tutorial on Laplace Approximation.
But in short, the result are as following:
- Assume that we visited restaurant , times.
- The context for these times was which is a dimensional vector.
- Our experience at these times is denoted by which is a binary number or .
- The formula to update the mean parameter for restaurant is
- The formula to update the variance parameter is as following