dsocs: Distribution of a Linear Combination of Chi-Squared Variables

View source: R/socs.R

dsocsR Documentation

Distribution of a Linear Combination of Chi-Squared Variables

Description

Probability density, cumulative probability distribution function, quantile function, and random generation for a linear combination of \chi^2 random variables.

Usage

dsocs(x, lambda, log= FALSE, method=c("Wood", "Farebrother"))
psocs(q, lambda, lower.tail=TRUE, log.p=FALSE, method=c("Wood", "Farebrother"))
qsocs(p, lambda, lower.tail=TRUE, log.p=FALSE, method=c("Wood", "Farebrother")) 
rsocs(n, lambda, approx=FALSE)

Arguments

x, q

Numeric vector of quantiles

p

Numeric vector of probabilities

n

Integer. Number of random values to generate.

lambda

Numeric vector of coefficients of the linear combination.

lower.tail

Logical value. If TRUE (the default), probabilities are P[X \le x]; otherwise, P[X > x]/

log, log.p

Logical value. If TRUE, probabilities are given as natural logarithms \log(p).

approx

Logical value specifying whether to generate random values from the approximate distribution (approx=TRUE) or according to the exact distribution (approx=FALSE, the default).

method

Character string (partially matched) specifying whether to use Wood's approximation (method="Wood", the default) or Farebrother's numerically exact algorithm (method="Farebrother").

Details

These functions calculate the probability density (dsocs), cumulative distribution function (psocs) and quantile function (qsocs), and generate random realisations (rsocs) of, a random variable Y such that

Y = \sum_{j=1^m} \lambda_j X_j^2

where X_1,\ldots,X_m are independent, identically distributed standard normal random variables, and \lambda_1,\ldots,\lambda_m are nonnegative coefficients. Thus, Y is a linear combination of independent \chi^2 random variables each having 1 degree of freedom.

The argument lambda contains the coefficients \lambda_1,\ldots,\lambda_m.

  • if method="Wood" (the default), the approximation of Wood (1989) is used: the distribution of Y is approximated by an F distribution, except in marginal cases when it is approximated by a gamma distribution or inverse-gamma distribution. This method is faster.

  • if method="Farebrother", the numerical algorithm of Farebrother (1984) is used. The cumulative distribution function and the probability density are evaluated to a high level of precision using a truncated infinite series. The quantile function is evaluated by root-finding.

For the random generator rsocs, the default behaviour is to generate realisations of Y according to its exact distribution by constructing \chi^2 random variables and summing. If approx=TRUE, then the realisations are generated from the (Wood's method) approximate distribution instead.

Value

A numeric vector.

Details of computation

For the probability density dsocs, distribution function psocs and quantile function qsocs:

  • if method="Wood" (the default), the probability density, distribution function and quantiles are computed using the relevant functions from the stats package: df, pf, qf or dgamma, pgamma, qgamma, using the parameters calculated by Wood's approximation. This method is faster, and the stats package functions have been thoroughly checked to ensure logically consistent behaviour for extreme values of the arguments.

  • if method="Farebrother", the probability density and distribution function are calculated to high precision (such that the error in the probability value is less than 1e-6) by Farebrother's (1984) algorithm as implemented in farebro. Quantiles are evaluated by root-finding using uniroot with a tolerance of 1e-6 for the discrepancy (the difference between the desired probability and the probability calculated by Farebrother's algorithm) so that the true error in the probability value is less than 2e-6. If either the Farebrother algorithm or uniroot reports an error, the computation falls back on Wood's approximation.

For the random generator rsocs,

  • if approx=FALSE (the default), exact realisations of Y are generated by generating standard normal random variables using rnorm, squaring, and summing with the weights lambda.

  • if approx=TRUE, realisations of the approximate distribution are generated using the relevant function rf or rgamma with the parameters calculated by Wood's approximation.

Author(s)

\adrian

.

References

Farebrother, R.W. (1984) Algorithm AS 204: The distribution of a positive linear combination of \chi^2 random variables. Applied Statistics 33 (3) 332–339.

Wood, A.T.A (1989) An F approximation to the distribution of a linear combination of chi-squared variables. Communications in Statistics – Simulation and Computation 18:4, 1439–1456.

See Also

pf, pgamma, farebro, uniroot for underlying algorithms.

Examples

   online <- interactive()
   N <- if(online) 1000 else 50
   co <- c(1, 0.5, 0.3)
   xx <- rsocs(N, co)
   plot(density(xx))
   curve(dsocs(x, co), add=TRUE, col=8, lwd=5)
   curve(dsocs(x, co, method="F"), add=TRUE, col=2)
   qqplot(xx, qsocs(ppoints(N), co))
   abline(0,1)
   plot(ppoints(N), psocs(sort(xx), co))
   abline(0,1)

   ## accuracy of quantiles: Wood (1989) table II, case I
   lam <- c(0.5, rep(0.1, 5))
   pp <- if(online) {
           c(0.01, 0.025, 0.05, 0.1, 0.25, 0.75, 0.9, 0.95, 0.975, 0.99)
          } else {
           c(0.05, 0.95)
          }
   qe <- qsocs(pp, lam, method="F")
   qw <- qsocs(pp, lam, method="W")
   ta <- rbind(exact=qe, Wood=qw)
   ta <- round(ta, 3)
   colnames(ta) <- paste0(round(100 * pp, 1), "%")
   ta

spatstat.univar documentation built on May 18, 2026, 9:06 a.m.