2012-04-05 80 views
4

我想计算特征函数已知的分布的密度函数。作为一个简单的例子,采取正态分布。使用fft计算特征函数的密度R

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

'密度'函数帮助页面表示它使用FFT。为什么不查看代码? – 2012-04-05 14:18:43

+0

什么是不正确的?如果你真正的问题只是“在离散傅立叶变换期间应用了什么常数?”然后检查帮助页面上的'fft',我相信给出的方程式。 – 2012-04-05 19:48:38

回答

9

这仅仅是麻烦的:拿一支笔和纸, 写的积分要计算 (傅立叶变换的特征函数), 离散它,并重新写入条款,使它们看起来像 离散傅立叶变换(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)