Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download

Think Stats by Allen B. Downey Think Stats is an introduction to Probability and Statistics for Python programmers.

This is the accompanying code for this book.

Website: http://greenteapress.com/wp/think-stats-2e/

Views: 7116
License: GPL3
1
"""This file contains code used in "Think Stats",
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
9
10
11
import random
12
import numpy as np
13
import scipy.stats
14
15
def write_sample(sample, filename):
16
"""Write a sequence of floats to a file.
17
"""
18
fp = open(filename, 'w')
19
for x in sample:
20
fp.write('%f\n' % x)
21
fp.close()
22
23
def uniform_sample(n):
24
return [random.uniform(0, 100) for i in range(n)]
25
26
def triangular_sample(n):
27
return [random.triangular(0, 100) for i in range(n)]
28
29
def expo_sample(n):
30
return [random.expovariate(1.0/50) for i in range(n)]
31
32
def gauss_sample(n):
33
return [random.gauss(50, 25) for i in range(n)]
34
35
def lognorm_sample(n):
36
return [random.lognormvariate(3, 1.3) for i in range(n)]
37
38
def pareto_sample(n):
39
return [10 * random.paretovariate(1.2) for i in range(n)]
40
41
def weibull_sample(n):
42
return [random.weibullvariate(60, 5) for i in range(n)]
43
44
def gumbel_sample(n):
45
rv = scipy.stats.gumbel_r(45, 10)
46
return rv.rvs(n)
47
48
def main():
49
50
funcs = [uniform_sample, triangular_sample, expo_sample,
51
gauss_sample, lognorm_sample, pareto_sample,
52
weibull_sample, gumbel_sample]
53
54
for i in range(len(funcs)):
55
sample = funcs[i](1000)
56
print(np.mean(sample))
57
filename = 'mystery%d.dat' % i
58
write_sample(sample, filename)
59
60
61
if __name__ == '__main__':
62
main()
63
64