Algorithm of rolling window median:
median is a sorted array where you take from it the middle value.
simple rolling implementation is with a queue(dqueue) and a sorted_array (any implementation, binary tree, skiparray).
d_queue is an array where you can push to tail and shift (pop) from the front of the array.
sorted_array is an array where you insert by order at position found using binary search.
I used a queue (first-in-first-out array) to track order of added values to know which items to remove from the median array, when they the queue is longer than the wanted size. to fall off elements by date time or some running index, it is possible to add another queue and check the first element is too old, and decide if to remove first value from both queues.
To calculate a median efficiently I use a sorted array technique. it is when you insert new items into its sorted place, so the array is always sorted.
The insert:
- Insert at ordered place in the sorted_array,
- and push a value into a queue.
The remove:
- If d_queue first element is off the window, or if in an another queue you can have with indexes, the index is too old, then:
- remove the first item from d_queue(s),
- and binary search for it in the sorted array and remove it.
To have the median:
- Use the value(s) in the middle of the sorted_array.
- If sorted_array length is even use the item in the middle.
- If sorted_array length is odd use an average of two items in the middle.