mirror of
https://github.com/MontFerret/ferret.git
synced 2024-12-23 01:24:30 +02:00
de703513e4
* linter cleanup * fix default case
42 lines
867 B
Go
42 lines
867 B
Go
package math
|
|
|
|
import (
|
|
"math"
|
|
|
|
"github.com/MontFerret/ferret/pkg/runtime/core"
|
|
"github.com/MontFerret/ferret/pkg/runtime/values"
|
|
"github.com/MontFerret/ferret/pkg/runtime/values/types"
|
|
)
|
|
|
|
func variance(input *values.Array, sample values.Int) values.Float {
|
|
if input.Length() == 0 {
|
|
return values.NewFloat(math.NaN())
|
|
}
|
|
|
|
m, _ := mean(input)
|
|
|
|
var err error
|
|
var variance values.Float
|
|
|
|
input.ForEach(func(value core.Value, idx int) bool {
|
|
err = core.ValidateType(value, types.Int, types.Float)
|
|
|
|
if err != nil {
|
|
return false
|
|
}
|
|
|
|
n := values.Float(toFloat(value))
|
|
|
|
variance += (n - m) * (n - m)
|
|
|
|
return true
|
|
})
|
|
|
|
// When getting the mean of the squared differences
|
|
// "sample" will allow us to know if it's a sample
|
|
// or population and wether to subtract by one or not
|
|
l := values.Float(input.Length() - (1 * sample))
|
|
|
|
return variance / l
|
|
}
|