[R] The quadprog package
Moshe Olshansky
m_olshansky at yahoo.com
Tue Sep 4 02:26:21 CEST 2007
Hi Thomas,
On my computer the solution is (0,0,1,0,0,0,0) (within
machine accuracy) and it satisfies the constraints.
--- thomas.schwander at mvv.de wrote:
> Hi everybody,
>
> I'm using Windows XP Prof, R 2.5.1 and a Pentium 4
> Processor.
> Now, I want to solve a quadratic optimization
> program (Portfolio Selection) with the quadprog
> package
>
> I want to minimize (\omega'%*%\Sigma%*%\omega)
> Subject to
> (1) \iota' %*% \omega = 1 (full investment)
> (2) R'%*%\omega = \mu (predefined expectation
> value)
> (3) \omega \ge 0 (no short sales).
>
> Where
> \omega is a Nx1 vector of the weights invested in
> each asset
> \Sigma is the NxN variance-covariance matrix
> \iota is a 1xN vector of 1's
> R' is a Nx1 vector of the expected returns
> and
> \mu is a number, the postualted return of the
> investor
>
> I've done the following code but it doesn't make
> what I want it to do. The weights aren't all
> positive and the \mu isn't reached. What's wrong
> with my code?
>
> Require(quadprog)
> Dmat<-diag(1,7,7)
> # muss als quadratische Matrix eingegeben werden
> Dmat
> dvec<-matrix(0,7,1) # muss als Spaltenvektor
> eingegeben werden
> dvec
> mu<-0 # (in Mio. â¬)
> bvec<-c(1,mu,matrix(0,7,1)) # muss als Spaltenvektor
> eingegeben werden
> bvec
> mu_r<-c(19.7,33.0,0.0,49.7, 82.5, 39.0,11.8)
>
Amat<-matrix(c(matrix(1,1,7),7*mu_r,diag(1,7,7)),9,7,byrow=T)
>
> # muss als Matrix angegeben werden, wie sie wirklich
> ist
> Amat
> meq<-2
>
loesung<-solve.QP(Dmat,dvec,Amat=t(Amat),bvec=bvec,meq=2)
> loesung
>
> # Ãberprüfen, ob System richtig gelöst wurde
> loesung$solution %*% mu_r
> sum(loesung$solution)
> for (i in 1:7){
> a<-loesung$solution[i]>=0
> print(a)
> }
>
> Thanks in advance for your answers.
>
> ______________________________
>
> Thomas Schwander
>
> MVV Energie
> Konzern-Risikocontrolling
>
> Telefon 0621 - 290-3115
> Telefax 0621 - 290-3664
>
> E-Mail: Thomas.Schwander at mvv.de . Internet:
> www.mvv.de
> MVV Energie AG . Luisenring 49 . 68159 Mannheim
> Handelsregister-Nr. HRB 1780, Amtsgericht Mannheim
> Vorsitzender des Aufsichtsrates: Herr Dr. Peter Kurz
> Vorstand: Dr. Rudolf Schulten (Vorsitzender) .
> Matthias Brückmann . Dr. Werner Dub . Hans-Jürgen
> Farrenkopf
>
>
>
>
> [[alternative HTML version deleted]]
>
> > ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained,
> reproducible code.
>
More information about the R-help
mailing list