Learn R Programming

madness (version 0.2.8)

twomoments: Estimate the mean and covariance of values.

Description

Given rows of observations of some vector (or multidimensional data), estimates the mean and covariance of the values, returning two madness objects. These have a common covariance and 'xtag', so can be combined together.

Usage

twomoments(X, diag.only=FALSE, vcov.func=vcov, xtag=NULL, df=NULL)

Value

A two element list. When diag.only=FALSE, the first element of the list is mu, representing the mean, a madness object, the second is Sigma, representing the covariance, also a madness object. When diag.only=TRUE, the first element is mu, but the second is sigmasq, a madness object representing the diagonal of the covariance matrix.

Arguments

X

a multidimensional array (or a data frame) of observed values.

diag.only

logical flag, defaulting to FALSE. When TRUE, only the diagonal of the covariance is computed, and returned instead of the entire covariance. This should be used for reasons of efficiency when only the marginal variances are needed.

vcov.func

a function which takes an object of class lm, and computes a variance-covariance matrix. If equal to the string "normal", we assume multivariate normal returns.

xtag

an optional string tag giving the name of the input data. defaults to figuring it out from the input expression.

df

the number of degrees of freedom to subtract from the sample size in the denominator of the covariance matrix estimate. The default value is the number of elements in the mean, the so-called Bessel's correction.

Author

Steven E. Pav shabbychef@gmail.com

Details

Given a \(n\times k_1 \times k_2 \times ... \times k_l\) array whose 'rows' are independent observations of \(X\), computes the \(k_1 \times k_2 \times ... \times k_l\) array of the mean of \(X\) and the \(k_1 \times k_2 \times ... \times k_l \times k_1 \times k_2 ... k_l\) array of the covariance, based on \(n\) observations, returned as two madness objects. The variance-covariance of each is estimated. The two objects have the same 'xtag', and so may be combined together. When the diag.only=TRUE, only the diagonal of the covariance is computed and returned.

One may use the default method for computing covariance, via the vcov function, or via a 'fancy' estimator, like sandwich:vcovHAC, sandwich:vcovHC, etc.

See Also

theta

Examples

Run this code
set.seed(123)
X <- matrix(rnorm(1000*8),ncol=8)
alst <- twomoments(X)
markowitz <- solve(alst$Sigma,alst$mu)
vcov(markowitz)

# now compute the Sharpe ratios:
alst <- twomoments(X,diag.only=TRUE,df=1)
srs <- alst$mu / sqrt(alst$sigmasq)

Run the code above in your browser using DataLab