[Returnanalytics-commits] r2237 - pkg/PerformanceAnalytics/sandbox/Meucci/R

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Fri Aug 17 00:06:08 CEST 2012


Author: mkshah
Date: 2012-08-17 00:06:07 +0200 (Fri, 17 Aug 2012)
New Revision: 2237

Added:
   pkg/PerformanceAnalytics/sandbox/Meucci/R/TheoryMultivariateOUnCointegration.R
Modified:
   pkg/PerformanceAnalytics/sandbox/Meucci/R/EmpiricalMultivariateOUnCointegration.R
Log:
Adding functions for theoretical Multivariate OU and correcting functions for empirical Multivariate OU

Modified: pkg/PerformanceAnalytics/sandbox/Meucci/R/EmpiricalMultivariateOUnCointegration.R
===================================================================
--- pkg/PerformanceAnalytics/sandbox/Meucci/R/EmpiricalMultivariateOUnCointegration.R	2012-08-16 21:32:08 UTC (rev 2236)
+++ pkg/PerformanceAnalytics/sandbox/Meucci/R/EmpiricalMultivariateOUnCointegration.R	2012-08-16 22:06:07 UTC (rev 2237)
@@ -23,6 +23,8 @@
   VecSig = solve( diag( N^2 ) - expm( Matrix( -TsT * tau ) ) ) %*% TsT %*% VecSig_tau
   Sig = VecSig
   dim( Sig ) = c( N , N )
+  
+  return( list( Mu = Mu, Th = Th, Sig = Sig ) )
 }
 
 OUstep = function( X_0 , t , Mu , Th , Sig )
@@ -53,6 +55,8 @@
                      
   X_t = Mu_t + Eps
   Mu_t = t( colMeans( Mu_t ) )
+  
+  return( list( X_t = X_t, Mu_t = Mu_t, Sig_t = Sig_t ) )
 }
 
 ProjectOU = function( x_0 , t , Mu , Th , Sig )

Added: pkg/PerformanceAnalytics/sandbox/Meucci/R/TheoryMultivariateOUnCointegration.R
===================================================================
--- pkg/PerformanceAnalytics/sandbox/Meucci/R/TheoryMultivariateOUnCointegration.R	                        (rev 0)
+++ pkg/PerformanceAnalytics/sandbox/Meucci/R/TheoryMultivariateOUnCointegration.R	2012-08-16 22:06:07 UTC (rev 2237)
@@ -0,0 +1,54 @@
+OUstep = function( X_0 , t , Mu , Th , Sig )
+{
+  NumSimul = nrow( X_0 )
+  N = ncol( X_0 )
+  
+  # location
+  ExpM = expm( Matrix( -Th %*% t ) )
+  
+  # repmat = function(X,m,n) - R equivalent of repmat (matlab)
+  X = t( Mu - ExpM %*% Mu )
+  mx = dim( X )[1]
+  nx = dim( X )[2]
+  Mu_t = matrix( t ( matrix( X , mx , nx*1 ) ), mx * NumSimul, nx * 1, byrow = T ) + t( X_0 %*% ExpM )
+  
+  # scatter
+  TsT = kronecker( Th , diag( N ) ) + kronecker( diag( N ) , Th )
+  
+  VecSig = Sig
+  dim( VecSig ) = c( N^2 , 1 )
+  VecSig_t = solve( TsT ) %*% ( diag( N^2 ) - expm( Matrix( -TsT %*% t ) ) ) %*% VecSig
+  Sig_t = VecSig_t
+  dim( Sig_t ) = c( N , N )
+  Sig_t = ( Sig_t + t( Sig_t ) ) / 2
+  
+  Eps = mvrnorm( NumSimul, rep( 0 , N ), Sig_t )
+  
+  X_t = Mu_t + Eps
+  Mu_t = t( colMeans( Mu_t ) )
+  
+  return( list( X_t = X_t, Mu_t = Mu_t, Sig_t = Sig_t ) )
+}
+
+OUstepEuler = function( X_0 , Dt , Mu , Th , Sig )
+{
+  NumSimul = nrow( X_0 )
+  N = ncol( X_0 )
+
+  # location
+  ExpM = expm( Matrix( -Th %*% Dt ) )
+
+  # repmat = function(X,m,n) - R equivalent of repmat (matlab)
+  X = t( Mu - ExpM %*% Mu )
+  mx = dim( X )[1]
+  nx = dim( X )[2]
+  Mu_t = matrix( t ( matrix( X , mx , nx*1 ) ), mx * NumSimul, nx * 1, byrow = T ) + t( X_0 %*% ExpM )
+              
+  # scatter
+  Sig_t = Sig %*% Dt
+  Eps = mvrnorm( NumSimul / 2, rep( 0 , N ) , Sig_t )
+  Eps = rbind( Eps, -Eps)
+              
+  X_t = Mu_t + Eps
+  Mu_t = t( colMeans( X_t ) )
+}
\ No newline at end of file



More information about the Returnanalytics-commits mailing list