fork download
  1. """ Example for Latin Hypercube Sampling
  2. 100 samples are drawn using LHS and for standard MC
  3. The means and stdevs of x and y and the mean of x*y are closer to
  4. the theoretical means
  5. For MC you would need more samples to get comparable results
  6. To run this, you should have installed
  7. numpy, scipy, pyDOE and matplotlib
  8. """
  9. import matplotlib.pyplot as plt
  10. import scipy.stats as ST
  11. import numpy as NP
  12. import pyDOE
  13.  
  14. NN = 100
  15. points = pyDOE.lhs(2, samples=NN, criterion='center')
  16. # points = pyDOE.lhs(2, samples=NN, criterion='maximin')
  17. x = points[:, 0]
  18. y = points[:, 1]
  19. print("LHS")
  20. print(NP.corrcoef(x, y))
  21. print(NP.mean(x), NP.mean(y)) # should be 0.5, 0.5
  22. print(NP.std(x), NP.std(y)) # should be 0.28867, 0.28867
  23. print(NP.mean(x*y)) # should be 0.25
  24.  
  25. xa = ST.distributions.uniform.rvs(0, 1, size=NN)
  26. ya = ST.distributions.uniform.rvs(0, 1, size=NN)
  27. print("Standard")
  28. print(NP.corrcoef(xa, ya))
  29. print(NP.mean(xa), NP.mean(ya))
  30. print(NP.std(xa), NP.std(ya))
  31. print(NP.mean(xa*ya))
  32.  
  33. f, (xa1, xa2) = plt.subplots(1, 2, sharex=True, sharey=True)
  34. xa1.scatter(x, y)
  35. xa1.set_title("LHS")
  36.  
  37. xa2.scatter(xa, ya)
  38. xa2.set_title("standard")
  39. plt.show()
Runtime error #stdin #stdout #stderr 0.01s 118784KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
Traceback (most recent call last):
  File "prog.py", line 9, in <module>
    import matplotlib.pyplot as plt
ImportError: No module named matplotlib