dousaoxiancy199896 2016-06-09 09:29
浏览 150

如何从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) }

  • 写回答

1条回答 默认 最新

  • dongyuqie4322 2016-06-09 12:22
    关注

    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
    
    评论

报告相同问题?

悬赏问题

  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法