How to get weighted average where latest value is weighted twice as all previous?

637 views Asked by At

I've used this before, but just can't remember it, and can't find it anywhere.

It's a running average where the newest value has twice the weight of all the previous values (combined), so that as time passes the oldest values have less and less effect. And I don't have enough memory to store the older values.

TO GET AVERAGE:

int sum=0;
int n = 0;
float aver = 0;
for(;;){
  float new_value = some_function();
  sum += new_value;
  ++n;
  aver = sum / n;  
}

But how to I get an average where the new_value is weighted to be twice the weighting of the previous average?

float aver = 0;
for(;;){
  float new_value = some_function();
  aver = aver * ??? + new_value * ???;

}
1

There are 1 answers

5
chux - Reinstate Monica On BEST ANSWER

a running average where the newest value has twice the weight of all the previous values, ...

how to I get an average where the new_value is weighted to be twice the weighting of the previous average?

Compute the special average on the sum and the latest.

int sum = 0;
int n = 0;
float average_special = 0.0;
for(;;) {
  float new_value = some_function();
  sum += new_value;
  ++n;
  average_special = (sum + new_value) / (n+1);  
}