[Returnanalytics-commits] r3428 - in pkg/PortfolioAnalytics: . R man sandbox

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Fri Jun 20 00:32:14 CEST 2014


Author: rossbennett34
Date: 2014-06-20 00:32:13 +0200 (Fri, 20 Jun 2014)
New Revision: 3428

Added:
   pkg/PortfolioAnalytics/R/black_litterman.R
   pkg/PortfolioAnalytics/man/BlackLittermanFormula.Rd
   pkg/PortfolioAnalytics/man/black.litterman.Rd
Removed:
   pkg/PortfolioAnalytics/sandbox/BlackLittermanFormula.R
Modified:
   pkg/PortfolioAnalytics/NAMESPACE
Log:
functionalizing the Black Litterman script from Meucci package and moving to R. Also adding man files for black litterman functions

Modified: pkg/PortfolioAnalytics/NAMESPACE
===================================================================
--- pkg/PortfolioAnalytics/NAMESPACE	2014-06-19 21:35:29 UTC (rev 3427)
+++ pkg/PortfolioAnalytics/NAMESPACE	2014-06-19 22:32:13 UTC (rev 3428)
@@ -76,6 +76,7 @@
 export(add.objective_v2)
 export(add.sub.portfolio)
 export(applyFUN)
+export(black.litterman)
 export(box_constraint)
 export(center)
 export(chart.Concentration)

Copied: pkg/PortfolioAnalytics/R/black_litterman.R (from rev 3427, pkg/PortfolioAnalytics/sandbox/BlackLittermanFormula.R)
===================================================================
--- pkg/PortfolioAnalytics/R/black_litterman.R	                        (rev 0)
+++ pkg/PortfolioAnalytics/R/black_litterman.R	2014-06-19 22:32:13 UTC (rev 3428)
@@ -0,0 +1,75 @@
+
+#' @title Computes the Black-Litterman formula for the moments of the posterior normal.
+#'
+#' @description This function computes the Black-Litterman formula for the moments of the posterior normal, as described in  
+#' A. Meucci, "Risk and Asset Allocation", Springer, 2005.
+#' 
+#'   @param		Mu       [vector] (N x 1) prior expected values.
+#'   @param		Sigma    [matrix] (N x N) prior covariance matrix.
+#'   @param		P        [matrix] (K x N) pick matrix.
+#'   @param		v        [vector] (K x 1) vector of views.
+#'   @param		Omega    [matrix] (K x K) matrix of confidence.
+#'
+#'   @return	BLMu     [vector] (N x 1) posterior expected values.
+#'   @return	BLSigma  [matrix] (N x N) posterior covariance matrix.
+#'
+#' @references
+#' A. Meucci - "Exercises in Advanced Risk and Portfolio Management" \url{http://symmys.com/node/170}.
+#'
+#' See Meucci's script for "BlackLittermanFormula.m"
+#'
+#' @author Xavier Valls \email{flamejat@@gmail.com}
+BlackLittermanFormula = function( Mu, Sigma, P, v, Omega)
+{
+	BLMu    = Mu + Sigma %*% t( P ) %*% ( solve( P %*% Sigma %*% t( P ) + Omega ) %*% ( v - P %*% Mu ) );
+	BLSigma =  Sigma -  Sigma %*% t( P ) %*% ( solve( P %*% Sigma %*% t( P ) + Omega ) %*% ( P %*% Sigma ) );
+
+	return( list( BLMu = BLMu , BLSigma = BLSigma ) );
+
+}
+
+#' Black Litterman Estimates
+#' 
+#' Compute the Black Litterman estimate of moments for the posterior normal.
+#' 
+#' @note This function is largely based on the work of Xavier Valls to port
+#' the matlab code of Attilio Meucci to \R as documented in the Meucci package.
+#' 
+#' @param R returns
+#' @param P a K x N pick matrix
+#' @param Mu vector of length N of the prior expected values. The sample mean
+#' is used if \code{mu} is not provided as an argument.
+#' @param Sigma an N x N matrix of the prior covariance matrix. The sample covariance
+#' is used if \code{Sigma} is not provided as an argument.
+#' @return \itemize{
+#'   \item{BLMu:}{ posterior expected values}
+#'   \item{BLSigma:}{ posterior covariance matrix}
+#' }
+#' @author Ross Bennett, Xavier Valls
+#' @references
+#' A. Meucci - "Exercises in Advanced Risk and Portfolio Management" \url{http://symmys.com/node/170}.
+#' @seealso \code{\link{BlackLittermanFormula}}
+#' @export
+black.litterman <- function(R, P, Mu, Sigma){
+  
+  # Compute the sample estimate if mu is null
+  if(hasArg(Mu)){
+    if(length(Mu) != NCOL(R)) stop("length of Mu must equal number of columns of R")
+  } else {
+    Mu <- colMeans(R)
+  }
+  
+  # Compute the sample estimate if sigma is null
+  if(hasArg(Sigma)){
+    if(!all(dim(Sigma) == NCOL(R))) stop("dimensions of Sigma must equal number of columns of R")
+  } else {
+    Sigma <- cov(R)
+  }
+  
+  # Compute the Omega matrix and views value
+  Omega = tcrossprod(P %*% Sigma, P)
+  Views = as.numeric(sqrt( diag( Omega ) ))
+  B = BlackLittermanFormula( Mu, Sigma, P, Views, Omega )
+  return(B)
+}
+

Added: pkg/PortfolioAnalytics/man/BlackLittermanFormula.Rd
===================================================================
--- pkg/PortfolioAnalytics/man/BlackLittermanFormula.Rd	                        (rev 0)
+++ pkg/PortfolioAnalytics/man/BlackLittermanFormula.Rd	2014-06-19 22:32:13 UTC (rev 3428)
@@ -0,0 +1,36 @@
+% Generated by roxygen2 (4.0.1): do not edit by hand
+\name{BlackLittermanFormula}
+\alias{BlackLittermanFormula}
+\title{Computes the Black-Litterman formula for the moments of the posterior normal.}
+\usage{
+BlackLittermanFormula(Mu, Sigma, P, v, Omega)
+}
+\arguments{
+\item{Mu}{[vector] (N x 1) prior expected values.}
+
+\item{Sigma}{[matrix] (N x N) prior covariance matrix.}
+
+\item{P}{[matrix] (K x N) pick matrix.}
+
+\item{v}{[vector] (K x 1) vector of views.}
+
+\item{Omega}{[matrix] (K x K) matrix of confidence.}
+}
+\value{
+BLMu     [vector] (N x 1) posterior expected values.
+
+BLSigma  [matrix] (N x N) posterior covariance matrix.
+}
+\description{
+This function computes the Black-Litterman formula for the moments of the posterior normal, as described in
+A. Meucci, "Risk and Asset Allocation", Springer, 2005.
+}
+\author{
+Xavier Valls \email{flamejat at gmail.com}
+}
+\references{
+A. Meucci - "Exercises in Advanced Risk and Portfolio Management" \url{http://symmys.com/node/170}.
+
+See Meucci's script for "BlackLittermanFormula.m"
+}
+

Added: pkg/PortfolioAnalytics/man/black.litterman.Rd
===================================================================
--- pkg/PortfolioAnalytics/man/black.litterman.Rd	                        (rev 0)
+++ pkg/PortfolioAnalytics/man/black.litterman.Rd	2014-06-19 22:32:13 UTC (rev 3428)
@@ -0,0 +1,41 @@
+% Generated by roxygen2 (4.0.1): do not edit by hand
+\name{black.litterman}
+\alias{black.litterman}
+\title{Black Litterman Estimates}
+\usage{
+black.litterman(R, P, Mu, Sigma)
+}
+\arguments{
+\item{R}{returns}
+
+\item{P}{a K x N pick matrix}
+
+\item{Mu}{vector of length N of the prior expected values. The sample mean
+is used if \code{mu} is not provided as an argument.}
+
+\item{Sigma}{an N x N matrix of the prior covariance matrix. The sample covariance
+is used if \code{Sigma} is not provided as an argument.}
+}
+\value{
+\itemize{
+  \item{BLMu:}{ posterior expected values}
+  \item{BLSigma:}{ posterior covariance matrix}
+}
+}
+\description{
+Compute the Black Litterman estimate of moments for the posterior normal.
+}
+\note{
+This function is largely based on the work of Xavier Valls to port
+the matlab code of Attilio Meucci to \R as documented in the Meucci package.
+}
+\author{
+Ross Bennett, Xavier Valls
+}
+\references{
+A. Meucci - "Exercises in Advanced Risk and Portfolio Management" \url{http://symmys.com/node/170}.
+}
+\seealso{
+\code{\link{BlackLittermanFormula}}
+}
+

Deleted: pkg/PortfolioAnalytics/sandbox/BlackLittermanFormula.R
===================================================================
--- pkg/PortfolioAnalytics/sandbox/BlackLittermanFormula.R	2014-06-19 21:35:29 UTC (rev 3427)
+++ pkg/PortfolioAnalytics/sandbox/BlackLittermanFormula.R	2014-06-19 22:32:13 UTC (rev 3428)
@@ -1,30 +0,0 @@
-#' @title Computes the Black-Litterman formula for the moments of the posterior normal.
-#'
-#' @description This function computes the Black-Litterman formula for the moments of the posterior normal, as described in  
-#' A. Meucci, "Risk and Asset Allocation", Springer, 2005.
-#' 
-#'   @param		Mu       [vector] (N x 1) prior expected values.
-#'   @param		Sigma    [matrix] (N x N) prior covariance matrix.
-#'   @param		P        [matrix] (K x N) pick matrix.
-#'   @param		v        [vector] (K x 1) vector of views.
-#'   @param		Omega    [matrix] (K x K) matrix of confidence.
-#'
-#'   @return	BLMu     [vector] (N x 1) posterior expected values.
-#'   @return	BLSigma  [matrix] (N x N) posterior covariance matrix.
-#'
-#' @references
-#' A. Meucci - "Exercises in Advanced Risk and Portfolio Management" \url{http://symmys.com/node/170}.
-#'
-#' See Meucci's script for "BlackLittermanFormula.m"
-#'
-#' @author Xavier Valls \email{flamejat@@gmail.com}
-#' @export
-
-BlackLittermanFormula = function( Mu, Sigma, P, v, Omega)
-{
-	BLMu    = Mu + Sigma %*% t( P ) %*% ( solve( P %*% Sigma %*% t( P ) + Omega ) %*% ( v - P %*% Mu ) );
-	BLSigma =  Sigma -  Sigma %*% t( P ) %*% ( solve( P %*% Sigma %*% t( P ) + Omega ) %*% ( P %*% Sigma ) );
-
-	return( list( BLMu = BLMu , BLSigma = BLSigma ) );
-
-}
\ No newline at end of file



More information about the Returnanalytics-commits mailing list