It's been a few weeks since I last posted. Sorry about that. Unfortunately, sometimes you come home from work just not wanting to look at a computer.
I'm working on a series of posts requested by a few friends. They would like to see me build a time series simulation framework. I've done this in the past with SAS for clients (using ETS, IML, and Risk Dimensions); doing it in R will be new. I've got a bit of a learning curve to get up. So far, R seems to have a lot of functionality built in. The later posts will focus on building a platform for automating the simulation. If performance becomes a problem, we'll introduce some multiprocessing, Python, and/or GPU programming. So stay tuned.
I also want to welcome the audience at R-bloggers.com. Tal, who runs the site, has graciously agreed to syndicate this blog over there. I've found his site to be a great resource for learning R.
Saturday, January 14, 2012
Monday, January 2, 2012
Portfolio Optimization in R, Part 4 Redeux
So where did we mess up?
In the calculation of returns for the market cap weighted portfolio and
the portfolio optimization portfolio, we simply took the starting weights (W0)
and multiplied them by the relevant series of returns.
resEqual = as.matrix(returns) %*% t(ret)
and
subRes = as.matrix(subRes) %*% t(ret)
To correct this, we have 2 options.
- Recalculate the weight at each time point assuming a starting weight. Multiply those weights to each day’s returns to produce the series
- Assume a starting monetary value of the portfolio (1 is convenient), and apply the return series to the position values. At each period, calculate the daily portfolio return.
1 does not equal 2. Why?
Remember that we are using log returns.
#1 is a weighted arithmetic average.
#2 is the log of a sum of functions.
If we say that prices are final and weights are initial –
that is that we observe the price at the end of time t and the weight at the
beginning (as a function of prior period prices), then we can rewrite #2 as
The literature uses #1 because it makes the math easier. The numbers are approximately equal because log(1+r)
~= r (or exp(r) ~= 1+r, if you prefer).
However, they are different and the difference can compound with
time. Your brokerage account works
according to #2.
We will use #1 as that is the convention. If I was analyzing something for real, and I
was given a log returns data set to use, I would use #2. #1 may be more compact and make the math
nice. #2 will more closely reflect your
account balance.
How do we calculate wi,,t given wi,t-1? We apply the returns to the weights and
re-standardize the numbers.
Because we want to reuse
this methodology for cap weighted and optimized portfolios, we should just
create a function that will take a series of returns and a vector of weights,
and give back the return series.
reweight = function(returns,
startWeight){
n
= nrow(returns)
lastWeight
= as.vector(startWeight)
outReturn
= data.frame()
for(i in seq(1,n)){
rts
= as.vector(exp(returns[i,]))
w
= lastWeight * rts
sumW
= sum(w)
w
= w/sumW
r
= as.matrix(returns[i,]) %*% w
lastWeight
= w
outReturn
= rbind(outReturn,r)
}
return
(outReturn)
}
Substituting the function call in place
of the lines quoted above, the new Annualized Returns table looks like this:
CapWeight
Portfolio
|
EqualWeight
Portfolio
|
Portfolio
Optimization
|
|
Annualized
Return
|
0.0258
|
0.0128
|
0.0282
|
Annualized
Std Dev
|
0.2199
|
0.2242
|
0.1775
|
Annualized
Sharpe (Rf=0%)
|
0.1174
|
0.0570
|
0.1590
|
Here we can see the capitalization
weighted portfolio performs much better than previously calculated. This is expected. The Portfolio Optimization portfolio also
does better and has a slight advantage over the cap weight portfolio in the
Sharpe Ratio.
The correlation chart is:
The cap weight portfolio and the equal
weight portfolio are nearly perfectly correlated. The optimized portfolio is less correlated,
but still has a high degree of correlation.
NOTE: we can put a Beta on the optimized
portfolio of 0.70 (.87 * .1775 / .2199) using the calculations above.
The cumulative return graph is:
This really just shows us what we already know. The cap and equal weights are highly
correlated. The cap weight pulls away
after the 2009 bottom. The optimized
portfolio shows less volatility than the cap weight portfolio after the bottom. It has a steady march up while the cap weighted
portfolio whips up and down.
Subscribe to:
Posts (Atom)