mirror of
https://github.com/MontFerret/ferret.git
synced 2024-12-14 11:23:02 +02:00
1af8b37a0f
* New type system * Fixed dot notation for HTML elements
42 lines
886 B
Go
42 lines
886 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, error) {
|
|
if input.Length() == 0 {
|
|
return values.NewFloat(math.NaN()), nil
|
|
}
|
|
|
|
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, nil
|
|
}
|