If I have the mean, s.d., median and count for sample A, and the same for sample B, can I throw away the samples, and calculate exactly mean, median and s.d. for the combination? Well the mean is easy.
Here is the question in R code:
a=c(1,2,3,4,5)
b=c(2,2,3,4,4,5)
c=c(a,b)
m=( (length(a)*mean(a)) + (length(b)*mean(b)) ) / (length(a)+length(b))
print(mean(c) == m) #TRUE
s= ???
print(sd(c) == s)
d= ???
print(median(c) == d)
I tried a few things for s.d. but failed; I had no idea for the median.
(For s.d. the closest I got was var(a)*(length(a)-1)
, doing the same for b
, then dividing the sum by length(a)+length(b)-1
. That gives 1.733 compared to var(c)
of 1.7636.)
As a practical example, if I'm collecting stock ticks, I calculate mean, median and sd for each 1 minute period. Can I then use those 1 minute bars to make the same data for the 5 minute bars, and use the 5 minute bar data to make the hourly bars, and so on? Or, if I want to know the s.d. for weekly bars do I need to keep the ticks and load a full weeks worth of ticks in memory? (Yes, I realize I could get a good enough approximation of the weekly answer by treating my minute bars as ticks, but I wanted to confirm my hunch that an exact answer is impossible once I've thrown away the ticks.)
UPDATE: given that I was wrong and getting the s.d. is possible (see answers below), I'm now wondering if the median is also not impossible? E.g. how about if I also knew the median absolute deviation (mad()
in R) for each sub-period?