-
Notifications
You must be signed in to change notification settings - Fork 7
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
Take samples at distances proportional to speed. Refresh proportional to speed. #112
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,15 +29,12 @@ end | |
function ab(x, θ, C::LocalBound, ∇ϕx::AbstractVector, v, B::BouncyParticle) | ||
(C.c + dot(θ, ∇ϕx), v, 2sqrt(length(θ))/C.c/norm(θ, 2)) | ||
end | ||
function ab(x, θ, C::LocalBound, vdϕ::Number, v, B::BouncyParticle) | ||
@assert vdϕ isa Number | ||
(C.c + vdϕ, v, 2sqrt(length(θ))/C.c/norm(θ, 2)) | ||
end | ||
|
||
|
||
function ab(x, θ, C::GlobalBound, ∇ϕx, v, B::Boomerang) | ||
(sqrt(normsq(θ) + normsq((x - B.μ)))*C.c, 0.0, Inf) | ||
end | ||
ab(x, θ, c, Flow) = ab(x, θ, GlobalBound(c), nothing, nothing, Flow) | ||
ab(x, θ, c, flow) = ab(x, θ, GlobalBound(c), nothing, nothing, flow) | ||
|
||
function event(t, x, θ, Z::Union{BouncyParticle,Boomerang}) | ||
t, copy(x), copy(θ), nothing | ||
|
@@ -150,34 +147,55 @@ function pdmp(∇ϕ!, t0, x0, θ0, T, c::Bound, Flow::Union{BouncyParticle, Boom | |
end | ||
|
||
|
||
################################## | ||
################################## ################################## | ||
function ab(t, x, θ, C::LocalBound, vdϕ::Number, v, B::BouncyParticle) | ||
@assert vdϕ isa Number | ||
(C.c + vdϕ, v, t + 2sqrt(length(θ))/C.c/norm(θ, 2)) | ||
end | ||
|
||
function pdmp_inner!(rng, dϕ::F1, ∇ϕ!::F2, ∇ϕx, t, x, θ, c::Bound, abc, (t′, renew), τref, (acc, num), | ||
Flow::BouncyParticle, args...; subsample=false, oscn=false, factor=1.5, adapt=false) where {F1, F2} | ||
function next_event1(rng, u::Tuple, abc, flow) | ||
t, x, v, V = u | ||
a, b, Δ = abc | ||
τ = t + poisson_time(a, b, rand(rng)) | ||
τrefresh = t + waiting_time_ref(rng, flow)/V | ||
when, what = findmin((τ, Δ, τrefresh)) | ||
return when, (:bounce, :expire, :refresh)[what] | ||
end | ||
|
||
function pdmp_inner!(rng, dϕ::F1, ∇ϕ!::F2, ∇ϕx, t, x, θ, V, c::Bound, abc, (t′, action), Δrec, (acc, num), | ||
flow::BouncyParticle, args...; subsample=false, oscn=false, factor=1.5, adapt=false) where {F1, F2} | ||
while true | ||
if τref < t′ | ||
t, _ = move_forward!(τref - t, t, x, θ, Flow) | ||
refresh!(rng, θ, Flow) | ||
if t + Δrec/V <= t′ # record! (large speed, more records) | ||
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. Take samples of the particle position at deterministic time intervals (to reduce sampling error), but measure time proportional to speed (large speed, more samples taken) |
||
t, _ = move_forward!(V\Δrec, t, x, θ, flow) | ||
Δrec = 1/flow.λref | ||
return t, V, (acc, num), c, abc, (t′, action), Δrec | ||
end | ||
Δrec = Δrec - (t′ - t)*V # coming closer to rec | ||
@assert Δrec > 0.0 | ||
if action == :refresh | ||
@assert Δrec >= 0 | ||
t, _ = move_forward!(t′ - t, t, x, θ, flow) | ||
refresh!(rng, θ, flow) | ||
V = norm(θ, 2) | ||
θdϕ, v = dϕ(t, x, θ, args...) | ||
#∇ϕx = grad_correct!(∇ϕx, x, Flow) | ||
l = λ(θdϕ, Flow) | ||
τref = t + waiting_time_ref(rng, Flow) | ||
abc = ab(x, θ, c, θdϕ, v, Flow) | ||
t′, renew = next_time(t, abc, rand(rng)) | ||
return t, (acc, num), c, abc, (t′, renew), τref | ||
elseif renew | ||
#∇ϕx = grad_correct!(∇ϕx, x, flow) | ||
l = λ(θdϕ, flow) | ||
abc = ab(t, x, θ, c, θdϕ, v, flow) | ||
t′, action = next_event1(rng, (t, x, θ, V), abc, flow) | ||
#return t, V, (acc, num), c, abc, (t′, action), Δrec | ||
elseif action == :expire | ||
τ = t′ - t | ||
t, _ = move_forward!(τ, t, x, θ, Flow) | ||
t, _ = move_forward!(τ, t, x, θ, flow) | ||
θdϕ, v = dϕ(t, x, θ, args...) | ||
#∇ϕx = grad_correct!(∇ϕx, x, Flow) | ||
abc = ab(x, θ, c, θdϕ, v, Flow) | ||
t′, renew = next_time(t, abc, rand(rng)) | ||
else | ||
#∇ϕx = grad_correct!(∇ϕx, x, flow) | ||
abc = ab(t, x, θ, c, θdϕ, v, flow) | ||
t′, action = next_event1(rng, (t, x, θ, V), abc, flow) | ||
else # action == :reflect | ||
τ = t′ - t | ||
t, _ = move_forward!(τ, t, x, θ, Flow) | ||
t, _ = move_forward!(τ, t, x, θ, flow) | ||
θdϕ, v = dϕ(t, x, θ, args...) | ||
#∇ϕx = grad_correct!(∇ϕx, x, Flow) | ||
l, lb = λ(θdϕ, Flow), pos(abc[1] + abc[2]*τ) | ||
#∇ϕx = grad_correct!(∇ϕx, x, flow) | ||
l, lb = λ(θdϕ, flow), pos(abc[1] + abc[2]*τ) | ||
num += 1 | ||
if rand(rng)*lb <= l | ||
acc += 1 | ||
|
@@ -191,25 +209,25 @@ function pdmp_inner!(rng, dϕ::F1, ∇ϕ!::F2, ∇ϕx, t, x, θ, c::Bound, abc, | |
#error("subsampling needs to be seeded by time") | ||
end | ||
if oscn | ||
@assert Flow.L == I | ||
oscn!(rng, θ, ∇ϕx, Flow.ρ; normalize=false) | ||
@assert flow.L == I | ||
oscn!(rng, θ, ∇ϕx, flow.ρ; normalize=false) | ||
else | ||
reflect!(∇ϕx, x, θ, Flow) | ||
reflect!(∇ϕx, x, θ, flow) | ||
end | ||
θdϕ, v = dϕ(t, x, θ, args...) | ||
#∇ϕx = grad_correct!(∇ϕx, x, Flow) | ||
abc = ab(x, θ, c, θdϕ, v, Flow) | ||
t′, renew = next_time(t, abc, rand(rng)) | ||
!subsample && return t, (acc, num), c, abc, (t′, renew), τref | ||
#∇ϕx = grad_correct!(∇ϕx, x, flow) | ||
abc = ab(t, x, θ, c, θdϕ, v, flow) | ||
t′, action = next_event1(rng, (t, x, θ, V), abc, flow) | ||
#!subsample && return t, V, (acc, num), c, abc, (t′, action), Δrec | ||
else | ||
abc = ab(x, θ, c, θdϕ, v, Flow) | ||
t′, renew = next_time(t, abc, rand(rng)) | ||
abc = ab(t, x, θ, c, θdϕ, v, flow) | ||
t′, action = next_event1(rng, (t, x, θ, V), abc, flow) | ||
end | ||
end | ||
end | ||
end | ||
""" | ||
pdmp(dϕ, ∇ϕ!, t0, x0, θ0, T, c::Bound, Flow::BouncyParticle, args...; oscn=false, adapt=false, subsample=false, progress=false, progress_stops = 20, islocal = false, seed=Seed(), factor=2.0) | ||
pdmp(dϕ, ∇ϕ!, t0, x0, θ0, T, c::Bound, flow::BouncyParticle, args...; oscn=false, adapt=false, subsample=false, progress=false, progress_stops = 20, islocal = false, seed=Seed(), factor=2.0) | ||
|
||
The first directional derivative `dϕ[1]` tells me if I move up or down the potential. The second directional derivative `dϕ[2]` tells me how fast the first changes. The gradient `∇ϕ!` tells me the surface I want to reflect on. | ||
|
||
|
@@ -256,31 +274,31 @@ The remaining arguments: | |
t, x = ZigZagBoomerang.sep(trace) | ||
|
||
""" | ||
function pdmp(dϕ, ∇ϕ!, t0, x0, θ0, T, c::Bound, Flow::BouncyParticle, args...; oscn=false, adapt=false, subsample=false, progress=false, progress_stops = 20, islocal = false, seed=Seed(), factor=2.0) | ||
function pdmp(dϕ, ∇ϕ!, t0, x0, θ0, T, c::Bound, flow::BouncyParticle, args...; oscn=false, adapt=false, subsample=false, progress=false, progress_stops = 20, islocal = false, seed=Seed(), factor=2.0) | ||
t, x, θ, ∇ϕx = t0, copy(x0), copy(θ0), copy(θ0) | ||
V = norm(θ, 2) | ||
rng = Rng(seed) | ||
Ξ = Trace(t0, x0, θ0, Flow) | ||
τref = waiting_time_ref(rng, Flow) | ||
Ξ = Trace(t0, x0, θ0, flow) | ||
θdϕ, v = dϕ(t, x, θ, args...) | ||
#@assert v2 ≈ v | ||
#@assert θdϕ ≈ dot(∇ϕx, θ) | ||
|
||
#∇ϕx = grad_correct!(∇ϕx, x, Flow) | ||
#∇ϕx = grad_correct!(∇ϕx, x, flow) | ||
num = acc = 0 | ||
#l = 0.0 | ||
abc = ab(x, θ, c, θdϕ, v, Flow) | ||
abc = ab(t, x, θ, c, θdϕ, v, flow) | ||
if progress | ||
prg = Progress(progress_stops, 1) | ||
else | ||
prg = missing | ||
end | ||
stops = ismissing(prg) ? 0 : max(prg.n - 1, 0) # allow one stop for cleanup | ||
tstop = T/stops | ||
|
||
t′, renew = next_time(t, abc, rand(rng)) | ||
Δrec = 1/flow.λref | ||
t′, action = next_event1(rng, (t, x, θ, V), abc, flow) | ||
while t < T | ||
t, (acc, num), c, abc, (t′, renew), τref = pdmp_inner!(rng, dϕ, ∇ϕ!, ∇ϕx, t, x, θ, c, abc, (t′, renew), τref, (acc, num), Flow, args...; oscn=oscn, subsample=subsample, factor=factor, adapt=adapt) | ||
push!(Ξ, event(t, x, θ, Flow)) | ||
t, V, (acc, num), c, abc, (t′, action), Δrec = pdmp_inner!(rng, dϕ, ∇ϕ!, ∇ϕx, t, x, θ, V, c, abc, (t′, action), Δrec, (acc, num), flow, args...; oscn=oscn, subsample=subsample, factor=factor, adapt=adapt) | ||
push!(Ξ, event(t, x, θ, flow)) | ||
|
||
if t > tstop | ||
tstop += T/stops | ||
|
@@ -299,5 +317,5 @@ struct Wrapper{F} | |
end | ||
(F::Wrapper)(y, t, x, θ, args...) = F.f(y, x, args...), nothing | ||
|
||
pdmp(∇ϕ!, t0, x0, θ0, T, c, Flow::Union{BouncyParticle, Boomerang}, args...; nargs...) = | ||
pdmp(Wrapper(∇ϕ!), t0, x0, θ0, T, GlobalBound(c), Flow, args...; nargs...) | ||
pdmp(∇ϕ!, t0, x0, θ0, T, c, flow::Union{BouncyParticle, Boomerang}, args...; nargs...) = | ||
pdmp(Wrapper(∇ϕ!), t0, x0, θ0, T, GlobalBound(c), flow, args...; nargs...) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Refreshment at intensity corresponding to speed