ak9im/p2/stats/stats.go
leo 37f15b9fa6
fix autocorrelation: convert divisor to float64
both for Autocorrelate and AutocorrelateMP, even though for the latter
the change only covers converting the divisor expression as a whole,
instead of individual elements separately.
2023-02-25 13:40:16 +01:00

64 lines
1.1 KiB
Go

package stats
import (
"math"
"gonum.org/v1/gonum/stat"
)
// Mean returns a mean value for f []float64.
func Mean(f []float64) float64 {
return stat.Mean(f, nil)
}
// Variance calculates the variance of f []float64.
func Variance(f []float64) float64 {
return stat.Variance(f, nil)
}
func Autocorrelate(f []float64, maxShift float64) []float64 {
fLen := len(f)
m := float64(fLen) * maxShift
v := make([]float64, 0, int(m)*4)
currentShift := 0
for m >= float64(currentShift) {
var r float64
for i := 0; i < fLen-currentShift; i++ {
r += f[i] * f[i+currentShift]
}
r *= 1 / float64(fLen-currentShift)
v = append(v, r)
currentShift++
}
return v
}
func AutocorrelateMP(f []float64, maxShift float64) []float64 {
fLen := len(f)
m := float64(fLen) * maxShift
v := make([]float64, 0, int(m)*4)
currentShift := 0
for m >= float64(currentShift) {
var r float64
for i := 0; i < fLen-currentShift; i++ {
r += f[i] * f[i+currentShift]
}
r *= math.Pow(float64(fLen-currentShift), -1)
v = append(v, r)
currentShift++
}
return v
}