stat/distuv: fix CDF and score functions for Weibull distribution
Also completes test coverage for Weibull.
@@ -6,7 +6,6 @@ package distuv
import (
"math"
- "math/cmplx"
"golang.org/x/exp/rand"
)
@@ -28,7 +27,7 @@ func (w Weibull) CDF(x float64) float64 {
if x < 0 {
return 0
}
- return 1 - cmplx.Abs(cmplx.Exp(w.LogCDF(x)))
+ return -math.Expm1(-math.Pow(x/w.Lambda, w.K))
}
// Entropy returns the entropy of the distribution.
@@ -46,14 +45,6 @@ func (w Weibull) gammaIPow(i, pow float64) float64 {
return math.Pow(math.Gamma(1+i/w.K), pow)
}
-// LogCDF computes the value of the log of the cumulative density function at x.
-func (w Weibull) LogCDF(x float64) complex128 {
- if x < 0 {
- return 0
- }
- return cmplx.Log(-1) + complex(-math.Pow(x/w.Lambda, w.K), 0)
-}
-
// LogProb computes the natural logarithm of the value of the probability
// density function at x. -Inf is returned if x is less than zero.
//
@@ -146,7 +137,7 @@ func (w Weibull) Rand() float64 {
// For more information, see https://en.wikipedia.org/wiki/Score_%28statistics%29.
//
// Special cases:
-// Score(0) = [NaN, NaN]
+// Score(x) = [NaN, NaN] for x <= 0
func (w Weibull) Score(deriv []float64, x float64) []float64 {
if deriv == nil {
deriv = make([]float64, w.NumParameters())
@@ -159,11 +150,6 @@ func (w Weibull) Score(deriv []float64, x float64) []float64 {
deriv[1] = (w.K * (math.Pow(x/w.Lambda, w.K) - 1)) / w.Lambda
return deriv
}
- if x < 0 {
- deriv[0] = 0
- deriv[1] = 0
- return deriv
- }
deriv[0] = math.NaN()
deriv[1] = math.NaN()
return deriv
@@ -175,14 +161,11 @@ func (w Weibull) Score(deriv []float64, x float64) []float64 {
// (d/dx) log(p(x)) .
//
// Special cases:
-// ScoreInput(0) = NaN
+// ScoreInput(x) = NaN for x <= 0
func (w Weibull) ScoreInput(x float64) float64 {
if x > 0 {
return (-w.K*math.Pow(x/w.Lambda, w.K) + w.K - 1) / x
}
- if x < 0 {
- return 0
- }
return math.NaN()
}
