logmower-shipper/vendor/github.com/montanaflynn/stats/entropy.go
rasmus e45bf4739b go mod vendor
+ move k8s.io/apimachinery fork from go.work to go.mod
(and include it in vendor)
2022-11-07 00:26:05 +02:00

32 lines
586 B
Go

package stats
import "math"
// Entropy provides calculation of the entropy
func Entropy(input Float64Data) (float64, error) {
input, err := normalize(input)
if err != nil {
return math.NaN(), err
}
var result float64
for i := 0; i < input.Len(); i++ {
v := input.Get(i)
if v == 0 {
continue
}
result += (v * math.Log(v))
}
return -result, nil
}
func normalize(input Float64Data) (Float64Data, error) {
sum, err := input.Sum()
if err != nil {
return Float64Data{}, err
}
for i := 0; i < input.Len(); i++ {
input[i] = input[i] / sum
}
return input, nil
}