[Returnanalytics-commits] r3451 - in pkg/PortfolioAnalytics: demo sandbox

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Mon Jun 30 21:38:58 CEST 2014


Author: rossbennett34
Date: 2014-06-30 21:38:58 +0200 (Mon, 30 Jun 2014)
New Revision: 3451

Added:
   pkg/PortfolioAnalytics/demo/meucci_ffv.R
Removed:
   pkg/PortfolioAnalytics/sandbox/meucci_ffv.R
Modified:
   pkg/PortfolioAnalytics/demo/00Index
Log:
moving ffv script to demo

Modified: pkg/PortfolioAnalytics/demo/00Index
===================================================================
--- pkg/PortfolioAnalytics/demo/00Index	2014-06-30 19:36:42 UTC (rev 3450)
+++ pkg/PortfolioAnalytics/demo/00Index	2014-06-30 19:38:58 UTC (rev 3451)
@@ -31,3 +31,4 @@
 regime_switching       Demonstrate optimization with support for regime switching to switch portfolios based on the regime.
 higher_moments_boudt     Demonstrate using a statistical factor model to estimate moments based on work by Kris Boudt.
 multi_layer_optimization    Demonstrate multi layer optimization of optimization problem with two layers and two sub portfolios in the lower layer.
+meucci_ffv    Demonstrate Meucci's Fully Flexible Views framework to estimate moments and use as inputs for  minimum variance optimization.

Copied: pkg/PortfolioAnalytics/demo/meucci_ffv.R (from rev 3450, pkg/PortfolioAnalytics/sandbox/meucci_ffv.R)
===================================================================
--- pkg/PortfolioAnalytics/demo/meucci_ffv.R	                        (rev 0)
+++ pkg/PortfolioAnalytics/demo/meucci_ffv.R	2014-06-30 19:38:58 UTC (rev 3451)
@@ -0,0 +1,96 @@
+# Demonstrate Meucci's Fully Flexible Views framework to estimate moments and
+# use as inputs for a minimum variance optimization
+
+library(PortfolioAnalytics)
+data(edhec)
+R <- edhec[,1:5]
+funds <- colnames(R)
+
+# Construct initial portfolio
+init.portf <- portfolio.spec(assets=funds)
+init.portf <- add.constraint(portfolio=init.portf, type="weight_sum", 
+                             min_sum=0.99, max_sum=1.01)
+init.portf <- add.constraint(portfolio=init.portf, type="box",
+                             min=0.05, max=0.5)
+init.portf <- add.objective(portfolio=init.portf, type="risk", name="StdDev")
+init.portf <- add.objective(portfolio=init.portf, type="return", name="mean", multiplier=0)
+
+# prior probabilities
+p <- rep(1 / nrow(R), nrow(R))
+
+# Express views
+# lambda is the ad-hoc multiplier
+# m_k = m(V_k) + lambda * sigma(V_k)
+# sigma(k) is a measure of volatility (i.e. standard deviation, interquartile range, etc.)
+# Meucci recommends -2 (very bearish), -1 (bearish), 1 (bullish), 2 (very bullish)
+
+# View 1: very bearish view on R[,1] - R[,2]
+V1 <- coredata(R[,1] - R[,2])
+b1 <- mean(V1) - 2 * sd(V1)
+
+# View 2: bearish view on R[,5] - R[,4]
+V2 <- coredata(R[,5] - R[,4])
+b2 <- mean(V2) - 1 * sd(V2)
+
+# Compute the posterior probabilities for each view
+# Equality constraints to constrain the posterior probabilities to sum to 1
+Aeq <- matrix(1, ncol=nrow(R))
+beq <- 1
+p1 <- EntropyProg(p, t(V1), b1, Aeq, beq)$p_
+p2 <- EntropyProg(p, t(V2), b2, Aeq, beq)$p_
+
+# Assign confidence weights to the views and pool opinions
+# 0.35 : confidence weight on reference model
+# 0.25 : confidence weight on view 1
+# 0.4  : confidence weight on view 2
+
+# Prior posterior of pooled opinions
+p_ <- cbind(p, p1, p2) %*% c(0.35 , 0.25 , 0.4)
+
+# Generate random portfolios
+rp <- random_portfolios(init.portf, 10000)
+
+# Optimization using first and second moments estimated from Meucci's Fully 
+# Flexible Views framework.
+opt.meucci <- optimize.portfolio(R, 
+                                 init.portf, 
+                                 optimize_method="random", 
+                                 rp=rp, 
+                                 trace=TRUE,
+                                 method="meucci", 
+                                 posterior_p=p_)
+
+
+# Optimization using sample estimates for first and second moments
+opt.sample <- optimize.portfolio(R, 
+                                 init.portf, 
+                                 optimize_method="random", 
+                                 rp=rp,
+                                 trace=TRUE)
+
+#Extract the stats for plotting
+stats.meucci <- extractStats(opt.meucci)
+stats.sample <- extractStats(opt.sample)
+
+
+# Plots
+# Plot the optimal weights
+chart.Weights(combine.optimizations(list(meucci=opt.meucci, sample=opt.sample)))
+
+# Plot the risk-reward of each chart on the same scale
+xrange <- range(c(stats.meucci[,"StdDev"], stats.sample[,"StdDev"]))
+yrange <- range(c(stats.meucci[,"mean"], stats.sample[,"mean"]))
+layout(matrix(c(1,2)), widths=1, heights=1)
+# c(bottom, left, top, right)
+par(mar=c(0, 4, 4, 4) + 0.1)
+plot(x=stats.meucci[,"StdDev"], stats.meucci[,"mean"], xlab="", ylab="mean", 
+     xlim=xrange, ylim=yrange, xaxt="n", yaxt="n")
+axis(2, pretty(yrange), cex.axis=0.8)
+legend("topright", legend="Meucci", bty="n")
+par(mar=c(5, 4, 0, 4) + 0.1)
+plot(x=stats.sample[,"StdDev"], stats.sample[,"mean"], xlab="StdDev", ylab="", 
+     xlim=xrange, ylim=yrange, yaxt="n", cex.axis=0.8)
+axis(4, pretty(yrange), cex.axis=0.8)
+legend("topright", legend="Sample", bty="n")
+par(mar=c(5, 4, 4, 2) + 0.1)
+layout(matrix(1), widths=1, heights=1)

Deleted: pkg/PortfolioAnalytics/sandbox/meucci_ffv.R
===================================================================
--- pkg/PortfolioAnalytics/sandbox/meucci_ffv.R	2014-06-30 19:36:42 UTC (rev 3450)
+++ pkg/PortfolioAnalytics/sandbox/meucci_ffv.R	2014-06-30 19:38:58 UTC (rev 3451)
@@ -1,96 +0,0 @@
-# Demonstrate Meucci's Fully Flexible Views framework to estimate moments and
-# use as inputs for a minimum variance optimization
-
-library(PortfolioAnalytics)
-data(edhec)
-R <- edhec[,1:5]
-funds <- colnames(R)
-
-# Construct initial portfolio
-init.portf <- portfolio.spec(assets=funds)
-init.portf <- add.constraint(portfolio=init.portf, type="weight_sum", 
-                             min_sum=0.99, max_sum=1.01)
-init.portf <- add.constraint(portfolio=init.portf, type="box",
-                             min=0.05, max=0.5)
-init.portf <- add.objective(portfolio=init.portf, type="risk", name="StdDev")
-init.portf <- add.objective(portfolio=init.portf, type="return", name="mean", multiplier=0)
-
-# prior probabilities
-p <- rep(1 / nrow(R), nrow(R))
-
-# Express views
-# lambda is the ad-hoc multiplier
-# m_k = m(V_k) + lambda * sigma(V_k)
-# sigma(k) is a measure of volatility (i.e. standard deviation, interquartile range, etc.)
-# Meucci recommends -2 (very bearish), -1 (bearish), 1 (bullish), 2 (very bullish)
-
-# View 1: very bearish view on R[,1] - R[,2]
-V1 <- coredata(R[,1] - R[,2])
-b1 <- mean(V1) - 2 * sd(V1)
-
-# View 2: bearish view on R[,5] - R[,4]
-V2 <- coredata(R[,5] - R[,4])
-b2 <- mean(V2) - 1 * sd(V2)
-
-# Compute the posterior probabilities for each view
-# Equality constraints to constrain the posterior probabilities to sum to 1
-Aeq <- matrix(1, ncol=nrow(R))
-beq <- 1
-p1 <- EntropyProg(p, t(V1), b1, Aeq, beq)$p_
-p2 <- EntropyProg(p, t(V2), b2, Aeq, beq)$p_
-
-# Assign confidence weights to the views and pool opinions
-# 0.35 : confidence weight on reference model
-# 0.25 : confidence weight on view 1
-# 0.4  : confidence weight on view 2
-
-# Prior posterior of pooled opinions
-p_ <- cbind(p, p1, p2) %*% c(0.35 , 0.25 , 0.4)
-
-# Generate random portfolios
-rp <- random_portfolios(init.portf, 10000)
-
-# Optimization using first and second moments estimated from Meucci's Fully 
-# Flexible Views framework.
-opt.meucci <- optimize.portfolio(R, 
-                                 init.portf, 
-                                 optimize_method="random", 
-                                 rp=rp, 
-                                 trace=TRUE,
-                                 method="meucci", 
-                                 posterior_p=p_)
-
-
-# Optimization using sample estimates for first and second moments
-opt.sample <- optimize.portfolio(R, 
-                                 init.portf, 
-                                 optimize_method="random", 
-                                 rp=rp,
-                                 trace=TRUE)
-
-#Extract the stats for plotting
-stats.meucci <- extractStats(opt.meucci)
-stats.sample <- extractStats(opt.sample)
-
-
-# Plots
-# Plot the optimal weights
-chart.Weights(combine.optimizations(list(meucci=opt.meucci, sample=opt.sample)))
-
-# Plot the risk-reward of each chart on the same scale
-xrange <- range(c(stats.meucci[,"StdDev"], stats.sample[,"StdDev"]))
-yrange <- range(c(stats.meucci[,"mean"], stats.sample[,"mean"]))
-layout(matrix(c(1,2)), widths=1, heights=1)
-# c(bottom, left, top, right)
-par(mar=c(0, 4, 4, 4) + 0.1)
-plot(x=stats.meucci[,"StdDev"], stats.meucci[,"mean"], xlab="", ylab="mean", 
-     xlim=xrange, ylim=yrange, xaxt="n", yaxt="n")
-axis(2, pretty(yrange), cex.axis=0.8)
-legend("topright", legend="Meucci", bty="n")
-par(mar=c(5, 4, 0, 4) + 0.1)
-plot(x=stats.sample[,"StdDev"], stats.sample[,"mean"], xlab="StdDev", ylab="", 
-     xlim=xrange, ylim=yrange, yaxt="n", cex.axis=0.8)
-axis(4, pretty(yrange), cex.axis=0.8)
-legend("topright", legend="Sample", bty="n")
-par(mar=c(5, 4, 4, 2) + 0.1)
-layout(matrix(1), widths=1, heights=1)



More information about the Returnanalytics-commits mailing list