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

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Tue Jun 25 12:05:33 CEST 2013


Author: xavierv
Date: 2013-06-25 12:05:32 +0200 (Tue, 25 Jun 2013)
New Revision: 2430

Added:
   pkg/Meucci/R/ProjectionStudentT.R
   pkg/Meucci/demo/S_BondProjectionPricingStudentT.R
   pkg/Meucci/man/ProjectionStudentT.Rd
Modified:
   pkg/Meucci/DESCRIPTION
   pkg/Meucci/NAMESPACE
Log:
- added demo file S_BondProjectionPricingStudentT and ProjectionStudentT function

Modified: pkg/Meucci/DESCRIPTION
===================================================================
--- pkg/Meucci/DESCRIPTION	2013-06-25 08:36:35 UTC (rev 2429)
+++ pkg/Meucci/DESCRIPTION	2013-06-25 10:05:32 UTC (rev 2430)
@@ -68,3 +68,4 @@
     'NormalCopulaPdf.R'
     'StudentTCopulaPdf.R'
     'ConvertChangeInYield2Price.R'
+    'ProjectionStudentT.R'

Modified: pkg/Meucci/NAMESPACE
===================================================================
--- pkg/Meucci/NAMESPACE	2013-06-25 08:36:35 UTC (rev 2429)
+++ pkg/Meucci/NAMESPACE	2013-06-25 10:05:32 UTC (rev 2430)
@@ -22,6 +22,7 @@
 export(PanicCopula)
 export(PartialConfidencePosterior)
 export(PlotDistributions)
+export(ProjectionStudentT)
 export(Raw2Central)
 export(Raw2Cumul)
 export(RejectOutlier)

Added: pkg/Meucci/R/ProjectionStudentT.R
===================================================================
--- pkg/Meucci/R/ProjectionStudentT.R	                        (rev 0)
+++ pkg/Meucci/R/ProjectionStudentT.R	2013-06-25 10:05:32 UTC (rev 2430)
@@ -0,0 +1,49 @@
+#' Perform the horizon projection of a Student t invariant, as described in 
+#' A. Meucci "Risk and Asset Allocation", Springer, 2005
+#'
+#'  @param	nu    : [scalar] degree of freedom
+#'	@param	s     : [scalar] scatter parameter
+#'  @param	m     : [scalar] location parameter
+#'  @param	T     : [scalar] multiple of the estimation period to the invesment horizon 
+#'  
+#'  @return	x_Hor : [scalar]
+#'  @return	f_Hor : [scalar]
+#'	@return	F_Hor : [scalar]
+#'
+#' @references
+#' \url{http://}
+#' See Meucci's script for "ProjectionStudentT.m"
+#'
+#' @author Xavier Valls \email{flamejat@@gmail.com}
+#' @export
+
+ProjectionStudentT = function(nu, m, s, T)
+{
+	# set up grid
+	N  = 2 ^ 14; # coarseness level
+	a  = -qnorm( 10^(-15), 0, sqrt( T ) );
+	h  = 2 * a / N;
+	Xi = seq(-a+h, a, h );
+
+	# discretized initial pdf (standardized)
+	f = 1 / h * ( pt( Xi + h/2, nu ) - pt( Xi - h/2, nu ) );
+	f[ N ] = 1 / h *( pt(-a + h/2, nu ) - pt( -a, nu ) + pt( a, nu )- pt( a-h/2, nu ) );
+
+	# discretized characteristic function
+	Phi = fft(f);                     
+
+	# projection of discretized characteristic function
+	Signs = ( -1 )^((0:(N-1)) * ( T - 1));   
+	Phi_T = h ^ ( T - 1 ) * Signs * (Phi ^ T);
+
+	# horizon discretized pdf (standardized)
+	f_T = as.numeric( ifft( Phi_T ) );
+
+	# horizon discretized pdf and cdf (non-standardized)
+	x_Hor = m * T + s * Xi;
+	f_Hor = f_T / s;
+	F_Hor = h * cumsum( f_Hor * s );
+
+	return( list( x = x_Hor, f = f_Hor, F = F_Hor ) );
+
+}
\ No newline at end of file

Added: pkg/Meucci/demo/S_BondProjectionPricingStudentT.R
===================================================================
--- pkg/Meucci/demo/S_BondProjectionPricingStudentT.R	                        (rev 0)
+++ pkg/Meucci/demo/S_BondProjectionPricingStudentT.R	2013-06-25 10:05:32 UTC (rev 2430)
@@ -0,0 +1,59 @@
+#'This script projects the distribution of the market invariants for the bond markets 
+#'(i.e. the changes in yield to maturity) from the estimation interval (Student t assumption)
+#'to the investment horizon. Then it computes the distribution of prices at the investment 
+#'horizon  as described in A. Meucci,"Risk and Asset Allocation", Springer, 2005,  Chapter 3.
+#'
+#' @references
+#' \url{http://}
+#' See Meucci's script for "S_BondProjectionPricingStudentT.m"
+#'
+#' @author Xavier Valls \email{flamejat@@gmail.com}
+#' @export
+
+##################################################################################################################
+### Inputs
+
+tau = 4/52;        # time to horizon expressed in years
+tau_tilde = 1/52;  # estimation period expressed in years
+
+FlatCurve  = 0.04;   
+TimesToMat = c( 4, 5, 10, 52, 520 ) / 52; # time to maturity of selected bonds expressed in years
+
+# determine the parameters of the distribution of the invariants (changes in yield to maturity)
+Periods = tau / tau_tilde; # number of estimation periods until the investment horizon
+u_minus_tau = TimesToMat - tau;
+
+nu  = 8;
+mus = 0 * u_minus_tau;
+sigmas = ( 20 + 5 / 4 * u_minus_tau ) / 10000;
+Num_Scenarios = 100000;
+
+##################################################################################################################
+### Projection and pricing 
+BondCurrent_Prices_Shifted = exp(-FlatCurve * u_minus_tau);
+BondCurrent_Prices = exp(-FlatCurve * TimesToMat);
+
+# generate common source of randomness
+U = runif( Num_Scenarios );  
+
+N = length( TimesToMat ); # number of bonds
+par( mfrow = c( N,1 ));
+for( n in 1 : N )
+{
+    # project bond market to horizon
+    Projection = ProjectionStudentT( nu, mus[ n ], sigmas[ n ], Periods);
+    
+    # generate co-dependent changes in yield-to-maturity
+    DY_Scenarios = interp1( Projection$F, Projection$x, U, method = "linear"); 
+
+    # compute the horizon prices, (3.81) in "Risk and Asset Allocation" - Springer
+    X = -u_minus_tau[ n ] * DY_Scenarios;
+    Z = BondCurrent_Prices_Shifted[ n ] * exp(X); 
+    
+    # compute and plot linear returns
+    L = Z / BondCurrent_Prices[ n ] - 1;  
+    
+    #for n=1 histogram represents the only bar (not empty)
+    hist(L, round(10 * log(Num_Scenarios)), xlab = paste( "Linear returns for bond",  n  ), main = "" );
+    
+}

Added: pkg/Meucci/man/ProjectionStudentT.Rd
===================================================================
--- pkg/Meucci/man/ProjectionStudentT.Rd	                        (rev 0)
+++ pkg/Meucci/man/ProjectionStudentT.Rd	2013-06-25 10:05:32 UTC (rev 2430)
@@ -0,0 +1,37 @@
+\name{ProjectionStudentT}
+\alias{ProjectionStudentT}
+\title{Perform the horizon projection of a Student t invariant, as described in
+A. Meucci "Risk and Asset Allocation", Springer, 2005}
+\usage{
+  ProjectionStudentT(nu, m, s, T)
+}
+\arguments{
+  \item{nu}{: [scalar] degree of freedom}
+
+  \item{s}{: [scalar] scatter parameter}
+
+  \item{m}{: [scalar] location parameter}
+
+  \item{T}{: [scalar] multiple of the estimation period to
+  the invesment horizon}
+}
+\value{
+  x_Hor : [scalar]
+
+  f_Hor : [scalar]
+
+  F_Hor : [scalar]
+}
+\description{
+  Perform the horizon projection of a Student t invariant,
+  as described in A. Meucci "Risk and Asset Allocation",
+  Springer, 2005
+}
+\author{
+  Xavier Valls \email{flamejat at gmail.com}
+}
+\references{
+  \url{http://} See Meucci's script for
+  "ProjectionStudentT.m"
+}
+



More information about the Returnanalytics-commits mailing list