-
-
Notifications
You must be signed in to change notification settings - Fork 38
Ficticious jump algorithm for time-dependent variable rate jumps. #252
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
pihop
wants to merge
16
commits into
SciML:master
Choose a base branch
from
pihop:bounded_variable_jumps
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 13 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
2e2ba58
Extrande algorithm
pihop d47e5da
if validity window not given assume bound holds everywhere
pihop 05532c3
bug fix. compute rates at proposed jump times
pihop a0bd99f
check simulation with step rate function
pihop 0cf67a7
avoid duplicating bounded variable jumps
pihop e1df00d
typo corrected
pihop fb4a584
Merge upstream to Extrande branch and resolve conflicts
pihop 9348586
Test time-dependent birth death process mean against ODE solution
pihop 930bb07
allow extrande be used with no variable rate jumps
pihop 2d35fb4
format
pihop fd59fe1
Add extrande to the tested algorithms
pihop 0d7d2e7
Remove unused function is_fictitious
pihop 8018704
Fixes, ma jumps now part of the algorithm
pihop db901fd
format
pihop 15a31f9
add extrande to more test sets
pihop fad63ca
another attempt at correcting the mass action jump treatment
pihop 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 hidden or 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
This file contains hidden or 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
This file contains hidden or 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 | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,117 @@ | ||||||
# Define the aggregator. | ||||||
struct Extrande <: AbstractAggregatorAlgorithm end | ||||||
|
||||||
""" | ||||||
Extrande sampling method for jumps with defined rate bounds. | ||||||
""" | ||||||
|
||||||
nullaffect!(integrator) = nothing | ||||||
const NullAffectJump = ConstantRateJump((u, p, t) -> 0.0, nullaffect!) | ||||||
|
||||||
mutable struct ExtrandeJumpAggregation{T, S, F1, F2, F3, F4, RNG} <: | ||||||
AbstractSSAJumpAggregator | ||||||
next_jump::Int | ||||||
prev_jump::Int | ||||||
next_jump_time::T | ||||||
end_time::T | ||||||
cur_rates::Vector{T} | ||||||
sum_rate::T | ||||||
ma_jumps::S | ||||||
rate_bnds::F3 | ||||||
wds::F4 | ||||||
rates::F1 | ||||||
affects!::F2 | ||||||
save_positions::Tuple{Bool, Bool} | ||||||
rng::RNG | ||||||
end | ||||||
|
||||||
function ExtrandeJumpAggregation(nj::Int, njt::T, et::T, crs::Vector{T}, sr::T, maj::S, | ||||||
rs::F1, affs!::F2, sps::Tuple{Bool, Bool}, rng::RNG; | ||||||
rate_bounds::F3, windows::F4, | ||||||
kwargs...) where {T, S, F1, F2, F3, F4, RNG} | ||||||
ExtrandeJumpAggregation{T, S, F1, F2, F3, F4, RNG}(nj, nj, njt, et, crs, sr, maj, | ||||||
rate_bounds, windows, rs, affs!, sps, | ||||||
rng) | ||||||
end | ||||||
|
||||||
############################# Required Functions ############################## | ||||||
function aggregate(aggregator::Extrande, u, p, t, end_time, constant_jumps, | ||||||
ma_jumps, save_positions, rng; variable_jumps = (), kwargs...) | ||||||
ma_jumps_ = !isnothing(ma_jumps) ? ma_jumps : () | ||||||
rates, affects! = get_jump_info_fwrappers(u, p, t, | ||||||
(constant_jumps..., variable_jumps..., ma_jumps_..., | ||||||
NullAffectJump)) | ||||||
rbnds, wnds = get_va_jump_bound_info_fwrapper(u, p, t, | ||||||
(constant_jumps..., variable_jumps..., ma_jumps_..., | ||||||
NullAffectJump)) | ||||||
build_jump_aggregation(ExtrandeJumpAggregation, u, p, t, end_time, ma_jumps, | ||||||
rates, affects!, save_positions, rng; u = u, rate_bounds = rbnds, | ||||||
windows = wnds, kwargs...) | ||||||
end | ||||||
|
||||||
# set up a new simulation and calculate the first jump / jump time | ||||||
function initialize!(p::ExtrandeJumpAggregation, integrator, u, params, t) | ||||||
p.end_time = integrator.sol.prob.tspan[2] | ||||||
generate_jumps!(p, integrator, u, params, t) | ||||||
end | ||||||
|
||||||
# execute one jump, changing the system state | ||||||
@inline function execute_jumps!(p::ExtrandeJumpAggregation, integrator, u, params, t) | ||||||
# execute jump | ||||||
u = update_state!(p, integrator, u) | ||||||
nothing | ||||||
end | ||||||
|
||||||
@fastmath function next_extrande_jump(p::ExtrandeJumpAggregation, u, params, t) | ||||||
ttnj = typemax(typeof(t)) | ||||||
nextrx = zero(Int) | ||||||
Wmin = typemax(typeof(t)) | ||||||
Bmax = typemax(typeof(t)) | ||||||
|
||||||
# Calculate the total rate bound and the largest common validity window. | ||||||
if !isempty(p.rate_bnds) | ||||||
Bmax = typeof(t)(0.) | ||||||
|
Bmax = typeof(t)(0.) | |
Bmax = zero(t) |
This file contains hidden or 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
This file contains hidden or 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 |
---|---|---|
@@ -0,0 +1,64 @@ | ||
using DiffEqBase, JumpProcesses, OrdinaryDiffEq, Test | ||
using StableRNGs | ||
using Statistics | ||
rng = StableRNG(48572) | ||
|
||
f = function (du, u, p, t) | ||
du[1] = 0.0 | ||
end | ||
|
||
rate = (u, p, t) -> t < 5.0 ? 1.0 : 0.0 | ||
rbound = (u, p, t) -> 1.0 | ||
rinterval = (u, p, t) -> Inf | ||
affect! = (integrator) -> (integrator.u[1] = integrator.u[1] + 1) | ||
jump = VariableRateJump(rate, affect!; urate = rbound, rateinterval = rinterval) | ||
|
||
prob = ODEProblem(f, [0.0], (0.0, 10.0)) | ||
jump_prob = JumpProblem(prob, Extrande(), jump; rng = rng) | ||
|
||
# Test that process doesn't jump when rate switches to 0. | ||
sol = solve(jump_prob, Tsit5()) | ||
@test sol(5.0)[1] == sol[end][1] | ||
|
||
# Birth-death process with time-varying birth rates. | ||
Nsims = 1000000 | ||
u0 = [10.0] | ||
|
||
function runsimulations(jump_prob, testts) | ||
Psamp = zeros(Int, length(testts), Nsims) | ||
for i in 1:Nsims | ||
sol_ = solve(jump_prob, Tsit5()) | ||
Psamp[:, i] = getindex.(sol_(testts).u, 1) | ||
end | ||
mean(Psamp, dims = 2) | ||
end | ||
|
||
# Variable rate birth jumps. | ||
rateb = (u, p, t) -> (0.1 * sin(t) + 0.2) | ||
ratebbound = (u, p, t) -> 0.3 | ||
ratebwindow = (u, p, t) -> Inf | ||
affectb! = (integrator) -> (integrator.u[1] = integrator.u[1] + 1) | ||
jumpb = VariableRateJump(rateb, affectb!; urate = ratebbound, rateinterval = ratebwindow) | ||
|
||
# Constant rate death jumps. | ||
rated = (u, p, t) -> u[1] * 0.08 | ||
affectd! = (integrator) -> (integrator.u[1] = integrator.u[1] - 1) | ||
jumpd = ConstantRateJump(rated, affectd!) | ||
|
||
# Problem definition. | ||
bd_prob = ODEProblem(f, u0, (0.0, 2pi)) | ||
jump_bd_prob = JumpProblem(bd_prob, Extrande(), jumpb, jumpd) | ||
|
||
test_times = range(1.0, stop = 2pi, length = 3) | ||
means = runsimulations(jump_bd_prob, test_times) | ||
|
||
# ODE for the mean. | ||
fu = function (du, u, p, t) | ||
du[1] = (0.1 * sin(t) + 0.2) - (u[1] * 0.08) | ||
end | ||
|
||
ode_prob = ODEProblem(fu, u0, (0.0, 2 * pi)) | ||
ode_sol = solve(ode_prob, Tsit5()) | ||
|
||
# Test extrande against the ODE mean. | ||
@test prod(isapprox.(means, getindex.(ode_sol(test_times).u, 1), rtol = 1e-3)) |
This file contains hidden or 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
This file contains hidden or 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.
This doesn't make sense as
MassActionJump
s don't use the same interface asConstantRateJump
s orVariableRateJump
s. So while you can merge the rate functions from the latter two you need to keep track ofMassActionJump
s separately and handle their rate calculation separately. See what we do inDirect
for handling them.