2
$\begingroup$

Info: Newbie in Latin Hypercube Sampling (LHS)

I have a number of variables/parameters that have different number of elements:

$A = [1,2,3],$ $B = [1,2],$ and $C = [1,2,3,4]$

From these arrays I am creating a dataset (Cartesian product):

$$D = A \times B \times C$$

For the created dataset I have to run simulations in order to get the outputs/results, let's say:

$$\begin{array}{c|c|c|c|} & \text{A} & \text{B} & \text{C} \\ \hline \text{Row 1} & 1 & 1 & 1 \\ \hline \text{Row 2} & 1 & 1 & 2 \\ \hline \dots & \dots & \dots & \dots \\ \hline \text{Row 24} & 3 & 2 & 4\\ \hline \end{array}$$

Since the dataset could be very big and simulations are time-consuming, I want to use LHS to get samples from Dataset. How can I do it when I have different sizes of elements in different arrays, is it doable?

Also, in a case when I'd have the same size arrays, let's say:

$A = [1,2,3],$ $B = [1,2,3]$

I would have square dataset after Cartesian product of $A$ and $B$:

$\begin{array}{|c|c|c|c|} \hline \text{3} & & & \\ \hline \text{2} & & & \\ \hline \text{1} & & & \\ \hline & \text{1} & 2 & 3 \\ \hline \end{array}$ After LHS (random): $\begin{array}{|c|c|c|c|} \hline \text{3} & * & & \\ \hline \text{2} & & & * \\ \hline \text{1} & & * & \\ \hline & \text{1} & 2 & 3 \\ \hline \end{array}$ or $\begin{array}{|c|c|c|c|} \hline \text{3} & & & * \\ \hline \text{2} & & * & \\ \hline \text{1} & * & & \\ \hline & \text{1} & 2 & 3 \\ \hline \end{array}$

So, in this case, from the dataset generated I have to take:

$\begin{array}{c|c|c|} & \text{A} & \text{B} \\ \hline \text{Row 1} & 1 & 3 \\ \hline \text{Row 2} & 2 & 1 \\ \hline \text{Row 3} & 3 & 2\\ \hline \end{array}$ or $\begin{array}{c|c|c|} & \text{A} & \text{B}\\ \hline \text{Row 1} & 1 & 1 \\ \hline \text{Row 2} & 2 & 2 \\ \hline \text{Row 3} & 3 & 3\\ \hline \end{array}$

Is this the right way of using LHS? And how could I use it in the case when I have different sizes of arrays?

$\endgroup$
0
$\begingroup$

This R code should do what you were asking for...

require(lhs)

A <- c(1,2,3)
B <- c(1,2)
C <- c(1,2,3,4)

D <- expand.grid(A,B,C)

print(D)

# out of the 24 possible combinations, say that you wanted to find 10
#   combinations of the 3 variables

set.seed(1976)
X <- randomLHS(n = 10, k = 3)
pairs(X, labels = c("A","B","C"))

# now transforming the samples to your integers...
Y <- X
Y[,1] <- 1 + floor(X[,1] * length(A))
Y[,2] <- 1 + floor(X[,2] * length(B))
Y[,3] <- 1 + floor(X[,3] * length(C))

# or using a loop

Y <- X
lens <- c(length(A), length(B), length(C))
for (i in 1:3)
{
  Y[,i] <- 1 + floor(Y[,i] * lens[i])
}

pairs(Y, labels = c("A","B","C"), xlim = c(0,5), ylim = c(0,5), pch=19)

print(Y)

# check that there are no duplicates
nrow(Y) == nrow(unique(as.data.frame(Y)))
$\endgroup$
  • $\begingroup$ I see. I have just another question. What if I have an array A <- c(-4,-3,-2)? How could I fix it at the floor code? $\endgroup$ – flowcyan Dec 9 '19 at 14:40
  • $\begingroup$ You would use the Latin hypercube sample to index the A vector. Like this... Y[,1] <- A[1 + floor(X[,1] * length(A))] $\endgroup$ – R Carnell Dec 11 '19 at 2:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.