# HG changeset patch # User Brian Neal # Date 1357611032 21600 # Node ID 5a9a6d1dbf1b51d2c7313a936d8921e45c505e89 # Parent 65b70b4e12adec2bc66a89b7f3377aa683847b70 Chapter 5.4, exercise 4, Pareto distributions. diff -r 65b70b4e12ad -r 5a9a6d1dbf1b ch5ex4.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/ch5ex4.py Mon Jan 07 20:10:32 2013 -0600 @@ -0,0 +1,52 @@ +"""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)