-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.html
More file actions
2983 lines (2982 loc) · 146 KB
/
index.html
File metadata and controls
2983 lines (2982 loc) · 146 KB
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="" xml:lang="">
<head>
<meta charset="utf-8" />
<meta name="generator" content="pandoc" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes" />
<title>Fall 2022 Final Exam</title>
<style>
/* Default styles provided by pandoc.
** See https://pandoc.org/MANUAL.html#variables-for-html for config info.
*/
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;}
/* The extra [class] is a hack that increases specificity enough to
override a similar rule in reveal.js */
ul.task-list[class]{list-style: none;}
ul.task-list li input[type="checkbox"] {
font-size: inherit;
width: 0.8em;
margin: 0 0.8em 0.2em -1.6em;
vertical-align: middle;
}
/* CSS for syntax highlighting */
html { -webkit-text-size-adjust: 100%; }
pre > code.sourceCode { white-space: pre; position: relative; }
pre > code.sourceCode > span { display: inline-block; 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 { 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;
color: #aaaaaa;
}
pre.numberSource { margin-left: 3em; border-left: 1px solid #aaaaaa; padding-left: 4px; }
div.sourceCode
{ }
@media screen {
pre > code.sourceCode > span > a:first-child::before { text-decoration: underline; }
}
code span.al { color: #ff0000; font-weight: bold; } /* Alert */
code span.an { color: #60a0b0; font-weight: bold; font-style: italic; } /* Annotation */
code span.at { color: #7d9029; } /* Attribute */
code span.bn { color: #40a070; } /* BaseN */
code span.bu { color: #008000; } /* BuiltIn */
code span.cf { color: #007020; font-weight: bold; } /* ControlFlow */
code span.ch { color: #4070a0; } /* Char */
code span.cn { color: #880000; } /* Constant */
code span.co { color: #60a0b0; font-style: italic; } /* Comment */
code span.cv { color: #60a0b0; font-weight: bold; font-style: italic; } /* CommentVar */
code span.do { color: #ba2121; font-style: italic; } /* Documentation */
code span.dt { color: #902000; } /* DataType */
code span.dv { color: #40a070; } /* DecVal */
code span.er { color: #ff0000; font-weight: bold; } /* Error */
code span.ex { } /* Extension */
code span.fl { color: #40a070; } /* Float */
code span.fu { color: #06287e; } /* Function */
code span.im { color: #008000; font-weight: bold; } /* Import */
code span.in { color: #60a0b0; font-weight: bold; font-style: italic; } /* Information */
code span.kw { color: #007020; font-weight: bold; } /* Keyword */
code span.op { color: #666666; } /* Operator */
code span.ot { color: #007020; } /* Other */
code span.pp { color: #bc7a00; } /* Preprocessor */
code span.sc { color: #4070a0; } /* SpecialChar */
code span.ss { color: #bb6688; } /* SpecialString */
code span.st { color: #4070a0; } /* String */
code span.va { color: #19177c; } /* Variable */
code span.vs { color: #4070a0; } /* VerbatimString */
code span.wa { color: #60a0b0; font-weight: bold; font-style: italic; } /* Warning */
</style>
<link rel="stylesheet" href="../assets/theme.css" />
<script defer=""
src="https://cdn.jsdelivr.net/npm/katex@latest/dist/katex.min.js"></script>
<script>document.addEventListener("DOMContentLoaded", function () {
var mathElements = document.getElementsByClassName("math");
var macros = [];
for (var i = 0; i < mathElements.length; i++) {
var texText = mathElements[i].firstChild;
if (mathElements[i].tagName == "SPAN") {
katex.render(texText.data, mathElements[i], {
displayMode: mathElements[i].classList.contains('display'),
throwOnError: false,
macros: macros,
fleqn: false
});
}}});
</script>
<link rel="stylesheet"
href="https://cdn.jsdelivr.net/npm/katex@latest/dist/katex.min.css" />
</head>
<body>
<header id="title-block-header">
<h1 class="title">Fall 2022 Final Exam</h1>
</header>
<p><link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/js/bootstrap.bundle.min.js" integrity="sha384-MrcW6ZMFYlzcLA8Nl+NtUVF0sA7MsXsP1UyJoMp4YLEuNSfAP+JcXn/tWtIaxVXM" crossorigin="anonymous"></script>
<!-- add after bootstrap.min.css -->
<link rel="stylesheet" href="https://cdn.rawgit.com/afeld/bootstrap-toc/v1.0.1/dist/bootstrap-toc.min.css"/>
<!-- add after bootstrap.min.js or bootstrap.bundle.min.js -->
<script src="https://cdn.rawgit.com/afeld/bootstrap-toc/v1.0.1/dist/bootstrap-toc.min.js"></script></p>
<!-- for difficulty gauges-->
<script src="https://cdn.plot.ly/plotly-2.16.1.min.js"></script>
<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=G-B947E6J6H4"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'G-B947E6J6H4');
</script>
<p><a href="../index.html">← return to practice.dsc10.com</a></p>
<hr />
<p><strong>Instructor(s):</strong> Suraj Rampure, Puoya Tabaghi, Janine
Tiefenbruck</p>
<p>This exam was administered in-person. The exam was closed-notes,
except students were provided a copy of the <a
href="https://drive.google.com/file/d/1ky0Np67HS2O4LO913P-ing97SJG0j27n/view">DSC
10 Reference Sheet</a>. No calculators were allowed. Students had
<strong>3 hours</strong> to take this exam.</p>
<hr />
<p><strong>Note (groupby / pandas 2.0):</strong> Pandas 2.0+ no longer
silently drops columns that can’t be aggregated after a
<code>groupby</code>, so code written for older pandas may behave
differently or raise errors. In these practice materials we use
<code>.get()</code> to select the column(s) we want after
<code>.groupby(...).mean()</code> (or other aggregations) so that our
solutions run on current pandas. On real exams you will not be penalized
for omitting <code>.get()</code> when the old behavior would have
produced the same answer.</p>
<hr />
<p>Credit cards allow consumers to make purchases by borrowing money and
paying it back later. Credit card companies are wary of granting this
borrowing ability to consumers who may not be able to pay back their
debt. Therefore, potential credit card carriers must submit an
application that contains information about themselves and their history
of paying back debt.</p>
<p>The DataFrame <code>apps</code> contains application data for a
random sample of 1,000 applicants for a particular credit card from the
1990s. The columns are:</p>
<ul>
<li><p><code>"status" (str)</code>: Whether the credit card application
was approved: <code>"approved"</code> or <code>"denied"</code> values
only.</p></li>
<li><p><code>"age" (float)</code>: The applicant’s age, in years, to the
nearest twelfth of a year.</p></li>
<li><p><code>"income" (float)</code>: The applicant’s annual income, in
tens of thousands of dollars.</p></li>
<li><p><code>"homeowner" (str)</code>: Whether the credit card applicant
owns their own home: <code>"yes"</code> or <code>"no"</code> values
only.</p></li>
<li><p><code>"dependents" (int)</code>: The number of dependents, or
individuals that rely on the applicant as a primary source of income,
such as children.</p></li>
</ul>
<p>The first few rows of <code>apps</code> are shown below, though
remember that <code>apps</code> has 1,000 rows.</p>
<center><img src='../assets/images/fa22-final/apps.jpg' width=45%></center>
<p><br></p>
<p><strong>Throughout this exam, we will refer to <code>apps</code>
repeatedly.</strong></p>
<p>Assume that:</p>
<ul>
<li><p>Each applicant only submitted a single application.</p></li>
<li><p>We have already run <code>import babypandas as bpd</code> and
<code>import numpy as np</code>.</p></li>
</ul>
<p><strong>Tip:</strong> Open this page in another tab, so that it is
easy to refer to this data description as you work through the exam.</p>
<hr />
<h2 id="problem-1">Problem 1</h2>
<div class="meta">
<p><span class="pill pill-lecture" title="Lecture number(s)">Lecture
7</span></div></p>
<p>In this question, we’ll explore the number of dependents of each
applicant. To begin, let’s define the variable dep_counts as
follows.</p>
<p><code>dep_counts = apps.groupby("dependents").count().get(["status"])</code></p>
<p>The visualization below shows the distribution of the numbers of
dependents per applicant. Note that every applicant has 6 or fewer
dependents.</p>
<center><img src='../assets/images/fa22-final/dep_hist_B.jpg' width=30%></center>
<p><br></p>
<p>Use <code>dep_counts</code> and the visualization above to answer the
following questions.</p>
<p><br></p>
<h3 id="problem-1.1">Problem 1.1</h3>
<p>What is the type of the variable <code>dep_counts</code>?</p>
<ul class="task-list">
<li><p><input type="radio" disabled="" /> array</p></li>
<li><p><input type="radio" disabled="" /> Series</p></li>
<li><p><input type="radio" disabled="" /> DataFrame</p></li>
</ul>
<div id="accordionExample" class="accordion">
<div class="accordion-item">
<h2 class="accordion-header" id="heading1_1">
<button class="accordion-button" type="button" data-bs-toggle="collapse" data-bs-target="#collapse1_1" aria-expanded="true" aria-controls="collapse1_1">
Click to view the solution.
</button>
</h2>
<div id="collapse1_1" class="accordion-collapse collapse"
aria-labelledby="heading1_1" data-bs-parent="#accordionExample">
<div class="accordion-body">
<header id="title-block-header">
<h1 class="title"> </h1>
</header>
<p><strong>Answer:</strong> DataFrame</p>
<p>As usual, <code>.groupby</code> produces a new DataFrame. Then we use
<code>.get</code> on this DataFrame with a list as the input, which
produces a DataFrame with just one column. Remember that
<code>.get("status")</code> produces a Series, but
<code>.get(["status"])</code> produces a DataFrame</p>
<hr/>
<h5>Difficulty: ⭐️⭐️</h5>
<p>
</p>
<p>The average score on this problem was 78%.</p>
</div>
</div>
</div>
</div>
<p><br></p>
<h3 id="problem-1.2">Problem 1.2</h3>
<p>What type of data visualization is shown above?</p>
<ul class="task-list">
<li><p><input type="radio" disabled="" /> line plot</p></li>
<li><p><input type="radio" disabled="" /> scatter plot</p></li>
<li><p><input type="radio" disabled="" /> bar chart</p></li>
<li><p><input type="radio" disabled="" /> histogram</p></li>
</ul>
<div id="accordionExample" class="accordion">
<div class="accordion-item">
<h2 class="accordion-header" id="heading1_2">
<button class="accordion-button" type="button" data-bs-toggle="collapse" data-bs-target="#collapse1_2" aria-expanded="true" aria-controls="collapse1_2">
Click to view the solution.
</button>
</h2>
<div id="collapse1_2" class="accordion-collapse collapse"
aria-labelledby="heading1_2" data-bs-parent="#accordionExample">
<div class="accordion-body">
<header id="title-block-header">
<h1 class="title"> </h1>
</header>
<p><strong>Answer:</strong> histogram</p>
<p>This is a histogram because the number of dependents per applicant is
a numerical variable. It makes sense, for example, to subtract the
number of dependents for two applicants to see how many more dependents
one applicant has than the other. Histograms show distributions of
numerical variables.</p>
<hr/>
<h5>Difficulty: ⭐️</h5>
<p>
</p>
<p>The average score on this problem was 91%.</p>
</div>
</div>
</div>
</div>
<p><br></p>
<h3 id="problem-1.3">Problem 1.3</h3>
<p>How many of the 1,000 applicants in <code>apps</code> have 2 or more
dependents? Give your answer as an <strong>integer</strong>.</p>
<div id="accordionExample" class="accordion">
<div class="accordion-item">
<h2 class="accordion-header" id="heading1_3">
<button class="accordion-button" type="button" data-bs-toggle="collapse" data-bs-target="#collapse1_3" aria-expanded="true" aria-controls="collapse1_3">
Click to view the solution.
</button>
</h2>
<div id="collapse1_3" class="accordion-collapse collapse"
aria-labelledby="heading1_3" data-bs-parent="#accordionExample">
<div class="accordion-body">
<header id="title-block-header">
<h1 class="title"> </h1>
</header>
<p><strong>Answer:</strong> 400</p>
<p>The bars of a density histogram have a combined total area of 1, and
the area in any bar represents the proportion of values that fall in
that bin.</p>
<p>In tihs problem, we want the total area of the bins corresponding to
2 or more dependents. Since this involves 5 bins, whose exact heights
are unclear, we will instead calculate the proportion of all applicants
with 0 or 1 dependents, and then subtract this proportion from 1.</p>
<p>Since the width of each bin is 1, we have for each bin, <span class="math display">
\begin{align*}
\text{Area} &= \text{Height} \cdot \text{Width}\\
\text{Area} &= \text{Height}.
\end{align*}</span></p>
<p>Since the height of the first bar is 0.4, this means a proportion of
0.4 applicants have 0 dependents. Similarly, since the height of the
second bar is 0.2, a proportion of 0.2 applicants have 1 dependent. This
means <span class="math inline">1-(0.4+0.2) = 0.4</span> proportion of
applicants have 2 or more dependents. Since there are 1,000 applicants
total, this is 400 applicants.</p>
<hr/>
<h5>Difficulty: ⭐️⭐️</h5>
<p>
</p>
<p>The average score on this problem was 82%.</p>
</div>
</div>
</div>
</div>
<p><br></p>
<h3 id="problem-1.4">Problem 1.4</h3>
<p>Define the DataFrame <code>dependents_status</code> as follows.</p>
<p><code>dependents_status = apps.groupby(["dependents", "status"]).count()</code></p>
<p>What is the maximum number of rows that
<code>dependents_status</code> could have? Give your answer as an
<strong>integer</strong>.</p>
<div id="accordionExample" class="accordion">
<div class="accordion-item">
<h2 class="accordion-header" id="heading1_4">
<button class="accordion-button" type="button" data-bs-toggle="collapse" data-bs-target="#collapse1_4" aria-expanded="true" aria-controls="collapse1_4">
Click to view the solution.
</button>
</h2>
<div id="collapse1_4" class="accordion-collapse collapse"
aria-labelledby="heading1_4" data-bs-parent="#accordionExample">
<div class="accordion-body">
<header id="title-block-header">
<h1 class="title"> </h1>
</header>
<p><strong>Answer:</strong> 14</p>
<p>When we group by multiple columns, the resulting DataFrame has one
row for each combination of values in those columns. Since there are 7
possible values for <code>"dependents"</code> (0, 1, 2, 3, 4, 5, 6) and
2 possible values for <code>"status"</code> (<code>"approved"</code>,
<code>"denied"</code>), this means there are <span class="math inline">7\cdot 2 = 14</span> possible combinations of values
for these two columns.</p>
<hr/>
<h5>Difficulty: ⭐️⭐️⭐️</h5>
<p>
</p>
<p>The average score on this problem was 59%.</p>
</div>
</div>
</div>
</div>
<p><br></p>
<h3 id="problem-1.5">Problem 1.5</h3>
<p>Recall that <code>dep_counts</code> is defined as follows.</p>
<p><code>dep_counts = apps.groupby("dependents").count().get(["status"])</code></p>
<p>Below, we define several more variables.</p>
<div class="sourceCode" id="cb1"><pre class="sourceCode py"><code class="sourceCode python"><span id="cb1-1"><a href="#cb1-1" aria-hidden="true" tabindex="-1"></a>variable1 <span class="op">=</span> dep_counts[dep_counts.get(<span class="st">"status"</span>) <span class="op">>=</span> <span class="dv">2</span>].<span class="bu">sum</span>()</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>variable2 <span class="op">=</span> dep_counts[dep_counts.index <span class="op">></span> <span class="dv">2</span>].get(<span class="st">"status"</span>).<span class="bu">sum</span>() </span>
<span id="cb1-4"><a href="#cb1-4" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb1-5"><a href="#cb1-5" aria-hidden="true" tabindex="-1"></a>variable3 <span class="op">=</span> (dep_counts.get(<span class="st">"status"</span>).<span class="bu">sum</span>() </span>
<span id="cb1-6"><a href="#cb1-6" aria-hidden="true" tabindex="-1"></a> <span class="op">-</span> dep_counts[dep_counts.index <span class="op"><</span> <span class="dv">2</span>].get(<span class="st">"status"</span>).<span class="bu">sum</span>())</span>
<span id="cb1-7"><a href="#cb1-7" aria-hidden="true" tabindex="-1"></a> </span>
<span id="cb1-8"><a href="#cb1-8" aria-hidden="true" tabindex="-1"></a>variable4 <span class="op">=</span> dep_counts.take(np.arange(<span class="dv">2</span>, <span class="dv">7</span>)).get(<span class="st">"status"</span>).<span class="bu">sum</span>()</span>
<span id="cb1-9"><a href="#cb1-9" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb1-10"><a href="#cb1-10" aria-hidden="true" tabindex="-1"></a>variable5 <span class="op">=</span> (dep_counts.get(<span class="st">"status"</span>).<span class="bu">sum</span>() </span>
<span id="cb1-11"><a href="#cb1-11" aria-hidden="true" tabindex="-1"></a> <span class="op">-</span> dep_counts.get(<span class="st">"status"</span>).loc[<span class="dv">1</span>] </span>
<span id="cb1-12"><a href="#cb1-12" aria-hidden="true" tabindex="-1"></a> <span class="op">-</span> dep_counts.get(<span class="st">"status"</span>).loc[<span class="dv">2</span>])</span></code></pre></div>
<p>Which of these variables are equal to your answer from part (c)?
Select all that apply.</p>
<ul class="task-list">
<li><p><input type="checkbox" disabled="" /> <code>variable1</code></p></li>
<li><p><input type="checkbox" disabled="" /> <code>variable2</code></p></li>
<li><p><input type="checkbox" disabled="" /> <code>variable3</code></p></li>
<li><p><input type="checkbox" disabled="" /> <code>variable4</code></p></li>
<li><p><input type="checkbox" disabled="" /> <code>variable5</code></p></li>
<li><p><input type="checkbox" disabled="" /> None of the above.</p></li>
</ul>
<div id="accordionExample" class="accordion">
<div class="accordion-item">
<h2 class="accordion-header" id="heading1_5">
<button class="accordion-button" type="button" data-bs-toggle="collapse" data-bs-target="#collapse1_5" aria-expanded="true" aria-controls="collapse1_5">
Click to view the solution.
</button>
</h2>
<div id="collapse1_5" class="accordion-collapse collapse"
aria-labelledby="heading1_5" data-bs-parent="#accordionExample">
<div class="accordion-body">
<header id="title-block-header">
<h1 class="title"> </h1>
</header>
<p><strong>Answer:</strong> <code>variable3</code>,
<code>variable4</code></p>
<p>First, the DataFrame <code>dep_counts</code> is indexed by
<code>"dependents"</code> and has just one column, called
<code>"status"</code> containing the number of applicants with each
number of dependents. For example, <code>dep_counts</code> may look like
the DataFrame shown below.</p>
<center><img src="../assets/images/fa22-final/dep_counts.jpg" width="10%"/></center>
<p><br/></p>
<p><code>variable1</code> does not work because it doesn’t make sense to
query with the condition <code>dep_counts.get("status") >= 2</code>.
In the example <code>dep_counts</code> shown above, all rows would
satisfy this condition, but not all rows correspond to applicants with 2
or more dependents. We should be querying based on the values in the
index instead.</p>
<p><code>variable2</code> is close but it uses a strict inequality
<code>></code> where it should use <code>>=</code> because we want
to include applicants with 2 dependents.</p>
<p><code>variable3</code> is correct. It uses the same approach we used
in part (c). That is, in order to calculate the number of applicants
with 2 or more dependents, we calculate the total number of applicants
minus the number of applicants with less than 2 dependents.</p>
<p><code>variable4</code> works as well. The strategy here is to keep
only the rows that correspond to 2 or more dependents. Recall that
<code>np.arange(2, 7)</code> evaluates to the array
<code>np.array([2, 3, 4, 5, 6])</code>. Since we are told that each
applicant has 6 or fewer dependents, keeping only these rows
correspondings to keeping all applicants with 2 or more dependents.</p>
<p><code>variable5</code> does not work because it subtracts away the
applicants with 1 or 2 dependents, leaving the applicants with 0, 3, 4,
5, or 6 dependents. This is not what we want.</p>
<hr/>
<h5>Difficulty: ⭐️⭐️</h5>
<p>
</p>
<p>The average score on this problem was 77%.</p>
</div>
</div>
</div>
</div>
<p><br></p>
<p><br></p>
<h3 id="problem-1.6">Problem 1.6</h3>
<p>Next, we define variables <code>x</code> and <code>y</code> as
follows.</p>
<div class="sourceCode" id="cb2"><pre class="sourceCode py"><code class="sourceCode python"><span id="cb2-1"><a href="#cb2-1" aria-hidden="true" tabindex="-1"></a>x <span class="op">=</span> dep_counts.index.values</span>
<span id="cb2-2"><a href="#cb2-2" aria-hidden="true" tabindex="-1"></a>y <span class="op">=</span> dep_counts.get(<span class="st">"status"</span>)</span></code></pre></div>
<p><em>Note</em>: If <code>idx</code> is the index of a Series or
DataFrame, <code>idx.values</code> gives the values in <code>idx</code>
<strong>as an array</strong>.</p>
<p>Which of the following expressions evaluate to the mean number of
dependents? Select all that apply.</p>
<ul class="task-list">
<li><p><input type="checkbox" disabled="" /> <code>np.mean(x * y)</code></p></li>
<li><p><input type="checkbox" disabled="" /> <code>x.sum() / y.sum()</code></p></li>
<li><p><input type="checkbox" disabled="" /> <code>(x * y / y.sum()).sum()</code></p></li>
<li><p><input type="checkbox" disabled="" /> <code>np.mean(x)</code></p></li>
<li><p><input type="checkbox" disabled="" /> <code>(x * y).sum() / y.sum()</code></p></li>
<li><p><input type="checkbox" disabled="" /> None of the above.</p></li>
</ul>
<div id="accordionExample" class="accordion">
<div class="accordion-item">
<h2 class="accordion-header" id="heading1_6">
<button class="accordion-button" type="button" data-bs-toggle="collapse" data-bs-target="#collapse1_6" aria-expanded="true" aria-controls="collapse1_6">
Click to view the solution.
</button>
</h2>
<div id="collapse1_6" class="accordion-collapse collapse"
aria-labelledby="heading1_6" data-bs-parent="#accordionExample">
<div class="accordion-body">
<header id="title-block-header">
<h1 class="title"> </h1>
</header>
<p><strong>Answer:</strong> <code>(x * y / y.sum()).sum()</code>,
<code>(x * y).sum() / y.sum()</code></p>
<p>We know that <code>x</code> is
<code>np.array([0, 1, 2, 3, 4, 5, 6])</code> and <code>y</code> is a
Series containing the number of applicants with each number of
dependents. We don’t know the exact values of the data in
<code>y</code>, but we do know there are 7 elements that sum to 1000,
the first two of which are 400 and 200.</p>
<p><code>np.mean(x * y)</code> does not work because <code>x * y</code>
has 7 elements, so <code>np.mean(x * y)</code> is equivalent to
<code>sum(x * y) / 7</code>, but the mean number of dependents should be
<code>sum(x * y) / 1000</code> since there are 1000 applicants.</p>
<p><code>x.sum() / y.sum()</code> evaluates to <span class="math inline">\frac{21}{1000}</span> regardless of how many
applicants have each number of dependents, so it must be incorrect.</p>
<p><code>(x * y / y.sum()).sum()</code> works. We can think of
<code>y / y.sum()</code> as a Series containing the proportion of
applicants with each number of dependents. For example, the first two
entries of <code>y / y.sum()</code> are 0.4 and 0.2. When we multiply
this Series by <code>x</code> and sum up all 7 entries, the result is a
weighted average of the different number of dependents, where the
weights are given by the proportion of applicants with each number of
dependents.</p>
<p><code>np.mean(x)</code> evaluates to 3 regardless of how many
applicants have each number of dependents, so it must be incorrect.</p>
<p><code>(x * y).sum() / y.sum()</code> works because the numerator
<code>(x * y).sum()</code> represents the total number of dependents
across all 1,000 applicants and the denominator is the number of
applicants, or 1,000. The total divided by the count gives the mean
number of dependents.</p>
<hr/>
<h5>Difficulty: ⭐️⭐️⭐️</h5>
<p>
</p>
<p>The average score on this problem was 71%.</p>
</div>
</div>
</div>
</div>
<p><br></p>
<h3 id="problem-1.7">Problem 1.7</h3>
<p>What does the expression <code>y.iloc[0] / y.sum()</code> evaluate
to? Give your answer as a <strong>fully simplified
fraction</strong>.</p>
<div id="accordionExample" class="accordion">
<div class="accordion-item">
<h2 class="accordion-header" id="heading1_7">
<button class="accordion-button" type="button" data-bs-toggle="collapse" data-bs-target="#collapse1_7" aria-expanded="true" aria-controls="collapse1_7">
Click to view the solution.
</button>
</h2>
<div id="collapse1_7" class="accordion-collapse collapse"
aria-labelledby="heading1_7" data-bs-parent="#accordionExample">
<div class="accordion-body">
<header id="title-block-header">
<h1 class="title"> </h1>
</header>
<p><strong>Answer:</strong> 0.4</p>
<p><code>y.iloc[0]</code> represents the number of applicants with 0
dependents, which is 400. <code>y.sum()</code> represents the total
number of applicants, which is 1,000. So the ratio of these is 0.4.</p>
<hr/>
<h5>Difficulty: ⭐️⭐️⭐️</h5>
<p>
</p>
<p>The average score on this problem was 73%.</p>
</div>
</div>
</div>
</div>
<p><br></p>
<hr />
<h2 id="problem-2">Problem 2</h2>
<div class="meta">
<p><span class="pill pill-lecture" title="Lecture number(s)">Lecture
20</span></div></p>
<p>For each application in <code>apps</code>, we want to assign an age
category based on the value in the <code>"age"</code> column, according
to the table below.</p>
<center>
<table class="table" style="width:30%">
<thead>
<tr>
<th scope="col"><code>"age"</code></th>
<th scope="col">age category</th>
</tr>
</thead>
<tbody>
<tr>
<th scope="row">under 25</th>
<td><code>"young adult"</code></td>
</tr>
<tr>
<th scope="row">at least 25, but less than 50</th>
<td><code>"middle aged"</code></td>
</tr>
<tr>
<th scope="row">at least 50, but less than 75</th>
<td><code>"older adult"</code></td>
</tr>
<tr>
<th scope="row">75 or over</th>
<td><code>"elderly"</code></td>
</tr>
</tbody>
</table>
</center>
<div class="sourceCode" id="cb3"><pre class="sourceCode py"><code class="sourceCode python"><span id="cb3-1"><a href="#cb3-1" aria-hidden="true" tabindex="-1"></a>cat_names <span class="op">=</span> [<span class="st">"young adult"</span>, <span class="st">"middle aged"</span>, <span class="st">"older adult"</span>, <span class="st">"elderly"</span>]</span>
<span id="cb3-2"><a href="#cb3-2" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb3-3"><a href="#cb3-3" aria-hidden="true" tabindex="-1"></a><span class="kw">def</span> age_to_bin(one_age):</span>
<span id="cb3-4"><a href="#cb3-4" aria-hidden="true" tabindex="-1"></a> <span class="co">'''Returns the age category corresponding to one_age.'''</span></span>
<span id="cb3-5"><a href="#cb3-5" aria-hidden="true" tabindex="-1"></a> one_age <span class="op">=</span> __(a)__</span>
<span id="cb3-6"><a href="#cb3-6" aria-hidden="true" tabindex="-1"></a> bin_pos <span class="op">=</span> __(b)__</span>
<span id="cb3-7"><a href="#cb3-7" aria-hidden="true" tabindex="-1"></a> <span class="cf">return</span> cat_names[bin_pos]</span>
<span id="cb3-8"><a href="#cb3-8" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb3-9"><a href="#cb3-9" aria-hidden="true" tabindex="-1"></a>binned_ages <span class="op">=</span> __(c)__</span>
<span id="cb3-10"><a href="#cb3-10" aria-hidden="true" tabindex="-1"></a>apps_cat <span class="op">=</span> apps.assign(age_category <span class="op">=</span> binned_ages)</span></code></pre></div>
<p><br></p>
<h3 id="problem-2.1">Problem 2.1</h3>
<p>Which of the following is a correct way to fill in blanks (a) and
(b)?</p>
<center>
<table class="table" style="width:50%">
<thead>
<tr>
<th scope="col"></th>
<th scope="col">Blank (a)</th>
<th scope="col">Blank (b)</th>
</tr>
</thead>
<tbody>
<tr>
<th scope="row">Option 1</th>
<td><code>75 - one_age</code></td>
<td><code>round(one_age / 25)</code></td>
</tr>
<tr>
<th scope="row">Option 2</th>
<td><code>min(75, one_age)</code></td>
<td><code>one_age / 25</code></td>
</tr>
<tr>
<th scope="row">Option 3</th>
<td><code>75 - one_age</code></td>
<td><code>int(one_age / 25)</code></td>
</tr>
<tr>
<th scope="row">Option 4</th>
<td><code>min(75, one_age)</code></td>
<td><code>int(one_age / 25)</code></td>
</tr>
<tr>
<th scope="row">Option 5</th>
<td><code>min(74, one_age)</code></td>
<td><code>round(one_age / 25)</code></td>
</tr>
</tbody>
</table>
</center>
<ul class="task-list">
<li><p><input type="radio" disabled="" /> Option 1</p></li>
<li><p><input type="radio" disabled="" /> Option 2</p></li>
<li><p><input type="radio" disabled="" /> Option 3</p></li>
<li><p><input type="radio" disabled="" /> Option 4</p></li>
<li><p><input type="radio" disabled="" /> Option 5</p></li>
</ul>
<div id="accordionExample" class="accordion">
<div class="accordion-item">
<h2 class="accordion-header" id="heading2_1">
<button class="accordion-button" type="button" data-bs-toggle="collapse" data-bs-target="#collapse2_1" aria-expanded="true" aria-controls="collapse2_1">
Click to view the solution.
</button>
</h2>
<div id="collapse2_1" class="accordion-collapse collapse"
aria-labelledby="heading2_1" data-bs-parent="#accordionExample">
<div class="accordion-body">
<header id="title-block-header">
<h1 class="title"> </h1>
</header>
<p><strong>Answer:</strong> Option 4</p>
<p>The line <code>one_age = min(75, one_age)</code> either leaves
<code>one_age</code> alone or sets it equal to 75 if the age was higher
than 75, which means anyone over age 75 is considered to be 75 years old
for the purposes of classifying them into age categories. From the
return statement, we know we need our value for <code>bin_pos</code> to
be either 0, 1 ,2 or 3 since <code>cat_names</code> has a length of 4.
When we divide <code>one_age</code> by 25, we get a decimal number that
represents how many times 25 fits into <code>one_age</code>. We want to
round this number <em>down</em> to get the number of whole copies of 25
that fit into <code>one_age</code>. If that value is 0, it means the
person is a <code>"young adult"</code>, if that value is 1, it means
they are <code>"middle aged"</code>, and so on. The rounding down
behavior that we want is accomplished by
<code>int(one_age/25)</code>.</p>
<hr/>
<h5>Difficulty: ⭐️⭐️</h5>
<p>
</p>
<p>The average score on this problem was 76%.</p>
</div>
</div>
</div>
</div>
<p><br></p>
<p><br></p>
<h3 id="problem-2.2">Problem 2.2</h3>
<p>Which of the following is a correct way to fill in blank (c)?</p>
<ul class="task-list">
<li><p><input type="radio" disabled="" /> <code>age to bin(apps.get("age"))</code></p></li>
<li><p><input type="radio" disabled="" /> <code>apps.get("age").apply(age to bin)</code></p></li>
<li><p><input type="radio" disabled="" /> <code>apps.get("age").age to bin()</code></p></li>
<li><p><input type="radio" disabled="" /> <code>apps.get("age").apply(age to bin(one age))</code></p></li>
</ul>
<div id="accordionExample" class="accordion">
<div class="accordion-item">
<h2 class="accordion-header" id="heading2_2">
<button class="accordion-button" type="button" data-bs-toggle="collapse" data-bs-target="#collapse2_2" aria-expanded="true" aria-controls="collapse2_2">
Click to view the solution.
</button>
</h2>
<div id="collapse2_2" class="accordion-collapse collapse"
aria-labelledby="heading2_2" data-bs-parent="#accordionExample">
<div class="accordion-body">
<header id="title-block-header">
<h1 class="title"> </h1>
</header>
<p><strong>Answer: </strong>
<code>apps.get("age").apply(age to bin)</code></p>
<p>We want our result to be a Series because the next line in the code
assigns it to a DataFrame. We also need to use the <code>.apply()</code>
method to apply our function to the entirety of the <code>"age"</code>
column. The <code>.apply()</code> method only takes in the name of a
function and not its variables, as it treats the entries of the column
as the variables directly.</p>
<hr/>
<h5>Difficulty: ⭐️</h5>
<p>
</p>
<p>The average score on this problem was 96%.</p>
</div>
</div>
</div>
</div>
<p><br></p>
<p><br></p>
<h3 id="problem-2.3">Problem 2.3</h3>
<p>Which of the following is a correct alternate implementation of the
age to bin function? Select all that apply.</p>
<p>Option 1:</p>
<div class="sourceCode" id="cb4"><pre class="sourceCode py"><code class="sourceCode python"><span id="cb4-1"><a href="#cb4-1" aria-hidden="true" tabindex="-1"></a><span class="kw">def</span> age_to_bin(one_age):</span>
<span id="cb4-2"><a href="#cb4-2" aria-hidden="true" tabindex="-1"></a> bin_pos <span class="op">=</span> <span class="dv">3</span></span>
<span id="cb4-3"><a href="#cb4-3" aria-hidden="true" tabindex="-1"></a> <span class="cf">if</span> one_age <span class="op"><</span> <span class="dv">25</span>:</span>
<span id="cb4-4"><a href="#cb4-4" aria-hidden="true" tabindex="-1"></a> bin_pos <span class="op">=</span> <span class="dv">0</span></span>
<span id="cb4-5"><a href="#cb4-5" aria-hidden="true" tabindex="-1"></a> <span class="cf">if</span> one_age <span class="op"><</span> <span class="dv">50</span>:</span>
<span id="cb4-6"><a href="#cb4-6" aria-hidden="true" tabindex="-1"></a> bin_pos <span class="op">=</span> <span class="dv">1</span></span>
<span id="cb4-7"><a href="#cb4-7" aria-hidden="true" tabindex="-1"></a> <span class="cf">if</span> one_age <span class="op"><</span> <span class="dv">75</span>:</span>
<span id="cb4-8"><a href="#cb4-8" aria-hidden="true" tabindex="-1"></a> bin_pos <span class="op">=</span> <span class="dv">2</span></span>
<span id="cb4-9"><a href="#cb4-9" aria-hidden="true" tabindex="-1"></a> <span class="cf">return</span> cat_names[bin_pos]</span></code></pre></div>
<p>Option 2:</p>
<div class="sourceCode" id="cb5"><pre class="sourceCode py"><code class="sourceCode python"><span id="cb5-1"><a href="#cb5-1" aria-hidden="true" tabindex="-1"></a><span class="kw">def</span> age_to_bin(one_age):</span>
<span id="cb5-2"><a href="#cb5-2" aria-hidden="true" tabindex="-1"></a> bin_pos <span class="op">=</span> <span class="dv">3</span></span>
<span id="cb5-3"><a href="#cb5-3" aria-hidden="true" tabindex="-1"></a> <span class="cf">if</span> one_age <span class="op"><</span> <span class="dv">75</span>:</span>
<span id="cb5-4"><a href="#cb5-4" aria-hidden="true" tabindex="-1"></a> bin_pos <span class="op">=</span> <span class="dv">2</span></span>
<span id="cb5-5"><a href="#cb5-5" aria-hidden="true" tabindex="-1"></a> <span class="cf">if</span> one_age <span class="op"><</span> <span class="dv">50</span>:</span>
<span id="cb5-6"><a href="#cb5-6" aria-hidden="true" tabindex="-1"></a> bin_pos <span class="op">=</span> <span class="dv">1</span></span>
<span id="cb5-7"><a href="#cb5-7" aria-hidden="true" tabindex="-1"></a> <span class="cf">if</span> one_age <span class="op"><</span> <span class="dv">25</span>:</span>
<span id="cb5-8"><a href="#cb5-8" aria-hidden="true" tabindex="-1"></a> bin_pos <span class="op">=</span> <span class="dv">0</span></span>
<span id="cb5-9"><a href="#cb5-9" aria-hidden="true" tabindex="-1"></a> <span class="cf">return</span> cat_names[bin_pos]</span></code></pre></div>
<p>Option 3:</p>
<div class="sourceCode" id="cb6"><pre class="sourceCode py"><code class="sourceCode python"><span id="cb6-1"><a href="#cb6-1" aria-hidden="true" tabindex="-1"></a><span class="kw">def</span> age_to_bin(one_age):</span>
<span id="cb6-2"><a href="#cb6-2" aria-hidden="true" tabindex="-1"></a> bin_pos <span class="op">=</span> <span class="dv">0</span></span>
<span id="cb6-3"><a href="#cb6-3" aria-hidden="true" tabindex="-1"></a> <span class="cf">for</span> cutoff <span class="kw">in</span> np.arange(<span class="dv">25</span>, <span class="dv">100</span>, <span class="dv">25</span>):</span>
<span id="cb6-4"><a href="#cb6-4" aria-hidden="true" tabindex="-1"></a> <span class="cf">if</span> one_age <span class="op">>=</span> cutoff:</span>
<span id="cb6-5"><a href="#cb6-5" aria-hidden="true" tabindex="-1"></a> bin_pos <span class="op">=</span> bin_pos <span class="op">+</span> <span class="dv">1</span> </span>
<span id="cb6-6"><a href="#cb6-6" aria-hidden="true" tabindex="-1"></a> <span class="cf">return</span> cat_names[bin_pos]</span></code></pre></div>
<p>Option 4:</p>
<div class="sourceCode" id="cb7"><pre class="sourceCode py"><code class="sourceCode python"><span id="cb7-1"><a href="#cb7-1" aria-hidden="true" tabindex="-1"></a><span class="kw">def</span> age_to_bin(one_age):</span>
<span id="cb7-2"><a href="#cb7-2" aria-hidden="true" tabindex="-1"></a> bin_pos <span class="op">=</span> <span class="op">-</span><span class="dv">1</span></span>
<span id="cb7-3"><a href="#cb7-3" aria-hidden="true" tabindex="-1"></a> <span class="cf">for</span> cutoff <span class="kw">in</span> np.arange(<span class="dv">0</span>, <span class="dv">100</span>, <span class="dv">25</span>):</span>
<span id="cb7-4"><a href="#cb7-4" aria-hidden="true" tabindex="-1"></a> <span class="cf">if</span> one_age <span class="op">>=</span> cutoff:</span>
<span id="cb7-5"><a href="#cb7-5" aria-hidden="true" tabindex="-1"></a> bin_pos <span class="op">=</span> bin_pos <span class="op">+</span> <span class="dv">1</span> </span>
<span id="cb7-6"><a href="#cb7-6" aria-hidden="true" tabindex="-1"></a> <span class="cf">return</span> cat_names[bin_pos]</span></code></pre></div>
<ul class="task-list">
<li><p><input type="checkbox" disabled="" /> Option 1</p></li>
<li><p><input type="checkbox" disabled="" /> Option 2</p></li>
<li><p><input type="checkbox" disabled="" /> Option 3</p></li>
<li><p><input type="checkbox" disabled="" /> Option 4</p></li>
<li><p><input type="checkbox" disabled="" /> None of the above.</p></li>
</ul>
<div id="accordionExample" class="accordion">
<div class="accordion-item">
<h2 class="accordion-header" id="heading2_3">
<button class="accordion-button" type="button" data-bs-toggle="collapse" data-bs-target="#collapse2_3" aria-expanded="true" aria-controls="collapse2_3">
Click to view the solution.
</button>
</h2>
<div id="collapse2_3" class="accordion-collapse collapse"
aria-labelledby="heading2_3" data-bs-parent="#accordionExample">
<div class="accordion-body">
<header id="title-block-header">
<h1 class="title"> </h1>
</header>
<p><strong>Answer: </strong> Option 2 and Option 3</p>
<p>Option 1 doesn’t work for inputs less than 25. For example, on an
input of 10, every condition is satsified, so <code>bin_pos</code> will
be set to 0, then 1, then 2, meaning the function will return
<code>"older adult"</code> instead of <code>"young adult"</code>.</p>
<p>Option 2 reverses the order of the conditions, which ensures that
even when a number satisfies many conditions, the last one it satisfies
determines the correct <code>bin_pos</code>. For example, 27 would
satisfy the first 2 conditions but not the last one, and the function
would return <code>"middle aged"</code> as expected.</p>
<p>In option 3, <code>np.arange(25, 100, 25)</code> produces
<code>np.array([25,50,75])</code>. The <code>if</code> condition checks
the whether the age is at least 25, then 50, then 75. For every time
that it is, it adds to <code>bin_pos</code>, otherwise it keeps
<code>bin_pos</code>. At the end, <code>bin_pos</code> represents the
number of these values that the age is greater than or equal to, which
correctly determines the age category.</p>
<p>Option 4 is equivalent to option 3 except for two things. First,
<code>bin_pos</code> starts at -1, but since 0 is included in the set of
<code>cutoff</code> values, the first time through the loop will set
<code>bin_pos</code> to 0, as in Option 3. This change doesn’t affect
the behavior of the funtion. The other change, however, is that the
<code>return</code> statement is inside the <code>for</code>-loop, which
does change the behavior of the function dramatically. Now the
<code>for</code>-loop will only run once, checking whether the age is at
least 0 and then returning immediately. Since ages are always at least
0, this function will return <code>"young adult"</code> on every input,
which is clearly incorrect.</p>
<hr/>
<h5>Difficulty: ⭐️⭐️⭐️</h5>
<p>
</p>
<p>The average score on this problem was 62%.</p>
</div>
</div>
</div>
</div>
<p><br></p>
<h3 id="problem-2.4">Problem 2.4</h3>
<p>We want to determine the number of <code>"middle aged"</code>
applicants whose applications were denied. Fill in the blank below so
that <code>count</code> evaluates to that number.</p>
<div class="sourceCode" id="cb8"><pre class="sourceCode py"><code class="sourceCode python"><span id="cb8-1"><a href="#cb8-1" aria-hidden="true" tabindex="-1"></a>df <span class="op">=</span> apps_cat.________.reset_index()</span>
<span id="cb8-2"><a href="#cb8-2" aria-hidden="true" tabindex="-1"></a>count <span class="op">=</span> df[(df.get(<span class="st">"age_category"</span>) <span class="op">==</span> <span class="st">"middle aged"</span>) <span class="op">&</span> </span>
<span id="cb8-3"><a href="#cb8-3" aria-hidden="true" tabindex="-1"></a> (df.get(<span class="st">"status"</span>) <span class="op">==</span> <span class="st">"denied"</span>)].get(<span class="st">"income"</span>).iloc[<span class="dv">0</span>]</span></code></pre></div>
<p>What goes in the blank?</p>
<div id="accordionExample" class="accordion">
<div class="accordion-item">
<h2 class="accordion-header" id="heading2_4">
<button class="accordion-button" type="button" data-bs-toggle="collapse" data-bs-target="#collapse2_4" aria-expanded="true" aria-controls="collapse2_4">
Click to view the solution.
</button>
</h2>
<div id="collapse2_4" class="accordion-collapse collapse"
aria-labelledby="heading2_4" data-bs-parent="#accordionExample">
<div class="accordion-body">
<header id="title-block-header">
<h1 class="title"> </h1>
</header>
<p><strong>Answer: </strong>
<code>groupby(["age_category", "status"]).count()</code></p>
<p>We can tell by the line in which <code>count</code> is defined that
<code>df</code> needs to have columns called
<code>"age category"</code>, <code>"status"</code>, and
<code>"income"</code> with one row such that the values in these columns
are <code>"middle aged"</code>, <code>"denied"</code>, and the number of
such applicants, respectively. Since there is one row corresponding to a
possible combination of values for <code>"age category"</code> and
<code>"status"</code>, this suggests we need to group by the pair of
columns, since <code>.groupby</code> produces a DataFrame with one row
for each possible combination of values in the columns we are grouping
by. Since we want to know how many individuals have this combination of
values for <code>"age category"</code> and <code>"status"</code>, we
should use <code>.count()</code> as the aggregation method. Another clue
to to use <code>.groupby</code> is the presence of
<code>.reset_index()</code> which is needed to query based on columns
called <code>"age category"</code> and <code>"status"</code>.</p>
<hr/>
<h5>Difficulty: ⭐️⭐️</h5>
<p>
</p>
<p>The average score on this problem was 78%.</p>
</div>
</div>
</div>
</div>
<p><br></p>
<h3 id="problem-2.5">Problem 2.5</h3>
<p>The total variation distance between the distributions of
<code>"age category"</code> for approved applications and denied
applications is 0.4.</p>
<p>One of the visualizations below shows the distributions of
<code>"age category"</code> for approved applications and denied
applications. Which visualization is it?</p>
<center><img src='../assets/images/fa22-final/four_tvds_B.jpg' width=50%></center>
<p><br></p>
<div id="accordionExample" class="accordion">
<div class="accordion-item">
<h2 class="accordion-header" id="heading2_5">
<button class="accordion-button" type="button" data-bs-toggle="collapse" data-bs-target="#collapse2_5" aria-expanded="true" aria-controls="collapse2_5">
Click to view the solution.
</button>
</h2>
<div id="collapse2_5" class="accordion-collapse collapse"
aria-labelledby="heading2_5" data-bs-parent="#accordionExample">
<div class="accordion-body">
<header id="title-block-header">
<h1 class="title"> </h1>
</header>
<p><strong>Answer: </strong> Option 2</p>
<p>TVD represents the total overrepresentation of one distrubtion,
summed across all categories. To find the TVD visually, we can estimate
how much each bar for approved applications extends beyond the
corresponding bar for denied applications in each bar chart.</p>
<p>In Option 1, the approved bar extends beyond the denied bar only in
the <code>"young adult"</code> category, and by 0.2, so the TVD for
Option 1 is 0.2. In Option 2, the approved bar extends beyond the denied
bar only in the <code>"older adult"</code> category, and by 0.4, so the
TVD for Option 2 is 0.4. In Option 3, the approved bar extends beyond
the denied bar in <code>"elderly"</code> by 0.2 and in
<code>"young adult"</code> by 0.4, for a TVD of 0.6. In Option 4, the
approved bar extends beyond the denied bar in
<code>"young adult only"</code> by 0.2, for a TVD of 0.2.</p>
<p>Note that even without knowing the exact lengths of the bars in
Option 2, we can still conclude that Option 2 is correct by process of
elimination, since it’s the only one whose TVD appears close to 0.4</p>
<hr/>
<h5>Difficulty: ⭐️⭐️⭐️</h5>
<p>
</p>
<p>The average score on this problem was 60%.</p>
</div>
</div>
</div>
</div>
<p><br></p>
<hr />
<h2 id="problem-3">Problem 3</h2>
<div class="meta">
<p><span class="pill pill-lecture" title="Lecture number(s)">Lecture
14</span></div></p>
<p>In <code>apps</code>, our sample of 1,000 credit card applications,
500 of the applications come from homeowners and 500 come from people
who don’t own their own home. In this sample, homeowner ages have a mean
of 40 and standard deviation of 10. We want to use the bootstrap method
to compute a confidence interval for the mean age of a homeowner in the
population of all credit card applicants.</p>
<p><br></p>
<h3 id="problem-3.1">Problem 3.1</h3>
<p><span style="color:red"><b>Note: This problem is out of scope; it
covers material no longer included in the course.</b></span></p>
<p>Suppose our computer is too slow to bootstrap 10,000 times, and
instead can only bootstrap 20 times. Here are the 20 resample means,
sorted in ascending order: <span class="math display">
\begin{align*}
&37, 38, 39, 39, 40, 40, 40, 40, 41 , 41, \\
&42, 42, 42, 42, 42, 42, 43, 43, 43 , 44
\end{align*}
</span> What are the left and right endpoints of a bootstrapped
<strong>80%</strong> confidence interval for the population mean? Use
the mathematical definition of percentile.</p>
<div id="accordionExample" class="accordion">
<div class="accordion-item">
<h2 class="accordion-header" id="heading3_1">
<button class="accordion-button" type="button" data-bs-toggle="collapse" data-bs-target="#collapse3_1" aria-expanded="true" aria-controls="collapse3_1">
Click to view the solution.
</button>
</h2>
<div id="collapse3_1" class="accordion-collapse collapse"
aria-labelledby="heading3_1" data-bs-parent="#accordionExample">
<div class="accordion-body">
<header id="title-block-header">
<h1 class="title"> </h1>
</header>
<p><strong>Answer: </strong> Left endpoint = 38, Right endpoint = 43</p>
<p>To find an 80% confidence interval, we need to find the 10th and 90th
percentiles of the resample means. Using the mathematical definiton of
percentile, the 10th percentile is at position <span class="math inline">0.1*20 = 2</span> when we count starting with 1.
Since 38 is the second element of the sorted data, that is the left
endpoint of our confidence interval.</p>
<hr/>
<h5>Difficulty: ⭐️⭐️⭐️</h5>
<p>
</p>
<p>The average score on this problem was 63%. <br/><br/></p>
<p>Similarly, the 90th percentile is at position <span class="math inline">0.9*20 = 18</span> when we count starting with 1.
Since 43 is the 18th element of the sorted data, that is the right
endpoint of our confidence interval.</p>
<hr/>
<h5>Difficulty: ⭐️⭐️⭐️</h5>
<p>
</p>
<p>The average score on this problem was 65%.</p>
</div>
</div>
</div>
</div>
<p><br></p>
<h3 id="problem-3.2">Problem 3.2</h3>
<p><span style="color:red"><b>Note: This problem is out of scope; it
covers material no longer included in the course.</b></span></p>
<p>True or False: Using the mathematical definition of percentile, the
50th percentile of the bootstrapped distribution above equals its
median.</p>
<ul class="task-list">
<li><p><input type="radio" disabled="" /> True</p></li>
<li><p><input type="radio" disabled="" /> False</p></li>
</ul>
<div id="accordionExample" class="accordion">
<div class="accordion-item">
<h2 class="accordion-header" id="heading3_2">
<button class="accordion-button" type="button" data-bs-toggle="collapse" data-bs-target="#collapse3_2" aria-expanded="true" aria-controls="collapse3_2">
Click to view the solution.
</button>
</h2>
<div id="collapse3_2" class="accordion-collapse collapse"
aria-labelledby="heading3_2" data-bs-parent="#accordionExample">
<div class="accordion-body">
<header id="title-block-header">
<h1 class="title"> </h1>
</header>
<p><strong>Answer: </strong> False</p>
<p>The 50th percentile according to the mathematial definition is the
element at position <span class="math inline">0.5*20 10</span> when we
count starting with 1. The 10th element is 41. However, the median of a
data set with 20 elements is halfway between the 10th and 11th values.