from operator import truediv


def variance(n, k, m, E, hyper_v_cache={}):
    """
    Returns variance of class count in given hypergeometric distribution

    :param m: represents the size of whole dataset

    :param k: represents the size of the sample taken from the dataset

    :param n: represents the size of class in the dataset

    :param E: represents the expectation value of the hypergeometric distribution

    :rtype: variance of class count
    """
    try: #check if can be found from cache
        return hyper_v_cache[m,k,n,E]
    except KeyError:
        pass

    tmp1 = truediv((m - k), m)
    tmp2 = truediv((m - n), (m - 1))
    variance = E*tmp1*tmp2
    hyper_v_cache[m,k,n,E] = variance

    return variance