HTML
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<p id="rating"></p>
<script type="text/javascript">
// create dummy data as test inputs
let ratings = {
a: [5],
b: [4, 5],
c: [4, 5, 5],
d: [4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4],
e: [1, 2, 3, 4, 5],
f: [3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3],
g: [3],
h: [1],
i: [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1],
j: [5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5],
}
//get average number of votes from database snapshots
let avg_num_votes = function() {
let cumulative_rating_length = 0
let count_num_item = 0
// find total length of rating array (i.e. num of votes) from each items
// exclude items with zero ratings
for (const key of Object.keys(ratings)) {
if (ratings[key].length > 0) {
cumulative_rating_length += ratings[key].length
count_num_item += 1
}
}
// get average rating length i.e. number of votes
return cumulative_rating_length / count_num_item
}
// averaging array function
let average_array = function (a) {
// sum all elements in array, then divide by length, to get its average
let sum = a.reduce((previous, current) => current += previous)
return sum / a.length
}
//get average ratings from database snapshots
let avg_ratings = function() {
let arr = []
// concat all ratings into one array
// exclude items with zero ratings
for (const key of Object.keys(ratings)) {
if (ratings[key].length > 0) {
arr = arr.concat(ratings[key])
}
}
return average_array(arr)
}
// reference #1: https://stats.stackexchange.com/questions/15979/how-to-find-confidence-intervals-for-ratings/16035
// reference #2: http://thebroth.com/blog/118/bayesian-rating.html
let bayesian_rating = function(item) {
// br = ( (avg_num_votes * avg_ratings) +...