Optimal estimation of ensemble averages from a quantum measurement