Skip to content

Instantly share code, notes, and snippets.

@olozzalap
Last active August 14, 2018 17:05
Show Gist options
  • Save olozzalap/dfcc7a1960369c9e25ec72f205a66327 to your computer and use it in GitHub Desktop.
Save olozzalap/dfcc7a1960369c9e25ec72f205a66327 to your computer and use it in GitHub Desktop.
Number of Reviews decaying weight value to dynamically factor in number of reviews along with the average score and factor in an exponentially decaying weightDecelerationFactor
// averageScore: float
// numReviews: int
// weightDecelerationFactor: float (a higher weightDecelerationFactor represents lower marginal value per each additional review, a lower weightDecelerationFactor represents higher marginal value per each additional review)
function parseScoreWithNumReviewsTotalValue(averageScore, numReviews, weightDecelerationFactor) {
let nextReviewWeight = 1;
let totalNumReviewsWeightFactor = 1;
// Check to ensure weightDecelerationFactor is in bounds
if (weightDecelerationFactor <= 0.00 || weightDecelerationFactor >= 1.00) {
console.log("please provide a valid float value between 0 and 1 for the weightDecelerationFactor");
return false;
}
for (i = 0; i < numReviews; i++) {
// First calculates the nextReviewWeight (always decreasing towards, but never reaching, 0)
nextReviewWeight = nextReviewWeight - (nextReviewWeight * weightDecelerationFactor);
// Adds this nextReviewWeight to the totalNumReviewsWeightFactor
totalNumReviewsWeightFactor = totalNumReviewsWeightFactor + nextReviewWeight;
// Finally multiplies weightDecelerationFactor by one minus itself to continue descresing towards zero, this represents the decreasing marginal value of each additional review
weightDecelerationFactor = weightDecelerationFactor * (1 - weightDecelerationFactor);
// console.log(totalNumReviewsWeightFactor);
}
// Multiply this final totalNumReviewsWeightFactor by the given averageScore to get the calculated totalValue
let totalValue = totalNumReviewsWeightFactor * averageScore;
// console.log(weightDecelerationFactor);
console.log(totalValue.toFixed(4));
return totalValue.toFixed(4);
}
// averageScore: 3.7, numReviews: 108, weightDecelerationFactor: .70 returns a totalValue of 20.0005
parseScoreWithNumReviewsTotalValue(3.7, 108, .70);
// averageScore: 3.7, numReviews: 108, weightDecelerationFactor: .47 returns a totalValue of 29.0963
parseScoreWithNumReviewsTotalValue(3.7, 108, .47);
// averageScore: 2.1, numReviews: 108, weightDecelerationFactor: .70 returns a totalValue of 11.3516
parseScoreWithNumReviewsTotalValue(2.1, 108, .70);
// averageScore: 3.7, numReviews: 3,746, weightDecelerationFactor: .70 returns a totalValue of 38.3583
parseScoreWithNumReviewsTotalValue(3.7, 3746, .70);
// averageScore: 4.65, numReviews: 3,746, weightDecelerationFactor: .80 returns a totalValue of 38.7206
parseScoreWithNumReviewsTotalValue(4.65, 3746, .84);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment