From a4d7a65d5a61151cb9042a173545bb7fa1759456 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Pelletier?= Date: Sat, 1 Feb 2014 23:07:49 -0500 Subject: [PATCH] Ajout de la fonction cftocdf.R Corrections mineures fft.density.R --- R/cftocdf.R | 38 ++++++++++++++++++++++++++++++++++++++ R/fft.density.R | 11 ++++++----- 2 files changed, 44 insertions(+), 5 deletions(-) create mode 100644 R/cftocdf.R diff --git a/R/cftocdf.R b/R/cftocdf.R new file mode 100644 index 0000000..2cddb6b --- /dev/null +++ b/R/cftocdf.R @@ -0,0 +1,38 @@ +# Get distribution function from characteristic function +# +# Author: François Pelletier +############################################################################### + + +#' Get distribution function from characteristic function +#' +#' @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) +{ + 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 + if(!MSwindows) + { + unlist(mclapply(grid, + function(x) 1/2-1/pi* + integrate(integrand,wmin,wmax,x,char.fun,param)$value)) + } + else + { + warning("For faster calculations, use a POSIX compatible Operating System") + Fx <- numeric(n) + for(i in 1:n) + { + Fx[i] <- 1/2-1/pi*integrate(integrand,wmin,wmax,x,char.fun,param)$value + } + } +} diff --git a/R/fft.density.R b/R/fft.density.R index ca4e257..f726633 100644 --- a/R/fft.density.R +++ b/R/fft.density.R @@ -1,21 +1,22 @@ -# Get density from characteristic function +# Get density function from characteristic function using FFT # # Author: François Pelletier ############################################################################### -#' Get density from characteristic function +#' Get density function from characteristic function using FFT #' #' @param char.fun Vectorized characteristic function #' @param n Amount of discretization points #' @param min Lower bound for density function #' @param max Upper bound for density function +#' @param param Characteristic function parameters #' @return A data.frame object containing #' @return transform.grid: transform variate grid #' @return char.fun.t: characteristic function evaluated at t #' @return density.grid: density function grid -#' @return density.value: density function evaluated at point x +#' @return density.value: density function evaluated on [min,max] range #' @author François Pelletier -fft.density <- function(char.fun,n,min,max,...) +cftodensity.fft <- function(char.fun,n,min,max,param) { index <- 0:(n-1) # Index density.step <- (max-min)/n # Step for density function @@ -24,7 +25,7 @@ fft.density <- function(char.fun,n,min,max,...) lbound.char.fun <- -n/2 * transform.step # Evaluate characteristic function on range [c,d] ubound.char.fun <- n/2 * transform.step # Range centered at 0 transform.grid <- lbound.char.fun + index * transform.step # Grid for transform variate - char.fun.t <- char.fun(transform.grid,...) # Evaluate characteristifc function + char.fun.t <- char.fun(transform.grid,param) # Evaluate characteristifc function tilted.char.fun.t <- exp( -1i * index * transform.step * min ) * char.fun.t # Tilt characteristic function density.value <- Re(transform.step / (2*pi) * exp( - 1i * lbound.char.fun * density.grid ) * fft(tilted.char.fun.t)) #Use FFT to get density value, then untilt and normalize data.frame(transform.grid,char.fun.t,density.grid,density.value)