def _make_normal_model(self, weights, root, xmax=175, xlabel='adult weight (kg)', axis=None): cdf = _13_Cdf._make_cdf_from_list(weights) pyplot.clf() t = weights[:] t.sort() mu, var = _03_thinkstats._trimmed_mean_var(t) print('n, Mean, Var', len(weights), mu, var) sigma = math.sqrt(var) print('Sigma', sigma) xs, ps = continuous._render_normal_cdf(mu, sigma, xmax) pyplot.plot(xs, ps, label='model', linewidth=4, color='0.7') xs, ps = cdf._render() pyplot.plot(xs, ps, label='data', linewidth=2, color='blue') _05_myplot._save(root, title='Adult weight', xlabel=xlabel, ylabel='CDF', axis=axis or [0, xmax, 0, 1])
def _summarize_weight(self): """Print summary statistics for male and female weight.""" # make a dictionary that maps from gender code to list of weights d = {1: [], 2: [], 'all': []} [d[r.sex].append(r.weight2) for r in self.records if r.weight2 != 'NA'] [d['all'].append(r.weight2) for r in self.records if r.weight2 != 'NA'] print('Weight (kg):') print('key n mean var sigma cv') for key, t in d.iteritems(): mu, var = _03_thinkstats._trimmed_mean_var(t) sigma = math.sqrt(var) cv = sigma / mu print(key, len(t), mu, var, sigma, cv)
def _make_normal_model(weights): """Plot the CDF of birthweights with a normal model.""" # estimate parameters: trimming outliers yields a better fit mu, var = _03_thinkstats._trimmed_mean_var(weights, p=0.01) print('Mean, Var', mu, var) # plot the model sigma = math.sqrt(var) print('Sigma', sigma) xs, ps = _render_normal_cdf(mu, sigma, 200) pyplot.clf() pyplot.plot(xs, ps, label='model', linewidth=4, color='0.8') # plot the data cdf = _13_Cdf._make_cdf_from_list(weights) xs, ps = cdf._render() pyplot.plot(xs, ps, label='data', linewidth=2, color='blue') _05_myplot._save('nsfg_birthwgt_model', title='Birth weights', xlabel='birth weight (oz)', ylabel='CDF')