Advertisement
Guest User

Agenda Composition Simulator

a guest
May 11th, 2015
253
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.92 KB | None | 0 0
  1. NUM_AGENDA_POINTS = 7
  2. DECK_SIZE = 49
  3. AGENDAS = ["TFP"]*3 + [2]*5 + [1]
  4.  
  5. import random, collections
  6.  
  7. def simulate(points, deck_size, agendas, samples):
  8.     deck = agendas + [0]*(deck_size - len(agendas))
  9.     counts = collections.defaultdict(int)
  10.     for loop in range(samples):
  11.         random.shuffle(deck)
  12.         counts[num_accesses(deck, points)] += 1
  13.     out = dict()
  14.     for k, v in counts.iteritems():
  15.         out[k] = v/float(samples)
  16.     return out
  17.  
  18. def num_accesses(deck, points):
  19.     if points == 0:
  20.         return 0
  21.     total = 0
  22.     for e, v in enumerate(deck):
  23.         if v == "TFP":
  24.             if random.random() > 0.4:
  25.                 continue
  26.             else:
  27.                 total += 3
  28.                 continue
  29.         total += v
  30.         if total >= points:
  31.             return e + 1
  32.  
  33. def test_num_accesses():
  34.     deck = [0,0,1,0,3]
  35.     assert(num_accesses(deck, 1) == 3)        
  36.     assert(num_accesses(deck, 4) == 5)
  37.     assert(num_accesses(deck, 0) == 0)
  38.  
  39. test_num_accesses()
  40.  
  41. def pprint(d):
  42.     ks = sorted(d.iterkeys())
  43.     for k in ks:
  44.         print k, d[k]
  45.  
  46. def ms(d):
  47.     mean, variance = 0, 0
  48.     for value, probability in d.iteritems():
  49.         mean += probability * value
  50.     for value, probability in d.iteritems():
  51.         variance += probability * (value - mean)**2
  52.     return mean, variance
  53.    
  54. def quantiles(d, qs):
  55.     i, q, maxi = 0, qs[0], len(qs)-1
  56.     total_probability = 0
  57.     out = dict()
  58.     for value, probability in d.iteritems():
  59.         total_probability += probability
  60.         if total_probability >= q:
  61.             out[q] = value
  62.             i += 1
  63.             if i > maxi:
  64.                 break
  65.             q = qs[i]
  66.     return out
  67.  
  68. out = simulate(NUM_AGENDA_POINTS, DECK_SIZE, AGENDAS, 100000)
  69. pprint(out)
  70. mean, variance = ms(out)
  71. q = quantiles(out, [.1,.25,.5,.75,.9])
  72. print "Quantiles:"
  73. pprint(q)
  74. print "Mean, stdev:\n", mean, variance**0.5
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement