title: How to calculate a weighted mean?
body: <p>My language is PHP, but the algorithm should be fairly universal.</p>
  
  <p>I have an associative array of (let's say) ratings and number of times that rating has been given.</p>
  
  <pre><code>$ratings = array(
      1 =&gt; 1,
      2 =&gt; 3,
      3 =&gt; 6,
      4 =&gt; 3,
      5 =&gt; 3
  );
  </code></pre>
  
  <p>This is the equivalent of: <code>[1, 2, 2, 2, 3, 3, 3, 3, 3, 3, 4, 4, 4, 5, 5, 5]</code>, but given the numbers I'm working with, it would be quite inefficient to convert from the first form to the second.</p>
  
  <p>What would be the algorithm to calculate the mean of the above numbers?</p>