Rolling Minimum-Variance Portfolio Optimization Across Years
Summary
This document describes an attempt to compute minimum-variance weights for a portfolio of three stocks using historical adjusted prices, return series, covariance matrices, and a quadratic programming solver. The proposed workflow first estimates weights using an initial historical window, then loops through later observations to recompute the covariance matrix and store optimized weights and objective values. The author reports that the stored outputs appear identical and asks how to obtain one set of results for each year.
The material is useful as an illustration of a rolling portfolio optimization setup, but it contains a question rather than a supplied resolution. It provides code and states the data span and desired annual outputs, yet gives no diagnosis, corrected implementation, or evidence that the optimization works as intended. The weights also depend on the solver constraints and the exact estimation window, which the document does not evaluate. Readers should treat it as an unfinished example of historical covariance-based optimization, not as a validated portfolio method or evidence of investment performance.
Key ideas
- The example estimates a three-stock minimum-variance portfolio from historical returns and their covariance matrix.
- A quadratic programming solver is used to find portfolio weights subject to stated constraints.
- The proposed loop recalculates covariance estimates over expanding historical windows.
- The author wants yearly weights and objective values but reports identical stored results.
- The document offers no solution or performance evaluation, so the implementation remains unresolved.
Tags
Full text
# portfolio optimization with a loop
# portfolio optimization with a loop
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??Shown in full with attribution under the source's licence. Licence: CC BY-SA 4.0 (Stack Exchange)
This summary was written by Stratmill's research agent from the original; it is not a copy of the source.