Questions tagged [median]

The median is the 'middle' value from a set of values. If the number of values is an even number, the median is the mean of the 'middle' values.

The median is generally used in programming in the sense of the term that comes from statistics. In simple terms it means the number that has half the values above it and half the values below it in the range. It is different than average in that an average can be influenced by extremes on either end of the spectrum. If a few numbers are a lot greater or a lot smaller than the rest, the average will be significantly different from the median. Median can give you a better sense of where the typical middle case is than the average.

The median is also very useful in signal and image processing, in the context of a moving median filter. This filter is usually used to reduce "salt and pepper" type noise, as well as spikes, because each output pixel or element contains the median value of the m-by-n neighborhood around that corresponding pixel in the data.

For a more statistically and technically correct and thorough explanation, see Wikipedia.

In scientific software for statistical computing and graphics, the median of a numeric vector can be found by function median, or quantile with prob = 0.5.

1504 questions
274
votes
38 answers

Function to Calculate Median in SQL Server

According to MSDN, Median is not available as an aggregate function in Transact-SQL. However, I would like to find out whether it is possible to create this functionality (using the Create Aggregate function, user defined function, or some other…
Yaakov Ellis
  • 40,752
  • 27
  • 129
  • 174
273
votes
28 answers

Finding median of list in Python

How do you find the median of a list in Python? The list can be of any size and the numbers are not guaranteed to be in any particular order. If the list contains an even number of elements, the function should return the average of the middle…
ChucksPlace
  • 2,779
  • 2
  • 12
  • 4
262
votes
50 answers

Simple way to calculate median with MySQL

What's the simplest (and hopefully not too slow) way to calculate the median with MySQL? I've used AVG(x) for finding the mean, but I'm having a hard time finding a simple way of calculating the median. For now, I'm returning all the rows to PHP,…
davr
  • 18,877
  • 17
  • 76
  • 99
254
votes
10 answers

Find running median from a stream of integers

Possible Duplicate: Rolling median algorithm in C Given that integers are read from a data stream. Find median of elements read so far in efficient way. Solution I have read: We can use a max heap on left side to represent elements that are…
Luv
  • 5,381
  • 9
  • 48
  • 61
127
votes
13 answers

Rolling median algorithm in C

I am currently working on an algorithm to implement a rolling median filter (analogous to a rolling mean filter) in C. From my search of the literature, there appear to be two reasonably efficient ways to do it. The first is to sort the initial…
AWB
  • 1,443
  • 2
  • 10
  • 8
88
votes
14 answers

"On-line" (iterator) algorithms for estimating statistical median, mode, skewness, kurtosis?

Is there an algorithm to estimate the median, mode, skewness, and/or kurtosis of set of values, but that does NOT require storing all the values in memory at once? I'd like to calculate the basic statistics: mean: arithmetic average variance: …
Ryan B. Lynch
  • 2,307
  • 3
  • 21
  • 21
85
votes
9 answers

How to find median and quantiles using Spark

How can I find median of an RDD of integers using a distributed method, IPython, and Spark? The RDD is approximately 700,000 elements and therefore too large to collect and find the median. This question is similar to this question: How can I…
pr338
  • 8,730
  • 19
  • 52
  • 71
83
votes
12 answers

Calculate median in c#

I need to write function that will accept array of decimals and it will find the median. Is there a function in the .net Math library?
WingMan20-10
  • 3,594
  • 9
  • 31
  • 42
79
votes
8 answers

Median / quantiles within PySpark groupBy

I would like to calculate group quantiles on a Spark dataframe (using PySpark). Either an approximate or exact result would be fine. I prefer a solution that I can use within the context of groupBy / agg, so that I can mix it with other PySpark…
abeboparebop
  • 7,396
  • 6
  • 37
  • 46
70
votes
9 answers

Finding the median of an unsorted array

To find the median of an unsorted array, we can make a min-heap in O(nlogn) time for n elements, and then we can extract one by one n/2 elements to get the median. But this approach would take O(nlogn) time. Can we do the same by some method in O(n)…
Luv
  • 5,381
  • 9
  • 48
  • 61
63
votes
10 answers

What is the right approach when using STL container for median calculation?

Let's say I need to retrieve the median from a sequence of 1000000 random numeric values. If using anything but std::list, I have no (built-in) way to sort sequence for median calculation. If using std::list, I can't randomly access values to…
sharkin
  • 12,162
  • 24
  • 86
  • 122
58
votes
12 answers

Calculating Median in Ruby

How do I calculate the median of an array of numbers using Ruby? I am a beginner and am struggling with handling the cases of the array being of odd and even length.
tbone
  • 892
  • 1
  • 7
  • 17
54
votes
17 answers

Calculating median - javascript

I've been trying to calculate median but still I've got some mathematical issues I guess as I couldn't get the correct median value and couldn't figure out why. Here's the code; class StatsCollector { constructor() { this.inputNumber =…
Muhammed Bayram
  • 553
  • 1
  • 4
  • 5
52
votes
7 answers

How do I find the median of numbers in linear time using heaps?

Wikipedia says: Selection algorithms: Finding the min, max, both the min and max, median, or even the k-th largest element can be done in linear time using heaps. All it says is that it can be done, and not how. Can you give me some start on how…
Lazer
  • 90,700
  • 113
  • 281
  • 364
52
votes
25 answers

Fastest way of finding the middle value of a triple?

Given is an array of three numeric values and I'd like to know the middle value of the three. The question is, what is the fastest way of finding the middle of the three? My approach is this kind of pattern - as there are three numbers there are six…
Gnark
  • 4,080
  • 7
  • 33
  • 44
1
2 3
99 100