Search code examples
roptimizationcorrelationminimizationpearson-correlation

How to estimate the correlation between two functions with unknown variables inside?


I need to solve this optimization problem in order to estimate lambda:

minimization problem

Basically, I need to find the correlation between these two functions:

f1 <- function(lambda, tau){slope = (1-exp(-lambda*tau))/(lambda*tau)            
      return(slope)}

f2 <- function(lambda, tau){curve = ((1-exp(-lambda*tau))/(lambda*tau))-exp(-lambda*tau) 
 return(curve)}

I know the different values of tau. Suppose for example tau = 0.25: now f1 and f2 have only one missing parameter, lambda, which should be estimated. However, when I try to implement the optim() function to be minimized, it does not work since f1 and f2 are not numeric. How can I build this kind of optimization problem mantaining f1 and f2 as functions?

Many thanks


Solution

  • If I am understanding correctly, you are trying to minimise the squared correlation between the output of f1 and f2 at different values of lambda. This means that for each value of lambda you are assessing, you need to feed in the complete vector of tau values. This will give a vector output for each value of lambda so that a correlation between the output from the two functions can be calculated at any single value of lambda.

    To do this, we create a vectorized function that takes lambda values and calculates the squared correlation between f1 and f2 at those values of lambda across all values of tau

    f3 <- function(lambda) {
      sapply(lambda, function(l) {
          cor(f1(l, seq(1/12, 10, 1/12)), f2(l, seq(1/12, 10, 1/12)))^2
      })
    }
    

    To get the optimal value of lambda that minimizes the squared correlation, we just use optimize:

    optimize(f3, c(0, 100))$minimum
    #> [1] 0.6678021