# short-sale constraint with nonpositive-definite matrix in portfolio optimization

I need help about portfolio optimization in R. I have inverted matrix and I want to use it as an input in portfolio optimization. It was non-positive definite before I have handled it. In portfolio selection theory we need inverted matrix I have already have it. The problem for me that to impose a non-negativity constraint (short sale prohibited) on weights. In traditional optimization packages we have to put covariance matrix and it solves. I do not want cope with the covariance matrix by using different technique. I have already inverted matrix. The aim for me to solve portfolio selection codes with putting inverted covariance matrix(precision matrix) under non-negativity constraint (each element of weight vector must be positive) Which method should I use I do not know. If you help me, I would be very happy..

This is my work so far:

I am trying to construct a portfolio weight vector to minimize the variance of the returns.

       w ̂=argmin w'Σ w
s. t.  w'I = 1           #weights sum up to 1
w'μ=ρ             #target expected return
w≥0               #non-negativity(short-sale) constraint


where w is the vector of weights, Σ covariance matrix.

optimization<-function(returns) {
p <- ncol(x)                    #number of assets
n <- nrow(x)                    #number of observations
x <- matrix(data\$return,n,assets)
mean <- colMeans(na.rm=FALSE,x)
M <- as.integer(10)             #nuber of ports on the eff.front.
S <- cov(x)                     #covariance matrix
Rmax<- 0.01                     #max monthly return value
Dmat   <- solve(S)              #inverse of covariance matrix
u <- rep(1,p)                   #vector of ones

These codes are for the Lagrange solutions

a<- matrix(rep(0,4), nrow=2)
a[1,1] <- t(u)%*% Dmat %*%u
a[1,2] <- t(mean)%*%Dmat%*%u
a[2,1] <- a[1,2]
a[2,2] <- t(mean)%*%Dmat%*%mean
d <- a[1,1]*a[2,2]-a[1,2]*a[1,2]
f <- (Dmat%*%(a[2,2]*u-a[1,2]*mean))/d
g <- (Dmat%*%(-a[1,2]*u+a[1,1]*mean))/d
r <- seq(0, Rmax, length=M)
w <- matrix((rep(0, p*M)), nrow=p)


I tried to find non-negative weights using the codes below:

for(i in 1:M) { w[,i] = f+r[i]*g                    #portfolio weights
if (w[,i] <0) {w[,i]=0} else {w[,i]=w[,i]}
}


Also, I tried to make a loop using 'while' function in R.

while (w> 0)
{ for(i in 1:M) { w[,i] = f+r[i]*g }
print(w)
}


Unfortunately, I could not get the positive weights. Is there another solution to get positive weights?

• Have you already inverted the covariance matrix? About your actual optimisation problem, what have tried, and why did it not work? Dec 22, 2014 at 21:17
• I actually use Lagrange solution for portfolio optimization. I solved the minimum variance portfolio subject to the constraints that are all weights sum up to 1 and target return constraint. But I could not add the nonnegativity constraints. In this case I have negative weights as well. I have inverted matrix. I can not use the traditional optimization packages such as quadprog etc. in R. Because they use covariance matrix as an input. I have inverted one. Dec 22, 2014 at 22:24
• Why could you not add the non-negativity constraints? You just need to include more terms in the Lagrangian, no? Search for Lagrangian with inequality constraints - I do not have time right now to post a full answer. Dec 22, 2014 at 22:53
• I need to add w≥0 constraint. Should I add like this and solve for w ? L(w,λ,δ, θ)=w'Σw+λ(q-w'μ)+δ(1-w'e)+θ(w) Dec 22, 2014 at 23:12
• Hi, welcome to Quant.SE! I merged your answer into the question as it was not really an answer but more of an extension. Dec 23, 2014 at 7:45

## 1 Answer

Why not just use PortfolioAnalytics, as if your matrix is non positive definite you will have problems using non optimization approaches.

Here is an example taken from my blog: retmat is a matrix of returns

library(PortfolioAnalytics)
moms_portfolio = portfolio.spec(assets=colnames(retmat))
moms_portfolio = add.constraint(portfolio=moms_portfolio,type="full_investment")
moms_portfolio = add.constraint(portfolio=moms_portfolio,type="long_only")
moms_portfolio = add.objective(portfolio=moms_portfolio,name="StdDev",type="risk")

/#Optimize (May want to switch to DEoptim or different optimizer if ROI fails)

optimal_portfolio = optimize.portfolio(retmat,moms_portfolio,optimize_method="ROI")

optimal_portfolio = optimize.portfolio(retmat,moms_portfolio,optimize_method="DEoptim", search_size=5000, trace=TRUE, traceDE=0)

edit 1: Documentation

see here for general optimization with Portfolio analytics: Portfolio Optimization Vignette

Portfolio Optimization With custom moments and objectives

• Thank you very much for your answer. But there is no package called PortfolioAnalytics in Rstudio. I think optimization packages firstly solve the non-positivity problem of covariance matrix, then solve the precision matrix and optimization problem. I have already precision matrix. I do not want to deal with non-positive definite problem of the covariance matrix. I want to solve the portfolio optimization problem with non-negativity (short-sale) constraint by using precision matrix. I had problem most of optimization packages with the covariance matrix, because they require positive-definite. Dec 30, 2014 at 4:06
• There is a package called PortfolioAnalytics.... r-forge.r-project.org/R/?group_id=579. The optimization package i listed does not "alter" or change the covariance matrix, it's a global optimizer so regardless of what your covariance matrix looks like it will work. It is impossible to analytically solve the problem as your matrix is not invertable and you have a long only constraint. DEoptim does not require a positive definite covariance matrix as it is a global optimizer. Dec 30, 2014 at 10:46
• How can I be sure that DEoptim is taking into account my precision matrix. It only needs return matrix. Jan 1, 2015 at 21:32
• See the source code, portfolio analytics is calculating the covariance matrix the same way as you and then calling deoptim, if you want to pass your own covariance matrix see the following: r-forge.r-project.org/scm/viewvc.php/pkg/PortfolioAnalytics/… ,download the most recent pdf. I would highly suggest attempting the above code with your data as your problem is quite trivial and has well documented solutions. Jan 2, 2015 at 1:33
• Edited my answer to add documentation which can help you understand how this all works. Jan 2, 2015 at 2:05