Ejemplo n.º 1
0
def _normal_plot(ys, label, color='b', jitter=0.0, **line_options):
    """
    Makes a normal probability plot.
    
    Args:
        ys:           sequence of values
        label:        string label for the plotted line
        color:        color string passed along to pyplot.plot
        jitter:       float magnitude of jitter added to the ys
        line_options: dictionary of options for pyplot.plot        
    """
    n = len(ys)
    xs = [random.gauss(0.0, 1.0) for i in range(n)]
    xs.sort()
    ys = [y + random.uniform(-jitter, +jitter) for y in ys]
    ys.sort()

    inter, slope = correlation._least_squares(xs, ys)
    fit = correlation._fit_line(xs, inter, slope)
    pyplot.plot(*fit, color=color, linewidth=0.5, alpha=0.5)

    pyplot.plot(sorted(xs), sorted(ys),
                color=color,
                marker='.',
                label=label,
                markersize=3,
                alpha=0.1,
                **line_options)
Ejemplo n.º 2
0
def _compute_least_squares(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._spearman_corr(ages, weights))

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

    res = correlation._residuals(ages, weights, inter, slope)
    R2 = correlation._coef_determination(weights, res)

    print('R^2', R2)
    print
    return inter, slope, R2
Ejemplo n.º 3
0
def _compute_correlations():
    resp = brfss_scatter.Respondents()
    resp._read_records()
    print('Number of records:', len(resp.records))

    heights, weights = resp._get_height_weight()
    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._spearman_corr(heights, weights)
    print('Spearman correlation (weights):', spearman)

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

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