Beispiel #1
0
def quadratic_mean(data):
    """quadratic_mean(iterable_of_numbers) -> quadratic mean of numbers
    quadratic_mean(iterable_of_rows) -> quadratic means of columns

    Return the quadratic mean of the given numbers or columns.

    >>> quadratic_mean([2, 2, 4, 5])
    3.5

    The quadratic mean, or RMS (Root Mean Square), is the square root of the
    arithmetic mean of the squares of the data. It is a type of average
    best used to get an average absolute magnitude when quantities vary from
    positive to negative:

    >>> quadratic_mean([-3, -2, 0, 2, 3])
    2.280350850198276

    When passed an iterable of sequences, each inner sequence represents a
    row of data, and ``quadratic_mean`` operates on each column. All rows
    must have the same number of columns, or ValueError is raised.

    >>> data = [[0, 1, 2, 4],
    ...         [1, 2, 4, 6],
    ...         [2, 4, 6, 6]]
    ...
    >>> quadratic_mean(data)  #doctest: +ELLIPSIS
    [1.29099..., 2.64575..., 4.3204..., 5.41602...]

    """
    count, total = stats._len_sum(v.sqr(x) for x in data)
    if not count:
        raise stats.StatsError(
        'quadratic mean of empty sequence is not defined')
    return v.sqrt(v.div(total, count))
Beispiel #2
0
def average_deviation(data, m=None):
    """average_deviation(data [, m]) -> average absolute deviation of data.

    Returns the average deviation of the sample data from the population
    centre ``m`` (usually the mean, or the median). If you know the
    population mean or median, pass it as the second element:

    >>> data = [2.0, 2.25, 2.5, 2.5, 3.25]  # A sample from a population
    >>> mu = 2.75                           # with a known mean.
    >>> average_deviation(data, mu)
    0.45

    If you don't know the centre location, you can estimate it by passing
    the sample mean or median instead. If ``m`` is not None, or not given,
    the sample mean is calculated from the data and used:

    >>> average_deviation(data)
    0.3

    If data is an iterable of sequences, each inner sequence represents a
    row of data, and ``average_deviation`` operates on each column. Every
    row must have the same number of columns, or ValueError is raised.
    Similarly, m (if given) must have either the same number of items, or
    be a single number.

    >>> data = [[0, 1, 2, 4],
    ...         [1, 2, 4, 6],
    ...         [2, 4, 6, 6]]
    ...
    >>> average_deviation(data, [1, 2, 3.5, 6])  #doctest: +ELLIPSIS
    [0.666666..., 1.0, 1.5, 0.666666...]

    """
    if m is None:
        if not isinstance(data, list):
            data = list(data)
        m = stats.mean(data)
    f = lambda x, m: abs(x-m)
    count, total = stats._len_sum(v.apply(f, x, m) for x in data)
    if not count:
        raise stats.StatsError(
        'average deviation requires at least 1 data point')
    return v.div(total, count)
Beispiel #3
0
def harmonic_mean(data):
    """harmonic_mean(iterable_of_numbers) -> harmonic mean of numbers
    harmonic_mean(iterable_of_rows) -> harmonic means of columns

    Return the harmonic mean of the given numbers or columns.

    The harmonic mean, or subcontrary mean, is the reciprocal of the
    arithmetic mean of the reciprocals of the data. It is a type of average
    best used for averaging rates or speeds.

    >>> harmonic_mean([0.25, 0.5, 1.0, 1.0])
    0.5

    If data includes one or more zero values, the result will be zero if the
    zeroes are all the same sign, or an NAN if they are of opposite signs.

    When passed an iterable of sequences, each inner sequence represents a
    row of data, and ``harmonic_mean`` operates on each column. All rows
    must have the same number of columns, or ValueError is raised.

    >>> data = [[0, 1, 2, 4],
    ...         [1, 2, 4, 8],
    ...         [2, 4, 8, 8]]
    ...
    >>> harmonic_mean(data)  #doctest: +ELLIPSIS
    [0.0, 1.71428..., 3.42857..., 6.0]

    """
    # FIXME harmonic_mean([x]) should equal x exactly, but due to rounding
    # errors in the 1/(1/x) round trip, sometimes it doesn't.
    invert = functools.partial(_divide, 1)
    n, total = stats._len_sum(v.apply(invert, x) for x in data)
    if not n:
        raise stats.StatsError(
        'harmonic mean of empty sequence is not defined')
    return v.div(n, total)