1 Star 4 Fork 11

王布衣 / pandas

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
stddev.go 1.51 KB
一键复制 编辑 原始数据 按行查看 历史
王布衣 提交于 2023-03-08 08:43 . !83#I6L2WO支持ARM框架
package stat
import (
"gitee.com/quant1x/vek"
"gitee.com/quant1x/vek/vek32"
"golang.org/x/exp/slices"
"gonum.org/v1/gonum/stat"
"math"
)
// Std_TODO StdDev 这个版本有bug, gonum计算的std不对
// TODO: 于总来解决
func Std_TODO[T Float](f []T) T {
if len(f) == 0 {
return T(0)
}
var d any
var s any
s = f
switch fs := s.(type) {
case []float32:
d = f32_std(fs)
case []float64:
// 这里计算不对
d = stat.StdDev(fs, nil)
default:
// 应该不会走到这里
panic(ErrUnsupportedType)
}
return d.(T)
}
// Std 计算标准差
func Std[T BaseType](f []T) T {
if len(f) == 0 {
return typeDefault[T]()
}
var d any
var s any
s = f
switch fs := s.(type) {
case []float32:
d = f32_std(fs)
case []float64:
d = f64_std(fs)
default:
// 应该不会走到这里
panic(ErrUnsupportedType)
}
return d.(T)
}
func f64_std(f []float64) float64 {
values := slices.Clone(f)
// 求平均数
meam := vek.Mean(values)
// 减去 平均数
vek.SubNumber_Inplace(values, meam)
// 计算方差
y := vek.Repeat(2.00, len(f))
vek.Pow_Inplace(values, y)
// 再求方差平均数
meam = vek.Mean(values)
meam = math.Sqrt(meam)
return meam
}
func f32_std(f []float32) float32 {
values := slices.Clone(f)
// 求平均数
meam := vek32.Mean(values)
// 减去 平均数
vek32.SubNumber_Inplace(values, meam)
// 计算方差
y := vek32.Repeat(2.00, len(f))
vek32.Pow_Inplace(values, y)
// 再求方差平均数
meam = vek32.Mean(values)
meam = float32(math.Sqrt(float64(meam)))
return meam
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/quant1x/pandas.git
git@gitee.com:quant1x/pandas.git
quant1x
pandas
pandas
v0.9.26

搜索帮助

344bd9b3 5694891 D2dac590 5694891