Gamestudio Links
Zorro Links
Newest Posts
SGT_FW
by Aku_Aku. 11/30/26 08:19
Removing outliers from files
by JeyKey II. 11/29/26 10:01
Experiences with cTrader
by JeyKey II. 11/27/26 09:04
make a complaint
by EW_bytes. 11/26/26 23:00
FXCM => AVA Trade Plugin???
by Martin_HH. 11/25/26 21:59
No connection to MT4
by Martin_HH. 11/23/26 06:38
MinMax function on History t6 file
by LucasJoshua. 11/21/26 14:28
What are you working on?
by rayp. 11/20/26 18:10
AUM Magazine
Latest Screens
Dorifto samurai
Shadow 2
Rocker`s Revenge
Stug 3 Stormartillery
Who's Online Now
4 registered members (TipmyPip, JeyKey II, Martin_HH, Aku_Aku), 12,430 guests, and 1 spider.
Key: Admin, Global Mod, Mod
Newest Members
EvanTJ, rht, MaliceTheMaker, Haitao_Ren, JimSimons
19250 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
Median calculation gives dodgy result #468108
09/18/17 21:43
09/18/17 21:43
Joined: Aug 2017
Posts: 40
J
johnnyp Offline OP
Newbie
johnnyp  Offline OP
Newbie
J

Joined: Aug 2017
Posts: 40
Code:
var a[4] = {1, 4, 3, 5};
printf("n%f", Median(a, 4));
printf("n%f", Percentile(a, 4, 50));

# output
4.500000
4.500000



Unless I am much mistaken the median value should be 3.5

Median() always gives the right answer when length is even, and never when length is odd.

Last edited by johnnyp; 09/18/17 22:01.
Re: Median calculation gives dodgy result [Re: johnnyp] #468111
09/19/17 00:06
09/19/17 00:06
Joined: Sep 2017
Posts: 7
TX
F
flare9x Offline
Newbie
flare9x  Offline
Newbie
F

Joined: Sep 2017
Posts: 7
TX
Interesting - may have a bug there.

I tested in R:

Code:
x <- c(1,3,4,5)
median(x)
[1] 3.5



Can you try maybe the R bridge, maybe the R function can calculate it correctly?

Last edited by flare9x; 09/19/17 00:08.
Re: Median calculation gives dodgy result [Re: flare9x] #468125
09/19/17 09:55
09/19/17 09:55
Joined: Jul 2000
Posts: 28,149
Frankfurt
jcl Offline

Chief Engineer
jcl  Offline

Chief Engineer

Joined: Jul 2000
Posts: 28,149
Frankfurt
I would also expect that the median in that example is 3.5 and not 4.5. The Median function calls the Percentile function and the Percentile function is from some statistics library. Maybe that library does not consider even lengths. I'll forward that to the developers to check.

Re: Median calculation gives dodgy result [Re: jcl] #468129
09/19/17 12:53
09/19/17 12:53
Joined: Aug 2017
Posts: 40
J
johnnyp Offline OP
Newbie
johnnyp  Offline OP
Newbie
J

Joined: Aug 2017
Posts: 40
I wrote my own version of Percentile.

Code:
var percentile(vars data, int length, var percent)
{
	int* ids = sortIdx(data, length);
	var pos = (length-1)*percent/100;
	var below = data[ids[min(length-1, (int)pos)]];
	if(pos == round(pos))
		return below;
	var above = data[ids[min(length-1, (int)pos+1)]];
	return (below+above)/2;
}



Moderated by  Petra 

Powered by UBB.threads™ PHP Forum Software 7.7.1