-
Notifications
You must be signed in to change notification settings - Fork 15
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
Issue when counting reaction in time instead of reaction steps #136
Comments
If you add some logging on the |
Hello,
From this function, here is what I get: This is my differentiation when changing rate high while keeping rate low constant: I am getting the same differentiation pattern as when I did not use ln(dt), but it is not my desired differentiation. When I stop my reaction at t = 20 without log, here is what my function gives me: When I do the differentiation run and change rate high while keeping rate low constant, here is what I get: So, it is a similar "pattern" to the differentiation with the log, but my function outputs are different. |
Thanks for the issue! You are correct that the One way to solve the issue is to use a bound on the total reaction rate that is independent of the parameters. We can then introduce a "ghost" reaction that has no effect (corresonding to using StochasticAD
import Random
using Distributions
# Function
function Gillespie_trial3(rh1, rl1; Rtot_bound = rh1 + rl1)
rh = rh1 #rate of high tracks
rl = rl1 #rate of low tracks
Rtot = rh + rl
t = 0 #s
tracks = 0
ltracks = 0
htracks = 0
for l in 1:500
dt = rand(Exponential(1/Rtot_bound)) #time of next reaction
probs = [rh / Rtot_bound, 1 - Rtot / Rtot_bound, rl / Rtot_bound]
step_index = rand(Categorical(probs)) #high or low dose rate
tracks += [1, 0, 1][step_index] #whole number of tracks
t += dt
if t > 40
break
end
htracks += [1, 0, 0][step_index]
ltracks += [0, 0, 1][step_index]
end
return htracks, ltracks, t
end
#Testing function to see results
x = [0.75, 1.5]
println(Gillespie_trial3(x[1], x[2]))
x = [1.5, 1.5]
println(Gillespie_trial3(x[1], x[2]))
#Derivative estimate
x = [0.75, 1.5]
function deriv2(x)
a = derivative_estimate(l -> Gillespie_trial3(l, 1.5; Rtot_bound = sum(x) + 0.5), x[1])
b = derivative_estimate(m -> Gillespie_trial3(0.75, m; Rtot_bound = sum(x) + 0.5), x[2])
return a, b
end
mean(deriv2(x)[1][1] for i in 1:1000) # approximately 40
mean(deriv2(x)[1][2] for i in 1:1000) # 0
mean(deriv2(x)[2][1] for i in 1:1000) # 0
mean(deriv2(x)[2][2] for i in 1:1000) # approximately 40 |
This was a great suggestion and has been solving our problem so far, thank you very much! |
Hello!
I am trying to run a simulation where I am adding a high or low track at random, and I expect that for the same amount of time (such as t = 40), as my rate of high tracks increases, the number of high tracks will increase but the number of low tracks will stay the same. To implement this, I used an if t > 40 break statement, and when I run the function, it gives me the results as expected. However, when I apply the derivative estimate, when rate of high tracks increases, I am getting a negative value for rate of low tracks. This should be the result I get if I did not implement a restraint on time. Is my problem due to using an if statement? If so, is there another way I can ensure my reaction always stops at the specified time instead of number of reaction steps?
Any help is appreciated, thank you!
The text was updated successfully, but these errors were encountered: