Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
allendowney
GitHub Repository: allendowney/thinkbayes2
Path: blob/master/scripts/effect_soln.py
1901 views
1
"""This file contains code used in "Think Bayes",
2
by Allen B. Downey, available from greenteapress.com
3
4
Copyright 2014 Allen B. Downey
5
License: GNU GPLv3 http://www.gnu.org/licenses/gpl.html
6
"""
7
8
from __future__ import print_function, division
9
10
from variability import *
11
12
import thinkplot
13
import thinkbayes2
14
15
16
def RunEstimate(update_func, num_points=31, median_flag=False):
17
"""Runs the whole analysis.
18
19
update_func: which of the update functions to use
20
num_points: number of points in the Suite (in each dimension)
21
"""
22
d = ReadHeights(nrows=None)
23
labels = {1:'male', 2:'female'}
24
25
suites = {}
26
for key, xs in d.items():
27
label = labels[key]
28
print(label, len(xs))
29
Summarize(xs)
30
31
xs = thinkbayes2.Jitter(xs, 1.3)
32
33
mus, sigmas = FindPriorRanges(xs, num_points, median_flag=median_flag)
34
suite = Height(mus, sigmas, label)
35
suites[label] = suite
36
update_func(suite, xs)
37
print('MAP', suite.MaximumLikelihood())
38
39
suite1 = suites['male']
40
suite2 = suites['female']
41
42
mu1 = suite1.Marginal(0)
43
sigma1 = suite1.Marginal(1)
44
45
mu2 = suite2.Marginal(0)
46
sigma2 = suite2.Marginal(1)
47
48
diff = mu1 - mu2
49
sigma = (sigma1 + sigma2) / 2
50
51
pmf_d = diff / sigma
52
53
thinkplot.Cdf(pmf_d.MakeCdf())
54
thinkplot.Show(xlabel='# stddev between means',
55
ylabel='PMF')
56
57
58
def main():
59
random.seed(17)
60
61
func = UpdateSuite5
62
median_flag = (func == UpdateSuite5)
63
RunEstimate(func, median_flag=median_flag)
64
65
66
if __name__ == '__main__':
67
main()
68
69
70
71