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

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Thu Aug 29 12:47:04 CEST 2013


Author: xavierv
Date: 2013-08-29 12:47:04 +0200 (Thu, 29 Aug 2013)
New Revision: 2930

Added:
   pkg/Meucci/R/BlackLittermanFormula.R
   pkg/Meucci/R/Log2Lin.R
   pkg/Meucci/R/PlotCompositionEfficientFrontier.R
   pkg/Meucci/demo/S_BlackLittermanBasic.R
   pkg/Meucci/man/BlackLittermanFormula.Rd
   pkg/Meucci/man/Log2Lin.Rd
   pkg/Meucci/man/PlotCompositionEfficientFrontier.Rd
Modified:
   pkg/Meucci/DESCRIPTION
   pkg/Meucci/NAMESPACE
   pkg/Meucci/R/PlotVolVsCompositionEfficientFrontier.R
Log:
- added S_BlackLittermanBasic.R demo script from chapter 9 and its associated functions

Modified: pkg/Meucci/DESCRIPTION
===================================================================
--- pkg/Meucci/DESCRIPTION	2013-08-29 10:11:43 UTC (rev 2929)
+++ pkg/Meucci/DESCRIPTION	2013-08-29 10:47:04 UTC (rev 2930)
@@ -94,6 +94,9 @@
     'EfficientFrontierReturns.R'
     'EfficientFrontierPrices.R'
     'FitOrnsteinUhlenbeck.R'
+    'PlotVolVsCompositionEfficientFrontier.R'
+    'BlackLittermanFormula.R'
     '
     FitOrnsteinUhlenbeck.R'
-    'PlotVolVsCompositionEfficientFrontier.R'
+    'Log2Lin.R'
+    'PlotCompositionEfficientFrontier.R'

Modified: pkg/Meucci/NAMESPACE
===================================================================
--- pkg/Meucci/NAMESPACE	2013-08-29 10:11:43 UTC (rev 2929)
+++ pkg/Meucci/NAMESPACE	2013-08-29 10:47:04 UTC (rev 2930)
@@ -1,3 +1,4 @@
+export(BlackLittermanFormula)
 export(BlackScholesCallPrice)
 export(Central2Raw)
 export(CentralAndStandardizedStatistics)
@@ -25,6 +26,7 @@
 export(integrateSubIntervals)
 export(InterExtrapolate)
 export(linreturn)
+export(Log2Lin)
 export(LognormalCopulaPdf)
 export(LognormalMoments2Parameters)
 export(LognormalParam2Statistics)
@@ -37,6 +39,7 @@
 export(PanicCopula)
 export(PartialConfidencePosterior)
 export(PerformIidAnalysis)
+export(PlotCompositionEfficientFrontier)
 export(PlotDistributions)
 export(PlotMarginalsNormalInverseWishart)
 export(PlotVolVsCompositionEfficientFrontier)

Added: pkg/Meucci/R/BlackLittermanFormula.R
===================================================================
--- pkg/Meucci/R/BlackLittermanFormula.R	                        (rev 0)
+++ pkg/Meucci/R/BlackLittermanFormula.R	2013-08-29 10:47:04 UTC (rev 2930)
@@ -0,0 +1,27 @@
+#' This function computes the Black-Litterman formula for the moments of the posterior normal, as described in  
+#' A. Meucci, "Risk and Asset Allocation", Springer, 2005.
+#' 
+#'   @param		Mu      : [vector] (N x 1) prior expected values.
+#'   @param		Sigma   : [matrix] (N x N) prior covariance matrix.
+#'   @param		P       : [matrix] (K x N) pick matrix.
+#'   @param		v       : [vector] (K x 1) vector of views.
+#'   @param		Omega   : [matrix] (K x K) matrix of confidence.
+#'
+#'   @return	BLMu    : [vector] (N x 1) posterior expected values.
+#'   @return	BLSigma : [matrix] (N x N) posterior covariance matrix.
+#'
+#' @references
+#' \url{http://}
+#' See Meucci's script for "BlackLittermanFormula.m"
+#'
+#' @author Xavier Valls \email{flamejat@@gmail.com}
+#' @export
+
+BlackLittermanFormula = function( Mu, Sigma, P, v, Omega)
+{
+	BLMu    = Mu + Sigma %*% t( P ) %*% ( solve( P %*% Sigma %*% t( P ) + Omega ) %*% ( v - P %*% Mu ) );
+	BLSigma =  Sigma -  Sigma %*% t( P ) %*% ( solve( P %*% Sigma %*% t( P ) + Omega ) %*% ( P %*% Sigma ) );
+
+	return( list( BLMu = BLMu , BLSigma = BLSigma ) );
+
+}
\ No newline at end of file

Added: pkg/Meucci/R/Log2Lin.R
===================================================================
--- pkg/Meucci/R/Log2Lin.R	                        (rev 0)
+++ pkg/Meucci/R/Log2Lin.R	2013-08-29 10:47:04 UTC (rev 2930)
@@ -0,0 +1,23 @@
+#' Map moments of log-returns to linear returns, as described in  A. Meucci,
+#' "Risk and Asset Allocation", Springer, 2005.
+#'  
+#'	@param   Mu    : [vector] (N x 1)
+#'	@param   Sigma : [matrix] (N x N)
+#'  
+#'	@return  M     : [vector] (N x 1)
+#'  @return  S     : [matrix] (N x N)
+#'
+#' @references
+#' \url{http://symmys.com/node/170}
+#' See Meucci's script for "Log2Lin.m"
+#'
+#' @author Xavier Valls \email{flamejat@@gmail.com}
+#' @export
+
+Log2Lin = function( Mu, Sigma )
+{
+	M = exp( Mu + (1/2) * diag( Sigma )) - 1;
+	S = exp( Mu + (1/2) * diag( Sigma )) %*% t( exp( Mu + ( 1/2 ) * diag(Sigma) ) ) * ( exp( Sigma ) - 1 );
+
+	return( list( M = M, S = S ) );
+}
\ No newline at end of file

Added: pkg/Meucci/R/PlotCompositionEfficientFrontier.R
===================================================================
--- pkg/Meucci/R/PlotCompositionEfficientFrontier.R	                        (rev 0)
+++ pkg/Meucci/R/PlotCompositionEfficientFrontier.R	2013-08-29 10:47:04 UTC (rev 2930)
@@ -0,0 +1,30 @@
+#' Plot the efficient frontier, as described in  A. Meucci,
+#' "Risk and Asset Allocation", Springer, 2005.
+#'  
+#'	@param   Portfolios : [matrix] (M x N) M portfolios of size N (weights)
+#'
+#' @references
+#' \url{http://symmys.com/node/170}
+#' See Meucci's script for "PlotCompositionEfficientFrontier.m"
+#'
+#' @author Xavier Valls \email{flamejat@@gmail.com}
+#' @export
+
+PlotCompositionEfficientFrontier = function(Portfolios)
+{
+	dev.new();
+
+	xx = dim( Portfolios )[ 1 ];
+	N  = dim( Portfolios )[ 2 ];
+	Data = t( apply( Portfolios, 1, cumsum ) );
+
+	plot( c(2000, 2000), xlim= c( 1, xx ), ylim = c( 0, max(Data) ), xlab = " Portfolio # risk propensity", ylab = "Portfolio composition" );
+	
+	for( n in 1 : N )
+	{
+	    x = rbind( 1, matrix(1 : xx), xx );
+	    y = rbind( 0, matrix( Data[ , N-n+1 ] ), 0 );
+	    polygon( x, y, col = rgb( 0.9 - mod(n,3)*0.2, 0.9 - mod(n,3)*0.2, 0.9 - mod(n,3)*0.2) );
+	}
+
+}
\ No newline at end of file

Modified: pkg/Meucci/R/PlotVolVsCompositionEfficientFrontier.R
===================================================================
--- pkg/Meucci/R/PlotVolVsCompositionEfficientFrontier.R	2013-08-29 10:11:43 UTC (rev 2929)
+++ pkg/Meucci/R/PlotVolVsCompositionEfficientFrontier.R	2013-08-29 10:47:04 UTC (rev 2930)
@@ -31,8 +31,5 @@
 	    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/demo/S_BlackLittermanBasic.R
===================================================================
--- pkg/Meucci/demo/S_BlackLittermanBasic.R	                        (rev 0)
+++ pkg/Meucci/demo/S_BlackLittermanBasic.R	2013-08-29 10:47:04 UTC (rev 2930)
@@ -0,0 +1,34 @@
+#' This script describes to basic market-based Black-Litterman approach in particular: 
+#' - full confidence = conditional
+#' - no confidence   = reference model
+#' Described in A. Meucci, "Risk and Asset Allocation",
+#' Springer, 2005,  Chapter 9.
+#'
+#' @references
+#' \url{http://symmys.com/node/170}
+#' See Meucci's script for "S_BlackLittermanBasic.m"
+#'
+#' @author Xavier Valls \email{flamejat@@gmail.com}
+
+##################################################################################################################
+### Load inputs
+load("../data/covNRets.Rda"); 
+
+##################################################################################################################
+### Compute efficient frontier
+NumPortf = 40; # number of MV-efficient portfolios 
+L2L = Log2Lin( covNRets$Mu, covNRets$Sigma );
+EFR = EfficientFrontierReturns( NumPortf, L2L$S, L2L$M );
+PlotCompositionEfficientFrontier( EFR$Composition );
+
+##################################################################################################################
+### Modify expected returns the Black-Litterman way and compute new efficient frontier 
+P = cbind( 1, 0, 0, 0, 0, -1 ); # pick matrix
+Omega = P %*% covNRets$Sigma %*% t( P );
+Views = sqrt( diag( Omega ) ); # views value
+
+B = BlackLittermanFormula( covNRets$Mu, covNRets$Sigma, P, Views, Omega );
+
+L2LBL = Log2Lin( B$BLMu, B$BLSigma );                
+EFRBL = EfficientFrontierReturns( NumPortf, L2LBL$S, L2LBL$M );
+PlotCompositionEfficientFrontier( EFRBL$Composition );

Added: pkg/Meucci/man/BlackLittermanFormula.Rd
===================================================================
--- pkg/Meucci/man/BlackLittermanFormula.Rd	                        (rev 0)
+++ pkg/Meucci/man/BlackLittermanFormula.Rd	2013-08-29 10:47:04 UTC (rev 2930)
@@ -0,0 +1,36 @@
+\name{BlackLittermanFormula}
+\alias{BlackLittermanFormula}
+\title{This function computes the Black-Litterman formula for the moments of the posterior normal, as described in
+A. Meucci, "Risk and Asset Allocation", Springer, 2005.}
+\usage{
+  BlackLittermanFormula(Mu, Sigma, P, v, Omega)
+}
+\arguments{
+  \item{Mu}{: [vector] (N x 1) prior expected values.}
+
+  \item{Sigma}{: [matrix] (N x N) prior covariance matrix.}
+
+  \item{P}{: [matrix] (K x N) pick matrix.}
+
+  \item{v}{: [vector] (K x 1) vector of views.}
+
+  \item{Omega}{: [matrix] (K x K) matrix of confidence.}
+}
+\value{
+  BLMu : [vector] (N x 1) posterior expected values.
+
+  BLSigma : [matrix] (N x N) posterior covariance matrix.
+}
+\description{
+  This function computes the Black-Litterman formula for
+  the moments of the posterior normal, as described in A.
+  Meucci, "Risk and Asset Allocation", Springer, 2005.
+}
+\author{
+  Xavier Valls \email{flamejat at gmail.com}
+}
+\references{
+  \url{http://} See Meucci's script for
+  "BlackLittermanFormula.m"
+}
+

Added: pkg/Meucci/man/Log2Lin.Rd
===================================================================
--- pkg/Meucci/man/Log2Lin.Rd	                        (rev 0)
+++ pkg/Meucci/man/Log2Lin.Rd	2013-08-29 10:47:04 UTC (rev 2930)
@@ -0,0 +1,30 @@
+\name{Log2Lin}
+\alias{Log2Lin}
+\title{Map moments of log-returns to linear returns, as described in  A. Meucci,
+"Risk and Asset Allocation", Springer, 2005.}
+\usage{
+  Log2Lin(Mu, Sigma)
+}
+\arguments{
+  \item{Mu}{: [vector] (N x 1)}
+
+  \item{Sigma}{: [matrix] (N x N)}
+}
+\value{
+  M : [vector] (N x 1)
+
+  S : [matrix] (N x N)
+}
+\description{
+  Map moments of log-returns to linear returns, 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
+  "Log2Lin.m"
+}
+

Added: pkg/Meucci/man/PlotCompositionEfficientFrontier.Rd
===================================================================
--- pkg/Meucci/man/PlotCompositionEfficientFrontier.Rd	                        (rev 0)
+++ pkg/Meucci/man/PlotCompositionEfficientFrontier.Rd	2013-08-29 10:47:04 UTC (rev 2930)
@@ -0,0 +1,23 @@
+\name{PlotCompositionEfficientFrontier}
+\alias{PlotCompositionEfficientFrontier}
+\title{Plot the efficient frontier, as described in  A. Meucci,
+"Risk and Asset Allocation", Springer, 2005.}
+\usage{
+  PlotCompositionEfficientFrontier(Portfolios)
+}
+\arguments{
+  \item{Portfolios}{: [matrix] (M x N) M portfolios of size
+  N (weights)}
+}
+\description{
+  Plot the efficient frontier, 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
+  "PlotCompositionEfficientFrontier.m"
+}
+



More information about the Returnanalytics-commits mailing list