Percentiles.html 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640
  1. ---
  2. redirect_from:
  3. - "/chapters/13/1/percentiles"
  4. interact_link: content/chapters/13/1/Percentiles.ipynb
  5. kernel_name: python3
  6. has_widgets: false
  7. title: |-
  8. Percentiles
  9. prev_page:
  10. url: /chapters/13/Estimation.html
  11. title: |-
  12. Estimation
  13. next_page:
  14. url: /chapters/13/2/Bootstrap.html
  15. title: |-
  16. The Bootstrap
  17. comment: "***PROGRAMMATICALLY GENERATED, DO NOT EDIT. SEE ORIGINAL FILES IN /content***"
  18. ---
  19. <div class="jb_cell tag_remove_input">
  20. <div class="cell border-box-sizing code_cell rendered">
  21. </div>
  22. </div>
  23. <div class="jb_cell">
  24. <div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
  25. <div class="text_cell_render border-box-sizing rendered_html">
  26. <h3 id="Percentiles">Percentiles<a class="anchor-link" href="#Percentiles"> </a></h3><p>Numerical data can be sorted in increasing or decreasing order. Thus the values of a numerical data set have a <em>rank order</em>. A percentile is the value at a particular rank.</p>
  27. <p>For example, if your score on a test is on the 95th percentile, a common interpretation is that only 5% of the scores were higher than yours. The median is the 50th percentile; it is commonly assumed that 50% the values in a data set are above the median.</p>
  28. <p>But some care is required in giving percentiles a precise definition that works for all ranks and all lists. To see why, consider an extreme example where all the students in a class score 75 on a test. Then 75 is a natural candidate for the median, but it's not true that 50% of the scores are above 75. Also, 75 is an equally natural candidate for the 95th percentile or the 25th or any other percentile. Ties – that is, equal data values – have to be taken into account when defining percentiles.</p>
  29. <p>You also have to be careful about exactly how far up the list to go when the relevant index isn't clear. For example, what should be the 87th percentile of a collection of 10 values? The 8th value of the sorted collection, or the 9th, or somewhere in between?</p>
  30. <p>In this section, we will give a definition that works consistently for all ranks and all lists.</p>
  31. </div>
  32. </div>
  33. </div>
  34. </div>
  35. <div class="jb_cell">
  36. <div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
  37. <div class="text_cell_render border-box-sizing rendered_html">
  38. <h3 id="A-Numerical-Example">A Numerical Example<a class="anchor-link" href="#A-Numerical-Example"> </a></h3><p>Before giving a general definition of all percentiles, we will define the 80th percentile of a collection of values to be the smallest value in the collection that is at least as large as 80% of all of the values.</p>
  39. <p>For example, let's consider the sizes of the five largest continents – Africa, Antarctica, Asia, North America, and South America – rounded to the nearest million square miles.</p>
  40. </div>
  41. </div>
  42. </div>
  43. </div>
  44. <div class="jb_cell">
  45. <div class="cell border-box-sizing code_cell rendered">
  46. <div class="input">
  47. <div class="inner_cell">
  48. <div class="input_area">
  49. <div class=" highlight hl-ipython3"><pre><span></span><span class="n">sizes</span> <span class="o">=</span> <span class="n">make_array</span><span class="p">(</span><span class="mi">12</span><span class="p">,</span> <span class="mi">17</span><span class="p">,</span> <span class="mi">6</span><span class="p">,</span> <span class="mi">9</span><span class="p">,</span> <span class="mi">7</span><span class="p">)</span>
  50. </pre></div>
  51. </div>
  52. </div>
  53. </div>
  54. </div>
  55. </div>
  56. <div class="jb_cell">
  57. <div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
  58. <div class="text_cell_render border-box-sizing rendered_html">
  59. <p>The 80th percentile is the smallest value that is at least as large as 80% of the elements of <code>sizes</code>, that is, four-fifths of the five elements. That's 12:</p>
  60. </div>
  61. </div>
  62. </div>
  63. </div>
  64. <div class="jb_cell">
  65. <div class="cell border-box-sizing code_cell rendered">
  66. <div class="input">
  67. <div class="inner_cell">
  68. <div class="input_area">
  69. <div class=" highlight hl-ipython3"><pre><span></span><span class="n">np</span><span class="o">.</span><span class="n">sort</span><span class="p">(</span><span class="n">sizes</span><span class="p">)</span>
  70. </pre></div>
  71. </div>
  72. </div>
  73. </div>
  74. <div class="output_wrapper">
  75. <div class="output">
  76. <div class="jb_output_wrapper }}">
  77. <div class="output_area">
  78. <div class="output_text output_subarea output_execute_result">
  79. <pre>array([ 6, 7, 9, 12, 17])</pre>
  80. </div>
  81. </div>
  82. </div>
  83. </div>
  84. </div>
  85. </div>
  86. </div>
  87. <div class="jb_cell">
  88. <div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
  89. <div class="text_cell_render border-box-sizing rendered_html">
  90. <p>The 80th percentile is a value on the list, namely 12. You can see that 80% of the values are less than or equal to it, and that it is the smallest value on the list for which this is true.</p>
  91. <p>Analogously, the 70th percentile is the smallest value in the collection that is at least as large as 70% of the elements of <code>sizes</code>. Now 70% of 5 elements is "3.5 elements", so the 70th percentile is the 4th element on the list. That's 12, the same as the 80th percentile for these data.</p>
  92. </div>
  93. </div>
  94. </div>
  95. </div>
  96. <div class="jb_cell">
  97. <div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
  98. <div class="text_cell_render border-box-sizing rendered_html">
  99. <h3 id="The-percentile-function">The <code>percentile</code> function<a class="anchor-link" href="#The-percentile-function"> </a></h3><p>The <code>percentile</code> function takes two arguments: a rank between 0 and 100, and a array. It returns the corresponding percentile of the array.</p>
  100. </div>
  101. </div>
  102. </div>
  103. </div>
  104. <div class="jb_cell">
  105. <div class="cell border-box-sizing code_cell rendered">
  106. <div class="input">
  107. <div class="inner_cell">
  108. <div class="input_area">
  109. <div class=" highlight hl-ipython3"><pre><span></span><span class="n">percentile</span><span class="p">(</span><span class="mi">70</span><span class="p">,</span> <span class="n">sizes</span><span class="p">)</span>
  110. </pre></div>
  111. </div>
  112. </div>
  113. </div>
  114. <div class="output_wrapper">
  115. <div class="output">
  116. <div class="jb_output_wrapper }}">
  117. <div class="output_area">
  118. <div class="output_text output_subarea output_execute_result">
  119. <pre>12</pre>
  120. </div>
  121. </div>
  122. </div>
  123. </div>
  124. </div>
  125. </div>
  126. </div>
  127. <div class="jb_cell">
  128. <div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
  129. <div class="text_cell_render border-box-sizing rendered_html">
  130. <h3 id="The-General-Definition">The General Definition<a class="anchor-link" href="#The-General-Definition"> </a></h3><p>Let $p$ be a number between 0 and 100. The $p$th percentile of a collection is the smallest value in the collection that is at least as large as p% of all the values.</p>
  131. <p>By this definition, any percentile between 0 and 100 can be computed for any collection of values, and it is always an element of the collection.</p>
  132. <p>In practical terms, suppose there are $n$ elements in the collection. To find the $p$th percentile:</p>
  133. <ul>
  134. <li>Sort the collection in increasing order.</li>
  135. <li>Find p% of n: $(p/100) \times n$. Call that $k$.</li>
  136. <li>If $k$ is an integer, take the $k$th element of the sorted collection.</li>
  137. <li>If $k$ is not an integer, round it up to the next integer, and take that element of the sorted collection.</li>
  138. </ul>
  139. </div>
  140. </div>
  141. </div>
  142. </div>
  143. <div class="jb_cell">
  144. <div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
  145. <div class="text_cell_render border-box-sizing rendered_html">
  146. <h3 id="Example">Example<a class="anchor-link" href="#Example"> </a></h3><p>The table <code>scores_and_sections</code> contains one row for each student in a class of 359 students. The columns are the student's discussion section and midterm score.</p>
  147. </div>
  148. </div>
  149. </div>
  150. </div>
  151. <div class="jb_cell">
  152. <div class="cell border-box-sizing code_cell rendered">
  153. <div class="input">
  154. <div class="inner_cell">
  155. <div class="input_area">
  156. <div class=" highlight hl-ipython3"><pre><span></span><span class="n">scores_and_sections</span> <span class="o">=</span> <span class="n">Table</span><span class="o">.</span><span class="n">read_table</span><span class="p">(</span><span class="n">path_data</span> <span class="o">+</span> <span class="s1">&#39;scores_by_section.csv&#39;</span><span class="p">)</span>
  157. <span class="n">scores_and_sections</span>
  158. </pre></div>
  159. </div>
  160. </div>
  161. </div>
  162. <div class="output_wrapper">
  163. <div class="output">
  164. <div class="jb_output_wrapper }}">
  165. <div class="output_area">
  166. <div class="output_html rendered_html output_subarea output_execute_result">
  167. <table border="1" class="dataframe">
  168. <thead>
  169. <tr>
  170. <th>Section</th> <th>Midterm</th>
  171. </tr>
  172. </thead>
  173. <tbody>
  174. <tr>
  175. <td>1 </td> <td>22 </td>
  176. </tr>
  177. <tr>
  178. <td>2 </td> <td>12 </td>
  179. </tr>
  180. <tr>
  181. <td>2 </td> <td>23 </td>
  182. </tr>
  183. <tr>
  184. <td>2 </td> <td>14 </td>
  185. </tr>
  186. <tr>
  187. <td>1 </td> <td>20 </td>
  188. </tr>
  189. <tr>
  190. <td>3 </td> <td>25 </td>
  191. </tr>
  192. <tr>
  193. <td>4 </td> <td>19 </td>
  194. </tr>
  195. <tr>
  196. <td>1 </td> <td>24 </td>
  197. </tr>
  198. <tr>
  199. <td>5 </td> <td>8 </td>
  200. </tr>
  201. <tr>
  202. <td>6 </td> <td>14 </td>
  203. </tr>
  204. </tbody>
  205. </table>
  206. <p>... (349 rows omitted)</p>
  207. </div>
  208. </div>
  209. </div>
  210. </div>
  211. </div>
  212. </div>
  213. </div>
  214. <div class="jb_cell">
  215. <div class="cell border-box-sizing code_cell rendered">
  216. <div class="input">
  217. <div class="inner_cell">
  218. <div class="input_area">
  219. <div class=" highlight hl-ipython3"><pre><span></span><span class="n">scores_and_sections</span><span class="o">.</span><span class="n">select</span><span class="p">(</span><span class="s1">&#39;Midterm&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">hist</span><span class="p">(</span><span class="n">bins</span><span class="o">=</span><span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="o">-</span><span class="mf">0.5</span><span class="p">,</span> <span class="mf">25.6</span><span class="p">,</span> <span class="mi">1</span><span class="p">))</span>
  220. </pre></div>
  221. </div>
  222. </div>
  223. </div>
  224. <div class="output_wrapper">
  225. <div class="output">
  226. <div class="jb_output_wrapper }}">
  227. <div class="output_area">
  228. <div class="output_subarea output_stream output_stderr output_text">
  229. <pre>/home/choldgraf/anaconda/envs/textbook/lib/python3.6/site-packages/matplotlib/axes/_axes.py:6462: UserWarning: The &#39;normed&#39; kwarg is deprecated, and has been replaced by the &#39;density&#39; kwarg.
  230. warnings.warn(&#34;The &#39;normed&#39; kwarg is deprecated, and has been &#34;
  231. </pre>
  232. </div>
  233. </div>
  234. </div>
  235. <div class="jb_output_wrapper }}">
  236. <div class="output_area">
  237. <div class="output_png output_subarea ">
  238. <img src="../../../images/chapters/13/1/Percentiles_12_1.png"
  239. >
  240. </div>
  241. </div>
  242. </div>
  243. </div>
  244. </div>
  245. </div>
  246. </div>
  247. <div class="jb_cell">
  248. <div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
  249. <div class="text_cell_render border-box-sizing rendered_html">
  250. <p>What was the 85th percentile of the scores? To use the <code>percentile</code> function, create an array <code>scores</code> containing the midterm scores, and find the 85th percentile:</p>
  251. </div>
  252. </div>
  253. </div>
  254. </div>
  255. <div class="jb_cell">
  256. <div class="cell border-box-sizing code_cell rendered">
  257. <div class="input">
  258. <div class="inner_cell">
  259. <div class="input_area">
  260. <div class=" highlight hl-ipython3"><pre><span></span><span class="n">scores</span> <span class="o">=</span> <span class="n">scores_and_sections</span><span class="o">.</span><span class="n">column</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>
  261. </pre></div>
  262. </div>
  263. </div>
  264. </div>
  265. </div>
  266. </div>
  267. <div class="jb_cell">
  268. <div class="cell border-box-sizing code_cell rendered">
  269. <div class="input">
  270. <div class="inner_cell">
  271. <div class="input_area">
  272. <div class=" highlight hl-ipython3"><pre><span></span><span class="n">percentile</span><span class="p">(</span><span class="mi">85</span><span class="p">,</span> <span class="n">scores</span><span class="p">)</span>
  273. </pre></div>
  274. </div>
  275. </div>
  276. </div>
  277. <div class="output_wrapper">
  278. <div class="output">
  279. <div class="jb_output_wrapper }}">
  280. <div class="output_area">
  281. <div class="output_text output_subarea output_execute_result">
  282. <pre>22</pre>
  283. </div>
  284. </div>
  285. </div>
  286. </div>
  287. </div>
  288. </div>
  289. </div>
  290. <div class="jb_cell">
  291. <div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
  292. <div class="text_cell_render border-box-sizing rendered_html">
  293. <p>According to the percentile function, the 85th percentile was 22. To check that this is consistent with our new definition, let's apply the definition directly.</p>
  294. <p>First, put the scores in increasing order:</p>
  295. </div>
  296. </div>
  297. </div>
  298. </div>
  299. <div class="jb_cell">
  300. <div class="cell border-box-sizing code_cell rendered">
  301. <div class="input">
  302. <div class="inner_cell">
  303. <div class="input_area">
  304. <div class=" highlight hl-ipython3"><pre><span></span><span class="n">sorted_scores</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">sort</span><span class="p">(</span><span class="n">scores_and_sections</span><span class="o">.</span><span class="n">column</span><span class="p">(</span><span class="mi">1</span><span class="p">))</span>
  305. </pre></div>
  306. </div>
  307. </div>
  308. </div>
  309. </div>
  310. </div>
  311. <div class="jb_cell">
  312. <div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
  313. <div class="text_cell_render border-box-sizing rendered_html">
  314. <p>There are 359 scores in the array. So next, find 85% of 359, which is 305.15.</p>
  315. </div>
  316. </div>
  317. </div>
  318. </div>
  319. <div class="jb_cell">
  320. <div class="cell border-box-sizing code_cell rendered">
  321. <div class="input">
  322. <div class="inner_cell">
  323. <div class="input_area">
  324. <div class=" highlight hl-ipython3"><pre><span></span><span class="mf">0.85</span> <span class="o">*</span> <span class="mi">359</span>
  325. </pre></div>
  326. </div>
  327. </div>
  328. </div>
  329. <div class="output_wrapper">
  330. <div class="output">
  331. <div class="jb_output_wrapper }}">
  332. <div class="output_area">
  333. <div class="output_text output_subarea output_execute_result">
  334. <pre>305.15</pre>
  335. </div>
  336. </div>
  337. </div>
  338. </div>
  339. </div>
  340. </div>
  341. </div>
  342. <div class="jb_cell">
  343. <div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
  344. <div class="text_cell_render border-box-sizing rendered_html">
  345. <p>That's not an integer. By our definition, the 85th percentile is the 306th element of <code>sorted_scores</code>, which, by Python's indexing convention, is item 305 of the array.</p>
  346. </div>
  347. </div>
  348. </div>
  349. </div>
  350. <div class="jb_cell">
  351. <div class="cell border-box-sizing code_cell rendered">
  352. <div class="input">
  353. <div class="inner_cell">
  354. <div class="input_area">
  355. <div class=" highlight hl-ipython3"><pre><span></span><span class="c1"># The 306th element of the sorted array</span>
  356. <span class="n">sorted_scores</span><span class="o">.</span><span class="n">item</span><span class="p">(</span><span class="mi">305</span><span class="p">)</span>
  357. </pre></div>
  358. </div>
  359. </div>
  360. </div>
  361. <div class="output_wrapper">
  362. <div class="output">
  363. <div class="jb_output_wrapper }}">
  364. <div class="output_area">
  365. <div class="output_text output_subarea output_execute_result">
  366. <pre>22</pre>
  367. </div>
  368. </div>
  369. </div>
  370. </div>
  371. </div>
  372. </div>
  373. </div>
  374. <div class="jb_cell">
  375. <div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
  376. <div class="text_cell_render border-box-sizing rendered_html">
  377. <p>That's the same as the answer we got by using <code>percentile</code>. In future, we will just use <code>percentile</code>.</p>
  378. </div>
  379. </div>
  380. </div>
  381. </div>
  382. <div class="jb_cell">
  383. <div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
  384. <div class="text_cell_render border-box-sizing rendered_html">
  385. <h3 id="Quartiles">Quartiles<a class="anchor-link" href="#Quartiles"> </a></h3><p>The <em>first quartile</em> of a numercial collection is the 25th percentile. The terminology arises from <em>the first quarter</em>. The second quartile is the median, and the third quartile is the 75th percentile.</p>
  386. <p>For our <code>scores</code> data, those values are:</p>
  387. </div>
  388. </div>
  389. </div>
  390. </div>
  391. <div class="jb_cell">
  392. <div class="cell border-box-sizing code_cell rendered">
  393. <div class="input">
  394. <div class="inner_cell">
  395. <div class="input_area">
  396. <div class=" highlight hl-ipython3"><pre><span></span><span class="n">percentile</span><span class="p">(</span><span class="mi">25</span><span class="p">,</span> <span class="n">scores</span><span class="p">)</span>
  397. </pre></div>
  398. </div>
  399. </div>
  400. </div>
  401. <div class="output_wrapper">
  402. <div class="output">
  403. <div class="jb_output_wrapper }}">
  404. <div class="output_area">
  405. <div class="output_text output_subarea output_execute_result">
  406. <pre>11</pre>
  407. </div>
  408. </div>
  409. </div>
  410. </div>
  411. </div>
  412. </div>
  413. </div>
  414. <div class="jb_cell">
  415. <div class="cell border-box-sizing code_cell rendered">
  416. <div class="input">
  417. <div class="inner_cell">
  418. <div class="input_area">
  419. <div class=" highlight hl-ipython3"><pre><span></span><span class="n">percentile</span><span class="p">(</span><span class="mi">50</span><span class="p">,</span> <span class="n">scores</span><span class="p">)</span>
  420. </pre></div>
  421. </div>
  422. </div>
  423. </div>
  424. <div class="output_wrapper">
  425. <div class="output">
  426. <div class="jb_output_wrapper }}">
  427. <div class="output_area">
  428. <div class="output_text output_subarea output_execute_result">
  429. <pre>16</pre>
  430. </div>
  431. </div>
  432. </div>
  433. </div>
  434. </div>
  435. </div>
  436. </div>
  437. <div class="jb_cell">
  438. <div class="cell border-box-sizing code_cell rendered">
  439. <div class="input">
  440. <div class="inner_cell">
  441. <div class="input_area">
  442. <div class=" highlight hl-ipython3"><pre><span></span><span class="n">percentile</span><span class="p">(</span><span class="mi">75</span><span class="p">,</span> <span class="n">scores</span><span class="p">)</span>
  443. </pre></div>
  444. </div>
  445. </div>
  446. </div>
  447. <div class="output_wrapper">
  448. <div class="output">
  449. <div class="jb_output_wrapper }}">
  450. <div class="output_area">
  451. <div class="output_text output_subarea output_execute_result">
  452. <pre>20</pre>
  453. </div>
  454. </div>
  455. </div>
  456. </div>
  457. </div>
  458. </div>
  459. </div>
  460. <div class="jb_cell">
  461. <div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
  462. <div class="text_cell_render border-box-sizing rendered_html">
  463. <p>Distributions of scores are sometimes summarized by the "middle 50%" interval, between the first and third quartiles.</p>
  464. </div>
  465. </div>
  466. </div>
  467. </div>