Сalculate a windowed average where the newest value replaces the oldest value in a set of size 10. In this case, I have a real-time number stream and have access to current value, current average and the previous value.Is there any way to calculate this value incrementally storing minimal amount of historical state?

Hallie Stanton

Hallie Stanton

Answered question

2022-11-07

Сalculate a windowed average where the newest value replaces the oldest value in a set of size 10. In this case, I have a real-time number stream and have access to current value, current average and the previous value.Is there any way to calculate this value incrementally storing minimal amount of historical state?

Answer & Explanation

Waldruhylm

Waldruhylm

Beginner2022-11-08Added 14 answers

To do this you need to store all values currently in the window. This is best done using a double-linked list. So for each new value you append the value to the list and pop of the first (oldest) number in the list. Then you can subtract oldest / 10 and add new / 10 to the average value.
So the algorithm is (for maximal window size λ):
Initialize an empty List L, a v g = 0, l = 0
Get a new value n
If l < λ set a v g = ( a v g l + n ) / ( l + 1 ), l = l + 1, and append n to L
If l = λ append n to L and pop the first element o of L. Set a v g = a v g + ( n o ) / λ
Jump to 2

Do you have a similar question?

Recalculate according to your conditions!

New Questions in Research Methodology

Ask your question.
Get an expert answer.

Let our experts help you. Answer in as fast as 15 minutes.

Didn't find what you were looking for?