lrnv
lrnv

Reputation: 1106

How can I minimize this function?

I was trying to prove there that a certain function cannot go negative. As I did not manage to make the proof, and also to convince myself that it is true, I coded the function as follows:

test = function(s,t){
  # s is a vector of positives reals of length d
  # t is a vector of complexes of length d-1
  d = length(s)
  t = c(t, (1-sum(t*s[1:(d-1)]))/s[d])
  modulii = abs((t+1)/(t-1))
  return(max(modulii)-1)
}

# I want to minimize this test function over all 
  # s positive reals of length d
  # t complex of length d-1. 

# How can i do that ? 

# simple starting points: 
d = 3
s = runif(d)
t = complex(real = runif(d-1),imaginary = runif(d-1))

test(s,t) # should be positive.

How can I code an optimization routine that minimize this function with respect to :

?

I struggle with optim and complex numbers. I want to be sure that the minimum cannot be negative ;)

Upvotes: 1

Views: 121

Answers (1)

G. Grothendieck
G. Grothendieck

Reputation: 269421

Define a function proj which takes a vector of length 3*d-2 and produces a list with s and t from it squaring the first d elements to form s and taking the next d-1 and following d-1 elements as the real and imaginary parts of t. Then define f to run proj and pass the result to test and run.

d <- 3
proj <- function(x) {
  d <- (length(x) + 2) / 3
  list(s = head(x, d)^2, t = complex(x[seq(d+1, length = d-1)], tail(x, d-1)))
}   
f <- function(x) with(proj(x), test(s, t))
result <- optim(rep(0.5, 3*d-2), f)
result
## $par
## [1]  1.0863555573  5.9011341467 -0.0009866435 -0.1252050359  1.0720624611
## [6] -0.3826544395 -6.2322265938
##
## $value
## [1] 8.911303e-09
##
## $counts
## function gradient 
##      188       NA 
##
## $convergence
## [1] 0
##
## $message
## NULL

proj(result$par)
## $s
## [1] 1.180168e+00 3.482338e+01 9.734655e-07
##
## $t
## [1] -0.3826544+0i -6.2322266+0i

Upvotes: 3

Related Questions