2016-06-09 91 views
0

我想獲得golang中的數學統計量的偏度和峯度。但我在golang中找不到任何外部軟件包。如何從golang中的數組中獲得偏度值

我在github網站上找到了JavaScript的偏斜函數。但是,這函數的值是至R示例代碼不同....

 
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) }

回答

0

看看:stat,它既有偏度和峯度來實現,簡單如:

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)) 

和結果:

Skewness -> -0.014112840588657319 
Kurtosis -> -0.9955286230856646