def main(): pregnancies = first.get_pregnancies_dataset() live_pregnancies = [preg for preg in pregnancies.records if preg.outcome == 1] firsts, subsequents = first.split_one_vs_all(live_pregnancies, birthord=1) period_first = first.get_values_for_field(firsts, 'prglength') period_subsequent = first.get_values_for_field(subsequents, 'prglength') var_first = thinkstats.Var(period_first) var_second = thinkstats.Var(period_subsequent) print('Variance of the first children gestation periods: {} weeks^2' .format(var_first)) print('\tAnd the sd is {} weeks.'.format(math.sqrt(var_first))) print('Variance of the subsequent children gestation periods: {} weeks^2' .format(var_second)) print('\tAnd the sd is {} weeks.'.format(math.sqrt(var_second))) weights = get_pumpkin_weights() print('Finding the variance and mean for these pumpkins: {}' .format(weights)) mu, var = pumpkins(weights) sd = math.sqrt(var) print('\tmean = {}\tvariance = {}\tsd = {}'.format(mu, var, sd)) bar_chart(pregnancies) plot_pregnancy_pmf(pregnancies) plot_pregnancies_pmf_differences(pregnancies)
def bar_chart(pregnancies): live_pregnancies = first.get_live_pregnancies(pregnancies) firsts, seconds = first.split_one_vs_all(live_pregnancies, birthord=1) period_first = first.get_values_for_field(firsts, 'prglength') period_second = first.get_values_for_field(seconds, 'prglength') hist1 = pmf.MakeHistFromList(period_first) hist2 = pmf.MakeHistFromList(period_second) fig = plt.figure() ax = fig.add_subplot(111) width = 0.35 x1, y1 = hist1.Render() x2, y2 = hist2.Render() r1 = ax.bar(x1, y1, width, color='blue') r2 = ax.bar(np.array(x2) + width, y2, width, color='grey') ax.set_xlabel('Gestation period(weeks)') ax.set_ylabel('Frequency') ax.set_title('Comparison of the gestation periods of first and subsequent' ' babies') ax.legend((r1[0], r2[0]), ('First Babies', 'Subsequent Babies')) plt.show()