From bd141366ab3a9f5b6a179bd208cbe55274feb0b4 Mon Sep 17 00:00:00 2001 From: returnandrisk Date: Fri, 6 May 2016 01:06:38 +0800 Subject: [PATCH 1/2] Change plot title for the location-dispersion ellipsoid and plot shape to square for better visualization --- R/PerformIidAnalysis.R | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/R/PerformIidAnalysis.R b/R/PerformIidAnalysis.R index ce06a68..45fe2f5 100644 --- a/R/PerformIidAnalysis.R +++ b/R/PerformIidAnalysis.R @@ -53,7 +53,8 @@ PerformIidAnalysis = function( Dates = dim( Data)[1], Data, Str = "") X = Data[ 1 : length(Data)-1 ]; Y = Data[ 2 : length(Data) ]; - plot(X, Y, main="changes in implied vol"); + par(pty = "s"); + plot(X, Y, main = "Location-Dispersion Ellipsoid", asp = 1); m = cbind( apply( cbind( X, Y ), 2, mean )); S = cov( cbind( X, Y )); From f426e404378d6c88330910d31277012763614044 Mon Sep 17 00:00:00 2001 From: Return and Risk Date: Fri, 6 May 2016 01:14:23 +0800 Subject: [PATCH 2/2] Update PerformIidAnalysis.R --- R/PerformIidAnalysis.R | 126 ++++++++++++++++++++--------------------- 1 file changed, 63 insertions(+), 63 deletions(-) diff --git a/R/PerformIidAnalysis.R b/R/PerformIidAnalysis.R index 45fe2f5..467a13c 100644 --- a/R/PerformIidAnalysis.R +++ b/R/PerformIidAnalysis.R @@ -1,63 +1,63 @@ -#' @title Performs simple invariance (i.i.d.) tests on a time series. -#' -#' @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 -#' -#' @param Dates : [vector] (T x 1) dates -#' @param Data : [matrix] (T x N) data -#' @param Str : [string] title for the plot -#' -#' @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 -#' A. Meucci - "Exercises in Advanced Risk and Portfolio Management" \url{http://symmys.com/node/170}. -#' -#' See Meucci's script for "PerformIidAnalysis.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) ]; - par(pty = "s"); - plot(X, Y, main = "Location-Dispersion Ellipsoid", asp = 1); - - m = cbind( apply( cbind( X, Y ), 2, mean )); - S = cov( cbind( X, Y )); - TwoDimEllipsoid( m, S, 2, FALSE, FALSE); - -} \ No newline at end of file +#' @title Performs simple invariance (i.i.d.) tests on a time series. +#' +#' @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 +#' +#' @param Dates : [vector] (T x 1) dates +#' @param Data : [matrix] (T x N) data +#' @param Str : [string] title for the plot +#' +#' @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 +#' A. Meucci - "Exercises in Advanced Risk and Portfolio Management" \url{http://symmys.com/node/170}. +#' +#' See Meucci's script for "PerformIidAnalysis.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) ]; + par(pty = "s"); + plot(X, Y, main = "Location-Dispersion Ellipsoid", asp = 1); + + m = cbind( apply( cbind( X, Y ), 2, mean )); + S = cov( cbind( X, Y )); + TwoDimEllipsoid( m, S, 2, FALSE, FALSE); + +}