constrOptim: Minimise a loss function to calibrate option model parameters using the Feller Condition

别来无恙 提交于 2021-01-29 08:13:03

问题


I want to calibrate the Heston SV model (http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.139.3204&rep=rep1&type=pdf) using the function callHestoncffrom the package NMOF. For that I need to estimate four structural parameters (vT, rho, k & sigma) using a loss function. The loss function is a weighted sum of squared errors of the form 1/N * sum(w_i * (market_price - model_price)^2) where the weight is 1/market_price. The code is structured as follows:

Random sample data from whole set:

 index_level strike   T mid_price riskfree_rate        tau
1     2883.98   3070  37      1.70     0.0197700 0.10136986
2     3112.76   3070  27     74.00     0.0191700 0.07397260
3     3141.63   3150 100     94.05     0.0194325 0.27397260
4     2980.38   3040  16      7.00     0.0218863 0.04383562
5     2937.78   2970  16     18.15     0.0189688 0.04383562
6     2924.43   2975  30     70.85     0.0195425 0.08219178

Code:

par_vec <- vector(mode = "double", length = 4)
market_price <- calibration_file$mid_price
w_i <- 1/market_price

### loss function with generated Heston prices


objective_function <- function(par_vec){
  
  S <- calibration_file$index_level
  K <- calibration_file$strike
  tau <- calibration_file$tau
  r <- calibration_file$riskfree_rate
  q <- rep(0, 1374)
  
  y <- vector(mode = "double", length = 6)
  
  for (i in 1:6){
    y[i] <- callHestoncf(S = S[i], X = K[i], tau = tau[i], r = r[i], q = q[i], v0 = 0.04,
                         vT = par_vec[1], rho = par_vec[2], k = par_vec[3], sigma = par_vec[4], implVol = FALSE)
    print(i)
  }
  
  error <- sum(w_i*(market_price-y)^2)/6
  return(error)
}
  
  
### initial values and bounds

x0 <- c(0.05,-0.85,3.00,0.32)#initial values
lb <- c(0.01, -1, 1, 0.01) #lower bound
ub <- c(1, -0.2, 10, 1) #upper bound

### Feller condition
confun=function(par_vec){
  f=NULL
  f=rbind(f,par_vec[4]^2-2*par_vec[1]*par_vec[2])
  list(ceq=NULL,c=f)
}


### optimisation with Feller condition imposed

parameters <- solnl(x0, objfun=objective_function, confun=confun,  A = NULL, B = NULL,
                    Aeq = NULL, Beq = NULL, lb = lb, ub = ub, tolX = 1e-05,
                    tolFun = 1e-06, tolCon = 1e-06, maxnFun = 1e+07, maxIter = 4000)
parameters

The optimisation fails when a large dataset is inputted (I tried with 1374 obs) but works with 50 but takes some time, I guess due to the numerical integration in callHestoncf. When I input a large set of data I get the error >could not find function "A"<. I also tried to leave out the lower and upper bounds but then the integral diverges according to R.

来源:https://stackoverflow.com/questions/65577935/constroptim-minimise-a-loss-function-to-calibrate-option-model-parameters-using

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!