Skip to content
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

Final epidemic size with susceptibility groups #39

Closed
wants to merge 58 commits into from

Conversation

pratikunterwegs
Copy link
Member

@pratikunterwegs pratikunterwegs commented Sep 15, 2022

Implements:

  1. Fixes final_size using Cpp Newton solver #4 by porting code from @BlackEdder (at https://gitlab.com/epidemics-r/code_snippets/-/blob/feature/newton_solver/include/finalsize.hpp) into the package. Final size Newton solver now included in the header finalsize.h.
  2. Fixes Cpp implementation of final size with susceptibility groups #40 by adding a new source file with the Rcpp function final_size_grps_cpp. This calls solve_final_size_by_susceptibility, solve_final_size_newton, and epi_spread from the header.
  3. Adds tests for internal C++ functions called by Rcpp functions.

Warning!
Build sometimes fails because the test for final_size_grps_cpp fails to calculate the correct final size, instead returning implausible values - likely due to an issue with the Newton solver. Tagging @BlackEdder in case this can be fixed.

Review input requested

  1. Should the Newton solver should be made more modular, allowing for testing of intermediate functions?
  2. Suggest more tests for the plausibility of solutions.
  3. Suggest text for the p_susceptibility and susceptibility parameters in function documentation.

@pratikunterwegs pratikunterwegs self-assigned this Sep 16, 2022
@pratikunterwegs pratikunterwegs added New feature New feature or request Tests Tests and statistical correctness labels Sep 16, 2022
@pratikunterwegs
Copy link
Member Author

pratikunterwegs commented Sep 23, 2022

We’re still getting errors in the 1. simple, 2. complex, and 3. Polymod data case, with the final sizes returned as either 0.0 or NaN.

This behaviour is not uniform across test runs. It seems a maximum of 7 tests are failing, but sometimes only 5 fail.

The failures are also not uniform across systems: locally, 5-7 tests fail (Mac M2), but only 1 test (the complex data case) fails on GHA. However, all 7 fail on Windows GHA.

Latest build reports are here: https://github.com/epiverse-trace/finalsize/actions/runs/3111847610/jobs/5044606831

Possible reasons:

  1. Contact matrices are not correctly scaled
  2. r0 is not being included - should all final size calls specify the contact matrix argument as: r0 * contact_matrix? Where the contact matrix is assumed to be correctly scaled.

pratikunterwegs and others added 3 commits September 23, 2022 12:33
* WIP finalsize_grps_cpp tests: test Newton solver

* WIP finalsize_grps_cpp tests: Test internal solve fun

* Check finalsize_grps_cpp with r0 = 1.3 (passes)

* Check finalsize_grps with r0 = 2.0 (fails)

* WIP formatting for R tests

* Test Cpp solve_by_susc with r0 = 2.0 (fails)

* Return ArrayXd

* Rcpp export return ArrayXd

* Test Newton solver with r0 = 2.0 (fails)
@pratikunterwegs pratikunterwegs marked this pull request as draft September 29, 2022 15:11
pratikunterwegs and others added 15 commits October 3, 2022 09:08
R implementation of final size of an epidemic with susceptibility groups.

* WIP - first draft finalsize_grps in R

* WIP - first draft tests for finalsize_grps in R

* WIP - missing comma for args

* Split off epi_spread and remove Newton solver

* Implement iterative solver for susc grps, fixes #44

* Use iterative solver in final_size_grps, fixes #45

* Test for epi_spread, WIP #46, #47

* Add tests for iterative solver, fixes #46

* Test final_size_grps, fixes #47

* Documentation for final_size_grps and related fns, fixes #48

* Add extra test for epi_spread, WIP #46, #47

* Minor refactor of p_susc in epi_spread_data, remove commented code

* Housekeeping: No explicit namespacing for testthat

* Kronecker con_mat replication, vectors instead of mats, fixes #49

* Vectorise con_mat filling in iterative solver, fixes #51

* Vectorise fn_f in iterative solver, and pi adjust, fixes #52

* Squash stopifnot calls into one, fixes #50

* Pass function args re solver steps, solves #53

* Update tests for epi_spread and final_size_grps

* Refactor final size from pi to epi_final_size, fixes #54

* Remove test for Newton solver
Correct struct member in return
Move Rcpp C++ 11 plugin to header

Format helper functions

WIP move helper funs to new header
WIP remove unused params

WIP add documentation for finalsize by grp

WIP add iterations as fn arg to Newton solver

WIP minor edits, eg. to remove atuo return types

WIP finalsize_by_grp add checks on args

WIP finalsize_by_grp begin documentation
Add tests for internal funs in helper_funs.h

Add tests for cpp funs in finalsize.h

WIP - add tests: finalsize_grps_cpp with polymod data

WIP finalsize_grps_cpp: scale contacts and test for NaN

WIP finalsize_grps_tests: check for correct proportions infected

WIP - checks for final size = 0

Add more tests for final size cpp code

Check that final sizes are not Inf, NaN, or zero

Add check for p_susceptibility rowwise sum = 1.0

WIP finalsize_grps_cpp tests: Add check for error on p_susc sum

WIP finalsize_grps_cpp tests: do not scale contacts in simple case

WIP finalsize_grps_cpp tests: p_susceptibility in tests sums to 1

WIP finalsize_grps_cpp tests: test susc - final size relation

WIP finalsize_grps_cpp tests: scale contacts by r0 and scale demo
Procedural Rcpp export

Supress permissive export, export new finalsize by grp fun
* WIP finalsize_grps_cpp tests: test Newton solver

* WIP finalsize_grps_cpp tests: Test internal solve fun

* Check finalsize_grps_cpp with r0 = 1.3 (passes)

* Check finalsize_grps with r0 = 2.0 (fails)

* WIP formatting for R tests

* Test Cpp solve_by_susc with r0 = 2.0 (fails)

* Return ArrayXd

* Rcpp export return ArrayXd

* Test Newton solver with r0 = 2.0 (fails)
@pratikunterwegs
Copy link
Member Author

Closing this PR as (1) we are instead focusing on an R implementation of the Newton solver, and (2) the commit history has been wrecked by rebasing on to main after merging #42 - picked commits are duplicated, adding to an already long commit history. The thing to do here was to squash-merge main into this feature branch.

@pratikunterwegs pratikunterwegs deleted the feature/susc_grps branch October 3, 2022 09:50
@pratikunterwegs pratikunterwegs restored the feature/susc_grps branch October 3, 2022 11:08
@pratikunterwegs pratikunterwegs deleted the feature/susc_grps branch October 10, 2022 14:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
New feature New feature or request Tests Tests and statistical correctness
Projects
No open projects
Development

Successfully merging this pull request may close these issues.

Cpp implementation of final size with susceptibility groups final_size using Cpp Newton solver
4 participants