From 943fa00fdc6caf80b5c7037f2f44c5b90c7ce1a3 Mon Sep 17 00:00:00 2001 From: Jonathan J Lawlor Date: Fri, 7 Nov 2014 18:23:43 -0500 Subject: [PATCH] Simplify Skew Sig Simplify the Skew interface to remove dependence on mean and standard deviation calculation. --- stat.go | 15 +++++++++------ stat_test.go | 6 ++---- 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/stat.go b/stat.go index d1036b63..c7cc16c9 100644 --- a/stat.go +++ b/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] } diff --git a/stat_test.go b/stat_test.go index 277a9976..f9070237 100644 --- a/stat_test.go +++ b/stat_test.go @@ -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") } }