| 1 |
"""Plot histogram of SortedList sublist lengths while items are added. |
| 2 |
|
| 3 |
Script output is a video named `sublist-lengths.mp4`. The video displays a line |
| 4 |
graph where the x-axis is sublist length and the y-axis is the percentage of |
| 5 |
sublists with that length. Sublist lengths are divided into buckets as with a |
| 6 |
histogram. A line fitting the histogram as a normal distribution is plotted |
| 7 |
alongside in green. |
| 8 |
|
| 9 |
A line graph was used to represent the histogram because it was easier to |
| 10 |
animate. |
| 11 |
|
| 12 |
Observations: |
| 13 |
|
| 14 |
* Average sublist length cycles between `load` and `load * 2` with an |
| 15 |
exponentially increasing period. |
| 16 |
|
| 17 |
* As the number of elements increases, the sublist length distribution can be |
| 18 |
approximated by a normal distribution. The fit is valid within 30% of the |
| 19 |
mean. |
| 20 |
|
| 21 |
* At the end of the video, the normal distribution fitting the histogram with |
| 22 |
mean=1500 has standard deviation=102.94. |
| 23 |
|
| 24 |
* Observing the mean and standard deviation over time shows that the standard |
| 25 |
deviation tends to increase at a given mean over time. In this way, the curve |
| 26 |
flattens as more values are added to it. |
| 27 |
|
| 28 |
""" |
| 29 |
|
| 30 |
from __future__ import division, print_function |
| 31 |
|
| 32 |
import argparse |
| 33 |
import itertools as it |
| 34 |
import numpy as np |
| 35 |
import scipy |
| 36 |
import scipy.stats |
| 37 |
import sortedcontainers as sc |
| 38 |
from matplotlib import pyplot as plt |
| 39 |
from matplotlib import animation |
| 40 |
import random |
| 41 |
|
| 42 |
DISPLAY_FIT = False |
| 43 |
|
| 44 |
def background(): |
| 45 |
"Plot the background of each animated frame." |
| 46 |
hist_line.set_data([], []) |
| 47 |
norm_line.set_data([], []) |
| 48 |
return hist_line, norm_line |
| 49 |
|
| 50 |
|
| 51 |
def frame(num): |
| 52 |
"Draw frame." |
| 53 |
values.update(func() for func in it.repeat(random.random, LOAD)) |
| 54 |
data = np.array([len(sublist) for sublist in values._lists]) |
| 55 |
hist, bins = np.histogram(data, bins=BINS, normed=True) |
| 56 |
hist_line.set_data(bins[:-1], hist) |
| 57 |
norm = scipy.stats.norm |
| 58 |
params = norm.fit(data) |
| 59 |
norm_line.set_data(LENGTHS, norm.pdf(LENGTHS, *params)) |
| 60 |
|
| 61 |
if DISPLAY_FIT: |
| 62 |
print('Normal', '%10.3f %10.3f' % params) |
| 63 |
|
| 64 |
return hist_line, norm_line |
| 65 |
|
| 66 |
|
| 67 |
if __name__ == '__main__': |
| 68 |
parser = argparse.ArgumentParser() |
| 69 |
parser.add_argument('--seed', type=int, default=0) |
| 70 |
parser.add_argument('--load', type=float, default=1e3) |
| 71 |
parser.add_argument('--show', action='store_true') |
| 72 |
parser.add_argument('--filename', default='sublist-lengths-add.mp4') |
| 73 |
parser.add_argument('--display-fit', action='store_true') |
| 74 |
|
| 75 |
args = parser.parse_args() |
| 76 |
|
| 77 |
random.seed(args.seed) |
| 78 |
|
| 79 |
LOAD = int(args.load) |
| 80 |
BINS = np.array(range(LOAD, LOAD * 2 + 1, LOAD // 20)) |
| 81 |
LENGTHS = np.array(range(LOAD, LOAD * 2)) |
| 82 |
DISPLAY_FIT = args.display_fit |
| 83 |
|
| 84 |
values = sc.SortedList(load=LOAD) |
| 85 |
|
| 86 |
# Initialize with random values. |
| 87 |
|
| 88 |
for func in it.repeat(random.random, LOAD): |
| 89 |
values.add(func()) |
| 90 |
|
| 91 |
fig = plt.figure() |
| 92 |
bounds = (LOAD - 5 * LOAD // 100, LOAD * 2 - 5 * LOAD // 100) |
| 93 |
ax = plt.axes(xlim=bounds, ylim=(0, 0.01)) |
| 94 |
hist_line, = ax.plot([], []) |
| 95 |
norm_line, = ax.plot([], []) |
| 96 |
|
| 97 |
animator = animation.FuncAnimation( |
| 98 |
fig, frame, init_func=background, frames=1000, interval=10, blit=True, |
| 99 |
) |
| 100 |
|
| 101 |
if args.show: |
| 102 |
plt.show() |
| 103 |
else: |
| 104 |
animator.save( |
| 105 |
args.filename, fps=20, extra_args=['-vcodec', 'libx264'] |
| 106 |
) |