Refactor Stan GP and convolution functions for efficiency#1421
Conversation
Add a shared matern_indices() helper for the Matern spectral densities and build the GP basis functions in PHI() and PHI_periodic() via an outer product instead of rep_matrix() plus diag_post_multiply(). Tidy convolve_with_rev_pmf() by allocating after validation and dropping the redundant length guard. The helper keeps linspaced_vector() bounds data-only so it compiles under the Stan version shipped with rstan. Co-authored-by: Bob Carpenter <3383807+bob-carpenter@users.noreply.github.com>
|
Important Review skippedAuto incremental reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the ⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Run ID: You can disable this status message by setting the Use the checkbox below for a quick retry:
WalkthroughThis PR refactors Gaussian process and convolution Stan functions for improved performance. It introduces a shared ChangesGaussian Process and Convolution Optimization
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Automated review (quality gate)Reviewed the diff against Correctness — verified algebraically identical to the originals:
rstan constraint: Tests: new Style: new lines all within the 80-char limit; no trailing whitespace. The over-80 lines flagged by grep are all pre-existing. Minor suggestions (non-blocking):
This is an automated review from the PR quality gate. CI is in progress; will relay results. |
Add a shared reporting_phi() helper that converts the reporting overdispersion into the negative binomial phi, with a large-phi Poisson fallback. report_lp(), report_log_lik(), and report_rng() now use it instead of repeating the computation and the 1e5 default. Also drop a stray double semicolon in calc_conv_indices_len().
Automated re-review — commit 3bd55a5Reviewed the new commit. No Critical or Important issues.
Other:
This is an automated review from the PR quality gate. CI re-running on the new commit; will relay results. |
|
This is how benchmark results would change (along with a 95% confidence interval in relative change) if 3bd55a5 is merged into main:
|
|
I think the simplification of code itself is enough of a positive. |
Description
It picks up the efficiency refactoring started in #1274 by @bob-carpenter, which stalled because the Stan code would not compile under
rstan.The blocker was the new
matern_indices()helper callinglinspaced_vector(M, factor, factor * M)withfactor = pi() / (2 * L). The Stan version shipped withrstanrequires the bounds oflinspaced_vector()to be data-only, andfactorderives from the function argumentL, which Stan treats as parameter-dependent. The build succeeds undercmdstanr(the dev backend) because that constraint is relaxed there, so the failure only showed up for contributors usingrstan.This PR applies the same lessons while keeping the code rstan-compatible:
matern_indices()helper for the Matern spectral densities, written so thelinspaced_vector()bounds stay data-only (literals) and the scaling is applied afterwards.diagSPD_Matern12/32/52()now use it.PHI()andPHI_periodic()via an outer product (vector * row_vector) instead ofrep_matrix()plusdiag_post_multiply(), avoiding materialising the replicate matrix.convolve_with_rev_pmf()by allocatingzafter the validity checks and dropping the redundantif (len > xlen)guard (the loop runs zero times otherwise).nureject message to use the decimal values it actually compares against.The Matern refactors are algebraically identical to the originals, and the existing Stan unit tests pass unchanged. A new test covers
matern_indices().Per maintainer preference, the no-op changes from #1274 (inlining
setup_noise()'s return, reformattingdiagSPD_Periodic()) were left out to keep the diff focused on genuine efficiency and DRY wins.Initial submission checklist
expose_stan_fns()compiles under rstan;test-stan-guassian-process.Randtest-stan-convole.Rpass).After the initial Pull Request
This was opened by a bot. Please ping @seabbs for any questions.
Summary by CodeRabbit
Release Notes
Refactor
Tests
Chores