""" Example for Latin Hypercube Sampling
    100 samples are drawn using LHS and for standard MC
    The means and stdevs of x and y and the mean of x*y are closer to
    the theoretical means
    For MC you would need more samples to get comparable results
    To run this, you should have installed 
    numpy, scipy, pyDOE and matplotlib
"""
import matplotlib.pyplot as plt
import scipy.stats as ST
import numpy as NP
import pyDOE

NN = 100
points = pyDOE.lhs(2, samples=NN, criterion='center')
# points = pyDOE.lhs(2, samples=NN, criterion='maximin')
x = points[:, 0]
y = points[:, 1]
print("LHS")
print(NP.corrcoef(x, y))
print(NP.mean(x), NP.mean(y))  # should be 0.5, 0.5
print(NP.std(x), NP.std(y))  # should be 0.28867, 0.28867
print(NP.mean(x*y))  # should be 0.25

xa = ST.distributions.uniform.rvs(0, 1, size=NN)
ya = ST.distributions.uniform.rvs(0, 1, size=NN)
print("Standard")
print(NP.corrcoef(xa, ya))
print(NP.mean(xa), NP.mean(ya))
print(NP.std(xa), NP.std(ya))
print(NP.mean(xa*ya))

f, (xa1, xa2) = plt.subplots(1, 2, sharex=True, sharey=True)
xa1.scatter(x, y)
xa1.set_title("LHS")

xa2.scatter(xa, ya)
xa2.set_title("standard")
plt.show()