def welford(x_array):
    """Welford's method.
    
    Mean and variance calculation using Welford's method, 
    taken from part 3 of "Ten Little Algorithms" by Jason Sachs.

    Args:
        x_array (array): sample sequence.

    Returns:
        M, S: mean and variance of x_array.

    """
    k = 0 
    M = 0
    S = 0
    for x in x_array:
        k += 1
        Mnext = M + (x - M) / k
        S = S + (x - M)*(x - Mnext)
        M = Mnext
    return (M, S/(k-1))