""" Usage: python main.py :beta-value Example: python main.py 0.1 """ import numpy as np import sys from wm import weighted_majority np.random.seed (1234) T = 250 n = 10 v = 0.2 beta = float(sys.argv[1]) xs = np.where (np.random.rand (T, n) >= 0.5, 1, -1) x1s = xs[:, 0] ys = np.where(np.random.rand (1, T) >= v, x1s, -x1s).T #create a inner scope so that the value of i will be preserved, refer to this http://stackoverflow.com/questions/2295290/what-do-lambda-function-closures-capture-in-python hypothesis = [(lambda i: lambda x: x[i])(i) for i in xrange (n)] weights, cum_loss, cum_loss_nonnoise = weighted_majority(xs, ys, hypothesis, beta) #ploting the results import matplotlib.pyplot as plt rown, coln = 3,4 ts = [5, 10, 12, 15, 20, 28, 40, 60, 100, 140, 190, 250, 310] for i, t in zip(xrange (rown * coln), ts): #the normalized weight normalized_weight = weights[t, :] / np.sum (weights[t, :]) print normalized_weight plt.subplot (rown, coln, i+1) plt.bar (np.arange (n), normalized_weight) plt.title ('t = %s' %t) plt.savefig ('img/weights-beta=%.2f.png' %beta) #plotting the cumulative loss function plt.clf () plt.plot (cum_loss) plt.title ('cumulative loss function when beta = %.2f' %beta) plt.savefig ('img/cum-loss-beta=%.2f.png' %beta) #plotting the cumulative loss function considering only the non-noise case plt.clf () plt.plot (cum_loss_nonnoise) plt.title ('cumulative loss(non-noise) function when beta = %.2f' %beta) plt.savefig ('img/cum-loss-nonnoise-beta=%.2f.png' %beta)