如何在Golang中从数组中获取偏度值

r1zk6ea1  于 2023-04-03  发布在  Go
关注(0)|答案(1)|浏览(190)

我想在golang中得到数学统计的偏度和峰度,但是我在golang中找不到任何外部包。
我在github网站上找到了一个JavaScript的skewness函数。但是这个函数的值和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)
}
9lowa7mx

9lowa7mx1#

请看: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

相关问题