Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
In the Cluster Portfolio Allocation post, I have outlined the 3 steps to construct Cluster Risk Parity portfolio. At each rebalancing period:
- Create Clusters
- Allocate funds within each Cluster using Risk Parity
- Allocate funds across all Clusters using Risk Parity
I created a helper function distribute.weights() function in strategy.r at github to automate these steps. It has 2 parameters:
- Function to allocate funds. For example, risk.parity.portfolio, will use use risk parity to allocate funds both within and across clusters.
- Function to create clusters. For example, cluster.group.kmeans.90, will create clusters using k-means algorithm
Here is the example how to put it all together. Let’s first load historical prices for the 10 major asset classes:
############################################################################### # Load Systematic Investor Toolbox (SIT) # http://systematicinvestor.wordpress.com/systematic-investor-toolbox/ ############################################################################### setInternet2(TRUE) con = gzcon(url('http://www.systematicportfolio.com/sit.gz', 'rb')) source(con) close(con) #***************************************************************** # Load historical data for ETFs #****************************************************************** load.packages('quantmod') tickers = spl('GLD,UUP,SPY,QQQ,IWM,EEM,EFA,IYR,USO,TLT') data <- new.env() getSymbols(tickers, src = 'yahoo', from = '1900-01-01', env = data, auto.assign = T) for(i in ls(data)) data[[i]] = adjustOHLC(data[[i]], use.Adjusted=T) bt.prep(data, align='remove.na')
Next, let’s run the 2 versions of Cluster Portfolio Allocation using Equal Weight and Risk Parity algorithms to allocate funds:
#***************************************************************** # Code Strategies #****************************************************************** periodicity = 'months' lookback.len = 250 cluster.group = cluster.group.kmeans.90 obj = portfolio.allocation.helper(data$prices, periodicity = periodicity, lookback.len = lookback.len, min.risk.fns = list( EW=equal.weight.portfolio, RP=risk.parity.portfolio, C.EW = distribute.weights(equal.weight.portfolio, cluster.group), C.RP=distribute.weights(risk.parity.portfolio, cluster.group) ) ) models = create.strategies(obj, data)$models
Finally, let’s examine the results:
#***************************************************************** # Create Report #****************************************************************** strategy.performance.snapshoot(models, T)
The Cluster Portfolio Allocation produce portfolios with better risk-adjusted returns and smaller drawdowns.
To view the complete source code for this example, please have a look at the bt.cluster.portfolio.allocation.test1() function in bt.test.r at github.
R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.