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

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Thu Aug 1 11:13:54 CEST 2013


Author: xavierv
Date: 2013-08-01 11:13:53 +0200 (Thu, 01 Aug 2013)
New Revision: 2691

Added:
   pkg/Meucci/demo/S_FactorResidualCorrelation.R
   pkg/Meucci/demo/S_SwapPca2Dim.R
Modified:
   pkg/Meucci/R/GenerateUniformDrawsOnUnitSphere.R
   pkg/Meucci/R/MvnRnd.R
   pkg/Meucci/man/GenerateUniformDrawsOnUnitSphere.Rd
Log:
- added S_FactoriResidualCorrelation and S_SwapPca2Dim demo scripts from chapter 3

Modified: pkg/Meucci/R/GenerateUniformDrawsOnUnitSphere.R
===================================================================
--- pkg/Meucci/R/GenerateUniformDrawsOnUnitSphere.R	2013-07-31 20:51:16 UTC (rev 2690)
+++ pkg/Meucci/R/GenerateUniformDrawsOnUnitSphere.R	2013-08-01 09:13:53 UTC (rev 2691)
@@ -7,9 +7,9 @@
 #'	@return   X  : [matrix] (T x N) of draws
 #'
 #'@note
-#' \item{ Initial script by Xiaoyu Wang - Dec 2006}
-#' \item{ 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 }
+#' Initial 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 
 #'
 #' @references
 #' \url{http://symmys.com/node/170}

Modified: pkg/Meucci/R/MvnRnd.R
===================================================================
--- pkg/Meucci/R/MvnRnd.R	2013-07-31 20:51:16 UTC (rev 2690)
+++ pkg/Meucci/R/MvnRnd.R	2013-08-01 09:13:53 UTC (rev 2691)
@@ -1,5 +1,3 @@
-if ( !require( "QZ" ) ) stop("QZ package installation required for this script")
-
 #' Generate normal simulations whose sample moments match the population moments,
 #' as described in  A. Meucci, "Risk and Asset Allocation", Springer, 2005.
 #'  
@@ -18,6 +16,7 @@
 
 MvnRnd = function( M, S, J )
 {
+	if ( !require( "QZ" ) ) stop("QZ package installation required for this script")
 	N = length(M);
 
 	# generate antithetic variables (mean = 0)

Added: pkg/Meucci/demo/S_FactorResidualCorrelation.R
===================================================================
--- pkg/Meucci/demo/S_FactorResidualCorrelation.R	                        (rev 0)
+++ pkg/Meucci/demo/S_FactorResidualCorrelation.R	2013-08-01 09:13:53 UTC (rev 2691)
@@ -0,0 +1,40 @@
+#' This script illustrates exogenous loadings and endogenous factors the true analytical VaR under the lognormal  
+#' assumptions from the estimation interval to the investment horizon, as described in A. Meucci,
+#' "Risk and Asset Allocation", Springer, 2005,  Chapter 3.
+#'
+#' @references
+#' \url{http://symmys.com/node/170}
+#' See Meucci's script for "S_FactorResidualCorrelation.m"
+#'
+#' @author Xavier Valls \email{flamejat@@gmail.com}
+
+##################################################################################################################
+### Input parameters:
+N     = 4; # market size
+nSim  = 10000;
+mu    = 0.1 + 0.3 * runif(N);
+sigma = 0.5 * mu; 
+dd = matrix(rnorm( N*N ), N, N );
+Corr = cov2cor( dd %*% t( dd ) );
+Sigma = diag( sigma, length(sigma) ) %*% Corr %*% diag( sigma, length(sigma) ); 
+
+##################################################################################################################
+### Generate simulations for X
+X = MvnRnd(mu, Sigma, nSim);
+
+##################################################################################################################
+### Generate a random vector beta
+beta = matrix(1, N ) + rnorm(N) * 0.1;
+
+##################################################################################################################
+### Compute factor realization by cross-sectional regression and residuals
+F = ( X %*% beta ) / ( t( beta ) %*% (beta) )[1];
+F = solve( t( beta ) %*% beta)[1] * ( X %*% beta );
+
+# compute residual
+U = X - F %*% t(beta);
+
+# correlation of residuals U among themselves and with factors F
+R = cor( cbind( F, U ) );
+print(R);
+

Added: pkg/Meucci/demo/S_SwapPca2Dim.R
===================================================================
--- pkg/Meucci/demo/S_SwapPca2Dim.R	                        (rev 0)
+++ pkg/Meucci/demo/S_SwapPca2Dim.R	2013-08-01 09:13:53 UTC (rev 2691)
@@ -0,0 +1,77 @@
+#' This script performs the principal component analysis of a simplified two-point swap curve.
+#' it computes and plots, among others, 
+#' 1. the invariants, namely rate changes
+#' 2. the location-dispersion ellipsoid of rates along with the 2-d location-dispersion ellipsoid
+#' 3. the effect on the curve of the two uncorrelated principal factors 
+#' Described in A. Meucci, "Risk and Asset Allocation", Springer, 2005,  Chapter 3.
+#'
+#' @references
+#' \url{http://}
+#' See Meucci's script for "S_AutocorrelatedProcess.m"
+#'
+#' @author Xavier Valls \email{flamejat@@gmail.com}
+##################################################################################################################
+### Load data
+load( "../data/swap2y4y.mat" );
+
+##################################################################################################################
+### Current curve
+Current_Curve = swap2y4y$Rates[ nrow( swap2y4y$Rates ), ];
+dev.new();
+plot(c( 2, 4 ), Current_Curve, type = "l", main = "Current_Curve", xlab = "time to maturity, years", ylab = "par swap rate, #" );
+
+##################################################################################################################
+### Determine weekly invariants (changes in rates)
+Keep = seq( 1, length(swap2y4y$Dates), 5 );
+
+Rates = swap2y4y$Rates[ Keep, ];
+X = Rates[ -1, ] - Rates[ -nrow( Rates ), ];
+
+Dates = swap2y4y$Dates[ Keep ];
+
+PerformIidAnalysis( Dates[ -1 ], X[ , 1 ], "weekly 2yr rates" );
+PerformIidAnalysis( Dates[ -1 ], X[ , 2 ], "weekly 4yr rates" );
+
+# scatter plot of  invariants
+dev.new();
+plot( X[ , 1 ], X[ , 2 ], xlab = "2yr rate", ylab = "4yr rate" );
+m = 0 * matrix( apply( X, 2, mean ) ); # estimator shrunk to zero
+S = cov(X);
+TwoDimEllipsoid(m, S, 2, TRUE, FALSE);
+
+
+##################################################################################################################
+### Perform PCA
+E = eigen(S);
+# sort eigenvalues in decreasing order
+Index = order(-E$values);
+EigVals = EVals[ Index ];
+EigVecs = E$vectors[ , Index ];
+
+# plot eigenvectors
+dev.new();
+plot( c( 2, 4 ), EigVecs[ , 1 ], type = "l", col = "red", xlab = "Time to maturity, years", ylab = "" );
+lines( c( 2, 4 ), EigVecs[ , 2 ], col = "green" );
+legend("topleft", 1.9, c("1st factor loading","2nd factor loading"), col = c( "red" , "green"),
+	lty = 1, bg = "gray90" );
+
+# factors
+F = X %*% EigVecs;
+F_std = apply( F, 2, sd);
+
+dev.new(); # 1-st factor effect
+plot( c( 2, 4), Current_Curve, type = "l", xlab = "Time to maturity, years", ylab = "",  ylim = c( 4.9, 5.3) );
+lines( c( 2, 4), matrix( Current_Curve ) + F_std[ 1 ] * EigVecs[ , 1 ], col = "red" );
+lines( c( 2, 4), matrix( Current_Curve ) - F_std[ 1 ] * EigVecs[ , 1 ], col = "green" );
+legend("topleft", 1.9, c( "base", "+1 sd of 1st fact","-1 sd of 1st fact"), col = c( "black", "red" , "green"),
+	lty = 1, bg = "gray90" );
+dev.new(); # 2-nd factor effect
+plot(  c( 2, 4), Current_Curve, type = "l", xlab = "Time to maturity, years", ylab = "",  ylim = c( 5, 5.15) );
+lines( c( 2, 4), matrix( Current_Curve ) + F_std[ 2 ] * EigVecs[ , 2 ], col = "red" );
+lines( c( 2, 4), matrix( Current_Curve ) - F_std[ 2 ] * EigVecs[ , 2 ], col = "green" );
+legend("topleft", 1.9, c( "base", "+1 sd of 2nd fact","-1 sd of 2nd fact"), col = c( "black", "red" , "green"),
+	lty = 1, bg = "gray90" );
+# generalized R2
+R2 = cumsum(EigVals) / sum(EigVals);  # first entry: one factor, second entry: both factors
+disp(R2);
+

Modified: pkg/Meucci/man/GenerateUniformDrawsOnUnitSphere.Rd
===================================================================
--- pkg/Meucci/man/GenerateUniformDrawsOnUnitSphere.Rd	2013-07-31 20:51:16 UTC (rev 2690)
+++ pkg/Meucci/man/GenerateUniformDrawsOnUnitSphere.Rd	2013-08-01 09:13:53 UTC (rev 2691)
@@ -19,9 +19,9 @@
   Springer, 2005.
 }
 \note{
-  \item{ Initial script by Xiaoyu Wang - Dec 2006} \item{
-  We decompose X=U*R, where U is a uniform distribution on
-  unit sphere and
+  Initial 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