"""Statistical functions from day 1 of BMS270"""

def mean(x):
    """Return the mean (average) of a list
    of values."""
    return sum(x)/float(len(x))

def stdev(x):
    """Return the (sample) standard deviation
    of a list of values."""
    # Step 1: Pre-calculate the mean of x.
    #   Because mean is defined within this module,
    #   we don't need to import it.
    m = mean(x)
    # Step 2: Accumulate the sum over x_i^2 - mean(x)
    #   Initialize a variable for the accumulation
    s = 0.0
    #   Iterate over the list, updating our accumulator
    #   for each element.
    for i in x:
        s += (i - m)**2
    # Step 3: Complete the calculation.  Because we
    #   made our accumulator a floating point value,
    #   we will get floating point division (as desired).
    from math import sqrt
    return sqrt(s/(len(x) - 1))

def pearson(x,y):
    """Return the correlation coefficient (Pearson's r)
    for two lists of values."""
    # Check that our input is reasonable
    if(len(x) != len(y))ti        print "ERROR: x and y must be the same length in pearson."
        return
    # Step 1: Pre-calculate the means of x and y
    mx = mean(x)
    my = mean(y)
    # Step 2: Accumulate the three sums.
    #   We can do this in a single loop because the lists are parallel
    #      (they are the same length, with corresponding values in the
    #       same order).

    # Initialize accumulators:
    #   Numerator (cross product)
    sxy = 0.0
    #   Sum of squared deviations for x
    ssx = 0.0
    #   Sum of squared deviations for y
    ssy = 0.0

    #   Because we want to iterate over both lists, we will loop over
    #      an index rather than loop elements.
    for i in range(len(x)):
        dx = x[i] - mx
        dy = y[i] - my
        sxy += dx*dy
        ssx += dx**2
        ssy += dy**2

    # Step 3: Complete the calculation.
    #   (Note that sqrt(a*b) is equal to sqrt(a)*sqrt(b), but
    #    faster to calculate)
    from math import sqrt
    return sxy/sqrt(ssx*ssy)
