예제 #1
0
    def _summarize_weight_change(self):
        """Print the mean reported change in weight in kg."""

        data = [(r.weight2, r.wtyrago) for r in self.records if r.weight2 != 'NA' and r.wtyrago != 'NA']

        changes = [(curr - prev) for curr, prev in data]

        print('Mean change', _03_thinkstats._mean(changes))
예제 #2
0
def _difference_in_means(firsts, others, attr):
    """
    Compute the difference in means between tables for a given attr.

    Prints summary statistics.
    """
    firsts_mean = _03_thinkstats._mean(getattr(firsts, attr))
    print('First babies, %s, trimmed mean:' % attr, firsts_mean)

    others_mean = _03_thinkstats._mean(getattr(others, attr))
    print('Other babies, %s, trimmed mean:' % attr, others_mean)

    diff = others_mean - firsts_mean
    print('Difference in means:', diff)
    print

    return diff
예제 #3
0
    def testVar(self):
        t = [1, 1, 1, 3, 3, 591]
        mu = _03_thinkstats._mean(t)
        var1 = _03_thinkstats._var(t)
        var2 = _03_thinkstats._var(t, mu)

        self.assertAlmostEquals(mu, 100.0)
        self.assertAlmostEquals(var1, 48217.0)
        self.assertAlmostEquals(var2, 48217.0)
예제 #4
0
def _make_line_plot(age_bins):
    xs = []
    ys = []
    for bin, weights in sorted(age_bins.iteritems()):
        xs.append(bin)
        ys.append(_03_thinkstats._mean(weights))

    _05_myplot._plot(xs, ys, 'bs-')
    _05_myplot._save(root='agemodel_line',
                     xlabel="Mother's age (years)",
                     ylabel='Mean birthweight (oz)',
                     legend=False)
예제 #5
0
    def testMeanAndVar(self):
        t = [1, 2, 2, 3, 5]
        mu = _03_thinkstats._mean(t)
        var = _03_thinkstats._var(t, mu)

        pmf = _04_Pmf._make_pmf_from_list(t)
        mu2 = pmf._mean()
        var2 = pmf._var()
        var3 = pmf._var(mu2)

        self.assertAlmostEquals(mu, mu2)
        self.assertAlmostEquals(var, var2)
        self.assertAlmostEquals(var, var3)
예제 #6
0
def _cov(xs, ys, mux=None, muy=None):
    """
    Computes Cov(X, Y).

    Args:
        xs:  sequence of values
        ys:  sequence of values
        mux: optional float mean of xs
        muy: optional float mean of ys

    Returns:
        Cov(X, Y)
    """
    if mux is None:
        mux = _03_thinkstats._mean(xs)
    if muy is None:
        muy = _03_thinkstats._mean(ys)

    total = 0.0
    for x, y in zip(xs, ys):
        total += (x - mux) * (y - muy)

    return total / len(xs)
예제 #7
0
def _estimate_rankits(n=6, m=1000):
    """
    Estimates the expected values of sorted random samples.

    Args:
        n: sample size
        m: number of iterations

    Returns: list of n rankits
    """
    t = _samples(n, m)
    t = zip(*t)
    means = [_03_thinkstats._mean(x) for x in t]
    return means
예제 #8
0
def _partition(ages, weights, bin_size=2):
    """
    Break ages into bins.

    Returns a map from age to list of weights.
    """
    weight_dict = {}
    for age, weight in zip(ages, weights):
        bin = bin_size * math.floor(age / bin_size) + bin_size / 2.0
        weight_dict.setdefault(bin, []).append(weight)

    for bin, bin_weights in weight_dict.iteritems():
        try:
            mean = _03_thinkstats._mean(bin_weights)
        except ZeroDivisionError:
            continue

    return weight_dict
예제 #9
0
 def testMean(self):
     t = [1, 1, 1, 3, 3, 591]
     mu = _03_thinkstats._mean(t)
     self.assertEquals(mu, 100)