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

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Tue Jul 30 11:54:10 CEST 2013


Author: xavierv
Date: 2013-07-30 11:54:10 +0200 (Tue, 30 Jul 2013)
New Revision: 2678

Added:
   pkg/Meucci/R/MleRecursionForStudentT.R
   pkg/Meucci/demo/S_FitSwapToStudentT.R
   pkg/Meucci/man/MleRecursionForStudentT.Rd
Modified:
   pkg/Meucci/DESCRIPTION
   pkg/Meucci/NAMESPACE
Log:
- added S_FitSwapToStudentT demo script from chapter 4 and its associated function

Modified: pkg/Meucci/DESCRIPTION
===================================================================
--- pkg/Meucci/DESCRIPTION	2013-07-30 09:04:10 UTC (rev 2677)
+++ pkg/Meucci/DESCRIPTION	2013-07-30 09:54:10 UTC (rev 2678)
@@ -84,3 +84,4 @@
     'RandNormalInverseWishart.R'
     'FitMultivariateGarch.R'
     'MvnRnd.R'
+    'MleRecursionForStudentT.R'

Modified: pkg/Meucci/NAMESPACE
===================================================================
--- pkg/Meucci/NAMESPACE	2013-07-30 09:04:10 UTC (rev 2677)
+++ pkg/Meucci/NAMESPACE	2013-07-30 09:54:10 UTC (rev 2678)
@@ -23,6 +23,7 @@
 export(LognormalCopulaPdf)
 export(LognormalMoments2Parameters)
 export(LognormalParam2Statistics)
+export(MleRecursionForStudentT)
 export(MvnRnd)
 export(NoisyObservations)
 export(NormalCopulaPdf)

Added: pkg/Meucci/R/MleRecursionForStudentT.R
===================================================================
--- pkg/Meucci/R/MleRecursionForStudentT.R	                        (rev 0)
+++ pkg/Meucci/R/MleRecursionForStudentT.R	2013-07-30 09:54:10 UTC (rev 2678)
@@ -0,0 +1,57 @@
+#' Compute recursively the ML estimators of location and scatter of a multivariate Student t distribution with 
+#' given degrees of freedom, as described in  A. Meucci, "Risk and Asset Allocation", Springer, 2005.
+#'  
+#'  @param   x         : [matrix] (T x N) observations
+#'  @param   Nu        : [scalar] degrees of freedom parameter
+#'  @param   Tolerance : [scalar] tolerance parameter. Default: 10^(-10)
+#'
+#'  @return  Mu        : [vector] (N x 1) mean
+#'  @return  Sigma     : [matrix] (N x N) covariance
+#'
+#' @references
+#' \url{http://}
+#' See Meucci's script for "MleRecursionForStudentT.m"
+#'
+#' @author Xavier Valls \email{flamejat@@gmail.com}
+#' @export
+
+MleRecursionForStudentT = function(x, Nu, Tolerance = 10^(-10) )
+{
+
+    T      = nrow( x );
+    N      = ncol( x );
+    Ones_N = matrix( 1, 1, N ); # fixed for fast matrix operation
+    Ones_T = matrix( 1, T, 1 ); # fixed for fast matrix operation 
+
+    # initialize variables
+    w = matrix( 1, T, 1 );
+    Mu = matrix( 0, N, 1 );
+    Sigma = matrix( 0, N, N );
+
+    Error = 10^6;
+    # start main loop
+    while( Error > Tolerance )
+    {
+        
+        # update
+        Mu_Old = Mu;
+        Sigma_Old = Sigma;
+        
+        # Step 1
+        W = w %*% Ones_N;
+        Mu = matrix( apply(  W * x, 2, sum  ) ) / sum( w );
+        
+        x_c = x - Ones_T %*% t(Mu);
+        Sigma = t( W * x_c ) %*% x_c / T;
+
+        # Step 2
+        InvS = solve(Sigma);
+        Ma2 = apply( ( x_c %*% InvS ) * x_c, 1, sum );
+        w = ( Nu + N) / ( Nu + Ma2 );
+
+        # convergence
+        Error = sum( diag( (Sigma - Sigma_Old) ^2) ) / N + t(Mu - Mu_Old) %*% ( Mu - Mu_Old ) / N;    
+    }
+
+    return( list( Mu = Mu, Sigma = Sigma)  );
+}
\ No newline at end of file

Added: pkg/Meucci/demo/S_FitSwapToStudentT.R
===================================================================
--- pkg/Meucci/demo/S_FitSwapToStudentT.R	                        (rev 0)
+++ pkg/Meucci/demo/S_FitSwapToStudentT.R	2013-07-30 09:54:10 UTC (rev 2678)
@@ -0,0 +1,47 @@
+#' This script demonstrates the recursive ML estimation of the location and scatter parameters of a multivariate 
+#' Student t distribution, as described in A. Meucci, "Risk and Asset Allocation", Springer, 2005,  Chapter 4.
+#'
+#' @references
+#' \url{http://symmys.com/node/170}
+#' See Meucci's script for "S_FitSwapToStudentT.m"
+#'
+#' TO DO: Change colors from TwoDimEllipsoid in each iteration
+#'
+#' @author Xavier Valls \email{flamejat@@gmail.com}
+
+##################################################################################################################
+### Load data
+load( "../data/usSwapRates.Rda" );
+
+##################################################################################################################
+### Inputs
+ChooseRates = c( 1, 2 ); # 1=2yr; 2=5yr; 3=10yr
+
+Y = cbind( UsSwapRates[ , 1 ], UsSwapRates[ , 3 ] );
+X = Y[ -1, ] - Y[ -nrow( Y ), ];
+
+Nus = c( 3, 100 );
+
+#################################################################################################################
+### Computations
+Tolerance = 10^(-10);
+Estimate = array( list() , length(Nus));
+for( q in 1 : length(Nus) )
+{ 
+    Estimate[[q]] = MleRecursionForStudentT( X, Nus[ q ], Tolerance );
+}
+
+#################################################################################################################
+### Figures
+dev.new();
+h = plot( X[ , 1 ], X[ , 2 ], bg = "blue", xlab = colnames(UsSwapRates)[1], ylab = colnames(UsSwapRates)[3]);
+
+for( q in 1 : length(Nus) )
+{
+    M = Estimate[[ q ]]$Mu;
+    S = Estimate[[ q ]]$Sigma * Nus[ q ] / ( Nus[ q ] - 2);
+    dd = TwoDimEllipsoid( M, S, 2, 0, 0 );
+    #set(dd, 'color', 0.7*[rand() rand() rand()]);
+}
+
+

Added: pkg/Meucci/man/MleRecursionForStudentT.Rd
===================================================================
--- pkg/Meucci/man/MleRecursionForStudentT.Rd	                        (rev 0)
+++ pkg/Meucci/man/MleRecursionForStudentT.Rd	2013-07-30 09:54:10 UTC (rev 2678)
@@ -0,0 +1,34 @@
+\name{MleRecursionForStudentT}
+\alias{MleRecursionForStudentT}
+\title{Compute recursively the ML estimators of location and scatter of a multivariate Student t distribution with
+given degrees of freedom, as described in  A. Meucci, "Risk and Asset Allocation", Springer, 2005.}
+\usage{
+  MleRecursionForStudentT(x, Nu, Tolerance = 10^(-10))
+}
+\arguments{
+  \item{x}{: [matrix] (T x N) observations}
+
+  \item{Nu}{: [scalar] degrees of freedom parameter}
+
+  \item{Tolerance}{: [scalar] tolerance parameter. Default:
+  10^(-10)}
+}
+\value{
+  Mu : [vector] (N x 1) mean
+
+  Sigma : [matrix] (N x N) covariance
+}
+\description{
+  Compute recursively the ML estimators of location and
+  scatter of a multivariate Student t distribution with
+  given degrees of freedom, 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
+  "MleRecursionForStudentT.m"
+}
+



More information about the Returnanalytics-commits mailing list