2

The main idea is how to generate matrices where the sum of each matches values is 1. That is, I do not have matrices but would like to generate them based on this condition.

I aim to generate a list of lower.tri matrices. The main trick here is how I can generate all these matrices where the sum of every match elements is 1.

For example,

w1 <- c(0,0.7,0.8,0.5,0.2,
        0,0,0.7,0.6,0.3,
        0,0,0,0.9,0.8,
        0,0,0,0,0.3,
        0,0,0,0,0)
w1 <- matrix(w1,5,5)

 w2 <- c(0,0.3,0.2,0.5,0.8,
         0,0,0.3,0.4,0.7,
         0,0,0,0.1,0.2,
         0,0,0,0,0.7,
         0,0,0,0,0)
 w2 <- matrix(w2,5,5) 

The main idea is:

w5 <- 1 - (w1+w2+w3+w4). w1 <- 1-w2 , w3 <- 1- (w1+w2). w4 <- 1- (w1+w2+w3).

Here we can see that the sum of each matches values is 1.

For example:

> w1
     [,1] [,2] [,3] [,4] [,5]
[1,]  0.0  0.0  0.0  0.0    0
[2,]  0.7  0.0  0.0  0.0    0
[3,]  0.8  0.7  0.0  0.0    0
[4,]  0.5  0.6  0.9  0.0    0
[5,]  0.2  0.3  0.8  0.3    0
> w2
     [,1] [,2] [,3] [,4] [,5]
[1,]  0.0  0.0  0.0  0.0    0
[2,]  0.3  0.0  0.0  0.0    0
[3,]  0.2  0.3  0.0  0.0    0
[4,]  0.5  0.4  0.1  0.0    0
[5,]  0.8  0.7  0.2  0.7    0

w1[2,1] = 0.7
w2[2,1]= 0.3

Then their sum = 1. The same for all w1[k,j] + w2[k,j].

The question:

How can I generate 5 matrices where the sum of each matches values is 1. I was thinking about lapply however, I really cannot get the idea for this part.

For example, The output should a list of 5 matrices. For example,

> w1
         [,1] [,2] [,3] [,4] [,5]
    [1,]  0.0  0.0  0.0  0.0    0
    [2,]  0.5  0.0  0.0  0.0    0
    [3,]  0.6  0.6  0.0  0.0    0
    [4,]  0.5  0.6  0.7  0.0    0
    [5,]  0.2  0.2  0.2  0.3    0

    > w2
         [,1] [,2] [,3] [,4] [,5]
    [1,]  0.0  0.0  0.0  0.0    0
    [2,]  0.2  0.0  0.0  0.0    0
    [3,]  0.2  0.3  0.0  0.0    0
    [4,]  0.5  0.2  0.1  0.0    0
    [5,]  0.7  0.5  0.2  0.5    0

> w3
         [,1] [,2] [,3] [,4] [,5]
    [1,]  0.0  0.0  0.0  0.0    0
    [2,]  0.1  0.0  0.0  0.0    0
    [3,]  0.2  0.1  0.0  0.0    0
    [4,]  0.3  0.2  0.3  0.0    0
    [5,]  0.1  0.3  0.6  0.2    0

1 Answers1

0

Assuming you want positive numbers, you can use the Dirichlet distribution to generate a vector which sums to 1. For example, the rdirichlet function in the MCMCpack library. Then you can do:

library(MCMCpack)

nmatrices <- 3 # number of matrices
matrix_size <- 5
matrices <- replicate(nmatrices, matrix(0, ncol=matrix_size, nrow=matrix_size), 
                      simplify=FALSE)
for(i in seq_along(matrices)){
  matrices[[i]][lower.tri(matrices[[i]])] <- rdirichlet(1, rep(1,10))
}

Result:

> matrices
[[1]]
           [,1]       [,2]       [,3]       [,4] [,5]
[1,] 0.00000000 0.00000000 0.00000000 0.00000000    0
[2,] 0.15475899 0.00000000 0.00000000 0.00000000    0
[3,] 0.01094188 0.10190545 0.00000000 0.00000000    0
[4,] 0.18999179 0.08817135 0.06223413 0.00000000    0
[5,] 0.05055661 0.02846215 0.26253043 0.05044721    0

[[2]]
          [,1]       [,2]      [,3]       [,4] [,5]
[1,] 0.0000000 0.00000000 0.0000000 0.00000000    0
[2,] 0.1592983 0.00000000 0.0000000 0.00000000    0
[3,] 0.1277237 0.03365446 0.0000000 0.00000000    0
[4,] 0.0380273 0.07080578 0.0728188 0.00000000    0
[5,] 0.1745902 0.01504430 0.2400594 0.06797775    0

[[3]]
           [,1]       [,2]         [,3]       [,4] [,5]
[1,] 0.00000000 0.00000000 0.0000000000 0.00000000    0
[2,] 0.13537093 0.00000000 0.0000000000 0.00000000    0
[3,] 0.14060566 0.03186886 0.0000000000 0.00000000    0
[4,] 0.27152069 0.23647956 0.0008148619 0.00000000    0
[5,] 0.00176678 0.04141304 0.1052544582 0.03490516    0

EDIT

I didn't understand the question. You can do:

nmatrices <- 3 # number of matrices
matrix_size <- 4
matrices <- replicate(nmatrices, matrix(0, ncol=matrix_size, nrow=matrix_size), 
                      simplify=FALSE)
samples <- rdirichlet(matrix_size*(matrix_size-1)/2, rep(1,nmatrices))
for(m in seq_along(matrices)){
  for(k in seq_len(matrix_size*(matrix_size-1)/2)){
    matrices[[m]][lower.tri(matrices[[m]])][k] <- samples[k, m]
  }
}

Result:

> matrices
[[1]]
          [,1]      [,2]      [,3] [,4]
[1,] 0.0000000 0.0000000 0.0000000    0
[2,] 0.8444768 0.0000000 0.0000000    0
[3,] 0.0509210 0.2392519 0.0000000    0
[4,] 0.5588843 0.1921104 0.2482988    0

[[2]]
           [,1]      [,2]      [,3] [,4]
[1,] 0.00000000 0.0000000 0.0000000    0
[2,] 0.03179142 0.0000000 0.0000000    0
[3,] 0.49615251 0.2485702 0.0000000    0
[4,] 0.03481287 0.1113190 0.7224681    0

[[3]]
          [,1]      [,2]       [,3] [,4]
[1,] 0.0000000 0.0000000 0.00000000    0
[2,] 0.1237318 0.0000000 0.00000000    0
[3,] 0.4529265 0.5121779 0.00000000    0
[4,] 0.4063028 0.6965705 0.02923302    0
Stéphane Laurent
  • 75,186
  • 15
  • 119
  • 225