Portfolio Optimization with Tawny
Tawny provides a simple, closed form portfolio optimizer to minimize the risk in a portfolio. This optimizer can run over a rolling window to calculate a time series of portfolio weights for further analysis.
For illustration purposes, use a subset of the S&P with 200 total days of returns. A real world example requires downloading the data using the getPortfolioReturns function described in Working with Portfolios. Use random matrix theory as the filter first and plot the returns.
data(sp500.subset) h <- sp500.subset # Optimize using a window of length 150 # (there will be 51 total iterations) ws.1 <- optimizePortfolio(h, 150, getCorFilter.RMT() ) # Plot the performance of the resulting optimized portfolio pf.1 <- plotPerformance(h, ws.1, 150, y.min=-0.4, name='RMT', bg='#fefefe', color='#87abc1')
Now filter the same portfolio using shrinkage estimation. Note that in this example, we also plot for comparison the equal weighted portfolio and the market.
# Generate weights based on the constant correlation shrinkage estimator ws.2 <- optimizePortfolio(h, 150, getCorFilter.Shrinkage()) pf.2 <- plotPerformance(h, ws.2, 150, name='shrinkage', color='#b89c5a', colors=pf.1$colors) # Compare the performance with a naive equal-weighted portfolio ef <- compare.EqualWeighted(h, 150, colors=pf.2$colors) # Also compare against the S&P 500 mf <- compare.Market('^GSPC',200,150, end=end(h), colors=ef$colors)
The plotting and performance analysis is very simple and not intended for serious use. There are other packages available that do this much better.