在R中使用fft从特征函数计算密度

在R中使用fft从特征函数计算密度,r,fft,R,Fft,我想计算一个分布的密度函数,它的特征函数是已知的。作为一个简单的例子,以正态分布为例 norm.char<-function(t,mu,sigma) exp((0+1i)*t*mu-0.5*sigma^2*t^2) 但这仍然是不正确的。有人知道在密度计算中,R中的fft有一个很好的参考吗?显然,问题是连续FFT和离散FFT的混合。有人能推荐一种手术方法吗? 谢谢这很麻烦:拿笔和纸, 写出你想要计算的积分 (特征函数的傅里叶变换), 将其离散化,并重写条款,使其看起来像 离散傅里叶变换(F

我想计算一个分布的密度函数,它的特征函数是已知的。作为一个简单的例子,以正态分布为例

norm.char<-function(t,mu,sigma) exp((0+1i)*t*mu-0.5*sigma^2*t^2)
但这仍然是不正确的。有人知道在密度计算中,R中的fft有一个很好的参考吗?显然,问题是连续FFT和离散FFT的混合。有人能推荐一种手术方法吗?
谢谢

这很麻烦:拿笔和纸, 写出你想要计算的积分 (特征函数的傅里叶变换), 将其离散化,并重写条款,使其看起来像 离散傅里叶变换(FFT)假设间隔开始 零)

请注意,
fft
是一种非规范化变换:没有
1/N
因子

characteristic_function_to_density <- function(
  phi, # characteristic function; should be vectorized
  n,   # Number of points, ideally a power of 2
  a, b # Evaluate the density on [a,b[
) {
  i <- 0:(n-1)            # Indices
  dx <- (b-a)/n           # Step size, for the density
  x <- a + i * dx         # Grid, for the density
  dt <- 2*pi / ( n * dx ) # Step size, frequency space
  c <- -n/2 * dt          # Evaluate the characteristic function on [c,d]
  d <-  n/2 * dt          # (center the interval on zero)
  t <- c + i * dt         # Grid, frequency space
  phi_t <- phi(t)
  X <- exp( -(0+1i) * i * dt * a ) * phi_t
  Y <- fft(X)
  density <- dt / (2*pi) * exp( - (0+1i) * c * x ) * Y
  data.frame(
    i = i,
    t = t,
    characteristic_function = phi_t,
    x = x,
    density = Re(density)
  )
}

d <- characteristic_function_to_density(
  function(t,mu=1,sigma=.5) 
    exp( (0+1i)*t*mu - sigma^2/2*t^2 ),
  2^8,
  -3, 3
)
plot(d$x, d$density, las=1)
curve(dnorm(x,1,.5), add=TRUE)

characteristic\u function\u to\u density
density
函数帮助页上说它使用FFT。为什么不检查代码?到底什么是不正确的?如果你真正的问题仅仅是“离散傅里叶变换中应用了什么常数?”那么请查看帮助页面中的
fft
,我相信它给出了公式。
characteristic_function_to_density <- function(
  phi, # characteristic function; should be vectorized
  n,   # Number of points, ideally a power of 2
  a, b # Evaluate the density on [a,b[
) {
  i <- 0:(n-1)            # Indices
  dx <- (b-a)/n           # Step size, for the density
  x <- a + i * dx         # Grid, for the density
  dt <- 2*pi / ( n * dx ) # Step size, frequency space
  c <- -n/2 * dt          # Evaluate the characteristic function on [c,d]
  d <-  n/2 * dt          # (center the interval on zero)
  t <- c + i * dt         # Grid, frequency space
  phi_t <- phi(t)
  X <- exp( -(0+1i) * i * dt * a ) * phi_t
  Y <- fft(X)
  density <- dt / (2*pi) * exp( - (0+1i) * c * x ) * Y
  data.frame(
    i = i,
    t = t,
    characteristic_function = phi_t,
    x = x,
    density = Re(density)
  )
}

d <- characteristic_function_to_density(
  function(t,mu=1,sigma=.5) 
    exp( (0+1i)*t*mu - sigma^2/2*t^2 ),
  2^8,
  -3, 3
)
plot(d$x, d$density, las=1)
curve(dnorm(x,1,.5), add=TRUE)