Skip to content

Commit 53b2814

Browse files
committed
Fixed minor bug in Poisson distribution.
1 parent 7ff2742 commit 53b2814

2 files changed

Lines changed: 22 additions & 8 deletions

File tree

4.8.1/Numerics/Distributions/Univariate/Poisson.cs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -259,10 +259,17 @@ public override double InverseCDF(double probability)
259259
if (_parametersValid == false)
260260
ValidateParameters([Lambda], true);
261261

262-
double lower = Minimum;
263-
double upper = Mean;
264-
Brent.Bracket(x => CDF(x) - probability, ref lower, ref upper, out var f1, out var f2);
265-
return Brent.Solve(x => CDF(x) - probability, lower, upper);
262+
int k = (int)Math.Max(0, Math.Floor(Lambda)); // start near the mean
263+
264+
// Move downward if needed
265+
while (k > 0 && CDF(k - 1) >= probability)
266+
k--;
267+
268+
// Move upward to find first k such that CDF(k) >= probability
269+
while (CDF(k) < probability)
270+
k++;
271+
272+
return k;
266273
}
267274

268275
/// <inheritdoc/>

8.0/Numerics/Distributions/Univariate/Poisson.cs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -259,10 +259,17 @@ public override double InverseCDF(double probability)
259259
if (_parametersValid == false)
260260
ValidateParameters([Lambda], true);
261261

262-
double lower = Minimum;
263-
double upper = Mean;
264-
Brent.Bracket(x => CDF(x) - probability, ref lower, ref upper, out var f1, out var f2);
265-
return Brent.Solve(x => CDF(x) - probability, lower, upper);
262+
int k = (int)Math.Max(0, Math.Floor(Lambda)); // start near the mean
263+
264+
// Move downward if needed
265+
while (k > 0 && CDF(k - 1) >= probability)
266+
k--;
267+
268+
// Move upward to find first k such that CDF(k) >= probability
269+
while (CDF(k) < probability)
270+
k++;
271+
272+
return k;
266273
}
267274

268275
/// <inheritdoc/>

0 commit comments

Comments
 (0)