import math


def _ln_binomial(n, k):
    """log of binomial coefficient function (n k), i.e., n choose k"""
    if k > n:
        raise ValueError
    if k == n or k == 0:
        return 0
    if k * 2 > n:
        k = n - k
    return math.lgamma(n + 1) - math.lgamma(k + 1) - math.lgamma(n - k + 1)