-
-
Notifications
You must be signed in to change notification settings - Fork 33
remove duplicate code for collocation #180
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,7 @@ | ||
export TwoStageCost, two_stage_method | ||
export EpanechnikovKernel, UniformKernel, TriangularKernel, QuarticKernel | ||
export TriweightKernel, TricubeKernel, GaussianKernel, CosineKernel | ||
export LogisticKernel, SigmoidKernel, SilvermanKernel | ||
|
||
struct TwoStageCost{F, F2, D} <: Function | ||
cost_function::F | ||
|
@@ -10,7 +13,6 @@ end | |
(f::TwoStageCost)(p) = f.cost_function(p) | ||
(f::TwoStageCost)(p, g) = f.cost_function2(p, g) | ||
|
||
decide_kernel(kernel::CollocationKernel) = kernel | ||
function decide_kernel(kernel::Symbol) | ||
if kernel == :Epanechnikov | ||
return EpanechnikovKernel() | ||
|
@@ -32,51 +34,19 @@ function decide_kernel(kernel::Symbol) | |
return LogisticKernel() | ||
elseif kernel == :Sigmoid | ||
return SigmoidKernel() | ||
else | ||
elseif kernel == :Silverman | ||
return SilvermanKernel() | ||
else | ||
return error("Kernel name not recognized") | ||
end | ||
end | ||
|
||
function construct_t1(t, tpoints) | ||
Vaibhavdixit02 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
hcat(ones(eltype(tpoints), length(tpoints)), tpoints .- t) | ||
end | ||
function construct_t2(t, tpoints) | ||
hcat(ones(eltype(tpoints), length(tpoints)), tpoints .- t, (tpoints .- t) .^ 2) | ||
end | ||
function construct_w(t, tpoints, h, kernel) | ||
W = @. calckernel((kernel,), (tpoints - t) / h) / h | ||
Diagonal(W) | ||
end | ||
function construct_estimated_solution_and_derivative!(data, kernel, tpoints) | ||
_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)) | ||
|
||
Wd = similar(data, n, size(data, 1)) | ||
WT1 = similar(data, n, 2) | ||
WT2 = similar(data, n, 3) | ||
x = map(tpoints) do _t | ||
T1 = construct_t1(_t, tpoints) | ||
T2 = construct_t2(_t, tpoints) | ||
W = construct_w(_t, tpoints, h, kernel) | ||
mul!(Wd, W, data') | ||
mul!(WT1, W, T1) | ||
mul!(WT2, W, T2) | ||
(e2' * ((T2' * WT2) \ T2')) * Wd, (e1' * ((T1' * WT1) \ T1')) * Wd | ||
end | ||
estimated_derivative = reduce(hcat, transpose.(first.(x))) | ||
estimated_solution = reduce(hcat, transpose.(last.(x))) | ||
estimated_derivative, estimated_solution | ||
end | ||
function construct_iip_cost_function(f, du, preview_est_sol, preview_est_deriv, tpoints) | ||
function (p) | ||
_du = PreallocationTools.get_tmp(du, p) | ||
vecdu = vec(_du) | ||
cost = zero(first(p)) | ||
for i in 1:length(preview_est_sol) | ||
for i in eachindex(preview_est_sol) | ||
est_sol = preview_est_sol[i] | ||
f(_du, est_sol, p, tpoints[i]) | ||
vecdu .= vec(preview_est_deriv[i]) .- vec(_du) | ||
|
@@ -89,7 +59,7 @@ end | |
function construct_oop_cost_function(f, du, preview_est_sol, preview_est_deriv, tpoints) | ||
function (p) | ||
cost = zero(first(p)) | ||
for i in 1:length(preview_est_sol) | ||
for i in eachindex(preview_est_sol) | ||
est_sol = preview_est_sol[i] | ||
_du = f(est_sol, p, tpoints[i]) | ||
cost += sum(abs2, vec(preview_est_deriv[i]) .- vec(_du)) | ||
|
@@ -102,23 +72,27 @@ get_chunksize(cs) = cs | |
get_chunksize(cs::Type{Val{CS}}) where {CS} = CS | ||
|
||
function two_stage_method(prob::DiffEqBase.DEProblem, tpoints, data; | ||
kernel = EpanechnikovKernel(), | ||
kernel::Union{CollocationKernel, Symbol} = EpanechnikovKernel(), | ||
loss_func = L2Loss, mpg_autodiff = false, | ||
verbose = false, verbose_steps = 100, | ||
autodiff_chunk = length(prob.p)) | ||
f = prob.f | ||
kernel_function = decide_kernel(kernel) | ||
estimated_derivative, estimated_solution = construct_estimated_solution_and_derivative!(data, | ||
kernel_function, | ||
tpoints) | ||
if kernel isa Symbol | ||
@warn "Passing kernels as Symbols will be deprecated" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wanted to do it like that at first, but I ran into issues because Julia cannot dispatch on a keyword argument, i.e. it is impossible to have two methods, one with kernel restricted to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think you can There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah without kwarg handling it's not obvious, I think the way to do it with
|
||
kernel = decide_kernel(kernel) | ||
end | ||
|
||
# Step - 1 | ||
|
||
estimated_derivative, estimated_solution = collocate_data(data, tpoints, kernel) | ||
|
||
# Step - 2 | ||
|
||
du = PreallocationTools.dualcache(similar(prob.u0), autodiff_chunk) | ||
preview_est_sol = [@view estimated_solution[:, i] | ||
for i in 1:size(estimated_solution, 2)] | ||
for i in axes(estimated_solution, 2)] | ||
preview_est_deriv = [@view estimated_derivative[:, i] | ||
for i in 1:size(estimated_solution, 2)] | ||
for i in axes(estimated_solution, 2)] | ||
f = prob.f | ||
if DiffEqBase.isinplace(prob) | ||
cost_function = construct_iip_cost_function(f, du, preview_est_sol, | ||
preview_est_deriv, tpoints) | ||
|
Uh oh!
There was an error while loading. Please reload this page.