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

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Tue Aug 20 12:55:02 CEST 2013


Author: xavierv
Date: 2013-08-20 12:55:02 +0200 (Tue, 20 Aug 2013)
New Revision: 2832

Added:
   pkg/Meucci/R/ FitOrnsteinUhlenbeck.R
   pkg/Meucci/demo/S_StatArbSwaps.R
   pkg/Meucci/man/FitOrnsteinUhlenbeck.Rd
Modified:
   pkg/Meucci/DESCRIPTION
   pkg/Meucci/NAMESPACE
   pkg/Meucci/data/swapParRates.Rda
Log:
- added S_StatArbSwaps demo script from chapter 3 and its associated data and functions

Modified: pkg/Meucci/DESCRIPTION
===================================================================
--- pkg/Meucci/DESCRIPTION	2013-08-20 04:18:38 UTC (rev 2831)
+++ pkg/Meucci/DESCRIPTION	2013-08-20 10:55:02 UTC (rev 2832)
@@ -87,3 +87,5 @@
     'FitMultivariateGarch.R'
     'MvnRnd.R'
     'MleRecursionForStudentT.R'
+    '
+    FitOrnsteinUhlenbeck.R'

Modified: pkg/Meucci/NAMESPACE
===================================================================
--- pkg/Meucci/NAMESPACE	2013-08-20 04:18:38 UTC (rev 2831)
+++ pkg/Meucci/NAMESPACE	2013-08-20 10:55:02 UTC (rev 2832)
@@ -12,6 +12,7 @@
 export(EntropyProg)
 export(FitExpectationMaximization)
 export(FitMultivariateGarch)
+export(FitOrnsteinUhlenbeck)
 export(garch1f4)
 export(garch2f8)
 export(GenerateLogNormalDistribution)

Added: pkg/Meucci/R/ FitOrnsteinUhlenbeck.R
===================================================================
--- pkg/Meucci/R/ FitOrnsteinUhlenbeck.R	                        (rev 0)
+++ pkg/Meucci/R/ FitOrnsteinUhlenbeck.R	2013-08-20 10:55:02 UTC (rev 2832)
@@ -0,0 +1,55 @@
+#' Fit a multivariate OU process at estimation step tau, as described in  A. Meucci 
+#' "Risk and Asset Allocation", Springer, 2005
+#'
+#'  @param  Y   : [matrix] (T x N)
+#'  @param  tau : [scalar] time step
+#'  
+#'  @return Mu  : [vector] long-term means
+#'  @return Th  : [matrix] whose eigenvalues have positive real part / mean reversion speed
+#'  @return Sig : [matrix] Sig = S * S', covariance matrix of Brownian motions
+#'
+#' @note
+#'	o dY_t = -Th * (Y_t - Mu) * dt + S * dB_t where
+#'	o dB_t: vector of Brownian motions
+#'
+#' @references
+#' \url{http://symmys.com/node/170}
+#' See Meucci's script for "EfficientFrontierReturns.m"
+#'
+#' @author Xavier Valls \email{flamejat@@gmail.com}
+#' @export
+
+FitOrnsteinUhlenbeck = function( Y, tau )
+{
+	T = nrow(Y);
+	N = ncol(Y);
+
+	X    = Y[ -1,  ];
+	F    = cbind( matrix( 1, T-1, 1 ), Y[ -nrow(Y), ] );
+	E_XF = t(X) %*% F / T;
+	E_FF = t(F) %*% F / T;
+	B    = E_XF %*% solve( E_FF );
+	if( length( B[ , -1 ] ) != 1 )
+	{
+		Th = -logm( B[ , -1 ] ) / tau;
+
+	}else
+	{
+		Th = -log( B[ , -1 ] ) / tau;
+	}
+
+	Mu = solve( diag( 1, N ) - B[ , -1 ] ) %*%  B[ , 1 ] ;
+
+	U  = F %*% t(B) - X;
+	
+	Sig_tau = cov(U);
+
+	N = length(Mu);
+	TsT = kron( Th, diag( 1, N ) ) + kron( diag( 1, N ), Th );
+
+	VecSig_tau = matrix(Sig_tau, N^2, 1);
+	VecSig = ( solve( diag( 1, N^2 ) - expm( -TsT * tau ) ) %*% TsT ) %*% VecSig_tau;
+	Sig = matrix( VecSig, N, N );
+
+	return( list( Mu = Mu, Theta = Th, Sigma = Sig ) )
+}
\ No newline at end of file

Modified: pkg/Meucci/data/swapParRates.Rda
===================================================================
(Binary files differ)

Added: pkg/Meucci/demo/S_StatArbSwaps.R
===================================================================
--- pkg/Meucci/demo/S_StatArbSwaps.R	                        (rev 0)
+++ pkg/Meucci/demo/S_StatArbSwaps.R	2013-08-20 10:55:02 UTC (rev 2832)
@@ -0,0 +1,69 @@
+#' This script search for cointegrated stat-arb strategies among swap contracts, 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_StatArbSwaps.m"
+#'
+#' @author Xavier Valls \email{flamejat@@gmail.com}
+
+# TODO: Check the loadings of the principal components analysis, fix the date ticks on the plots.
+
+##################################################################################################################
+### Load data
+load("../data/swapParRates.Rda");
+
+##################################################################################################################
+### Estimate covariance and PCA decomposition
+S   = cov( swapParRates$Rates );
+PC  = princomp( covmat=S );
+E   = PC$loadings
+Lam = ( PC$sdev )^2
+##################################################################################################################
+### Set up dates ticks
+dev.new(); 
+h = plot(swapParRates$Dates, swapParRates$Dates); 
+XTick = NULL;
+years = as.numeric(format(swapParRates$Dates[1],"%Y")) : as.numeric(format(swapParRates$Dates[length(swapParRates$Dates)],"%Y"))
+
+
+for( n in years )
+{
+    XTick = cbind( XTick, datenum(n,1,1) ); ##ok<AGROW>
+}
+
+a = min(swapParRates$Dates); 
+b = max(swapParRates$Dates); 
+X_Lim = cbind( a - 0.01 * ( b-a ),  b + 0.01 * ( b - a ) );
+
+##################################################################################################################
+### Plots
+nLam = length(Lam);
+Thetas = matrix( NaN, nLam, 1);
+for( n in 1 : nLam )
+{
+    Y = swapParRates$Rates %*% E[ , n ] * 10000;
+    FOU = FitOrnsteinUhlenbeck(Y, 1/252);
+    Sd_Y = sqrt( FOU$Sigma / (2 * FOU$Theta));
+    Thetas[n] = FOU$Theta;
+    
+    dev.new();
+    current_line = array( Y[ length(Y) ], length(swapParRates$Dates ) );
+    Mu_line = array( FOU$Mu, length(swapParRates$Dates) );
+    Z_line_up = Mu_line + Sd_Y[1];
+    Z_line_dn = Mu_line - Sd_Y[1];
+        
+    plot( swapParRates$Dates , Y, "l", xlab = "year", ylab = "basis points", main = paste( "eigendirection n. ", n, ", theta = ", FOU$Theta ) );
+    lines( swapParRates$Dates, Mu_line, col = "blue" );
+    lines( swapParRates$Dates, Z_line_up, col = "red" );
+    lines( swapParRates$Dates, Z_line_dn, col = "red" );
+    lines( swapParRates$Dates, current_line, col = "green");
+    
+    #set(gca(), 'xlim', X_Lim, 'XTick', XTick);
+    #datetick('x','yy','keeplimits','keepticks');
+    #grid off;
+    #title(['eigendirection n. ' num2str(n) ',    theta = ' num2str(Theta)],'FontWeight','bold');
+}
+
+dev.new();
+plot( 1:length( Lam ), Thetas, "l", xlab = " eigendirection n.", ylab = "theta" );

Added: pkg/Meucci/man/FitOrnsteinUhlenbeck.Rd
===================================================================
--- pkg/Meucci/man/FitOrnsteinUhlenbeck.Rd	                        (rev 0)
+++ pkg/Meucci/man/FitOrnsteinUhlenbeck.Rd	2013-08-20 10:55:02 UTC (rev 2832)
@@ -0,0 +1,38 @@
+\name{FitOrnsteinUhlenbeck}
+\alias{FitOrnsteinUhlenbeck}
+\title{Fit a multivariate OU process at estimation step tau, as described in  A. Meucci
+"Risk and Asset Allocation", Springer, 2005}
+\usage{
+  FitOrnsteinUhlenbeck(Y, tau)
+}
+\arguments{
+  \item{Y}{: [matrix] (T x N)}
+
+  \item{tau}{: [scalar] time step}
+}
+\value{
+  Mu : [vector] long-term means
+
+  Th : [matrix] whose eigenvalues have positive real part /
+  mean reversion speed
+
+  Sig : [matrix] Sig = S * S', covariance matrix of
+  Brownian motions
+}
+\description{
+  Fit a multivariate OU process at estimation step tau, as
+  described in A. Meucci "Risk and Asset Allocation",
+  Springer, 2005
+}
+\note{
+  o dY_t = -Th * (Y_t - Mu) * dt + S * dB_t where o dB_t:
+  vector of Brownian motions
+}
+\author{
+  Xavier Valls \email{flamejat at gmail.com}
+}
+\references{
+  \url{http://symmys.com/node/170} See Meucci's script for
+  "EfficientFrontierReturns.m"
+}
+



More information about the Returnanalytics-commits mailing list