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

5

我希望能够计算一个特征函数已知的分布的密度函数。以正态分布为简单例子。

norm.char<-function(t,mu,sigma) exp((0+1i)*t*mu-0.5*sigma^2*t^2)

然后我想使用R的fft函数。但是我无法正确获取乘法常数,而且我必须重新排序结果(取值的第二半部分,然后是第一半)。我尝试了类似于

的东西。

 xmax = 5
 xmin = -5
 deltat = 2*pi/(xmax-xmin)
 N=2^8
 deltax = (xmax-xmin)/(N-1)
 x = xmin + deltax*seq(0,N-1)
 t = deltat*seq(0,N-1)
 density = Re(fft(norm.char(t*2*pi,mu,sigma)))
 density = c(density[(N/2+1):N],density[1:(N/2)])

但这仍然不正确。有人知道在R中与密度计算相关的fft的好参考资料吗?显然问题在于连续FFT和离散FFT的混合。有人可以推荐一个步骤吗? 谢谢


1
“density”函数的帮助页面说它使用FFT。为什么不检查一下代码呢? - IRTFM
什么确切地说是不正确的?如果您真正的问题只是“在离散傅里叶变换期间应用了哪些常数?”,那么请查看fft的帮助页面,我相信其中给出了方程式。 - Carl Witthoft
1个回答

11

这很繁琐:拿起笔和纸,写下你想计算的积分(特征函数的傅里叶变换),将其离散化,并重新编写项,使其看起来像离散傅里叶变换(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)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接