J.soo
J.soo

Reputation: 253

how to get skewness value from array in golang

I want to get skewness and kurtosis of math statistics in golang. But I can't find any external package in golang.

I found the skewness function of a JavaScript in github site. But the value of this function is different to R example code....

package main

import ( "fmt" "math" )

func main() { arr := []float64{19.09, 19.55, 17.89, 17.73, 25.15, 27.27, 25.24, 21.05, 21.65, 20.92, 22.61, 15.71, 22.04, 22.60, 24.25} getSkewness(arr) } func getSkewness(arr []float64) {

var delta, n, delta_n, term1, mean, m2, m3 float64 for _, v := range arr { n += 1 delta = v - mean delta_n = delta / n term1 = delta * delta_n * (n - 1) m3 += term1*delta_n*(n-2) - 3*delta_n*m2 m2 += term1 mean += delta_n } g := math.Sqrt(n) * m3 / math.Pow(m2, 3/2) result := math.Sqrt(n*n-1) * g / (n - 2) fmt.Println(result) }

Upvotes: 0

Views: 665

Answers (1)

Anzel
Anzel

Reputation: 20553

Take a look at: stat, it has both skewness and kurtosis implemented, simple as:

arr := stat.Float64Slice{19.09, 19.55, 17.89, 17.73, 25.15, 27.27, 25.24, 21.05, 21.65, 20.92, 22.61, 15.71, 22.04, 22.60, 24.25}
fmt.Println("Skewness -> ", stat.Skew(arr))
fmt.Println("Kurtosis -> ", stat.Kurtosis(arr))

and the results:

Skewness ->  -0.014112840588657319
Kurtosis ->  -0.9955286230856646

Upvotes: 0

Related Questions