import math


def compute_mcc(tp, fp, tn, fn):
    """
    Compute the Matthew correlation coefficient.
    Regarded as a good measure of quality in case of unbalanced classes.
    Returns a value between -1 and +1. +1 represents perfect prediction, 0 random, and -1 total disagreement
    between prediction and observation.
    """
    n = tn + tp + fn + fp
    s = (tp + fn) / n
    p = (tp + fp) / n
    numerator = (tp / n - s*p)
    denominator = math.sqrt(p*s*(1-s)*(1-p))
    try:
        mcc = numerator / denominator
    except ZeroDivisionError:
        mcc = 0.0  # If denominator is zero, it means the classifier just predicts the majority class!
    return mcc