1

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

MrFlick
  • 195,160
  • 17
  • 277
  • 295
  • 1
    How are you defining correlation between two functions? Are you assuming these are density functions and are considering the expected correlations between samples of these distributions? What exactly are you trying to optimize here? I'm not sure I understand what you are trying to do. – MrFlick Jun 08 '22 at 12:53
  • I agree with @MrFlivk . The obvious way to do this is to create a third function which takes the lambda parameter and feeds it to the other two functions. For example, to find the lambda that minimizes the difference between the outputs of both functions you would define `f3 <- function(lambda) f1(lambda, 0.25) - f2(lambda, 0.25)` then run e.g. `optimize(f3, 0, 100)`, but it is not clear whether you want to minimize the difference, the product, or something else. – Allan Cameron Jun 08 '22 at 13:01
  • okay, I try to be more clear. My objective is to implement a dynamic nelson siegel model. Diebold and Li shows a two step procedure which consitis in fixing lambda and then estimate the reamining part. For this reason, I want to find the lambda that minimizes the squared correlation between slope (f1) and curvatore (f2) factor loadings. So first i have defined the two factor loadings, then for tau going from 1 month to 10 years i want to find the lambda that minimizes the correlation between the two factors. I don't know if this explanation is sufficient – Davide Grossoni Jun 08 '22 at 13:11
  • I have attached a picture representing the minimization problem i want to perform in the original post, but probably you cannot see it – Davide Grossoni Jun 08 '22 at 13:16

2 Answers2

1

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
Allan Cameron
  • 147,086
  • 7
  • 49
  • 87
  • I think you are correct but I have some issues. First, you say "for each value of lambda" but I do not know lambda for sure, I know tau but not lambda. Can you explain the meaning of that sentence please? Only for understanding purposes. Thank you very much – Davide Grossoni Jun 08 '22 at 14:18
  • In order to find the optimum value of lambda, one needs to try out lots of different values of lambda and find out which produces the minimum squared correlation. This is what `optimize` does under the hood. It uses gradient descent to move towards the optimum value of lambda. Although you only get one actual lambda value at the end, it has been selected from lots of possibilities as being the value that minimises the squared correlation. Does that make sense? – Allan Cameron Jun 08 '22 at 14:42
0

Perhaps the examples at the bottom of the page help: https://search.r-project.org/CRAN/refmans/NMOF/html/NSf.html

They input a vector of times into the functions (which is fixed for a given yield-curve), so you can compute the correlation for a given lambda. To minimize the correlation, do a grid search over the lambdas. In your case, for instance,

lambda <- 2
cor(f1(lambda, 1:10), f2(lambda, 1:10))

Note that I have assumed maturity measured in years, 1 to 10. You will need to fill in appropriate values. To find a lambda that leads to a low correlation, you could run a grid search.

lambdas <- seq(0.00001, 25, length.out = 1000)
squared.corr <- rep(NA_real_, length(lambdas))

for (i in seq_along(lambdas)) {
    c <- cor(f1(lambdas[i], 1:10),
             f2(lambdas[i], 1:10))
    squared.corr[i] <- c*c
}
lambdas[which.min(c2)]
## [1] 0.490

(I am one of the authors of Gilli, Grosse and Schumann (2010), on which the suggestion to minimize the correlation is based.)

Enrico Schumann
  • 1,278
  • 7
  • 8
  • I think not, because I need to estimate lambda to apply in the DNS model and one method from https://mpra.ub.uni-muenchen.de/68377/1/MPRA_paper_68377.pdf consists of minimizing the squared correlation between curvatore and slope. In the example above given lambda the code will return the factors – Davide Grossoni Jun 08 '22 at 13:24
  • I do not understand why you set lambda equal to 2 if my objective is to find the optimal one – Davide Grossoni Jun 08 '22 at 14:28
  • It was meant to illustrate the computation. – Enrico Schumann Jun 08 '22 at 14:37