Skip to content

Fix O(T²) backward trajectory, extend backward sampling to single-cluster models#82

Merged
osorensen merged 2 commits intobackward-samplingfrom
copilot/sub-pr-81
Mar 26, 2026
Merged

Fix O(T²) backward trajectory, extend backward sampling to single-cluster models#82
osorensen merged 2 commits intobackward-samplingfrom
copilot/sub-pr-81

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Mar 15, 2026

Three issues remained open from the initial CPF-IBS review. This PR addresses all three.

Changes

  • O(T²) → O(T) trajectory assembly (src/particle.cpp)
    assemble_backward_trajectory was prepending to Armadillo matrices via insert_cols/insert_rows on every iteration, shifting all existing data each time. Replaced with pre-allocated matrices (set_size(n_items, T+1) etc.) written directly at column index t in the backward loop.

  • Backward sampling extended to single-cluster models (src/rejuvenate.cpp)
    assemble_backward_trajectory was only reachable inside the if (prior.n_clusters > 1) block, so backward_sampling = TRUE was silently a no-op for single-cluster models and the corresponding tests were not exercising the new code path. Two changes:

    • stored_weights is now also transferred during MH acceptance so the weights are always current.
    • Added else if (options.backward_sampling) after the multi-cluster block to call assemble_backward_trajectory for single-cluster models.
    // Before: backward sampling unreachable for n_clusters == 1
    if (prior.n_clusters > 1) {
      ...
      if (options.backward_sampling) this->assemble_backward_trajectory(T, resampler);
    }
    
    // After
    if (prior.n_clusters > 1) {
      ...
      if (options.backward_sampling) this->assemble_backward_trajectory(T, resampler);
      else sample_particle_filter();
    } else if (options.backward_sampling) {
      this->assemble_backward_trajectory(T, resampler);
    }
  • Duplicate alpha_hat assignment removed (tests/testthat/test-compute_sequentially_complete.R)
    Redundant back-to-back identical assignment that overwrote itself.


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

…, duplicate alpha_hat

Co-authored-by: osorensen <21175639+osorensen@users.noreply.github.com>
Copilot AI changed the title [WIP] Add backward sampling option to set SMC options Fix O(T²) backward trajectory, extend backward sampling to single-cluster models Mar 15, 2026
Copilot AI requested a review from osorensen March 15, 2026 19:06
@osorensen osorensen marked this pull request as ready for review March 26, 2026 08:09
@osorensen osorensen merged commit 4239c90 into backward-sampling Mar 26, 2026
@osorensen osorensen deleted the copilot/sub-pr-81 branch March 26, 2026 08:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants