Skip to content

Commit

Permalink
version 0.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Dean Bodenham authored and gaborcsardi committed Feb 25, 2015
0 parents commit 6fb9e32
Show file tree
Hide file tree
Showing 15 changed files with 859 additions and 0 deletions.
14 changes: 14 additions & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
Package: momentchi2
Title: Moment-Matching Methods for Weighted Sums of Chi-Squared Random
Variables
Version: 0.1.0
Author: Dean Bodenham
Maintainer: Dean Bodenham <dean.bodenham@bsse.ethz.ch>
Description: A collection of moment-matching methods for computing the cumulative distribution function of a positively-weighted sum of chi-squared random variables. Methods include the Satterthwaite-Welch method, Hall-Buckley-Eagleson method, Wood's F method, and the Lindsay-Pilla-Basak method.
Depends: R (>= 3.1.2)
License: GPL-2 | GPL-3
LazyData: true
Packaged: 2015-02-25 10:34:38 UTC; dean
NeedsCompilation: no
Repository: CRAN
Date/Publication: 2015-02-25 11:54:43
14 changes: 14 additions & 0 deletions MD5
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
5ee40ec8495bffa3946acfdc6c39054f *DESCRIPTION
0350d3b800d73c738d77906579aa2495 *NAMESPACE
f3aafbc0da1b35c49183229768cd9f8a *R/hbe.R
7d15925dcbc2d7ec07c809558e743c4d *R/lpb4.R
7f4fd15940c235968698eb1f9f30164f *R/momentchi2.R
e9cc77074cbb03e0ce1d7f063a6421c7 *R/sw.R
03f2e03eb7f6dba4f7afeb34fdf567d0 *R/utils.R
1082e3177e99757c5333d692355760eb *R/wf.R
a87e85635b1d3db3872ab914b7a467fc *README
5805ae87815034699b126bd958e5ff40 *man/hbe.Rd
021af66111e31030b15b5c3bf33e1fc5 *man/lpb4.Rd
fbc798aea43414d5c89e7e379f85eb93 *man/momentchi2.Rd
6ef29cc9b124407db1e4e23b1a07a89f *man/sw.Rd
bd1cd529a995c561698d9b31eb84d49c *man/wf.Rd
6 changes: 6 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Generated by roxygen2 (4.1.0): do not edit by hand

export(hbe)
export(lpb4)
export(sw)
export(wf)
51 changes: 51 additions & 0 deletions R/hbe.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#' Hall-Buckley-Eagleson method
#'
#' Computes the cdf of a positively-weighted sum of chi-squared random variables with the Hall-Buckley-Eagleson (HBE) method.
#' @inheritParams sw
#' @keywords distribution
#' @references
#' \itemize{
#' \item P. Hall. Chi squared approximations to the distribution of a sum of independent random variables. \emph{The Annals of Probability}, 11(4):1028-1036, 1983.
#' \item M. J. Buckley and G. K. Eagleson. An approximation to the distribution of quadratic forms in normal random variables. \emph{Australian Journal of Statistics}, 30(1):150-159, 1988.
#' }
#' @export
#' @examples
#' #Examples taken from Table 18.6 in N. L. Johnson, S. Kotz, N. Balakrishnan.
#' #Continuous Univariate Distributions, Volume 1, John Wiley & Sons, 1994.
#'
#' hbe(c(1.5, 1.5, 0.5, 0.5), 10.203) # should give value close to 0.95
#' hbe(coeff=c(1.5, 1.5, 0.5, 0.5), x=10.203) # specifying parameters
#' hbe(c(1.5, 1.5, 0.5, 0.5), c(0.627, 10.203)) # x is a vector, output approx. 0.05, 0.95

hbe <- function(coeff, x){
if ( (missing(x)) || (missing(coeff)) )
stop("missing an argument - need to specify \"coeff\" and \"x\"")

if (checkCoeffsArePositiveError(coeff))
stop(getCoeffError(coeff))

if (checkXvaluesArePositiveError(x))
stop(getXvaluesError(x))
#end of error checking

#compute cumulants and nu
kappa <- c(sum(coeff), 2*sum(coeff^2), 8*sum(coeff^3) )
K_1 <- sum(coeff)
K_2 <- 2 * sum(coeff^2)
K_3 <- 8 * sum(coeff^3)
nu <- 8 * (K_2^3) / (K_3^2)

#gamma parameters for chi-square
gamma_k <- nu/2
gamma_theta <- 2

#need to transform the actual x value to x_chisqnu ~ chi^2(nu)
#This transformation is used to match the first three moments
#First x is normalised and then scaled to be x_chisqnu
x_chisqnu_vec <- sqrt(2 * nu / K_2) * (x - K_1) + nu

#now this is a chi_sq(nu) variable
p_chisqnu_vec <- pgamma(x_chisqnu_vec, shape=gamma_k, scale=gamma_theta)
return(p_chisqnu_vec)
}

Loading

0 comments on commit 6fb9e32

Please sign in to comment.