Find the ultimate overall variability of max and min matrix

Is there a more elegant way to determine the maximum or minimum level of variability (CV) in the marginal totals of columns of a binary matrix based on its filling and size? Given that all the totals of the rows and columns should be nonzero. eg.

foo(n_col, n_row, fill){ get maximum possible CV }

Let's say we have a matrix called mwhere all the totals of columns and rows are > 0, but the matrix is ​​minimally filled.

m <- matrix(rep(0,25), nrow = 5)
diag(m) <- 1
#     [,1] [,2] [,3] [,4] [,5]
#[1,]    1    0    0    0    0
#[2,]    0    1    0    0    0
#[3,]    0    0    1    0    0
#[4,]    0    0    0    1    0
#[5,]    0    0    0    0    1

variability1 <- sd(colSums(m))/mean(colSums(m))
variability1
# [1] 0
# the maximum and minimum for this fill is zero 
# considering that  all column and row totals must be > 0

Perhaps we could check the maximum with increasing fill levels, for example:

# find out which matrix elements are zeros
empty <- which(m < 1)
# vector for results
variability <- rep(NA, length(empty))
#
for(i in 1:length(variability)){
m[empty[[i]] ] <- 1
variability[[i]] <- sd(colSums(m))/mean(colSums(m))
}
# we get what should the maximum CV for each given level of matrix fill...
c(variability1, variability)

, ? , ?

+4
1

, fill. . OP

fill 1 n_row n_col n_col m. , m , fill [max(n_row, n_col),n_row*n_col].

fill [max(n_row, n_col),n_row*n_col],

 sd(colSums(m))/mean(colSums(m))

m , m fill 1 .

, m, m. , m , , . x, :

sd(x)/mean(x)

x [1, n_row] sum(x) fill.

, sum(x) fill, mean(x) x fill. , - sd(x) x.

x, x , , x. fill. , fill x, x . : fill fill + 1, x, ? , sum(x)=fill x , fill , x. x (.. x[i] <= n_row i [1,n_col]), : x , x. var(x):

var(x + dx) = var(x) + gradient(var(x)) %*% dx + 1/2 * t(dx) %*% Hessian(var(x)) %*% dx

dx - n_col , 1, 0 (.. ). var(x) x, . , dx , . :

gradient(var(x))[i] = 2*(x[i]-mean(x))/(n_col-1),      for all i in [1,n_col]
Hessian(var(x))[i,i] = 2/n_col                  ,      for all i in [1,n_col]

, dx. , , x , , x. , i - x, x[i], x. x. i - x, x[i] < n_row. : x , x[i] < n_row, x.

, a fill x, x , dx, x fill + 1. , x, x, x fill + 1. . , x x fill + 1, x_1 fill dx_1 ,

var(x_1 + dx_1) > var(x + dx)

, x var(x) fill, x, :

var(x_1 + dx_1) = var(x_1) + gradient(var(x_1)) %*% dx_1 + 1/2 * t(dx_1) %*% Hessian(var(x_1)) %*% dx_1
                <= var(x_1) + 2*(max(x_1)-mean(x_1))/(n_col-1) + constant
                <= var(x) + 2*(max(x)-mean(x))/(n_col-1) + constant
                = var(x + dx)

, , . :

  • 1 - 2 dx_1 at x_1 , x_1 , , gradient(var(x_1)) %*% dx_1 <= 2*(max(x_1)-mean(x_1))/(n_col-1). , x dx fill, , constant.
  • 2- 3, , var(x_1) <= var(x) fill, (ii) gradient(var(x)) %*% dx = 2*(max(x)-mean(x))/(n_col-1) dx fill, (iii) max(x_1) <= max(x), , x fill. , x_-1 fill-1. x_-1 x x_1 - x_-1 . Taylor x_-1 , x_-1 , x, , x_1, var(x_1) <= var(x). max(x_1) <= max(x). fill-k >= max(n_row, n_col), fill, 1. , , x; x_1. ( ), , max(x_1) <= max(x).

, , , x - 1. var(x), x . dx x , . x + dx , x .

fill :

  • x.
  • i - x[i] <- min(n_row, fill - (ncol_-i)). , (n_col-i) , , 1, n_row, .
  • fill <- fill - x[i]

OP,

R :

foo <- function(n_col, n_row, fill) {
  ## preallocate the vector of column sums x and initialize to NA
  x <- rep(NA, n_col)
  for (i in seq_len(n_col)) {
    x[i] <- pmin.int(n_row, fill-(n_col-i))
    fill <- fill - x[i]
  }
  ## compute the variability given the vector of column sums x
  sd(x)/mean(x)
}

, fill cumsum, :

foo <- function(n_col, n_row, fill) {
  x <- pmin.int(pmax.int(cumsum(c(fill-n_col+1,rep(-n_row+1,n_col-1))),1),n_row)
  ## compute the variability given the vector of column sums x
  sd(x)/mean(x)
}

, OP:

n_col=5
n_row=5
variability <- sapply(max(n_col,n_row):(n_col*n_row), function(fill) foo(n_col, n_row, fill))
print(variability)
## [1] 0.0000000 0.3726780 0.6388766 0.8385255 0.9938080 0.8660254 0.8131156 0.8122329 0.8426501
##[10] 0.7319251 0.6666667 0.6404344 0.6443795 0.5414886 0.4707512 0.4330127 0.4259177 0.3049184
##[19] 0.1944407 0.0931695 0.0000000
+3

Source: https://habr.com/ru/post/1661342/


All Articles