Calculate logZ