[Returnanalytics-commits] r2446 - in pkg/Meucci: . R demo man
noreply at r-forge.r-project.org
noreply at r-forge.r-project.org
Wed Jun 26 19:34:40 CEST 2013
Author: xavierv
Date: 2013-06-26 19:34:40 +0200 (Wed, 26 Jun 2013)
New Revision: 2446
Added:
pkg/Meucci/R/TwoDimEllipsoid.R
pkg/Meucci/demo/S_WishartLocationDispersion.R
pkg/Meucci/man/TwoDimEllipsoid.Rd
Modified:
pkg/Meucci/DESCRIPTION
pkg/Meucci/NAMESPACE
Log:
- added S_WishartLocationDispersion demo file and TwoDimEllipsoid plotting function
Modified: pkg/Meucci/DESCRIPTION
===================================================================
--- pkg/Meucci/DESCRIPTION 2013-06-26 16:59:14 UTC (rev 2445)
+++ pkg/Meucci/DESCRIPTION 2013-06-26 17:34:40 UTC (rev 2446)
@@ -69,3 +69,4 @@
'StudentTCopulaPdf.R'
'ConvertChangeInYield2Price.R'
'ProjectionStudentT.R'
+ 'TwoDimEllipsoid.R'
Modified: pkg/Meucci/NAMESPACE
===================================================================
--- pkg/Meucci/NAMESPACE 2013-06-26 16:59:14 UTC (rev 2445)
+++ pkg/Meucci/NAMESPACE 2013-06-26 17:34:40 UTC (rev 2446)
@@ -33,3 +33,4 @@
export(subIntervals)
export(SummStats)
export(Tweak)
+export(TwoDimEllipsoid)
Added: pkg/Meucci/R/TwoDimEllipsoid.R
===================================================================
--- pkg/Meucci/R/TwoDimEllipsoid.R (rev 0)
+++ pkg/Meucci/R/TwoDimEllipsoid.R 2013-06-26 17:34:40 UTC (rev 2446)
@@ -0,0 +1,98 @@
+#' 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.
+#'
+#' @param Location : [vector] (2 x 1) location vector (typically the expected value
+#' @param Square_Dispersion : [matrix] (2 x 2) scatter matrix Square_Dispersion (typically the covariance matrix)
+#' @param Scale : [scalar] a scalar Scale, that specifies the scale (radius) of the ellipsoid
+#' @param PlotEigVectors : [boolean] true then the eigenvectors (=principal axes) are plotted
+#' @param PlotSquare : [boolean] true then the enshrouding box is plotted. If Square_Dispersion is the covariance
+#'
+#' @return E : [figure handle]
+#'
+#' @references
+#' \url{http://}
+#' See Meucci's script for "TwoDimEllipsoid.m"
+#'
+#' @author Xavier Valls \email{flamejat@@gmail.com}
+#' @export
+
+
+TwoDimEllipsoid = function( Location, Square_Dispersion, Scale = 1, PlotEigVectors = FALSE, PlotSquare = FALSE )
+{
+
+ ##########################################################################################################
+ ### compute the ellipsoid in the r plane, solution to ((R-Location)' * Dispersion^-1 * (R-Location) ) = Scale^2
+
+ Eigen = eigen(Square_Dispersion);
+ Centered_Ellipse = c();
+ Angle = seq( 0, 2*pi, pi/500 );
+ NumSteps = length(Angle);
+
+ for( i in 1 : NumSteps )
+ {
+ # normalized variables (parametric representation of the ellipsoid)
+ y = c( cos( Angle[ i ] ), sin( Angle[ i ] ) );
+ Centered_Ellipse = c( Centered_Ellipse, Eigen$vectors %*% diag(sqrt(Eigen$values)) %*% y ); ##ok<AGROW>
+ }
+
+ R = Location %*% array( 1, NumSteps ) + Scale * Centered_Ellipse;
+
+ ##########################################################################################################
+ ### Plot the ellipsoid
+
+ E = lines( R[1, ], R[2, ], col = "red", lwd = 2 );
+
+ ##########################################################################################################
+ ### Plot a rectangle centered in Location with semisides of lengths Dispersion[ 1] and Dispersion[ 2 ], respectively
+
+ if( PlotSquare )
+ {
+ Dispersion = sqrt( diag( Square_Dispersion ) );
+ Vertex_LowRight_A = Location[ 1 ] + Scale * Dispersion[ 1 ];
+ Vertex_LowRight_B = Location[ 2 ] - Scale * Dispersion[ 2 ];
+ Vertex_LowLeft_A = Location[ 1 ] - Scale * Dispersion[ 1 ];
+ Vertex_LowLeft_B = Location[ 2 ] - Scale * Dispersion[ 2 ];
+ Vertex_UpRight_A = Location[ 1 ] + Scale * Dispersion[ 1 ];
+ Vertex_UpRight_B = Location[ 2 ] + Scale * Dispersion[ 2 ];
+ Vertex_UpLeft_A = Location[ 1 ] - Scale * Dispersion[ 1 ];
+ Vertex_UpLeft_B = Location[ 2 ] + Scale * Dispersion[ 2 ];
+
+ Square = rbind( c( Vertex_LowRight_A, Vertex_LowRight_B ),
+ c( Vertex_LowLeft_A, Vertex_LowLeft_B ),
+ c( Vertex_UpLeft_A, Vertex_UpLeft_B ),
+ c( Vertex_UpRight_A, Vertex_UpRight_B ),
+ c( Vertex_LowRight_A, Vertex_LowRight_B ) );
+
+ h = lines(Square[ , 1 ], Square[ , 2 ], col = "red", lwd = 2 );
+
+ }
+
+ ##########################################################################################################
+ ### Plot eigenvectors in the r plane (centered in Location) of length the square root of the eigenvalues (rescaled)
+ if( PlotEigVectors )
+ {
+ L_1 = Scale * sqrt( Eigen$values[ 1 ] );
+ L_2 = Scale * sqrt( Eigen$values[ 2 ] );
+
+ # deal with reflection: matlab chooses the wrong one
+ Sign = sign( Eigen$vectors[ 1, 1 ] );
+
+ # eigenvector 1
+ Start_A = Location[ 1 ];
+ End_A = Location[ 1 ] + Sign * (Eigen$vectors[ 1, 1 ]) * L_1;
+ Start_B = Location[ 2 ];
+ End_B = Location[ 2 ] + Sign * (Eigen$vectors[ 1, 2 ]) * L_1;
+
+ h = lines( c( Start_A, End_A ), c( Start_B, End_B ), col = "red", lwd = 2 );
+
+ # eigenvector 2
+ Start_A = Location[ 1 ];
+ End_A = Location[ 1 ] + ( Eigen$vectors[ 2, 1 ] * L_2);
+ Start_B = Location[ 2 ];
+ End_B = Location[ 2 ] + ( Eigen$vectors[ 2, 2 ] * L_2);
+
+ h = lines( c( Start_A, End_A ), c( Start_B, End_B ), col = "red", lwd = 2 );
+
+ }
+}
\ No newline at end of file
Added: pkg/Meucci/demo/S_WishartLocationDispersion.R
===================================================================
--- pkg/Meucci/demo/S_WishartLocationDispersion.R (rev 0)
+++ pkg/Meucci/demo/S_WishartLocationDispersion.R 2013-06-26 17:34:40 UTC (rev 2446)
@@ -0,0 +1,87 @@
+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.
+#'
+#' @references
+#' \url{http://}
+#' See Meucci's script for "S_WishartCorrelation.m"
+#'
+#' @author Xavier Valls \email{flamejat@@gmail.com}
+#' @export
+
+###################################################################################################################
+### Set input parameters
+s = c( 1, 1 ); # variances
+r = -0.9; # correlation
+Sigma = diag( s ) %*% rbind( c( 1, r ), c( r, 1 ) ) %*% diag( s );
+nu = 5; # degrees of freedom
+nSim = 10000;
+
+###################################################################################################################
+### Set input parameters
+
+W_xx = matrix( NaN, nSim, 1 );
+W_yy = matrix( NaN, nSim, 1 );
+W_xy = matrix( NaN, nSim, 1 );
+Vec_W = matrix( NaN, nSim, 4 );
+Dets = matrix( NaN, nSim, 1 );
+Traces = matrix( NaN, nSim, 1 );
+
+
+for( j in 1 : nSim )
+{
+ X = rmvnorm( nu, matrix( 0, 2, 1 ), Sigma);
+ W = t( X ) %*% X;
+
+ Dets[ j ] = det( W );
+ Traces[ j ] = tr( W );
+
+ W_xx[ j ] = W[ 1, 1 ];
+ W_yy[ j ] = W[ 2, 2 ];
+ W_xy[ j ] = W[ 1, 2 ];
+
+ Vec_W [ j, ] = as.vector( W );
+}
+
+# compute expected values of W_xx and W_xy, see (2.227) in "Risk and Asset Allocation - Springer
+E_xx = nu * Sigma[ 1, 1 ];
+E_xy = nu * Sigma[ 1, 2 ];
+
+# compute covariance matrix of W_xx and W_xy, see (2.228) in "Risk and Asset Allocation - Springer
+m = 1;
+n = 1;
+p = 1;
+q = 1;
+var_Wxx = nu * ( Sigma[ m, p ] * Sigma[ n, q ] + Sigma[ m, q ] * Sigma[ n, p ] );
+m = 1;
+n = 2;
+p = 1;
+q = 2;
+var_Wxy = nu * ( Sigma[ m, p ] * Sigma[ n, q ] + Sigma[ m, q ] * Sigma[ n, p ] );
+m = 1;
+n = 1;
+p = 1;
+q = 2;
+cov_Wxx_Wxy = nu * ( Sigma[ m, p ] * Sigma[ n, q ] + Sigma[ m, q ] * Sigma[ n, p ] );
+
+S_xx_xy = rbind( cbind( var_Wxx, cov_Wxx_Wxy ), cbind( cov_Wxx_Wxy, var_Wxy ));
+
+# compute X_1 and X_2, i.e. normalized version of W_xx and W_xy
+X_1 = ( W_xx - E_xx ) / sqrt( var_Wxx );
+X_2 = ( W_xy - E_xy ) / sqrt( var_Wxy );
+X = cbind( X_1, X_2 );
+
+# compute expected value and covariance of X_1 and X_2
+E = rbind( 0, 0 );
+E_hat = t( apply( X, 2, mean) );
+
+S = diag( 1 / c( sqrt( var_Wxx ), sqrt( var_Wxy ))) %*% S_xx_xy %*% diag( 1 / c( sqrt( var_Wxx ), sqrt( var_Wxy )));
+S_hat = cov( X );
+
+figure();
+plot( X_1, X_2, xlab = "X_1", ylab = "X_2");
+
+TwoDimEllipsoid(E, S, 1, TRUE, FALSE);
Added: pkg/Meucci/man/TwoDimEllipsoid.Rd
===================================================================
--- pkg/Meucci/man/TwoDimEllipsoid.Rd (rev 0)
+++ pkg/Meucci/man/TwoDimEllipsoid.Rd 2013-06-26 17:34:40 UTC (rev 2446)
@@ -0,0 +1,44 @@
+\name{TwoDimEllipsoid}
+\alias{TwoDimEllipsoid}
+\title{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.}
+\usage{
+ TwoDimEllipsoid(Location, Square_Dispersion, Scale = 1,
+ PlotEigVectors = FALSE, PlotSquare = FALSE)
+}
+\arguments{
+ \item{Location}{: [vector] (2 x 1) location vector
+ (typically the expected value}
+
+ \item{Square_Dispersion}{: [matrix] (2 x 2) scatter
+ matrix Square_Dispersion (typically the covariance
+ matrix)}
+
+ \item{Scale}{: [scalar] a scalar Scale, that specifies
+ the scale (radius) of the ellipsoid}
+
+ \item{PlotEigVectors}{: [boolean] true then the
+ eigenvectors (=principal axes) are plotted}
+
+ \item{PlotSquare}{: [boolean] true then the enshrouding
+ box is plotted. If Square_Dispersion is the covariance}
+}
+\value{
+ E : [figure handle]
+}
+\description{
+ 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.
+}
+\author{
+ Xavier Valls \email{flamejat at gmail.com}
+}
+\references{
+ \url{http://} See Meucci's script for "TwoDimEllipsoid.m"
+}
+
More information about the Returnanalytics-commits
mailing list