-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresampling_with_code.html
1865 lines (1830 loc) · 136 KB
/
resampling_with_code.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en"><head>
<meta charset="utf-8">
<meta name="generator" content="quarto-1.6.1">
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes">
<title>5 Resampling with code – Resampling statistics</title>
<style>
code{white-space: pre-wrap;}
span.smallcaps{font-variant: small-caps;}
div.columns{display: flex; gap: min(4vw, 1.5em);}
div.column{flex: auto; overflow-x: auto;}
div.hanging-indent{margin-left: 1.5em; text-indent: -1.5em;}
ul.task-list{list-style: none;}
ul.task-list li input[type="checkbox"] {
width: 0.8em;
margin: 0 0.8em 0.2em -1em; /* quarto-specific, see https://github.com/quarto-dev/quarto-cli/issues/4556 */
vertical-align: middle;
}
/* CSS for syntax highlighting */
pre > code.sourceCode { white-space: pre; position: relative; }
pre > code.sourceCode > span { line-height: 1.25; }
pre > code.sourceCode > span:empty { height: 1.2em; }
.sourceCode { overflow: visible; }
code.sourceCode > span { color: inherit; text-decoration: inherit; }
div.sourceCode { margin: 1em 0; }
pre.sourceCode { margin: 0; }
@media screen {
div.sourceCode { overflow: auto; }
}
@media print {
pre > code.sourceCode { white-space: pre-wrap; }
pre > code.sourceCode > span { display: inline-block; text-indent: -5em; padding-left: 5em; }
}
pre.numberSource code
{ counter-reset: source-line 0; }
pre.numberSource code > span
{ position: relative; left: -4em; counter-increment: source-line; }
pre.numberSource code > span > a:first-child::before
{ content: counter(source-line);
position: relative; left: -1em; text-align: right; vertical-align: baseline;
border: none; display: inline-block;
-webkit-touch-callout: none; -webkit-user-select: none;
-khtml-user-select: none; -moz-user-select: none;
-ms-user-select: none; user-select: none;
padding: 0 4px; width: 4em;
}
pre.numberSource { margin-left: 3em; padding-left: 4px; }
div.sourceCode
{ }
@media screen {
pre > code.sourceCode > span > a:first-child::before { text-decoration: underline; }
}
/* CSS for citations */
div.csl-bib-body { }
div.csl-entry {
clear: both;
margin-bottom: 0em;
}
.hanging-indent div.csl-entry {
margin-left:2em;
text-indent:-2em;
}
div.csl-left-margin {
min-width:2em;
float:left;
}
div.csl-right-inline {
margin-left:2em;
padding-left:1em;
}
div.csl-indent {
margin-left: 2em;
}</style>
<script src="site_libs/quarto-nav/quarto-nav.js"></script>
<script src="site_libs/quarto-nav/headroom.min.js"></script>
<script src="site_libs/clipboard/clipboard.min.js"></script>
<script src="site_libs/quarto-search/autocomplete.umd.js"></script>
<script src="site_libs/quarto-search/fuse.min.js"></script>
<script src="site_libs/quarto-search/quarto-search.js"></script>
<meta name="quarto:offset" content="./">
<link href="./resampling_with_code2.html" rel="next">
<link href="./about_technology.html" rel="prev">
<script src="site_libs/quarto-html/quarto.js"></script>
<script src="site_libs/quarto-html/popper.min.js"></script>
<script src="site_libs/quarto-html/tippy.umd.min.js"></script>
<script src="site_libs/quarto-html/anchor.min.js"></script>
<link href="site_libs/quarto-html/tippy.css" rel="stylesheet">
<link href="site_libs/quarto-html/quarto-syntax-highlighting.css" rel="stylesheet" id="quarto-text-highlighting-styles">
<script src="site_libs/bootstrap/bootstrap.min.js"></script>
<link href="site_libs/bootstrap/bootstrap-icons.css" rel="stylesheet">
<link href="site_libs/bootstrap/bootstrap.min.css" rel="stylesheet" id="quarto-bootstrap" data-mode="light">
<script id="quarto-search-options" type="application/json">{
"location": "sidebar",
"copy-button": false,
"collapse-after": 3,
"panel-placement": "start",
"type": "textbox",
"limit": 50,
"keyboard-shortcut": [
"f",
"/",
"s"
],
"show-item-context": false,
"language": {
"search-no-results-text": "No results",
"search-matching-documents-text": "matching documents",
"search-copy-link-title": "Copy link to search",
"search-hide-matches-text": "Hide additional matches",
"search-more-match-text": "more match in this document",
"search-more-matches-text": "more matches in this document",
"search-clear-button-title": "Clear",
"search-text-placeholder": "",
"search-detached-cancel-button-title": "Cancel",
"search-submit-button-title": "Submit",
"search-label": "Search"
}
}</script>
<script type="text/javascript">
$(document).ready(function() {
$("table").addClass('lightable-paper lightable-striped lightable-hover')
});
</script>
<script src="https://cdnjs.cloudflare.com/polyfill/v3/polyfill.min.js?features=es6"></script>
<script src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-chtml-full.js" type="text/javascript"></script>
<script type="text/javascript">
const typesetMath = (el) => {
if (window.MathJax) {
// MathJax Typeset
window.MathJax.typeset([el]);
} else if (window.katex) {
// KaTeX Render
var mathElements = el.getElementsByClassName("math");
var macros = [];
for (var i = 0; i < mathElements.length; i++) {
var texText = mathElements[i].firstChild;
if (mathElements[i].tagName == "SPAN") {
window.katex.render(texText.data, mathElements[i], {
displayMode: mathElements[i].classList.contains('display'),
throwOnError: false,
macros: macros,
fleqn: false
});
}
}
}
}
window.Quarto = {
typesetMath
};
</script>
<link rel="stylesheet" href="style.css">
<link rel="stylesheet" href="font-awesome.min.css">
</head>
<body class="nav-sidebar floating">
<div id="quarto-search-results"></div>
<header id="quarto-header" class="headroom fixed-top">
<nav class="quarto-secondary-nav">
<div class="container-fluid d-flex">
<button type="button" class="quarto-btn-toggle btn" data-bs-toggle="collapse" role="button" data-bs-target=".quarto-sidebar-collapse-item" aria-controls="quarto-sidebar" aria-expanded="false" aria-label="Toggle sidebar navigation" onclick="if (window.quartoToggleHeadroom) { window.quartoToggleHeadroom(); }">
<i class="bi bi-layout-text-sidebar-reverse"></i>
</button>
<nav class="quarto-page-breadcrumbs" aria-label="breadcrumb"><ol class="breadcrumb"><li class="breadcrumb-item"><a href="./resampling_with_code.html"><span class="chapter-number">5</span> <span class="chapter-title">Resampling with code</span></a></li></ol></nav>
<a class="flex-grow-1" role="navigation" data-bs-toggle="collapse" data-bs-target=".quarto-sidebar-collapse-item" aria-controls="quarto-sidebar" aria-expanded="false" aria-label="Toggle sidebar navigation" onclick="if (window.quartoToggleHeadroom) { window.quartoToggleHeadroom(); }">
</a>
<button type="button" class="btn quarto-search-button" aria-label="Search" onclick="window.quartoOpenSearch();">
<i class="bi bi-search"></i>
</button>
</div>
</nav>
</header>
<!-- content -->
<div id="quarto-content" class="quarto-container page-columns page-rows-contents page-layout-article">
<!-- sidebar -->
<nav id="quarto-sidebar" class="sidebar collapse collapse-horizontal quarto-sidebar-collapse-item sidebar-navigation floating overflow-auto">
<div class="pt-lg-2 mt-2 text-left sidebar-header">
<div class="sidebar-title mb-0 py-0">
<a href="./">Resampling statistics</a>
</div>
</div>
<div class="mt-2 flex-shrink-0 align-items-center">
<div class="sidebar-search">
<div id="quarto-search" class="" title="Search"></div>
</div>
</div>
<div class="sidebar-menu-container">
<ul class="list-unstyled mt-1">
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="./index.html" class="sidebar-item-text sidebar-link">
<span class="menu-text">Python version</span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="./preface_third.html" class="sidebar-item-text sidebar-link">
<span class="menu-text">Preface to the third edition</span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="./preface_second.html" class="sidebar-item-text sidebar-link">
<span class="menu-text">Preface to the second edition</span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="./intro.html" class="sidebar-item-text sidebar-link">
<span class="menu-text"><span class="chapter-number">1</span> <span class="chapter-title">Introduction</span></span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="./resampling_method.html" class="sidebar-item-text sidebar-link">
<span class="menu-text"><span class="chapter-number">2</span> <span class="chapter-title">The resampling method</span></span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="./what_is_probability.html" class="sidebar-item-text sidebar-link">
<span class="menu-text"><span class="chapter-number">3</span> <span class="chapter-title">What is probability?</span></span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="./about_technology.html" class="sidebar-item-text sidebar-link">
<span class="menu-text"><span class="chapter-number">4</span> <span class="chapter-title">Introducing Python and the Jupyter notebook</span></span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="./resampling_with_code.html" class="sidebar-item-text sidebar-link active">
<span class="menu-text"><span class="chapter-number">5</span> <span class="chapter-title">Resampling with code</span></span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="./resampling_with_code2.html" class="sidebar-item-text sidebar-link">
<span class="menu-text"><span class="chapter-number">6</span> <span class="chapter-title">More resampling with code</span></span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="./sampling_tools.html" class="sidebar-item-text sidebar-link">
<span class="menu-text"><span class="chapter-number">7</span> <span class="chapter-title">Tools for samples and sampling</span></span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="./probability_theory_1a.html" class="sidebar-item-text sidebar-link">
<span class="menu-text"><span class="chapter-number">8</span> <span class="chapter-title">Probability Theory, Part 1</span></span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="./probability_theory_1b.html" class="sidebar-item-text sidebar-link">
<span class="menu-text"><span class="chapter-number">9</span> <span class="chapter-title">Probability Theory Part 1 (continued)</span></span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="./more_sampling_tools.html" class="sidebar-item-text sidebar-link">
<span class="menu-text"><span class="chapter-number">10</span> <span class="chapter-title">Two puzzles and more tools</span></span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="./probability_theory_2_compound.html" class="sidebar-item-text sidebar-link">
<span class="menu-text"><span class="chapter-number">11</span> <span class="chapter-title">Probability Theory, Part 2: Compound Probability</span></span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="./probability_theory_3.html" class="sidebar-item-text sidebar-link">
<span class="menu-text"><span class="chapter-number">12</span> <span class="chapter-title">Probability Theory, Part 3</span></span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="./probability_theory_4_finite.html" class="sidebar-item-text sidebar-link">
<span class="menu-text"><span class="chapter-number">13</span> <span class="chapter-title">Probability Theory, Part 4: Estimating Probabilities from Finite Universes</span></span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="./sampling_variability.html" class="sidebar-item-text sidebar-link">
<span class="menu-text"><span class="chapter-number">14</span> <span class="chapter-title">On Variability in Sampling</span></span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="./monte_carlo.html" class="sidebar-item-text sidebar-link">
<span class="menu-text"><span class="chapter-number">15</span> <span class="chapter-title">The Procedures of Monte Carlo Simulation (and Resampling)</span></span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="./standard_scores.html" class="sidebar-item-text sidebar-link">
<span class="menu-text"><span class="chapter-number">16</span> <span class="chapter-title">Ranks, Quantiles and Standard Scores</span></span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="./inference_ideas.html" class="sidebar-item-text sidebar-link">
<span class="menu-text"><span class="chapter-number">17</span> <span class="chapter-title">The Basic Ideas in Statistical Inference</span></span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="./inference_intro.html" class="sidebar-item-text sidebar-link">
<span class="menu-text"><span class="chapter-number">18</span> <span class="chapter-title">Introduction to Statistical Inference</span></span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="./point_estimation.html" class="sidebar-item-text sidebar-link">
<span class="menu-text"><span class="chapter-number">19</span> <span class="chapter-title">Point Estimation</span></span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="./framing_questions.html" class="sidebar-item-text sidebar-link">
<span class="menu-text"><span class="chapter-number">20</span> <span class="chapter-title">Framing Statistical Questions</span></span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="./testing_counts_1.html" class="sidebar-item-text sidebar-link">
<span class="menu-text"><span class="chapter-number">21</span> <span class="chapter-title">Hypothesis-Testing with Counted Data, Part 1</span></span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="./significance.html" class="sidebar-item-text sidebar-link">
<span class="menu-text"><span class="chapter-number">22</span> <span class="chapter-title">The Concept of Statistical Significance in Testing Hypotheses</span></span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="./testing_counts_2.html" class="sidebar-item-text sidebar-link">
<span class="menu-text"><span class="chapter-number">23</span> <span class="chapter-title">The Statistics of Hypothesis-Testing with Counted Data, Part 2</span></span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="./testing_measured.html" class="sidebar-item-text sidebar-link">
<span class="menu-text"><span class="chapter-number">24</span> <span class="chapter-title">The Statistics of Hypothesis-Testing With Measured Data</span></span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="./testing_procedures.html" class="sidebar-item-text sidebar-link">
<span class="menu-text"><span class="chapter-number">25</span> <span class="chapter-title">General Procedures for Testing Hypotheses</span></span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="./confidence_1.html" class="sidebar-item-text sidebar-link">
<span class="menu-text"><span class="chapter-number">26</span> <span class="chapter-title">Confidence Intervals, Part 1: Assessing the Accuracy of Samples</span></span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="./confidence_2.html" class="sidebar-item-text sidebar-link">
<span class="menu-text"><span class="chapter-number">27</span> <span class="chapter-title">Confidence Intervals, Part 2: The Two Approaches to Estimating Confidence Intervals</span></span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="./reliability_average.html" class="sidebar-item-text sidebar-link">
<span class="menu-text"><span class="chapter-number">28</span> <span class="chapter-title">Some Last Words About the Reliability of Sample Averages</span></span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="./correlation_causation.html" class="sidebar-item-text sidebar-link">
<span class="menu-text"><span class="chapter-number">29</span> <span class="chapter-title">Correlation and Causation</span></span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="./how_big_sample.html" class="sidebar-item-text sidebar-link">
<span class="menu-text"><span class="chapter-number">30</span> <span class="chapter-title">How Large a Sample?</span></span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="./bayes_simulation.html" class="sidebar-item-text sidebar-link">
<span class="menu-text"><span class="chapter-number">31</span> <span class="chapter-title">Bayesian Analysis by Simulation</span></span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="./references.html" class="sidebar-item-text sidebar-link">
<span class="menu-text">References</span></a>
</div>
</li>
<li class="sidebar-item sidebar-item-section">
<div class="sidebar-item-container">
<a class="sidebar-item-text sidebar-link text-start" data-bs-toggle="collapse" data-bs-target="#quarto-sidebar-section-1" role="navigation" aria-expanded="true">
<span class="menu-text">Appendices</span></a>
<a class="sidebar-item-toggle text-start" data-bs-toggle="collapse" data-bs-target="#quarto-sidebar-section-1" role="navigation" aria-expanded="true" aria-label="Toggle section">
<i class="bi bi-chevron-right ms-2"></i>
</a>
</div>
<ul id="quarto-sidebar-section-1" class="collapse list-unstyled sidebar-section depth1 show">
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="./exercise_solutions.html" class="sidebar-item-text sidebar-link">
<span class="menu-text"><span class="chapter-number">A</span> <span class="chapter-title">Exercise Solutions</span></span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="./technical_note.html" class="sidebar-item-text sidebar-link">
<span class="menu-text"><span class="chapter-number">B</span> <span class="chapter-title">Technical Note to the Professional Reader</span></span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="./acknowlegements.html" class="sidebar-item-text sidebar-link">
<span class="menu-text"><span class="chapter-number">C</span> <span class="chapter-title">Acknowledgements</span></span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="./code_topics.html" class="sidebar-item-text sidebar-link">
<span class="menu-text"><span class="chapter-number">D</span> <span class="chapter-title">Code topics</span></span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="./errors_suggestions.html" class="sidebar-item-text sidebar-link">
<span class="menu-text"><span class="chapter-number">E</span> <span class="chapter-title">Errors and suggestions</span></span></a>
</div>
</li>
</ul>
</li>
</ul>
</div>
</nav>
<div id="quarto-sidebar-glass" class="quarto-sidebar-collapse-item" data-bs-toggle="collapse" data-bs-target=".quarto-sidebar-collapse-item"></div>
<!-- margin-sidebar -->
<div id="quarto-margin-sidebar" class="sidebar margin-sidebar">
<nav id="TOC" role="doc-toc" class="toc-active">
<h2 id="toc-title">Table of contents</h2>
<ul>
<li><a href="#statistics-and-probability" id="toc-statistics-and-probability" class="nav-link active" data-scroll-target="#statistics-and-probability"><span class="header-section-number">5.1</span> Statistics and probability</a></li>
<li><a href="#a-new-treatment-for-burkitt-lymphoma" id="toc-a-new-treatment-for-burkitt-lymphoma" class="nav-link" data-scroll-target="#a-new-treatment-for-burkitt-lymphoma"><span class="header-section-number">5.2</span> A new treatment for Burkitt lymphoma</a></li>
<li><a href="#a-physical-model-of-the-hypothetical-hospital" id="toc-a-physical-model-of-the-hypothetical-hospital" class="nav-link" data-scroll-target="#a-physical-model-of-the-hypothetical-hospital"><span class="header-section-number">5.3</span> A physical model of the hypothetical hospital</a></li>
<li><a href="#a-trial-a-run-a-count-and-a-proportion" id="toc-a-trial-a-run-a-count-and-a-proportion" class="nav-link" data-scroll-target="#a-trial-a-run-a-count-and-a-proportion"><span class="header-section-number">5.4</span> A trial, a run, a count and a proportion</a></li>
<li><a href="#simulate-one-trial-with-code" id="toc-simulate-one-trial-with-code" class="nav-link" data-scroll-target="#simulate-one-trial-with-code">5.5 Simulate one trial with code</a></li>
<li><a href="#from-numbers-to-s" id="toc-from-numbers-to-s" class="nav-link" data-scroll-target="#from-numbers-to-s">5.6 From numbers to arrays</a></li>
<li><a href="#sec-introducing-functions" id="toc-sec-introducing-functions" class="nav-link" data-scroll-target="#sec-introducing-functions">5.7 Functions</a></li>
<li><a href="#sec-named-arguments" id="toc-sec-named-arguments" class="nav-link" data-scroll-target="#sec-named-arguments">5.8 Functions and named arguments</a></li>
<li><a href="#sec-ranges" id="toc-sec-ranges" class="nav-link" data-scroll-target="#sec-ranges">5.9 Ranges</a></li>
<li><a href="#sec-python-range" id="toc-sec-python-range" class="nav-link" data-scroll-target="#sec-python-range">5.10 <code>range</code> in Python</a></li>
<li><a href="#sec-random-choice" id="toc-sec-random-choice" class="nav-link" data-scroll-target="#sec-random-choice">5.11 Choosing values at random</a></li>
<li><a href="#sec-sampling-arrays" id="toc-sec-sampling-arrays" class="nav-link" data-scroll-target="#sec-sampling-arrays">5.12 Creating arrays with sampling</a>
<ul class="collapse">
<li><a href="#sum-adding-all-the-values" id="toc-sum-adding-all-the-values" class="nav-link" data-scroll-target="#sum-adding-all-the-values">5.12.1 <code>sum</code> — adding all the values</a></li>
</ul></li>
<li><a href="#sec-counting-results" id="toc-sec-counting-results" class="nav-link" data-scroll-target="#sec-counting-results">5.13 Counting results</a>
<ul class="collapse">
<li><a href="#comparison" id="toc-comparison" class="nav-link" data-scroll-target="#comparison">5.13.1 Comparison</a></li>
</ul></li>
<li><a href="#more-comparisons" id="toc-more-comparisons" class="nav-link" data-scroll-target="#more-comparisons">5.14 More comparisons</a></li>
<li><a href="#sec-count-with-sum" id="toc-sec-count-with-sum" class="nav-link" data-scroll-target="#sec-count-with-sum">5.15 Counting <code>True</code> values with <code>sum</code></a></li>
<li><a href="#the-procedure-for-one-simulated-trial" id="toc-the-procedure-for-one-simulated-trial" class="nav-link" data-scroll-target="#the-procedure-for-one-simulated-trial">5.16 The procedure for one simulated trial</a></li>
<li><a href="#repeating-the-trial" id="toc-repeating-the-trial" class="nav-link" data-scroll-target="#repeating-the-trial">5.17 Repeating the trial</a></li>
<li><a href="#what-have-we-learned-from-saint-hypothetical" id="toc-what-have-we-learned-from-saint-hypothetical" class="nav-link" data-scroll-target="#what-have-we-learned-from-saint-hypothetical"><span class="header-section-number">5.18</span> What have we learned from Saint Hypothetical?</a></li>
<li><a href="#conclusions" id="toc-conclusions" class="nav-link" data-scroll-target="#conclusions"><span class="header-section-number">5.19</span> Conclusions</a></li>
</ul>
</nav>
</div>
<!-- main -->
<main class="content" id="quarto-document-content">
<header id="title-block-header" class="quarto-title-block default">
<div class="quarto-title">
<h1 class="title"><span id="sec-resampling-code" class="quarto-section-identifier"><span class="chapter-number">5</span> <span class="chapter-title">Resampling with code</span></span></h1>
</div>
<div class="quarto-title-meta">
</div>
</header>
<p><a href="resampling_method.html" class="quarto-xref"><span>Chapter 2</span></a> used simulation and resampling from tables of random numbers, dice, and coins. Making random choices in this way can make it easier to understand the process, but of course, physical methods of making random outcomes can be slow and boring.</p>
<p>We saw that short computer programs can do a huge number of resampling trials in a less than a second. The flexibility of a programming language makes it possible to simulate many different outcomes and tests.</p>
<p>Programs can build up tables of random numbers, and do basic tasks like counting the number of values in a row or taking proportions. With these simple tools, we can simulate many problems in probability and statistics.</p>
<p>In this chapter, we will model another problem using Python, but this chapter will add three new things.</p>
<ul>
<li><p>The problem we will work on is a little different from the ambulances problem from <a href="resampling_method.html" class="quarto-xref"><span>Chapter 2</span></a>. It is a real problem about deciding whether a new cancer treatment is better than the alternatives, and it introduces the idea of making a model of the world, to ask questions about chances and probabilities.</p></li>
<li><p>We will slow down a little to emphasize the steps in solving this kind of problem. First we work out how to simulate a single <em>trial</em>. Then we work out how to run many simulated trials.</p></li>
<li><p>We sprinted through the code in <a href="resampling_method.html" class="quarto-xref"><span>Chapter 2</span></a>, with the promise we would come back to the details. Here we give more explanation for some ideas in the code in the last chapter. These are:</p>
<ul>
<li>Storing several values together in one place, with <span class="python">arrays</span>.</li>
<li>Using <em>functions</em> (code recipes) to apply procedures.</li>
<li><em>Comparing</em> numbers to other numbers.</li>
<li><em>Counting</em> numbers that match a condition.</li>
</ul></li>
</ul>
<p>In the next chapter, we will talk more about <em>using arrays</em> to store results, and <em>for loops</em> to repeat a procedure many times.</p>
<section id="statistics-and-probability" class="level2" data-number="5.1">
<h2 data-number="5.1" class="anchored" data-anchor-id="statistics-and-probability"><span class="header-section-number">5.1</span> Statistics and probability</h2>
<p>We have already emphasized that <em>statistics</em> is a way of drawing conclusions about data from the real world, in the presence of random variation; <em>probability</em> is the way of reasoning about random variation. This chapter introduces our first <em>statistical</em> problem, where we use probability to draw conclusions about some important data — about a potential cure for a type of cancer. We will not make much of the distinction between probability and statistics here, but we will come back to it several times in later chapters.</p>
</section>
<section id="a-new-treatment-for-burkitt-lymphoma" class="level2" data-number="5.2">
<h2 data-number="5.2" class="anchored" data-anchor-id="a-new-treatment-for-burkitt-lymphoma"><span class="header-section-number">5.2</span> A new treatment for Burkitt lymphoma</h2>
<p><a href="https://en.wikipedia.org/wiki/Burkitt_lymphoma">Burkitt lymphoma</a> is an unusual cancer of the lymphatic system. The lymphatic system is a vein-like network throughout the body that is involved in the immune reaction to disease. In developed countries, with standard treatment, the cure rate for Burkitt lymphoma is about 90%.</p>
<p>In 2006, researchers at the US National Cancer Institute (NCI), tested a new treatment for Burkitt lymphoma <span class="citation" data-cites="dunleavy2006burkitt">(<a href="references.html#ref-dunleavy2006burkitt" role="doc-biblioref">Dunleavy et al. 2006</a>)</span>. They gave the new treatment to 17 patients, and found that all 17 patients were doing well after two years or more of follow up. By “doing well”, we mean that their lymphoma had not progressed; as a short-hand, we will say that these patients were “cured”, but of course, we do not know what happened to them after this follow up.</p>
<p>Here is where we put on our statistical hat and ask ourselves the following question — <em>how surprised are we that the NCI researchers saw their result of 17 out of 17 patients cured?</em></p>
<p>At this stage you might and should ask, what could we possibly mean by “surprised”? That is a good and important question, and we will discuss that much more in the chapters to come. For now, please bear with us as we do a thought experiment.</p>
<p>Let us forget the 17 out of 17 result of the NCI study for a moment. Imagine that there is another hospital, called Saint Hypothetical General, just down the road from the NCI, that was also treating 17 patients with Burkitt lymphoma. Saint Hypothetical were not using the NCI treatment, they were using the standard treatment.</p>
<p>We already know that each patient given the standard treatment has a 90% chance of cure. Given that 90% cure rate, what is the chance that 17 out of 17 of the Hypothetical group will be cured?</p>
<p>You may notice that this question about the Hypothetical group is similar to the problem of the 16 ambulances in <a href="resampling_method.html" class="quarto-xref"><span>Chapter 2</span></a>. In that problem, we were interested to know how likely it was that 3 or more of 16 ambulances would be out of action on any one day, given that each ambulance had a 10% chance of being out of action. Here we would like to know the chances that all 17 patients would be cured, given that each patient has a 90% chance of being cured (and a 10% chance of relapse).</p>
</section>
<section id="a-physical-model-of-the-hypothetical-hospital" class="level2" data-number="5.3">
<h2 data-number="5.3" class="anchored" data-anchor-id="a-physical-model-of-the-hypothetical-hospital"><span class="header-section-number">5.3</span> A physical model of the hypothetical hospital</h2>
<p>As in the ambulance example, we could make a physical model of chance in this world. For example, to simulate whether a given patient is cured or not by a 90% effective treatment, we could throw a ten sided die and record the result. We could say, arbitrarily, that a result of 0 means “not cured”, and all the numbers 1 through 9 mean “cured” (typical 10-sided dice have sides numbered 0 through 9).</p>
<p>We could roll 17 dice to simulate one “trial” in this random world. For each trial, we record the number of dice that show numbers 1 through 9 (and not 0). This will be a number between 0 and 17, and it is the number of patients “cured” in our simulated trial.</p>
<p><a href="#fig-17-d10s" class="quarto-xref">Figure <span>5.1</span></a> is the result of one such trial we did with a set of 17 10-sided dice we happened to have to hand:</p>
<div id="fig-17-d10s" class="quarto-float quarto-figure quarto-figure-center anchored">
<figure class="quarto-float quarto-float-fig figure">
<div aria-describedby="fig-17-d10s-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<img src="images/17_d10s.png" class="img-fluid figure-img">
</div>
<figcaption class="quarto-float-caption-bottom quarto-float-caption quarto-float-fig" id="fig-17-d10s-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Figure 5.1: One roll of 17 10-sided dice
</figcaption>
</figure>
</div>
<p>The trial in <a href="#fig-17-d10s" class="quarto-xref">Figure <span>5.1</span></a> shows are two dice with the 0 face uppermost, and the rest with numbers from 1 through 9. Therefore, there were 15 out of 17 not-zero numbers, meaning that 15 out of 17 simulated “patients” were “cured” in this simulated trial.</p>
<!---
Check we have adapted text above to picture.
-->
<p>We could repeat this simulated trial procedure 100 times, and we would then have 100 counts of the not-zero numbers. Each of the 100 counts would be the number of patients cured in that trial. We can ask how many of these 100 counts were equal to 17. This will give us an estimate of the probability we would see 17 out of 17 patients cured, given that any one patient has a 90% chance of cure. For example, say we saw 15 out of 100 counts were equal to 17. That would give us an estimate of 15 / 100 or 0.15 or 15%, for the probability we would see 17 out of 17 patients cured.</p>
<p>So, if Saint Hypothetical General did see 17 out of 17 patients cured with the standard treatment, they would be a little surprised, because they would only expect to see that happen 15% of the time. But they would not be <em>very</em> surprised — 15% of the time is uncommon, but not <em>very</em> uncommon.</p>
</section>
<section id="a-trial-a-run-a-count-and-a-proportion" class="level2" data-number="5.4">
<h2 data-number="5.4" class="anchored" data-anchor-id="a-trial-a-run-a-count-and-a-proportion"><span class="header-section-number">5.4</span> A trial, a run, a count and a proportion</h2>
<p>Here we stop to emphasize the steps in the process of a random simulation.</p>
<ol type="1">
<li><em>We decide what we mean by one trial</em>. Here one trial has the same meaning in medicine as resampling — we mean the result of treating 17 patients. One <em>simulated trial</em> is then the simulation of one set of outcomes from 17 patients.</li>
<li>Work out the <em>outcome</em> of interest from the trial. The outcome here is the number of patients cured.</li>
<li>We work out a way to <em>simulate one trial</em>. Here we chose to throw 17 10-sided dice, and count the number of not zero values. This is the outcome from one simulation trial.</li>
<li>We <em>repeat</em> the simulated trial procedure many times, and collect the results from each trial. Say we repeat the trial procedure 100 times; we will call this a <em>run</em> of 100 trials.</li>
<li>We <em>count</em> the number of trials with an outcome that matches the outcome we are interested in. In this case we are interested in the outcome 17 out of 17 cured, so we count the number of trials with a score of 17. Say 15 out of the run of 100 trials had an outcome of 17 cured. That is our <em>count</em>.</li>
<li>Finally we divide the count by the number of trials to get the <em>proportion</em>. From the example above, we divide 15 by 100 to get 0.15 (15%). This is our estimate of the chance of seeing 17 out of 17 patients cured in any one trial. We can also call this an estimate of the <em>probability</em> that 17 out of 17 patients will be cured on any on trial.</li>
</ol>
<p>Our next step is to work out the code for step 2: <em>simulate one trial</em>.</p>
<div id="nte-lymphoma" class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Note 5.1: Notebook: Simulating lymphoma trials
</div>
</div>
<div class="callout-body-container callout-body">
<div class="nb-links">
<p><a class="notebook-link" href="notebooks/lymphoma.ipynb">Download notebook</a> <a class="interact-button" href="./interact/lab/index.html?path=lymphoma.ipynb">Interact</a></p>
</div>
</div>
</div>
<div class="nb-start" name="lymphoma" title="Simulating lymphoma trials">
</div>
</section>
<section id="simulate-one-trial-with-code" class="level2" data-number="5.5">
<h2 data-number="5.5" class="anchored" data-anchor-id="simulate-one-trial-with-code">5.5 Simulate one trial with code</h2>
<p>We can use the computer to do something very similar to rolling 17 10-sided dice, by asking the computer for 17 random whole numbers from 0 through 9.</p>
<div class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Whole numbers
</div>
</div>
<div class="callout-body-container callout-body">
<p>A whole number is a number that is not negative, and does not have fractional part (does not have anything after a decimal point). 0 and 1 and 2 and 3 are whole numbers, but -1 and <span class="math inline">\(\frac{3}{5}\)</span> and 11.3 are not. The whole numbers from 0 through 9 are 0, 1, 2, 3, 4, 5, 6, 7, 8, 9.</p>
</div>
</div>
<p>We have already discussed what we mean by <em>random</em> in <a href="resampling_method.html#sec-randomness-computer" class="quarto-xref"><span>Section 2.2</span></a>.</p>
<div class="python">
<p>We will be asking the computer to generate many random numbers. So, before we start, we again import Numpy and get its <em>random number generator</em>:</p>
<div class="cell" data-layout-align="center">
<div class="sourceCode cell-code" id="cb1"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb1-1"><a href="#cb1-1" aria-hidden="true" tabindex="-1"></a><span class="im">import</span> numpy <span class="im">as</span> np</span>
<span id="cb1-2"><a href="#cb1-2" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb1-3"><a href="#cb1-3" aria-hidden="true" tabindex="-1"></a><span class="co"># Ask for Numpy's default random number generator and name</span></span>
<span id="cb1-4"><a href="#cb1-4" aria-hidden="true" tabindex="-1"></a><span class="co"># it `rnd`. `rnd` is short for "random".</span></span>
<span id="cb1-5"><a href="#cb1-5" aria-hidden="true" tabindex="-1"></a>rnd <span class="op">=</span> np.random.default_rng()</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
</div>
</section>
<section id="from-numbers-to-s" class="level2" data-number="5.6">
<h2 data-number="5.6" class="anchored" data-anchor-id="from-numbers-to-s">5.6 From numbers to arrays</h2>
<p>We next need to prepare the <em>sequence</em> of numbers that we want NumPy to select from.</p>
<p>We have already seen the idea that Python has <em>values</em> that are individual numbers. Remember, a <em>variable</em> is a <em>named value</em>. Here we attach the name <code>a</code> to the value 1.</p>
<div class="cell" data-layout-align="center">
<div class="sourceCode cell-code" id="cb2"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb2-1"><a href="#cb2-1" aria-hidden="true" tabindex="-1"></a>a <span class="op">=</span> <span class="dv">1</span></span>
<span id="cb2-2"><a href="#cb2-2" aria-hidden="true" tabindex="-1"></a><span class="co"># Show the value of "a"</span></span>
<span id="cb2-3"><a href="#cb2-3" aria-hidden="true" tabindex="-1"></a>a</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>1</code></pre>
</div>
</div>
<p>NumPy also allows <em>values</em> that are <em>sequences of numbers</em>. NumPy calls these sequences <em>arrays</em>.</p>
<p>Here we make a array that contains the 10 numbers we will select from:</p>
<div class="cell" data-layout-align="center">
<div class="sourceCode cell-code" id="cb4"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb4-1"><a href="#cb4-1" aria-hidden="true" tabindex="-1"></a><span class="co"># Make an array of numbers, store with the name "some_numbers".</span></span>
<span id="cb4-2"><a href="#cb4-2" aria-hidden="true" tabindex="-1"></a>some_numbers <span class="op">=</span> np.array([<span class="dv">0</span>, <span class="dv">1</span>, <span class="dv">2</span>, <span class="dv">3</span>, <span class="dv">4</span>, <span class="dv">5</span>, <span class="dv">6</span>, <span class="dv">7</span>, <span class="dv">8</span>, <span class="dv">9</span>])</span>
<span id="cb4-3"><a href="#cb4-3" aria-hidden="true" tabindex="-1"></a><span class="co"># Show the value of "some_numbers"</span></span>
<span id="cb4-4"><a href="#cb4-4" aria-hidden="true" tabindex="-1"></a>some_numbers</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])</code></pre>
</div>
</div>
<p>Notice that the value for <code>some_numbers</code> is an array, and that this value <em>contains</em> 10 numbers.</p>
<p>Put another way, <code>some_numbers</code> is now the name we can use for this collection of 10 values.</p>
<p>Arrays are very useful for simulations and data analysis, and we will be using them for nearly every example in this book.</p>
</section>
<section id="sec-introducing-functions" class="level2" data-number="5.7">
<h2 data-number="5.7" class="anchored" data-anchor-id="sec-introducing-functions">5.7 Functions</h2>
<p><em>Functions</em> are another tool that we will be using everywhere, and that you seen already, although we have not introduced them until now.</p>
<p>You can think of functions as named <em>production lines</em>.</p>
<p>For example, consider the Python <em>function</em> <code>np.round</code></p>
<div class="python">
</div>
<div class="cell" data-layout-align="center">
<div class="sourceCode cell-code" id="cb6"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb6-1"><a href="#cb6-1" aria-hidden="true" tabindex="-1"></a><span class="co"># We load the Numpy library so we have access to the Numpy functions.</span></span>
<span id="cb6-2"><a href="#cb6-2" aria-hidden="true" tabindex="-1"></a><span class="im">import</span> numpy <span class="im">as</span> np</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<p><code>np.round</code> is the name for a simple production line, that takes in a number, and (by default) sends back the number rounded to the nearest <em>integer</em>.</p>
<div id="nte-what-is-integer" class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Note 5.2: What is an integer?
</div>
</div>
<div class="callout-body-container callout-body">
<p>An <em>integer</em> is a positive or negative <em>whole number</em>.</p>
<p>In other words, a number is an <em>integer</em> if the number is <em>either</em> a whole number (0, 1, 2 …), <em>or</em> a negative whole number (-1, -2, -3 …). All of -208, -2, 0, 10, 105 are integers, but <span class="math inline">\(\frac{3}{5}\)</span>, -10.3 and 0.2 are not.</p>
<p>We will use the term <em>integer</em> fairly often, because it is a convenient way to name all the positive and negative whole numbers.</p>
</div>
</div>
<p>Think of a function as a named <em>production line</em>. We sent the function (production line) raw material (components) to work on. The production line does some work on the components. A finished result comes off the other end.</p>
<p>Therefore, think of <code>np.round</code> as the name of a production line, that takes in a <em>component</em> (in this case, any number), and does some work, and sends back the finished <em>result</em> (in this case, the number rounded to the nearest integer.</p>
<p>The components we send to a function are called <em>arguments</em>. The finished result the function sends back is the <em>return value</em>.</p>
<ul>
<li><strong>Arguments</strong>: the value or values we send to a function. These are the components in the production line analogy.</li>
<li><strong>Return value</strong>: the values the function sends back. This is the finished result in the production line analogy.</li>
</ul>
<p>See <a href="#fig-round_function_pl_np" class="quarto-xref">Figure <span>5.2</span></a> for an illustration of <code>np.round</code> as a production line.</p>
<div class="cell" data-layout-align="center">
<div class="cell-output-display">
<div id="fig-round_function_pl_np" class="quarto-float quarto-figure quarto-figure-center anchored" data-fig-align="center">
<figure class="quarto-float quarto-float-fig figure">
<div aria-describedby="fig-round_function_pl_np-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<img src="diagrams/round_function_pl_np.svg" class="img-fluid quarto-figure quarto-figure-center figure-img" style="width:70.0%">
</div>
<figcaption class="quarto-float-caption-bottom quarto-float-caption quarto-float-fig" id="fig-round_function_pl_np-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Figure 5.2: The <code>np.round</code> function as a production line
</figcaption>
</figure>
</div>
</div>
</div>
<p>In the next few code cells, you see examples where <code>np.round</code> takes in a not-integer number, as an <em>argument</em>, and sends back the nearest integer as the <em>return value</em>:</p>
<div class="cell" data-layout-align="center">
<div class="sourceCode cell-code" id="cb7"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb7-1"><a href="#cb7-1" aria-hidden="true" tabindex="-1"></a><span class="co"># Put in 3.2, round sends back 3.</span></span>
<span id="cb7-2"><a href="#cb7-2" aria-hidden="true" tabindex="-1"></a>np.<span class="bu">round</span>(<span class="fl">3.2</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>np.float64(3.0)</code></pre>
</div>
</div>
<div class="python">
<div id="nte-numpy-float-display" class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Note 5.3: Numpy display of floating point values
</div>
</div>
<div class="callout-body-container callout-body">
<p>Way back in <a href="resampling_method.html#nte-numpy-int-display" class="quarto-xref">Note <span>2.3</span></a>, you saw that Numpy has a particular way of displaying single values that shows both the <em>value</em> (here 3.0) and the type of value (here <code>np.float64</code>). You previously saw examples of Numpy integer values of type <code>np.int64</code>. The value that comes back from <code>np.round(3.2)</code> is a <em>floating point</em> (<code>float</code>) type, taking up <code>64</code> units (<a href="https://en.wikipedia.org/wiki/Bit">bits</a>) of computer memory. A floating point value is a type of value that may have digits following the decimal point, such as 0.001, 5.93, or 6.0. Notice that 6.0 (and 3.0) can be written as floating point values, with 0 after the decimal point, as here, or as integers, with no decimal point, as in 6 (and 3). It just so happens that <code>np.round</code> returns the value as the floating point version of the number.</p>
</div>
</div>
</div>
<div class="cell" data-layout-align="center">
<div class="sourceCode cell-code" id="cb9"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb9-1"><a href="#cb9-1" aria-hidden="true" tabindex="-1"></a><span class="co"># Put in -2.7, round sends back -3.</span></span>
<span id="cb9-2"><a href="#cb9-2" aria-hidden="true" tabindex="-1"></a>np.<span class="bu">round</span>(<span class="op">-</span><span class="fl">2.7</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>np.float64(-3.0)</code></pre>
</div>
</div>
<p>Like many functions, <code>np.round</code> can take more than one argument (component). You can send <code>round</code> the number of digits you want to round to, after the number of you want it to work on, like this (see <a href="#fig-round_ndigits_pl_np" class="quarto-xref">Figure <span>5.3</span></a>):</p>
<div class="cell" data-layout-align="center">
<div class="sourceCode cell-code" id="cb11"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb11-1"><a href="#cb11-1" aria-hidden="true" tabindex="-1"></a><span class="co"># Put in 3.1415, and the number of digits to round to (2).</span></span>
<span id="cb11-2"><a href="#cb11-2" aria-hidden="true" tabindex="-1"></a><span class="co"># round sends back 3.14</span></span>
<span id="cb11-3"><a href="#cb11-3" aria-hidden="true" tabindex="-1"></a>np.<span class="bu">round</span>(<span class="fl">3.1415</span>, <span class="dv">2</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>np.float64(3.14)</code></pre>
</div>
</div>
<div class="cell" data-layout-align="center">
<div class="cell-output-display">
<div id="fig-round_ndigits_pl_np" class="quarto-float quarto-figure quarto-figure-center anchored" data-fig-align="center">
<figure class="quarto-float quarto-float-fig figure">
<div aria-describedby="fig-round_ndigits_pl_np-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<img src="diagrams/round_function_ndigits_pl_np.svg" class="img-fluid quarto-figure quarto-figure-center figure-img" style="width:70.0%">
</div>
<figcaption class="quarto-float-caption-bottom quarto-float-caption quarto-float-fig" id="fig-round_ndigits_pl_np-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Figure 5.3: <code>round</code> with optional arguments specifying number of digits
</figcaption>
</figure>
</div>
</div>
</div>
<p>Notice that the second argument — here 2 — is <em>optional</em>. We only have to send <code>round</code> one argument: the number we want it to round. But we can <em>optionally</em> send it a second argument — the number of decimal places we want it to round to. If we don’t specify the second argument, then <code>round</code> assumes we want to round to 0 decimal places, and therefore, to the nearest integer.</p>
</section>
<section id="sec-named-arguments" class="level2" data-number="5.8">
<h2 data-number="5.8" class="anchored" data-anchor-id="sec-named-arguments">5.8 Functions and named arguments</h2>
<p>In the example above, we sent <code>round</code> two arguments. <code>round</code> knows that we mean the first argument to be the number we want to round, and the second argument is the number of decimal places we want to round to. It knows which is which by the <em>position</em> of the arguments — the <em>first</em> argument is the <em>number</em> it should round, and <em>second</em> is the number of digits.</p>
<p>In fact, internally, the <code>round</code> function also gives these arguments <em>names</em>. It calls the number it should round — <code>a</code> — and the number of digits it should round to — <code>decimals</code>. This is useful, because it is often clearer and simpler to identify the argument we are specifying with its name, instead of just relying on its position.</p>
<p>If we aren’t using the argument names, we call the round function as we did above:</p>
<div class="cell" data-layout-align="center">
<div class="sourceCode cell-code" id="cb13"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb13-1"><a href="#cb13-1" aria-hidden="true" tabindex="-1"></a><span class="co"># Put in 3.1415, and the number of digits to round to (2).</span></span>
<span id="cb13-2"><a href="#cb13-2" aria-hidden="true" tabindex="-1"></a><span class="co"># round sends back 3.14</span></span>
<span id="cb13-3"><a href="#cb13-3" aria-hidden="true" tabindex="-1"></a>np.<span class="bu">round</span>(<span class="fl">3.1415</span>, <span class="dv">2</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>np.float64(3.14)</code></pre>
</div>
</div>
<p>In this call, we relied on the fact that we, the people writing the code, and you, the person reading the code, remembered that the second argument (2) means the number of decimal places it should round to. But we can also specify the argument using its name, like this (see <a href="#fig-np_round_function_named" class="quarto-xref">Figure <span>5.4</span></a>):</p>
<div class="cell" data-layout-align="center">
<div class="sourceCode cell-code" id="cb15"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb15-1"><a href="#cb15-1" aria-hidden="true" tabindex="-1"></a><span class="co"># Put in 3.1415, and the number of digits to round to (2).</span></span>
<span id="cb15-2"><a href="#cb15-2" aria-hidden="true" tabindex="-1"></a><span class="co"># Use the name of the number-of-decimals argument for clarity:</span></span>
<span id="cb15-3"><a href="#cb15-3" aria-hidden="true" tabindex="-1"></a>np.<span class="bu">round</span>(<span class="fl">3.1415</span>, decimals<span class="op">=</span><span class="dv">2</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>np.float64(3.14)</code></pre>
</div>
</div>
<div class="cell" data-layout-align="center">
<div class="cell-output-display">
<div id="fig-np_round_function_named" class="quarto-float quarto-figure quarto-figure-center anchored" data-fig-align="center">
<figure class="quarto-float quarto-float-fig figure">
<div aria-describedby="fig-np_round_function_named-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<img src="diagrams/round_function_named_np.svg" class="img-fluid quarto-figure quarto-figure-center figure-img" style="width:70.0%">
</div>
<figcaption class="quarto-float-caption-bottom quarto-float-caption quarto-float-fig" id="fig-np_round_function_named-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Figure 5.4: The <code>np.round</code> function with argument names
</figcaption>
</figure>
</div>
</div>
</div>
<p>Here Python sees the <em>first</em> argument, as before, and assumes that it is the number we want to round. Then it sees the second, named argument — <code>decimals=2</code> — and knows, <em>from the name</em>, that we mean this to be the number of decimals to round to.</p>
<p>In fact, we could even specify <em>both</em> arguments by name, like this:</p>
<div class="cell" data-layout-align="center">
<div class="sourceCode cell-code" id="cb17"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb17-1"><a href="#cb17-1" aria-hidden="true" tabindex="-1"></a><span class="co"># Put in 3.1415, and the number of digits to round to (2).</span></span>
<span id="cb17-2"><a href="#cb17-2" aria-hidden="true" tabindex="-1"></a>np.<span class="bu">round</span>(a<span class="op">=</span><span class="fl">3.1415</span>, decimals<span class="op">=</span><span class="dv">2</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>np.float64(3.14)</code></pre>
</div>
</div>
<p>We don’t usually name both arguments for <code>round</code>, as we have above, because it is so obvious that the first argument is the thing we want to round, and so naming the argument does not make it any more clear what the code is doing. But — as so often in programming — whether to use the names, or let Python work out which argument is which by position, is a judgment call. The judgment you are making is about the way to write the code to be most clear for your reader, where your most important reader may be you, coming back to the code in a week or a year.</p>
<div class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
How do you know what names to use for the function arguments?
</div>
</div>
<div class="callout-body-container callout-body">
<p>You can find the names of the function arguments in the help for the function, either online, or in the notebook interface. For example, to get the help for <code>np.round</code>, including the argument names, you could make a new cell, and type <code>np.round?</code>, then execute the cell by pressing Shift-Enter. This will show the help for the function in the notebook interface.</p>
</div>
</div>
</section>
<section id="sec-ranges" class="level2" data-number="5.9">
<h2 data-number="5.9" class="anchored" data-anchor-id="sec-ranges">5.9 Ranges</h2>
<p>Now let us return to the variable <code>some_numbers</code> that we created above:</p>
<div class="cell" data-layout-align="center">
<div class="sourceCode cell-code" id="cb19"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb19-1"><a href="#cb19-1" aria-hidden="true" tabindex="-1"></a><span class="co"># Make an array of numbers, store with the name "some_numbers".</span></span>
<span id="cb19-2"><a href="#cb19-2" aria-hidden="true" tabindex="-1"></a>some_numbers <span class="op">=</span> np.array([<span class="dv">0</span>, <span class="dv">1</span>, <span class="dv">2</span>, <span class="dv">3</span>, <span class="dv">4</span>, <span class="dv">5</span>, <span class="dv">6</span>, <span class="dv">7</span>, <span class="dv">8</span>, <span class="dv">9</span>])</span>
<span id="cb19-3"><a href="#cb19-3" aria-hidden="true" tabindex="-1"></a><span class="co"># Show the value of "some_numbers"</span></span>
<span id="cb19-4"><a href="#cb19-4" aria-hidden="true" tabindex="-1"></a>some_numbers</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])</code></pre>
</div>
</div>
<p>In fact, we often need to do this: generate a sequence or <em>range</em> of integers, such as 0 through 9.</p>
<div class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Pick a number from 1 through 5
</div>
</div>
<div class="callout-body-container callout-body">
<p>Ranges can be confusing in normal speech because it is not always clear whether they include their beginning and end. For example, if someone says “pick a number between 1 and 5”, do they mean to pick from <em>all</em> of the numbers, including the first and last (any of 1 or 2 or 3 or 4 or 5)? Or do they mean only the numbers that are <em>between</em> 1 and 5 (so 2 or 3 or 4)? Or do they mean all the numbers up to, but not including 5 (so 1 or 2 or 3 or 4)?</p>
<p>To avoid this confusion, we will nearly always use “from” and “through” in ranges, meaning that we do include both the start and the end number. For example, if we say “pick a number from 1 through 5” we mean one of 1 or 2 or 3 or 4 or 5.</p>
</div>
</div>
<p>Creating ranges of numbers is so common that Python has a standard Numpy function <code>np.arange</code> to do that.</p>
<div class="python">
<div class="cell" data-layout-align="center">
<div class="sourceCode cell-code" id="cb21"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb21-1"><a href="#cb21-1" aria-hidden="true" tabindex="-1"></a><span class="co"># An array containing all the numbers from 0 through 9.</span></span>
<span id="cb21-2"><a href="#cb21-2" aria-hidden="true" tabindex="-1"></a>some_numbers <span class="op">=</span> np.arange(<span class="dv">0</span>, <span class="dv">10</span>)</span>
<span id="cb21-3"><a href="#cb21-3" aria-hidden="true" tabindex="-1"></a>some_numbers</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])</code></pre>
</div>
</div>
<div class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Pronouncing <code>np.arange</code>
</div>
</div>
<div class="callout-body-container callout-body">
<p>Yes, we know, it looks as if <code>arange</code> in <code>np.arange</code> should be pronounced like “arrange”, but in fact, the <code>a</code> in <code>arange</code> stands for “array”. <code>arange</code> — unlike the similar Python function <code>range</code> — sends back its values in the form of an array. So, bear with your aging authors, and remember to pronounce <code>arange</code> as “A-range”. As well as being easier on our ears, it will help you remember what the function is for.</p>
</div>
</div>
<p>Notice that we send <code>np.arange</code> the <em>arguments</em> 0 and 10. The first argument, here 0, is the <em>start</em> value. The second argument, here 10, is the <em>stop</em> value. Numpy (in the <code>arange</code> function) understands this to mean: <em>start</em> at 0 (the start value) and go up to <em>but do not include</em> 10 (the stop value).</p>
<p>You can therefore read <code>np.arange(0, 10)</code> as “the sequence of integers starting at 0, up to, but not including 10”.</p>
<p>Like <code>np.round</code>, the arguments to <code>np.arange</code> also have names, so, we could also write:</p>
<div class="cell" data-layout-align="center">
<div class="sourceCode cell-code" id="cb23"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb23-1"><a href="#cb23-1" aria-hidden="true" tabindex="-1"></a><span class="co"># An array containing all the numbers from 0 through 9.</span></span>
<span id="cb23-2"><a href="#cb23-2" aria-hidden="true" tabindex="-1"></a><span class="co"># Now using named arguments.</span></span>
<span id="cb23-3"><a href="#cb23-3" aria-hidden="true" tabindex="-1"></a>some_numbers <span class="op">=</span> np.arange(start<span class="op">=</span><span class="dv">0</span>, stop<span class="op">=</span><span class="dv">10</span>)</span>
<span id="cb23-4"><a href="#cb23-4" aria-hidden="true" tabindex="-1"></a>some_numbers</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])</code></pre>
</div>
</div>
<p>So far, we have sent <code>arange</code> two arguments, but we can also send just one argument, like this:</p>
<div class="cell" data-layout-align="center">
<div class="sourceCode cell-code" id="cb25"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb25-1"><a href="#cb25-1" aria-hidden="true" tabindex="-1"></a><span class="co"># An array containing all the numbers from 0 through 9.</span></span>
<span id="cb25-2"><a href="#cb25-2" aria-hidden="true" tabindex="-1"></a>some_integers <span class="op">=</span> np.arange(<span class="dv">10</span>)</span>
<span id="cb25-3"><a href="#cb25-3" aria-hidden="true" tabindex="-1"></a>some_integers</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])</code></pre>
</div>
</div>
<p>When we sent <code>arange</code> a single argument, like this, <code>arange</code> understands this to mean we have sent just the <em>stop</em> value, and that is should assume a <em>start</em> value of 0.</p>
<p>Again, if we wanted, we could send this argument by name:</p>
<div class="cell" data-layout-align="center">
<div class="sourceCode cell-code" id="cb27"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb27-1"><a href="#cb27-1" aria-hidden="true" tabindex="-1"></a><span class="co"># An array containing all the numbers from 0 through 9.</span></span>
<span id="cb27-2"><a href="#cb27-2" aria-hidden="true" tabindex="-1"></a><span class="co"># Specify the stop value by explicit name, for clarity.</span></span>
<span id="cb27-3"><a href="#cb27-3" aria-hidden="true" tabindex="-1"></a>some_integers <span class="op">=</span> np.arange(stop<span class="op">=</span><span class="dv">10</span>)</span>
<span id="cb27-4"><a href="#cb27-4" aria-hidden="true" tabindex="-1"></a>some_integers</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])</code></pre>
</div>
</div>
</div>
<!---
End of Python section
-->
<p>Here are some more examples of <code>np.arange</code>:</p>
<div class="cell" data-layout-align="center">
<div class="sourceCode cell-code" id="cb29"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb29-1"><a href="#cb29-1" aria-hidden="true" tabindex="-1"></a><span class="co"># All the integers starting at 10, up to, but not including 15.</span></span>
<span id="cb29-2"><a href="#cb29-2" aria-hidden="true" tabindex="-1"></a><span class="co"># In other words, 10 through 14.</span></span>
<span id="cb29-3"><a href="#cb29-3" aria-hidden="true" tabindex="-1"></a>np.arange(<span class="dv">10</span>, <span class="dv">15</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>array([10, 11, 12, 13, 14])</code></pre>
</div>
</div>
<div class="cell" data-layout-align="center">
<div class="sourceCode cell-code" id="cb31"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb31-1"><a href="#cb31-1" aria-hidden="true" tabindex="-1"></a><span class="co"># Here we are only sending one value (7). np.arange understands this to be</span></span>
<span id="cb31-2"><a href="#cb31-2" aria-hidden="true" tabindex="-1"></a><span class="co"># the stop value, and assumes 0 as the start value.</span></span>
<span id="cb31-3"><a href="#cb31-3" aria-hidden="true" tabindex="-1"></a><span class="co"># In other words, 0 through 6</span></span>
<span id="cb31-4"><a href="#cb31-4" aria-hidden="true" tabindex="-1"></a>np.arange(<span class="dv">7</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>array([0, 1, 2, 3, 4, 5, 6])</code></pre>
</div>
</div>
</section>
<section id="sec-python-range" class="level2 python" data-number="5.10">
<h2 data-number="5.10" class="anchored" data-anchor-id="sec-python-range">5.10 <code>range</code> in Python</h2>
<p>So far you have seen ranges of integers using <code>np.arange</code>. The <code>np.</code> prefix refers to the fact that <code>np.arange</code> is a function from the Numpy package. The <code>a</code> in <code>arange</code> signals that the result <code>np.arange</code> returns is an <em>array</em>:</p>
<div class="cell" data-layout-align="center">
<div class="sourceCode cell-code" id="cb33"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb33-1"><a href="#cb33-1" aria-hidden="true" tabindex="-1"></a>arr <span class="op">=</span> np.arange(<span class="dv">7</span>)</span>
<span id="cb33-2"><a href="#cb33-2" aria-hidden="true" tabindex="-1"></a><span class="co"># Show the result</span></span>
<span id="cb33-3"><a href="#cb33-3" aria-hidden="true" tabindex="-1"></a>arr</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>array([0, 1, 2, 3, 4, 5, 6])</code></pre>
</div>
</div>
<p><code>arr</code> is an array. We can ask Python what kind of value we have by using the Python <code>type</code> function. It accepts a value, and sends back something to show us the type of the value. In the case of <code>arr</code>:</p>
<div class="cell" data-layout-align="center">
<div class="sourceCode cell-code" id="cb35"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb35-1"><a href="#cb35-1" aria-hidden="true" tabindex="-1"></a><span class="co"># Show what type of thing this is.</span></span>
<span id="cb35-2"><a href="#cb35-2" aria-hidden="true" tabindex="-1"></a><span class="bu">type</span>(arr)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code><class 'numpy.ndarray'></code></pre>
</div>
</div>
<p>We do often use <code>np.arange</code> to get a range of integers in a convenient array format, but Python has another way of getting a range of integers — the <code>range</code> function.</p>
<p>The <code>range</code> function is very similar to <code>np.arange</code>, but it is <em>not</em> part of Numpy — it is a basic function in Python — and it does <em>not</em> return an <em>array</em> of numbers, it returns something else. Here we ask for a <code>range</code> from 0 through 6 (0 up to, but not including 7):</p>
<div class="cell" data-layout-align="center">
<div class="sourceCode cell-code" id="cb37"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb37-1"><a href="#cb37-1" aria-hidden="true" tabindex="-1"></a><span class="co"># Notice no `np.` before `range`. This is the Python `range` function.</span></span>
<span id="cb37-2"><a href="#cb37-2" aria-hidden="true" tabindex="-1"></a>r <span class="op">=</span> <span class="bu">range</span>(<span class="dv">7</span>)</span>
<span id="cb37-3"><a href="#cb37-3" aria-hidden="true" tabindex="-1"></a>r</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>range(0, 7)</code></pre>
</div>
</div>
<p>Notice that the thing that came back is something that <em>represents</em> or <em>stands in for</em> the number 0 through 6. It is not an array, but a specific type of thing called — a <code>range</code>.</p>
<div class="cell" data-layout-align="center">
<div class="sourceCode cell-code" id="cb39"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb39-1"><a href="#cb39-1" aria-hidden="true" tabindex="-1"></a><span class="bu">type</span>(r)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code><class 'range'></code></pre>
</div>
</div>
<p>The <code>range</code> above is a container for the numbers 0 through 6, in the same way that the array from <code>np.arange</code> was a container for these numbers. A container is a Python value that contains other values. We can get the numbers out of the Python <code>range</code> container in many different ways, but one of them is to convert this container to an array container, using the <code>np.array</code> function. The <code>np.array</code> function takes the thing we pass it, and makes it into an array. When we apply <code>np.array</code> to <code>r</code> above, we get the numbers that <code>r</code> contains:</p>
<div class="cell" data-layout-align="center">
<div class="sourceCode cell-code" id="cb41"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb41-1"><a href="#cb41-1" aria-hidden="true" tabindex="-1"></a><span class="co"># Get the numbers from the range `r`, convert to an array.</span></span>
<span id="cb41-2"><a href="#cb41-2" aria-hidden="true" tabindex="-1"></a>a_from_r <span class="op">=</span> np.array(r)</span>
<span id="cb41-3"><a href="#cb41-3" aria-hidden="true" tabindex="-1"></a><span class="co"># Show the result</span></span>
<span id="cb41-4"><a href="#cb41-4" aria-hidden="true" tabindex="-1"></a>a_from_r</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>array([0, 1, 2, 3, 4, 5, 6])</code></pre>
</div>
</div>
<p>The <code>range</code> function has the same <code>start</code> and <code>stop</code> arguments that <code>np.arange</code> does, and with the same meaning:</p>
<div class="cell" data-layout-align="center">
<div class="sourceCode cell-code" id="cb43"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb43-1"><a href="#cb43-1" aria-hidden="true" tabindex="-1"></a><span class="co"># 3 up to, not including 12.</span></span>
<span id="cb43-2"><a href="#cb43-2" aria-hidden="true" tabindex="-1"></a><span class="co"># (3 through 11)</span></span>
<span id="cb43-3"><a href="#cb43-3" aria-hidden="true" tabindex="-1"></a>r_2 <span class="op">=</span> <span class="bu">range</span>(<span class="dv">3</span>, <span class="dv">12</span>)</span>
<span id="cb43-4"><a href="#cb43-4" aria-hidden="true" tabindex="-1"></a>r_2</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>range(3, 12)</code></pre>
</div>
</div>
<div class="cell" data-layout-align="center">
<div class="sourceCode cell-code" id="cb45"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb45-1"><a href="#cb45-1" aria-hidden="true" tabindex="-1"></a>np.array(r_2)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>array([ 3, 4, 5, 6, 7, 8, 9, 10, 11])</code></pre>
</div>
</div>
<p>You may reasonably ask — why do I need this <code>range</code> thing, if I have the very similar <code>np.arange</code>? The answer is — you don’t <em>need</em> <code>range</code>, and you can always use <code>np.arange</code> where you would use <code>range</code>, but for reasons we will go into later (<a href="resampling_with_code2.html#sec-for-range" class="quarto-xref"><span>Section 6.6.3</span></a>), <code>range</code> is a good option when we want to represent a sequence of numbers as input to a <code>for</code> loop. We cover <code>for</code> loops in more detail in <a href="resampling_with_code2.html#sec-for-loops" class="quarto-xref"><span>Section 6.6.2</span></a>, but for now, the only thing to remember is that <code>range</code> and <code>np.arange</code> are both ways of expressing sequential ranges of integers.</p>
</section>
<section id="sec-random-choice" class="level2" data-number="5.11">
<h2 data-number="5.11" class="anchored" data-anchor-id="sec-random-choice">5.11 Choosing values at random</h2>
<p>We can use the <code>rnd.choice</code> function to select a single value <em>at random</em> from the sequence of numbers in <code>some_integers</code>.</p>
<div class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
More on <code>rnd.choice</code>
</div>
</div>
<div class="callout-body-container callout-body">
<p>The <code>rnd.choice</code> function will be a fundamental tool for taking many kinds of samples, and we cover it in more detail in <a href="sampling_tools.html" class="quarto-xref"><span>Chapter 7</span></a>.</p>
</div>
</div>
<div class="cell" data-layout-align="center">
<div class="sourceCode cell-code" id="cb47"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb47-1"><a href="#cb47-1" aria-hidden="true" tabindex="-1"></a><span class="co"># Select an integer from the choices in some_integers.</span></span>
<span id="cb47-2"><a href="#cb47-2" aria-hidden="true" tabindex="-1"></a>my_integer <span class="op">=</span> rnd.choice(some_integers)</span>