Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Sometimes, when you're required to maintain a moving average of a variable, the best thing you can do is go back to where that requirement came from, and see if it can be replaced with a weighted average with exponentially-decaying weights (read the <a href="http://en.wikipedia.org/wiki/Exponential_decay" rel="nofollow">wikipedia article</a> explaining it) . It is not as simple to understand as a moving average, but is much simpler to maintain and store, especially if you want to calculate it online and in real time.</p> <p>Suppose, for example, instead of looking at moving average on the series you provided, you look at the average with decaying weights with half-life of one minute.</p> <ul> <li>10:00, 1 (average is 1)</li> <li>10:01, 2 (old average is 1 with weight 0.5, new data is 2, new weighted average is (1*0.5+2*1)/(0.5+1)=<strong>1.667</strong>)</li> <li>10:02, 2 (old average is 1.667 with weight 0.75, new data is 2, new weighted average is (1.667*0.75+2*1)/(0.75+1)=<strong>1.85</strong>)</li> <li>10:03, 15 (old average is 1.85 with weight 0.875, new data is 15, new weighted average is (1.85*0.875+15*1)/(0.875+1)=<strong>8.8667</strong>)</li> <li>...</li> </ul> <p>It may look complicated but it's actually quite simple. Of course, you'd need to adjust the half-life you look at to something that suits your needs (it's a bit different than choosing the window for a moving average).</p> <p>There are two big advantages to using decaying-weights average over moving average:</p> <ol> <li>You don't need to log discrete values to calculate the average; all you need is to store the current value and the time it was sampled.</li> <li>You only need to recalculate the average when a the data changes. When it doesn't change, the weight of the average you have decays, but its value remains. So you can calculate it when new data is received, rather than in a separate task running in <code>cron</code> or something of that sort. </li> </ol> <p>P.S., Playing with the equations a little bit, you can find some more useful things you can do with it, such as storing e^X for that value which you can index, as it maintains the ordinal relation between different values of the metric you're monitoring over time.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload