Problem integrating a option_model in R

Hello

I would like to estimate the parameters of an option_model below ***. It works over one point/day but over several points/days i got an error message "Error in integrate(P1, lower = 0, upper = Inf, S, X, tau, r, q, v0, vT, : evaluation of function gave a result of wrong length". I know it is a matter of vectorisation.
For example with the below sample, i would like to estimate over the full sample the five parameters (v0, vT, rho, k, sigma).

Any other code for equivalent model is okay. Thanks for any help.

|S | K | r | T | vol | black_scholes_price | call_price|
|10 | 5 | 0 | 0.0833333333333333 | 0.1 | 5 | 5.04015662835657|
|10 | 5 | 0 | 0.0833333333333333 | 0.2 | 5 | 4.85949388084548|
|10 | 5 | 0 | 0.0833333333333333 | 0.3 | 5 | 5.08943090813426|
|10 | 5 | 0 | 0.0833333333333333 | 0.4 | 5 | 4.95319345262156|
|10 | 5 | 0 | 0.166666666666667 | 0.1 | 5 | 5.05969686378105|
|10 | 5 | 0 | 0.166666666666667 | 0.2 | 5 | 4.92278056639807|
|10 | 5 | 0 | 0.166666666666667 | 0.3 | 5 | 4.9172201320526|
|10 | 5 | 0 | 0.166666666666667 | 0.4 | 5 | 4.93092494872619|
|10 | 5 | 0 | 0.25 | 0.1 | 5 | 4.55768128699368|
***R: Price of a European Call under the Heston Model

Well it seems that my post has no success :wink: it is a question of posting or not a draft code maybe? or subject ?

Hello! I believe you're not getting any answers because there is no reprex. If you could provide one, it would be useful to know where exactly the code is failing.

Hello, you are right, i m posting a basic code to improve. The objective is to estimate the parameters of the model over the posted ten rows above. It works for each rows, but not for all the ten. This is due to a problem with the loop. Thanks a lot.

j=10 #nb of lines to include in the parameter estimation
v0 = 0.2
vT = 0.2
rho= 0.5
k= 0.2
sig= 0.05
var <- c(v0,vT,rho,k,sig)
func_optim <- function(var){
for(i in 1:j) {
prices <- df_date[i]
v0= var[1]
vT= var[2]
rho= var[3]
k= var[4]
mserr <- (call(S=df_data[i],X=df_data[i],tau=df_data[i],r=df_data[i],q=0, v, vT, rho,k)-prices)^2
return(mserr)
}}
optimize(var, func_optim)

So, a couple of comments:

  1. The original error you cited simply comes from looping over 10 values when the number of rows in that dataset is 9. The proper way to define j would be j <- nrow(df_data), if that is indeed what you were trying to do. However, that's not the only problem.

  2. You don't need to define v0, vT, rho, k, sig inside each loop, since their values are always the same once you call the func_optim function.

  3. func_optim has return(...) inside a loop. This means that, when the function reaches that return(...) in the first loop, it will stop, returning one single value corresponding to i = 1.

  4. Even if you place return(mserr) outside of the loop, it will still only return one single value, in that case corresponding to the last iteration of the loop. I don't think this is what you want, but I'm not sure.

R functions return a single object, so you should think beforehand what you want this object to be. If you need multiple values, you will need to define a vector, a list, or a similar object which lets you store more than one value.

  1. Finally, and this is just a suggestion, try to avoid repeating names from objects inside functions, to avoid mix-ups. So in this case, var could be the function parameter, and var0 could be the vector with your values:
library(NMOF)

j <- nrow(df_data) # To loop over the number of rows in the dataset

func_optim <- function(var){ # var is the function parameter
  # These values are defined outside the loop:
  v0 <- var[1]
  vT <- var[2]
  rho <- var[3]
  k <- var[4]
  sig <- var[5]
  
  for(i in 1:j){
    prices <- df_data$call_price[i]
    mserr <- (callHestoncf(S=df_data$S[i], X=df_data$K[i], tau=df_data$T[i], r=df_data$r[i], q=0, v0, vT, rho, k, sig)-prices)^2
    # Store each mserr value somewhere if that is what you need
  }
  
  #return(wherever you stored mserr values)
}

var0 <- c(0.2, 0.2, 0.5, 0.2, 0.05) # var0 contains the actual v0, vT, rho, k, sig values
optim(var0, func_optim)

Hope this helps!

Hello

Thank you for your response. Interesting.
To answer point 4: what i want is the estimation of the parameters (var)
over the full sample, so one value for each variable for all nine rows.
Please could you try your code? at this point, to me it seems that it returns the var estimations for the last row, not for the full sample (all rows). Thank you.

If any one has a solution, i ll be more than happy to discuss. Thanks.