“Unexpected end of input error when trying to mimic lsqnonlin of MATLAB with nlsLM in R.”

huangapple go评论70阅读模式
英文:

Unexpected end of input error when trying to mimic lsqnonlin of MATLAB with nlsLM in R

问题

我正在尝试解决一个非常直接的非线性最小二乘问题,其公式中以零向量作为因变量,因为我只对最小化的参数感兴趣。但是,无论我对这个算法做什么,代码都会给出以下错误:Error in str2lang(x) : <text>:2:0: unexpected end of input 1: ~ ^*

编辑:用指定变量替换方程的左侧似乎解决了解析器问题。然而,现在我收到了以下错误:

在'data'中没有初始值的参数:params

有人知道我应该在哪里寻找解决这个问题的方法吗?

这是我使用的代码的重要部分:

fit <- function(params, T_k_IV_matrix) {
  T <- T_k_IV_matrix[, "tenor"]
  k <- T_k_IV_matrix[, "k"]
  IV <- T_k_IV_matrix[, "impl_volatility"]
  vt <- params[1]
  mt <- params[2]
  wt <- params[3]
  nt <- params[4]
  rhot <- params[5]

  (1/4) * exp(-2 * nt * T) * (wt^2) * (T^2) * (IV^4) +
    (1 - 2 * exp(-nt * T) * mt * T - exp(-nt * T) * wt * rhot * 
sqrt(vt) * T) * (IV^2) -(vt + 2 * exp(-nt * T) * wt * rhot * 
sqrt(vt) * k + exp(-2 * nt * T) * (wt^2) * (k^2))
}

对于(i in 1:length(grouped_data)) {
  T_k_IV_matrix <- data.table(
    tenor = c(110, 110, 82, 138, 82, 173, 138, 173, 173, 47),
    k = c(0.1629164648, 0.4307829161, -0.3811180797, 0.0009384402, 
-0.1823215568, -0.0219789067, -0.0988005847, -0.4187103349, 
-0.0737730947, -0.0527505654),
    impl_volatility = c(0.950298, 1.106709, 0.732060, 0.740338, 
0.638142, 0.525060, 0.689398, 0.652785, 0.955989, 0.962296)
  )
  y = rep(0, nrow(T_k_IV_matrix))

  x0  = c(0.04, 0.1, 0.5, 0.3,-0.8)
  lb = c(0.001,-Inf,0.001,0.001,-.999)
  ub = c(Inf,Inf,Inf,Inf,.999)

  result <- nlsLM(
    formula = y ~  fit(params, T_k_IV_matrix),
    start = x0, 
    lower = lb,
    upper = ub
  )
  params[[i]] <- coef(result)
}
英文:

I am trying to solve a quite straight forward non linear least squares problem where the formulation takes 'so to say' a vector of zeroes as dependent variable because I am only interested in the parameters of the minimization for a prediction exercise. However, whatever I do with this algorithm, the code keeps giving me the following error: Error in str2lang(x) : <text>:2:0: unexpected end of input
1: ~
^

EDIT: The suggestion to replace the LHS of the equation with a specified variable seems to resolve the parser issue. However, I now get the following error:

Error in nlsLM(formula = y ~ fit(params, T_k_IV_matrix), start = x0, lower = lb,  : 
  parameters without starting value in 'data': params

Does anyone know where I should be looking to solve this problem?

Here is the important part of the code I use:

fit <- function(params, T_k_IV_matrix) {
  T <- T_k_IV_matrix[, "tenor"]
  k <- T_k_IV_matrix[, "k"]
  IV <- T_k_IV_matrix[, "impl_volatility"]
  vt <- params[1]
  mt <- params[2]
  wt <- params[3]
  nt <- params[4]
  rhot <- params[5]

  (1/4) * exp(-2 * nt * T) * (wt^2) * (T^2) * (IV^4) +
    (1 - 2 * exp(-nt * T) * mt * T - exp(-nt * T) * wt * rhot * 
sqrt(vt) * T) * (IV^2) -(vt + 2 * exp(-nt * T) * wt * rhot * 
sqrt(vt) * k + exp(-2 * nt * T) * (wt^2) * (k^2))
}

for (i in 1:length(grouped_data)) {
  T_k_IV_matrix <- data.table(
    tenor = c(110, 110, 82, 138, 82, 173, 138, 173, 173, 47),
    k = c(0.1629164648, 0.4307829161, -0.3811180797, 0.0009384402, 
-0.1823215568, -0.0219789067, -0.0988005847, -0.4187103349, 
-0.0737730947, -0.0527505654),
    impl_volatility = c(0.950298, 1.106709, 0.732060, 0.740338, 
0.638142, 0.525060, 0.689398, 0.652785, 0.955989, 0.962296)
  )
  y = rep(0, nrow(T_k_IV_matrix))

  x0  = c(0.04, 0.1, 0.5, 0.3,-0.8)
  lb = c(0.001,-Inf,0.001,0.001,-.999)
  ub = c(Inf,Inf,Inf,Inf,.999)

  result <- nlsLM(
    formula = y ~  fit(params, T_k_IV_matrix),
    start = x0, 
    lower = lb,
    upper = ub
  )
  params[[i]] <- coef(result)
}

答案1

得分: 3

以下是要翻译的内容:

"fit <- function(vt,mt,nt,wt, rhot, T_k_IV_matrix) {
T <- T_k_IV_matrix$tenor
k <- T_k_IV_matrix$k
IV <- T_k_IV_matrix$impl_volatility

(1/4) * exp(-2 * nt * T) * (wt^2) * (T^2) * (IV^4) +
(1 - 2 * exp(-nt * T) * mt * T - exp(-nt * T) * wt * rhot * sqrt(vt) * T) * (IV^2) -
(vt + 2 * exp(-nt * T) * wt * rhot * sqrt(vt) * k + exp(-2 * nt * T) * (wt^2) * (k^2))
}

params <- list()

for (i in 1:length(grouped_data)) {
group <- grouped_data[[i]]
T_k_IV_matrix <- data.table(
tenor = c(110, 110, 82, 138, 82, 173, 138, 173, 173, 47),
k = c(0.1629164648, 0.4307829161, -0.3811180797, 0.0009384402,
-0.1823215568, -0.0219789067, -0.0988005847, -0.4187103349,
-0.0737730947, -0.0527505654),
impl_volatility = c(0.950298, 1.106709, 0.732060, 0.740338,
0.638142, 0.525060, 0.689398, 0.652785, 0.955989, 0.962296)
)
y = rep(0, nrow(T_k_IV_matrix))

x0 = c(vt = 0.04, mt=0.1, nt=0.01, wt=0.3, rhot=-0.8)
lb = c(0.001,-Inf,0.001,0.001,-.999)
ub = c(Inf,Inf,Inf,Inf,.999)

result <- nlsLM(
formula = y ~ fit(vt,mt,nt,wt, rhot, T_k_IV_matrix),
start = x0,
lower = lb,
upper = ub
)

params[[i]] <- coef(result)
}"

英文:

The solution to the above problem consists of three parts. Firstly, the LHS of the nlsLM statement needed to be written as a variable itself to avoid the parsing problem. Secondly, the fit function required the parameter vector to be written out instead of being a list of parameters. Thirdly, to get the wished-for results, the allocation of the variables in the fit function needed to be accessed by '$x' instead of [,"x"].

fit &lt;- function(vt,mt,nt,wt, rhot, T_k_IV_matrix) { 
  T &lt;- T_k_IV_matrix$tenor
  k &lt;- T_k_IV_matrix$k
  IV &lt;- T_k_IV_matrix$impl_volatility
  
  (1/4) * exp(-2 * nt * T) * (wt^2) * (T^2) * (IV^4) +
    (1 - 2 * exp(-nt * T) * mt * T - exp(-nt * T) * wt * rhot * sqrt(vt) * T) * (IV^2) -
    (vt + 2 * exp(-nt * T) * wt * rhot * sqrt(vt) * k + exp(-2 * nt * T) * (wt^2) * (k^2))
}

params &lt;- list()

for (i in 1:length(grouped_data)) {
  group &lt;- grouped_data[[i]]
    T_k_IV_matrix &lt;- data.table(
    tenor = c(110, 110, 82, 138, 82, 173, 138, 173, 173, 47),
    k = c(0.1629164648, 0.4307829161, -0.3811180797, 0.0009384402, 
    -0.1823215568, -0.0219789067, -0.0988005847, -0.4187103349, 
    -0.0737730947, -0.0527505654),
    impl_volatility = c(0.950298, 1.106709, 0.732060, 0.740338, 
    0.638142, 0.525060, 0.689398, 0.652785, 0.955989, 0.962296)
  )
  y = rep(0, nrow(T_k_IV_matrix))
  
  x0  = c(vt = 0.04, mt=0.1, nt=0.01, wt=0.3, rhot=-0.8)
  lb = c(0.001,-Inf,0.001,0.001,-.999)
  ub = c(Inf,Inf,Inf,Inf,.999)
  
  result &lt;- nlsLM(
    formula = y ~ fit(vt,mt,nt,wt, rhot, T_k_IV_matrix),
    start = x0, 
    lower = lb,
    upper = ub
  )
  
  params[[i]] &lt;- coef(result)
}

huangapple
  • 本文由 发表于 2023年5月22日 23:36:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/76307818.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定