在R中使用nloptr不会更改初始值 [英] No change to initial values using nloptr in R

查看:357
本文介绍了在R中使用nloptr不会更改初始值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

几个月来,我一直在为R中的优化问题而苦苦挣扎.我终于找到了lpSolve的线性问题,这要归功于幻想运动数据的例子.但是,我最初的(也是)当前的问题是使用R中的nloptr尝试使用等式约束进行非线性优化.

我想做的是最小化两只股票投资组合的方差,其中两只股票的收益几乎完全负相关(对于熟悉学术金融的股票,最终目标是证明/证明套利机会是否存在).我想最小化方差,但要确保两个权重的总和正好等于1,且介于0和1之间.以下是我正在使用的确切代码,该代码应易于重现:

sd1 <- 0.01
sd2 <- 0.025
corr.lo <- -0.999

# Objective function
eval.f <- function(w) {
  return(
    (w[1]*sd1)^2 +
      (w[2]*sd2)^2 +
      (2*w[1]*w[2]*sd1*sd2*corr.lo)
  )
}

# Constraint function
eval.g <- function(w) {
  return(w[1] + w[2] - 1)
}

w0 <- c(0.5, 0.5)

opts <- list('algorithm' = 'NLOPT_GN_ISRES', 'xtol_rel' = 1.0e-8)

res <- nloptr(
  x0 = w0,
  eval_f = eval.f,
  lb = c(0,0),
  ub = c(1,1),
  eval_g_eq = eval.g,
  opts = opts
)

优化运行没有错误,并生成以下内容:

> res

Call:
nloptr(x0 = w0, eval_f = eval.f, lb = c(0, 0), ub = c(1, 1),     eval_g_eq = eval.g, opts = opts)


Minimization using NLopt version 2.4.0 

NLopt solver status: 5 ( NLOPT_MAXEVAL_REACHED: Optimization stopped because maxeval (above) was reached. )

Number of Iterations....: 100 
Termination conditions:  xtol_rel: 1e-08 
Number of inequality constraints:  0 
Number of equality constraints:    1 
Current value of objective function:  0.000125508655202602 
Current value of controls: 0.5 0.5

我正在使用上述算法,因为该算法在运行时不会告诉我我需要渐变(我不熟悉).问题是w0的初始值不要从每个初始值的50%更改.任何人都可以复制并提供建议,或者尝试为我指出正确的方向吗?

提前谢谢!

解决方案

我已经找到了我自己的问题的答案.我认为优化的运行时间不够长.通过在opts列表中指定maxeval = 1000000,我得到了一个满足约束条件的答案.

I've been struggling with optimization problems in R for months now. I've finally figured out lpSolve for linear problems, thanks to examples on data for fantasy sports. However, my original and (still) current problem is trying nonlinear optimization with equality constraints using nloptr in R.

What I'm trying to do is minimize the variance of a two-stock portfolio, in which the two stocks' returns are almost perfectly negatively correlated (for those that are familiar with academic finance, the end goal is to prove/disprove whether or not arbitrage opportunities exist). I want to minimize the variance, subject to the sum of the two weights being exactly equal to 1, while being between 0 and 1. Below is the exact code I am using, which should be easily reproducible:

sd1 <- 0.01
sd2 <- 0.025
corr.lo <- -0.999

# Objective function
eval.f <- function(w) {
  return(
    (w[1]*sd1)^2 +
      (w[2]*sd2)^2 +
      (2*w[1]*w[2]*sd1*sd2*corr.lo)
  )
}

# Constraint function
eval.g <- function(w) {
  return(w[1] + w[2] - 1)
}

w0 <- c(0.5, 0.5)

opts <- list('algorithm' = 'NLOPT_GN_ISRES', 'xtol_rel' = 1.0e-8)

res <- nloptr(
  x0 = w0,
  eval_f = eval.f,
  lb = c(0,0),
  ub = c(1,1),
  eval_g_eq = eval.g,
  opts = opts
)

The optimization runs without errors, and generates the following:

> res

Call:
nloptr(x0 = w0, eval_f = eval.f, lb = c(0, 0), ub = c(1, 1),     eval_g_eq = eval.g, opts = opts)


Minimization using NLopt version 2.4.0 

NLopt solver status: 5 ( NLOPT_MAXEVAL_REACHED: Optimization stopped because maxeval (above) was reached. )

Number of Iterations....: 100 
Termination conditions:  xtol_rel: 1e-08 
Number of inequality constraints:  0 
Number of equality constraints:    1 
Current value of objective function:  0.000125508655202602 
Current value of controls: 0.5 0.5

I'm using the above algorithm because that's one that runs without telling me I need a gradient (which I am not familiar with). The problem is, the initial values of w0 DO NOT change from 50% each. Can anyone either reproduce this and offer advice, or try and point me in the right direction?

Thanks in advance!

解决方案

I've found the answer my own question. The optimization wasn't running long enough, I think. By specifying maxeval = 1000000 in the opts list, I got an answer that satisfied the constraints.

这篇关于在R中使用nloptr不会更改初始值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆