A friend of mine recently showed me John Cook's A Bayesian view of Amazon Resellers as a cute example of Bayesian analysis on a problem where we have very different proportions of data between parameters. I really like this mini-problem, and so wanted to share a solution that softens the most painful step.

# Problem

Suppose we are debating which Amazon reseller to buy a used book from, concerned about each candidate's reliability as a seller.

- Seller A has 90 positive reviews, and 10 negative reviews.
- Seller B has 2 positive reviews, and 0 negative reviews.

Which seller should we buy from?

# Analysis

Since we hardly have a wealth of data points, especially for Seller B, a Bayesian approach seems suitable. Letting $$\theta_{A}$$ and $$\theta_{B}$$ be the probabilities of satisfaction with each reseller respectively, we'll assume that positive and negative ratings are equally likely and opt for a uniform $$\text{Beta}(1, 1)$$ prior. Consequently, our posteriors become $$\text{Beta}(91, 11)$$ and $$\text{Beta}(3, 1)$$ respectively.

Now here is where it gets a little bit tricky. Given our knowledge of the posteriors, we want to find $$

p(\theta_{A} > \theta_{B} | \mathcal{D}),

$$ where we have conditioned on our data. How do we find this quantity though?

Cook omits the details in his article, but links to a paper on Exact Calculation of Beta Inequalities. In short, it is possible to solve for this probability either analytically or using some explicit numerical integration, but these approaches involve writing down a little (or a lot) of actual math.

# Keeping it simple

Given that we are really just interested in our decision at hand based on this value and not its precise quantity, why not just use a Monte Carlo method for numerical integration! This has the benefit of being trivially easy to do, and we don't have to even think about the math. We have Beta distributions that we can sample from easily, and the parameters are independent in their posteriors. So let's just take $$10^7$$ samples from each posterior and count the fraction of those where $$\theta_{A} > \theta_{B}$$.

Doing so in Python:

```
import numpy as np
post_a_samples = np.random.beta(91, 11, 10**7)
post_b_samples = np.random.beta(3, 1, 10**7)
post_a_larger = np.mean(post_a_samples > post_b_samples)
print(f'p(theta_a > theta_b | D) is approx. {post_a_larger}')
```

This gives me a result of 0.7124. The analytic result that Cook provides is 0.713, so we seem awfully close! This would suggest that despite Seller B having no negative reviews yet, counter to our intuition we ought to buy from Seller A.

The choice of prior is perhaps not ideal for this problem, but in any case I thought it was a neat example and the solution a good use case for handy Monte Carlo sampling.

explorations in machine learning, programming, and algorithm design