【问题标题】:Translating code that carries out SOCP/SDP optimisation from MATLAB to R将执行 SOCP/SDP 优化的代码从 MATLAB 翻译到 R
【发布时间】:2014-07-11 12:22:10
【问题描述】:

我有以下 MATLAB 代码,它在链接的论文 (http://www.optimization-online.org/DB_FILE/2014/05/4366.pdf) 中使用,并且希望能够使用 Rsocp 包来执行相同的功能,但在 R 中。@ 987654327@包可用命令:

install.packages("Rsocp", repos="http://R-Forge.R-project.org")

并通过socp() 函数执行与下面MATLAB 代码中的solvesdp(constraints, -wcvar, ops) 类似的函数。

我没有 MATLAB,这让我更难以解决这个问题。

我遇到的问题是 R 的 socp() 函数将矩阵作为反映数据(/协方差矩阵和平均返回值)和约束的输入,而 MATLAB 代码似乎正在优化一个函数......在这种特定情况下,它看起来像是优化 -wcvar 以获得最佳权重,所以我不确定如何在 R 中设置我的问题以获得类似的结果。

我希望在翻译成 R 时得到帮助的 MATLAB 代码如下:

function [w] = rgop(T, mu, sigma, epsilon)

% This function determines the robust growth-optimal portfolio
%   Input parameters:
%       T       - the number of time periods
%       mu      - the mean vector of asset returns
%       sigma   - the covariance matrix of asset returns
%       epsilon - violation probability
%   Output parameters:
%       w       - robust growth-optimal portfolios

% the number of assets
n = length(mu);

% portfolio weights 
w = sdpvar(n,1);

% mean and standard deviation of portfolio
rp = w'*mu;
sigmap = sqrt(w'*sigma*w);

% preclude short selling
constraints = [w >= 0];  %#ok<NBRAK>

% budget constraint
constraints = [constraints, sum(w) == 1];

% worst-case value-at-risk (theorem 4.1)
wcvar = 1/2*(1 - (1 - rp + sqrt((1-epsilon)/epsilon/T)*sigmap)^2 - ((T-1)/epsilon/T)*sigmap^2);

 % maximise WCVAR 
ops = sdpsettings('solver','sdpt3','verbose',0);
solvesdp(constraints, -wcvar, ops);

 w = double(w);

 end

对于协方差矩阵的平方根函数,可以使用:

 Rsocp:::.SqrtMatrix()

请注意,这个问题与我之前的问题部分相关,但更侧重于获取最坏情况的 VaR 权重:

SOCP Solver Error for fPortoflio using solveRsocp

也许一个好的开始是在 Rsocp 包已经被使用的地方使用这个代码......

https://r-forge.r-project.org/scm/viewvc.php/pkg/fPortfolio/R/solveRsocp.R?view=markup&root=rmetrics&pathrev=3507

编辑

我认为solvesdp函数的MATLAB代码可以从这个链接获得:

https://code.google.com/p/vroster/source/browse/trunk/matlab/yalmip/solvesdp.m?r=11

还有一个关于 SOCP 优化的简单问题...通过 SOCP 优化获得的结果是否与使用其他优化方法获得的结果相同?唯一的区别是速度和效率吗?

EDIT2

既然被请求了……

rgop <- function(tp, mu, sigma, epsilon){
  # INPUTS
  # tp - the number of time periods
  # mu - the mean vector of asset returns
  # sigma - the covariance matrix of asset returns
  # epsilon - violation probability

  # OUTPUT
  # w - robust growth-optimal portfolios

  #n is number of assets
  n <- length(mu)

  # portfolio weights (BUT THIS IS THE OUTPUT)
  # for now will assume equal weight
  w <- rep(1/n,n)

  # mean and standard deviation of portfolio
  rp <- sum(w*mu)

  sigmap <- as.numeric(sqrt(t(w) %*% sigma %*% w))

  # worst-case value-at-risk (theorem 4.1)
  wcvar = 1/2*(1 - (1 - rp + sqrt((1-epsilon)/epsilon/tp)*sigmap)^2 - ((tp-1)/epsilon/tp)*sigmap^2);

  # optimise...not sure how to carry out this optimisation...
  # which is the main thrust of this question...
  # could use DEoptim...but would like to understand the SOCP method

}

【问题讨论】:

  • Rsocp 包中的socp 函数需要标准形式的 SOCP,看起来 MATLAB 的 solvesdp 有很多余地。您需要将优化问题重新表述为标准形式。 this document 的第 2 节有许多重新表述的技巧。如果您无法弄清楚,我建议您在 math.stackexchange.com 上提问,因为此时您的问题实际上是数学练习。
  • 如果设置设置为ops = sdpsettings('solver','sdpt3','verbose',0); 时执行solvesdp 的代码是否可以使用MATLAB 代码的该部分来查看它如何处理约束?例如@也可以创建 987654342@ 来模仿使用类似设置的能力?
  • 还有一个关于一般 SOCP 优化的快速问题...通过 SOCP 优化获得的结果是否与使用其他优化方法获得的结果相同?唯一的区别是速度和效率吗?
  • 请展示您尝试过的内容以及您遇到的问题。你肯定不会在第一行遇到问题n = length(mu);
  • 这个问题现在有一点 R 代码...显示了一些翻译...但没有解决问题的主旨...

标签: r matlab mathematical-optimization


【解决方案1】:

SOCP 只是在您对问题有足够了解并以某些技术方式限制问题的情况下快速找到最小值的方法。当您发现这些约束可能难以制定时,因此值得询问您是否需要速度。通常答案是肯定的,但出于调试/探索的目的,使用 R 的 optim 函数进行粗略的数值优化可能会很有成效。

【讨论】:

  • 谢谢您,但您能帮忙翻译代码以获得实用的解决方案吗?
  • 一个实用的解决方案是产生答案。您想了解凸优化中的约束吗?你最好阅读名著:) web.stanford.edu/~boyd/cvxbook
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-01-10
  • 1970-01-01
  • 1970-01-01
  • 2019-10-15
  • 2011-11-30
  • 1970-01-01
  • 2020-02-08
相关资源
最近更新 更多