# [R-sig-finance] Solve.QP

roger bos roger.bos at gmail.com
Wed Jul 13 15:10:10 CEST 2005

I have went through this exercise and posted an answer to the email
list: search for "portfolio.optim" and "solved" and it will be the
first hit.  Anyway, I have pasted the code in this message.

library(MASS)
rm(sol)

n<-100	 # number of assets
m<-200			 # number of states of the world

rho<-0.7
sigma<-0.2
mu<-.1
Cov <- matrix(rho*sigma*sigma, ncol=n, nrow=n) diag(Cov) <- rep(sigma*sigma, n)
S <- 1+matrix(mvrnorm(m, rep(mu, n), Sigma=Cov), ncol=n)
Cov <- var(S) #gives same answer as cov(S) mu <- apply(S, 2, mean)
mu.target <- mean(mu)
bLo <- rep(0, n)

Amat <- rbind(1, mu)
dim(bLo) <- c(n,1)

bvec <- t(rbind(1, mu.target, bLo))
zMat <- diag(n)
Amat <- t(rbind(Amat, zMat))

Dmat=Cov
dvec=rep(0, nrow(Amat))

#The first two rows of Amat should be equality constraints (so weights
sum to 1) meq <- 2

sol <- solve.QP(Dmat=Dmat, dvec=dvec, Amat=Amat, bvec=bvec, meq) sol
w.asset <- zapsmall(sol$solution) par(mfrow=c(1,2)) plot(w.asset, type="n") lines(w.asset, type="h") hist(w.asset) #test to see that we get the same result with portfolio.optim res<-portfolio.optim(S) wgt <- zapsmall(res$pw)
cbind(w.asset, wgt) #shows both methods are equal

Thanks,

Roger

On 7/11/05, KAHRA HANNU <hannu.kahra at mpsgr.it> wrote:
> Lorenzo,
>
> Have a look at the portfolio.optim R code and how it applies solve.QP. Download the tseries version 1.9 package and unzip it. The tseries folder contains a folder called R that contains the tseries file. Have a look at the portfolio.optim function in the file.
>
> Regards,
>
> Hannu Kahra
> Progetti Speciali
> Monte Paschi Asset Management SGR S.p.A.
> Via San Vittore, 37
> IT-20123 Milano, Italia
>
> Tel.: +39 02 43828 754
> Mobile: +39 333 876 1558
> Fax: +39 02 43828 247
> E-mail: hannu.kahra at mpsgr.it
> Web: www.mpsam.it
>
> -----Original Message-----
> From: r-sig-finance-bounces at stat.math.ethz.ch [mailto:r-sig-finance-bounces at stat.math.ethz.ch] On Behalf Of L.Isella
> Sent: 10. heinäkuuta 2005 20:01
> To: r-sig-finance at stat.math.ethz.ch
> Subject: [R-sig-finance] Solve.QP
>
> Dear All,
> I am starting to use R for portfolio optimization.
> I found the portfolio.optim package in tseries very useful, since it allows the user to use both the historical data to create the covariance matrix or to provide it directly.
> However, I would like not to simply use it as a black box. Portfolio.optim relies on solve.QP.
> Now, a typical problem in portfolio management is to minimize the variance of the portfolio:
> /frac{1}{2}W^T%*%CovMat%*%W,
> where CovMat is the covariance matrix of my portfolio, W is the vector of the (unknown) weights.
> The constraints are:
> \sum_i W_i=1 (the portfolio is totally invested)
> and
> \sum_i W_i\mu_i=\mu (\mu is the required variance of my portfolio, W_i and \mu_i the weight and the expected return of the i-th asset, respectively).
> Then I could e.g. impose W_i>=0 for all i (no short-selling allowed).
> Is it straightforward to implement these constraints on the weights as
> A%*%W>= b, where A is a matrix and b a vector?
> Maybe it is a naive question and I am just trying to re-invent the wheel, but I will be grateful to anyone who can help clarify these issues to me.
> Best Regards
> Lorenzo
>
> _______________________________________________
> R-sig-finance at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-sig-finance
>
> _______________________________________________
> R-sig-finance at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-sig-finance
>