Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
braverock
GitHub Repository: braverock/portfolioanalytics
Path: blob/master/R/black_litterman.R
1433 views
1
2
#' @title Computes the Black-Litterman formula for the moments of the posterior normal.
3
#'
4
#' @description This function computes the Black-Litterman formula for the moments of the posterior normal, as described in
5
#' A. Meucci, "Risk and Asset Allocation", Springer, 2005.
6
#'
7
#' @param Mu [vector] (N x 1) prior expected values.
8
#' @param Sigma [matrix] (N x N) prior covariance matrix.
9
#' @param P [matrix] (K x N) pick matrix.
10
#' @param v [vector] (K x 1) vector of views.
11
#' @param Omega [matrix] (K x K) matrix of confidence.
12
#'
13
#' @return BLMu [vector] (N x 1) posterior expected values.
14
#' @return BLSigma [matrix] (N x N) posterior covariance matrix.
15
#'
16
#' @references
17
#' A. Meucci - "Exercises in Advanced Risk and Portfolio Management" \url{https://www.arpm.co/articles/exercises-in-advanced-risk-and-portfolio-management/}.
18
#'
19
#' See Meucci's script for "BlackLittermanFormula.m"
20
#'
21
#' @author Xavier Valls \email{flamejat@@gmail.com}
22
BlackLittermanFormula = function( Mu, Sigma, P, v, Omega)
23
{
24
BLMu = Mu + Sigma %*% t( P ) %*% ( solve( P %*% Sigma %*% t( P ) + Omega ) %*% ( v - P %*% Mu ) );
25
BLSigma = Sigma - Sigma %*% t( P ) %*% ( solve( P %*% Sigma %*% t( P ) + Omega ) %*% ( P %*% Sigma ) );
26
27
return( list( BLMu = BLMu , BLSigma = BLSigma ) );
28
29
}
30
31
#' Black Litterman Estimates
32
#'
33
#' Compute the Black Litterman estimate of moments for the posterior normal.
34
#'
35
#' @note This function is largely based on the work of Xavier Valls to port
36
#' the matlab code of Attilio Meucci to \R as documented in the Meucci package.
37
#'
38
#' @param R returns
39
#' @param P a K x N pick matrix
40
#' @param Mu vector of length N of the prior expected values. The sample mean
41
#' is used if \code{Mu=NULL}.
42
#' @param Sigma an N x N matrix of the prior covariance matrix. The sample
43
#' covariance is used if \code{Sigma=NULL}.
44
#' @param Views a vector of length K of the views
45
#' @return \describe{
46
#' \item{BLMu:}{ posterior expected values}
47
#' \item{BLSigma:}{ posterior covariance matrix}
48
#' }
49
#' @author Ross Bennett, Xavier Valls
50
#' @references
51
#' A. Meucci - "Exercises in Advanced Risk and Portfolio Management" \url{https://www.arpm.co/articles/exercises-in-advanced-risk-and-portfolio-management/}.
52
#' @seealso \code{\link{BlackLittermanFormula}}
53
#' @export
54
black.litterman <- function(R, P, Mu=NULL, Sigma=NULL, Views=NULL){
55
56
# Compute the sample estimate if mu is null
57
if(is.null(Mu)){
58
Mu <- colMeans(R)
59
}
60
if(length(Mu) != NCOL(R)) stop("length of Mu must equal number of columns of R")
61
62
# Compute the sample estimate if sigma is null
63
if(is.null(Sigma)){
64
Sigma <- cov(R)
65
}
66
if(!all(dim(Sigma) == NCOL(R))) stop("dimensions of Sigma must equal number of columns of R")
67
68
# Compute the Omega matrix and views value
69
Omega = tcrossprod(P %*% Sigma, P)
70
if(is.null(Views)) Views = as.numeric(sqrt( diag( Omega ) ))
71
B = BlackLittermanFormula( Mu, Sigma, P, Views, Omega )
72
return(B)
73
}
74
75
76