[Returnanalytics-commits] r3015 - pkg/Meucci/demo

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Sat Sep 7 12:16:35 CEST 2013


Author: xavierv
Date: 2013-09-07 12:16:35 +0200 (Sat, 07 Sep 2013)
New Revision: 3015

Modified:
   pkg/Meucci/demo/AnalyticalvsNumerical.R
   pkg/Meucci/demo/FullFlexProbs.R
Log:
- AnalyticalvsNumerical demo script revised

Modified: pkg/Meucci/demo/AnalyticalvsNumerical.R
===================================================================
--- pkg/Meucci/demo/AnalyticalvsNumerical.R	2013-09-07 08:49:48 UTC (rev 3014)
+++ pkg/Meucci/demo/AnalyticalvsNumerical.R	2013-09-07 10:16:35 UTC (rev 3015)
@@ -1,40 +1,43 @@
+#' This example script compares the numerical and the analytical solution of entropy-pooling, as described
+#' in A. Meucci, "Fully Flexible Views: Theory and Practice", The Risk Magazine, October 2008, p 100-106.
+#'
+#' Most recent version of article and MATLAB code available at
+#' http://www.symmys.com/node/158
+#'
+#' @references 
+#' A. Meucci, Fully Flexible Views: Theory and Practice \url{http://www.symmys.com/node/158}
+#' See Meucci script for "S_MAIN.m"
+#' 
+#' @author Ram Ahluwalia \email{ram@@wingedfootcapital.com} and Xavier Valls \email{flamejat@@gmail.com}
 
-# This example script compares the numerical and the analytical solution of entropy-pooling, see
-# "A. Meucci - Fully Flexible Views: Theory and Practice 
-# and associated script S_Main
-# Example compares analytical vs. numerical approach to entropy pooling
-
-# Code by A. Meucci, September 2008
-# Last version available at www.symmys.com > Teaching > MATLAB
-
 ###############################################################
 # prior
 ###############################################################
-library( matlab )
-library( MASS )
+
 # analytical representation
-N = 2 # market dimension (2 assets)
-Mu = zeros( N , 1 )
-r= .6
-Sigma = ( 1 - r ) * eye( N ) + r * ones( N , N ) # nxn correlation matrix with correlaiton 'r' in off-diagonals
 
+N  = 2 # market dimension (2 assets)
+Mu = matrix( 0, N , 1 )
+r  = 0.6
+Sigma = ( 1 - r ) * diag( 1, N ) + r * matrix( 1, N , N ) # nxn correlation matrix with correlation 'r' in off-diagonals
+
 # numerical representation
-J = 100000 # number of scenarios
-p = ones( J , 1 ) / J
-dd = mvrnorm( J / 2 , zeros( N , 1 ) , Sigma ) # distribution centered on (0,0) with variance Sigma
-X = ones( J , 1 ) %*% t(Mu) + rbind( dd , -dd ) # JxN matrix of scenarios
+J  = 100000 # number of scenarios
+p  = matrix( 1, J , 1 ) / J
+dd = rmvnorm( J / 2 , matrix( 1, N , 1 ) , Sigma )      # distribution centered on (0,0) with variance Sigma
+X  = matrix( 1, J , 1 ) %*% t(Mu) + rbind( dd , -dd )   # JxN matrix of scenarios
 
 ###############################################################
 # views
 ###############################################################
 
 # location
-Q = matrix( c( 1 , -1 ) , nrow = 1 ) # long the first and asset and short the second asset produces an expectation (of Mu_Q calculated below)
-Mu_Q = .5
+Q = cbind(  1 , -1  ) # long the first and asset and short the second asset produces an expectation (of Mu_Q calculated below)
+Mu_Q = 0.5
 
 # scatter
-G = matrix( c( -1 , 1 ) , nrow = 1 )
-Sigma_G = .5^2
+G = cbind(  -1 , 1  )
+Sigma_G = 0.5^2
 
 ###############################################################
 #  posterior 
@@ -43,34 +46,36 @@
 # analytical posterior
 RevisedMuSigma = Prior2Posterior( Mu , Q , Mu_Q , Sigma , G , Sigma_G )
 Mu_ = RevisedMuSigma$M_
-
+Sigma_ = RevisedMuSigma$S_
 # numerical posterior
-Aeq = ones( 1 , J )  # constrain probabilities to sum to one...
+Aeq = matrix( 1, 1 , J )  # constrain probabilities to sum to one...
 beq = 1
 
 # create views
-    QX = X %*% t(Q) # a Jx1 matrix
 
-    Aeq = rbind( Aeq , t(QX) )    # ...constrain the first moments... 
-        # QX is a linear combination of vector Q and the scenarios X
-    
-    beq = rbind( beq , Mu_Q )
-    
-    SecMom = G %*% Mu_ %*% t(Mu_) %*% t(G) + Sigma_G  # ...constrain the second moments... 
-        # We use Mu_ from analytical result. We do not use Revised Sigma because we are testing whether
-        # the numerical approach for handling expectations of covariance matches the analytical approach
-        # TODO: Can we perform this procedure without relying on Mu_ from the analytical result?
-    GX = X %*% t(G)
-    
-    for ( k in 1:nrow( G ) )
-        {
-        for ( l in k:nrow( G ) )
-            {
-            Aeq = rbind( Aeq , t(GX[ , k ] * GX[ , l ] ) )
-            beq = rbind( beq , SecMom[ k , l ] )
-            }
-        }
+QX = X %*% t(Q) # a Jx1 matrix
 
+Aeq = rbind( Aeq , t(QX) )    # ...constrain the first moments... 
+    # QX is a linear combination of vector Q and the scenarios X
+
+beq = rbind( beq , Mu_Q )
+
+SecMom = G %*% Mu_ %*% t(Mu_) %*% t(G) + Sigma_G  # ...constrain the second moments... 
+    # We use Mu_ from analytical result. We do not use Revised Sigma because we are testing whether
+    # the numerical approach for handling expectations of covariance matches the analytical approach
+    # TODO: Can we perform this procedure without relying on Mu_ from the analytical result?
+
+GX = X %*% t(G)
+
+for ( k in 1:nrow( G ) )
+{
+    for ( l in k:nrow( G ) )
+    {
+        Aeq = rbind( Aeq , t(GX[ , k ] * GX[ , l ] ) )
+        beq = rbind( beq , SecMom[ k , l ] )
+    }
+}
+
 emptyMatrix = matrix( , nrow = 0 , ncol = 0 )
 p_ = EntropyProg( p , emptyMatrix , emptyMatrix , Aeq , beq ) # ...compute posterior probabilities
 

Modified: pkg/Meucci/demo/FullFlexProbs.R
===================================================================
--- pkg/Meucci/demo/FullFlexProbs.R	2013-09-07 08:49:48 UTC (rev 3014)
+++ pkg/Meucci/demo/FullFlexProbs.R	2013-09-07 10:16:35 UTC (rev 3015)
@@ -35,7 +35,7 @@
 #'A. Meucci, "Personalized Risk Management: Historical Scenarios with Fully Flexible Probabilities"
 #'GARP Risk Professional, Dec 2010, p 47-51
 #'
-#' Most recent version of article and code available at
+#' Most recent version of article and MATLAB code available at
 #' http://www.symmys.com/node/150
 #' @references 
 #' \url{http://www.symmys.com/node/150}



More information about the Returnanalytics-commits mailing list