# Portfolio Optmization With Risk Aversion Parameter R

I have this problem in R.

$$\max w^Tu- y w^T A w$$ where A is covariance variance matrix, y risk aversion parameter. Is it rigth if I use the function solve.QP multiplying the covariance matrix for lambda and setting dvec (vector appearing in the quadratic function to be minimized) equal to the vector of mean returns of the asset? In order to find the maximum I have to change the sign

weigth4<- matrix(0, nrow=15, ncol=15)
mu <- matrix(NA, nrow=15, ncol=15)
for ( i in 1:15){
mu[i,]<- mean(c[[i]][2])
}
Amat<- cbind(1, diag(15), -diag(15))
bvec<- c(1, rep(0.01, 15), rep(-0.5,15))
for ( i in 1:15){
result4<- solve.QP(Dmat=2*list[[i]], dvec=(-1)*mu[1,], Amat=Amat, bvec=bvec, meq=1)
weigth4[i,]<- result4$solution }  In the case of expected utility maximization, I also want to check for the impact of different values of y= 0, 1, 5, 10. In any event, please rule out any short positions. How can I find these portfolios? • in programming questions it is always good to provide a full reproducible example. In your case a lot of data is missing. maybe you can provide a small 2 or 3-dimensional example? Furthermore: your Amad and bvec describe constraints, right? Why do you set them? And if you have the code - what is the problem at all ? :) Apr 18, 2018 at 6:36 ## 1 Answer You can look at the example here. I adapted is a bit. Below first I sample returns of stocks with 20% vola pa. Then I calculate the covariance matrix. In the quadprog-part I define the matirx Amat as diagonal and bvec as zeros. Then $$Amat * w \ge bvec$$ gives you the non-negative weight constraint. You can play around and find the constraint for the weights summing up to 1. In the solution blow you see that the stock with the negative expected mean was not chosen and all weights are non-negative. library(quadprog) rets = rnorm(20*3,mean=0, sd = 0.2/sqrt(250)) matrix(rets, ncol=3) Dmat <- cov(matrix(rets, ncol=3)) dvec <- c(0.01,0.01,-0.01) Amat <- diag(3) bvec <- c(0,0,0) solve.QP(Dmat,dvec,Amat,bvec=bvec)  $solution [1] 34.39526 60.13777  0.00000
>
>     $value [1] -0.4726651$unconstrained.solution [1]   -5.189056  100.950539 -146.724093
>
>     $iterations [1] 2 0$Lagrangian [1] 0.00000000 0.00000000 0.01008373
>
>     $iact [1] 3  Ok, the code below gives you the portfolios constraint as well: Dmat <- cov(matrix(rets, ncol=3)) dvec <- c(0.01,0.01,-0.01) Amat <- diag(3) Amat = cbind( c(1,1,1), Amat) bvec <- c(0,0,0) bvec = c(1,bvec) solve.QP(Dmat,dvec,Amat,bvec=bvec, meq = 1)  $solution
>     [1] 0.3638438 0.6361562 0.0000000
>
>     $value [1] -0.009947108$unconstrained.solution
>     [1]   -5.189056  100.950539 -146.724093
>
>     $iterations [1] 3 0$Lagrangian
>     [1] 0.009894217 0.000000000 0.000000000 0.019895103
>
>     $iact [1] 4 1  You need the meq with tell the quadprog that the first constraint is equality. Then you translate the$w_1 + w_2 + w_3 = 1\$ in the Amat and bvec setting.