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

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Tue Jul 2 13:33:19 CEST 2013


Author: xavierv
Date: 2013-07-02 13:33:19 +0200 (Tue, 02 Jul 2013)
New Revision: 2487

Added:
   pkg/Meucci/R/SimulateJumpDiffusionMerton.R
   pkg/Meucci/data/bondAttribution.Rda
   pkg/Meucci/data/fixedIncome.Rda
   pkg/Meucci/data/linearModel.Rda
   pkg/Meucci/data/swap2y4y.Rda
   pkg/Meucci/data/swapParRates.Rda
   pkg/Meucci/data/swaps.Rda
   pkg/Meucci/demo/S_FixedIncomeInvariants.R
   pkg/Meucci/demo/S_HorizonEffect.R
   pkg/Meucci/demo/S_JumpDiffusionMerton.R
   pkg/Meucci/man/SimulateJumpDiffusionMerton.Rd
Modified:
   pkg/Meucci/DESCRIPTION
   pkg/Meucci/NAMESPACE
Log:
-added three more demo files, some ported datafiles and a function

Modified: pkg/Meucci/DESCRIPTION
===================================================================
--- pkg/Meucci/DESCRIPTION	2013-07-02 04:07:16 UTC (rev 2486)
+++ pkg/Meucci/DESCRIPTION	2013-07-02 11:33:19 UTC (rev 2487)
@@ -72,3 +72,4 @@
     'ProjectionStudentT.R'
     'TwoDimEllipsoid.R'
     'PerformIidAnalysis.R'
+    'SimulateJumpDiffusionMerton.R'

Modified: pkg/Meucci/NAMESPACE
===================================================================
--- pkg/Meucci/NAMESPACE	2013-07-02 04:07:16 UTC (rev 2486)
+++ pkg/Meucci/NAMESPACE	2013-07-02 11:33:19 UTC (rev 2487)
@@ -29,6 +29,7 @@
 export(RejectOutlier)
 export(RIEfficientFrontier)
 export(robustBayesianPortfolioOptimization)
+export(SimulateJumpDiffusionMerton)
 export(std)
 export(StudentTCopulaPdf)
 export(subIntervals)

Added: pkg/Meucci/R/SimulateJumpDiffusionMerton.R
===================================================================
--- pkg/Meucci/R/SimulateJumpDiffusionMerton.R	                        (rev 0)
+++ pkg/Meucci/R/SimulateJumpDiffusionMerton.R	2013-07-02 11:33:19 UTC (rev 2487)
@@ -0,0 +1,70 @@
+
+#' This function simulates a jump diffusion process, as described in A. Meucci "Risk and Asset Allocation",
+#' Springer, 2005
+#'
+#'  @param  m  : [scalar] deterministic drift of diffusion
+#'  @param  s  : [scalar] standard deviation of diffusion
+#'  @param  l  : [scalar] Poisson process arrival rate
+#'  @param  a  : [scalar] drift of log-jump
+#'  @param  D  : [scalar] st.dev of log-jump
+#'  @param  ts : [vector] time steps
+#'  @param  J  : [scalar] number of simulations
+#'
+#'  @return  X  : [matrix] (J x length(ts)) of simulations
+#'
+#' @references
+#' \url{http://symmys.com/node/170}
+#' See Meucci's script for "SimulateJumpDiffusionMerton.m"
+#'
+#' @author Xavier Valls \email{flamejat@@gmail.com}
+#' @export
+
+SimulateJumpDiffusionMerton = function( m, s, l, a, D, ts, J )
+{
+    L = length(ts);
+    T = ts[ L ];
+
+    # simulate number of jumps; 
+    N = rpois( J, l * T );
+
+    Jumps = matrix( 0, J, L );
+    for( j in 1 : J )
+    {
+        # simulate jump arrival time
+        t = T * rnorm(N[ j ]);
+        t = sort(t);
+
+        # simulate jump size
+        S = a + D * rnorm(N[ j ]);
+        
+        # put things together
+        CumS = cumsum(S);
+        Jumps_ts = matrix( 0, 1, L);
+        for( n in 1 : L )
+        {
+            Events = sum( t <= ts[ n ]);
+            if( Events )
+            {
+                Jumps_ts[ n ] = CumS[ Events ];
+            }
+        }
+
+        Jumps[ j, ] = Jumps_ts;
+    }
+
+    D_Diff = matrix( NaN, J, L );
+    for( l in 1 : L )
+    {
+        Dt = ts[ l ];
+        if( l > 1 )
+        {
+            Dt = ts[ l ] - ts[ l - 1 ];
+        }
+
+        D_Diff[ , l ] = m * Dt + s * sqrt(Dt) * rnorm(J); 
+    }
+
+    X = cbind( matrix(0, J, 1), apply(D_Diff, 2, cumsum) + Jumps );
+
+    return( X );
+}
\ No newline at end of file

Added: pkg/Meucci/data/bondAttribution.Rda
===================================================================
(Binary files differ)


Property changes on: pkg/Meucci/data/bondAttribution.Rda
___________________________________________________________________
Added: svn:mime-type
   + application/octet-stream

Added: pkg/Meucci/data/fixedIncome.Rda
===================================================================
(Binary files differ)


Property changes on: pkg/Meucci/data/fixedIncome.Rda
___________________________________________________________________
Added: svn:mime-type
   + application/octet-stream

Added: pkg/Meucci/data/linearModel.Rda
===================================================================
(Binary files differ)


Property changes on: pkg/Meucci/data/linearModel.Rda
___________________________________________________________________
Added: svn:mime-type
   + application/octet-stream

Added: pkg/Meucci/data/swap2y4y.Rda
===================================================================
(Binary files differ)


Property changes on: pkg/Meucci/data/swap2y4y.Rda
___________________________________________________________________
Added: svn:mime-type
   + application/octet-stream

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


Property changes on: pkg/Meucci/data/swapParRates.Rda
___________________________________________________________________
Added: svn:mime-type
   + application/octet-stream

Added: pkg/Meucci/data/swaps.Rda
===================================================================
(Binary files differ)


Property changes on: pkg/Meucci/data/swaps.Rda
___________________________________________________________________
Added: svn:mime-type
   + application/octet-stream

Added: pkg/Meucci/demo/S_FixedIncomeInvariants.R
===================================================================
--- pkg/Meucci/demo/S_FixedIncomeInvariants.R	                        (rev 0)
+++ pkg/Meucci/demo/S_FixedIncomeInvariants.R	2013-07-02 11:33:19 UTC (rev 2487)
@@ -0,0 +1,29 @@
+#' This file performs the quest for invariance in the fixed income market, 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_FixedIncomeInvariants.m"
+#'
+#' @author Xavier Valls \email{flamejat@@gmail.com}
+
+##################################################################################################################
+### Load government yield curve and bond yield data for different dates
+load("../data/fixedIncome.Rda");
+
+##################################################################################################################
+### Pick time-to-maturity for one point on the yield curve
+ycMaturityIndex = 4; # 1..6
+
+# select the yield time series for a constant time-to-maturity
+yield = fixedIncome$ycYieldPercent[ , ycMaturityIndex ]; 
+
+##################################################################################################################
+### Quest for invariance
+# changes in the yield curve
+X = yield[ -1 ] - yield[ -length( yield ) ];
+PerformIidAnalysis( 1:length( X ), X, "Changes in yield curve" );
+
+# changes in the logarithm of the yield curve
+Y = log( yield[ -1 ] ) - log( yield[ -length( yield ) ] );
+PerformIidAnalysis( 1 : length( Y ), Y, "Changes in log of yield curve" );

Added: pkg/Meucci/demo/S_HorizonEffect.R
===================================================================
--- pkg/Meucci/demo/S_HorizonEffect.R	                        (rev 0)
+++ pkg/Meucci/demo/S_HorizonEffect.R	2013-07-02 11:33:19 UTC (rev 2487)
@@ -0,0 +1,98 @@
+
+#'This script studies horizon effect on explicit factors / implicit loadings linear model, as described in 
+#'A. Meucci, "Risk and Asset Allocation", Springer, 2005,  Chapter 3.
+#'Compounded returns follow the linear model X = tau*muX + D*F + epsilon, where 
+#' tau: investment horizon (in weeks)
+#' muX: expected weekly compounded returns
+#' F: factor compounded returns, with zero expectation and tau-proportional covariance
+#' D: matrix of factor loadings
+#' epsilon: uncorrelated (idiosyncratic) shocks.
+#' R = exp(X)-1 and Z = exp(F)-1 are the linear returns
+#'
+#' @references
+#' \url{http://symmys.com/node/170}
+#' See Meucci's script for "S_HorizonEffect.m"
+#'
+#' @author Xavier Valls \email{flamejat@@gmail.com}
+
+##################################################################################################################
+# Load parameters of the model: D, muX, sigmaF, sigmaEps
+load( "../data/DB_LinearModel.mat" );
+
+# Specify range of investment horizon, weeks
+tauRangeWeeks = 1:52;
+
+# constants
+
+N = nrow(linearModel$D);
+K = ncol(linearModel$D);
+
+ntauRangeWeeks = length(tauRangeWeeks);
+aMinusTauMuX = matrix(0, ntauRangeWeeks);
+normDminusB  = matrix(0, ntauRangeWeeks);
+minCorrU     = matrix(0, ntauRangeWeeks);
+meanCorrU    = matrix(0, ntauRangeWeeks);
+maxCorrU     = matrix(0, ntauRangeWeeks);
+
+for( i in 1 : ntauRangeWeeks )
+{
+    tau = tauRangeWeeks[ i ];
+    
+    # statitics of linear returns (analytically)
+    # let Y = [1+R; 1+Z] ~ LogN(mu*tau, covJointXF*tau)
+    mu = rbind(linearModel$muX, matrix(0, K));
+    # covariance of [X; F] for tau=1:
+    covJointXF = rbind(
+        cbind( linearModel$D %*% linearModel$sigmaF %*% t(linearModel$D) + linearModel$sigmaEps,
+         linearModel$D %*% linearModel$sigmaF ),
+        cbind( linearModel$sigmaF %*% t( linearModel$D ), linearModel$sigmaF)
+        );
+    E_Y  = exp( mu * tau + diag( covJointXF * tau ) / 2 );
+    E_YY = (E_Y %*% t( E_Y )) * exp( covJointXF * tau );
+    E_R  = E_Y[ 1 : N ] - matrix( 1, N );
+    E_Z  = E_Y[ -(1:N)] - matrix( 1, K);
+    E_RR = E_YY[ 1:N , 1:N ] - matrix( 1, N ) %*% t(E_R) - E_R %*% matrix( 1, 1, N ) - matrix( 1, N, N );
+    E_ZZ = E_YY[-(1:N), -(1:N) ] - matrix( 1, K ) %*% t(E_Z) - E_Z %*% matrix( 1, 1, K ) - matrix( 1, K, K );
+    E_RZ = E_YY[ 1:N, -(1:N) ] - matrix( 1, N ) %*% t(E_Z) - E_R %*% matrix( 1, 1, K) - matrix( 1, N, K );
+    SigmaZ  = E_ZZ - E_Z %*% t(E_Z);
+    SigmaR  = E_RR - E_R %*% t(E_R);
+    SigmaRZ = E_RZ - E_R %*% t(E_Z);
+
+    # compute OLS loadings for the linear return model
+    B = SigmaRZ %*% solve( SigmaZ ); # right division of SigmaRZ by SigmaZ
+    a = E_R - B %*% E_Z;
+    aMinusTauMuX[ i] = norm( a - tau*linearModel$muX, type="2" );
+    normDminusB[ i ]  = norm( linearModel$D - B, type = "F" );
+    
+    # pairwise correlations of U
+    SigmaU = SigmaR - B %*% t( SigmaRZ );
+    corrU = cov2cor(SigmaU);
+    stackedCorrU = as.array(corrU);
+    minCorrU[ i ]  = min(min(abs(corrU)));
+    meanCorrU[ i ] = ( N * mean( mean( abs( corrU) ) ) - 1 ) / ( N - 1 );
+    corrU[ seq(1, N * N, (N+1) ) ] = 0;
+    maxCorrU[ i ] = max( max( abs( corrU) ) );
+}
+
+##################################################################################################################
+### Plots
+# relationship between the constant nd the intercept
+dev.new();
+plot(tauRangeWeeks, aMinusTauMuX, type= "l", xlab = expression(paste("investment horizon, ", tau,", weeks")),
+ main = expression( paste( "norm of ( a - ", tau,mu[X],")"^t )));
+
+# relationship between the loadings D in and the loadings B
+dev.new();
+plot(tauRangeWeeks, normDminusB, type = "l", xlab = expression(paste("investment horizon, ", tau,", weeks")), main = expression("norm of (D-B)"^t));
+
+
+
+# determine if U idiosyncratic
+dev.new();
+plot(tauRangeWeeks, maxCorrU, col = "red", type = "l", xlab = expression(paste("investment horizon, ", tau,", weeks")),
+    ylab = "", main = "pairwise correlations of elements of U" );
+lines(tauRangeWeeks, meanCorrU, col = "blue");
+lines(tauRangeWeeks, minCorrU, col = "green");
+legend( "topleft", 1.9, c( "max absolute corr", "mean absolute corr", "min absolute corr" ), col = c( "red","blue", "green" ),
+     lty=1, bg = "gray90" );
+}
\ No newline at end of file

Added: pkg/Meucci/demo/S_JumpDiffusionMerton.R
===================================================================
--- pkg/Meucci/demo/S_JumpDiffusionMerton.R	                        (rev 0)
+++ pkg/Meucci/demo/S_JumpDiffusionMerton.R	2013-07-02 11:33:19 UTC (rev 2487)
@@ -0,0 +1,26 @@
+#'This script simulates a jump-diffusion process, 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_JumoDiffusionMerton.m"
+#'
+#' @author Xavier Valls \email{flamejat@@gmail.com}
+
+##################################################################################################################
+### Parameters
+ts = seq( 1/252, 1, 1/252); # grid of time values at which the process is evaluated ("0" will be added, too)
+J  = 10; # number of simulations
+
+##################################################################################################################
+### Simulate processes
+
+mu  = 0.00; # deterministic drift
+sig = 0.20; # Gaussian component
+
+l = 3.45; # Poisson process arrival rate
+a = 0;    # drift of log-jump
+D = 0.2;  # st.dev of log-jump
+
+X = SimulateJumpDiffusionMerton( mu, sig, l, a, D, ts, J );    
+matplot(c( 0, ts), t(X), type="l", xlab = "time", main = "Merton jump-diffusion");

Added: pkg/Meucci/man/SimulateJumpDiffusionMerton.Rd
===================================================================
--- pkg/Meucci/man/SimulateJumpDiffusionMerton.Rd	                        (rev 0)
+++ pkg/Meucci/man/SimulateJumpDiffusionMerton.Rd	2013-07-02 11:33:19 UTC (rev 2487)
@@ -0,0 +1,38 @@
+\name{SimulateJumpDiffusionMerton}
+\alias{SimulateJumpDiffusionMerton}
+\title{This function simulates a jump diffusion process, as described in A. Meucci "Risk and Asset Allocation",
+Springer, 2005}
+\usage{
+  SimulateJumpDiffusionMerton(m, s, l, a, D, ts, J)
+}
+\arguments{
+  \item{m}{: [scalar] deterministic drift of diffusion}
+
+  \item{s}{: [scalar] standard deviation of diffusion}
+
+  \item{l}{: [scalar] Poisson process arrival rate}
+
+  \item{a}{: [scalar] drift of log-jump}
+
+  \item{D}{: [scalar] st.dev of log-jump}
+
+  \item{ts}{: [vector] time steps}
+
+  \item{J}{: [scalar] number of simulations}
+}
+\value{
+  X : [matrix] (J x length(ts)) of simulations
+}
+\description{
+  This function simulates a jump diffusion process, as
+  described in A. Meucci "Risk and Asset Allocation",
+  Springer, 2005
+}
+\author{
+  Xavier Valls \email{flamejat at gmail.com}
+}
+\references{
+  \url{http://symmys.com/node/170} See Meucci's script for
+  "SimulateJumpDiffusionMerton.m"
+}
+



More information about the Returnanalytics-commits mailing list