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

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Wed Jun 26 22:19:19 CEST 2013


Author: xavierv
Date: 2013-06-26 22:19:19 +0200 (Wed, 26 Jun 2013)
New Revision: 2450

Added:
   pkg/Meucci/R/PerformIidAnalysis.R
   pkg/Meucci/demo/S_DerivativesInvariants.R
   pkg/Meucci/man/PerformIidAnalysis.Rd
Modified:
   pkg/Meucci/DESCRIPTION
   pkg/Meucci/NAMESPACE
   pkg/Meucci/R/LognormalCopulaPdf.R
Log:
- added S_DerivativesInvariants demo file and PerformIidAnalysis function

Modified: pkg/Meucci/DESCRIPTION
===================================================================
--- pkg/Meucci/DESCRIPTION	2013-06-26 19:45:48 UTC (rev 2449)
+++ pkg/Meucci/DESCRIPTION	2013-06-26 20:19:19 UTC (rev 2450)
@@ -70,3 +70,4 @@
     'ConvertChangeInYield2Price.R'
     'ProjectionStudentT.R'
     'TwoDimEllipsoid.R'
+    'PerformIidAnalysis.R'

Modified: pkg/Meucci/NAMESPACE
===================================================================
--- pkg/Meucci/NAMESPACE	2013-06-26 19:45:48 UTC (rev 2449)
+++ pkg/Meucci/NAMESPACE	2013-06-26 20:19:19 UTC (rev 2450)
@@ -21,6 +21,7 @@
 export(normalizeProb)
 export(PanicCopula)
 export(PartialConfidencePosterior)
+export(PerformIidAnalysis)
 export(PlotDistributions)
 export(ProjectionStudentT)
 export(Raw2Central)

Modified: pkg/Meucci/R/LognormalCopulaPdf.R
===================================================================
--- pkg/Meucci/R/LognormalCopulaPdf.R	2013-06-26 19:45:48 UTC (rev 2449)
+++ pkg/Meucci/R/LognormalCopulaPdf.R	2013-06-26 20:19:19 UTC (rev 2450)
@@ -1,5 +1,3 @@
-library(pracma);
-
 #' Computes the pdf of the copula of the lognormal distribution at the generic point u in the unit hypercube,
 #' as described in  A. Meucci, "Risk and Asset Allocation", Springer, 2005.
 #'  

Added: pkg/Meucci/R/PerformIidAnalysis.R
===================================================================
--- pkg/Meucci/R/PerformIidAnalysis.R	                        (rev 0)
+++ pkg/Meucci/R/PerformIidAnalysis.R	2013-06-26 20:19:19 UTC (rev 2450)
@@ -0,0 +1,61 @@
+#' This function performs simple invariance (i.i.d.) tests on a time series, as described in
+#' A. Meucci "Risk and Asset Allocation", Springer, 2005
+#'
+#'  @param	Dates : [vector] (T x 1) dates
+#'	@param	Data  : [matrix] (T x N) data
+#'  @param	Starting_Prices : [vector] (N x 1) 
+#'  
+#'  @note it checks the evolution over time
+#   it checks that the variables are identically distributed by looking at the histogram of two subsamples
+#   it checks that the variables are independent by looking at the 1-lag scatter plot
+#     under i.i.d. the location-dispersion ellipsoid should be a circle
+#'
+#' @references
+#' \url{http://}
+#' See (6.77)-(6.79) in "Risk and Asset Allocation"-Springer (2005), by A. Meucci
+#' See Meucci's script for "ConvertCompoundedReturns2Price.m"
+#'
+#' @author Xavier Valls \email{flamejat@@gmail.com}
+#' @export
+
+PerformIidAnalysis = function( Dates = dim( Data, 1), Data, Str = "")
+{
+
+	##########################################################################################################
+	### Time series over time
+	dev.new(); 
+	plot( Dates, Data, main = Str ); 
+	#datetick( 'x', 'mmmyy', 'keeplimits', 'keepticks' );
+	
+
+	##########################################################################################################
+	### Test "identically distributed hypothesis": split observations into two sub-samples and plot histogram
+	Sample_1 = Data[ 1:round(length(Data) / 2) ];
+	Sample_2 = Data[ round(length(Data)/2) + 1: length(Data) ];
+	num_bins_1 = round(5 * log(length(Sample_1)));
+	num_bins_2 = round(5 * log(length(Sample_2)));
+	X_lim = c( min(Data) - .1 * (max(Data) - min(Data)), max(Data) + .1 * (max(Data) - min(Data)));
+
+	dev.new();
+
+	layout( matrix(c(1,1,2,2,0,3,3,0), 2, 4, byrow = TRUE), heights=c(1,1,1));
+	hist(Sample_1, num_bins_1, xlab = "", ylab = "", main = "first half" );
+	hist(Sample_2, num_bins_2, xlab = "", ylab = "", main = "second half" );
+	
+	##########################################################################################################
+	### Test "independently distributed hypothesis": scatter plot of observations at lagged times
+	
+
+	X = Data[ 1 : length(Data)-1 ];
+	Y = Data[ 2 : length(Data) ];
+	plot(X, Y, main="changes in implied vol");
+
+	m = cbind( apply( cbind( X, Y ), 2, mean ));
+	S = cov( cbind( X, Y ));
+	TwoDimEllipsoid( m, S, 2, FALSE, FALSE);
+	#axisLimits = axis;
+	#textX = axisLimits(1:2)*[-0.1,1.1]';
+	#textY = axisLimits(3:4)*[0.1,0.9]';
+	#text(textX, textY, Str);
+
+}
\ No newline at end of file

Added: pkg/Meucci/demo/S_DerivativesInvariants.R
===================================================================
--- pkg/Meucci/demo/S_DerivativesInvariants.R	                        (rev 0)
+++ pkg/Meucci/demo/S_DerivativesInvariants.R	2013-06-26 20:19:19 UTC (rev 2450)
@@ -0,0 +1,49 @@
+#' This script performs the quest for invariance in the derivatives market, as described 
+#' in A. Meucci,"Risk and Asset Allocation", Springer, 2005,  Chapter 3.
+#'
+#' @references
+#' \url{http://}
+#' See Meucci's script for "S_DerivativesInvariants.m"
+#'
+#' @author Xavier Valls \email{flamejat@@gmail.com}
+#' @export 
+
+##################################################################################################################
+### Load implied vol for options on SPX for different time to maturity and moneyness
+# Variable name: derivatives
+load('../data/derivatives.Rda');
+
+##################################################################################################################
+### Simple univariate test
+# select the implied vol time series for a specific time to maturity and moneyness 
+maturityIndex  = 1;   # 1..6
+moneynessIndex = 4;   # 1..7
+
+##################################################################################################################
+### Quest for invariance for changes in implied vol and changes in log implied vol
+
+#saving the sequence in a variable for legibility
+eachFiveRowsSeq = seq( 1 , length(derivatives$impVol[ , 1, 1 ]), 5 ); 
+
+X = diff( derivatives$impVol[ eachFiveRowsSeq , maturityIndex, moneynessIndex ] );
+PerformIidAnalysis( 1:length(X), X, 'Changes in implied vol');
+
+Y = diff(log(derivatives$impVol[ eachFiveRowsSeq , maturityIndex, moneynessIndex ]));
+PerformIidAnalysis( 1:size(Y,1), Y, 'Changes in log of implied vol' );
+
+##################################################################################################################
+### Multivariate test with AR(1) structure
+[T, Mat, Mon] 
+Dim = dim(derivatives$impVol[ eachFiveRowsSeq  , ,  ]);
+Z = matrix(log(derivatives$impVol[ eachFiveRowsSeq  , , ] ), Dim[ 1 ], Dim[ 2 ] * Dim[ 3 ]);
+# VAR(1) model by least square
+X = Z[ -1,  ];
+F = cbind(matrix( 1, Dim[ 1 ]-1, 1),  Z[ -length( Z[1, ] ) , ]);
+E_XF = t( X ) %*% F / Dim[ 1 ];
+E_FF = t( F ) %*% F / Dim[ 1 ];
+B = E_XF %*% (E_FF \ diag( 1,  ncol(size(E_FF) ) ) );
+Eps = X - F %*% t( B ); # residuals
+
+PerformIidAnalysis(1:size(Eps,1), Eps(:,3), 'VAR(1) residuals');
+
+### EOF
\ No newline at end of file

Added: pkg/Meucci/man/PerformIidAnalysis.Rd
===================================================================
--- pkg/Meucci/man/PerformIidAnalysis.Rd	                        (rev 0)
+++ pkg/Meucci/man/PerformIidAnalysis.Rd	2013-06-26 20:19:19 UTC (rev 2450)
@@ -0,0 +1,31 @@
+\name{PerformIidAnalysis}
+\alias{PerformIidAnalysis}
+\title{This function performs simple invariance (i.i.d.) tests on a time series, as described in
+A. Meucci "Risk and Asset Allocation", Springer, 2005}
+\usage{
+  PerformIidAnalysis(Dates = dim(Data, 1), Data, Str = "")
+}
+\arguments{
+  \item{Dates}{: [vector] (T x 1) dates}
+
+  \item{Data}{: [matrix] (T x N) data}
+
+  \item{Starting_Prices}{: [vector] (N x 1)}
+}
+\description{
+  This function performs simple invariance (i.i.d.) tests
+  on a time series, as described in A. Meucci "Risk and
+  Asset Allocation", Springer, 2005
+}
+\note{
+  it checks the evolution over time
+}
+\author{
+  Xavier Valls \email{flamejat at gmail.com}
+}
+\references{
+  \url{http://} See (6.77)-(6.79) in "Risk and Asset
+  Allocation"-Springer (2005), by A. Meucci See Meucci's
+  script for "ConvertCompoundedReturns2Price.m"
+}
+



More information about the Returnanalytics-commits mailing list