Sampling

e13tools.sampling.lhd(n_sam, n_val, val_rng=None, method='random', criterion=None, iterations=1000, get_score=False, quickscan=True, constraints=None)[source]

Generates a Latin Hypercube Design of n_sam samples, each with n_val values. Method for choosing the ‘best’ Latin Hypercube Design depends on the method and criterion that are used.

Parameters:
  • n_sam (int) – The number of samples to generate.
  • n_val (int) – The number of values in a single sample.
Other Parameters:
 
  • val_rng (2D array_like or None. Default: None) – Array defining the lower and upper limits of every value in a sample. Requires: numpy.shape(val_rng) = (n_val, 2). If None, output is normalized.
  • method ({‘random’; ‘fixed’; ‘center’}. Default: ‘random’) – String specifying the method used to construct the Latin Hypercube Design. See Notes for more details. If n_sam == 1 or n_val == 1, method is set to the closest corresponding method if necessary.
  • criterion (float, {‘maximin’; ‘correlation’; ‘multi’} or None. Default: None) – Float or string specifying the criterion the Latin Hypercube Design has to satisfy or None for no criterion. See Notes for more details. If n_sam == 1 or n_val == 1, criterion is set to the closest corresponding criterion if necessary.
  • iterations (int. Default: 1000) – Number of iterations used for the criterion algorithm.
  • get_score (bool. Default: False) – If True, the normalized maximin, correlation and multi scores are also returned if a criterion is used.
  • quickscan (bool. Default: True) – If True, a faster but less precise algorithm will be used for the criteria.
  • constraints (2D array_like or None. Default: None) – If constraints is not empty and criterion is not None, sam_set + constraints will satisfy the given criterion instead of sam_set. Providing this argument when criterion is None will discard it. WARNING: If constraints is not a ‘fixed’ or ‘center’ lay-out LHD, the output might contain errors.
Returns:

sam_set (2D ndarray object) – Sample set array of shape [n_sam, n_val].

Notes

The ‘method’ argument specifies the way in which the values should be distributed within the value intervals.

The following methods can be used:

method interval lay-out
‘random’ Values are randomized
‘fixed’ Values are fixed to maximize spread
‘center’ Values are centered
‘r’ Same as ‘random’
‘f’ Same as ‘fixed’
‘c’ Same as ‘center’

The ‘fixed’ method chooses values in such a way, that the distance between the values is maxed out.

The ‘criterion’ argument specifies how much priority should be given to maximizing the minimum distance and minimizing the correlation between samples. Strings specify basic priority cases, while a value between 0 and 1 specifies a custom case.

The following criteria can be used (last column shows the equivalent float value):

criterion effect/priority equiv
None No priority
‘maximin’ Maximum priority for maximizing the minimum distance 0.0
‘correlation’ Maximum priority for minimizing the correlation 1.0
‘multi’ Equal priority for both 0.5
[0, 1] Priority is given according to value provided

Examples

Latin Hypercube with 5 samples with each 2 random, fixed or centered values:

>>> import numpy as np
>>> np.random.seed(0)
>>> lhd(5, 2, method='random')
array([[ 0.34303787,  0.55834501],
       [ 0.70897664,  0.70577898],
       [ 0.88473096,  0.19273255],
       [ 0.1097627 ,  0.91360891],
       [ 0.52055268,  0.2766883 ]])
>>> lhd(5, 2, method='fixed')
array([[ 0.5 ,  0.75],
       [ 0.25,  0.25],
       [ 0.  ,  1.  ],
       [ 0.75,  0.5 ],
       [ 1.  ,  0.  ]])
>>> lhd(5, 2, method='center')
array([[ 0.1,  0.9],
       [ 0.9,  0.5],
       [ 0.5,  0.7],
       [ 0.3,  0.3],
       [ 0.7,  0.1]])

Latin Hypercube with 4 samples, 3 values in a specified value range:

>>> import numpy as np
>>> np.random.seed(0)
>>> val_rng = [[0, 2], [1, 4], [0.3, 0.5]]
>>> lhd(4, 3, val_rng=val_rng)
array([[ 1.30138169,  2.41882975,  0.41686981],
       [ 0.27440675,  1.32819041,  0.48240859],
       [ 1.77244159,  3.53758114,  0.39180394],
       [ 0.85759468,  3.22274707,  0.31963924]])

Latin Hypercubes can also be created by specifying a criterion with either a string or a normalized float. The strings identify basic float values.

>>> import numpy as np
>>> np.random.seed(0)
>>> lhd(4, 3, method='fixed', criterion=0)
array([[ 0.66666667,  0.        ,  0.66666667],
       [ 1.        ,  0.66666667,  0.        ],
       [ 0.33333333,  1.        ,  1.        ],
       [ 0.        ,  0.33333333,  0.33333333]])
>>> np.random.seed(0)
>>> lhd(4, 3, method='fixed', criterion='maximin')
array([[ 0.66666667,  0.        ,  0.66666667],
       [ 1.        ,  0.66666667,  0.        ],
       [ 0.33333333,  1.        ,  1.        ],
       [ 0.        ,  0.33333333,  0.33333333]])