import math


def compute_pmi(p_x, p_y, p_xy, normalized, limit=1e-6):
    """
    Compute the Pointwise Mutual Information.

    :param float p_x: The probability of `x`, p(x).
    :param float p_y: The probability of `y`, p(y).
    :param float p_xy: The probability of `xy`, p(xy).
    :param bool normalized: Whether to return the normalized Pointwise
        Mutual Information in range [-1, 1].
    :param float limit: The value to use for computation when `p_xy` is 0.0
        (i.e., non observed), as the limit of PMI to zero (default: 1e-6).

    :return float pmi: The Pointwise Mutual Information.
    """

    if p_xy == 0.0:
        pmi = math.log(limit / (p_x * p_y))
        if normalized:
            pmi = pmi / -math.log(limit)
    else:
        pmi = math.log(p_xy / (p_x * p_y))
        if normalized:
            pmi = pmi / -math.log(p_xy)

    return pmi