gpt4 book ai didi

r - 在 R 中执行 SQP 算法

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:16:44 25 4
gpt4 key购买 nike

我想在 R 中运行以下优化:

u.c.: 0 <= x <= 1 和 Sum(x)=1

等式基于:Efficient Algorithms for Computing Risk Parity Portfolio Weights (等式 10)

原作者说要用SQP。我想遵循那个,但是怎么做呢?

最佳答案

代码可能是这样的:

fn <- function(w){return( 
((w[1] * w %*% Mat[1,]) - (w[1] * w %*% Mat[1,]))^2 +
((w[1] * w %*% Mat[1,]) - (w[2] * w %*% Mat[2,]))^2 +
((w[1] * w %*% Mat[1,]) - (w[3] * w %*% Mat[3,]))^2 +

((w[2] * w %*% Mat[2,]) - (w[1] * w %*% Mat[1,]))^2 +
((w[2] * w %*% Mat[2,]) - (w[2] * w %*% Mat[2,]))^2 +
((w[2] * w %*% Mat[2,]) - (w[3] * w %*% Mat[3,]))^2 +

((w[3] * w %*% Mat[3,]) - (w[1] * w %*% Mat[1,]))^2 +
((w[3] * w %*% Mat[3,]) - (w[2] * w %*% Mat[2,]))^2 +
((w[3] * w %*% Mat[3,]) - (w[3] * w %*% Mat[3,]))^2
)
}

library(Rsolnp)

#start values
w0 <- c(0.3, 0.6, 0.1)

#constrain function
eqcon <- function(w){(w[1]+w[2]+w[3])}
ebcon <- 1

#optimizer
sqp <- solnp(pars = w0,
fun = fn2,
eqfun = eqcon,
eqB = ebcon,
LB = c(0,0,0),
UB = c(1,1,1))

sqp$pars

关于r - 在 R 中执行 SQP 算法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44365227/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com