[R] Problem with minimization that I failed to understand

Rui Barradas ru|pb@rr@d@@ @end|ng |rom @@po@pt
Thu Mar 27 20:28:07 CET 2025


Às 18:35 de 27/03/2025, Daniel Lobo escreveu:
> Hi,
> 
> I have below minimization problem
> 
> 
> MyDat = structure(list(c(50L, 0L, 0L, 50L, 75L, 100L, 50L, 0L, 50L, 0L,
> 25L, 50L, 50L, 75L, 75L, 75L, 0L, 75L, 75L, 75L, 0L, 25L, 75L,
> 75L, 0L, 75L, 100L, 0L, 25L, 100L), c(75L, 0L, 0L, 50L, 100L,
> 50L, 75L, 75L, 100L, 25L, 0L, 25L, 100L, 0L, 50L, 0L, 25L, 25L,
> 100L, 75L, 0L, 0L, 0L, 50L, 0L, 75L, 75L, 0L, 50L, 25L), c(50L,
> 0L, 0L, 0L, 100L, 25L, 0L, 0L, 25L, 50L, 0L, 25L, 75L, 50L, 100L,
> 50L, 0L, 75L, 25L, 50L, 0L, 0L, 25L, 0L, 50L, 100L, 100L, 0L,
> 75L, 50L), c(25L, 0L, 0L, 75L, 75L, 25L, 50L, 50L, 100L, 25L,
> 0L, 100L, 50L, 25L, 100L, 25L, 25L, 100L, 50L, 100L, 0L, 0L,
> 100L, 50L, 0L, 50L, 75L, 0L, 50L, 25L), c(50L, 0L, 0L, 75L, 75L,
> 75L, 25L, 25L, 0L, 100L, 0L, 25L, 25L, 75L, 100L, 0L, 25L, 0L,
> 75L, 25L, 25L, 25L, 75L, 25L, 0L, 75L, 100L, 0L, 100L, 100L),
>      c(50L, 0L, 0L, 50L, 100L, 25L, 25L, 25L, 50L, 50L, 0L, 50L,
>      75L, 0L, 100L, 50L, 25L, 100L, 50L, 75L, 0L, 0L, 50L, 25L,
>      0L, 100L, 100L, 0L, 75L, 50L), c(50L, 0L, 0L, 50L, 75L, 25L,
>      75L, 50L, 100L, 25L, 0L, 75L, 25L, 0L, 50L, 0L, 50L, 75L,
>      100L, 75L, 0L, 0L, 100L, 0L, 0L, 50L, 75L, 0L, 100L, 100L
>      ), c(25L, 75L, 50L, 25L, 75L, 50L, 100L, 75L, 100L, 25L,
>      0L, 75L, 25L, 50L, 25L, 25L, 75L, 75L, 100L, 75L, 75L, 100L,
>      75L, 25L, 0L, 75L, 75L, 0L, 75L, 100L), c(55L, 30L, 20L,
>      30L, 45L, 30L, 30L, 30L, 70L, 30L, 10L, 45L, 45L, 45L, 45L,
>      30L, 30L, 55L, 45L, 45L, 30L, 30L, 30L, NA, 30L, 55L, 45L,
>      20L, 45L, 70L), c(85L, 40L, 40L, 40L, 55L, 40L, 20L, 30L,
>      30L, 30L, 20L, 30L, 70L, 40L, 85L, 55L, 30L, 40L, 30L, 55L,
>      20L, 30L, 55L, 0L, 40L, 55L, 70L, 40L, 85L, 70L), c(45L,
>      45L, 0L, 45L, 45L, 45L, 0L, 0L, 100L, 45L, 0L, 100L, 45L,
>      45L, 100L, 45L, 45L, 100L, 45L, 45L, 45L, 45L, 25L, 45L,
>      0L, 100L, 45L, 0L, 45L, 45L), c(55L, 45L, 45L, 45L, 55L,
>      45L, 45L, 45L, 45L, 45L, 45L, 45L, 45L, 45L, 55L, 55L, 45L,
>      55L, 45L, 45L, 45L, 45L, 45L, 45L, 45L, 55L, 45L, 45L, 45L,
>      45L), c(100L, 100L, 50L, 100L, 100L, 100L, 100L, 100L, 100L,
>      100L, 50L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L,
>      100L, 100L, 100L, 100L, 50L, 100L, 100L, 100L, 100L, 100L,
>      100L), c(100L, 25L, 25L, 0L, 100L, 60L, 0L, 0L, 25L, 60L,
>      0L, 60L, 100L, 60L, 100L, 100L, 25L, 100L, 60L, 100L, 100L,
>      60L, 100L, 60L, 100L, 100L, 100L, 100L, 60L, 60L), c(0L,
>      0L, 50L, 50L, 100L, 100L, 0L, 0L, 100L, 100L, 0L, 100L, 100L,
>      0L, 100L, 100L, 0L, 100L, 100L, 100L, 100L, 100L, 100L, 0L,
>      100L, 100L, 100L, 100L, 100L, 100L), c(40L, 100L, 40L, 100L,
>      100L, 40L, 100L, 100L, 100L, 40L, 100L, 100L, 100L, 100L,
>      100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L,
>      100L, 100L, 100L, 0L, 100L, 100L), c(100L, 100L, 100L, 100L,
>      100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L,
>      100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, -10L,
>      100L, 100L, 100L, -10L, 100L, 100L), c(70L, 0L, 25L, 0L,
>      100L, 25L, 0L, 0L, 0L, 45L, 0L, 25L, 100L, 100L, 100L, 100L,
>      0L, 70L, 0L, 100L, 45L, 45L, 0L, 0L, 100L, 100L, 100L, 0L,
>      100L, 100L), c(55L, 55L, 55L, 55L, 55L, 55L, 55L, 55L, 55L,
>      55L, 55L, 55L, 55L, 55L, 55L, 55L, 20L, 55L, 20L, 55L, 20L,
>      20L, 100L, 55L, 55L, 55L, 55L, 0L, 55L, 55L), c(65L, 65L,
>      100L, 65L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L,
>      100L, 100L, 100L, 100L, 65L, 100L, 100L, 100L, 65L, 100L,
>      0L, 65L, 100L, 100L, 100L, 100L, 100L, 100L), c(85L, 85L,
>      85L, 85L, 85L, 85L, 85L, 85L, 85L, 85L, 85L, 85L, 56L, 85L,
>      100L, 85L, 85L, 85L, 0L, 85L, 85L, 85L, 85L, 85L, 85L, 85L,
>      85L, 28L, 56L, 56L)), row.names = c(NA, -30L), class = "data.frame")
> 
> Fn = function(Wts) return(-Kendall::Kendall(1:Nobs,
> rank(-as.vector(as.matrix(MyDat) %*% matrix(Wts, nc = 1)[, 1, drop =
> T])))$tau[1])
> q1 = pracma::fmincon(c(0.12, 0.04, 0.07, 0.03, 0.06, 0.07, 0.07, 0.04,
> 0.09, 0.08, 0.02, 0.02, 0.03, 0.06, 0.02, 0, 0.07, 0.05, 0.02, 0.02, 0.02),
> fn = Fn,
>            A = matrix(c(rep(0, 20), -1), nrow = 1), b = -2.05/100, Aeq =
> matrix(c(rep(1, 20), 1), nrow = 1), beq = 1,
>            lb = rep(0.01, 21),
>            tol = 1e-16, maxfeval = 10000000, maxiter = 5000000)
> 
> 
> However with above code, I got sub-optimal value in terms of minimization
> of the objective function:
> 
> q1$value
> #0.1632184
> Fn(c(0.12, 0.04, 0.07, 0.03, 0.06, 0.07, 0.07, 0.04, 0.09, 0.08, 0.02,
> 0.02, 0.03, 0.06, 0.02, 0, 0.07, 0.05, 0.02, 0.02, 0.02))
> #0.1586207
> 
> Could you please help me to understand what went wrong with my code and how
> to correct that?
> 
> 	[[alternative HTML version deleted]]
> 
> ______________________________________________
> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide https://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
Hello,

I cannot reproduce your result.
Instead of checking the result with


Fn(c(0.12, 0.04, 0.07, etc, ...)


pass the actual return value, q1$par. That's what I did and the result 
matches the function minimum.
Here is the code I ran.




MyDat <- structure(list(
   c(50L, 0L, 0L, 50L, 75L, 100L, 50L, 0L, 50L, 0L,
     25L, 50L, 50L, 75L, 75L, 75L, 0L, 75L, 75L, 75L, 0L, 25L, 75L,
     75L, 0L, 75L, 100L, 0L, 25L, 100L),
   c(75L, 0L, 0L, 50L, 100L,
     50L, 75L, 75L, 100L, 25L, 0L, 25L, 100L, 0L, 50L, 0L, 25L, 25L,
     100L, 75L, 0L, 0L, 0L, 50L, 0L, 75L, 75L, 0L, 50L, 25L),
   c(50L, 0L, 0L, 0L, 100L, 25L, 0L, 0L, 25L, 50L, 0L, 25L, 75L, 50L, 100L,
     50L, 0L, 75L, 25L, 50L, 0L, 0L, 25L, 0L, 50L, 100L, 100L, 0L,
     75L, 50L),
   c(25L, 0L, 0L, 75L, 75L, 25L, 50L, 50L, 100L, 25L,
     0L, 100L, 50L, 25L, 100L, 25L, 25L, 100L, 50L, 100L, 0L, 0L,
     100L, 50L, 0L, 50L, 75L, 0L, 50L, 25L),
   c(50L, 0L, 0L, 75L, 75L,
     75L, 25L, 25L, 0L, 100L, 0L, 25L, 25L, 75L, 100L, 0L, 25L, 0L,
     75L, 25L, 25L, 25L, 75L, 25L, 0L, 75L, 100L, 0L, 100L, 100L),
   c(50L, 0L, 0L, 50L, 100L, 25L, 25L, 25L, 50L, 50L, 0L, 50L,
     75L, 0L, 100L, 50L, 25L, 100L, 50L, 75L, 0L, 0L, 50L, 25L,
     0L, 100L, 100L, 0L, 75L, 50L),
   c(50L, 0L, 0L, 50L, 75L, 25L,
     75L, 50L, 100L, 25L, 0L, 75L, 25L, 0L, 50L, 0L, 50L, 75L,
     100L, 75L, 0L, 0L, 100L, 0L, 0L, 50L, 75L, 0L, 100L, 100L),
   c(25L, 75L, 50L, 25L, 75L, 50L, 100L, 75L, 100L, 25L,
     0L, 75L, 25L, 50L, 25L, 25L, 75L, 75L, 100L, 75L, 75L, 100L,
     75L, 25L, 0L, 75L, 75L, 0L, 75L, 100L),
   c(55L, 30L, 20L,
     30L, 45L, 30L, 30L, 30L, 70L, 30L, 10L, 45L, 45L, 45L, 45L,
     30L, 30L, 55L, 45L, 45L, 30L, 30L, 30L, NA, 30L, 55L, 45L,
     20L, 45L, 70L),
   c(85L, 40L, 40L, 40L, 55L, 40L, 20L, 30L,
     30L, 30L, 20L, 30L, 70L, 40L, 85L, 55L, 30L, 40L, 30L, 55L,
     20L, 30L, 55L, 0L, 40L, 55L, 70L, 40L, 85L, 70L),
   c(45L, 45L, 0L, 45L, 45L, 45L, 0L, 0L, 100L, 45L, 0L, 100L, 45L,
     45L, 100L, 45L, 45L, 100L, 45L, 45L, 45L, 45L, 25L, 45L,
     0L, 100L, 45L, 0L, 45L, 45L),
   c(55L, 45L, 45L, 45L, 55L,
     45L, 45L, 45L, 45L, 45L, 45L, 45L, 45L, 45L, 55L, 55L, 45L,
     55L, 45L, 45L, 45L, 45L, 45L, 45L, 45L, 55L, 45L, 45L, 45L, 45L),
   c(100L, 100L, 50L, 100L, 100L, 100L, 100L, 100L, 100L,
     100L, 50L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L,
     100L, 100L, 100L, 100L, 50L, 100L, 100L, 100L, 100L, 100L, 100L),
   c(100L, 25L, 25L, 0L, 100L, 60L, 0L, 0L, 25L, 60L,
     0L, 60L, 100L, 60L, 100L, 100L, 25L, 100L, 60L, 100L, 100L,
     60L, 100L, 60L, 100L, 100L, 100L, 100L, 60L, 60L),
   c(0L, 0L, 50L, 50L, 100L, 100L, 0L, 0L, 100L, 100L, 0L, 100L, 100L,
     0L, 100L, 100L, 0L, 100L, 100L, 100L, 100L, 100L, 100L, 0L,
     100L, 100L, 100L, 100L, 100L, 100L),
   c(40L, 100L, 40L, 100L,
     100L, 40L, 100L, 100L, 100L, 40L, 100L, 100L, 100L, 100L,
     100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L,
     100L, 100L, 100L, 0L, 100L, 100L),
   c(100L, 100L, 100L, 100L,
     100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L,
     100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, -10L,
     100L, 100L, 100L, -10L, 100L, 100L),
   c(70L, 0L, 25L, 0L,
     100L, 25L, 0L, 0L, 0L, 45L, 0L, 25L, 100L, 100L, 100L, 100L,
     0L, 70L, 0L, 100L, 45L, 45L, 0L, 0L, 100L, 100L, 100L, 0L,
     100L, 100L),
   c(55L, 55L, 55L, 55L, 55L, 55L, 55L, 55L, 55L,
     55L, 55L, 55L, 55L, 55L, 55L, 55L, 20L, 55L, 20L, 55L, 20L,
     20L, 100L, 55L, 55L, 55L, 55L, 0L, 55L, 55L),
   c(65L, 65L,
     100L, 65L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L,
     100L, 100L, 100L, 100L, 65L, 100L, 100L, 100L, 65L, 100L,
     0L, 65L, 100L, 100L, 100L, 100L, 100L, 100L),
   c(85L, 85L,
     85L, 85L, 85L, 85L, 85L, 85L, 85L, 85L, 85L, 85L, 56L, 85L,
     100L, 85L, 85L, 85L, 0L, 85L, 85L, 85L, 85L, 85L, 85L, 85L,
     85L, 28L, 56L, 56L)),
   row.names = c(NA, -30L), class = "data.frame")



Fn <- function(Wts) return(
   Kendall::Kendall(
     1:Nobs,
     rank(-as.vector(as.matrix(MyDat) %*% matrix(Wts, nc = 1)[, 1, drop 
= T]))
   )$tau[1]
)

Nobs <- nrow(MyDat)

q1 <- pracma::fmincon(
   c(0.12, 0.04, 0.07, 0.03, 0.06, 0.07, 0.07, 0.04, 0.09, 0.08,
     0.02, 0.02, 0.03, 0.06, 0.02, 0, 0.07, 0.05, 0.02, 0.02, 0.02),
   fn = Fn,
   A = matrix(c(rep(0, 20), -1), nrow = 1), b = -2.05/100,
   Aeq = matrix(c(rep(1, 20), 1), nrow = 1), beq = 1,
   lb = rep(0.01, 21),
   tol = 1e-16,
   maxfeval = 10000000,
   maxiter = 5000000
)

q1$value
#> [1] -0.1632184
Fn(q1$par)
#> [1] -0.1632184



Hope this helps,

Rui Barradas


-- 
Este e-mail foi analisado pelo software antivírus AVG para verificar a presença de vírus.
www.avg.com



More information about the R-help mailing list