Take the 2-minute tour ×
Quantitative Finance Stack Exchange is a question and answer site for finance professionals and academics. It's 100% free, no registration required.

I am attempting to minimize the variance of a 3 stock portfolio using optimization within a loop. What I have done is calculated the stock returns and cov matrix from dates 1980-01-01 to 1989-12-31 and optimized for the minvar portfolio using solve.QP. From here, I would like to calculate the minvar portfolio for every year leading up to date 2010-12-31 and store the weights and optimal value in a vector. My code is as follows:

    library(quantmod)
    library(FRAPO)
    library(quadprog)
    getSymbols(c("F","AA","IBM"),from="1980-01-01",to="2010-12-31")
    port=cbind(F$F.Adjusted,AA$AA.Adjusted,IBM$IBM.Adjusted)
    portret=returnseries(port,"discrete",trim=TRUE)
    portret=data.frame(date=index(portret),coredata=portret)
    colnames(portret)=c("time","F","AA","IBM")
    #to determine where to end portA
    which(portret=="1989-12-29",arr.ind=TRUE)
    #row number 2527 references date 1989-12-29
    portA=subset(portret,select=c("F","AA","IBM"),subset=portret[,1]<portret[2527,1])
    portAcov=cov(portA)
    Dmat=portAcov
    dvec=c(0,0,0)
    A=c(1,1,1)
    B=diag(3)
    portAmeans=colMeans(portA)
    Amat=cbind(A,portAmeans,B)
    bvec=c(1,0,0,0,0)
    sol=solve.QP(Dmat,dvec,Amat,bvec,meq=1)
    #this gives me my minvar port weights for up to year 1990
    portB=subset(portret,select=c("F","AA","IBM"),subset=portret[,1]>=portret[2527,1])
    w=matrix(0,nrow(portret),3)
    w[1,1:3]=sol$solution
    v=matrix(0,nrow(portret),1)
    v[1]=sol$value
    for(i in (which(portret=="1990-01-02",arr.ind=TRUE)[1]:
          which(portret=="2010-12-31",arr.ind=TRUE)[1])){
    Dmat=cov(subset(portret[,2:4],select=c("F","AA","IBM"),subset=portret[,1]
                                                       <portret[i,1]))
    sol2=solve.QP(Dmat,dvec,Amat,bvec,meq=1)
    w[i,1:3]=sol2$solution
    v[i,]=sol2$value}

For some reason my weight vector w and value vector v equal all the same numbers. I have been crunching this code for days and cant figure it out. Additionally, I want yearly values and weights (i.e. optimal minvar weights for year 1990, 1991, 1992... 2010 and the corresponding values). Is there a simple way to do this??

share|improve this question
    
I actually just rewrote some of this code which answered one of my questions. I replaced j with i when populating the weight and value vectors while also making the necessary changes before the loop as well. This still leaves me with the fact that these are daily weights. I would like them annually... –  user2214069 Apr 22 '13 at 0:09
    
Could you clarify your question? I don't really understand your problem... –  phi Apr 22 '13 at 12:50
    
Firstly, I revised the code some so please use the new code versus the old. What I have now for vector w and v is daily minvar port weights and values. The portret variable contains dates and each date corresponds to a row number in my vectors w and v. What I would like is just the values from w and v that correspond to the last day of the year in portret from 1990-2010 i.e. 1990-12-31, 1991-12-31... 2010-12-31. One of the reasons this is so challenging for me is that each year has a different number of trading days and the last day of the trading year may be the 31st, 30th, 29th... –  user2214069 Apr 22 '13 at 14:48
add comment

Know someone who can answer? Share a link to this question via email, Google+, Twitter, or Facebook.

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.