ssa
1package gopw
2
3import (
4"math"
5"sort"
6)
7
8// Расчёт процентилей
9// Не работает ;)
10func prctile(input []float64, percent float64) float64 {
11var percentile float64
12length := len(input)
13if length == 0 {
14return math.NaN()
15}
16
17if length == 1 {
18return input[0]
19}
20
21if percent <= 0 || percent > 100 {
22return math.NaN()
23}
24
25// Start by sorting a copy of the slice
26//c := sortedCopy(input)
27sort.Float64s(input)
28
29// Multiply percent by length of input
30index := (percent / 100) * float64(len(input))
31
32// Check if the index is a whole number
33if index == float64(int64(index)) {
34
35// Convert float to int
36i := int(index)
37
38// Find the value at the index
39percentile = input[i-1]
40
41} else if index > 1 {
42
43// Convert float to int via truncation
44i := int(index)
45
46// Find the average of the index and following values
47// percentile = Mean([]float64{input[i-1], input[i]}) // Mean(Float64Data{input[i-1], input[i]})
48percentile = (input[i-1] + input[i]) / 2
49
50} else {
51return math.NaN()
52}
53
54return percentile
55
56}
57
58// *****************************************************************************
59