compute covariance matrix more efficiently