Advertisement
Guest User

Untitled

a guest
Apr 12th, 2013
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.07 KB | None | 0 0
  1. import sys
  2. import random
  3. import numpy
  4.  
  5. def between(tested,lo,hi):
  6. return (tested>=lo) and (tested <= hi)
  7.  
  8. def makeProjects():
  9. return numpy.random.normal(755,319,9)
  10.  
  11. def allocate(projects,categories):
  12. ratios = [0]*len(categories)
  13. allocation = [result.tolist().index(1) for result in numpy.random.multinomial(1,categories,size=9)]
  14. for cat in range(len(categories)):
  15. inCat = [projects[index] for index in filter(lambda index: allocation[index] == cat, range(len(projects)))]
  16. ratios[cat] = sum(inCat)/sum(projects)
  17. return ratios
  18.  
  19. def matchesCategories(projects,error):
  20. categories = [.02, .03, .20, .29, .46]
  21. ratios = allocate(projects,categories)
  22. ratios.sort()
  23. matches = [between(comp[0],comp[1]-error,comp[1]+error) for comp in zip(ratios,categories)]
  24. return all(matches)
  25.  
  26. def simulateProjects(nb,error):
  27. print 100.0*len(filter(lambda x: matchesCategories(makeProjects(),error),range(nb)))/nb
  28.  
  29. nb=1000
  30. err=0.01
  31.  
  32. if len(sys.argv) > 1:
  33. nb = int(sys.argv[1])
  34. if len(sys.argv) > 2:
  35. err = float(sys.argv[2])
  36.  
  37. simulateProjects(nb,err)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement