DEMutilities. lhs_samples

In order to be able to use this module import it like this:

import DEMutilities.lhs_samples
#or assign it to a shorter name
import DEMutilities.lhs_samples as lhs
DEMutilities.lhs_samples.lhs(dist, parms, siz=100, noCorrRestr=False, corrmat=None)

Latin Hypercube sampling of any distribution. dist is is a scipy.stats random number generator such as stats.norm, stats.beta, etc parms is a tuple with the parameters needed for the specified distribution.

  • dist: random number generator from scipy.stats module or a list of them.
  • parms: tuple of parameters as required for dist, or a list of them.
  • siz :number or shape tuple for the output sample
  • noCorrRestr: if true, does not enforce correlation structure on the sample.
  • corrmat: Correlation matrix
DEMutilities.lhs_samples.lhsFromDensity(kde, siz=100)

LHS sampling from a variable’s Kernel density estimate.

  • kde: scipy.stats.kde.gaussian_kde object
  • siz: Number or shape tuple for the output sample
DEMutilities.lhs_samples.lhsFromSample(sample, siz=100)

Latin Hypercube Sample from a set of values. For univariate distributions only

  • sample: list, tuple of array
  • siz: Number or shape tuple for the output sample
DEMutilities.lhs_samples.make_lhs(cps, N, distribution='uniform', dirkey='dir')

Returns a list of dictionaries with the same keys as in ‘cps’, and the values represent a latin hyper square design of the given value ranges in cps, wich must be lists of len==2. The design will contain a total of ‘N’ samples. Currently, uniform or normal (recommended for parameter studies) distributions are supported; for ‘uniform’ the values of cps will be interpreted as [min,max], for ‘normal’ it will be [mean,std].

Strings as dictionary values will be interpreted as fully specified dictionary keys whose values have to be taken over; e.g. E modulus in all contact models of the same particle.

-cps dictionary of parameters to be varied.
-N number of samples

-distribution can be either uniform or normal -dirkey this key will be added to the resulting dictionaries with a value of a possible directory name.

DEMutilities.lhs_samples.rank_restr(nvars=4, smp=100, noCorrRestr=False, Corrmat=None)

Returns the indices for sampling variables with the desired correlation structure.

  • nvars: number of variables
  • smp: number of samples
  • noCorrRestr: No correlation restriction if True
  • Corrmat: Correlation matrix. If None, assure uncorrelated samples.

Modify a sequence in-place by shuffling its contents.

This function only shuffles the array along the first axis of a multi-dimensional array. The order of sub-arrays is changed but their contents remains the same.

x : array_like
The array or list to be shuffled.


>>> arr = np.arange(10)
>>> np.random.shuffle(arr)
>>> arr
[1 7 5 2 9 4 3 6 0 8]

Multi-dimensional arrays are only shuffled along the first axis:

>>> arr = np.arange(9).reshape((3, 3))
>>> np.random.shuffle(arr)
>>> arr
array([[3, 4, 5],
       [6, 7, 8],
       [0, 1, 2]])
DEMutilities.lhs_samples.uniform(low=0.0, high=1.0, size=None)

Draw samples from a uniform distribution.

Samples are uniformly distributed over the half-open interval [low, high) (includes low, but excludes high). In other words, any value within the given interval is equally likely to be drawn by uniform.

low : float or array_like of floats, optional
Lower boundary of the output interval. All values generated will be greater than or equal to low. The default value is 0.
high : float or array_like of floats
Upper boundary of the output interval. All values generated will be less than high. The default value is 1.0.
size : int or tuple of ints, optional
Output shape. If the given shape is, e.g., (m, n, k), then m * n * k samples are drawn. If size is None (default), a single value is returned if low and high are both scalars. Otherwise, np.broadcast(low, high).size samples are drawn.
out : ndarray or scalar
Drawn samples from the parameterized uniform distribution.

randint : Discrete uniform distribution, yielding integers. random_integers : Discrete uniform distribution over the closed

interval [low, high].

random_sample : Floats uniformly distributed over [0, 1). random : Alias for random_sample. rand : Convenience function that accepts dimensions as input, e.g.,

rand(2,2) would generate a 2-by-2 array of floats, uniformly distributed over [0, 1).

The probability density function of the uniform distribution is

p(x) = \frac{1}{b - a}

anywhere within the interval [a, b), and zero elsewhere.

When high == low, values of low will be returned. If high < low, the results are officially undefined and may eventually raise an error, i.e. do not rely on this function to behave when passed arguments satisfying that inequality condition.

Draw samples from the distribution:

>>> s = np.random.uniform(-1,0,1000)

All values are within the given interval:

>>> np.all(s >= -1)
>>> np.all(s < 0)

Display the histogram of the samples, along with the probability density function:

>>> import matplotlib.pyplot as plt
>>> count, bins, ignored = plt.hist(s, 15, density=True)
>>> plt.plot(bins, np.ones_like(bins), linewidth=2, color='r')