Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes for kde #31

Merged
merged 2 commits into from
Apr 7, 2013
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 4 additions & 16 deletions src/kde.jl
Original file line number Diff line number Diff line change
Expand Up @@ -45,26 +45,16 @@ function kde(data::Vector, npoints::Integer)
end

# Transform to Fourier basis
ft = fft(grid)
ft = rfft(grid)

# Find transform of KDE by convolving grid with FT of kernel
# Hardcodes a Gaussian kernel
jmax = npoints2 - 1
smooth = Array(Complex{Float64}, npoints)
smooth[1] = ft[1]
rj = 0.0
for j = 1:jmax
rj = rj + 1.0
fac = exp(-fac1 * rj^2)
j1 = j + 1
j2 = j1 + npoints2
smooth[j1] = fac * ft[j1]
smooth[j2] = fac * ft[j2]
for j = 2:length(ft)
ft[j] *= exp(-fac1 * (j-1)^2)
end
smooth[npoints2 + 1] = exp(-fac1 * npoints^2) * ft[npoints2 + 1]

# Invert the Fourier transform to get the KDE
smooth = real(ifft(smooth))
smooth = irfft(ft,npoints)

# Fix any noise that crept in
for j in 1:npoints
Expand All @@ -74,7 +64,5 @@ function kde(data::Vector, npoints::Integer)
end

# Return the grid over which KDE was calculated and KDE
# TODO: Values of smooth oscillate improperly
# TODO: Values of smooth are on wrong scale
return dlo:step:(dhi - step), smooth
end