Fix bugs in loglikelihood calculation