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

Use broadcasting vs loop in day of week #746

Merged
merged 4 commits into from
Aug 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
- The interface for defining delay distributions has been generalised to also cater for continuous distributions
- When defining probability distributions these can now be truncated using the `tolerance` argument
- Ornstein-Uhlenbeck and 5 / 2 Matérn kernels have been added. By @sbfnk in # and reviewed by @.
- Switch to broadcasting the day of the week effect. By @seabbs in #746 and reviewed by @jamesmbaazam.
- A warning is now thrown if nonparametric PMFs passed to delay options have consecutive tail values that are below a certain low threshold as these lead to loss in speed with little gain in accuracy. By @jamesmbaazam in #752 and reviewed by @seabbs.

## Bug fixes
Expand Down
9 changes: 1 addition & 8 deletions inst/stan/functions/observation_model.stan
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,9 @@
* @return A vector of reports adjusted for day of the week effects.
*/
vector day_of_week_effect(vector reports, array[] int day_of_week, vector effect) {
int t = num_elements(reports);
int wl = num_elements(effect);
// scale day of week effect
vector[wl] scaled_effect = wl * effect;
vector[t] scaled_reports;
for (s in 1:t) {
// add reporting effects (adjust for simplex scale)
scaled_reports[s] = reports[s] * scaled_effect[day_of_week[s]];
}
return(scaled_reports);
return reports .* scaled_effect[day_of_week];
}

/**
Expand Down
37 changes: 37 additions & 0 deletions tests/testthat/test-stan-observation_model.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
skip_on_cran()
skip_on_os("windows")

test_that("day_of_week_effect applies day of week effect correctly", {
reports <- c(100, 200, 300, 400, 500, 600, 700, 800, 900, 1000)
day_of_week <- c(1, 2, 3, 1, 2, 3, 1, 2, 3, 1)
effect <- c(1.0, 1.1, 1.2)

expected <- reports * effect[day_of_week] * 3
result <- day_of_week_effect(reports, day_of_week, effect)

expect_equal(result, expected)
})

test_that("scale_obs scales reports by fraction observed", {
reports <- c(100, 200, 300, 400, 500, 600, 700, 800, 900, 1000)
frac_obs <- 0.5

expected <- c(50, 100, 150, 200, 250, 300, 350, 400, 450, 500)
result <- scale_obs(reports, frac_obs)

expect_equal(result, expected)
})

test_that("truncate_obs truncates reports correctly", {
reports <- c(100, 200, 300, 400, 500, 600, 700, 800, 900, 1000)
trunc_rev_cmf <- c(1.0, 0.9, 0.8, 0.7, 0.6, 0.5, 0.4, 0.3, 0.2, 0.1)

expected_truncate <- c(100, 180, 240, 280, 300, 300, 280, 240, 180, 100)
result_truncate <- truncate_obs(reports, trunc_rev_cmf, reconstruct = 0)

expect_equal(result_truncate, expected_truncate)

result_reconstruct <- truncate_obs(expected_truncate, trunc_rev_cmf, reconstruct = 1)

expect_equal(result_reconstruct, reports)
})
Loading