将执行SOCP / SDP优化的代码从MATLAB转换为R

时间:2014-07-03 09:00:39

标签: r matlab mathematical-optimization

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

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

}

1 个答案:

答案 0 :(得分:1)

如果您对某些技术方面的问题有足够的了解,那么SOCP只是一种快速找到最低限度的方法。由于您发现这些约束可能很难制定,因此值得询问您是否需要速度。通常答案是肯定的,但是对于调试/探索目的,使用R的优化函数进行粗略的数值优化可能是富有成效的。