1
0
mirror of https://github.com/golang/go synced 2024-11-24 23:57:57 -07:00

Refactor statistical functions: use Percentile for Median calculation

This commit is contained in:
HEMANTH M 2024-10-06 10:21:00 +05:30
parent 6efd1117bf
commit 298669593b

View File

@ -31,22 +31,7 @@ func Mean(numbers []float64) float64 {
// Special cases are: // Special cases are:
// Median([]float64{}) = 0 // Median([]float64{}) = 0
func Median(numbers []float64) float64 { func Median(numbers []float64) float64 {
if len(numbers) == 0 { return Percentile(numbers, 50)
return 0
}
// Create a sorted copy of the input slice
sorted := make([]float64, len(numbers))
copy(sorted, numbers)
sort.Float64s(sorted)
n := len(sorted)
if n%2 == 0 {
// If even, return the average of the two middle numbers
return (sorted[n/2-1] + sorted[n/2]) / 2
}
// If odd, return the middle number
return sorted[n/2]
} }
// Mode returns the mode(s) of a slice of float64 numbers. // Mode returns the mode(s) of a slice of float64 numbers.