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

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Tue Jun 25 10:14:14 CEST 2013


Author: xavierv
Date: 2013-06-25 10:14:13 +0200 (Tue, 25 Jun 2013)
New Revision: 2428

Added:
   pkg/Meucci/R/ConvertChangeInYield2Price.R
   pkg/Meucci/demo/S_AutocorrelatedProcess.R
   pkg/Meucci/demo/S_BondProjectionPricingNormal.R
   pkg/Meucci/man/ConvertChangeInYield2Price.Rd
Modified:
   pkg/Meucci/DESCRIPTION
   pkg/Meucci/NAMESPACE
Log:
- added two scripts from chapter 3 and its related functions

Modified: pkg/Meucci/DESCRIPTION
===================================================================
--- pkg/Meucci/DESCRIPTION	2013-06-25 03:44:36 UTC (rev 2427)
+++ pkg/Meucci/DESCRIPTION	2013-06-25 08:14:13 UTC (rev 2428)
@@ -67,3 +67,4 @@
     'LognormalCopulaPdf.R'
     'NormalCopulaPdf.R'
     'StudentTCopulaPdf.R'
+    'ConvertChangeInYield2Price.R'

Modified: pkg/Meucci/NAMESPACE
===================================================================
--- pkg/Meucci/NAMESPACE	2013-06-25 03:44:36 UTC (rev 2427)
+++ pkg/Meucci/NAMESPACE	2013-06-25 08:14:13 UTC (rev 2428)
@@ -4,6 +4,7 @@
 export(ComputeMoments)
 export(ComputeMVE)
 export(CondProbViews)
+export(ConvertChangeInYield2Price)
 export(Cumul2Raw)
 export(DetectOutliersViaMVE)
 export(EntropyProg)

Added: pkg/Meucci/R/ConvertChangeInYield2Price.R
===================================================================
--- pkg/Meucci/R/ConvertChangeInYield2Price.R	                        (rev 0)
+++ pkg/Meucci/R/ConvertChangeInYield2Price.R	2013-06-25 08:14:13 UTC (rev 2428)
@@ -0,0 +1,30 @@
+#' Convert change in yield-to-maturity to price for fixed-income securities, as described in 
+#' A. Meucci "Risk and Asset Allocation", Springer, 2005
+#'
+#'  @param	Exp_DY        : [vector] (N x 1) expected value of change in yield to maturity
+#'	@param	Cov_DY        : [matrix] (N x N) covariance of change in yield to maturity
+#'  @param	Times2Mat     : [scalar] time to maturity
+#'  @param	CurrentPrices : [vector] (N x 1) current prices
+#'  
+#'  @return	Exp_Prices    : [vector] (N x 1) expected prices
+#'  @return	Cov_Prices    : [matrix] (N x N) covariance of prices
+#'
+#' @references
+#' \url{http://}
+#' See (6.77)-(6.79) in "Risk and Asset Allocation"-Springer (2005), by A. Meucci
+#' See Meucci's script for "ConvertChangeInYield2Price.m"
+#'
+#' @author Xavier Valls \email{flamejat@@gmail.com}
+#' @export
+
+
+ConvertChangeInYield2Price = function( Exp_DY, Cov_DY, Times2Mat, CurrentPrices )
+{
+	Mu    = log( CurrentPrices ) - Times2Mat * Exp_DY;
+	Sigma = diag( Times2Mat^2 ) %*% Cov_DY;
+
+	Exp_Prices = exp(Mu + (1/2) * diag( Sigma ));
+	Cov_Prices = exp(Mu + (1/2) * diag( Sigma )) %*% t(exp(Mu + (1/2) * diag(Sigma))) * ( exp( Sigma ) - 1);
+
+	return( list( Exp_Prices = Exp_Prices, Cov_Prices = Cov_Prices ) );
+}
\ No newline at end of file

Added: pkg/Meucci/demo/S_AutocorrelatedProcess.R
===================================================================
--- pkg/Meucci/demo/S_AutocorrelatedProcess.R	                        (rev 0)
+++ pkg/Meucci/demo/S_AutocorrelatedProcess.R	2013-06-25 08:14:13 UTC (rev 2428)
@@ -0,0 +1,35 @@
+
+#' This script simulates a Ornstein-Uhlenbeck AR(1) process, as described in A. Meucci, "
+#' Risk and Asset Allocation", Springer, 2005,  Chapter 3.
+#'
+#' @references
+#' \url{http://}
+#' See Meucci's script for "S_AutocorrelatedProcess.m"
+#'
+#' @author Xavier Valls \email{flamejat@@gmail.com}
+#' @export
+
+##################################################################################################################
+### Input parameters
+theta = 0.1;  # reversion speed
+m     = 0.05; # long term mean 
+sigma = 0.01; # volatility
+T     = 10^4; # number of steps
+tau   = 0.01; # discrete time interval
+
+##################################################################################################################
+### Determine parameters
+var = sigma^2 / 2 / theta * ( 1 - exp( -2 * theta * tau ) );
+sd = sqrt(var);
+eps = rnorm( T, 0, sd );
+
+x = matrix( NaN, T, 1);
+x[ 1 ] = 0;
+
+for( t in 1 : (T - 1) )
+{
+    x[ t + 1 ] = m + exp( -theta * tau ) * ( x[ t ] - m ) + eps[ t ];
+}
+
+dev.new()
+plot( x, type="l", main = "AR(1) process vs. time" );

Added: pkg/Meucci/demo/S_BondProjectionPricingNormal.R
===================================================================
--- pkg/Meucci/demo/S_BondProjectionPricingNormal.R	                        (rev 0)
+++ pkg/Meucci/demo/S_BondProjectionPricingNormal.R	2013-06-25 08:14:13 UTC (rev 2428)
@@ -0,0 +1,61 @@
+#################################################################################################################
+### 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 to the investment horizon 
+### Then it computes the distribution of prices at the investment horizon 
+### == Chapter 3 ==
+##################################################################################################################
+clc; clear; close all;
+run ../../LIBRARY/InitializeLibrary.m; # load library of functions
+
+##################################################################################################################
+### Inputs
+tau = 1/52;        # time to horizon expressed in years
+tau_tilde = 1/52;  # estimation period expressed in years
+
+FlatCurve  = 0.04;   
+TimesToMat = c( 1, 5, 10, 52, 520 ) / 52; # time to maturity of selected bonds expressed in years
+
+# parameters of the distribution of the changes in yield to maturity
+u_minus_tau = TimesToMat - tau;
+mus = 0 * u_minus_tau;
+sigmas = ( 20 + 5 / 4 * u_minus_tau ) / 10000;
+
+nSim = 100000;
+
+##################################################################################################################
+### Bond market projection to horizon and pricing 
+BondCurrent_Prices_Shifted = exp( -FlatCurve * u_minus_tau );
+BondCurrent_Prices = exp( -FlatCurve * TimesToMat );
+
+# project bond market to horizon
+N = length( TimesToMat ); # number of bonds
+U = runif( nSim );
+BondMarket_Scenarios = matrix( 0, nSim, N );
+for( n in 1 : N )
+{
+    # generate co-dependent changes in yield-to-maturity
+    DY_Scenarios = qnorm( U, mus[ n ] * tau / tau_tilde, sigmas[ n ] * sqrt( tau / tau_tilde ) ); 
+
+    # compute the horizon prices, (3.81) in "Risk and Asset Allocation" - Springer
+    X = -u_minus_tau[ n ] * DY_Scenarios;
+    BondMarket_Scenarios[ , n ] = BondCurrent_Prices_Shifted[ n ] * exp( X ); 
+}
+
+##################################################################################################################
+### MV inputs - analytical
+Exp_Hrzn_DY_Hat  = mus * tau / tau_tilde;
+SDev_Hrzn_DY_Hat = sigmas * sqrt( tau / tau_tilde );
+Corr_Hrzn_DY_Hat = matrix( 1, N, N ); # full co-dependence
+Cov_Hrzn_DY_Hat  = diag( SDev_Hrzn_DY_Hat ) %*% Corr_Hrzn_DY_Hat %*% diag( SDev_Hrzn_DY_Hat );
+Bond = ConvertChangeInYield2Price( Exp_Hrzn_DY_Hat, Cov_Hrzn_DY_Hat, u_minus_tau, BondCurrent_Prices_Shifted );
+print( Bond$Exp_Prices );
+print( Bond$Cov_Prices );
+
+##################################################################################################################
+### MV inputs - numerical
+BondExp_Prices = t( apply(BondMarket_Scenarios, 2, mean) );
+BondCov_Prices = cov( BondMarket_Scenarios );
+print( BondExp_Prices );
+print( BondCov_Prices );
+
+### EOF
\ No newline at end of file

Added: pkg/Meucci/man/ConvertChangeInYield2Price.Rd
===================================================================
--- pkg/Meucci/man/ConvertChangeInYield2Price.Rd	                        (rev 0)
+++ pkg/Meucci/man/ConvertChangeInYield2Price.Rd	2013-06-25 08:14:13 UTC (rev 2428)
@@ -0,0 +1,38 @@
+\name{ConvertChangeInYield2Price}
+\alias{ConvertChangeInYield2Price}
+\title{Convert change in yield-to-maturity to price for fixed-income securities, as described in
+A. Meucci "Risk and Asset Allocation", Springer, 2005}
+\usage{
+  ConvertChangeInYield2Price(Exp_DY, Cov_DY, Times2Mat,
+    CurrentPrices)
+}
+\arguments{
+  \item{Exp_DY}{: [vector] (N x 1) expected value of change
+  in yield to maturity}
+
+  \item{Cov_DY}{: [matrix] (N x N) covariance of change in
+  yield to maturity}
+
+  \item{Times2Mat}{: [scalar] time to maturity}
+
+  \item{CurrentPrices}{: [vector] (N x 1) current prices}
+}
+\value{
+  Exp_Prices : [vector] (N x 1) expected prices
+
+  Cov_Prices : [matrix] (N x N) covariance of prices
+}
+\description{
+  Convert change in yield-to-maturity to price for
+  fixed-income securities, as described in A. Meucci "Risk
+  and Asset Allocation", Springer, 2005
+}
+\author{
+  Xavier Valls \email{flamejat at gmail.com}
+}
+\references{
+  \url{http://} See (6.77)-(6.79) in "Risk and Asset
+  Allocation"-Springer (2005), by A. Meucci See Meucci's
+  script for "ConvertChangeInYield2Price.m"
+}
+



More information about the Returnanalytics-commits mailing list