You are right the nested "where" statements are not necessary and can be replaced with k*logp + (n-k)*log1mp directly. I will fix that.