Skip to content

Instantly share code, notes, and snippets.

@segdeha
Created November 3, 2019 05:11
Show Gist options
  • Save segdeha/a45ef8d6d7f094f4038a2318a9d60051 to your computer and use it in GitHub Desktop.
Save segdeha/a45ef8d6d7f094f4038a2318a9d60051 to your computer and use it in GitHub Desktop.
/**
* Calculate a weighted estimate for the interval until the next purchase
* Current purchase a tiny bit less weight than all previous purchases
* @param {Number} lastEstimate The last stored purchase interval estimate
* @param {Number} latestInterval The interval between the most recent and previous purchases
* @param {Number} numberOfPurchases Total number of purchases for the item
*/
const calculateEstimate = (lastEstimate, latestInterval, numberOfPurchases) => {
if (isNaN(lastEstimate)) {
lastEstimate = 14;
}
// FIXME algorithm doesn't work when there's only 1 purchase in the database
let previousFactor = lastEstimate * numberOfPurchases;
let latestFactor = latestInterval * (numberOfPurchases - 1);
let totalDivisor = numberOfPurchases * 2 - 1;
return (previousFactor + latestFactor) / totalDivisor;
};
export default calculateEstimate;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment