mirror of
https://github.com/gonum/gonum.git
synced 2025-10-20 21:59:25 +08:00
Simplify Skew Sig
Simplify the Skew interface to remove dependence on mean and standard deviation calculation.
This commit is contained in:
15
stat.go
15
stat.go
@@ -768,24 +768,27 @@ func Quantile(p float64, c CumulantKind, x, weights []float64) float64 {
|
||||
// Skew computes the skewness of the sample data.
|
||||
// If weights is nil then all of the weights are 1. If weights is not nil, then
|
||||
// len(x) must equal len(weights).
|
||||
func Skew(x []float64, mean, stdev float64, weights []float64) float64 {
|
||||
func Skew(x []float64, weights []float64) float64 {
|
||||
|
||||
// this is a three-pass approach. There are one pass approaches, but
|
||||
// I don't know of a two-pass one, or the numerical stability of the
|
||||
// one vs three pass version.
|
||||
|
||||
u, std := MeanStdDev(x, weights)
|
||||
if weights == nil {
|
||||
var s float64
|
||||
for _, v := range x {
|
||||
z := (v - mean) / stdev
|
||||
z := (v - u) / std
|
||||
s += z * z * z
|
||||
}
|
||||
return s * skewCorrection(float64(len(x)))
|
||||
}
|
||||
if len(x) != len(weights) {
|
||||
panic("stat: slice length mismatch")
|
||||
}
|
||||
var (
|
||||
s float64
|
||||
sumWeights float64
|
||||
)
|
||||
for i, v := range x {
|
||||
z := (v - mean) / stdev
|
||||
z := (v - u) / std
|
||||
s += weights[i] * z * z * z
|
||||
sumWeights += weights[i]
|
||||
}
|
||||
|
@@ -1171,14 +1171,12 @@ func TestSkew(t *testing.T) {
|
||||
ans: -1.12066646837198,
|
||||
},
|
||||
} {
|
||||
mean := Mean(test.x, test.weights)
|
||||
std := StdDev(test.x, test.weights)
|
||||
skew := Skew(test.x, mean, std, test.weights)
|
||||
skew := Skew(test.x, test.weights)
|
||||
if math.Abs(skew-test.ans) > 1e-14 {
|
||||
t.Errorf("Skew mismatch case %d. Expected %v, Found %v", i, test.ans, skew)
|
||||
}
|
||||
}
|
||||
if !Panics(func() { Skew(make([]float64, 3), 0, 1, make([]float64, 2)) }) {
|
||||
if !Panics(func() { Skew(make([]float64, 3), make([]float64, 2)) }) {
|
||||
t.Errorf("Skew did not panic with x, weights length mismatch")
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user