OPUS CODE Articles rating score Calculation

• Lower bound of `Wilson score confidence interval for a Bernoulli parameter` provides a way to sort a product based on positive and negative ratings.
• The idea here is to treat the existing set of user ratings as a statistical sampling of a hypothetical set of user ratings from all users and then use this score. In other words, what user community would think about upvoting a product with 95% confidence given that we have an existing rating for this product with a sample (subset from the whole community) user ratings.
• Therefore if we know what a sample population thinks i.e. user reviews for a product, you can use this to estimate the preferences of the whole community.

• If there are `X` positive votes and `Y` negative votes for an article. We can estimate that with 95% confidence between `wilson_lower_bound_score` and `wilson_upper_bound_score` % of users will upvote this product using the Wilson Score of Confidence interval. • Wilson Confidence Interval considers binomial distribution for score calculation i.e. it considers only positive and negative ratings. If your product is rated on a 5 scale rating, then we can convert ratings {1–3} into negative and {4,5} to positive rating and can calculate Wilson score.

Python implementation with star rating (with +5 allowance) :

 ``` 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20``` ```def save(self, *args, **kwargs): if self.id and (self.thumbs_up.count() or self.thumbs_down.count()): import math Z = 1.96 pos, neg = self.thumbs_up.count(), self.thumbs_down.count() n = pos + neg phat = pos/n self.score = ((phat + Z*Z/(2*n) - Z * math.sqrt((phat*(1-phat)+Z*Z/(4*n))/n))/(1+Z*Z/n)) star = f'
• ' half_star = f'
• ' star_score = divmod(math.ceil(self.score*100)+5, 20) star_score_display = star*star_score if star_score >= 10 and star_score < 5: star_score_display += half_star self.star_score = star_score_display return super().save(*args, **kwargs) ```

Useful?