adding missing aggregate fns (avg, median, variance and stddev)

I would have done these each in their own commit but there was enough mutual dependance (thus the refactor to a letfn) that it seemed appropriate to do it in one commit.