Exemplo n.º 1
0
    def testLeastSquares(self):
        xs = [1, 2, 3]
        ys = [3, 6, 8]
        inter, slope = correlation.LeastSquares(xs, ys)
        self.assertAlmostEquals(inter, 0.66666666)
        self.assertAlmostEquals(slope, 2.5)

        res = correlation.Residuals(xs, ys, inter, slope)
        for got, exp in zip(res, [-0.166666666, 0.33333333, -0.16666666666]):
            self.assertAlmostEquals(got, exp)

        R2 = correlation.CoefDetermination(ys, res)
        self.assertAlmostEquals(R2, 0.986842105263)
Exemplo n.º 2
0
def Fit(xs, ys):
    """Find the linear least squares fit between xs and ys."""
    inter, slope = correlation.LeastSquares(xs, ys)
    print '(inter, slope):', inter, slope

    res = correlation.Residuals(xs, ys, inter, slope)
    R2 = correlation.CoefDetermination(ys, res)

    print 'inter', inter
    print 'slope', slope
    print 'R^2', R2
    print

    return inter, slope, R2
Exemplo n.º 3
0
def Fit(halfs, fulls):
    """Find the linear least squares fit between halfs and fulls."""
    inter, slope = correlation.LeastSquares(halfs, fulls)
    print '(inter, slope):', inter, slope

    res = correlation.Residuals(halfs, fulls, inter, slope)
    R2 = correlation.CoefDetermination(fulls, res)

    print 'inter', inter
    print 'slope', slope
    print 'R^2', R2
    print

    print 'prediction', inter + slope * ConvertTimeToMinutes('1:34:05')

    return inter, slope, R2
Exemplo n.º 4
0
def ComputeLeastSquares(ages, weights):
    """Computes least squares fit for ages and weights.

    Prints summary statistics.
    """
    # compute the correlation between age and weight
    print 'Pearson correlation', correlation.Corr(ages, weights)
    print 'Spearman correlation', correlation.SpearmanCorr(ages, weights)

    # compute least squares fit
    inter, slope = correlation.LeastSquares(ages, weights)
    print '(inter, slope):', inter, slope

    res = correlation.Residuals(ages, weights, inter, slope)
    R2 = correlation.CoefDetermination(weights, res)

    print 'R^2', R2
    print
    return inter, slope, R2
Exemplo n.º 5
0
def ComputeCorrelations():
    resp = brfss_scatter.Respondents()
    resp.ReadRecords()
    print('Number of records:', len(resp.records))

    heights, weights = resp.GetHeightWeight()
    pearson = correlation.Corr(heights, weights)
    print('Pearson correlation (weights):', pearson)

    log_weights = Log(weights)
    pearson = correlation.Corr(heights, log_weights)
    print('Pearson correlation (log weights):', pearson)

    spearman = correlation.SpearmanCorr(heights, weights)
    print('Spearman correlation (weights):', spearman)

    inter, slope = correlation.LeastSquares(heights, log_weights)
    print('Least squares inter, slope (log weights):', inter, slope)

    res = correlation.Residuals(heights, log_weights, inter, slope)
    R2 = correlation.CoefDetermination(log_weights, res)
    print('Coefficient of determination:', R2)
    print('sqrt(R^2):', math.sqrt(R2))