symbolicMoments: Symbolic calculation of moments

Description Usage Arguments Value Note Examples

View source: R/symbolicMoments.R

Description

Compute the moments of a multivariate variable X = X[1], ..., X[n] and return the formula as quoted expression.

Usage

1
2
symbolicMoments(distribution, missingOrders, mean = NA, cov = NA,
  var = NA, simplify = TRUE)

Arguments

distribution

string specifying the (multivariate) distribution of X. The following values are possible:

  • "zero" sets all moments to 0,

  • "normal" calculates the moments of a centralized multivariate normal distribution,

  • "lognormal" calculates the raw moments of a multivariate lognormal distribution,

  • "gamma" calculates the raw moments of a multivariate gamma distribution,

  • "NA" sets all moments to NA.

missingOrders

numeric vector or matrix. Each row gives the order of a moment that shall be calculated.

mean

vector or list with expected values. Entry mean[[i]] should contain a value for E(X[i]).

cov

matrix or nested list. Entry cov[i][j] (or cov[[i]][[j]] respectivly) should contain a value for the covariance Cov(X[i], X[j]).

var

optional. If n = 1, cov would only contain one entry - the variance Var(X). This value can be passed to parameter var instead of cov.

simplify

bool indiciating if the resulting expressions should be simplified. Function Simplify from package Deriv is used for simplification.

Value

A list where each element is a quoted expression. The i-th element of this list gives a formula for the moment whose order is given in the i-th row of missingOrders. If simplify = TRUE, the returned value may as well be a vector or number.

Note

The calculation of the central moments of a multivariate normal distribution is based on function callmultmoments of package symmoments. If the calculation for a multivariate gamma distribution leads to NaNs, then the values of cov and mean do not fit to a gamma distribution. All entries of cov should be positive and the diagonals should be large with respect to the other entries. More specifically, the inequations

mean[i] > sum_(k != i) mean[k]*cov[i,k]/cov[i,i]

should be satisfied for all i in 1:n.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# raw moments of a one dimensional gamma distribution
symbolicMoments(distribution = "gamma", missingOrders = as.matrix(1:3, ncol = 1),
                mean = "μ", var = "σ")

# raw moments of a one dimensional lognormal distribution 
symbolicMoments(distribution = "lognormal", missingOrders = as.matrix(1:2, ncol = 1),
                mean = 2, var = 1, simplify = FALSE)

# evaluate the result
symbolicMoments(distribution = "lognormal", missingOrders = as.matrix(1:2, ncol = 1),
                mean = 2, var = 1, simplify = TRUE)

#### central moments of a four dimensional normal distribution ####

missingOrders <- matrix(c(4, 0, 0, 0,
                          3, 1, 0, 0,
                          2, 2, 0, 0,
                          2, 1, 1, 0,
                          1, 1, 1, 1), ncol = 4, byrow = TRUE)

cov <-  matrix(c("σ11", "σ12", "σ13", "σ14", 
                 "σ12", "σ22", "σ23", "σ24", 
                 "σ13", "σ23", "σ33", "σ34", 
                 "σ14", "σ24", "σ34", "σ44"), ncol = 4, byrow = TRUE)

symbolicMoments("normal", missingOrders, mean = "μ", cov = cov)

CharlotteJana/momcalc documentation built on Oct. 17, 2019, 7:21 a.m.