twomoments: Estimate the mean and covariance of values.

Description Usage Arguments Details Value Author(s) See Also Examples

View source: R/twomoments.r

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

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

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.

Details

Given a n x k_1 x k_2 ... x k_l array whose 'rows' are independent observations of X, computes the k_1 x k_2 x ... x k_l array of the mean of X and the k_1 x k_2 x ... x k_l x k_1 x 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.

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.

Author(s)

Steven E. Pav shabbychef@gmail.com

See Also

theta

Examples

1
2
3
4
5
6
7
8
9
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)

shabbychef/madness documentation built on April 11, 2021, 11:03 p.m.