go(stats): actually calculate median
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
surtur 2022-07-18 04:17:58 +02:00
parent 988c20885e
commit 71e67bdb58
Signed by: wanderer
GPG Key ID: 19CE1EC1D9E0486D
2 changed files with 24 additions and 2 deletions

@ -5,6 +5,7 @@ package stats
import (
"fmt"
"sort"
"git.dotya.ml/wanderer/math-optim/report"
"gonum.org/v1/gonum/floats"
@ -97,8 +98,29 @@ func statsFromBest(best []float64) []float64 {
s[0] = floats.Min(best)
s[1] = floats.Max(best)
s[2] = stat.Mean(best, nil)
s[3] = stat.Mean(best, nil)
s[3] = median(best)
s[4] = stat.StdDev(best, nil)
return s
}
// as per https://gosamples.dev/calculate-median/.
func median(data []float64) float64 {
dataCopy := make([]float64, len(data))
copy(dataCopy, data)
sort.Float64s(dataCopy)
var median float64
//nolint: gocritic
if l := len(dataCopy); l == 0 {
return 0
} else if l%2 == 0 {
median = (dataCopy[l/2-1] + dataCopy[l/2]) / 2
} else {
median = dataCopy[l/2]
}
return median
}

@ -40,7 +40,7 @@ func TestParseBenchStats(t *testing.T) {
},
}
wantResults := []float64{1336.3261400058473, 2169.7378893600176, 1737.765688971335, 1737.765688971335, 342.37072192259393}
wantResults := []float64{1336.3261400058473, 2169.7378893600176, 1737.765688971335, 1722.4993632597375, 342.37072192259393}
want := report.Row{
Title: "D=5, f=De Jong 5th, G=3, I=4",
Values: wantResults,