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

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Wed Sep 18 18:20:01 CEST 2013


Author: xavierv
Date: 2013-09-18 18:20:01 +0200 (Wed, 18 Sep 2013)
New Revision: 3138

Modified:
   pkg/Meucci/R/GenerateUniformDrawsOnUnitSphere.R
   pkg/Meucci/demo/S_CornishFisher.R
   pkg/Meucci/demo/S_ESContributionFactors.R
   pkg/Meucci/demo/S_ESContributionsStudentT.R
   pkg/Meucci/demo/S_ExtremeValueTheory.R
   pkg/Meucci/demo/S_InvestorsObjective.R
   pkg/Meucci/demo/S_VaRContributionsUniform.R
   pkg/Meucci/man/GenerateUniformDrawsOnUnitSphere.Rd
Log:
 - updated documentation for chapter 5 demo scripts and its functions

Modified: pkg/Meucci/R/GenerateUniformDrawsOnUnitSphere.R
===================================================================
--- pkg/Meucci/R/GenerateUniformDrawsOnUnitSphere.R	2013-09-18 14:01:43 UTC (rev 3137)
+++ pkg/Meucci/R/GenerateUniformDrawsOnUnitSphere.R	2013-09-18 16:20:01 UTC (rev 3138)
@@ -1,4 +1,6 @@
-#' Generate a uniform sample on the unit hypersphere, as described in  A. Meucci,
+#' @title Generate a uniform sample on the unit hypersphere.
+#'  
+#' @description Generate a uniform sample on the unit hypersphere, as described in  A. Meucci,
 #'  "Risk and Asset Allocation", Springer, 2005.
 #'  
 #'	@param   J : [scalar] number of draws
@@ -6,10 +8,11 @@
 #'  
 #'	@return   X  : [matrix] (T x N) of draws
 #'
-#'@note
-#' Initial script by Xiaoyu Wang - Dec 2006
+#' @note
+#' Initial MATLAB's script by Xiaoyu Wang - Dec 2006
+#'
 #' We decompose X=U*R, where U is a uniform distribution on unit sphere and
-#     R is a distribution on (0,1) proportional to r^(Dims-1), i.e. the area of surface of radius r 
+#  R is a distribution on (0,1) proportional to r^(Dims-1), i.e. the area of surface of radius r.
 #'
 #' @references
 #' A. Meucci - "Exercises in Advanced Risk and Portfolio Management" \url{http://symmys.com/node/170}.

Modified: pkg/Meucci/demo/S_CornishFisher.R
===================================================================
--- pkg/Meucci/demo/S_CornishFisher.R	2013-09-18 14:01:43 UTC (rev 3137)
+++ pkg/Meucci/demo/S_CornishFisher.R	2013-09-18 16:20:01 UTC (rev 3138)
@@ -2,7 +2,9 @@
 #'assumptions as described in A. Meucci,"Risk and Asset Allocation", Springer, 2005,  Chapter 5.
 #'
 #' @references
-#' A. Meucci - "Exercises in Advanced Risk and Portfolio Management" \url{http://symmys.com/node/170}.
+#' A. Meucci - "Exercises in Advanced Risk and Portfolio Management" \url{http://symmys.com/node/170},
+#' "E 223 - Cornish-Fisher approximation of the Value-at-Risk".
+#'
 #' See Meucci's script for "S_CornishFisher.m"
 #
 #' @author Xavier Valls \email{flamejat@@gmail.com}
@@ -21,7 +23,7 @@
 c = seq(0.001, 0.999, 0.001 );
 z = qnorm( c );
 
-Q_CF = E_X + Sd_X * ( z + Sk_X  /  6 * ( z ^ 2 - 1 ) );
+Q_CF   = E_X + Sd_X * ( z + Sk_X  /  6 * ( z ^ 2 - 1 ) );
 Q_true = qlnorm( c,mu,sig );
 
 x = Q_true;

Modified: pkg/Meucci/demo/S_ESContributionFactors.R
===================================================================
--- pkg/Meucci/demo/S_ESContributionFactors.R	2013-09-18 14:01:43 UTC (rev 3137)
+++ pkg/Meucci/demo/S_ESContributionFactors.R	2013-09-18 16:20:01 UTC (rev 3138)
@@ -1,22 +1,23 @@
-library(MASS);
-library(Matrix);
 #' This script computes the expected shortfall and the contributions to ES from each factor in simulations, using 
 #' the conditional expectation definition of the contributions as described in A. Meucci,"Risk and Asset Allocation",
 #' Springer, 2005,  Chapter 5. 
 #'
 #' @references
-#' A. Meucci - "Exercises in Advanced Risk and Portfolio Management" \url{http://symmys.com/node/170}.
+#' A. Meucci - "Exercises in Advanced Risk and Portfolio Management" \url{http://symmys.com/node/170},
+#' "E 235 - Expected shortfall and linear factor models".
+#'
 #' See Meucci's script for "S_ESContributionFactors.m"
 #
 #' @author Xavier Valls \email{flamejat@@gmail.com}
 
 ###################################################################################################################
 ### Inputs 
+if( !require( "Matrix" ) ) stop( "Matrix package installation is required to run this demo script" );
 
-N = 30; # number of securities
-K = 10; # number of factors
+N = 30;       # number of securities
+K = 10;       # number of factors
 a = runif(N); # allocation
-c = 0.95;      # ES confidence
+c = 0.95;     # ES confidence
 
 ###################################################################################################################
 ### Generate market simulations
@@ -41,6 +42,7 @@
 sigma = as.matrix(.bdiag(list( eps * sigma_f, eps^2 * sigma_u))) #block diagonal matrix
 corr  = cov2cor( sigma );
 diag_sigma = sqrt( diag( sigma ) );
+
 # scenarios
 nSim = 10000;
 l = matrix( 1, nSim);
@@ -55,17 +57,18 @@
 
 ###################################################################################################################
 ### Risk management
+
 # compute the objective
 Psi = M %*% a; 
 
 # compute ES
-th = ceiling((1-c) * nSim); # threshold
+th  = ceiling((1-c) * nSim); # threshold
 spc = matrix( 0, nSim, 1 );
 spc[ 1 : th ] = 1;
 spc = spc / sum( spc );
 
 Sort_Psi = sort( Psi );
-Index = order( Psi );
+Index    = order( Psi );
 ES_simul = t(Sort_Psi) %*% spc;
 
 # augment factor set to include residual
@@ -74,8 +77,7 @@
 b_ = cbind( t(a)%*%B, 1 );
 
 # sort factors according to order induced by objective's realizations
-
-Sort_F_ = F_[Index, ];
+Sort_F_   = F_[Index, ];
 DES_simul = matrix( NaN, 1, K+1 );
 for( k in 1 : (K+1) )
 {

Modified: pkg/Meucci/demo/S_ESContributionsStudentT.R
===================================================================
--- pkg/Meucci/demo/S_ESContributionsStudentT.R	2013-09-18 14:01:43 UTC (rev 3137)
+++ pkg/Meucci/demo/S_ESContributionsStudentT.R	2013-09-18 16:20:01 UTC (rev 3138)
@@ -1,12 +1,12 @@
-library(MASS);
-library(Matrix);
 #' This script computes the expected shortfall and the contributions to ES from each security: 
 #' - analytically, under the Student t assumption for the market
 #' - in simulations, using the conditional expectation definition of the contributions
 #' Described in A. Meucci,"Risk and Asset Allocation",Springer, 2005,  Chapter 5.  
 #'
 #' @references
-#' A. Meucci - "Exercises in Advanced Risk and Portfolio Management" \url{http://symmys.com/node/170}.
+#' A. Meucci - "Exercises in Advanced Risk and Portfolio Management" \url{http://symmys.com/node/170},
+#' "E 234 - Expected shortfall in elliptical markets III".
+#'
 #' See Meucci's script for "S_ESContributionsStudentT.m"
 #
 #' @author Xavier Valls \email{flamejat@@gmail.com}

Modified: pkg/Meucci/demo/S_ExtremeValueTheory.R
===================================================================
--- pkg/Meucci/demo/S_ExtremeValueTheory.R	2013-09-18 14:01:43 UTC (rev 3137)
+++ pkg/Meucci/demo/S_ExtremeValueTheory.R	2013-09-18 16:20:01 UTC (rev 3138)
@@ -5,7 +5,9 @@
 #' Described in A. Meucci,"Risk and Asset Allocation",Springer, 2005,  Chapter 5.  
 #'
 #' @references
-#' A. Meucci - "Exercises in Advanced Risk and Portfolio Management" \url{http://symmys.com/node/170}.
+#' A. Meucci - "Exercises in Advanced Risk and Portfolio Management" \url{http://symmys.com/node/170},
+#' "E 229 - Extreme value theory approximation of Value-at-Risk".
+#'
 #' See Meucci's script for "S_ExtremeValueTheory.m"
 #
 #' @author Xavier Valls \email{flamejat@@gmail.com}

Modified: pkg/Meucci/demo/S_InvestorsObjective.R
===================================================================
--- pkg/Meucci/demo/S_InvestorsObjective.R	2013-09-18 14:01:43 UTC (rev 3137)
+++ pkg/Meucci/demo/S_InvestorsObjective.R	2013-09-18 16:20:01 UTC (rev 3138)
@@ -3,7 +3,9 @@
 #' Allocation",Springer, 2005,  Chapter 5.  
 #'
 #' @references
-#' A. Meucci - "Exercises in Advanced Risk and Portfolio Management" \url{http://symmys.com/node/170}.
+#' A. Meucci - "Exercises in Advanced Risk and Portfolio Management" \url{http://symmys.com/node/170},
+#' "E 236 - Simulation of the investor’s objectives".
+#'
 #' See Meucci's script for "S_InvestorsObjective.m"
 #
 #' @author Xavier Valls \email{flamejat@@gmail.com}
@@ -33,19 +35,19 @@
 ### Compute current prices
 p_1 = nu_1 * s2_1;
 p_2 = exp( mu_2 + 0.5 * s2_2 ^ 2 );
-p = matrix( c( p_1, p_2 ));
+p   = matrix( c( p_1, p_2 ));
 
 ##################################################################################################################
 ### Generate samnple of prices at the investment horizon
-N = rmvnorm(J, cbind( 0, 0 ), rbind( c(1, r), c(r, 1)));
+N   = rmvnorm(J, cbind( 0, 0 ), rbind( c(1, r), c(r, 1)));
 N_1 = N[ , 1 ];
 N_2 = N[ , 2 ];
 
 U_1 = pnorm( N_1 );
 U_2 = pnorm( N_2 );
 
-aa = nu_1 / 2;
-bb = 2 * s2_1;
+aa  = nu_1 / 2;
+bb  = 2 * s2_1;
 P_1 = qgamma( U_1, aa, scale = bb);
 P_2 = qlnorm( U_2, mu_2, sqrt(s2_2));
 
@@ -58,7 +60,7 @@
 PnL = (P - matrix( 1, J, 1) %*% t( p )) %*% a;
 
 # generate sample of benchmark-relative wealth
-K = diag(1, 2) - p %*% t(b) / (t(b) %*% p)[1];
+K    = diag(1, 2) - p %*% t(b) / (t(b) %*% p)[1];
 WRel = P %*% t(K) %*% a;
 
 ##################################################################################################################

Modified: pkg/Meucci/demo/S_VaRContributionsUniform.R
===================================================================
--- pkg/Meucci/demo/S_VaRContributionsUniform.R	2013-09-18 14:01:43 UTC (rev 3137)
+++ pkg/Meucci/demo/S_VaRContributionsUniform.R	2013-09-18 16:20:01 UTC (rev 3138)
@@ -1,4 +1,3 @@
-
 #' This script computes the VaR and the contributions to VaR from each security 
 #'  - analytically, under the elliptical-uniform assumption for the market
 #'  - in simulations, using the conditional expectation definition of the contributions
@@ -6,8 +5,10 @@
 #' Allocation",Springer, 2005,  Chapter 5.  
 #'
 #' @references
-#' A. Meucci - "Exercises in Advanced Risk and Portfolio Management" \url{http://symmys.com/node/170}.
-#' See Meucci's script for "S_VaRContributionsUniform.m"
+#' A. Meucci - "Exercises in Advanced Risk and Portfolio Management" \url{http://symmys.com/node/170},
+#' "E 222 - Value-at-Risk in elliptical markets III".
+#'
+#' See Meucci's script for "S_VaRContributionsUniform.m" and E 220 from the book.
 #
 #' @author Xavier Valls \email{flamejat@@gmail.com}
 
@@ -17,8 +18,8 @@
 N = 10; 
 
 # market parameters (uniform on ellipsoid)
-Mu = matrix(runif(N));
-A  = matrix( runif(N*N), N, N) - 0.5;
+Mu    = matrix(runif(N));
+A     = matrix( runif(N*N), N, N) - 0.5;
 Sigma = A * t(A);
 
 # allocation
@@ -37,10 +38,11 @@
 ###################################################################################################################
 ### Compute contributions by simulations (brute-force approach)
 # compute and sort the objective
-Psi = M %*% a;
+Psi   = M %*% a;
 Q_sim = quantile( Psi, (1 - c) );
 
 e = mean( abs( a )) / 100; # perturbation
+
 DQ_simul = matrix( NaN, 1, N) ;
 for( n in 1 : N )
 {
@@ -48,10 +50,11 @@
     a_e = a;
     a_e[ n ] = a[ n ] + e;
     
-    Psi_e = M %*% a_e;
+    Psi_e   = M %*% a_e;
     Q_sim_e = quantile(Psi_e, (1 - c) );
     DQ_simul[ n ] = ( Q_sim_e - Q_sim )/e;
 }
+
 # compute contributions
 ContrQ_simul = a * t( DQ_simul );
 
@@ -61,8 +64,8 @@
 gc = quantile(X[  ,1 ], (1 - c));
 
 # ...the dependence on the allocation is analytical
-Q_an  = t(Mu) %*% a + gc * sqrt( t(a) %*% Sigma %*% a );
-DQ_an = Mu + gc * Sigma %*% a / sqrt( t(a) %*% Sigma %*% a )[1];
+Q_an      = t(Mu) %*% a + gc * sqrt( t(a) %*% Sigma %*% a );
+DQ_an     = Mu + gc * Sigma %*% a / sqrt( t(a) %*% Sigma %*% a )[1];
 ContrQ_an = a * DQ_an;
 
 ###################################################################################################################

Modified: pkg/Meucci/man/GenerateUniformDrawsOnUnitSphere.Rd
===================================================================
--- pkg/Meucci/man/GenerateUniformDrawsOnUnitSphere.Rd	2013-09-18 14:01:43 UTC (rev 3137)
+++ pkg/Meucci/man/GenerateUniformDrawsOnUnitSphere.Rd	2013-09-18 16:20:01 UTC (rev 3138)
@@ -1,7 +1,6 @@
 \name{GenerateUniformDrawsOnUnitSphere}
 \alias{GenerateUniformDrawsOnUnitSphere}
-\title{Generate a uniform sample on the unit hypersphere, as described in  A. Meucci,
- "Risk and Asset Allocation", Springer, 2005.}
+\title{Generate a uniform sample on the unit hypersphere.}
 \usage{
   GenerateUniformDrawsOnUnitSphere(J, N)
 }
@@ -19,9 +18,10 @@
   Springer, 2005.
 }
 \note{
-  Initial script by Xiaoyu Wang - Dec 2006 We decompose
-  X=U*R, where U is a uniform distribution on unit sphere
-  and
+  Initial MATLAB's script by Xiaoyu Wang - Dec 2006
+
+  We decompose X=U*R, where U is a uniform distribution on
+  unit sphere and
 }
 \author{
   Xavier Valls \email{flamejat at gmail.com}



More information about the Returnanalytics-commits mailing list