Skip to content

Commit

Permalink
[poincare] Fix HypergeometricDistribution::EvaluateAtAbscissa with nu…
Browse files Browse the repository at this point in the history
…merical errors
  • Loading branch information
PiaNumworks authored and GabrielNumworks committed Apr 16, 2024
1 parent deb447b commit f86d9a9
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 3 deletions.
8 changes: 8 additions & 0 deletions apps/distributions/test/distributions_results.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,14 @@ QUIZ_CASE(distributions_results_hypergeometric) {
assert_cumulative_distributive_function_direct_is(&distribution, 70.0, 0.0);
assert_finite_integral_between_abscissas_is(&distribution, 95.0, 102.0,
0.67319633087273711);

// H(88888888888888880, 68, 88888888888888880)
distribution.setParameterAtIndex(88888888888888880, 0);
distribution.setParameterAtIndex(68, 1);
distribution.setParameterAtIndex(88888888888888880, 2);
assert_roughly_equal<float>(distribution.evaluateAtAbscissa(68), 1);
assert_roughly_equal<float>(distribution.evaluateAtAbscissa(66), NAN, 0,
true);
}

QUIZ_CASE(distributions_results_poisson) {
Expand Down
1 change: 1 addition & 0 deletions apps/shared/plot_view_plots.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,7 @@ void WithHistogram::HistogramDrawing::draw(const AbstractPlotView *plotView,
// Step 2.2: Bar height
KDCoordinate top =
plotView->floatToKDCoordinatePixel(AbstractPlotView::Axis::Vertical, y);
assert(top >= 0);
KDCoordinate barHeight = plotViewHeight - top;
assert(barHeight >= 0);
KDCoordinate minBarHeight = 1 + borderWidth;
Expand Down
11 changes: 8 additions & 3 deletions poincare/src/hypergeometric_distribution.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,14 @@ T HypergeometricDistribution::EvaluateAtAbscissa(T k, T N, T K, T n) {
if (k > K || (n - k) > (N - K)) {
return 0;
}
return BinomialCoefficientNode::compute(k, K) *
BinomialCoefficientNode::compute(n - k, N - K) /
BinomialCoefficientNode::compute(n, N);
T result = BinomialCoefficientNode::compute(k, K) *
BinomialCoefficientNode::compute(n - k, N - K) /
BinomialCoefficientNode::compute(n, N);
if (result < 0 || result > 1) {
// Precision errors
return NAN;
}
return result;
}

template <typename T>
Expand Down

0 comments on commit f86d9a9

Please sign in to comment.