Wednesday, January 29, 2020

r - 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?




No comments:

Post a Comment

technique - How credible is wikipedia?

I understand that this question relates more to wikipedia than it does writing but... If I was going to use wikipedia for a source for a res...