1#' @title Computes the Black-Litterman formula for the moments of the posterior normal.2#'3#' @description This function computes the Black-Litterman formula for the moments of the posterior normal, as described in4#' A. Meucci, "Risk and Asset Allocation", Springer, 2005.5#'6#' @param Mu [vector] (N x 1) prior expected values.7#' @param Sigma [matrix] (N x N) prior covariance matrix.8#' @param P [matrix] (K x N) pick matrix.9#' @param v [vector] (K x 1) vector of views.10#' @param Omega [matrix] (K x K) matrix of confidence.11#'12#' @return BLMu [vector] (N x 1) posterior expected values.13#' @return BLSigma [matrix] (N x N) posterior covariance matrix.14#'15#' @references16#' A. Meucci - "Exercises in Advanced Risk and Portfolio Management" \url{https://www.arpm.co/articles/exercises-in-advanced-risk-and-portfolio-management/}.17#'18#' See Meucci's script for "BlackLittermanFormula.m"19#'20#' @author Xavier Valls \email{flamejat@@gmail.com}21BlackLittermanFormula = function( Mu, Sigma, P, v, Omega)22{23BLMu = Mu + Sigma %*% t( P ) %*% ( solve( P %*% Sigma %*% t( P ) + Omega ) %*% ( v - P %*% Mu ) );24BLSigma = Sigma - Sigma %*% t( P ) %*% ( solve( P %*% Sigma %*% t( P ) + Omega ) %*% ( P %*% Sigma ) );2526return( list( BLMu = BLMu , BLSigma = BLSigma ) );2728}2930#' Black Litterman Estimates31#'32#' Compute the Black Litterman estimate of moments for the posterior normal.33#'34#' @note This function is largely based on the work of Xavier Valls to port35#' the matlab code of Attilio Meucci to \R as documented in the Meucci package.36#'37#' @param R returns38#' @param P a K x N pick matrix39#' @param Mu vector of length N of the prior expected values. The sample mean40#' is used if \code{Mu=NULL}.41#' @param Sigma an N x N matrix of the prior covariance matrix. The sample42#' covariance is used if \code{Sigma=NULL}.43#' @param Views a vector of length K of the views44#' @return \describe{45#' \item{BLMu:}{ posterior expected values}46#' \item{BLSigma:}{ posterior covariance matrix}47#' }48#' @author Ross Bennett, Xavier Valls49#' @references50#' A. Meucci - "Exercises in Advanced Risk and Portfolio Management" \url{https://www.arpm.co/articles/exercises-in-advanced-risk-and-portfolio-management/}.51#' @seealso \code{\link{BlackLittermanFormula}}52#' @export53black.litterman <- function(R, P, Mu=NULL, Sigma=NULL, Views=NULL){5455# Compute the sample estimate if mu is null56if(is.null(Mu)){57Mu <- colMeans(R)58}59if(length(Mu) != NCOL(R)) stop("length of Mu must equal number of columns of R")6061# Compute the sample estimate if sigma is null62if(is.null(Sigma)){63Sigma <- cov(R)64}65if(!all(dim(Sigma) == NCOL(R))) stop("dimensions of Sigma must equal number of columns of R")6667# Compute the Omega matrix and views value68Omega = tcrossprod(P %*% Sigma, P)69if(is.null(Views)) Views = as.numeric(sqrt( diag( Omega ) ))70B = BlackLittermanFormula( Mu, Sigma, P, Views, Omega )71return(B)72}73747576