[Returnanalytics-commits] r3349 - pkg/PortfolioAnalytics/sandbox/RFinance2014

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Mon Apr 7 07:40:43 CEST 2014


Author: rossbennett34
Date: 2014-04-07 07:40:42 +0200 (Mon, 07 Apr 2014)
New Revision: 3349

Modified:
   pkg/PortfolioAnalytics/sandbox/RFinance2014/optimize.R
   pkg/PortfolioAnalytics/sandbox/RFinance2014/presentation.Rmd
Log:
Updates to optimization script to include maximizing CRRA expected utility example. Update outline in Rmd file for slides

Modified: pkg/PortfolioAnalytics/sandbox/RFinance2014/optimize.R
===================================================================
--- pkg/PortfolioAnalytics/sandbox/RFinance2014/optimize.R	2014-04-06 16:50:45 UTC (rev 3348)
+++ pkg/PortfolioAnalytics/sandbox/RFinance2014/optimize.R	2014-04-07 05:40:42 UTC (rev 3349)
@@ -316,17 +316,11 @@
 
 ##### Example 4 #####
 
-# CRRA 4th order expansion expected utility
-# PerformanceAnalytics for moments
-# M3.MM
-# M4.MM
-# StdDev.MM
-# skewness.MM
-# kurtosis.MM
 
 # Simple function to compute the moments used in CRRA
 custom.moments <- function(R, ...){
   out <- list()
+  out$mu <- colMeans(R)
   out$sigma <- cov(R)
   out$m3 <- PerformanceAnalytics:::M3.MM(R)
   out$m4 <- PerformanceAnalytics:::M4.MM(R)
@@ -336,6 +330,7 @@
 
 # Fourth order expansion of CRRA expected utility
 CRRA <- function(R, weights, lambda, sigma, m3, m4){
+  weights <- matrix(weights, ncol=1)
   M2.w <- t(weights) %*% sigma %*% weights
   M3.w <- t(weights) %*% m3 %*% (weights %x% weights)
   M4.w <- t(weights) %*% m4 %*% (weights %x% weights %x% weights)
@@ -354,14 +349,39 @@
 portf.tmp <- add.constraint(portf.tmp, type="box", 
                              min=0.05, max=0.4)
 
-# Set multiplier=0 so that it is calculated, but does not affect the optimization
 portf.tmp <- add.objective(portf.tmp, type="return", 
                             name="CRRA", arguments=list(lambda=5))
 
-momentargs <- custom.moments(R)
-constrained_objective(weights, R, portf.tmp, env=momentargs)
+# I just want these for plotting
+# Set multiplier=0 so that it is calculated, but does not affect the optimization
+portf.tmp <- add.objective(portf.tmp, type="return", name="mean", multiplier=0)
+portf.tmp <- add.objective(portf.tmp, type="risk", name="ES", multiplier=0)
+portf.tmp <- add.objective(portf.tmp, type="risk", name="StdDev", multiplier=0)
 
+# Run the optimization
+opt.crra <- optimize.portfolio(R, portf.tmp, optimize_method="DEoptim", 
+                               search_size=5000, trace=TRUE, 
+                               momentFUN="custom.moments")
 
+opt.crra
+chart.RiskReward(opt.crra, risk.col="ES")
+chart.RiskReward(opt.crra, risk.col="StdDev")
+
+# Run the optimization with rebalancing
+bt.opt.crra <- optimize.portfolio.rebalancing(R, portf.tmp, 
+                                              optimize_method="DEoptim",
+                                              search_size=5000, trace=TRUE,
+                                              momentFUN="custom.moments",
+                                              rebalance_on=rebal.freq, 
+                                              training_period=training, 
+                                              trailing_periods=trailing)
+# Compute the portfolio returns with rebalancing
+ret.crra <- summary(bt.opt.crra)$portfolio_returns
+colnames(ret.crra) <- "CRRA"
+
+# Plot the performance summary of the returns from example 3 and CRRA
+charts.PerformanceSummary(cbind(ret.bt.opt, ret.crra))
+
 # # Calculate the turnover per period
 # turnover.rebalancing <- function(object){
 #   weights <- extractWeights(object)

Modified: pkg/PortfolioAnalytics/sandbox/RFinance2014/presentation.Rmd
===================================================================
--- pkg/PortfolioAnalytics/sandbox/RFinance2014/presentation.Rmd	2014-04-06 16:50:45 UTC (rev 3348)
+++ pkg/PortfolioAnalytics/sandbox/RFinance2014/presentation.Rmd	2014-04-07 05:40:42 UTC (rev 3349)
@@ -139,18 +139,27 @@
 # Example 1
 
 ## Minimum Variance Portfolio
-Set up portfolio to minimize variance
-```{r, eval=FALSE}
-# Specify initial portfolio
-init <- portfolio.spec(funds)
-# Add constraints
-port1 <- add.constraint(init, type="full_investment")
-port1 <- add.constraint(port1, type="box", min=0.05, max=0.6)
-# Add objective
-port1 <- add.objective(port1, type="risk", name="var")
-```
+Consider a portfolio of stocks. Full investment and long only (or box) constraints. Objective to minimize portfolio variance. Demonstrate a custom moments function to compare a sample covariance matrix estimate and a Ledoit-Wolf shrinkage covariance matrix estimate. An alternative is a robust (MCD, MVE, etc.) estimate, DCC GARCH model, factor model, etc.
 
+Use the crsp_weekly data set
 
+# Example 2
+
+## Market Neutral Portfolio
+Consider a portfolio of stocks. Dollar neutral, beta neutral, box, and position limit constraints. Objective to minimize portfolio StdDev. This will demonstrate some of the more advanced constraint types.
+
+Use the crsp_weekly data set
+
+# Example 3
+
+## Minimum Expected Shortfall
+Consider an allocation to hedge funds using the EDHEC-Risk Alternative Index as a proxy. This will be an extended example starting with an objective to minimize portfolio expected shortfall, then risk budget percent contribution limit, then equal risk contribution limit.
+
+# Example 4
+
+## Maximize CRRA
+Consider an allocation to hedge funds using the EDHEC-Risk Alternative Index as a proxy. Objective to maximize the fourth order expansion of the Constant Relative Risk Aversion (CRRA) expected utility function as in the Boudt paper and Martinelli paper. Demonstrate a custom moment function and a custom objective function.
+
 # Conclusion
 
 ## Acknowledgements



More information about the Returnanalytics-commits mailing list