diff --git a/R/cftocdf.R b/R/cftocdf.R index 2cddb6b..b6653f6 100644 --- a/R/cftocdf.R +++ b/R/cftocdf.R @@ -6,25 +6,25 @@ #' Get distribution function from characteristic function #' +#' @param grid Distribution function evaluation points #' @param char.fun Vectorized characteristic function -#' @param n Amount of discretization points -#' @param min Lower bound for distribution function -#' @param max Upper bound for distribution function #' @param param Characteristic function parameters #' @param wmin Lower bound for transform variate #' @param wmax Upper bound for transform variate #' @return Distribution function values evaluated on [min,max] range #' @author François Pelletier -cftocdf <- function(char.fun,n,min,max,param,wmin=0,wmax=50,MSwindows=FALSE) +cftocdf <- function(grid,char.fun,...,wmin=0,wmax=50,MSwindows=FALSE) { - grid <- seq(from=min,to=max,length.out=n) - integrand <- function(w,x,char.fun,param,wmax=50) (1-w/wmax)* - Im(exp(-1i*w*x)*char.fun(w,param)) / w + n <- length(grid) + # Integral in the Gil-Pelaez Ttheorem + integrand <- function(w,x,char.fun,...,wmax=50) (1-w/wmax)* + Im(exp(-1i*w*x)*char.fun(w,...)) / w + # Integrate for each grid point using parallel computation if available if(!MSwindows) { unlist(mclapply(grid, function(x) 1/2-1/pi* - integrate(integrand,wmin,wmax,x,char.fun,param)$value)) + integrate(integrand,wmin,wmax,x,char.fun,...)$value)) } else { @@ -32,7 +32,7 @@ cftocdf <- function(char.fun,n,min,max,param,wmin=0,wmax=50,MSwindows=FALSE) Fx <- numeric(n) for(i in 1:n) { - Fx[i] <- 1/2-1/pi*integrate(integrand,wmin,wmax,x,char.fun,param)$value + Fx[i] <- 1/2-1/pi*integrate(integrand,wmin,wmax,x,char.fun,...)$value } } } diff --git a/R/chisquare.test.R b/R/chisquare.test.R new file mode 100644 index 0000000..7d2c0e6 --- /dev/null +++ b/R/chisquare.test.R @@ -0,0 +1,26 @@ +# Pearson's Chi-Squared test based on the characteristic function +# +# Author: François Pelletier +############################################################################### + +#' Pearson's Chi-Squared test based on the characteristic function +#' @param DATA.hist histogram object of the data +#' @param char.fun Characteristic function +#' @param ... Characteristic function arguments +#' @param alpha tolerance level +#' +#' @return A list containing the chi-square statistic, +#' degree of freedom, hypothesis reject boolean and p.value +#' @author François Pelletier +chisquare.test <- function(DATA.hist,char.fun,...,alpha=0.05) +{ + # Compute expected values for each histogram breaks using the characteristic function + expected <- diff(cftocdf(classes <- DATA.hist$breaks,char.fun,...)* + sum(observed <- DATA.hist$counts)) + # Compute the test statistic using chi-square distribution + p.value <- pchisq(chisquare.stat<-sum((observed-expected)^2/expected), + df<-length(classes)-2,lower.tail=FALSE) + # Create the return list + list(chisquare.stat=chisquare.stat,df=df,reject=p.value