contingency_test optimization#150
Open
silkefaas wants to merge 13 commits into
Open
Conversation
…contingency test doesnt care about the layout of the contingency matrix as long as the values are the same
…House/Conditional-Independence-Testing into performance_and_optimization
…House/Conditional-Independence-Testing into performance_and_optimization
…nditional-Independence-Testing into performance_and_optimization
ol590
approved these changes
May 8, 2026
Hess3l
requested changes
May 13, 2026
Hess3l
left a comment
There was a problem hiding this comment.
Looks good overall, just a few questions.
| } | ||
| temp_stat += temp_observed * (temp_observed / temp_expected).ln(); | ||
| temp_stat += temp_observed | ||
| * (temp_observed.ln() + ln_total - ln_row_sums[i] + ln_col_sums[i]); |
There was a problem hiding this comment.
Suggested change
| * (temp_observed.ln() + ln_total - ln_row_sums[i] + ln_col_sums[i]); | |
| * (temp_observed.ln() + ln_total - ln_row_sums[i] - ln_col_sums[i]); |
The formulas make me think this should be a minus
| } | ||
| temp_stat += temp_expected * (temp_expected / temp_observed).ln(); | ||
| temp_stat += temp_observed | ||
| * (temp_observed.ln() + ln_total - ln_row_sums[i] + ln_col_sums[i]); |
There was a problem hiding this comment.
Suggested change
| * (temp_observed.ln() + ln_total - ln_row_sums[i] + ln_col_sums[i]); | |
| * (temp_observed.ln() + ln_total - ln_row_sums[i] - ln_col_sums[i]); |
Once again I think this should be a minus
| @@ -29,15 +35,17 @@ pub fn contingency_test(observed: &Array2<f64>, lambda: f64) -> Result<(f64, f64 | |||
| let mut temp_stat: f64 = 0.0; | |||
| for i in 0..nrows { | |||
| for j in 0..ncols { | |||
There was a problem hiding this comment.
could the contents of this loop be (partially) implemented in a helper function? there is quite a bit of repeated code: lines 39, 58, 76.
| } | ||
| (2.0 * temp_stat) / (lambda * (lambda + 1.0)) | ||
| let statistic_raw = (2.0 * temp_stat) / (lambda * (lambda + 1.0)); | ||
| statistic_raw.max(0.0) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
The code for contingency_test is optimized in this pull request, by exchanging divisions for multiplication using some math rules as multiplication is way more efficient then division in Rust. The division within the natural logarithm could also be removed by rewriting it. This led to an improvement of around 20% for this function.
Related Issues
Type of Change
Checklist
cargo fmt --all(code is formatted)cargo clippy --workspace --all-targets -- -D warnings(no warnings)cargo test --workspace(all tests pass)Screenshots (if applicable)
Questions for Reviewers