2018-10-14 03:07:28 +02:00
package math
import (
"context"
2018-10-14 19:06:27 +02:00
"math"
2018-10-14 03:07:28 +02:00
"github.com/MontFerret/ferret/pkg/runtime/core"
"github.com/MontFerret/ferret/pkg/runtime/values"
2019-02-13 19:31:18 +02:00
"github.com/MontFerret/ferret/pkg/runtime/values/types"
2018-10-14 03:07:28 +02:00
"github.com/pkg/errors"
)
2018-10-14 19:06:27 +02:00
// Percentile 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.
2018-10-14 03:07:28 +02:00
func Percentile ( _ context . Context , args ... core . Value ) ( core . Value , error ) {
err := core . ValidateArgs ( args , 2 , 3 )
if err != nil {
return values . None , err
}
2019-02-13 19:31:18 +02:00
err = core . ValidateType ( args [ 0 ] , types . Array )
2018-10-14 03:07:28 +02:00
if err != nil {
return values . None , err
}
2019-02-13 19:31:18 +02:00
err = core . ValidateType ( args [ 1 ] , types . Int )
2018-10-14 03:07:28 +02:00
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
2019-03-29 16:48:51 +02:00
switch {
case index == even :
2018-10-14 03:07:28 +02:00
i := values . Int ( index )
percentile = sorted . Get ( i - 1 )
2019-03-29 16:48:51 +02:00
case index > 1 :
2018-10-14 03:07:28 +02:00
// 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 ) ) )
2019-03-29 16:48:51 +02:00
default :
2018-10-14 03:07:28 +02:00
return values . NewFloat ( math . NaN ( ) ) , errors . New ( "input is outside of range" )
}
return percentile , nil
}