From 87855b7b24617c15d8257a4da2b34cc948550807 Mon Sep 17 00:00:00 2001 From: Dusan Milovanovic Date: Sun, 23 Apr 2017 21:43:56 +0200 Subject: [PATCH] `def quantile (x, p)` correction in statistics.py If the value `p = 1 (100%)` is chosen, `sorted(x)[p_index]` is out of bounds because `p_index = p * len(x) = 1 * len(x) = len(x)` and the list `x` is indexed from `[0]` to `[len(x)-1]`. The proposed correction is to change line 50 of statistics.py from `p_index = int(p * len(x)) - 1` to `p_index = int(p * len(x)) - 1`. --- code/statistics.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/code/statistics.py b/code/statistics.py index 9db936c6..c80d3c05 100644 --- a/code/statistics.py +++ b/code/statistics.py @@ -47,7 +47,7 @@ def median(v): def quantile(x, p): """returns the pth-percentile value in x""" - p_index = int(p * len(x)) + p_index = int(p * len(x)) - 1 # for sorted list x is indexed from 0 to len(x)-1 return sorted(x)[p_index] def mode(x): @@ -132,4 +132,4 @@ def correlation(x, y): print "covariance(num_friends, daily_minutes)", covariance(num_friends, daily_minutes) print "correlation(num_friends, daily_minutes)", correlation(num_friends, daily_minutes) print "correlation(num_friends_good, daily_minutes_good)", correlation(num_friends_good, daily_minutes_good) - \ No newline at end of file +