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

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Tue Jul 30 19:43:49 CEST 2013


Author: xavierv
Date: 2013-07-30 19:43:49 +0200 (Tue, 30 Jul 2013)
New Revision: 2681

Added:
   pkg/Meucci/demo/S_ExtremeValueTheory.R
Modified:
   pkg/Meucci/DESCRIPTION
   pkg/Meucci/R/MvnRnd.R
   pkg/Meucci/demo/S_BivariateSample.R
   pkg/Meucci/demo/S_CallsProjectionPricing.R
   pkg/Meucci/demo/S_DisplayNormalCopulaCdf.R
   pkg/Meucci/demo/S_EllipticalNDim.R
   pkg/Meucci/demo/S_OrderStatisticsPdfLognormal.R
   pkg/Meucci/demo/S_OrderStatisticsPdfStudentT.R
   pkg/Meucci/demo/S_TStatApprox.R
   pkg/Meucci/demo/S_Wishart.R
   pkg/Meucci/demo/S_WishartLocationDispersion.R
Log:
- added last demo script remaning from chapter 5

Modified: pkg/Meucci/DESCRIPTION
===================================================================
--- pkg/Meucci/DESCRIPTION	2013-07-30 15:06:33 UTC (rev 2680)
+++ pkg/Meucci/DESCRIPTION	2013-07-30 17:43:49 UTC (rev 2681)
@@ -33,8 +33,7 @@
     R.utils,
     mvtnorm,
     dlm,
-    quadprog,
-    signal
+    quadprog
 Suggests:
     limSolve,
     Matrix,
@@ -48,6 +47,8 @@
     expm,
     latticeExtra,
     scatterplot3d,
+    signal,
+    fExtremes
 License: GPL
 URL: http://r-forge.r-project.org/projects/returnanalytics/
 Copyright: (c) 2012

Modified: pkg/Meucci/R/MvnRnd.R
===================================================================
--- pkg/Meucci/R/MvnRnd.R	2013-07-30 15:06:33 UTC (rev 2680)
+++ pkg/Meucci/R/MvnRnd.R	2013-07-30 17:43:49 UTC (rev 2681)
@@ -20,10 +20,10 @@
   return( X = X )
     
     # # compute sample covariance: NOTE defined as "cov(Y,1)", not as "cov(Y)"
-    # S_ = cov( Y , 1 )
+    # S_ = cov( Y )
     # 
     # # solve Riccati equation using Schur method
-    #     zerosMatrix = matrix( rep( 0 , length( N * N ) ) , nrow = N )
+    #     zerosMatrix = matrix( 0 ,  N ,  N );
     #     # define the Hamiltonian matrix
     #     H1 = cbind( zerosMatrix , -1*S_ )
     #     H2 = cbind( -S , zerosMatrix ) 

Modified: pkg/Meucci/demo/S_BivariateSample.R
===================================================================
--- pkg/Meucci/demo/S_BivariateSample.R	2013-07-30 15:06:33 UTC (rev 2680)
+++ pkg/Meucci/demo/S_BivariateSample.R	2013-07-30 17:43:49 UTC (rev 2681)
@@ -1,6 +1,3 @@
-library(mvtnorm);
-library(latticeExtra);
-
 #' This script generates draws from a bivariate distribution with different marginals,
 #' as described in A. Meucci, "Risk and Asset Allocation", Springer, 2005,  Chapter 2.
 #'
@@ -10,6 +7,8 @@
 #'
 #' @author Xavier Valls \email{flamejat@@gmail.com}
 
+if ( !require( "latticeExtra" ) ) stop("latticeExtra package installation required for this script")
+
 ###################################################################################################################
 ### input parameters
 

Modified: pkg/Meucci/demo/S_CallsProjectionPricing.R
===================================================================
--- pkg/Meucci/demo/S_CallsProjectionPricing.R	2013-07-30 15:06:33 UTC (rev 2680)
+++ pkg/Meucci/demo/S_CallsProjectionPricing.R	2013-07-30 17:43:49 UTC (rev 2681)
@@ -1,6 +1,3 @@
-library(mvtnorm);
-library(pracma);
-
 #'This script projects the distribution of the market invariants for the derivatives market
 #'Then it computes the distribution of prices at the investment horizon  as described in A. Meucci,
 #'"Risk and Asset Allocation", Springer, 2005,  Chapter 3.
@@ -9,9 +6,8 @@
 #' \url{http://symmys.com/node/170}
 #' See Meucci's script for "S_CallsProjectionPricing.m"
 #'
-#' @author Xavier Valls \email{flamejat@@gmail.com}
+#' @author Xavier Valls \email{flamejat@@gmail.com} 
 
-
 ##################################################################################################################
 ### Load data
 

Modified: pkg/Meucci/demo/S_DisplayNormalCopulaCdf.R
===================================================================
--- pkg/Meucci/demo/S_DisplayNormalCopulaCdf.R	2013-07-30 15:06:33 UTC (rev 2680)
+++ pkg/Meucci/demo/S_DisplayNormalCopulaCdf.R	2013-07-30 17:43:49 UTC (rev 2681)
@@ -1,4 +1,3 @@
-library(mvtnorm);
 #'This script displays the cdf of the copula of a normal distribution, as described 
 #' in A. Meucci, "Risk and Asset Allocation", Springer, 2005,  Chapter 2.
 #'

Modified: pkg/Meucci/demo/S_EllipticalNDim.R
===================================================================
--- pkg/Meucci/demo/S_EllipticalNDim.R	2013-07-30 15:06:33 UTC (rev 2680)
+++ pkg/Meucci/demo/S_EllipticalNDim.R	2013-07-30 17:43:49 UTC (rev 2681)
@@ -1,4 +1,3 @@
-library(mvtnorm);
 #'This script decomposes the N-variate normal distribution into its radial and uniform components
 #' then it uses the uniform component to generate an elliptical distribution with location parameter 
 #' Mu and dispersion parameter Sigma, as described in A. Meucci, "Risk and Asset Allocation",

Added: pkg/Meucci/demo/S_ExtremeValueTheory.R
===================================================================
--- pkg/Meucci/demo/S_ExtremeValueTheory.R	                        (rev 0)
+++ pkg/Meucci/demo/S_ExtremeValueTheory.R	2013-07-30 17:43:49 UTC (rev 2681)
@@ -0,0 +1,55 @@
+#' This script computes the quantile (VaR) : 
+#' - analytically, under the Student t assumption for the market.
+#' - in simulations, using the sample quantile.
+#' - using the extreme value theory approximation
+#' Described in A. Meucci,"Risk and Asset Allocation",Springer, 2005,  Chapter 5.  
+#'
+#' @references
+#' \url{http://symmys.com/node/170}
+#' See Meucci's script for "S_ExtremeValueTheory.m"
+#
+#' @author Xavier Valls \email{flamejat@@gmail.com}
+
+if ( !require( "fExtremes" ) ) stop("fExtremes package installation required for this script")
+##################################################################################################################
+### Market parameters (student t distribution)
+m  = 1;
+s  = 2;
+nu = 7;
+nSim = 10000;
+
+th = 0.95; # EVT threshold
+c = seq( th , 0.999, 0.001 ); # confidence range for quantiles
+
+###################################################################################################################
+### Analytical
+Q_an = m + s * qt(1 - c, nu);
+
+###################################################################################################################
+### Simulations
+# generate objective's scenarios
+X = rt( nSim/2, nu);
+X = rbind( X, -X );  # symmetrize simulations
+Psi = m + s * X;
+Q_simul = quantile( Psi, (1 - c));
+
+###################################################################################################################
+### EVT approximation
+psi_hat = quantile(Psi, (1 - th));
+Excess = psi_hat - Psi[ Psi < psi_hat ];
+xi_v = gpdFit(Excess);
+xi   = xi_v at fit$par.ests[1];
+v    = xi_v at fit$par.ests[2];
+
+
+Fpsi_hat = 1 - th;
+Q_EVT = psi_hat + v / xi * ( 1 - ( ( 1 - c ) / Fpsi_hat ) ^ ( -xi ) );
+
+###################################################################################################################
+### Plots
+dev.new();
+plot(c, Q_an, type = "l", xlab = "confidence, c", ylab = "quantile based satisfaction, Q_c(\alpha)" );
+lines(c, Q_simul, col = "green" );
+lines(c, Q_EVT, col = "red" );
+legend( "bottomleft", 1.9, c( "exact", "simulations", "EVT" ), col = c( "black","green", "red" ),
+     lty=1, bg = "gray90" );

Modified: pkg/Meucci/demo/S_OrderStatisticsPdfLognormal.R
===================================================================
--- pkg/Meucci/demo/S_OrderStatisticsPdfLognormal.R	2013-07-30 15:06:33 UTC (rev 2680)
+++ pkg/Meucci/demo/S_OrderStatisticsPdfLognormal.R	2013-07-30 17:43:49 UTC (rev 2681)
@@ -10,6 +10,8 @@
 #' @author Xavier Valls \email{flamejat@@gmail.com}
 #' @export
 
+if ( !require( "scatterplot3d" ) ) stop("scatterplot3d package installation required for this script")
+
 #################################################################################################################
 ### Input  
 

Modified: pkg/Meucci/demo/S_OrderStatisticsPdfStudentT.R
===================================================================
--- pkg/Meucci/demo/S_OrderStatisticsPdfStudentT.R	2013-07-30 15:06:33 UTC (rev 2680)
+++ pkg/Meucci/demo/S_OrderStatisticsPdfStudentT.R	2013-07-30 17:43:49 UTC (rev 2681)
@@ -1,5 +1,3 @@
-library(scatterplot3d);
-
 #' This script script shows that the pdf of the r-th order statistics of a tudent t random variable,
 #' as described in A. Meucci, "Risk and Asset Allocation", Springer, 2005,  Chapter 2.
 #'
@@ -10,6 +8,8 @@
 #' @author Xavier Valls \email{flamejat@@gmail.com}
 #' @export
 
+if ( !require( "scatterplot3d" ) ) stop("scatterplot3d package installation required for this script")
+
 #################################################################################################################
 ### Input  
 mu = 0;

Modified: pkg/Meucci/demo/S_TStatApprox.R
===================================================================
--- pkg/Meucci/demo/S_TStatApprox.R	2013-07-30 15:06:33 UTC (rev 2680)
+++ pkg/Meucci/demo/S_TStatApprox.R	2013-07-30 17:43:49 UTC (rev 2681)
@@ -1,5 +1,3 @@
-library( mvtnorm );
-
 #' Simulate invariants for the regression model, as described in  A. Meucci, 
 #' "Risk and Asset Allocation", Springer, 2005, Chapter 4.
 #'

Modified: pkg/Meucci/demo/S_Wishart.R
===================================================================
--- pkg/Meucci/demo/S_Wishart.R	2013-07-30 15:06:33 UTC (rev 2680)
+++ pkg/Meucci/demo/S_Wishart.R	2013-07-30 17:43:49 UTC (rev 2681)
@@ -10,9 +10,7 @@
 #' @author Xavier Valls \email{flamejat@@gmail.com}
 #' @export
 
-
-library(scatterplot3d);
-
+if ( !require( "scatterplot3d" ) ) stop("scatterplot3d package installation required for this script")
 ###################################################################################################################
 ### Set inputs
 s  = c( 1, 1 ); # variances

Modified: pkg/Meucci/demo/S_WishartLocationDispersion.R
===================================================================
--- pkg/Meucci/demo/S_WishartLocationDispersion.R	2013-07-30 15:06:33 UTC (rev 2680)
+++ pkg/Meucci/demo/S_WishartLocationDispersion.R	2013-07-30 17:43:49 UTC (rev 2681)
@@ -1,6 +1,3 @@
-library(mvtnorm);
-library(psych);
-
 #' This script computes the location-dispersion ellipsoid of the normalized (unit variance, zero expectation)
 #' first diagonal and off-diagonal elements of a 2x2 Wishart distribution as a function of the inputs,
 #' as described in A. Meucci, "Risk and Asset Allocation", Springer, 2005,  Chapter 2.



More information about the Returnanalytics-commits mailing list