Skip to content

relaxed types for use with Array{TrackedReal} #78

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions src/bivariate.jl
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ mutable struct BivariateKDE{Rx<:AbstractRange,Ry<:AbstractRange} <: AbstractKDE
"Second coordinate of gridpoints for evaluating the density."
y::Ry
"Kernel density at corresponding gridpoints `Tuple.(x, permutedims(y))`."
density::Matrix{Float64}
density::AbstractMatrix{}
end

function kernel_dist(::Type{D},w::Tuple{Real,Real}) where D<:UnivariateDistribution
Expand Down Expand Up @@ -54,7 +54,7 @@ function tabulate(data::Tuple{RealVector, RealVector}, midpoints::Tuple{Rx, Ry},
sx, sy = step(xmid), step(ymid)

# Set up a grid for discretized data
grid = zeros(Float64, nx, ny)
grid = zeros(eltype(xdata),nx,ny)
ainc = 1.0 / (sum(weights)*(sx*sy)^2)

# weighted discretization (cf. Jones and Lotwick)
Expand Down Expand Up @@ -91,11 +91,12 @@ function conv(k::BivariateKDE, dist::Tuple{UnivariateDistribution,UnivariateDist
ft[i+1,j+1] *= cf(distx,i*cx)*cf(disty,min(j,Ky-j)*cy)
end
end
dens = irfft(ft, Kx)
# i = 0:size(ft,1)-1
# j = 0:size(ft,2)-1
# ft = ft .* ( cf.(distx,i*cx) * cf.(disty,min.(j,Ky-j)*cy)' )

for i = 1:length(dens)
dens[i] = max(0.0,dens[i])
end
dens = irfft(ft, Kx)
dens = max.(0.0,dens)

# Invert the Fourier transform to get the KDE
BivariateKDE(k.x, k.y, dens)
Expand Down
20 changes: 5 additions & 15 deletions src/univariate.jl
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ mutable struct UnivariateKDE{R<:AbstractRange} <: AbstractKDE
"Gridpoints for evaluating the density."
x::R
"Kernel density at corresponding gridpoints `x`."
density::Vector{Float64}
density::AbstractVector{}
end

# construct kernel from bandwidth
Expand Down Expand Up @@ -101,7 +101,7 @@ function tabulate(data::RealVector, midpoints::R, weights::Weights=default_weigh
s = step(midpoints)

# Set up a grid for discretized data
grid = zeros(Float64, npoints)
grid = zeros(eltype(data),npoints)
ainc = 1.0 / (sum(weights)*s*s)

# weighted discretization (cf. Jones and Lotwick)
Expand All @@ -125,22 +125,12 @@ function conv(k::UnivariateKDE, dist::UnivariateDistribution)
K = length(k.density)
ft = rfft(k.density)

# Convolve fft with characteristic function of kernel
# empirical cf
# = \sum_{n=1}^N e^{i*t*X_n} / N
# = \sum_{k=0}^K e^{i*t*(a+k*s)} N_k / N
# = e^{i*t*a} \sum_{k=0}^K e^{-2pi*i*k*(-t*s*K/2pi)/K} N_k / N
# = A * fft(N_k/N)[-t*s*K/2pi + 1]
c = -twoπ/(step(k.x)*K)
for j = 0:length(ft)-1
ft[j+1] *= cf(dist,j*c)
end
j = 0:length(ft)-1
ft = ft .* cf.(dist,j*c)

dens = irfft(ft, K)
# fix rounding error.
for i = 1:K
dens[i] = max(0.0,dens[i])
end
dens = max.(0.0,dens)

# Invert the Fourier transform to get the KDE
UnivariateKDE(k.x, dens)
Expand Down