-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathskewness.go
33 lines (30 loc) · 1.15 KB
/
skewness.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package statistics
import (
"math"
"github.com/theriault/maths"
)
// Skewness returns the measure of the asymmetry of the probability distribution
// of a real-valued random variable about its mean. It is the 2nd central moment
// divided by the standard deviation raised to the third power.
//
// Zero skew means the values on both sides of the mean balance out overall.
// This can be because the distribution is symmetric, but can also be true for
// an asymmetric distribution where one tail is long and thin, and the other is
// short but fat.
//
// A negative skew means the distribution has a weighter tail on the left side.
// A positive skew means the distribution has a weighter tail on the right side.
//
// See also SampleSkewness.
//
// Time complexity: O(n)
// Space complexity: O(1)
//
// https://en.wikipedia.org/wiki/Skewness
func Skewness[A maths.Integer | maths.Float](X ...A) float64 {
n := float64(len(X))
mean := Mean(X...)
return PowerSumAround(X, mean, 3) / n / math.Pow(math.Sqrt(PowerSumAround(X, mean, 2)/n), 3)
// this is cleaner, but is more computationally expensive:
//return CentralMoment(X, 3) / math.Pow(StandardDeviation(X...), 3)
}