Chapter 6, exercise 2, #4. Wrote a program to output the center column of
a rule 30 CA as a stream of bytes. It is very slow though. It has to run a very
long time to produce enough data for dieharder. Committing it now but will have
to let it run overnight or something to generate a large file.
author |
Brian Neal <bgneal@gmail.com> |
date |
Sun, 13 Jan 2013 16:24:00 -0600 |
parents |
5a9a6d1dbf1b |
children |
|
rev |
line source |
bgneal@30
|
1 """Chapter 5.3 exercise 4 in Allen Downey's Think Complexity book.
|
bgneal@30
|
2
|
bgneal@30
|
3 "Write a version of plot_ccdf that plots the complementary CCDF on a log-log
|
bgneal@30
|
4 scale.
|
bgneal@30
|
5
|
bgneal@30
|
6 To test your function, use paretovariate from the random module to
|
bgneal@30
|
7 generate 100 values from a Pareto distribution. Plot the CCDF on a log-y scale
|
bgneal@30
|
8 and see if it falls on a straight line. What happens to the curve as you
|
bgneal@30
|
9 increase the number of values?"
|
bgneal@30
|
10
|
bgneal@30
|
11 """
|
bgneal@30
|
12 import random
|
bgneal@30
|
13 import sys
|
bgneal@30
|
14
|
bgneal@30
|
15 import matplotlib.pyplot as pyplot
|
bgneal@30
|
16
|
bgneal@30
|
17
|
bgneal@30
|
18 def plot_ccdf_log_log(x_vals, y_vals, title=''):
|
bgneal@30
|
19 """Given a set of x-values and y-values from a continuous distribution, plot
|
bgneal@30
|
20 the complementary distribution (CCDF) on a log-log scale.
|
bgneal@30
|
21
|
bgneal@30
|
22 """
|
bgneal@30
|
23 if len(x_vals) != len(y_vals):
|
bgneal@30
|
24 raise ValueError
|
bgneal@30
|
25
|
bgneal@30
|
26 ys = [1.0 - y for y in y_vals]
|
bgneal@30
|
27
|
bgneal@30
|
28 pyplot.clf()
|
bgneal@30
|
29 pyplot.xscale('log')
|
bgneal@30
|
30 pyplot.yscale('log')
|
bgneal@30
|
31 pyplot.title(title)
|
bgneal@30
|
32 pyplot.xlabel('x')
|
bgneal@30
|
33 pyplot.ylabel('1-y')
|
bgneal@30
|
34 pyplot.plot(x_vals, ys, label='1-y', color='green', linewidth=3)
|
bgneal@30
|
35 pyplot.legend(loc='upper right')
|
bgneal@30
|
36 pyplot.show()
|
bgneal@30
|
37
|
bgneal@30
|
38
|
bgneal@30
|
39 def main(script, n, alpha):
|
bgneal@30
|
40
|
bgneal@30
|
41 n = int(n)
|
bgneal@30
|
42 alpha = float(alpha)
|
bgneal@30
|
43
|
bgneal@30
|
44 x_vals = range(n)
|
bgneal@30
|
45 y_vals = [random.paretovariate(alpha) for x in x_vals]
|
bgneal@30
|
46 y_vals.sort(reverse=True)
|
bgneal@30
|
47
|
bgneal@30
|
48 plot_ccdf_log_log(x_vals, y_vals, 'paretovariate w/alpha = {}'.format(alpha))
|
bgneal@30
|
49
|
bgneal@30
|
50
|
bgneal@30
|
51 if __name__ == '__main__':
|
bgneal@30
|
52 main(*sys.argv)
|