Exemple #1
0
def cov(xdata, ydata):
    """Return the sample covariance between (x, y) data.

    >>> cov([(0.1, 2.3), (0.5, 2.7), (1.2, 3.1), (1.7, 2.9)])
    ... #doctest: +ELLIPSIS
    0.201666666666...

    >>> cov([0.75, 1.5, 2.5, 2.75, 2.75], [0.25, 1.1, 2.8, 2.95, 3.25])
    ... #doctest: +ELLIPSIS
    1.1675
    >>> cov([(0.1, 2.3), (0.5, 2.7), (1.2, 3.1), (1.7, 2.9)])
    ... #doctest: +ELLIPSIS
    0.201666666666...

    Covariance reduces down to standard variance when applied to the same
    data as both the x and y values:

    >>> data = [1.2, 0.75, 1.5, 2.45, 1.75]
    >>> cov(data, data)  #doctest: +ELLIPSIS
    0.40325000000...
    >>> stats.variance(data)  #doctest: +ELLIPSIS
    0.40325000000...

    """
    n, s = _sum_prod_deviations(zip(xdata, ydata), None, None)
    if n > 1:
        return s/(n-1)
    else:
        raise StatsError('sample covariance requires at least two points')
Exemple #2
0
def pcov(xdata, ydata=None):
    """Return the population covariance between (x, y) data.

    >>> pcov([0.75, 1.5, 2.5, 2.75, 2.75], [0.25, 1.1, 2.8, 2.95, 3.25])
    ... #doctest: +ELLIPSIS
    0.93399999999...
    >>> pcov([(0.1, 2.3), (0.5, 2.7), (1.2, 3.1), (1.7, 2.9)])
    0.15125

    """
    n, s = _sum_prod_deviations(zip(xdata, ydata), None, None)
    if n > 0:
        return s/n
    else:
        raise StatsError('population covariance requires at least one point')