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

fix: kernel functions #853

Merged
merged 6 commits into from
Sep 19, 2023
Merged
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
27 changes: 16 additions & 11 deletions src/collocation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -36,26 +36,26 @@
end

function calckernel(::QuarticKernel,t)
if abs(t)>0
if abs(t) > 1

Check warning on line 39 in src/collocation.jl

View check run for this annotation

Codecov / codecov/patch

src/collocation.jl#L39

Added line #L39 was not covered by tests
return 0
else
return (15*(1-t^2)^2)/16
end
end

function calckernel(::TriweightKernel,t)
if abs(t)>0
if abs(t) > 1

Check warning on line 47 in src/collocation.jl

View check run for this annotation

Codecov / codecov/patch

src/collocation.jl#L47

Added line #L47 was not covered by tests
return 0
else
return (35*(1-t^2)^3)/32
end
end

function calckernel(::TricubeKernel,t)
if abs(t)>0
if abs(t) > 1

Check warning on line 55 in src/collocation.jl

View check run for this annotation

Codecov / codecov/patch

src/collocation.jl#L55

Added line #L55 was not covered by tests
return 0
else
return (70*(1-abs(t)^3)^3)/80
return (70*(1-abs(t)^3)^3)/81

Check warning on line 58 in src/collocation.jl

View check run for this annotation

Codecov / codecov/patch

src/collocation.jl#L58

Added line #L58 was not covered by tests
end
end

Expand All @@ -64,7 +64,7 @@
end

function calckernel(::CosineKernel,t)
if abs(t)>0
if abs(t) > 1

Check warning on line 67 in src/collocation.jl

View check run for this annotation

Codecov / codecov/patch

src/collocation.jl#L67

Added line #L67 was not covered by tests
return 0
else
return (π*cos(π*t/2))/4
Expand Down Expand Up @@ -92,14 +92,14 @@
end

function construct_w(t,tpoints,h,kernel)
W = @. calckernel((kernel,),(tpoints-t)/h)/h
W = @. calckernel((kernel,),((tpoints-t)/(tpoints[end]-tpoints[begin]))/h)/h

Check warning on line 95 in src/collocation.jl

View check run for this annotation

Codecov / codecov/patch

src/collocation.jl#L95

Added line #L95 was not covered by tests
Diagonal(W)
end


"""
```julia
u′,u = collocate_data(data,tpoints,kernel=SigmoidKernel())
u′,u = collocate_data(data,tpoints,kernel=TriangularKernel(),bandwidth=nothing)
u′,u = collocate_data(data,tpoints,tpoints_sample,interp,args...)
```

Expand Down Expand Up @@ -128,24 +128,29 @@
data from intermediate timesteps. In this case, pass any of the methods like
`QuadraticInterpolation` as `interp`, and the timestamps to sample from as `tpoints_sample`.
"""
function collocate_data(data,tpoints,kernel=TriangularKernel())
function collocate_data(data, tpoints, kernel=TriangularKernel(), bandwidth=nothing)

Check warning on line 131 in src/collocation.jl

View check run for this annotation

Codecov / codecov/patch

src/collocation.jl#L131

Added line #L131 was not covered by tests
_one = oneunit(first(data))
_zero = zero(first(data))
e1 = [_one;_zero]
e2 = [_zero;_one;_zero]
n = length(tpoints)
h = (n^(-1/5))*(n^(-3/35))*((log(n))^(-1/16))
bandwidth = isnothing(bandwidth) ? (n^(-1/5))*(n^(-3/35))*((log(n))^(-1/16)) : bandwidth

Check warning on line 137 in src/collocation.jl

View check run for this annotation

Codecov / codecov/patch

src/collocation.jl#L137

Added line #L137 was not covered by tests

Wd = similar(data, n, size(data,1))
WT1 = similar(data, n, 2)
WT2 = similar(data, n, 3)
T2WT2 = similar(data, 3, 3)
T1WT1 = similar(data, 2, 2)

Check warning on line 143 in src/collocation.jl

View check run for this annotation

Codecov / codecov/patch

src/collocation.jl#L142-L143

Added lines #L142 - L143 were not covered by tests
x = map(tpoints) do _t
T1 = construct_t1(_t,tpoints)
T2 = construct_t2(_t,tpoints)
W = construct_w(_t,tpoints,h,kernel)
W = construct_w(_t,tpoints,bandwidth,kernel)

Check warning on line 147 in src/collocation.jl

View check run for this annotation

Codecov / codecov/patch

src/collocation.jl#L147

Added line #L147 was not covered by tests
mul!(Wd,W,data')
mul!(WT1,W,T1)
mul!(WT2,W,T2)
mul!(T2WT2,T2',WT2)
mul!(T1WT1,T1',WT1)
(det(T2WT2) ≈ 0.0 || det(T1WT1) ≈ 0.0) && error("Collocation failed with bandwidth $bandwidth. Please choose a higher bandwidth")

Check warning on line 153 in src/collocation.jl

View check run for this annotation

Codecov / codecov/patch

src/collocation.jl#L151-L153

Added lines #L151 - L153 were not covered by tests
(e2'*((T2'*WT2)\T2'))*Wd,(e1'*((T1'*WT1)\T1'))*Wd
end
estimated_derivative = reduce(hcat,transpose.(first.(x)))
Expand All @@ -163,7 +168,7 @@
tpoints_sample::AbstractVector{T},interp,args...) where T
u = zeros(T,size(data, 1),length(tpoints_sample))
du = zeros(T,size(data, 1),length(tpoints_sample))
for d1 in 1:size(data,1)
for d1 in axes(data, 1)

Check warning on line 171 in src/collocation.jl

View check run for this annotation

Codecov / codecov/patch

src/collocation.jl#L171

Added line #L171 was not covered by tests
interpolation = interp(data[d1,:],tpoints,args...)
u[d1,:] .= interpolation.(tpoints_sample)
du[d1,:] .= DataInterpolations.derivative.((interpolation,), tpoints_sample)
Expand Down
67 changes: 67 additions & 0 deletions test/collocation.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
using DiffEqFlux, OrdinaryDiffEq, Test

bounded_support_kernels = [
EpanechnikovKernel(),
UniformKernel(),
TriangularKernel(),
QuarticKernel(),
TriweightKernel(),
TricubeKernel(),
CosineKernel(),
]

unbounded_support_kernels =
[GaussianKernel(), LogisticKernel(), SigmoidKernel(), SilvermanKernel()]

@testset "Kernel Functions" begin
ts = collect(-5.0:0.1:5.0)
@testset "Kernels with support from -1 to 1" begin
minus_one_index = findfirst(x -> ==(x, -1.0), ts)
plus_one_index = findfirst(x -> ==(x, 1.0), ts)
@testset "$kernel" for (kernel, x0) in zip(
bounded_support_kernels,
[0.75, 0.50, 1.0, 15.0 / 16.0, 35.0 / 32.0, 70.0 / 81.0, pi / 4.0],
)
ws = DiffEqFlux.calckernel.((kernel,), ts)
# t < -1
@test all(ws[1:minus_one_index-1] .== 0.0)
# t > 1
@test all(ws[plus_one_index+1:end] .== 0.0)
# -1 < t <1
@test all(ws[minus_one_index+1:plus_one_index-1] .> 0.0)
# t = 0
@test DiffEqFlux.calckernel(kernel, 0.0) == x0
end
end
@testset "Kernels with unbounded support" begin
@testset "$kernel" for (kernel, x0) in zip(
unbounded_support_kernels,
[1 / (sqrt(2 * pi)), 0.25, 1 / pi, 1 / (2 * sqrt(2))],
)
# t = 0
@test DiffEqFlux.calckernel(kernel, 0.0) == x0
end
end
end

@testset "Collocation of data" begin
function f(u, p, t)
p .* u
end
rc = 2
ps = repeat([-0.001], rc)
tspan = (0.0, 50.0)
u0 = 3.4 .+ ones(rc)
t = collect(range(minimum(tspan), stop = maximum(tspan), length = 1000))
prob = ODEProblem(f, u0, tspan, ps)
data = Array(solve(prob, Tsit5(), saveat = t, abstol = 1e-12, reltol = 1e-12))
@testset "$kernel" for kernel in
[bounded_support_kernels..., unbounded_support_kernels...]
u′, u = collocate_data(data, t, kernel, 0.003)
@test sum(abs2, u - data) < 1e-8
end
@testset "$kernel" for kernel in [bounded_support_kernels...]
# Errors out as the bandwidth is too low
@test_throws ErrorException collocate_data(data, t, kernel, 0.001)
end
end
27 changes: 0 additions & 27 deletions test/collocation_regression.jl

This file was deleted.

4 changes: 2 additions & 2 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ const is_CI = haskey(ENV, "CI")

@time begin
if GROUP == "All" || GROUP == "DiffEqFlux" || GROUP == "Layers"
@safetestset "Collocation Regression" begin
include("collocation_regression.jl")
@safetestset "Collocation" begin
include("collocation.jl")
end
@safetestset "Stiff Nested AD Tests" begin
include("stiff_nested_ad.jl")
Expand Down