Learn R Programming

custom.gauss.quad (version 1.0.0)

moments: Moments Computed in Multiple Precision Using the Package Rmpfr

Description

This module computes the r'th moment $$\int_{-\infty}^{\infty} x^r f(x) dx,$$ where \(f\) is the weight function (specified by the list which.f), for any nonnegative integer r using nbits bits of precision for its computation, via the R package Rmpfr.

Usage

moments(which.f, r, nbits)

Value

The r'th moment with number of bits of precision nbits used in its computation, via the R package Rmpfr

Arguments

which.f

a list specifying the nonnegative integrable weight function \(f\), with the following 3 components: (i) name (in the form of a character string), (ii) support specified by a 2-vector of the endpoints of the interval, (iii) parameter vector when \(f\) belongs to a family of weight functions and is specified by the value of this parameter vector (if \(f\) is already fully specified then the parameter vector is set to NULL)

r

nonnegative integer, specifying that it is the r'th moment for the weight function \(f\) that is to be computed

nbits

number of bits in the multiple precision numbers used by the R package Rmpfr to carry out the computation of the r'th moment

Details

Suppose, for example, that we wish to find the Gauss quadrature nodes and weights for the weight function \(f\) that is the probability density function of a random variable with the same distribution as \(R/m^{1/2}\) where \(R\) has a \(\chi_m\) distribution (i.e. \(R^2\) has a \(\chi_m^2\) distribution). In this case, the r'th moment is $$\int_{-\infty}^{\infty} x^r f(x) dx = \left(\frac{2}{m} \right)^{r/2} \frac{\Gamma((r+m)/2)}{\Gamma(m/2)},$$ which can be computed to an arbitrary number of bits of precision nbits using the R package Rmpfr. In this case, we specify this weight function \(f\) by first assigning the value of m and then using the R command

which.f <- list(name="scaled.chi.pdf", support=c(0, Inf), parameters=m)

The code within the function moments used to compute the \(r\)'th moment, to an arbitrary number of bits of precision nbits using the package Rmpfr, is listed in the Examples section.

See Also

custom

Examples

Run this code
# The code for the function moments must include a section
# that computes the r th moment to an arbitrary number of bits
# of precision nbits using the R package Rmpfr for the particular
# weight function f of interest.
# Suppose that the weight function f is the probability density
# function of a random variable with the same probability
# distribution as R divided by the square root of m, where R has a
# chi distribution with m degrees of freedom.
# The code for the function moments includes the following:
#
#    if (which.f$name == "scaled.chi.pdf"){
#    m <- which.f$parameters
#    if (r == 0){
#    return(mpfr(1, nbits))
#    }
#    mp.2 <- mpfr(2, nbits)
#    mp.r <- mpfr(r, nbits)
#    mp.m <- mpfr(m, nbits)
#    term1 <- (mp.r/ mp.2) * log(mp.2 / mp.m)
#    term2 <- lgamma((mp.r + mp.m) / mp.2)
#    term3 <- lgamma(mp.m / mp.2)
#    return(exp(term1 + term2 - term3))
#    }

Run the code above in your browser using DataLab