Upstream montanaflynn/stats state of the touched files before 9ead4dd879

This commit is contained in:
upstream committed 2026-09-03 14:05:13 +02:00
commit 05657cd5a8
3 files changed
+2394

No files matched your search

+2236
View File
File diff suppressed because it is too large. Load diff
+74
View File
@@ -0,0 +1,74 @@
package stats
import "math"
// Quartiles holds the three quartile points
type Quartiles struct {
Q1 float64
Q2 float64
Q3 float64
}
// Quartile returns the three quartile points from a slice of data
func Quartile(input Float64Data) (Quartiles, error) {
il := input.Len()
if il == 0 {
return Quartiles{}, EmptyInputErr
}
// Start by sorting a copy of the slice
copy := sortedCopy(input)
// Find the cutoff places depeding on if
// the input slice length is even or odd
var c1 int
var c2 int
if il%2 == 0 {
c1 = il / 2
c2 = il / 2
} else {
c1 = (il - 1) / 2
c2 = c1 + 1
}
// Find the Medians with the cutoff points
Q1, _ := Median(copy[:c1])
Q2, _ := Median(copy)
Q3, _ := Median(copy[c2:])
return Quartiles{Q1, Q2, Q3}, nil
}
// InterQuartileRange finds the range between Q1 and Q3
func InterQuartileRange(input Float64Data) (float64, error) {
if input.Len() == 0 {
return math.NaN(), EmptyInputErr
}
qs, _ := Quartile(input)
iqr := qs.Q3 - qs.Q1
return iqr, nil
}
// Midhinge finds the average of the first and third quartiles
func Midhinge(input Float64Data) (float64, error) {
if input.Len() == 0 {
return math.NaN(), EmptyInputErr
}
qs, _ := Quartile(input)
mh := (qs.Q1 + qs.Q3) / 2
return mh, nil
}
// Trimean finds the average of the median and the midhinge
func Trimean(input Float64Data) (float64, error) {
if input.Len() == 0 {
return math.NaN(), EmptyInputErr
}
c := sortedCopy(input)
q, _ := Quartile(c)
return (q.Q1 + (q.Q2 * 2) + q.Q3) / 4, nil
}
+84
View File
@@ -0,0 +1,84 @@
package stats_test
import (
"testing"
"github.com/montanaflynn/stats"
)
func TestQuartile(t *testing.T) {
s1 := []float64{6, 7, 15, 36, 39, 40, 41, 42, 43, 47, 49}
s2 := []float64{7, 15, 36, 39, 40, 41}
for _, c := range []struct {
in []float64
Q1 float64
Q2 float64
Q3 float64
}{
{s1, 15, 40, 43},
{s2, 15, 37.5, 40},
} {
quartiles, err := stats.Quartile(c.in)
if err != nil {
t.Errorf("Should not have returned an error")
}
if quartiles.Q1 != c.Q1 {
t.Errorf("Q1 %v != %v", quartiles.Q1, c.Q1)
}
if quartiles.Q2 != c.Q2 {
t.Errorf("Q2 %v != %v", quartiles.Q2, c.Q2)
}
if quartiles.Q3 != c.Q3 {
t.Errorf("Q3 %v != %v", quartiles.Q3, c.Q3)
}
}
_, err := stats.Quartile([]float64{})
if err == nil {
t.Errorf("Empty slice should have returned an error")
}
}
func TestInterQuartileRange(t *testing.T) {
s1 := []float64{102, 104, 105, 107, 108, 109, 110, 112, 115, 116, 118}
iqr, _ := stats.InterQuartileRange(s1)
if iqr != 10 {
t.Errorf("IQR %v != 10", iqr)
}
_, err := stats.InterQuartileRange([]float64{})
if err == nil {
t.Errorf("Empty slice should have returned an error")
}
}
func TestMidhinge(t *testing.T) {
s1 := []float64{1, 3, 4, 4, 6, 6, 6, 6, 7, 7, 7, 8, 8, 9, 9, 10, 11, 12, 13}
mh, _ := stats.Midhinge(s1)
if mh != 7.5 {
t.Errorf("Midhinge %v != 7.5", mh)
}
_, err := stats.Midhinge([]float64{})
if err == nil {
t.Errorf("Empty slice should have returned an error")
}
}
func TestTrimean(t *testing.T) {
s1 := []float64{1, 3, 4, 4, 6, 6, 6, 6, 7, 7, 7, 8, 8, 9, 9, 10, 11, 12, 13}
tr, _ := stats.Trimean(s1)
if tr != 7.25 {
t.Errorf("Trimean %v != 7.25", tr)
}
_, err := stats.Trimean([]float64{})
if err == nil {
t.Errorf("Empty slice should have returned an error")
}
}