1
0
mirror of https://github.com/MontFerret/ferret.git synced 2025-03-05 15:16:07 +02:00
ferret/pkg/stdlib/math/percentile.go
Tim Voronov 5f94b77a39
Feature/#7 numeric functions (#116)
* #7 Added ABS

* #7 Added ACOS

* #7 Added ASIN

* #7 Added ATAN

* #7 Added ATAN2

* #7 Added AVERAGE

* #7 Added CEIL

* #7 Added COS

* #7 Added DEGREES

* #7 Added EXP

* #7 Added EXP2

* #7 Added FLOOR

* #7 Added LOG

* #7 Added LOG2

* #7 Added LOG10

* #7 Added MAX

* #7 Added MEDIAN

* #7 Added MIN

* #7 Added PERCENTILE

* #7 Added PI

* #7 Added POW

* #7 Added RADIANS

* #7 Added RAND

* #7 Added RANGE

* #7 Added ROUND

* #7 Added SIN

* #7 Added SQRT

* #7 Added TAN

* #7 Added SUM

* #7 Added STDDEV_POPULATION

* #7 Added STDDEV_SAMPLE, VARIANCE_POPULATION, VARIANCE_SAMPLE
2018-10-13 21:07:28 -04:00

93 lines
2.2 KiB
Go

package math
import (
"context"
"github.com/MontFerret/ferret/pkg/runtime/core"
"github.com/MontFerret/ferret/pkg/runtime/values"
"github.com/pkg/errors"
"math"
)
/*
* Returns the nth percentile of the values in a given array.
* @param array (Array) - Array of numbers.
* @param numb (Int) - A number which must be between 0 (excluded) and 100 (included).
* @param method (String, optional) - "rank" (default) or "interpolation".
* @returns (Float) - The nth percentile, or null if the array is empty or only null values are contained in it or the percentile cannot be calculated.
*/
func Percentile(_ context.Context, args ...core.Value) (core.Value, error) {
err := core.ValidateArgs(args, 2, 3)
if err != nil {
return values.None, err
}
err = core.ValidateType(args[0], core.ArrayType)
if err != nil {
return values.None, err
}
err = core.ValidateType(args[1], core.IntType)
if err != nil {
return values.None, err
}
// TODO: Implement different methods
//method := "rank"
//
//if len(args) > 2 {
// err = core.ValidateType(args[2], core.StringType)
//
// if err != nil {
// return values.None, err
// }
//
// if args[2].String() == "interpolation" {
// method = "interpolation"
// }
//}
arr := args[0].(*values.Array)
percent := values.Float(args[1].(values.Int))
if arr.Length() == 0 {
return values.NewFloat(math.NaN()), nil
}
if percent <= 0 || percent > 100 {
return values.NewFloat(math.NaN()), errors.New("input is outside of range")
}
sorted := arr.Sort()
// Multiply percent by length of input
l := values.Float(sorted.Length())
index := (percent / 100) * l
even := values.Float(values.Int(index))
var percentile core.Value
// Check if the index is a whole number
if index == even {
// Convert float to int
i := values.Int(index)
// Find the value at the index
percentile = sorted.Get(i - 1)
} else if index > 1 {
// Convert float to int via truncation
i := values.Int(index)
// Find the average of the index and following values
percentile, _ = mean(values.NewArrayWith(sorted.Get(i-1), sorted.Get(i)))
} else {
return values.NewFloat(math.NaN()), errors.New("input is outside of range")
}
return percentile, nil
}