[Returnanalytics-commits] r2065 - pkg/PerformanceAnalytics/sandbox/Meucci/R
noreply at r-forge.r-project.org
noreply at r-forge.r-project.org
Sun Jun 24 23:45:56 CEST 2012
Author: mkshah
Date: 2012-06-24 23:45:55 +0200 (Sun, 24 Jun 2012)
New Revision: 2065
Modified:
pkg/PerformanceAnalytics/sandbox/Meucci/R/EntropyProg.R
Log:
Moving common functions pHist and prior2Posterior to EntropyProg.R
Modified: pkg/PerformanceAnalytics/sandbox/Meucci/R/EntropyProg.R
===================================================================
--- pkg/PerformanceAnalytics/sandbox/Meucci/R/EntropyProg.R 2012-06-24 21:44:43 UTC (rev 2064)
+++ pkg/PerformanceAnalytics/sandbox/Meucci/R/EntropyProg.R 2012-06-24 21:45:55 UTC (rev 2065)
@@ -173,4 +173,64 @@
if ( sum( p_ ) > 1.001 ) { stop( "Sum or revised probabilities is greater than 1!" ) }
return ( list ( p_ = p_ , optimizationPerformance = optimizationPerformance ) )
+}
+
+#' Calculate the full-confidence posterior distributions of Mu and Sigma
+#'
+#' @param M a numeric vector with the Mu of the normal reference model
+#' @param Q a numeric vector used to construct a view on expectation of the linear combination Q %*% X
+#' @param M_Q a numeric vector with the view of the expectations of QX
+#' @param S a covariance matrix for the normal reference model
+#' @param G a numeric vector used to construct a view on covariance of the linear combination G %*% X
+#' @param S_G a numeric with the expectation associated with the covariance of the linear combination GX
+#'
+#' @return a list with
+#' M_ a numeric vector with the full-confidence posterior distribution of Mu
+#' S_ a covariance matrix with the full-confidence posterior distribution of Sigma
+#'
+#' @references
+#' \url{http://www.symmys.com}
+#' See Meucci script Prior2Posterior.m attached to Entropy Pooling Paper
+#' @author Ram Ahluwalia \email{ram@@wingedfootcapital.com}
+Prior2Posterior = function( M , Q , M_Q , S , G , S_G )
+{
+ # Compute posterior moments
+
+ if ( Q != 0 ) { M_ = M + S %*% t(Q) %*% solve( Q %*% S %*% t(Q) ) %*% ( M_Q - Q %*% M) }
+ else { M_ = M }
+
+ if ( G != 0 ) { S_ = S + (S %*% t(G)) %*% ( solve(G %*% S %*% t(G)) %*% S_G %*% solve(G %*% S %*% t(G)) - solve( G %*% S %*% t(G)) ) %*% (G %*% S) }
+ else { S_ = S }
+
+ return( list( M_ = M_ , S_ = S_ ) )
+}
+
+pHist = function( X , p , nBins )
+{
+ if ( length( match.call() ) < 3 )
+ {
+ J = size( X , 1 )
+ nBins = round( 10 * log(J) )
+ }
+
+ dist = hist( x = X , breaks = nBins , freq = FALSE , main = "Portfolio return distribution" )
+ n = dist$counts
+ x = dist$breaks
+ D = x[2] - x[1]
+
+ N = length(x)
+ np = zeros(N , 1)
+
+ for (s in 1:N)
+ {
+ # The boolean Index is true is X is within the interval centered at x(s) and within a half-break distance
+ Index = ( X >= x[s] - D/2 ) & ( X <= x[s] + D/2 )
+ # np = new probabilities?
+ np[ s ] = sum( p[ Index ] )
+ f = np/D
+ }
+
+ barplot( f , x , 1 )
+
+ return( list( f = f , x = x ) )
}
\ No newline at end of file
More information about the Returnanalytics-commits
mailing list