stat: refactor Quantile to prepare for more CumulantKinds
This CL exposes some hidden documentation about stat.CumulantKinds and
refactor the current empirical Quantile into its own function.
MODIFY
stat/stat.go
stat/stat.go
@@ -14,10 +14,10 @@ import (
// CumulantKind specifies the behavior for calculating the empirical CDF or Quantile
type CumulantKind int
-const (
+// List of supported CumulantKind values for the Quantile function.
// Constant values should match the R nomenclature. See
// https://en.wikipedia.org/wiki/Quantile#Estimating_the_quantiles_of_a_population
-
+const (
// Empirical treats the distribution as the actual empirical distribution.
Empirical CumulantKind = 1
)
@@ -1041,6 +1041,13 @@ func Quantile(p float64, c CumulantKind, x, weights []float64) float64 {
}
switch c {
case Empirical:
+ return empiricalQuantile(p, x, weights, sumWeights)
+ default:
+ panic("stat: bad cumulant kind")
+ }
+}
+
+func empiricalQuantile(p float64, x, weights []float64, sumWeights float64) float64 {
var cumsum float64
fidx := p * sumWeights
for i := range x {
@@ -1054,9 +1061,6 @@ func Quantile(p float64, c CumulantKind, x, weights []float64) float64 {
}
}
panic("impossible")
- default:
- panic("stat: bad cumulant kind")
- }
}
// Skew computes the skewness of the sample data.

MODIFY
stat/stat_test.go
stat/stat_test.go
@@ -1702,5 +1702,4 @@ func TestStdScore(t *testing.T) {
t.Errorf("StdScore mismatch case %d. Expected %v, Found %v", i, test.z, z)
}
}
-
}

stat: refactor Quantile to prepare for more CumulantKinds

This CL exposes some hidden documentation about stat.CumulantKinds and
refactor the current empirical Quantile into its own function.