Gamestudio Links
Zorro Links
Newest Posts
Camera always moves upwards?
by clonman. 11/13/25 14:04
brokerCommand PLOT_HLINE parameters
by M_D. 11/13/25 10:42
ZorroGPT
by TipmyPip. 11/10/25 11:04
Training with the R bridge does not work
by frutza. 11/05/25 00:46
Zorro 2.70
by opm. 10/24/25 03:44
Alpaca Plugin v1.4.0
by TipmyPip. 10/20/25 18:04
AUM Magazine
Latest Screens
Rocker`s Revenge
Stug 3 Stormartillery
Iljuschin 2
Galactic Strike X
Who's Online Now
5 registered members (Grant, AndrewAMD, ozgur, Quad, TipmyPip), 29,980 guests, and 3 spiders.
Key: Admin, Global Mod, Mod
Newest Members
sheliepaley, Blueguy, blobplayintennis, someone2, NotEBspark
19177 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,029
Frankfurt
jcl Offline

Chief Engineer
jcl  Offline

Chief Engineer

Joined: Jul 2000
Posts: 28,029
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