0

I need help in understanding how the quantile function works in numpy. because the answers do not match with other quantile calculators.

arr = [1,2,3,4,5] 

print("arr : ", arr)  
print("Q2 quantile of arr : ", np.quantile(arr, .50)) 
print("Q1 quantile of arr : ", np.quantile(arr, .25)) 
print("Q3 quantile of arr : ", np.quantile(arr, .75))  

I expected the output of Q1 to be 1.5 and Q3 to be 4.5 I verified it using an online calculator. It also differs from the answer given by numpy . Can someone help me in understanding how numpy calculates quantile and why is it different from the actual formula .

Daniel F
  • 13,620
  • 2
  • 29
  • 55
  • Possible duplicate of [Is scipy.stats doing wrong calculation for iqr?](https://stackoverflow.com/questions/51943661/is-scipy-stats-doing-wrong-calculation-for-iqr) – Daniel F Jul 26 '19 at 06:01
  • There is no difinitive method for calculating quartiles in literature. `numpy` does `precentile(arr, 25)`, which is an approved method, but many others methods split the array twice, which removes the median when the array has an odd number of values. – Daniel F Jul 26 '19 at 06:04

1 Answers1

1

For some ungodly reason statistics as a discipline hasn't defined exactly what a quartile is, despite using it in many "standard" formulas. Here's three different methods of calculating it which which are all very different. Luckily when datasets are large the differences are small, but with only five values . . . well, you can get some very big differences.

  • Your online calculator uses method 1 and returns [1.5, 4.5].

  • The answer with method 2 would be [2, 4]

  • numpy uses method 3 and returns [2, 4]

It seems to me like methods 2 and 3 would always give the same answer but there may be differences I haven't seen.

Daniel F
  • 13,620
  • 2
  • 29
  • 55
  • I dont think i am getting [2,4] as an answer using method 3 . there are 5 elements so n=1 . 25% of 1st data value = 0.25 . 75% of the 2nd (n+1) data value = 1.5 . so Q1 = 1.75 – Pradeepta Ranjan Choudhury Jul 27 '19 at 16:03
  • Also using the 1st example given in the wikipedia link the answers are different .according to numpy Q2 quantile of arr : 40.0 Q1 quantile of arr : 25.5 Q3 quantile of arr : 42.5 – Pradeepta Ranjan Choudhury Jul 27 '19 at 16:05