Skip to content

contingency_test optimization#150

Open
silkefaas wants to merge 13 commits into
developmentfrom
performance_and_optimization
Open

contingency_test optimization#150
silkefaas wants to merge 13 commits into
developmentfrom
performance_and_optimization

Conversation

@silkefaas
Copy link
Copy Markdown
Contributor

@silkefaas silkefaas commented Apr 15, 2026

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

  • Bug fix
  • New feature
  • Breaking change
  • Documentation update

Checklist

  • I ran cargo fmt --all (code is formatted)
  • I ran cargo clippy --workspace --all-targets -- -D warnings (no warnings)
  • I ran cargo test --workspace (all tests pass)
  • I added tests for new functionality
  • I updated relevant documentation

Screenshots (if applicable)

Questions for Reviewers

@silkefaas silkefaas marked this pull request as ready for review May 6, 2026 09:19
@silkefaas silkefaas requested a review from Hiddentale as a code owner May 6, 2026 09:19
@silkefaas silkefaas changed the title added textfile for draft pull request Contingency_test optimization May 8, 2026
@silkefaas silkefaas changed the title Contingency_test optimization contingency_test optimization May 8, 2026
@ol590 ol590 self-requested a review May 8, 2026 11:12
Copy link
Copy Markdown

@ol590 ol590 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code looks idiomatic and everything seems to work.

Copy link
Copy Markdown

@Hess3l Hess3l left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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]);
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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]);
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 {
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why this change?

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not actually deleted I think?

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.

4 participants