SciML / JumpProcesses.jl

Build and simulate jump equations like Gillespie simulations and jump diffusions with constant and state-dependent rates and mix with differential equations and scientific machine learning (SciML)
https://docs.sciml.ai/JumpProcesses/stable/
Other
140 stars 35 forks source link

Add test for Coevolve calling user-provided funcs with t beyond tspan #331

Closed gaurav-arya closed 1 year ago

gaurav-arya commented 1 year ago

See #330. The test this PR added was copied into #315, so now this PR just does some minor tweaks.

isaacsas commented 1 year ago

@gzagatti can you give this a review?

My plan is to merge this, make a bug fix release, and then hopefully merge your PR and make a minor release. It would be good after this is merged to update your PR with the test and ensure it doesn't have the same issue.

gzagatti commented 1 year ago

There's a subtle problem with the proposed fix.

Basically, the function next_time is designed to re-use rng in line 168. When you break before computing the next urate, you will be basically re-using the rng in the next interaction which will give incorrect samples. The correct way to proceed is to re-compute urate in those cases rather than recycling the rng.

This is one of the issues addressed by PR #315. In particular see line 260 and line 290. The new implementation of Coevolve ensures that we only compute urate, lrate and rate for the present time t. In other words, it is synced with the model time.

This is so subtle that on my first implementation of PR #315 I could not figure out why some of the samples from Coevolve were slightly different from other algorithms. It was poor recycling of the rng that caused the issue.

With that being said, I think it is valid to add the proposed tests. I would go even further and ensure that lrate, rate and urate are not called beyond tstop. Also you could probably improve the test speed by reducing tstop (or decresing the rates) and setting rateinterval equal to tstop.

gaurav-arya commented 1 year ago

Okay, I've reverted the fix and applied your suggestions to the test (the solve time can be reduced, but note that this will have negligible effect on the test runtime which is nearly all compile time for JumpProblem creation). I think this could be ready to merge as a broken test that your PR would then fix, or we can wait until #315 lands

isaacsas commented 1 year ago

I'm going to merge 315 shortly, and then if you want to tweak the version of this test here just update this PR and I'll merge it.

Thanks!

isaacsas commented 1 year ago

version of the test "here" I mean...

coveralls commented 1 year ago

Pull Request Test Coverage Report for Build 5537683364

Warning: This coverage report may be inaccurate.

This pull request's base commit is no longer the HEAD commit of its target branch. This means it includes changes from outside the original pull request, including, potentially, unrelated coverage changes.

Details


Files with Coverage Reduction New Missed Lines %
src/aggregators/coevolve.jl 1 98.41%
<!-- Total: 1 -->
Totals Coverage Status
Change from base Build 5520199253: 0.004%
Covered Lines: 2183
Relevant Lines: 2468

💛 - Coveralls