Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
| Download

📚 The CoCalc Library - books, templates and other resources

Views: 96143
License: OTHER
1
"""This file contains code for use with "Think Stats",
2
by Allen B. Downey, available from greenteapress.com
3
4
Copyright 2010 Allen B. Downey
5
License: GNU GPLv3 http://www.gnu.org/licenses/gpl.html
6
"""
7
8
import math
9
import sys
10
11
import irs
12
13
import Pmf
14
import Cdf
15
16
17
def PmfMean(pmf):
18
total = 0.0
19
for val, p in pmf.Items():
20
total += p * val
21
return total
22
23
24
def PmfMoment(pmf, mean=None, exponent=2):
25
if mean is None:
26
mean = PmfMean(pmf)
27
28
total = 0.0
29
for val, p in pmf.Items():
30
total += p * (val - mean)**exponent
31
return total
32
33
34
def RelativeMeanDifference(pmf, mean=None):
35
if mean is None:
36
mean = PmfMean(pmf)
37
38
diff = Pmf.Pmf()
39
for v1, p1 in pmf.Items():
40
for v2, p2 in pmf.Items():
41
diff.Incr(abs(v1-v2), p1*p2)
42
43
print PmfMean(diff), mean
44
45
return PmfMean(diff) / mean
46
47
48
def SummarizeData(pmf, cdf):
49
mean = PmfMean(pmf)
50
print 'mean:', mean
51
52
median = cdf.Percentile(50)
53
print 'median:', median
54
55
fraction_below_mean = cdf.Prob(mean)
56
print 'fraction below mean:', fraction_below_mean
57
58
m2 = PmfMoment(pmf, mean, 2)
59
m3 = PmfMoment(pmf, mean, 3)
60
61
sigma = math.sqrt(m2)
62
print 'sigma:', sigma
63
64
g1 = m3 / m2**(3/2)
65
print 'skewness:', g1
66
67
gp = 3 * (mean - median) / sigma
68
print 'Pearsons skewness:', gp
69
70
gini = RelativeMeanDifference(pmf) / 2
71
print 'gini', gini
72
73
def main(script, *args):
74
data = irs.ReadIncomeFile()
75
hist, pmf, cdf = irs.MakeIncomeDist(data)
76
SummarizeData(pmf, cdf)
77
78
79
if __name__ == "__main__":
80
main(*sys.argv)
81
82