[R] solve.QP

From: <Barbara.Rogo_at_uniroma1.it>
Date: Tue, 22 Jun 2010 12:18:06 +0200


Why sometimes I have error???

In this code I use solve.QP, and I have error sometimes and other times no. Why? Thanks...



matr.dati=cbind(c(0.035,0.065,0.040,0.130,0.120,0.140,0.100,0.090),c(0.0750,0.2150,0.0550,0.2280,0.1858,0.2450,0.1555,0.1650))
0.040 0.0550
0.130 0.2280
0.120 0.1858
0.140 0.2450
0.100 0.1555
0.090 0.1650


#importa i dati di medie e deviazioni standard

matr.dati1=read.table("medie_dev.txt")
matr.cov1=read.table("covarianze.txt")
matr.dati=data.matrix(matr.dati1)
matr.cov=data.matrix(matr.cov1)


#caricato il packages (install.packages("quadprog",dependencies=TRUE) quadprog per risolvere il problema di programmazione
quadratica
#con vincoli di uguaglianza e/o disuguaglianza
library(quadprog)

#individuiamo il portafoglio a varianza minima
dvec1=rep(0,nrow(matr.dati))
Amat1=t(rbind(rep(1,nrow(matr.dati)),diag(1,nrow(matr.dati),nrow(matr.dati)))) meq=1
bvec1=c(1,rep(0,nrow(matr.dati)))
pesi.port.min=solve.QP(matr.cov,dvec1,Amat1,bvec1,meq)[[1]] media.port.min=sum(t(pesi.port.min)*matr.dati[,1]) var.port.min=sum(diag(pesi.port.min)%*%matr.cov%*%diag(pesi.port.min)) dev.port.min=sqrt(var.port.min)

mediamax=max(matr.dati[,1])
passo=(mediamax-media.port.min)/50
mediap=mat.or.vec(nr=1,nc=50) #predispone il vettore dove salvare i vincoli di media di portafoglio

for (h in 1:50){

      mediap[h]=media.port.min+passo*(h-1)
      }



#Effettuiamo la simulazione, generiamo un campione casuale di 8 serie storiche di rendimenti da una normale multivariata a
#8 dimensioni

S=5#numero di simulazioni

library(MASS)
new.SST=array(replicate(S,mvrnorm(120,matr.dati[,1],matr.cov)),dim=c(120,8,S))

#calcola il vettore delle medie e le matrici di covarianza
media.new.SST=mat.or.vec(nr=S,nc=8) #predispone la matrice dove salvare le medie di new.SST, NON SERVE!!!! new.matr.cov=c(rep(0,8*8*S))
dim(new.matr.cov)=c(8,8,S)

for (i in 1:S){

      media.new.SST[i,]=colMeans(new.SST[,,i])
      new.matr.cov[,,i]=cov(new.SST[,,i])
      }

#Individuiamo i nuovi portafogli

pesicc1=mat.or.vec(nr=50,nc=8) #predispone la matrice dove salvare i risultati (le righe contengono le quote di composizione

                      #corrispondenti al livello di media mediap di portafoglio)

meq=2
dvec=rep(0,8)
#bvec=c(1,rep(0,8))

library(quadprog)

#su una singola traiettoria

      Amat1=t(rbind(media.new.SST[5,],rep(1,8),diag(1,8,8)))
      Dmat1=new.matr.cov[,,5]
      #D=solve(chol(Dmat1))
      for (h in 1:50){
            for (k in 1:8){
            bvec=c(mediap[h],1,rep(0,8))
                  pesicc1[h,]=solve.QP(Dmat1,dvec,Amat1,bvec,meq)[[1]]
                  }}
	[[alternative HTML version deleted]]

______________________________________________
R-help_at_r-project.org 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. Received on Tue 22 Jun 2010 - 10:21:09 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Tue 22 Jun 2010 - 10:30:33 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive