Adding Error Bars to 5-Star Reviews: A Bayesian Approach

Perhaps not surprisingly, I’m going to use the Bayes’ theorem to infer the true rating of the socks using the observational data as evidence. We want to find the true rating of each product, given all the user reviews:

The first term in the numerator on the right-hand side is the prior (our initial belief on the distribution of true rating) and the second term is the likelihood. To be conservative, we can use a flat prior for the rating, that is to say, we assume that before seeing any of the reviews it’s equally likely for the socks to have any rating between 1 to 5 stars. The likelihood for each individual review is a binomial distribution shown in Eq. 1 above, so to construct the full likelihood we only need to replace each k with the observed review score and multiply everything together. Remember that there is a one-to-one mapping between the bias of the coin p (see Eq 1.) and true rating (Eq. 2) and I’m using them interchangeably.

In order to find the probability distribution of the true rating of each item, I’m going to use PyMC3 to sample their posteriors. The following function takes in an instance of the Sock object and returns the MCMC samples from the posterior in Eq. 2. Going over the details of how PyMC3 works is beyond the scope of this article but feel free to ask questions in the comment section or post issues on the Github Repo if anything is confusing.