def two_pass_covariance(data1, data2):
    """Two pass algorithm computes the sample means
    and then the covariance"""
    n = len(data1)
    mean1 = sum(data1) / n
    mean2 = sum(data2) / n
    covariance = 0
    for i1, i2 in zip(data1, data2):
        a = i1 - mean1
        b = i2 - mean2
        covariance += a * b / n
    return covariance
