Home > database >  Create fully antithetic draws using R
Create fully antithetic draws using R

Time:01-12

say I have the matrix d, which is the result of two different realizations (rows) of a sampling procedure in two dimensions (columns). I want to develop a function that creates the fully-antithetic draws from this original matrix.

c1 <- c(0.1, 0.6);c2 <- c(0.3, 0.8);d <- rbind(c1,c2)
#    [,1] [,2]
# c1  0.1  0.6
# c2  0.3  0.8

That is to say, for example, for the first realization (c(0.1, 0.6)) I want to obtain the mirror images of this random draw in two dimensions, which generated 4 (2^2) possible combinations as follows:

d1_anthi = matrix( 
  c(   d[1,1] ,   d[1,2],
    1 - d[1,1],   d[1,2],
       d[1,1] , 1 - d[1,2],
    1 - d[1,1], 1 - d[1,2]), nrow=2,ncol=4)
t(d1_anthi)
#      [,1] [,2]
# [1,]  0.1  0.6
# [2,]  0.9  0.6
# [3,]  0.1  0.4
# [4,]  0.9  0.4

Analogously, for the second, realization the results is the following:

d2_anthi = matrix( 
  c(   d[2,1] ,   d[2,2],
       1 - d[2,1],   d[2,2],
       d[2,1] , 1 - d[2,2],
       1 - d[2,1], 1 - d[2,2]), nrow=2, ncol=4)
t(d2_anthi)
#       [,1] [,2]
# [1,]  0.3  0.8
# [2,]  0.7  0.8
# [3,]  0.3  0.2
# [4,]  0.7  0.2

Accordingly, my desired object will lock is like this:

anthi_draws <- rbind(t(d1_anthi),t(d2_anthi))
     # [,1] [,2]
# [1,]  0.1  0.6  <- original first realization
# [2,]  0.9  0.6
# [3,]  0.1  0.4
# [4,]  0.9  0.4
# [5,]  0.3  0.8  <- original second realization
# [6,]  0.7  0.8
# [7,]  0.3  0.2
# [8,]  0.7  0.2

Finally, I would like to create a function that, given a matrix of random numbers, is able to create this expanded matrix of antithetic draws. For example, in the picture below I have a sampling in three dimensions, then the total number of draws per original draw is 2^3 = 8.

enter image description here

In particular, I am having problems with the creating of the full combinatory that depends on the dimensions of the original sampling (columns of the matrix). I was planning on using expand.grid() but I couldn't create the full combinations using it. Any hints or help in order to create such a function is welcome. Thank you in advance.

CodePudding user response:

You can try this

do.call(
  rbind,
  apply(
    d,
    1,
    function(x) {
      expand.grid(data.frame(rbind(x, 1 - x)))
    }
  )
)

which gives

      X1  X2
c1.1 0.1 0.6
c1.2 0.9 0.6
c1.3 0.1 0.4
c1.4 0.9 0.4
c2.1 0.3 0.8
c2.2 0.7 0.8
c2.3 0.3 0.2
c2.4 0.7 0.2
  •  Tags:  
  • Related