def _var(x, ddof=0):
    """ Calculate variance for an array

    Uses Welford's algorithm[1] for online variance calculation.

    Parameters
    ----------
    x : array-like
        The data
    ddof : int
        Degrees of freedom

    References
    ----------
    .. [1] https://en.wikipedia.org/wiki/Algorithms_for_calculating_variance#Online_algorithm
    """
    n = 0.0
    mean_ = M2 = 0.0

    for x_ in x:
        n += 1
        delta = x_ - mean_
        mean_ += delta / n
        delta2 = x_ - mean_
        M2 += delta * delta2
    return M2 / max(n - ddof, 0)