mirror of
https://github.com/golang/go
synced 2024-11-21 16:34:42 -07:00
Refactor statistical functions: use Percentile for Median calculation
This commit is contained in:
parent
6efd1117bf
commit
298669593b
@ -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.
|
||||||
|
Loading…
Reference in New Issue
Block a user