import math


def gain(before, after, pos_df, neg_df):
    """Calculates the information gain from before to after."""
    p0count = before.num_covered(pos_df)  # tp
    p1count = after.num_covered(pos_df)  # tp after action step
    n0count = before.num_covered(neg_df)  # fn
    n1count = after.num_covered(neg_df)  # fn after action step
    return p1count * (
        math.log2((p1count + 1) / (p1count + n1count + 1))
        - math.log2((p0count + 1) / (p0count + n0count + 1))
    )