%pylab inline import random import matplotlib.pyplot as plt n = 10 k = 10 x = [] y = [] for i in range(n): x.append(random.random()) y.append(random.random()) part = 1. / k x.sort() y.sort() numInPartsX = [0 for i in range(k)] numInPartsY = [0 for i in range(k)] j = 0 step = part print x print print y def gist(x, numInParts, part): j = 0 step = part for i in x: while 1: if i < step: numInParts[j] += 1. break else: j += 1 step += part return numInParts print gist(x, numInPartsX, part) print print gist(y, numInPartsY, part) x2 = 0. for i in range(k): if numInPartsY[i] != 0: x2 += (numInPartsX[i] / k - numInPartsY[i] / k) ** 2 / numInPartsY[i] print x2 print x2 * k