Added function to calculate binned statistics