Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
braverock
GitHub Repository: braverock/portfolioanalytics
Path: blob/master/R/meucci_moments.R
1433 views
1
2
3
#' Compute moments
4
#'
5
#' Compute the first and second moments using the Fully Flexible Views
6
#' framework as described in A. Meucci - "Fully Flexible Views: Theory and Practice".
7
#'
8
#' @param R xts object of asset returns
9
#' @param posterior_p vector of posterior probabilities
10
#' @return a list with the first and second moments
11
#' \describe{
12
#' \item{\code{mu}: }{vector of expected returns}
13
#' \item{\code{sigma}: }{covariance matrix}
14
#' }
15
#' @references
16
#' A. Meucci - "Fully Flexible Views: Theory and Practice".
17
#' @author Ross Bennett
18
#' @export
19
meucci.moments <- function(R, posterior_p){
20
R = coredata(R)
21
# expected return vector
22
mu = t(R) %*% posterior_p
23
24
# covariance matrix
25
Scnd_Mom = t(R) %*% (R * (posterior_p %*% matrix( 1, 1, ncol(R))))
26
Scnd_Mom = ( Scnd_Mom + t(Scnd_Mom) ) / 2
27
sigma = Scnd_Mom - mu %*% t(mu)
28
list(mu=mu, sigma=sigma)
29
}
30
31