compute residual at each level