stat/distuv: improve accuracy of Exponential.CDF for small values by using math.Expm1
MODIFY
stat/distuv/exponential.go
stat/distuv/exponential.go
@@ -24,7 +24,7 @@ func (e Exponential) CDF(x float64) float64 {
if x < 0 {
return 0
}
- return 1 - math.Exp(-e.Rate*x)
+ return -math.Expm1(-e.Rate * x)
}
// ConjugateUpdate updates the parameters of the distribution from the sufficient

MODIFY
stat/distuv/exponential_test.go
stat/distuv/exponential_test.go
@@ -151,3 +151,13 @@ func TestExponentialFitPanic(t *testing.T) {
}()
e.Fit(make([]float64, 10), nil)
}
+
+func TestExponentialCDFSmallArgument(t *testing.T) {
+ t.Parallel()
+ e := Exponential{Rate: 1}
+ x := 1e-17
+ p := e.CDF(x)
+ if math.Abs(p-x) > 1e-20 {
+ t.Errorf("Wrong CDF value for small argument. Got: %v, want: %g", p, x)
+ }
+}

stat/distuv: improve accuracy of Exponential.CDF for small values by using math.Expm1