Handbook
Glossary
median ( seq -- x )
Statistics
Prev:
harmonic-mean ( seq -- x )
Next:
lower-median ( seq -- elt )
Vocabulary
math
.
statistics
Inputs
seq
a
sequence
Outputs
x
a non-negative real number
Word description
Computes the median of
seq
by finding the middle element of the sequence using
kth-smallest
. If there is an even number of elements in the sequence, the median is not unique, so the mean of the two middle values is output.
Examples
USING: math.statistics prettyprint ; { 1 2 3 } median .
2
USING: math.statistics prettyprint ; { 1 2 3 4 } median .
2+1/2
Errors
Throws a
signal-error.
(divide by zero) if the sequence is empty.
Definition
USING:
kernel
math
sequences
;
IN:
math.statistics
:
median
( seq -- x )
dup
length
odd?
[
lower-median
]
[
medians
+
2
/
]
if
;