stat: explicitly panic when calling CDF with empty x
MODIFY
stat/stat.go
stat/stat.go
@@ -56,6 +56,7 @@ func Bhattacharyya(p, q []float64) float64 {
//
// The x data must be sorted in increasing order. If weights is nil then all
// of the weights are 1. If weights is not nil, then len(x) must equal len(weights).
+// CDF will panic if the length of x is zero.
//
// CumulantKind behaviors:
// - Empirical: Returns the lowest fraction for which q is greater than or equal
@@ -67,6 +68,9 @@ func CDF(q float64, c CumulantKind, x, weights []float64) float64 {
if floats.HasNaN(x) {
return math.NaN()
}
+ if len(x) == 0 {
+ panic("stat: zero length slice")
+ }
if !sort.Float64sAreSorted(x) {
panic("x data are not sorted")
}

MODIFY
stat/stat_test.go
stat/stat_test.go
@@ -1396,6 +1396,16 @@ func TestCDF(t *testing.T) {
x []float64
weights []float64
}{
+ {
+ name: "x == nil",
+ kind: Empirical,
+ x: nil,
+ },
+ {
+ name: "len(x) == 0",
+ kind: Empirical,
+ x: []float64{},
+ },
{
name: "len(x) != len(weights)",
q: 1.5,

stat: explicitly panic when calling CDF with empty x