Gamestudio Links
Zorro Links
Newest Posts
Trading Journey
by howardR. 04/28/24 09:55
basik85278
by basik85278. 04/28/24 08:56
Zorro Trader GPT
by TipmyPip. 04/27/24 13:50
Help with plotting multiple ZigZag
by M_D. 04/26/24 20:03
Data from CSV not parsed correctly
by jcl. 04/26/24 11:18
M1 Oversampling
by jcl. 04/26/24 11:12
Why Zorro supports up to 72 cores?
by jcl. 04/26/24 11:09
Eigenwerbung
by jcl. 04/26/24 11:08
AUM Magazine
Latest Screens
The Bible Game
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Who's Online Now
2 registered members (SBGuy, Quad), 768 guests, and 5 spiders.
Key: Admin, Global Mod, Mod
Newest Members
wandaluciaia, Mega_Rod, EternallyCurious, howardR, 11honza11
19049 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,986
Frankfurt
jcl Offline

Chief Engineer
jcl  Offline

Chief Engineer

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