median

How can I calculate the median of values in SQLite?

大兔子大兔子 提交于 2019-11-28 21:15:37
I'd like to calculate the median value in a numeric row. How can I do that in SQLite 4? Let's say that the median is the element in the middle of an ordered list. SQLite (4 or 3) does not have any built-in function for that, but it's possible to do this by hand: SELECT x FROM MyTable ORDER BY x LIMIT 1 OFFSET (SELECT COUNT(*) FROM MyTable) / 2 When there is an even number of records, it is common to define the median as the average of the two middle records. In this case, the average can be computed like this: SELECT AVG(x) FROM (SELECT x FROM MyTable ORDER BY x LIMIT 2 OFFSET (SELECT (COUNT(*

Incremental median computation with max memory efficiency

狂风中的少年 提交于 2019-11-28 09:42:01
I have a process that generates values and that I observe. When the process terminates, I want to compute the median of those values. If I had to compute the mean, I could just store the sum and the number of generated values and thus have O(1) memory requirement. How about the median? Is there a way to save on the obvious O(n) coming from storing all the values? Edit: Interested in 2 cases: 1) the stream length is known, 2) it's not. You are going to need to store at least ceil(n/2) points, because any one of the first n/2 points could be the median. It is probably simplest to just store the

Why does median trip up data.table (integer versus double)?

不想你离开。 提交于 2019-11-28 06:17:38
I have a data.table called enc.per.day for encounters per day. It has 2403 rows in which a date of service is specified and the number of patients seen on that day. I wanted to see the median number of patients seen on any type of weekday. enc.per.day[,list(patient.encounters=median(n)),by=list(weekdays(DOS))] That line gives an error Error in [.data.table (enc.per.day, , list(patient.encounters = median(n)), : columns of j don't evaluate to consistent types for each group: result for group 4 has column 1 type 'integer' but expecting type 'double' The following all work well tapply(enc.per.day

how to calculate running median efficiently

耗尽温柔 提交于 2019-11-28 05:47:18
问题 I borrowed some code trying to implement a function to calculate the running median for a ton of data. The current one is too slow for me ( The tricky part is that I need to exclude all zeros from the running box ). Below is the code: from itertools import islice from collections import deque from bisect import bisect_left,insort def median(s): sp = [nz for nz in s if nz!=0] print sp Mnow = len(sp) if Mnow == 0: return 0 else: return np.median(sp) def RunningMedian(seq, M): seq = iter(seq) s

How can I calculate the median of values in SQLite?

陌路散爱 提交于 2019-11-27 20:54:23
问题 I'd like to calculate the median value in a numeric row. How can I do that in SQLite 4? 回答1: Let's say that the median is the element in the middle of an ordered list. SQLite (4 or 3) does not have any built-in function for that, but it's possible to do this by hand: SELECT x FROM MyTable ORDER BY x LIMIT 1 OFFSET (SELECT COUNT(*) FROM MyTable) / 2 When there is an even number of records, it is common to define the median as the average of the two middle records. In this case, the average can

Code to calculate “median of five” in C#

放肆的年华 提交于 2019-11-27 18:32:28
Note: Please don't interpret this as "homework question." This is just a thing I curious to know :) The median of five is sometimes used as an exercise in algorithm design and is known to be computable using only 6 comparisons . What is the best way to implement this "median of five using 6 comparisons" in C# ? All of my attempts seem to result in awkward code :( I need nice and readable code while still using only 6 comparisons. public double medianOfFive(double a, double b, double c, double d, double e){ // // return median // return c; } Note: I think I should provide the "algorithm" here

Calculating the Median with Mysql

大城市里の小女人 提交于 2019-11-27 18:26:39
问题 I'm having trouble with calculating the median of a list of values, not the average. I found this article Simple way to calculate median with MySQL It has a reference to the following query which I don't understand properly. SELECT x.val from data x, data y GROUP BY x.val HAVING SUM(SIGN(1-SIGN(y.val-x.val))) = (COUNT(*)+1)/2 If I have a time column and I want to calculate the median value, what do the x and y columns refer to? 回答1: val is your time column, x and y are two references to the

median of two sorted arrays

♀尐吖头ヾ 提交于 2019-11-27 18:04:06
问题 My question is with reference to Method 2 of this link. Here two equal length sorted arrays are given and we have to find the median of the two arrays merged. Algorithm: 1) Calculate the medians m1 and m2 of the input arrays ar1[] and ar2[] respectively. 2) If m1 and m2 both are equal then we are done. return m1 (or m2) 3) If m1 is greater than m2, then median is present in one of the below two subarrays. a) From first element of ar1 to m1 (ar1[0...|_n/2_|]) b) From m2 to last element of ar2

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

谁说胖子不能爱 提交于 2019-11-27 16:54:37
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 this can be done using heaps? You would use a min-max-median heap to find the min, max and median in constant time (and take linear time to build the heap). You can use order-statistics trees to find the kth smallest/largest value. Both of these data structures are described in this paper on min-max heaps [pdf link] . Min-max heaps are binary heaps

Median of Medians in Java

大兔子大兔子 提交于 2019-11-27 14:15:15
I am trying to implement Median of Medians in Java for a method like this: Select(Comparable[] list, int pos, int colSize, int colMed) list is a list of values of which to find a specified position pos is the specified position colSize is the size of the columns that I create in the first stage colMed is the position in those columns that I use as the medX I am not sure which sorting algorithm would be the best to use or how to implement this exactly.. I don't know if you still need this problem solved, but http://www.ics.uci.edu/~eppstein/161/960130.html has an algorithm: select(L,k) { if (L