Efficient way of calculating average difference of array elements from array average value

Posted by Saysmaster on Stack Overflow See other posts from Stack Overflow or by Saysmaster
Published on 2012-03-05T04:18:01Z Indexed on 2012/03/21 23:29 UTC
Read the original article Hit count: 301

Filed under:
|
|
|
|

Is there a way to calculate the average distance of array elements from array average value, by only "visiting" each array element once? (I search for an algorithm)

Example:

Array : [ 1 , 5 , 4 , 9 , 6 ]
Average : ( 1 + 5 + 4 + 9 + 6 ) / 5 = 5
Distance Array : [|1-5|, |5-5|, |4-5|, |9-5|, |6-5|] = [4 , 0 , 1 , 4 , 1 ]
Average Distance : ( 4 + 0 + 1 + 4 + 1 ) / 5 = 2

The simple algorithm needs 2 passes.

1st pass) Reads and accumulates values, then divides the result by array length to calculate average value of array elements.

2nd pass) Reads values, accumulates each one's distance from the previously calculated average value, and then divides the result by array length to find the average distance of the elements from the average value of the array.

The two passes are identical. It is the classic algorithm of calculating the average of a set of values. The first one takes as input the elements of the array, the second one the distances of each element from the array's average value.

Calculating the average can be modified to not accumulate the values, but caclulating the average "on the fly" as we sequentialy read the elements from the array.

The formula is:

Compute Running Average of Array's elements
-------------------------------------------
RA[i] = E[i] {for i == 1}
RA[i] = RA[i-1] - RA[i-1]/i + A[i]/i { for i > 1 }

Where A[x] is the array's element at position x, RA[x] is the average of the array's elements between position 1 and x (running average).

My question is:

Is there a similar algorithm, to calculate "on the fly" (as we read the array's elements), the average distance of the elements from the array's mean value?

The problem is that, as we read the array's elements, the final average value of the array is not known. Only the running average is known. So calculating differences from the running average will not yield the correct result. I suppose, if such algorithm exists, it probably should have the "ability" to compensate, in a way, on each new element read for the error calculated as far.

© Stack Overflow or respective owner

Related posts about arrays

Related posts about algorithm