Here is a clever algorithm to find the median of a stream of integers by using just one variable: The idea is to maintain a floating median. Start with an arbitrary number, say 0. If the incoming number is greater than the estimate, num += 1, else num -= 1. It is easy to prove that for a large enough stream, assuming the stream is drawn from iid samples, this would converge to the median, by central limit theorem. Us…
It is easy to prove that for a large enough stream, assuming the stream is drawn from iid samples, this would converge to the median, by central limit theorem. I'm not sure what proof you have in mind, but I don't think using the word "converge" is correct. Near as I can tell, your algorithm is x[n+1]= x[n] + sign(sample[n]-x[n]). If you are applying the c.l.t. to sign(sample[n]-x[n]), all this says is that your algo…
Find running median from a stream of integers
21–29 of 29 posts
Re: Find running median from a stream of integers
#22There is a fast and memory efficient solution using Indexable Skiplists: http://code.activestate.com/recipes/576930-efficient-running...
In a n-sized sliding window, only the most recent n elements need to be stored (not the entire data stream). The skiplist insertions, deletions, and indexed lookups are all O(log n).
Re: Find running median from a stream of integers
#23Earlier quoted context omitted.
> It is easy to prove that... Reminds me of a similar comment in the margin of a old book: http://en.wikipedia.org/wiki/Fermats_Last_Theorem . :) But it does sound both plausible and brilliant. How do you prove it (in broad terms)?
toemetoch, who is dead, said: "In a normal distribution the probability of a value higher or lower than mean is 50%. So if you find mean, the average of your +1/-1 noise is zero -> if you're on mean you'll remain there. If you're not on mean you have a (50 + error)% chance of moving towards mean and (50 - error)% chance of moving away from mean on the next random value -> the trend is towards mean."
... I'm not following. I can see my reply a few posts down. I accidentally hit submit twice, deleted one but the second one is still there.
Edit: The same technique can be found in some 1-bit ADCs, I believe it's delta-sigma ADC.
Re: Find running median from a stream of integers
#24Earlier quoted context omitted.
toemetoch, who is dead, said: "In a normal distribution the probability of a value higher or lower than mean is 50%. So if you find mean, the average of your +1/-1 noise is zero -> if you're on mean you'll remain there. If you're not on mean you have a (50 + error)% chance of moving towards mean and (50 - error)% chance of moving away from mean on the next random value -> the trend is towards mean."
toemetoch, who is dead ... I'm not following. I can see my reply a few posts down. I accidentally hit submit twice, deleted one but the second one is still there. Edit: The same technique can be found in some 1-bit ADCs, I believe it's delta-sigma ADC.
Re: Find running median from a stream of integers
#25Earlier quoted context omitted.
> It is easy to prove that... Reminds me of a similar comment in the margin of a old book: http://en.wikipedia.org/wiki/Fermats_Last_Theorem . :) But it does sound both plausible and brilliant. How do you prove it (in broad terms)?
Slightly off topic, but looks like toemetoch has tripped one of the hellban filters, isn't a spammer, and has no contact info in his profile. Might want to sort that out if you're reading this toemetoch!
Re: Find running median from a stream of integers
#26Earlier quoted context omitted.
> It is easy to prove that... Reminds me of a similar comment in the margin of a old book: http://en.wikipedia.org/wiki/Fermats_Last_Theorem . :) But it does sound both plausible and brilliant. How do you prove it (in broad terms)?
Slightly off topic, but looks like toemetoch has tripped one of the hellban filters, isn't a spammer, and has no contact info in his profile. Might want to sort that out if you're reading this toemetoch!
Re: Find running median from a stream of integers
#27Earlier quoted context omitted.
toemetoch, who is dead, said: "In a normal distribution the probability of a value higher or lower than mean is 50%. So if you find mean, the average of your +1/-1 noise is zero -> if you're on mean you'll remain there. If you're not on mean you have a (50 + error)% chance of moving towards mean and (50 - error)% chance of moving away from mean on the next random value -> the trend is towards mean."
toemetoch, who is dead ... I'm not following. I can see my reply a few posts down. I accidentally hit submit twice, deleted one but the second one is still there. Edit: The same technique can be found in some 1-bit ADCs, I believe it's delta-sigma ADC.
Re: Find running median from a stream of integers
#28Here is a clever algorithm to find the median of a stream of integers by using just one variable: The idea is to maintain a floating median. Start with an arbitrary number, say 0. If the incoming number is greater than the estimate, num += 1, else num -= 1. It is easy to prove that for a large enough stream, assuming the stream is drawn from iid samples, this would converge to the median, by central limit theorem. Us…
It is easy to prove that for a large enough stream, assuming the stream is drawn from iid samples, this would converge to the median, by central limit theorem. I'm not sure what proof you have in mind, but I don't think using the word "converge" is correct. Near as I can tell, your algorithm is x[n+1]= x[n] + sign(sample[n]-x[n]). If you are applying the c.l.t. to sign(sample[n]-x[n]), all this says is that your algo…
Re: Find running median from a stream of integers
#29Heap sounds like overkill to find the middle value in a running list. What's wrong with simple stack and keeping track of how many integers you've received?
Your solution is for median position, not median value, and it would require a queue, not a stack, so unneeded old values could be dropped.