Gamestudio Links
Zorro Links
Newest Posts
Data from CSV not parsed correctly
by EternallyCurious. 04/25/24 10:20
Trading Journey
by howardR. 04/24/24 20:04
M1 Oversampling
by Petra. 04/24/24 10:34
Zorro FIX plugin - Experimental
by flink. 04/21/24 07:12
Scripts not found
by juergen_wue. 04/20/24 18:51
zorro 64bit command line support
by 7th_zorro. 04/20/24 10:06
StartWeek not working as it should
by jcl. 04/20/24 08:38
folder management functions
by VoroneTZ. 04/17/24 06:52
AUM Magazine
Latest Screens
The Bible Game
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Who's Online Now
4 registered members (AndrewAMD, Quad, EternallyCurious, 1 invisible), 726 guests, and 5 spiders.
Key: Admin, Global Mod, Mod
Newest Members
Mega_Rod, EternallyCurious, howardR, 11honza11, ccorrea
19048 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: 27,982
Frankfurt
jcl Offline

Chief Engineer
jcl  Offline

Chief Engineer

Joined: Jul 2000
Posts: 27,982
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