Ajout de la fonction cftocdf.R

Corrections mineures fft.density.R
This commit is contained in:
François Pelletier 2014-02-01 23:07:49 -05:00
parent 4749acaf87
commit a4d7a65d5a
2 changed files with 44 additions and 5 deletions

38
R/cftocdf.R Normal file
View file

@ -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
}
}
}

View file

@ -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)