Mercurial > public > think_complexity
view ch5ex4.py @ 35:10db8c3a6b83
Chapter 5.5, exercise 6, #2: plot P(k) vs. k for a WS graph.
author | Brian Neal <bgneal@gmail.com> |
---|---|
date | Wed, 09 Jan 2013 20:51:16 -0600 |
parents | 5a9a6d1dbf1b |
children |
line wrap: on
line source
"""Chapter 5.3 exercise 4 in Allen Downey's Think Complexity book. "Write a version of plot_ccdf that plots the complementary CCDF on a log-log scale. To test your function, use paretovariate from the random module to generate 100 values from a Pareto distribution. Plot the CCDF on a log-y scale and see if it falls on a straight line. What happens to the curve as you increase the number of values?" """ import random import sys import matplotlib.pyplot as pyplot def plot_ccdf_log_log(x_vals, y_vals, title=''): """Given a set of x-values and y-values from a continuous distribution, plot the complementary distribution (CCDF) on a log-log scale. """ if len(x_vals) != len(y_vals): raise ValueError ys = [1.0 - y for y in y_vals] pyplot.clf() pyplot.xscale('log') pyplot.yscale('log') pyplot.title(title) pyplot.xlabel('x') pyplot.ylabel('1-y') pyplot.plot(x_vals, ys, label='1-y', color='green', linewidth=3) pyplot.legend(loc='upper right') pyplot.show() def main(script, n, alpha): n = int(n) alpha = float(alpha) x_vals = range(n) y_vals = [random.paretovariate(alpha) for x in x_vals] y_vals.sort(reverse=True) plot_ccdf_log_log(x_vals, y_vals, 'paretovariate w/alpha = {}'.format(alpha)) if __name__ == '__main__': main(*sys.argv)