Skip to content

Commit

Permalink
Attempt to catch error where NaN is introduced.
Browse files Browse the repository at this point in the history
  • Loading branch information
RobbieMcKinstry committed Nov 29, 2024
1 parent 4cbbdc1 commit eb17449
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion src/stats/chi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,22 @@ fn test_statistic<const N: usize, C: Categorical<N>>(table: &ContingencyTable<N,
let observed_count = table.observed_by_index(i) as i64;
let diff = observed_count - expected_count;
let error = diff.pow(2) as f64;
let incremental_error = error / (expected_count as f64);
let incremental_error = error / error_denominator(expected_count);
debug_assert!(!incremental_error.is_nan(), "The error must never be NaN.");
sum += incremental_error;
}
sum
}

// TODO: explain how we got here.
fn error_denominator(expected_count: i64) -> f64 {
if expected_count == 0 {
1.0
} else {
expected_count as f64
}
}

/// calculates the p-value given the test statistic and the degrees of freedom.
/// This is determined by the area of the Chi Square distribution (which is a special
/// case of the gamma distribution).
Expand Down

0 comments on commit eb17449

Please sign in to comment.