[Returnanalytics-commits] r2928 - in pkg/Meucci: . R data demo man

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Thu Aug 29 12:03:25 CEST 2013


Author: xavierv
Date: 2013-08-29 12:03:24 +0200 (Thu, 29 Aug 2013)
New Revision: 2928

Added:
   pkg/Meucci/R/PlotVolVsCompositionEfficientFrontier.R
   pkg/Meucci/data/covNRets.Rda
   pkg/Meucci/demo/S_MeanVarianceCalls.R
   pkg/Meucci/man/PlotVolVsCompositionEfficientFrontier.Rd
Modified:
   pkg/Meucci/DESCRIPTION
   pkg/Meucci/NAMESPACE
   pkg/Meucci/man/FitOrnsteinUhlenbeck.Rd
Log:
- added S_MeanVarianceCalls demo script from chapter 6 and its associated functions

Modified: pkg/Meucci/DESCRIPTION
===================================================================
--- pkg/Meucci/DESCRIPTION	2013-08-29 09:11:47 UTC (rev 2927)
+++ pkg/Meucci/DESCRIPTION	2013-08-29 10:03:24 UTC (rev 2928)
@@ -94,3 +94,6 @@
     'EfficientFrontierReturns.R'
     'EfficientFrontierPrices.R'
     'FitOrnsteinUhlenbeck.R'
+    '
+    FitOrnsteinUhlenbeck.R'
+    'PlotVolVsCompositionEfficientFrontier.R'

Modified: pkg/Meucci/NAMESPACE
===================================================================
--- pkg/Meucci/NAMESPACE	2013-08-29 09:11:47 UTC (rev 2927)
+++ pkg/Meucci/NAMESPACE	2013-08-29 10:03:24 UTC (rev 2928)
@@ -39,6 +39,7 @@
 export(PerformIidAnalysis)
 export(PlotDistributions)
 export(PlotMarginalsNormalInverseWishart)
+export(PlotVolVsCompositionEfficientFrontier)
 export(ProjectionStudentT)
 export(QuantileMixture)
 export(RandNormalInverseWishart)

Added: pkg/Meucci/R/PlotVolVsCompositionEfficientFrontier.R
===================================================================
--- pkg/Meucci/R/PlotVolVsCompositionEfficientFrontier.R	                        (rev 0)
+++ pkg/Meucci/R/PlotVolVsCompositionEfficientFrontier.R	2013-08-29 10:03:24 UTC (rev 2928)
@@ -0,0 +1,38 @@
+#' Plot the efficient frontier in the plane of portfolio weights versus standard deviation,
+#' as described in  A. Meucci, "Risk and Asset Allocation", Springer, 2005.
+#'  
+#'	@param   Portfolios: [matrix] (M x N) of portfolios weights
+#'	@param   vol       : [vector] (M x 1) of volatilities
+#'
+#' @references
+#' \url{http://symmys.com/node/170}
+#' See Meucci's script for "PlotVolVsCompositionEfficientFrontier.m"
+#'
+#' @author Xavier Valls \email{flamejat@@gmail.com}
+#' @export
+
+PlotVolVsCompositionEfficientFrontier = function( Portfolios, vol )
+{
+
+	colors = c( "cyan","white","magenta","green","black","red" );
+	numcolors = length(colors);
+
+	dev.new();
+	xx = dim( Portfolios )[ 1 ];
+	N  = dim( Portfolios )[ 2 ];
+	
+	Data = t( apply( Portfolios, 1, cumsum ) );
+	plot(c(0,0), xlim= c( min(vol)*100, max(vol)*100), ylim = c(0, max(Data)), xlab = "Risk %", ylab = "Portfolio weights") ;
+
+	for( n in 1 : N )
+	{
+	    x = rbind( 1, matrix( 1 : xx ), xx );
+	    v = rbind( min(vol), vol, max(vol) ) * 100
+	    y = rbind( 0, matrix(Data[ , N-n+1 ]), 0 );
+	    polygon( v, y, col = colors[ mod( n, numcolors ) + 1 ] );
+	}
+	#set(gca,'xlim',[v(1) v(end)],'ylim',[0 max(max(Data))])
+	#xlabel('Risk %')
+    #ylabel('Portfolio weights')
+
+}
\ No newline at end of file

Added: pkg/Meucci/data/covNRets.Rda
===================================================================
(Binary files differ)


Property changes on: pkg/Meucci/data/covNRets.Rda
___________________________________________________________________
Added: svn:mime-type
   + application/octet-stream

Added: pkg/Meucci/demo/S_MeanVarianceCalls.R
===================================================================
--- pkg/Meucci/demo/S_MeanVarianceCalls.R	                        (rev 0)
+++ pkg/Meucci/demo/S_MeanVarianceCalls.R	2013-08-29 10:03:24 UTC (rev 2928)
@@ -0,0 +1,75 @@
+#' This script computes the mean-variance frontier of a set of options
+#' Described in A. Meucci,"Risk and Asset Allocation", Springer, 2005,  Chapter 6.
+#'
+#' @references
+#' \url{http://symmys.com/node/170}
+#' See Meucci's script for "S_MeanVarianceCalls.m"
+#
+#' @author Xavier Valls \email{flamejat@@gmail.com}
+##################################################################################################################
+### Load dat
+
+load("../data/db.Rda" );
+
+##################################################################################################################
+### Inputs
+
+# market
+Stock_0 = db$Stock[ nrow(db$Stock), ];
+Vol_0   = db$Vol[ nrow(db$Stock), ];
+Strike  = Stock_0; # ATM strike
+Est = apply( diff( db$Dates ), 2, mean ) / 252; # estimation interval
+Hor = 2 * Est; # investment horizon
+
+# constraints
+N = length( Vol_0 );
+Constr = list( Aeq = matrix( 1, 1, N ), beq = 1, Aleq = rbind( diag( 1, N ), -diag( 1, N ) ), 
+			   bleq = rbind( matrix( 1, N, 1 ), matrix( 0, N, 1 ) ) 
+			  );
+
+J = 10000; # num simulations
+
+##################################################################################################################
+### Allocation process
+# quest for invariance
+x_Stock = diff( log( db$Stock ) );
+x_Vol   = diff( log( db$Vol ) );
+
+# estimation
+M = matrix( apply(cbind( x_Stock, x_Vol ), 2, mean ) );
+S = cov( cbind( x_Stock, x_Vol ) );
+
+# projection
+M_Hor = M * Hor / Est;
+S_Hor = S * Hor / Est;
+X = rmvnorm( J, M_Hor, S_Hor, method = "svd" );
+X_Stock = X[ , 1:N ];
+X_Vol = X[ , (N + 1):ncol(X) ];
+
+Stock_Hor = repmat( Stock_0,  J, 1  ) * exp( X_Stock );
+Vol_Hor   = repmat( Vol_0,  J, 1  ) * exp( X_Vol );
+
+##################################################################################################################
+### Pricing
+Call_0 = NULL;
+Call_Hor = NULL;
+for( n in 1 : N )
+{
+    Rate = 0.04;
+    Call_0   = cbind( Call_0, BlackScholesCallPrice( Stock_0[ n ], Strike[ n ], Rate, Vol_0[ n ], db$Expiry[ n ] )$c );
+    Call_Hor = cbind( Call_Hor, BlackScholesCallPrice(Stock_Hor[ , n ], Strike[ n ], Rate, Vol_Hor[  ,n ], db$Expiry[ n] - Hor )$c );
+}
+
+##################################################################################################################
+### Mean-variance
+L = Call_Hor / repmat( Call_0, J, 1  ) - 1;
+ExpectedValues = matrix( apply( L, 2, mean) );
+Covariance = cov( L );
+NumPortf = 40;
+#[e, vol, w] = 
+EFR = EfficientFrontierReturns( NumPortf, Covariance, ExpectedValues, Constr );
+
+##################################################################################################################
+### Plots
+PlotVolVsCompositionEfficientFrontier( EFR$Composition, EFR$Volatility );
+

Modified: pkg/Meucci/man/FitOrnsteinUhlenbeck.Rd
===================================================================
--- pkg/Meucci/man/FitOrnsteinUhlenbeck.Rd	2013-08-29 09:11:47 UTC (rev 2927)
+++ pkg/Meucci/man/FitOrnsteinUhlenbeck.Rd	2013-08-29 10:03:24 UTC (rev 2928)
@@ -56,9 +56,9 @@
 }
 \references{
   \url{http://symmys.com/node/170} See Meucci's script for
-  "EfficientFrontierReturns.m"
+  "FitOrnsteinUhlenbeck.m"
 
   \url{http://symmys.com/node/170} See Meucci's script for
-  "FitOrnsteinUhlenbeck.m"
+  "EfficientFrontierReturns.m"
 }
 

Added: pkg/Meucci/man/PlotVolVsCompositionEfficientFrontier.Rd
===================================================================
--- pkg/Meucci/man/PlotVolVsCompositionEfficientFrontier.Rd	                        (rev 0)
+++ pkg/Meucci/man/PlotVolVsCompositionEfficientFrontier.Rd	2013-08-29 10:03:24 UTC (rev 2928)
@@ -0,0 +1,26 @@
+\name{PlotVolVsCompositionEfficientFrontier}
+\alias{PlotVolVsCompositionEfficientFrontier}
+\title{Plot the efficient frontier in the plane of portfolio weights versus standard deviation,
+as described in  A. Meucci, "Risk and Asset Allocation", Springer, 2005.}
+\usage{
+  PlotVolVsCompositionEfficientFrontier(Portfolios, vol)
+}
+\arguments{
+  \item{Portfolios:}{[matrix] (M x N) of portfolios
+  weights}
+
+  \item{vol}{: [vector] (M x 1) of volatilities}
+}
+\description{
+  Plot the efficient frontier in the plane of portfolio
+  weights versus standard deviation, as described in A.
+  Meucci, "Risk and Asset Allocation", Springer, 2005.
+}
+\author{
+  Xavier Valls \email{flamejat at gmail.com}
+}
+\references{
+  \url{http://symmys.com/node/170} See Meucci's script for
+  "PlotVolVsCompositionEfficientFrontier.m"
+}
+



More information about the Returnanalytics-commits mailing list