title: Simple Popularity Algorithm
body: <h2>Summary</h2>
  
  <p>As Ted Jaspers wisely pointed out, the methodology I described in the original proposal back in 2012 is actually a special case of an <a href="https://en.wikipedia.org/wiki/Moving_average#Exponential_moving_average" rel="nofollow noreferrer">exponential moving average</a>. The beauty of this approach is that it can be calculated recursively, meaning you only need to store a single popularity value with each object and then you can recursively adjust this value when an event occurs. There's no need to record every event.</p>
  
  <p>This single popularity value represents all past events (within the limits of the data type being used), but older events begin to matter exponentially less as new events are factored in. This algorithm will adapt to different time scales and will respond to varying traffic volumes. Each time an event occurs, the <strong>new</strong> popularity value can be calculated using the following formula:</p>
  
  <p><code>(a * t) + ((1 - a) * p)</code></p>
  
  <ul>
  <li><code>a</code> — coefficient between 0 and 1 (higher values discount older events faster)</li>
  <li><code>t</code> — current timestamp</li>
  <li><code>p</code> — current popularity value (e.g. stored in a database)</li>
  </ul>
  
  <p>Reasonable values for <code>a</code> will depend on your application. A good starting place is <code>a=2/(N+1)</code>, where <code>N</code> is the number of events that should significantly affect the outcome. For example, on a low-traffic website where the event is a page view, you might expect hundreds of page views over a period of a few days. Choosing <code>N=100</code> (<code>a≈0.02</code>) would be a reasonable choice. For a high-traffic website, you might expect millions of page views over a period of a few days, in which case <code>N=1000000</code> (<code>a≈0.000002</code>) would be more reasonable. The value for <code>a</code> will likely need to be gradually adjusted over time.</p>
  
  <p>To illustrate how simple this popularity algorithm is, here's an example of how it can be implemented in Craft CMS in 2 lines of Twig markup:</p>
  
  <pre><code>{% set popularity = (0.02 * date().timestamp) + (0.98 * entry.popularity) %}
  {% do entry.setFieldValue("popularity", popularity) %}
  </code></pre>
  
  <p>Notice that there's no need to create new database tables or store endless event records in order to calculate popularity.</p>
  
  <p>One caveat to keep in mind is that exponential moving averages have a spin-up interval, so it takes a few recursions before the value can be considered accurate. This means the initial condition is important. For example, if the popularity of a new item is initialized using the current timestamp, the item immediately becomes the most popular item in the entire set before eventually settling down into a more accurate position. This might be desirable if you want to promote new content. Alternatively, you may want content to work its way up from the bottom, in which case you could initialize it with the timestamp of when the application was first launched. You could also find a happy medium by initializing the value with an average of all popularity values in the database, so it starts out right in the middle.</p>
  
  <hr>
  
  <h2>Original Proposal</h2>
  
  <p>There are plenty of <a href="http://blog.linkibol.com/2010/05/07/how-to-build-a-popularity-algorithm-you-can-be-proud-of/" rel="nofollow noreferrer">suggested algorithms</a> for calculating popularity based on an item's age and the number of votes, clicks, or purchases an item receives. However, the more robust methods I've seen often require overly complex calculations and multiple stored values which clutter the database. I've been contemplating an extremely simple algorithm that doesn't require storing <em>any</em> variables (other than the popularity value itself) and requires only <em>one</em> simple calculation. It's ridiculously simple:</p>
  
  <p><strong><code>p = (p + t) / 2</code></strong></p>
  
  <p>Here, <strong>p is the popularity value stored in the database</strong> and <strong>t is the current timestamp</strong>. When an item is first created, <strong>p</strong> must be initialized. There are two possible initialization methods:</p>
  
  <ol>
  <li>Initialize <strong>p</strong> with the current timestamp <strong>t</strong></li>
  <li>Initialize <strong>p</strong> with the average of all <strong>p</strong> values in the database</li>
  </ol>
  
  <p>Note that initialization method (1) gives recently added items a clear advantage over historical items, thus adding an element of <em>relevance</em>. On the other hand, initialization method (2) treats new items as equals when compared to historical items.</p>
  
  <p>Let's say you use initialization method (1) and initialize <strong>p</strong> with the current timestamp. When the item receives its first vote, <strong>p</strong> becomes the average of the creation time and the vote time. Thus, the popularity value <strong>p</strong> still represents a valid timestamp (assuming you round to the nearest integer), but the actual time it represents is abstracted.</p>
  
  <p>With this method, only one simple calculation is required and only one value needs to be stored in the database (<strong>p</strong>). This method also prevents runaway values, since a given item's popularity can never exceed the current time.</p>
  
  <p>An example of the algorithm at work over a period of 1 day: <a href="http://jsfiddle.net/q2UCn/" rel="nofollow noreferrer">http://jsfiddle.net/q2UCn/</a><br />
  An example of the algorithm at work over a period of 1 year: <a href="http://jsfiddle.net/tWU9y/" rel="nofollow noreferrer">http://jsfiddle.net/tWU9y/</a></p>
  
  <p>If you expect votes to steadily stream in at sub-second intervals, then you will need to use a microsecond timestamp, such as the PHP <code>microtime()</code> function. Otherwise, a standard UNIX timestamp will work, such as the PHP <code>time()</code> function.</p>
  
  <p>Now for my question: do you see any major flaws with this approach?</p>