import%20marimo%0A%0A__generated_with%20%3D%20%220.25.2%22%0Aapp%20%3D%20marimo.App(%0A%20%20%20%20width%3D%22medium%22%2C%0A%20%20%20%20app_title%3D%22Module%208%3A%20Anomalies%20by%20rule%20and%20by%20statistics%22%2C%0A)%0A%0Awith%20app.setup%3A%0A%20%20%20%20%23%20The%20setup%20cell%20runs%20first.%20It%20holds%20the%20imports%20and%20the%20tables%20every%20other%20cell%20uses.%0A%20%20%20%20import%20matplotlib.pyplot%20as%20plt%0A%20%20%20%20import%20marimo%20as%20mo%0A%20%20%20%20import%20numpy%20as%20np%0A%20%20%20%20import%20pandas%20as%20pd%0A%0A%20%20%20%20import%20course%20%20%23%20this%20course's%20helpers%3A%20the%20survey's%20tables%2C%20file%20locations%20and%20exercise%20checks%0A%20%20%20%20from%20course%20import%20NOT_YET%2C%20attempt%2C%20check_cases%2C%20try_it%2C%20waiting_for%0A%0A%20%20%20%20CASE%20%3D%20%5B%22region_code%22%2C%20%22hh_id%22%5D%20%20%23%20a%20household%3A%20its%20region%20and%20its%20id%2C%20since%20an%20id%20repeats%20across%20regions%0A%20%20%20%20UNIT%20%3D%20course.UNIT_KEY%20%20%23%20a%20fishing%20unit%3A%20the%20household%20and%20the%20unit's%20two%20key%20parts%0A%20%20%20%20FEET%20%3D%203.281%20%20%23%20feet%20in%20a%20meter%0A%0A%20%20%20%20%23%20The%20survey%20as%20module%202%20leaves%20it%3A%20every%20region%20in%20one%20table%2C%20removed%20households%20and%20voided%20rows%20left%20out%2C%20area%0A%20%20%20%20%23%20codes%20as%20text%0A%20%20%20%20households%20%3D%20course.households()%0A%20%20%20%20members%20%3D%20course.members()%0A%20%20%20%20units%20%3D%20course.units()%0A%20%20%20%20boat_rows%20%3D%20course.child(%22boat%22)%0A%20%20%20%20catch%20%3D%20course.child(%22catch%22)%0A%20%20%20%20landing%20%3D%20course.child(%22landing%22)%0A%20%20%20%20PROVINCE%20%3D%20households%5B%22region_code%22%5D%20%2B%20%22-%22%20%2B%20households%5B%22province_code%22%5D%20%20%23%20each%20household's%20province%2C%20as%20one%20label%0A%0A%20%20%20%20defects%20%3D%20course.planted_defects()%20%20%23%20every%20defect%20the%20course%20planted%3B%20a%20real%20survey%20has%20no%20such%20list%0A%20%20%20%20%23%20Each%20household's%20planted%20kinds%20as%20one%20text%2C%20to%20show%20beside%20what%20a%20check%20finds%0A%20%20%20%20planted_kinds%20%3D%20defects.groupby(CASE)%5B%22kind%22%5D.agg(lambda%20k%3A%20%22%2C%20%22.join(sorted(set(k)))).rename(%22planted%22).reset_index()%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%20Module%208%3A%20Anomalies%20by%20rule%20and%20by%20statistics%0A%0A%20%20%20%20**Where%20this%20is%20in%20processing%20a%20survey.**%20A%20second%20review%20of%20the%20data%2C%20after%20the%20edits%20of%20module%204%20and%20before%20any%20estimate%20goes%20out.%20The%20values%20every%20rule%20passed%20are%20checked%20again%2C%20this%20time%20with%20statistics%2C%20and%20the%20weights%20are%20checked%20before%20the%20outputs%20are%20validated.%20Module%204's%20frequency%20tables%20showed%20invalid%20and%20extreme%20values%2C%20and%20its%20rules%20dealt%20with%20the%20invalid%20ones%3B%20this%20module%20looks%20for%20the%20extreme%20and%20unusual%20values%20that%20break%20no%20rule%2C%20within%20groups%20and%20by%20interviewer.%0A%0A%20%20%20%20Module%204's%20rules%20find%20values%20the%20questionnaire%20forbids%3A%20a%20sex%20of%208%2C%20a%20species%20code%20that%20is%20not%20on%20the%20list%2C%20a%20boat%20with%20no%20engine%20of%205%20gross%20tons%20or%20more.%20Some%20wrong%20values%20break%20no%20rule.%20A%20boat%20measured%20in%20feet%20and%20recorded%20as%20meters%20becomes%20a%20long%20outrigger%20boat%20whose%20tonnage%20matches%20its%20dimensions%2C%20and%20every%20rule%20passes%20it.%20A%20final%20weight%20with%20an%20adjustment%20applied%20twice%20is%20still%20a%20positive%20number.%20An%20interview%20the%20interviewer%20never%20held%20is%2C%20on%20its%20own%2C%20an%20ordinary%20household.%20This%20module%20finds%20such%20values%20with%20statistics%3A%20a%20value%20against%20the%20values%20like%20it%2C%20a%20ratio%20against%20its%20group's%20ratios%2C%20an%20interviewer's%20work%20against%20the%20others'.%20It%20ends%20with%20every%20flag%2C%20from%20rules%20and%20from%20statistics%2C%20in%20one%20table%20a%20reviewer%20works%20through%2C%20and%20measures%20that%20table%20against%20the%20list%20of%20defects%20the%20course%20planted.%0A%0A%20%20%20%20%7C%20section%20%7C%20what%20you%20do%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%201%20%C2%B7%20Invalid%2C%20implausible%2C%20unusual%20%7C%20three%20kinds%20of%20suspect%20value%2C%20and%20what%20happens%20to%20each%20%7C%0A%20%20%20%20%7C%202%20%C2%B7%20Outliers%20within%20groups%3A%20IQR%2C%20robust%20z%2C%20Hidiroglou-Berthelot%20%7C%20thresholds%20from%20the%20data%20itself%2C%20group%20by%20group%20%7C%0A%20%20%20%20%7C%203%20%C2%B7%20Ratios%2C%20relations%20and%20weight%20outliers%20%7C%20values%20that%20are%20wrong%20only%20beside%20another%20value%3B%20weights%20that%20dominate%20an%20estimate%20%7C%0A%20%20%20%20%7C%204%20%C2%B7%20Interviewer-level%20indicators%20%7C%20patterns%20in%20one%20interviewer's%20work%20that%20no%20single%20record%20shows%20%7C%0A%20%20%20%20%7C%205%20%C2%B7%20Ranking%20flags%20into%20a%20review%20table%20%7C%20every%20flag%20in%20one%20ranked%20table%2C%20judged%20against%20the%20planted%20defects%20%7C%0A%0A%20%20%20%20**The%20tables.**%20The%20setup%20cell%20reads%20the%20survey%20with%20%60course.households()%60%2C%20%60course.members()%60%2C%20%60course.units()%60%20and%20%60course.child(...)%60%2C%20as%20module%202%20leaves%20it%3A%20every%20region%20in%20one%20table%2C%20the%20removed%20households%20and%20voided%20rows%20left%20out%2C%20area%20codes%20as%20text.%20A%20household%20is%20a%20region%20and%20an%20id%20together%20(%60region_code%60%20and%20%60hh_id%60)%2C%20because%20an%20id%20repeats%20across%20regions%3B%20the%20setup%20cell%20names%20that%20pair%20%60CASE%60.%0A%0A%20%20%20%20**The%20list%20of%20planted%20defects.**%20%60course.planted_defects()%60%20lists%20every%20defect%20the%20course%20planted%2C%20by%20household%20and%20kind.%20A%20real%20survey%20has%20no%20such%20list.%20Here%20it%20measures%20what%20each%20check%20finds%20and%20what%20it%20misses%2C%20which%20is%20how%20a%20survey%20team%20decides%20which%20checks%20are%20worth%20a%20reviewer's%20time.%0A%0A%20%20%20%20Exercises%20work%20as%20in%20the%20earlier%20modules%3A%20write%20the%20function%2C%20delete%20the%20%60raise%60%20line%2C%20use%20the%20**Try%20it**%20cell%2C%20read%20the%20check.%20If%20something%20goes%20red%2C%20read%20the%20error's%20**last%20line**%20first%3A%20it%20names%20what%20went%20wrong.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20course.quizzes_at(%228.0%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%201%20%C2%B7%20Invalid%2C%20implausible%2C%20unusual%0A%0A%20%20%20%20**The%20problem.**%20Module%204's%20rules%20ask%20whether%20a%20value%20is%20allowed.%20A%20boat's%20length%20is%20allowed%20if%20it%20is%20a%20positive%20number%2C%20and%20its%20stored%20tonnage%20is%20right%20if%20it%20equals%20length%20%C3%97%20beam%20%C3%97%20depth%20%C3%97%200.212%20(in%20meters%3B%20rule%20B3).%20The%20questionnaire%20takes%20a%20boat's%20size%20from%20its%20registration%20papers%20or%20measures%20it%2C%20in%20meters%20or%20in%20feet%2C%20and%20%60boat_measured_in%60%20says%20which%20(1%20papers%2C%202%20meters%2C%203%20feet).%20To%20compare%20boats%2C%20the%20cell%20below%20puts%20every%20measured%20boat%20in%20meters%2C%20beside%20the%20tonnage%20the%20capture%20program%20stored.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20%23%20One%20row%20per%20boat%2C%20every%20dimension%20in%20meters%20(a%20boat%20measured%20in%20feet%20divided%20by%203.281%3B%20one%20sized%20from%20its%20papers%0A%20%20%20%20%23%20has%20no%20dimensions)%2C%20and%20the%20gross%20tonnage%20the%20capture%20program%20stored%0A%20%20%20%20_in_feet%20%3D%20boat_rows%5B%22boat_measured_in%22%5D%20%3D%3D%203%0A%20%20%20%20boats%20%3D%20boat_rows.assign(%0A%20%20%20%20%20%20%20%20length_m%3Dboat_rows%5B%22boat_length%22%5D.where(~_in_feet%2C%20boat_rows%5B%22boat_length%22%5D%20%2F%20FEET)%2C%0A%20%20%20%20%20%20%20%20beam_m%3Dboat_rows%5B%22boat_beam%22%5D.where(~_in_feet%2C%20boat_rows%5B%22boat_beam%22%5D%20%2F%20FEET)%2C%0A%20%20%20%20%20%20%20%20depth_m%3Dboat_rows%5B%22boat_depth%22%5D.where(~_in_feet%2C%20boat_rows%5B%22boat_depth%22%5D%20%2F%20FEET)%2C%0A%20%20%20%20)%5B%5B*UNIT%2C%20%22boat_no%22%2C%20%22boat_hull%22%2C%20%22boat_engine%22%2C%20%22boat_measured_in%22%2C%20%22length_m%22%2C%20%22beam_m%22%2C%20%22depth_m%22%2C%20%22boat_tonnage%22%5D%5D%0A%20%20%20%20boats.head()%0A%20%20%20%20return%20(boats%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(boats)%3A%0A%20%20%20%20_outrigger%20%3D%20boats%5Bboats%5B%22boat_hull%22%5D%20%3D%3D%202%5D%0A%20%20%20%20_long%20%3D%20_outrigger%5B_outrigger%5B%22length_m%22%5D%20%3E%2015%5D%0A%20%20%20%20_usual%20%3D%20_outrigger%5B_outrigger%5B%22length_m%22%5D%20%3C%3D%2015%5D%5B%22length_m%22%5D%0A%20%20%20%20mo.vstack(%5B%0A%20%20%20%20%20%20%20%20mo.md(f%22%22%22%0A%20%20%20%20%20%20%20%20Here%20are%20the%20outrigger%20boats%20(%60boat_hull%20%3D%202%60)%20longer%20than%2015%20meters.%20Every%20rule%20of%20module%204%20passes%20them%3A%20the%20hull%20and%20the%20engine%20are%20valid%20codes%2C%20the%20stored%20tonnage%20is%20length%20%C3%97%20beam%20%C3%97%20depth%20%C3%97%200.212%2C%20and%20the%20unit's%20scale%20agrees%20with%20it%20(rule%20U3).%20The%20other%20%7Blen(_usual)%3A%2C%7D%20measured%20outrigger%20boats%20are%20**%7B_usual.min()%3A.1f%7D%20to%20%7B_usual.max()%3A.1f%7D%20meters**%20long%3B%20these%20%7Blen(_long)%7D%20are%20**%7B_long%5B'length_m'%5D.min()%3A.1f%7D%20to%20%7B_long%5B'length_m'%5D.max()%3A.1f%7D**.%0A%20%20%20%20%20%20%20%20%22%22%22)%2C%0A%20%20%20%20%20%20%20%20_long%5B%5B*CASE%2C%20%22boat_hull%22%2C%20%22boat_measured_in%22%2C%20%22length_m%22%2C%20%22beam_m%22%2C%20%22depth_m%22%2C%20%22boat_tonnage%22%5D%5D%2C%0A%20%20%20%20%20%20%20%20mo.md(%22The%20same%20boats%20with%20each%20dimension%20divided%20by%203.281%2C%20as%20if%20it%20had%20been%20written%20in%20feet%3A%22)%2C%0A%20%20%20%20%20%20%20%20_long%5BCASE%5D.assign(%0A%20%20%20%20%20%20%20%20%20%20%20%20length_m%3D_long%5B%22length_m%22%5D%20%2F%20FEET%2C%20beam_m%3D_long%5B%22beam_m%22%5D%20%2F%20FEET%2C%20depth_m%3D_long%5B%22depth_m%22%5D%20%2F%20FEET%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20boat_tonnage%3D_long%5B%22length_m%22%5D%20*%20_long%5B%22beam_m%22%5D%20*%20_long%5B%22depth_m%22%5D%20%2F%20FEET**3%20*%200.212%2C%0A%20%20%20%20%20%20%20%20).round(2)%2C%0A%20%20%20%20%5D)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20Divided%20by%203.281%2C%20they%20are%20ordinary%20outrigger%20boats.%20The%20interviewer%20measured%20in%20feet%20and%20recorded%20the%20unit%20as%20meters.%20The%20capture%20program%20then%20computed%20the%20tonnage%20from%20those%20numbers%2C%20so%20it%20agrees%20with%20them%2C%20and%20rule%20B3%20passes.%20No%20rule%20can%20say%20the%20length%20is%20wrong%2C%20because%20boats%20of%2030%20meters%20exist.%20What%20is%20wrong%20is%20the%20length%20**for%20this%20kind%20of%20hull**.%0A%0A%20%20%20%20**Three%20kinds%20of%20suspect%20value.**%20They%20call%20for%20different%20actions%2C%20so%20a%20check%20should%20say%20which%20kind%20it%20found.%0A%0A%20%20%20%20%7C%20kind%20%7C%20what%20it%20is%20%7C%20an%20example%20in%20the%20survey%20%7C%20found%20by%20%7C%20what%20happens%20next%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20**invalid**%20%7C%20a%20value%20the%20questionnaire%20does%20not%20allow%20%7C%20a%20sex%20of%208%3B%20an%20engine%20of%205%20%7C%20a%20rule%20(module%204)%20%7C%20edited%20by%20rule%2C%20or%20referred%20when%20no%20rule%20says%20what%20the%20value%20should%20be%20%7C%0A%20%20%20%20%7C%20**implausible**%20%7C%20allowed%2C%20but%20not%20believable%20with%20what%20else%20the%20record%20says%20%7C%20an%20outrigger%20boat%20of%2030%20meters%20%7C%20a%20limit%20from%20subject%20knowledge%2C%20or%20a%20statistic%20%7C%20referred%3A%20someone%20checks%20the%20questionnaire%20or%20calls%20the%20household%20back%20%7C%0A%20%20%20%20%7C%20**unusual**%20%7C%20believable%2C%20but%20rare%20%7C%20a%20household%20of%20twelve%3B%20a%20household%20of%20an%20area%20stratum%20that%20runs%20a%20large-scale%20unit%20%7C%20a%20statistic%20within%20groups%20%7C%20kept%2C%20and%20looked%20at%20when%20it%20moves%20an%20estimate%20%7C%0A%0A%20%20%20%20A%20limit%20for%20**implausible**%20comes%20from%20someone%20who%20knows%20the%20subject%3A%20a%20fisheries%20officer%20can%20say%20how%20long%20an%20outrigger%20boat%20can%20be.%20A%20limit%20for%20**unusual**%20can%20come%20from%20the%20same%20officer%20(%22they%20are%20rarely%20over%2011.5%20meters%22)%20or%20from%20the%20data%2C%20which%20is%20section%202.%20The%20order%20matters%20when%20you%20label%20a%20value.%20A%20boat%20of%20hull%207%20has%20no%20limits%20at%20all%2C%20so%20it%20must%20be%20labeled%20invalid%20before%20any%20length%20is%20compared%3B%20a%20boat%20with%20no%20length%20cannot%20be%20judged%20on%20its%20length.%0A%0A%20%20%20%20**In%20R**%20this%20is%20a%20%60case_when()%60%2C%20whose%20conditions%20are%20tried%20in%20order%2C%20the%20first%20true%20one%20winning%2C%20with%20%60TRUE%20~%20%22ok%22%60%20last.%20In%20Python%2C%20%60np.select(conditions%2C%20labels%2C%20default%3D%22ok%22)%60%20does%20the%20same%3A%20it%20takes%20the%20first%20condition%20that%20is%20true%20on%20each%20row.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20%23%20The%20course's%20reference%20limits%20for%20its%20boats%2C%20as%20a%20fisheries%20officer%20could%20give%20them%3A%20how%20long%20a%20boat%20of%20each%20hull%0A%20%20%20%20%23%20usually%20is%20at%20most%2C%20and%20how%20long%20it%20can%20be%20at%20most%0A%20%20%20%20limits%20%3D%20pd.DataFrame(%7B%0A%20%20%20%20%20%20%20%20%22boat_hull%22%3A%20%5B1%2C%202%2C%203%5D%2C%0A%20%20%20%20%20%20%20%20%22hull%22%3A%20%5B%22Dugout%20or%20plank%20canoe%22%2C%20%22Outrigger%20boat%22%2C%20%22Decked%20vessel%22%5D%2C%0A%20%20%20%20%20%20%20%20%22typical_max_m%22%3A%20%5B6.5%2C%2011.5%2C%2030.0%5D%2C%0A%20%20%20%20%20%20%20%20%22plausible_max_m%22%3A%20%5B10.0%2C%2017.0%2C%2045.0%5D%2C%0A%20%20%20%20%7D)%0A%20%20%20%20limits%0A%20%20%20%20return%20(limits%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%20Exercise%208.1.%20Invalid%2C%20implausible%20or%20unusual%3A%20one%20label%20per%20boat%0A%0A%20%20%20%20**Why%20it%20matters%3A**%20a%20reviewer%20acts%20differently%20on%20each%20kind.%20An%20invalid%20hull%20is%20edited%20by%20rule%20or%20referred%3B%20an%20implausible%20length%20is%20referred%20to%20someone%20who%20can%20call%20the%20household%20back%3B%20an%20unusual%20one%20is%20kept.%20A%20list%20of%20%22suspect%20boats%22%20with%20no%20label%20makes%20the%20reviewer%20work%20out%20the%20kind%20again%2C%20row%20by%20row.%0A%0A%20%20%20%20**Input%20%E2%86%92%20output%3A**%20the%20%60boats%60%20table%20(it%20needs%20%60boat_hull%60%20and%20%60length_m%60)%20and%20the%20%60limits%60%20table%20above%20%E2%86%92%20a%20Series%20with%20one%20label%20per%20boat%2C%20in%20the%20boats'%20order%2C%20the%20**first**%20of%20these%20that%20applies%3A%0A%0A%20%20%20%20%7C%20label%20%7C%20when%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20%60%22invalid%22%60%20%7C%20the%20boat's%20hull%20is%20not%20one%20of%20the%20hulls%20in%20%60limits%60%20%7C%0A%20%20%20%20%7C%20%60%22not%20measured%22%60%20%7C%20the%20length%20is%20missing%20(the%20size%20came%20from%20the%20papers)%20%7C%0A%20%20%20%20%7C%20%60%22implausible%22%60%20%7C%20the%20length%20is%20over%20the%20hull's%20%60plausible_max_m%60%20%7C%0A%20%20%20%20%7C%20%60%22unusual%22%60%20%7C%20the%20length%20is%20over%20the%20hull's%20%60typical_max_m%60%20%7C%0A%20%20%20%20%7C%20%60%22ok%22%60%20%7C%20none%20of%20the%20above%20%7C%0A%0A%20%20%20%20%7C%20boat_hull%20%7C%20length_m%20%7C%20label%20%7C%0A%20%20%20%20%7C--%3A%7C--%3A%7C%3A--%7C%0A%20%20%20%20%7C%207%20%7C%2030.0%20%7C%20invalid%20(not%20implausible%3A%20hull%207%20has%20no%20limits)%20%7C%0A%20%20%20%20%7C%202%20%7C%2030.0%20%7C%20implausible%20%7C%0A%20%20%20%20%7C%202%20%7C%2013.0%20%7C%20unusual%20%7C%0A%20%20%20%20%7C%201%20%7C%204.0%20%7C%20ok%20%7C%0A%0A%20%20%20%20**Tools%3A**%20%60Series.map()%60%20with%20a%20Series%20indexed%20by%20hull%20(module%205)%2C%20%60isin%60%2C%20%60isna%60%2C%20and%20%60np.select(conditions%2C%20labels%2C%20default%3D%22ok%22)%60%2C%20above.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20boat_label(boats%3A%20pd.DataFrame%2C%20limits%3A%20pd.DataFrame)%20-%3E%20pd.Series%3A%0A%20%20%20%20%22%22%22One%20label%20per%20boat%3A%20invalid%2C%20not%20measured%2C%20implausible%2C%20unusual%20or%20ok%2C%20the%20first%20that%20applies.%22%22%22%0A%20%20%20%20%23%20Exercise%208.1%3A%20the%20task%20and%20examples%20are%20in%20the%20text%20above%20this%20cell.%0A%20%20%20%20%23%20Step%201%3A%20each%20boat's%20typical_max_m%20and%20plausible_max_m%2C%20by%20its%20hull%3A%20map%20boat_hull%20through%20limits%20indexed%20by%20boat_hull%0A%20%20%20%20%23%20Step%202%3A%20the%20conditions%2C%20in%20order%3A%20hull%20not%20in%20limits%3B%20length%20missing%3B%20over%20plausible%3B%20over%20typical%0A%20%20%20%20%23%20Step%203%3A%20np.select(conditions%2C%20the%20four%20labels%2C%20default%3D%22ok%22)%2C%20returned%20as%20a%20Series%20with%20the%20boats'%20index%0A%20%20%20%20raise%20NotImplementedError(%22Exercise%208.1%22)%0A%0A%0A%40app.function%0Adef%20as_values(result%2C%20digits%3A%20int%20%3D%204)%20-%3E%20list%3A%0A%20%20%20%20%22%22%22A%20column's%20values%20in%20order%2C%20for%20the%20checks%3A%20numbers%20rounded%2C%20missing%20values%20as%20None%2C%20its%20name%20and%20index%20left%20out.%22%22%22%0A%20%20%20%20values%20%3D%20%5B%5D%0A%20%20%20%20for%20value%20in%20pd.Series(result).tolist()%3A%0A%20%20%20%20%20%20%20%20if%20isinstance(value%2C%20(bool%2C%20np.bool_))%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20values.append(bool(value))%0A%20%20%20%20%20%20%20%20elif%20value%20is%20None%20or%20(not%20isinstance(value%2C%20str)%20and%20pd.isna(value))%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20values.append(None)%0A%20%20%20%20%20%20%20%20elif%20isinstance(value%2C%20(int%2C%20float%2C%20np.number))%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20values.append(round(float(value)%2C%20digits))%0A%20%20%20%20%20%20%20%20else%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20values.append(value)%0A%20%20%20%20return%20values%0A%0A%0A%40app.function%0Adef%20as_rows(result%2C%20keys%2C%20digits%3A%20int%20%3D%204)%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22A%20result%20table%20in%20one%20shape%20for%20the%20checks%3A%20its%20keys%20as%20columns%2C%20rows%20sorted%20by%20them%2C%20columns%20in%20name%20order%2C%0A%20%20%20%20numbers%20rounded.%20So%20a%20correct%20answer%20passes%20whether%20its%20keys%20came%20back%20as%20the%20index%20or%20as%20columns.%22%22%22%0A%20%20%20%20table%20%3D%20result.to_frame()%20if%20isinstance(result%2C%20pd.Series)%20else%20result%0A%20%20%20%20keys%20%3D%20%5Bkeys%5D%20if%20isinstance(keys%2C%20str)%20else%20list(keys)%0A%20%20%20%20if%20any(name%20in%20keys%20for%20name%20in%20table.index.names)%3A%0A%20%20%20%20%20%20%20%20table%20%3D%20table.reset_index()%0A%20%20%20%20table%20%3D%20table.sort_values(keys%2C%20na_position%3D%22last%22).reset_index(drop%3DTrue)%5Bsorted(table.columns)%5D%0A%20%20%20%20numbers%20%3D%20table.select_dtypes(%22number%22).columns%0A%20%20%20%20table%5Bnumbers%5D%20%3D%20table%5Bnumbers%5D.astype(%22float64%22).round(digits)%0A%20%20%20%20return%20table%0A%0A%0A%40app.function%0Adef%20boats_fixture()%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22Eight%20boats%2C%20one%20of%20each%20case%20Exercise%208.1%20must%20tell%20apart%3A%20a%20hull%20with%20no%20limits%20(with%20a%20length%20and%20without)%2C%0A%20%20%20%20a%20missing%20length%2C%20lengths%20over%20each%20limit%2C%20and%20lengths%20within%20them.%22%22%22%0A%20%20%20%20return%20pd.DataFrame(%7B%0A%20%20%20%20%20%20%20%20%22region_code%22%3A%20%5B%2201%22%5D%20*%208%2C%0A%20%20%20%20%20%20%20%20%22hh_id%22%3A%20%5B1%2C%202%2C%203%2C%204%2C%205%2C%206%2C%207%2C%208%5D%2C%0A%20%20%20%20%20%20%20%20%22boat_hull%22%3A%20%5B7%2C%202%2C%202%2C%202%2C%201%2C%203%2C%201%2C%207%5D%2C%0A%20%20%20%20%20%20%20%20%22length_m%22%3A%20%5B30.0%2C%20None%2C%2030.0%2C%2013.0%2C%204.0%2C%2046.0%2C%206.0%2C%20None%5D%2C%0A%20%20%20%20%7D)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(boats%2C%20limits)%3A%0A%20%20%20%20check_cases(%0A%20%20%20%20%20%20%20%20%22Exercise%208.1%22%2C%20boat_label%2C%0A%20%20%20%20%20%20%20%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22eight%20boats%3A%20hull%207%20with%20and%20without%20a%20length%2C%20a%20missing%20length%2C%20and%20lengths%20over%20and%20within%20each%20limit%22%2C%20(boats_fixture()%2C%20limits))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22all%20the%20survey's%20boats%22%2C%20(boats%2C%20limits))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22the%20boats%20of%20region%2007%22%2C%20(boats%5Bboats%5B%22region_code%22%5D%20%3D%3D%20%2207%22%5D%2C%20limits))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22all%20the%20boats%2C%20with%20tighter%20limits%20for%20outrigger%20boats%22%2C%20(boats%2C%20limits.assign(typical_max_m%3D%5B6.5%2C%2010.0%2C%2030.0%5D%2C%20plausible_max_m%3D%5B10.0%2C%2014.0%2C%2045.0%5D)))%2C%0A%20%20%20%20%20%20%20%20%5D%2C%0A%20%20%20%20%20%20%20%20what%3D%22sets%20of%20boats%20and%20limits%22%2C%20summarize%3Das_values%2C%0A%20%20%20%20%20%20%20%20hint%3D%22Test%20the%20hull%20first%2C%20then%20the%20missing%20length%2C%20then%20plausible%2C%20then%20typical%3A%20np.select%20keeps%20the%20first%20condition%20that%20is%20true.%22%2C%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(limits)%3A%0A%20%20%20%20%23%20Try%20it%3A%20your%20labels%20for%20the%20eight%20boats%20of%20the%20check's%20first%20case%2C%20beside%20their%20hull%20and%20length%0A%20%20%20%20def%20_try_labels()%3A%0A%20%20%20%20%20%20%20%20_small%20%3D%20boats_fixture()%0A%20%20%20%20%20%20%20%20try%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20_labels%20%3D%20boat_label(_small%2C%20limits)%0A%20%20%20%20%20%20%20%20except%20NotImplementedError%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20mo.callout(mo.md(%22**Not%20written%20yet.**%20Write%20your%20code%2C%20delete%20the%20%60raise%60%20line%2C%20and%20run%20the%20function's%20cell.%22)%2C%20kind%3D%22neutral%22)%0A%20%20%20%20%20%20%20%20return%20_small.assign(label%3D_labels)%0A%0A%20%20%20%20_try_labels()%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.accordion(%7B%22If%20the%20check%20is%20red%3A%20common%20causes%20for%20Exercise%208.1%22%3A%20mo.md(r%22%22%22%0A%20%20%20%20%7C%20what%20you%20see%20%7C%20the%20usual%20cause%20%7C%20look%20at%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20The%20hull-7%20boat%20of%2030%20meters%20is%20%60ok%60%20%7C%20Its%20limits%20are%20missing%2C%20so%20every%20comparison%20is%20False%3B%20the%20hull%20was%20not%20tested%20first.%20%7C%20%60~boats%5B%22boat_hull%22%5D.isin(limits%5B%22boat_hull%22%5D)%60%20first%20%7C%0A%20%20%20%20%7C%20The%20boat%20with%20no%20length%20is%20%60ok%60%20%7C%20%60NaN%20%3E%20limit%60%20is%20False%3B%20the%20missing%20length%20was%20not%20tested%20before%20the%20limits.%20%7C%20%60boats%5B%22length_m%22%5D.isna()%60%20second%20%7C%0A%20%20%20%20%7C%20The%2030-meter%20outrigger%20boat%20is%20%60unusual%60%20%7C%20The%20typical%20limit%20was%20tested%20before%20the%20plausible%20one.%20%7C%20the%20order%20of%20the%20conditions%20%7C%0A%20%20%20%20%7C%20The%20hull-7%20boat%20with%20no%20length%20is%20%60not%20measured%60%20%7C%20The%20missing%20length%20was%20tested%20before%20the%20hull.%20%7C%20the%20hull%20first%20%7C%0A%20%20%20%20%7C%20*TypeError%3A%20unhashable%20type*%20or%20a%20%60KeyError%60%20in%20%60map%60%20%7C%20%60map%60%20was%20given%20the%20whole%20%60limits%60%20table%2C%20not%20one%20column%20indexed%20by%20hull.%20%7C%20%60limits.set_index(%22boat_hull%22)%5B%22typical_max_m%22%5D%60%20%7C%0A%0A%20%20%20%20Still%20stuck%20after%20ten%20minutes%3F%20Ask%20your%20table%2C%20then%20raise%20your%20hand.%0A%20%20%20%20%22%22%22)%7D)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(boats%2C%20limits)%3A%0A%20%20%20%20%23%20The%20survey's%20boats%20by%20label%20and%20hull%3A%20which%20kinds%20the%20limits%20find%2C%20and%20which%20defects%20they%20are%0A%20%20%20%20_labels%20%3D%20attempt(boat_label%2C%20boats%2C%20limits)%0A%20%20%20%20mo.stop(_labels%20is%20NOT_YET%2C%20waiting_for(%22Exercise%208.1%22))%0A%20%20%20%20_labeled%20%3D%20boats.assign(label%3D_labels)%0A%20%20%20%20_suspect%20%3D%20_labeled%5B_labeled%5B%22label%22%5D.isin(%5B%22invalid%22%2C%20%22implausible%22%2C%20%22unusual%22%5D)%5D.merge(planted_kinds%2C%20on%3DCASE%2C%20how%3D%22left%22)%0A%20%20%20%20_suspect%5B%22planted%22%5D%20%3D%20_suspect%5B%22planted%22%5D.fillna(%22(none%20planted)%22)%0A%20%20%20%20_unusual%20%3D%20_suspect%5B_suspect%5B%22label%22%5D%20%3D%3D%20%22unusual%22%5D%0A%20%20%20%20_no_engine%20%3D%20_unusual%5B_unusual%5B%22planted%22%5D.str.contains(%22no_engine_large_boat%22)%5D%0A%20%20%20%20_rest%20%3D%20_unusual.drop(_no_engine.index)%0A%20%20%20%20mo.vstack(%5B%0A%20%20%20%20%20%20%20%20pd.crosstab(_labeled%5B%22label%22%5D%2C%20_labeled%5B%22boat_hull%22%5D%2C%20margins%3DTrue%2C%20margins_name%3D%22all%22).rename(columns%3Dstr)%2C%0A%20%20%20%20%20%20%20%20mo.md(%22The%20boats%20labeled%20invalid%2C%20implausible%20or%20unusual%2C%20with%20the%20defects%20the%20course%20planted%20in%20their%20household%3A%22)%2C%0A%20%20%20%20%20%20%20%20_suspect%5B%5B*CASE%2C%20%22boat_hull%22%2C%20%22boat_engine%22%2C%20%22boat_measured_in%22%2C%20%22length_m%22%2C%20%22boat_tonnage%22%2C%20%22label%22%2C%20%22planted%22%5D%5D.round(2)%2C%0A%20%20%20%20%20%20%20%20mo.md(f%22%22%22%0A%20%20%20%20%20%20%20%20No%20boat%20is%20invalid%20by%20its%20hull%3A%20every%20hull%20is%201%2C%202%20or%203%2C%20and%20the%20survey's%20invalid%20boat%20codes%20are%20engines%2C%20which%20rule%20B1%20finds.%20The%20implausible%20boats%20are%20the%20feet%20recorded%20as%20meters%2C%20which%20no%20rule%20finds.%20Among%20the%20%7Blen(_unusual)%7D%20unusual%20boats%2C%20%7Blen(_no_engine)%7D%20are%20outrigger%20boats%20of%20%7B_no_engine%5B'length_m'%5D.min()%3A.1f%7D%20meters%20with%20no%20engine%2C%20planted%20for%20module%204's%20rule%20B2%2C%20which%20finds%20them%20by%20their%20tonnage.%20The%20other%20%7Blen(_rest)%7D%20are%20real%20canoes%2C%20measured%20in%20feet%20to%20the%20half%20foot%20and%20just%20over%20the%20typical%20limit%20once%20converted%20(%7B_rest%5B'length_m'%5D.max()%3A.2f%7D%20meters)%3B%20the%20defects%20planted%20in%20some%20of%20their%20households%20have%20nothing%20to%20do%20with%20the%20boat.%20A%20reviewer%20who%20reads%20such%20rows%20learns%20nothing%20about%20the%20boat%2C%20which%20is%20why%20the%20review%20table%20of%20section%205%20takes%20the%20implausible%20label%20and%20leaves%20the%20unusual%20one%20out.%0A%20%20%20%20%20%20%20%20%22%22%22)%2C%0A%20%20%20%20%5D)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%202%20%C2%B7%20Outliers%20within%20groups%3A%20IQR%2C%20robust%20z%2C%20Hidiroglou-Berthelot%0A%0A%20%20%20%20**The%20problem.**%20Limits%20like%20these%20exist%20for%20few%20items.%20Nobody%20has%20written%20down%20how%20many%20members%20a%20household%20can%20have%2C%20how%20many%20species%20one%20unit%20catches%2C%20or%20how%20far%20a%20final%20weight%20may%20stray%20from%20its%20design%20weight.%20The%20data%20says%20it%20instead%3A%20most%20values%20sit%20together%2C%20and%20a%20value%20far%20from%20the%20others%20is%20a%20candidate%20for%20review.%20Two%20cautions%20come%20with%20it.%0A%0A%20%20%20%20-%20**%22Far%22%20depends%20on%20the%20group.**%20A%2020-meter%20boat%20is%20ordinary%20among%20decked%20vessels%20and%20impossible%20among%20canoes.%20A%20threshold%20is%20computed%20within%20each%20group%3A%20the%20boat's%20hull%2C%20the%20household's%20stratum%2C%20the%20province.%0A%20%20%20%20-%20**The%20usual%20yardstick%20is%20pulled%20by%20what%20it%20should%20find.**%20A%20z-score%2C%20(x%20%E2%88%92%20mean)%20%2F%20standard%20deviation%2C%20measures%20distance%20in%20standard%20deviations%2C%20and%203%20is%20the%20usual%20cutoff.%20But%20the%20outlier%20moves%20the%20mean%20toward%20itself%20and%20inflates%20the%20standard%20deviation.%20In%20a%20group%20of%20n%20values%20no%20z-score%20can%20exceed%20(n%20%E2%88%92%201)%20%2F%20%E2%88%9An%2C%20which%20for%20ten%20values%20is%202.85%3A%20a%20cutoff%20of%203%20can%20never%20fire.%0A%0A%20%20%20%20Nine%20outrigger%20boats%20of%207%20to%209%20meters%20and%20one%20of%2030%20show%20it%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20%23%20Ten%20boats%2C%20one%20of%20them%2030%20meters%3A%20its%20z-score%20with%20the%20mean%20and%20standard%20deviation%2C%20and%20with%20the%20median%20and%20MAD%0A%20%20%20%20_lengths%20%3D%20pd.Series(%5B7.2%2C%207.5%2C%207.8%2C%208.0%2C%208.1%2C%208.3%2C%208.5%2C%208.8%2C%209.0%2C%2030.0%5D)%0A%20%20%20%20_z%20%3D%20(_lengths%20-%20_lengths.mean())%20%2F%20_lengths.std()%0A%20%20%20%20_mad%20%3D%20(_lengths%20-%20_lengths.median()).abs().median()%0A%20%20%20%20_robust%20%3D%20(_lengths%20-%20_lengths.median())%20%2F%20(1.4826%20*%20_mad)%0A%20%20%20%20mo.md(f%22%22%22%0A%20%20%20%20%7C%20%7C%20center%20%7C%20spread%20%7C%20the%2030-meter%20boat's%20distance%20%7C%0A%20%20%20%20%7C%3A--%7C--%3A%7C--%3A%7C--%3A%7C%0A%20%20%20%20%7C%20mean%20and%20standard%20deviation%20%7C%20%7B_lengths.mean()%3A.2f%7D%20%7C%20%7B_lengths.std()%3A.2f%7D%20%7C%20**z%20%3D%20%7B_z.iloc%5B-1%5D%3A.2f%7D**%3A%20under%203%2C%20not%20flagged%20%7C%0A%20%20%20%20%7C%20median%20and%20MAD%20%7C%20%7B_lengths.median()%3A.2f%7D%20%7C%20%7B1.4826%20*%20_mad%3A.2f%7D%20%7C%20**robust%20z%20%3D%20%7B_robust.iloc%5B-1%5D%3A.1f%7D**%20%7C%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%20Quartiles%3A%20Tukey's%20fences%0A%0A%20%20%20%20The%20**quartiles**%20split%20sorted%20values%20into%20four%20equal%20parts%3A%20Q1%20has%20a%20quarter%20of%20the%20values%20below%20it%2C%20Q3%20three%20quarters.%20The%20**interquartile%20range**%20IQR%20%3D%20Q3%20%E2%88%92%20Q1%20is%20the%20width%20of%20the%20middle%20half.%20Tukey's%20fences%20put%20a%20value%20outside%20when%20it%20is%20below%20Q1%20%E2%88%92%20k%20%C3%97%20IQR%20or%20above%20Q3%20%2B%20k%20%C3%97%20IQR%3A%20k%20%3D%201.5%20for%20%22outside%22%2C%20the%20whiskers%20of%20a%20box%20plot%2C%20and%20k%20%3D%203%20for%20%22far%20out%22.%20A%20few%20extreme%20values%20barely%20move%20the%20quartiles%2C%20so%20they%20cannot%20hide%20themselves.%0A%0A%20%20%20%20**In%20R**%2C%20%60boxplot.stats(x)%24out%60%20lists%20the%20values%20outside%20the%20k%20%3D%201.5%20fences%2C%20and%20within%20groups%20you%20write%20%60group_by(boat_hull)%20%7C%3E%20mutate(q1%20%3D%20quantile(length%2C%200.25)%2C%20q3%20%3D%20quantile(length%2C%200.75))%60.%20In%20pandas%20the%20group's%20quartile%20goes%20on%20every%20row%20with%20%60transform%60%2C%20as%20module%205%20put%20a%20group's%20total%20on%20every%20row%3A%0A%0A%20%20%20%20%60%60%60python%0A%20%20%20%20by_hull%20%3D%20boats.groupby(%22boat_hull%22)%5B%22length_m%22%5D%0A%20%20%20%20q1%20%3D%20by_hull.transform(lambda%20v%3A%20v.quantile(0.25))%20%20%20%23%20each%20boat's%20own%20hull's%20Q1%2C%20on%20its%20row%0A%20%20%20%20%60%60%60%0A%0A%20%20%20%20pandas'%20%60quantile%60%20interpolates%20between%20the%20two%20nearest%20values%20by%20default%2C%20which%20is%20R's%20default%20(type%207)%2C%20so%20the%20two%20give%20the%20same%20quartiles.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%20Exercise%208.2.%20Outside%20the%20fences%2C%20group%20by%20group%0A%0A%20%20%20%20**Why%20it%20matters%3A**%20this%20is%20the%20first%20statistical%20check%20most%20survey%20teams%20run%2C%20on%20any%20number%20in%20the%20survey%3A%20lengths%20by%20hull%2C%20household%20size%20by%20stratum%2C%20interview%20minutes%20by%20region.%20Written%20once%20for%20any%20column%20and%20any%20grouping%2C%20it%20is%20one%20line%20per%20check.%0A%0A%20%20%20%20**Input%20%E2%86%92%20output%3A**%20a%20Series%20of%20%60values%60%2C%20a%20Series%20of%20%60groups%60%20(the%20same%20length%20and%20index)%20and%20%60k%60%20%E2%86%92%20a%20Series%20of%20True%2FFalse%2C%20one%20per%20value%3A%20True%20where%20the%20value%20is%20below%20its%20group's%20Q1%20%E2%88%92%20k%20%C3%97%20IQR%20or%20above%20its%20group's%20Q3%20%2B%20k%20%C3%97%20IQR.%20A%20missing%20value%20is%20never%20flagged.%0A%0A%20%20%20%20%7C%20groups%20%7C%20values%20%7C%20k%20%3D%201.5%20%7C%0A%20%20%20%20%7C%3A--%7C--%3A%7C%3A--%7C%0A%20%20%20%20%7C%20A%20%7C%201%2C%202%2C%203%2C%204%2C%205%2C%206%2C%207%2C%208%2C%209%2C%20**30**%20%7C%20only%2030%20flagged%20%7C%0A%20%20%20%20%7C%20B%20%7C%2020%2C%2021%2C%2022%2C%2023%2C%2024%2C%2025%20%7C%20none%3A%20ordinary%20for%20B%2C%20though%20far%20above%20A%20%7C%0A%0A%20%20%20%20**Tools%3A**%20%60groupby(groups)%60%20on%20the%20values%2C%20%60transform%60%20with%20%60quantile%60%20(pandas'%20default%20interpolation)%2C%20comparisons%20and%20%60%7C%60%2C%20above.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20iqr_outliers(values%3A%20pd.Series%2C%20groups%3A%20pd.Series%2C%20k%3A%20float%20%3D%201.5)%20-%3E%20pd.Series%3A%0A%20%20%20%20%22%22%22True%20where%20a%20value%20is%20outside%20its%20group's%20fences%2C%20Q1%20-%20k*IQR%20and%20Q3%20%2B%20k*IQR.%22%22%22%0A%20%20%20%20%23%20Exercise%208.2%3A%20the%20task%20and%20examples%20are%20in%20the%20text%20above%20this%20cell.%0A%20%20%20%20%23%20Step%201%3A%20each%20value's%20group%20Q1%20and%20Q3%3A%20values.groupby(groups).transform(lambda%20v%3A%20v.quantile(...))%0A%20%20%20%20%23%20Step%202%3A%20the%20IQR%2C%20Q3%20-%20Q1%0A%20%20%20%20%23%20Step%203%3A%20return%20True%20where%20the%20value%20is%20below%20Q1%20-%20k%20*%20IQR%20or%20above%20Q3%20%2B%20k%20*%20IQR%0A%20%20%20%20raise%20NotImplementedError(%22Exercise%208.2%22)%0A%0A%0A%40app.function%0Adef%20groups_fixture()%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22Values%20in%20three%20groups%3A%20A%20with%20one%20value%20far%20above%20the%20rest%2C%20B%20with%20values%20that%20are%20high%20next%20to%20A's%20but%0A%20%20%20%20ordinary%20for%20B%2C%20C%20with%20one%20value%3B%20and%20a%20missing%20value%20in%20A.%22%22%22%0A%20%20%20%20return%20pd.DataFrame(%7B%0A%20%20%20%20%20%20%20%20%22group%22%3A%20%5B%22A%22%5D%20*%2011%20%2B%20%5B%22B%22%5D%20*%206%20%2B%20%5B%22C%22%5D%2C%0A%20%20%20%20%20%20%20%20%22value%22%3A%20%5B1%2C%202%2C%203%2C%204%2C%205%2C%206%2C%207%2C%208%2C%209%2C%2030%2C%20None%2C%2020%2C%2021%2C%2022%2C%2023%2C%2024%2C%2025%2C%205%5D%2C%0A%20%20%20%20%7D)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(boats)%3A%0A%20%20%20%20_small%20%3D%20groups_fixture()%0A%20%20%20%20check_cases(%0A%20%20%20%20%20%20%20%20%22Exercise%208.2%22%2C%20iqr_outliers%2C%0A%20%20%20%20%20%20%20%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22three%20small%20groups%2C%20k%20%3D%201.5%3A%20group%20B%20is%20high%20next%20to%20A%2C%20and%20A%20has%20a%20missing%20value%22%2C%20(_small%5B%22value%22%5D%2C%20_small%5B%22group%22%5D%2C%201.5))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22the%20same%20groups%2C%20k%20%3D%203%22%2C%20(_small%5B%22value%22%5D%2C%20_small%5B%22group%22%5D%2C%203.0))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22the%20survey's%20boat%20lengths%20by%20hull%2C%20k%20%3D%203%22%2C%20(boats%5B%22length_m%22%5D%2C%20boats%5B%22boat_hull%22%5D%2C%203.0))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22household%20size%20by%20stratum%2C%20k%20%3D%201.5%22%2C%20(households%5B%22hh_roster_members%22%5D.astype(%22float64%22)%2C%20households%5B%22sel_stratum%22%5D%2C%201.5))%2C%0A%20%20%20%20%20%20%20%20%5D%2C%0A%20%20%20%20%20%20%20%20what%3D%22sets%20of%20values%20and%20groups%22%2C%20summarize%3Das_values%2C%0A%20%20%20%20%20%20%20%20hint%3D%22Compute%20Q1%20and%20Q3%20within%20each%20group%20with%20transform%2C%20then%20flag%20values%20below%20Q1%20-%20k*IQR%20or%20above%20Q3%20%2B%20k*IQR.%22%2C%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(boats)%3A%0A%20%20%20%20%23%20Try%20it%3A%20the%20boats%20outside%20the%20far-out%20fences%20(k%20%3D%203)%20of%20their%20hull%0A%20%20%20%20def%20_try_fences()%3A%0A%20%20%20%20%20%20%20%20try%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20_outside%20%3D%20iqr_outliers(boats%5B%22length_m%22%5D%2C%20boats%5B%22boat_hull%22%5D%2C%203.0)%0A%20%20%20%20%20%20%20%20except%20NotImplementedError%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20mo.callout(mo.md(%22**Not%20written%20yet.**%20Write%20your%20code%2C%20delete%20the%20%60raise%60%20line%2C%20and%20run%20the%20function's%20cell.%22)%2C%20kind%3D%22neutral%22)%0A%20%20%20%20%20%20%20%20return%20boats%5B_outside%5D%5B%5B*CASE%2C%20%22boat_hull%22%2C%20%22length_m%22%2C%20%22beam_m%22%2C%20%22depth_m%22%2C%20%22boat_tonnage%22%5D%5D.round(2)%0A%0A%20%20%20%20_try_fences()%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.accordion(%7B%22If%20the%20check%20is%20red%3A%20common%20causes%20for%20Exercise%208.2%22%3A%20mo.md(r%22%22%22%0A%20%20%20%20%7C%20what%20you%20see%20%7C%20the%20usual%20cause%20%7C%20look%20at%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20Group%20B's%20high%20values%20are%20flagged%20%7C%20The%20quartiles%20are%20the%20whole%20column's%2C%20not%20each%20group's.%20%7C%20%60values.groupby(groups).transform(...)%60%20%7C%0A%20%20%20%20%7C%20A%20table%20of%20one%20row%20per%20group%2C%20or%20*ValueError%3A%20Can%20only%20compare%20identically-labeled%20Series*%20%7C%20%60.quantile()%60%20or%20%60.agg()%60%20gave%20one%20value%20per%20group%2C%20which%20does%20not%20line%20up%20with%20the%20values.%20%7C%20%60transform%60%2C%20one%20value%20per%20row%20%7C%0A%20%20%20%20%7C%20Too%20many%20or%20too%20few%20flags%20in%20the%20k%20%3D%203%20case%20%7C%20%60k%60%20is%20not%20used%2C%20or%20the%20fence%20is%20Q3%20%2B%20k%20(no%20IQR).%20%7C%20%60q3%20%2B%20k%20*%20iqr%60%20%7C%0A%20%20%20%20%7C%20Only%20high%20values%20flagged%20%7C%20The%20lower%20fence%20is%20missing.%20%7C%20%60(values%20%3C%20q1%20-%20k%20*%20iqr)%20%5C%7C%20(...)%60%20%7C%0A%20%20%20%20%7C%20*TypeError%3A%20unsupported%20operand%20type(s)%20for%20%5C%7C*%20%7C%20A%20comparison%20without%20brackets%3A%20%60%5C%7C%60%20binds%20before%20%60%3C%60.%20%7C%20brackets%20round%20each%20comparison%20%7C%0A%0A%20%20%20%20Still%20stuck%20after%20ten%20minutes%3F%20Ask%20your%20table%2C%20then%20raise%20your%20hand.%0A%20%20%20%20%22%22%22)%7D)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(boats)%3A%0A%20%20%20%20%23%20Every%20measured%20boat%20by%20hull%2C%20those%20outside%20its%20hull's%20far-out%20fences%20in%20orange%20(matplotlib)%3B%20the%20length%20axis%20is%0A%20%20%20%20%23%20logarithmic%0A%20%20%20%20_outside%20%3D%20attempt(iqr_outliers%2C%20boats%5B%22length_m%22%5D%2C%20boats%5B%22boat_hull%22%5D%2C%203.0)%0A%20%20%20%20mo.stop(_outside%20is%20NOT_YET%2C%20waiting_for(%22Exercise%208.2%22))%0A%20%20%20%20_measured%20%3D%20boats.assign(outside%3D_outside)%5Bboats%5B%22length_m%22%5D.notna()%5D%0A%20%20%20%20_jitter%20%3D%20np.random.default_rng(8).uniform(-0.25%2C%200.25%2C%20len(_measured))%20%20%23%20spread%20the%20dots%20sideways%3B%20a%20fixed%20seed%20draws%20the%20same%20chart%20each%20time%0A%20%20%20%20_x%20%3D%20_measured%5B%22boat_hull%22%5D.to_numpy()%20%2B%20_jitter%0A%20%20%20%20_fig%2C%20_ax%20%3D%20plt.subplots(figsize%3D(6.5%2C%203.4))%0A%20%20%20%20_ax.scatter(_x%5B~_measured%5B%22outside%22%5D%5D%2C%20_measured%5B%22length_m%22%5D%5B~_measured%5B%22outside%22%5D%5D%2C%20s%3D8%2C%20color%3D%22%234c78a8%22%2C%20alpha%3D0.45%2C%20label%3D%22within%20the%20fences%22)%0A%20%20%20%20_ax.scatter(_x%5B_measured%5B%22outside%22%5D%5D%2C%20_measured%5B%22length_m%22%5D%5B_measured%5B%22outside%22%5D%5D%2C%20s%3D26%2C%20color%3D%22%23e4572e%22%2C%20label%3D%22outside%20(k%20%3D%203)%22)%0A%20%20%20%20_ax.set_yscale(%22log%22)%0A%20%20%20%20_ax.set_xticks(%5B1%2C%202%2C%203%5D%2C%20%5B%221%20canoe%22%2C%20%222%20outrigger%20boat%22%2C%20%223%20decked%20vessel%22%5D)%0A%20%20%20%20_ax.set_xlabel(%22hull%20(boat_hull)%22)%0A%20%20%20%20_ax.set_ylabel(%22length%20(meters%2C%20log%20scale)%22)%0A%20%20%20%20_ax.set_title(%22Boat%20length%20by%20hull%2C%20far-out%20values%20marked%22)%0A%20%20%20%20_ax.legend(frameon%3DFalse%2C%20loc%3D%22upper%20left%22%2C%20bbox_to_anchor%3D(1%2C%201))%0A%20%20%20%20_ax.spines%5B%5B%22top%22%2C%20%22right%22%5D%5D.set_visible(False)%0A%20%20%20%20_fig.tight_layout()%0A%20%20%20%20_ax%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%20The%20robust%20z%3A%20median%20and%20MAD%0A%0A%20%20%20%20Quartiles%20say%20**whether**%20a%20value%20is%20outside%3B%20a%20reviewer%20also%20wants%20**how%20far**.%20The%20robust%20z%20measures%20it%20with%20statistics%20a%20few%20extreme%20values%20cannot%20move%3A%20the%20median%20for%20the%20center%2C%20and%20the%20**median%20absolute%20deviation**%20(MAD)%2C%20the%20median%20of%20each%20value's%20distance%20from%20the%20median%2C%20for%20the%20spread.%0A%0A%20%20%20%20robust%20z%20%3D%20(x%20%E2%88%92%20median)%20%2F%20(1.4826%20%C3%97%20MAD)%0A%0A%20%20%20%20For%20values%20from%20a%20normal%20distribution%2C%201.4826%20%C3%97%20MAD%20estimates%20the%20standard%20deviation%2C%20so%20a%20robust%20z%20reads%20on%20the%20scale%20of%20an%20ordinary%20z-score.%20A%20common%20cutoff%20is%20%7Crobust%20z%7C%20%3E%203.5.%0A%0A%20%20%20%20%7C%20%7C%20R%20%7C%20Python%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20MAD%2C%20scaled%20to%20estimate%20the%20standard%20deviation%20%7C%20%60mad(x)%60%20(the%201.4826%20is%20its%20default%20%60constant%60)%20%7C%20%60scipy.stats.median_abs_deviation(x%2C%20scale%3D%22normal%22)%60%2C%20or%20two%20lines%20of%20pandas%20%7C%0A%20%20%20%20%7C%20MAD%2C%20unscaled%20%7C%20%60mad(x%2C%20constant%20%3D%201)%60%20%7C%20%60median_abs_deviation(x)%60%20(its%20default%20%60scale%3D1.0%60)%20%7C%0A%0A%20%20%20%20The%20defaults%20differ%2C%20so%20a%20robust%20z%20computed%20in%20R%20and%20in%20scipy%20differ%20by%201.4826%20unless%20you%20say%20which%20you%20mean.%20pandas%20has%20no%20%60mad%60%3A%20its%20old%20%60Series.mad%60%20was%20the%20**mean**%20absolute%20deviation%2C%20and%20pandas%202.0%20removed%20it.%20Writing%20the%20robust%20z%20yourself%20avoids%20both%20traps.%0A%0A%20%20%20%20**When%20the%20MAD%20is%200.**%20If%20more%20than%20half%20of%20a%20group's%20values%20are%20equal%2C%20the%20median%20distance%20from%20the%20median%20is%200%2C%20and%20the%20robust%20z%20divides%20by%20zero.%20Counts%20are%20the%20usual%20case%3A%20most%20fishing%20units%20use%20one%20boat.%20Where%20the%20MAD%20is%200%20the%20robust%20z%20cannot%20measure%20distance%2C%20so%20it%20should%20say%20so%20(a%20missing%20value)%20rather%20than%20give%20infinity.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%20Exercise%208.3.%20The%20robust%20z%20within%20groups%0A%0A%20%20%20%20**Why%20it%20matters%3A**%20a%20distance%2C%20not%20just%20a%20flag%2C%20lets%20a%20reviewer%20read%20the%20most%20extreme%20values%20first%2C%20and%20the%20same%20function%20serves%20every%20numeric%20item%20of%20the%20survey.%20Section%203%20uses%20it%20on%20ratios.%0A%0A%20%20%20%20**Input%20%E2%86%92%20output%3A**%20a%20Series%20of%20%60values%60%20and%20a%20Series%20of%20%60groups%60%20%E2%86%92%20a%20Series%20of%20robust%20z%2C%20one%20per%20value%3A%20(x%20%E2%88%92%20the%20group's%20median)%20%2F%20(1.4826%20%C3%97%20the%20group's%20MAD).%20Where%20a%20group's%20MAD%20is%200%2C%20its%20robust%20z%20is%20**missing**%20(%60NaN%60)%2C%20not%20infinite.%0A%0A%20%20%20%20%7C%20groups%20%7C%20values%20%7C%20robust%20z%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20A%20%7C%201%2C%202%2C%203%2C%204%2C%20100%20%7C%20median%203%2C%20MAD%201%3A%20%E2%80%A6%2C%20100%20%E2%86%92%20(100%20%E2%88%92%203)%20%2F%201.4826%20%3D%2065.43%20%7C%0A%20%20%20%20%7C%20B%20%7C%205%2C%205%2C%205%2C%205%2C%209%20%7C%20MAD%200%3A%20all%20missing%20%7C%0A%0A%20%20%20%20**Tools%3A**%20%60groupby(...).transform(%22median%22)%60%20twice%20(the%20second%20time%20on%20the%20distances)%2C%20%60abs()%60%2C%20%60where%60%2C%20above.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20robust_z(values%3A%20pd.Series%2C%20groups%3A%20pd.Series)%20-%3E%20pd.Series%3A%0A%20%20%20%20%22%22%22(x%20-%20group%20median)%20%2F%20(1.4826%20*%20group%20MAD)%2C%20missing%20where%20the%20group's%20MAD%20is%200.%22%22%22%0A%20%20%20%20%23%20Exercise%208.3%3A%20the%20task%20and%20examples%20are%20in%20the%20text%20above%20this%20cell.%0A%20%20%20%20%23%20Step%201%3A%20each%20value's%20group%20median%20(transform(%22median%22))%0A%20%20%20%20%23%20Step%202%3A%20each%20value's%20group%20MAD%3A%20the%20median%2C%20within%20the%20group%2C%20of%20abs(value%20-%20median)%0A%20%20%20%20%23%20Step%203%3A%20(value%20-%20median)%20%2F%20(1.4826%20*%20MAD)%2C%20kept%20only%20where%20the%20MAD%20is%20over%200%20(.where)%2C%20and%20return%20it%0A%20%20%20%20raise%20NotImplementedError(%22Exercise%208.3%22)%0A%0A%0A%40app.function%0Adef%20spread_fixture()%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22Three%20small%20groups%3A%20A%20with%20one%20value%20far%20out%2C%20B%20with%20a%20MAD%20of%200%2C%20C%20with%20an%20even%20number%20of%20values.%22%22%22%0A%20%20%20%20return%20pd.DataFrame(%7B%0A%20%20%20%20%20%20%20%20%22group%22%3A%20%5B%22A%22%5D%20*%205%20%2B%20%5B%22B%22%5D%20*%205%20%2B%20%5B%22C%22%5D%20*%204%2C%0A%20%20%20%20%20%20%20%20%22value%22%3A%20%5B1.0%2C%202.0%2C%203.0%2C%204.0%2C%20100.0%2C%205.0%2C%205.0%2C%205.0%2C%205.0%2C%209.0%2C%2010.0%2C%2012.0%2C%2014.0%2C%2040.0%5D%2C%0A%20%20%20%20%7D)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(boats)%3A%0A%20%20%20%20_small%20%3D%20spread_fixture()%0A%20%20%20%20check_cases(%0A%20%20%20%20%20%20%20%20%22Exercise%208.3%22%2C%20robust_z%2C%0A%20%20%20%20%20%20%20%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22three%20small%20groups%2C%20one%20with%20a%20MAD%20of%200%22%2C%20(_small%5B%22value%22%5D%2C%20_small%5B%22group%22%5D))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22the%20survey's%20boat%20lengths%20by%20hull%22%2C%20(boats%5B%22length_m%22%5D%2C%20boats%5B%22boat_hull%22%5D))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22interview%20minutes%20by%20region%22%2C%20(households%5B%22hh_interview_minutes%22%5D.astype(%22float64%22)%2C%20households%5B%22region_code%22%5D))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22boats%20per%20fishing%20unit%20by%20scale%20(most%20units%20have%20one%20boat)%22%2C%20(units%5B%22unit_boats%22%5D.astype(%22float64%22)%2C%20units%5B%22unit_scale%22%5D))%2C%0A%20%20%20%20%20%20%20%20%5D%2C%0A%20%20%20%20%20%20%20%20what%3D%22sets%20of%20values%20and%20groups%22%2C%20summarize%3Das_values%2C%0A%20%20%20%20%20%20%20%20hint%3D%22Median%20and%20MAD%20within%20each%20group%20(transform%20twice)%2C%20divide%20by%201.4826%20times%20the%20MAD%2C%20and%20leave%20missing%20where%20the%20MAD%20is%200.%22%2C%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20%23%20Try%20it%3A%20the%20robust%20z%20of%20household%20size%20within%20each%20stratum%3B%20the%20largest%20households%20first%0A%20%20%20%20def%20_try_robust()%3A%0A%20%20%20%20%20%20%20%20try%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20_z%20%3D%20robust_z(households%5B%22hh_roster_members%22%5D.astype(%22float64%22)%2C%20households%5B%22sel_stratum%22%5D)%0A%20%20%20%20%20%20%20%20except%20NotImplementedError%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20mo.callout(mo.md(%22**Not%20written%20yet.**%20Write%20your%20code%2C%20delete%20the%20%60raise%60%20line%2C%20and%20run%20the%20function's%20cell.%22)%2C%20kind%3D%22neutral%22)%0A%20%20%20%20%20%20%20%20return%20households%5B%5B*CASE%2C%20%22sel_stratum%22%2C%20%22hh_roster_members%22%5D%5D.assign(robust_z%3D_z).sort_values(%22robust_z%22%2C%20ascending%3DFalse).head(8)%0A%0A%20%20%20%20_try_robust()%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.accordion(%7B%22If%20the%20check%20is%20red%3A%20common%20causes%20for%20Exercise%208.3%22%3A%20mo.md(r%22%22%22%0A%20%20%20%20%7C%20what%20you%20see%20%7C%20the%20usual%20cause%20%7C%20look%20at%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20%60inf%60%20or%20%60-inf%60%20in%20group%20B%20%7C%20The%20MAD%20of%200%20was%20not%20turned%20into%20a%20missing%20value.%20%7C%20%60.where(mad%20%3E%200)%60%20%7C%0A%20%20%20%20%7C%2097.0%20instead%20of%2065.43%20in%20group%20A%20%7C%20The%201.4826%20is%20missing.%20%7C%20%601.4826%20*%20mad%60%20%7C%0A%20%20%20%20%7C%20Close%2C%20but%20not%20equal%20%7C%20The%20mean%20or%20the%20standard%20deviation%20instead%20of%20the%20median%20or%20the%20MAD.%20%7C%20%60transform(%22median%22)%60%20both%20times%20%7C%0A%20%20%20%20%7C%20The%20same%20MAD%20for%20every%20group%20%7C%20The%20distances'%20median%20was%20taken%20over%20the%20whole%20column.%20%7C%20%60(values%20-%20median).abs().groupby(groups)%60%20%7C%0A%20%20%20%20%7C%20*ValueError%3A%20cannot%20reindex%20on%20an%20axis%20with%20duplicate%20labels*%20%7C%20%60groupby(...).median()%60%20gave%20one%20row%20per%20group%3B%20it%20was%20used%20as%20if%20it%20lined%20up%20with%20the%20values.%20%7C%20%60transform%60%20%7C%0A%0A%20%20%20%20Still%20stuck%20after%20ten%20minutes%3F%20Ask%20your%20table%2C%20then%20raise%20your%20hand.%0A%20%20%20%20%22%22%22)%7D)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20%23%20What%20the%20robust%20z%20finds%2C%20and%20where%20it%20cannot%20measure%3A%20three%20items%20of%20the%20survey%0A%20%20%20%20_size%20%3D%20attempt(robust_z%2C%20households%5B%22hh_roster_members%22%5D.astype(%22float64%22)%2C%20households%5B%22sel_stratum%22%5D)%0A%20%20%20%20mo.stop(_size%20is%20NOT_YET%2C%20waiting_for(%22Exercise%208.3%22))%0A%20%20%20%20_minutes%20%3D%20robust_z(households%5B%22hh_interview_minutes%22%5D.astype(%22float64%22)%2C%20households%5B%22region_code%22%5D)%0A%20%20%20%20_boats%20%3D%20robust_z(units%5B%22unit_boats%22%5D.astype(%22float64%22)%2C%20units%5B%22unit_scale%22%5D)%0A%20%20%20%20_short%20%3D%20households%5B%22hh_interview_minutes%22%5D%20%3C%2015%0A%20%20%20%20mo.md(f%22%22%22%0A%20%20%20%20%7C%20item%2C%20within%20%7C%20values%20%7C%20over%203.5%20%7C%20robust%20z%20missing%20(MAD%20of%200)%20%7C%0A%20%20%20%20%7C%3A--%7C--%3A%7C--%3A%7C--%3A%7C%0A%20%20%20%20%7C%20household%20size%2C%20stratum%20%7C%20%7B_size.size%3A%2C%7D%20%7C%20%7B(_size.abs()%20%3E%203.5).sum()%7D%20%7C%20%7B_size.isna().sum()%7D%20%7C%0A%20%20%20%20%7C%20interview%20minutes%2C%20region%20%7C%20%7B_minutes.size%3A%2C%7D%20%7C%20%7B(_minutes.abs()%20%3E%203.5).sum()%7D%20%7C%20%7B_minutes.isna().sum()%7D%20%7C%0A%20%20%20%20%7C%20boats%20per%20fishing%20unit%2C%20scale%20%7C%20%7Bunits%5B'unit_boats'%5D.notna().sum()%3A%2C%7D%20%7C%20%7B(_boats.abs()%20%3E%203.5).sum()%7D%20%7C%20%7B(_boats.isna()%20%26%20units%5B'unit_boats'%5D.notna()).sum()%3A%2C%7D%20%7C%0A%0A%20%20%20%20The%20largest%20households%20are%20unusual%2C%20not%20wrong.%20Boats%20per%20unit%20cannot%20be%20judged%20this%20way%3A%20most%20units%20have%20one%20boat%2C%20so%20the%20MAD%20is%200.%20And%20the%20shortest%20interviews%20are%20not%20outliers%20at%20all%3A%20the%20%7B_short.sum()%7D%20interviews%20under%2015%20minutes%20have%20robust%20z%20between%20%7B_minutes%5B_short%5D.min()%3A.2f%7D%20and%20%7B_minutes%5B_short%5D.max()%3A.2f%7D%3A%20inside%20the%20cutoff%20of%203.5%20this%20module%20uses%2C%20though%20a%20cutoff%20of%203%20would%20catch%20some%20of%20them.%20A%20short%20interview%20is%20not%20extreme%20on%20its%20own%3B%20section%204%20shows%20why%20it%20matters%20anyway.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%20Hidiroglou-Berthelot%3A%20a%20ratio%20of%20two%20values%0A%0A%20%20%20%20**The%20problem.**%20A%20household's%20final%20weight%20(%60sel_final_weight%60)%20is%20its%20design%20weight%20(%60sel_design_weight%60)%20adjusted%20twice%3A%20for%20the%20households%20of%20its%20area%20that%20did%20not%20respond%20(which%20gives%20%60sel_nr_weight%60)%2C%20and%20to%20match%20each%20province's%20listed%20fishing%20households%20by%20scale%20(calibration).%20Each%20adjustment%20is%20modest%2C%20so%20their%20product%2C%20the%20ratio%20of%20final%20to%20design%20weight%2C%20should%20be%20similar%20across%20a%20province's%20households.%20A%20household%20whose%20ratio%20is%20out%20of%20line%20has%20a%20weight%20error.%20Two%20problems%20stop%20a%20plain%20outlier%20check%20on%20the%20ratio%3A%0A%0A%20%20%20%20-%20**Ratios%20are%20lopsided.**%20Twice%20the%20median%20ratio%20is%201%20above%20it%3B%20half%20the%20median%20is%20only%200.5%20below.%20Fences%20on%20the%20ratio%20treat%20a%20weight%20doubled%20and%20a%20weight%20halved%20differently.%0A%20%20%20%20-%20**Size%20matters.**%20A%20household%20that%20stands%20for%2015%20households%20moves%20an%20estimate%20fifteen%20times%20as%20much%20as%20one%20that%20stands%20for%201.%20The%20same%20odd%20ratio%20deserves%20more%20attention%20on%20the%20larger%20weight.%0A%0A%20%20%20%20Hidiroglou%20and%20Berthelot%20(1986%2C%20*Survey%20Methodology*%2012(1))%20solved%20both%20for%20business%20surveys%2C%20comparing%20each%20firm's%20value%20this%20round%20with%20last%20round's.%20Statistics%20Canada's%20Banff%20system%20for%20edit%20and%20imputation%20uses%20their%20method%20(Banff%20Functional%20Description%2C%202017%2C%20chapter%204).%20With%20x%20and%20y%20the%20two%20values%20of%20a%20unit%2C%20and%20the%20median%20taken%20within%20its%20group%3A%0A%0A%20%20%20%20%7C%20step%20%7C%20formula%20%7C%20what%20it%20fixes%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20ratio%20%7C%20r%20%3D%20x%20%2F%20y%20%7C%20compares%20the%20two%20values%20%7C%0A%20%20%20%20%7C%20centering%20%7C%20s%20%3D%201%20%E2%88%92%20r_M%20%2F%20r%20when%20r%20%3C%20r_M%3B%20s%20%3D%20r%20%2F%20r_M%20%E2%88%92%201%20otherwise%20(r_M%20the%20group's%20median%20ratio)%20%7C%20half%20the%20median%20and%20double%20it%20are%20equally%20far%3A%20%E2%88%921%20and%20%2B1%20%7C%0A%20%20%20%20%7C%20size%20%7C%20e%20%3D%20s%20%C3%97%20max(x%2C%20y)%5EU%2C%20with%200%20%E2%89%A4%20U%20%E2%89%A4%201%20%7C%20U%20%3D%200%20ignores%20size%3B%20U%20%3D%201%20gives%20large%20units%20their%20full%20weight%20%7C%0A%20%20%20%20%7C%20fences%20%7C%20E_M%20%E2%88%92%20C%20%C3%97%20d_Q1%20and%20E_M%20%2B%20C%20%C3%97%20d_Q3%2C%20where%20d_Q1%20%3D%20max(E_M%20%E2%88%92%20E_Q1%2C%20%5C%7CA%20%C3%97%20E_M%5C%7C)%20and%20d_Q3%20%3D%20max(E_Q3%20%E2%88%92%20E_M%2C%20%5C%7CA%20%C3%97%20E_M%5C%7C)%20%7C%20each%20side's%20own%20quartile%20distance%20from%20the%20median%3B%20the%20minimum%20%5C%7CA%20%C3%97%20E_M%5C%7C%20stops%20a%20group%20of%20near-equal%20effects%20from%20flagging%20every%20small%20difference%20%7C%0A%0A%20%20%20%20E_Q1%2C%20E_M%20and%20E_Q3%20are%20the%20quartiles%20and%20median%20of%20the%20effects%20e%20in%20the%20group.%20Banff%20calls%20U%20the%20exponent%2C%20A%20the%20minimum%20distance%20multiplier%2C%20and%20has%20two%20values%20of%20C%3A%20one%20for%20values%20extreme%20enough%20to%20impute%2C%20one%20for%20values%20unusual%20enough%20to%20keep%20out%20of%20estimates.%20It%20recommends%20at%20least%20ten%20units%20in%20a%20group.%0A%0A%20%20%20%20**Which%20two%20values.**%20HB%20was%20built%20for%20two%20rounds%3A%20x%20this%20round%2C%20y%20the%20last.%20This%20survey%20has%20no%20earlier%20round%20for%20the%20same%20households%3A%20%60units_2021_edit_decisions.parquet%60%20holds%20the%202021%20round's%20fishing%20units%20under%20ids%20of%20their%20own%2C%20so%20no%20household%20can%20be%20matched%20to%20itself.%20This%20module%20takes%20the%20two%20values%20from%20one%20household%20at%20two%20stages%20instead%3A%20x%20the%20final%20weight%2C%20y%20the%20design%20weight.%20They%20are%20one%20item%20in%20one%20unit%2C%20so%20max(x%2C%20y)%20still%20measures%20how%20many%20households%20a%20household%20stands%20for%2C%20as%20HB's%20size%20term%20intends.%20The%20group%20is%20the%20province%2C%20whose%20areas'%20adjustments%20should%20resemble%20each%20other%20across%20strata.%0A%0A%20%20%20%20**A%20library%3F**%20The%20Banff%20procedure%20guide%20documents%20%60banff.outlier()%60%20as%20a%20Python%20function.%20The%20package%20named%20%60banff%60%20on%20PyPI%20(release%203.1.3%2C%20read%20in%20November%202026)%20requires%20pandas%20below%203%20and%20DuckDB%20below%202%2C%20so%20it%20cannot%20share%20this%20course's%20environment.%20The%20method%20is%20a%20few%20lines%20of%20pandas%2C%20written%20in%20the%20next%20two%20exercises.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%20Exercise%208.4.%20HB's%20effects%3A%20centered%20ratios%20weighted%20by%20size%0A%0A%20%20%20%20**Why%20it%20matters%3A**%20the%20centering%20and%20the%20size%20term%20are%20what%20make%20HB%20more%20than%20an%20outlier%20check%20on%20a%20ratio.%20Get%20them%20right%20and%20Exercise%208.5's%20fences%20do%20the%20rest.%0A%0A%20%20%20%20**Input%20%E2%86%92%20output%3A**%20two%20Series%20%60x%60%20and%20%60y%60%20(positive%20numbers%2C%20the%20same%20index)%2C%20a%20Series%20of%20%60groups%60%20and%20the%20%60exponent%60%20U%20%E2%86%92%20a%20Series%20of%20effects%20e%2C%20one%20per%20unit%2C%20as%20the%20table%20above%20defines%20them%3A%0A%0A%20%20%20%20%7C%20group%20%7C%20x%20%7C%20y%20%7C%20ratio%20%7C%20group's%20median%20ratio%20%7C%20s%20%7C%20U%20%3D%200.5%3A%20e%20%3D%20s%20%C3%97%20max(x%2C%20y)%5E0.5%20%7C%0A%20%20%20%20%7C%3A--%7C--%3A%7C--%3A%7C--%3A%7C--%3A%7C--%3A%7C--%3A%7C%0A%20%20%20%20%7C%20A%20%7C%205%20%7C%2010%20%7C%200.5%20%7C%201%20%7C%201%20%E2%88%92%201%20%2F%200.5%20%3D%20**%E2%88%921**%20%7C%20%E2%88%921%20%C3%97%20%E2%88%9A10%20%3D%20%E2%88%923.162%20%7C%0A%20%20%20%20%7C%20A%20%7C%2020%20%7C%2010%20%7C%202%20%7C%201%20%7C%202%20%2F%201%20%E2%88%92%201%20%3D%20**1**%20%7C%201%20%C3%97%20%E2%88%9A20%20%3D%204.472%20%7C%0A%20%20%20%20%7C%20B%20%7C%2040%20%7C%204%20%7C%2010%20%7C%201%20%7C%2010%20%2F%201%20%E2%88%92%201%20%3D%20**9**%20%7C%209%20%C3%97%20%E2%88%9A40%20%3D%2056.921%20%7C%0A%0A%20%20%20%20**Tools%3A**%20%60transform(%22median%22)%60%20within%20groups%2C%20%60where%60%20(or%20%60np.where%60)%20for%20the%20two%20cases%20of%20s%2C%20%60np.maximum(x%2C%20y)%60%20and%20%60**%60.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20hb_effects(x%3A%20pd.Series%2C%20y%3A%20pd.Series%2C%20groups%3A%20pd.Series%2C%20exponent%3A%20float)%20-%3E%20pd.Series%3A%0A%20%20%20%20%22%22%22Hidiroglou-Berthelot's%20effects%3A%20each%20ratio%20x%2Fy%20centered%20on%20its%20group's%20median%20ratio%2C%20times%20max(x%2C%20y)**exponent.%22%22%22%0A%20%20%20%20%23%20Exercise%208.4%3A%20the%20task%20and%20examples%20are%20in%20the%20text%20above%20this%20cell.%0A%20%20%20%20%23%20Step%201%3A%20the%20ratio%20x%20%2F%20y%2C%20and%20each%20unit's%20group%20median%20ratio%20(transform(%22median%22))%0A%20%20%20%20%23%20Step%202%3A%20s%20%3D%201%20-%20median%20%2F%20ratio%20where%20the%20ratio%20is%20below%20the%20median%2C%20ratio%20%2F%20median%20-%201%20elsewhere%0A%20%20%20%20%23%20Step%203%3A%20return%20s%20times%20the%20larger%20of%20x%20and%20y%20(np.maximum)%20to%20the%20power%20exponent%0A%20%20%20%20raise%20NotImplementedError(%22Exercise%208.4%22)%0A%0A%0A%40app.function%0Adef%20ratio_fixture()%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22Two%20groups%20of%20units%20with%20two%20values%20each%3A%20in%20A%20one%20ratio%20is%20half%20the%20median%20and%20one%20is%20double%3B%20in%20B%20one%20unit's%0A%20%20%20%20ratio%20is%20ten%20times%20the%20median%2C%20and%20one%20unit's%20smaller%20value%20is%20the%20first.%22%22%22%0A%20%20%20%20return%20pd.DataFrame(%7B%0A%20%20%20%20%20%20%20%20%22group%22%3A%20%5B%22A%22%5D%20*%205%20%2B%20%5B%22B%22%5D%20*%203%2C%0A%20%20%20%20%20%20%20%20%22x%22%3A%20%5B10.0%2C%2010.0%2C%205.0%2C%2020.0%2C%2010.0%2C%201.0%2C%202.0%2C%2040.0%5D%2C%0A%20%20%20%20%20%20%20%20%22y%22%3A%20%5B10.0%2C%2010.0%2C%2010.0%2C%2010.0%2C%2010.0%2C%201.0%2C%202.0%2C%204.0%5D%2C%0A%20%20%20%20%7D)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20_small%20%3D%20ratio_fixture()%0A%20%20%20%20check_cases(%0A%20%20%20%20%20%20%20%20%22Exercise%208.4%22%2C%20hb_effects%2C%0A%20%20%20%20%20%20%20%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22two%20small%20groups%2C%20exponent%200.5%3A%20half%20the%20median%2C%20double%20it%2C%20and%20ten%20times%20it%22%2C%20(_small%5B%22x%22%5D%2C%20_small%5B%22y%22%5D%2C%20_small%5B%22group%22%5D%2C%200.5))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22the%20same%20groups%2C%20exponent%200%3A%20the%20centered%20ratios%20alone%22%2C%20(_small%5B%22x%22%5D%2C%20_small%5B%22y%22%5D%2C%20_small%5B%22group%22%5D%2C%200.0))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22the%20final%20against%20the%20design%20weight%20by%20province%2C%20exponent%200.5%22%2C%20(households%5B%22sel_final_weight%22%5D%2C%20households%5B%22sel_design_weight%22%5D%2C%20PROVINCE%2C%200.5))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22the%20same%20weights%2C%20exponent%201%22%2C%20(households%5B%22sel_final_weight%22%5D%2C%20households%5B%22sel_design_weight%22%5D%2C%20PROVINCE%2C%201.0))%2C%0A%20%20%20%20%20%20%20%20%5D%2C%0A%20%20%20%20%20%20%20%20what%3D%22sets%20of%20values%2C%20groups%20and%20exponents%22%2C%20summarize%3Das_values%2C%0A%20%20%20%20%20%20%20%20hint%3D%22Center%20each%20ratio%20on%20its%20group's%20median%20ratio%20(1%20-%20median%2Fratio%20below%20it%2C%20ratio%2Fmedian%20-%201%20above)%2C%20then%20multiply%20by%20max(x%2C%20y)%20to%20the%20exponent.%22%2C%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20%23%20Try%20it%3A%20the%20households%20with%20the%20largest%20effects%2C%20final%20weight%20against%20design%20weight%20by%20province%20(exponent%200.5)%0A%20%20%20%20def%20_try_effects()%3A%0A%20%20%20%20%20%20%20%20try%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20_e%20%3D%20hb_effects(households%5B%22sel_final_weight%22%5D%2C%20households%5B%22sel_design_weight%22%5D%2C%20PROVINCE%2C%200.5)%0A%20%20%20%20%20%20%20%20except%20NotImplementedError%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20mo.callout(mo.md(%22**Not%20written%20yet.**%20Write%20your%20code%2C%20delete%20the%20%60raise%60%20line%2C%20and%20run%20the%20function's%20cell.%22)%2C%20kind%3D%22neutral%22)%0A%20%20%20%20%20%20%20%20_shown%20%3D%20households%5B%5B*CASE%2C%20%22province_code%22%2C%20%22sel_stratum%22%2C%20%22sel_design_weight%22%2C%20%22sel_final_weight%22%5D%5D.assign(effect%3D_e)%0A%20%20%20%20%20%20%20%20return%20_shown.reindex(_e.abs().sort_values(ascending%3DFalse).index).head(8).round(3)%0A%0A%20%20%20%20_try_effects()%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.accordion(%7B%22If%20the%20check%20is%20red%3A%20common%20causes%20for%20Exercise%208.4%22%3A%20mo.md(r%22%22%22%0A%20%20%20%20%7C%20what%20you%20see%20%7C%20the%20usual%20cause%20%7C%20look%20at%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20%E2%88%920.5%20where%20the%20ratio%20is%20half%20the%20median%20(exponent%200)%20%7C%20%60ratio%20%2F%20median%20-%201%60%20on%20both%20sides.%20%7C%20%601%20-%20median%20%2F%20ratio%60%20below%20the%20median%20%7C%0A%20%20%20%20%7C%200.5%20where%20the%20ratio%20is%20half%20the%20median%20%7C%20The%20two%20cases%20are%20swapped.%20%7C%20%60where(ratio%20%3C%20median%2C%20...)%60%3A%20the%20first%20argument%20is%20kept%20where%20the%20condition%20is%20True%20%7C%0A%20%20%20%20%7C%20Effects%20match%20with%20exponent%200%20but%20not%200.5%20%7C%20The%20size%20is%20%60x%60%20or%20%60y%60%2C%20not%20the%20larger%20of%20the%20two.%20%7C%20%60np.maximum(x%2C%20y)%20**%20exponent%60%20%7C%0A%20%20%20%20%7C%20Every%20ratio%20centered%20on%201%2C%20or%20on%20the%20whole%20column's%20median%20%7C%20The%20median%20is%20not%20the%20group's.%20%7C%20%60ratio.groupby(groups).transform(%22median%22)%60%20%7C%0A%20%20%20%20%7C%20*ValueError%3A%20The%20truth%20value%20of%20a%20Series%20is%20ambiguous*%20%7C%20%60if%20ratio%20%3C%20median%3A%60%20on%20a%20whole%20column.%20%7C%20%60where%60%20or%20%60np.where%60%20%7C%0A%0A%20%20%20%20Still%20stuck%20after%20ten%20minutes%3F%20Ask%20your%20table%2C%20then%20raise%20your%20hand.%0A%20%20%20%20%22%22%22)%7D)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%20Exercise%208.5.%20HB's%20fences%3A%20quartile%20distances%20with%20a%20minimum%0A%0A%20%20%20%20**Why%20it%20matters%3A**%20the%20fences%20decide%20which%20effects%20a%20reviewer%20sees.%20Each%20side%20takes%20its%20own%20distance%20from%20the%20median%2C%20so%20a%20group%20whose%20effects%20spread%20more%20on%20one%20side%20is%20not%20judged%20as%20if%20it%20were%20symmetric%3B%20the%20minimum%20keeps%20a%20group%20of%20nearly%20identical%20effects%20from%20flagging%20every%20small%20difference.%0A%0A%20%20%20%20**Input%20%E2%86%92%20output%3A**%20a%20Series%20of%20%60effects%60%2C%20a%20Series%20of%20%60groups%60%2C%20the%20multiplier%20%60c%60%20and%20the%20minimum%20distance%20multiplier%20%60a%60%20%E2%86%92%20a%20Series%20of%20True%2FFalse%3A%20True%20where%20the%20effect%20is%20below%20E_M%20%E2%88%92%20c%20%C3%97%20d_Q1%20or%20above%20E_M%20%2B%20c%20%C3%97%20d_Q3%2C%20with%20d_Q1%20%3D%20max(E_M%20%E2%88%92%20E_Q1%2C%20%7Ca%20%C3%97%20E_M%7C)%20and%20d_Q3%20%3D%20max(E_Q3%20%E2%88%92%20E_M%2C%20%7Ca%20%C3%97%20E_M%7C)%2C%20all%20within%20the%20group.%0A%0A%20%20%20%20%7C%20group%20%7C%20effects%20%7C%20E_Q1%20%3D%20E_M%20%3D%20E_Q3%20%7C%20d%20with%20a%20%3D%200.05%20%7C%20c%20%3D%204%3A%20fences%20%7C%20flagged%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C--%3A%7C--%3A%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20A%20%7C%2010%20seven%20times%2C%2010.4%2C%2013%20%7C%2010%20%7C%200.5%20%7C%208%20and%2012%20%7C%20only%2013%20(with%20no%20minimum%2C%2010.4%20too)%20%7C%0A%0A%20%20%20%20The%20function%20works%20on%20any%20numbers%2C%20not%20only%20HB's%20effects%2C%20so%20the%20check%20also%20gives%20it%20other%20columns.%0A%0A%20%20%20%20**Tools%3A**%20%60transform%60%20with%20%60quantile%60%20and%20%60%22median%22%60%2C%20%60np.maximum%60%2C%20%60abs()%60%2C%20comparisons%2C%20above.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20hb_outliers(effects%3A%20pd.Series%2C%20groups%3A%20pd.Series%2C%20c%3A%20float%2C%20a%3A%20float%20%3D%200.05)%20-%3E%20pd.Series%3A%0A%20%20%20%20%22%22%22True%20where%20an%20effect%20is%20outside%20its%20group's%20HB%20fences%2C%20E_M%20-%20c*d_Q1%20and%20E_M%20%2B%20c*d_Q3.%22%22%22%0A%20%20%20%20%23%20Exercise%208.5%3A%20the%20task%20and%20examples%20are%20in%20the%20text%20above%20this%20cell.%0A%20%20%20%20%23%20Step%201%3A%20each%20effect's%20group%20E_Q1%2C%20E_M%20and%20E_Q3%20(transform%20with%20quantile%2C%20and%20%22median%22)%0A%20%20%20%20%23%20Step%202%3A%20d_Q1%20%3D%20the%20larger%20of%20E_M%20-%20E_Q1%20and%20abs(a%20*%20E_M)%3B%20d_Q3%20%3D%20the%20larger%20of%20E_Q3%20-%20E_M%20and%20abs(a%20*%20E_M)%0A%20%20%20%20%23%20Step%203%3A%20return%20True%20where%20the%20effect%20is%20below%20E_M%20-%20c%20*%20d_Q1%20or%20above%20E_M%20%2B%20c%20*%20d_Q3%0A%20%20%20%20raise%20NotImplementedError(%22Exercise%208.5%22)%0A%0A%0A%40app.function%0Adef%20effects_fixture()%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22Effects%20in%20three%20groups%3A%20A%20whose%20quartiles%20equal%20its%20median%2C%20so%20only%20the%20minimum%20distance%20gives%20the%20fences%20width%3B%0A%20%20%20%20B%20spread%20more%20above%20the%20median%20than%20below%3B%20C%20with%20a%20negative%20median.%22%22%22%0A%20%20%20%20return%20pd.DataFrame(%7B%0A%20%20%20%20%20%20%20%20%22group%22%3A%20%5B%22A%22%5D%20*%209%20%2B%20%5B%22B%22%5D%20*%208%20%2B%20%5B%22C%22%5D%20*%209%2C%0A%20%20%20%20%20%20%20%20%22effect%22%3A%20%5B10.0%2C%2010.0%2C%2010.0%2C%2010.0%2C%2010.0%2C%2010.0%2C%2010.0%2C%2010.4%2C%2013.0%2C%20-0.3%2C%20-0.2%2C%20-0.1%2C%200.0%2C%200.1%2C%200.5%2C%201.0%2C%202.5%2C%20-10.0%2C%20-10.0%2C%20-10.0%2C%20-10.0%2C%20-10.0%2C%20-10.0%2C%20-10.0%2C%20-10.4%2C%20-12.5%5D%2C%0A%20%20%20%20%7D)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(boats)%3A%0A%20%20%20%20_small%20%3D%20effects_fixture()%0A%20%20%20%20check_cases(%0A%20%20%20%20%20%20%20%20%22Exercise%208.5%22%2C%20hb_outliers%2C%0A%20%20%20%20%20%20%20%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22three%20small%20groups%2C%20c%20%3D%204%2C%20a%20%3D%200.05%3A%20quartiles%20equal%20to%20the%20median%2C%20a%20lopsided%20group%2C%20a%20negative%20median%22%2C%20(_small%5B%22effect%22%5D%2C%20_small%5B%22group%22%5D%2C%204.0%2C%200.05))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22the%20same%20groups%2C%20c%20%3D%204%2C%20with%20no%20minimum%20(a%20%3D%200)%22%2C%20(_small%5B%22effect%22%5D%2C%20_small%5B%22group%22%5D%2C%204.0%2C%200.0))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22interview%20minutes%20by%20region%2C%20c%20%3D%202%22%2C%20(households%5B%22hh_interview_minutes%22%5D.astype(%22float64%22)%2C%20households%5B%22region_code%22%5D%2C%202.0%2C%200.05))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22boat%20lengths%20by%20hull%2C%20c%20%3D%206%22%2C%20(boats%5B%22length_m%22%5D%2C%20boats%5B%22boat_hull%22%5D%2C%206.0%2C%200.05))%2C%0A%20%20%20%20%20%20%20%20%5D%2C%0A%20%20%20%20%20%20%20%20what%3D%22sets%20of%20effects%20and%20groups%22%2C%20summarize%3Das_values%2C%0A%20%20%20%20%20%20%20%20hint%3D%22Within%20each%20group%3A%20d_Q1%20%3D%20max(E_M%20-%20E_Q1%2C%20%7Ca*E_M%7C)%2C%20d_Q3%20%3D%20max(E_Q3%20-%20E_M%2C%20%7Ca*E_M%7C)%3B%20flag%20below%20E_M%20-%20c*d_Q1%20or%20above%20E_M%20%2B%20c*d_Q3.%22%2C%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20%23%20Try%20it%3A%20your%20fences%20on%20the%20check's%20first%20small%20groups%2C%20beside%20the%20effects%0A%20%20%20%20def%20_try_hb_fences()%3A%0A%20%20%20%20%20%20%20%20_small%20%3D%20effects_fixture()%0A%20%20%20%20%20%20%20%20try%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20_flags%20%3D%20hb_outliers(_small%5B%22effect%22%5D%2C%20_small%5B%22group%22%5D%2C%204.0%2C%200.05)%0A%20%20%20%20%20%20%20%20except%20NotImplementedError%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20mo.callout(mo.md(%22**Not%20written%20yet.**%20Write%20your%20code%2C%20delete%20the%20%60raise%60%20line%2C%20and%20run%20the%20function's%20cell.%22)%2C%20kind%3D%22neutral%22)%0A%20%20%20%20%20%20%20%20return%20_small.assign(flagged%3D_flags)%0A%0A%20%20%20%20_try_hb_fences()%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.accordion(%7B%22If%20the%20check%20is%20red%3A%20common%20causes%20for%20Exercise%208.5%22%3A%20mo.md(r%22%22%22%0A%20%20%20%20%7C%20what%20you%20see%20%7C%20the%20usual%20cause%20%7C%20look%20at%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%2010.4%20flagged%20in%20group%20A%20%7C%20The%20minimum%20distance%20is%20missing%3A%20A's%20quartiles%20equal%20its%20median%2C%20so%20its%20fences%20have%20no%20width.%20%7C%20%60np.maximum(median%20-%20q1%2C%20(a%20*%20median).abs())%60%20%7C%0A%20%20%20%20%7C%20Group%20C%20flags%20-10.4%20%7C%20%60a%20*%20median%60%20is%20negative%20there%20and%20was%20used%20without%20%60abs()%60%2C%20so%20the%20minimum%20is%20lost.%20%7C%20%60(a%20*%20median).abs()%60%20%7C%0A%20%20%20%20%7C%20Group%20B's%20flags%20differ%20from%20the%20check%20%7C%20One%20distance%20(the%20IQR)%20used%20on%20both%20sides.%20%7C%20%60median%20-%20q1%60%20below%2C%20%60q3%20-%20median%60%20above%20%7C%0A%20%20%20%20%7C%20Fences%20around%20the%20whole%20column%2C%20not%20each%20group%20%7C%20The%20quartiles%20were%20not%20computed%20within%20groups.%20%7C%20%60effects.groupby(groups).transform(...)%60%20%7C%0A%20%20%20%20%7C%20*TypeError%3A%20'%3E'%20not%20supported*%20or%20*ValueError%3A%20ambiguous*%20%7C%20Python's%20%60max()%60%20on%20two%20Series.%20%7C%20%60np.maximum%60%20works%20element%20by%20element%20%7C%0A%0A%20%20%20%20Still%20stuck%20after%20ten%20minutes%3F%20Ask%20your%20table%2C%20then%20raise%20your%20hand.%0A%20%20%20%20%22%22%22)%7D)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%20HB%20on%20the%20survey's%20weights%0A%0A%20%20%20%20Set%20the%20exponent%20U%20and%20the%20multiplier%20C.%20The%20chart%20puts%20each%20household's%20effect%20against%20its%20final%20weight%3B%20the%20flagged%20ones%20are%20orange%2C%20and%20the%20table%20says%20which%20defects%20the%20course%20planted%20in%20each.%20Watch%20U%3A%20at%201%2C%20a%20small%20difference%20in%20a%20household%20that%20stands%20for%20fifteen%20households%20is%20flagged%3B%20at%200%2C%20only%20the%20ratio%20counts.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20hb_exponent%20%3D%20mo.ui.slider(0.0%2C%201.0%2C%20step%3D0.25%2C%20value%3D0.5%2C%20label%3D%22exponent%20U%22%2C%20show_value%3DTrue)%0A%20%20%20%20hb_c%20%3D%20mo.ui.slider(2%2C%2010%2C%20step%3D1%2C%20value%3D6%2C%20label%3D%22multiplier%20C%22%2C%20show_value%3DTrue)%0A%20%20%20%20mo.hstack(%5Bhb_exponent%2C%20hb_c%5D%2C%20justify%3D%22start%22%2C%20gap%3D2)%0A%20%20%20%20return%20hb_c%2C%20hb_exponent%0A%0A%0A%40app.cell%0Adef%20_(hb_c%2C%20hb_exponent)%3A%0A%20%20%20%20%23%20The%20households'%20HB%20effects%20and%20fences%2C%20by%20province%2C%20with%20the%20chosen%20U%20and%20C%20(and%20A%20%3D%200.05)%0A%20%20%20%20_effects%20%3D%20attempt(hb_effects%2C%20households%5B%22sel_final_weight%22%5D%2C%20households%5B%22sel_design_weight%22%5D%2C%20PROVINCE%2C%20hb_exponent.value)%0A%20%20%20%20mo.stop(_effects%20is%20NOT_YET%2C%20waiting_for(%22Exercise%208.4%22))%0A%20%20%20%20_flags%20%3D%20attempt(hb_outliers%2C%20_effects%2C%20PROVINCE%2C%20float(hb_c.value)%2C%200.05)%0A%20%20%20%20mo.stop(_flags%20is%20NOT_YET%2C%20waiting_for(%22Exercise%208.5%22))%0A%20%20%20%20_fig%2C%20_ax%20%3D%20plt.subplots(figsize%3D(6.5%2C%203.4))%0A%20%20%20%20_ax.scatter(households%5B%22sel_final_weight%22%5D%5B~_flags%5D%2C%20_effects%5B~_flags%5D%2C%20s%3D8%2C%20color%3D%22%234c78a8%22%2C%20alpha%3D0.45%2C%20label%3D%22within%20the%20fences%22)%0A%20%20%20%20_ax.scatter(households%5B%22sel_final_weight%22%5D%5B_flags%5D%2C%20_effects%5B_flags%5D%2C%20s%3D26%2C%20color%3D%22%23e4572e%22%2C%20label%3D%22flagged%22)%0A%20%20%20%20_ax.set_xscale(%22log%22)%0A%20%20%20%20_ax.set_xlabel(%22final%20weight%2C%20sel_final_weight%20(households%2C%20log%20scale)%22)%0A%20%20%20%20_ax.set_ylabel(%22HB%20effect%22)%0A%20%20%20%20_ax.set_title(f%22Final%20against%20design%20weight%20by%20province%20(U%20%3D%20%7Bhb_exponent.value%7D%2C%20C%20%3D%20%7Bhb_c.value%7D)%22)%0A%20%20%20%20_ax.legend(frameon%3DFalse%2C%20loc%3D%22upper%20left%22%2C%20bbox_to_anchor%3D(1%2C%201))%0A%20%20%20%20_ax.spines%5B%5B%22top%22%2C%20%22right%22%5D%5D.set_visible(False)%0A%20%20%20%20_fig.tight_layout()%0A%20%20%20%20_flagged%20%3D%20households%5B_flags%5D%5B%5B*CASE%2C%20%22province_code%22%2C%20%22sel_stratum%22%2C%20%22sel_design_weight%22%2C%20%22sel_nr_weight%22%2C%20%22sel_final_weight%22%5D%5D.merge(planted_kinds%2C%20on%3DCASE%2C%20how%3D%22left%22)%0A%20%20%20%20_flagged%5B%22planted%22%5D%20%3D%20_flagged%5B%22planted%22%5D.fillna(%22(none%20planted)%22)%0A%20%20%20%20mo.vstack(%5B_ax%2C%20mo.md(f%22**%7Bint(_flags.sum())%7D%20households%20flagged.**%22)%2C%20_flagged.round(3)%5D)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20%23%20The%20settings%20this%20module%20chose%20for%20HB%20on%20the%20survey's%20weights%2C%20and%20what%20they%20flag%0A%20%20%20%20_effects%20%3D%20attempt(hb_effects%2C%20households%5B%22sel_final_weight%22%5D%2C%20households%5B%22sel_design_weight%22%5D%2C%20PROVINCE%2C%200.5)%0A%20%20%20%20mo.stop(_effects%20is%20NOT_YET)%0A%20%20%20%20_flags%20%3D%20attempt(hb_outliers%2C%20_effects%2C%20PROVINCE%2C%206.0%2C%200.05)%0A%20%20%20%20mo.stop(_flags%20is%20NOT_YET)%0A%20%20%20%20_flagged%20%3D%20households%5B_flags%5D.merge(planted_kinds%2C%20on%3DCASE%2C%20how%3D%22left%22)%0A%20%20%20%20_inflated%20%3D%20_flagged%5B%22planted%22%5D.fillna(%22%22).str.contains(%22final_weight_inflated%22)%0A%20%20%20%20_other%20%3D%20_flagged%5B~_inflated%5D%0A%20%20%20%20_factor%20%3D%20(_other%5B%22sel_nr_weight%22%5D%20%2F%20_other%5B%22sel_design_weight%22%5D).round(2)%0A%20%20%20%20_province_factor%20%3D%20(households%5B%22sel_nr_weight%22%5D%20%2F%20households%5B%22sel_design_weight%22%5D).groupby(PROVINCE).median()%0A%20%20%20%20mo.md(f%22%22%22%0A%20%20%20%20At%20U%20%3D%200.5%20and%20C%20%3D%206%2C%20HB%20flags%20%7Blen(_flagged)%7D%20households%3A%20the%20%7Bint(_inflated.sum())%7D%20final%20weights%20the%20course%20inflated%2C%20and%20%7Blen(_other)%7D%20whose%20area's%20adjustment%20for%20nonresponse%20multiplied%20its%20design%20weight%20by%20%7B'%2C%20'.join(f'%7Bf%3A.2f%7D'%20for%20f%20in%20_factor)%7D%2C%20against%20a%20median%20of%20%7B'%2C%20'.join(f'%7B_province_factor%5Bp%5D%3A.2f%7D'%20for%20p%20in%20(_other%5B'region_code'%5D%20%2B%20'-'%20%2B%20_other%5B'province_code'%5D))%7D%20in%20its%20province.%20That%20weight%20is%20right%3A%20its%20area%20had%20more%20households%20that%20did%20not%20respond.%20Lower%20C%2C%20or%20raise%20U%20toward%201%2C%20and%20more%20such%20areas%20come%20in.%20Section%205%20uses%20U%20%3D%200.5%20and%20C%20%3D%206.%20They%20were%20chosen%20by%20looking%20at%20this%20survey's%20weights%2C%20as%20a%20survey%20team%20chooses%20them%20on%20a%20round%20it%20knows%3B%20on%20a%20new%20round%2C%20the%20first%20review%20shows%20whether%20they%20still%20fit.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%203%20%C2%B7%20Ratios%2C%20relations%20and%20weight%20outliers%0A%0A%20%20%20%20**The%20problem.**%20Some%20values%20are%20wrong%20only%20beside%20another%20one.%20A%20length%20of%2030%20meters%20is%20wrong%20beside%20%22outrigger%20boat%22%3B%20a%20final%20weight%20of%20160%20is%20wrong%20beside%20a%20design%20weight%20of%2015.%20A%20relation%20between%20two%20items%20that%20must%20hold%20**exactly**%20is%20a%20rule%2C%20and%20module%204%20has%20them%3A%20the%20members%20count%20on%20the%20cover%20equals%20the%20roster's%20rows%20(H1)%2C%20the%20stored%20tonnage%20equals%20length%20%C3%97%20beam%20%C3%97%20depth%20%C3%97%200.212%20(B3).%20A%20relation%20that%20holds%20**approximately**%20is%20a%20ratio%20check.%0A%0A%20%20%20%20A%20boat's%20gross%20tonnage%20grows%20with%20length%20%C3%97%20beam%20%C3%97%20depth%2C%20so%20its%20**tonnage%20per%20meter%20of%20length**%20grows%20with%20beam%20%C3%97%20depth%2C%20which%20varies%20little%20among%20boats%20of%20one%20hull.%20Feet%20recorded%20as%20meters%20multiply%20every%20dimension%20by%203.281%2C%20the%20tonnage%20by%203.281%C2%B3%20%E2%89%88%2035%20and%20the%20tonnage%20per%20meter%20by%203.281%C2%B2%20%E2%89%88%2011.%20A%20stored%20tonnage%20with%20the%20factor%20left%20out%20is%201%20%2F%200.212%20%E2%89%88%204.7%20times%20too%20large%20and%20shows%20the%20same%20way%2C%20and%20B3%20finds%20it%20too%3B%20the%20ratio%20check%20finds%20both%20without%20knowing%20the%20cause.%0A%0A%20%20%20%20**The%20tool.**%20The%20robust%20z%20of%20the%20ratio's%20**logarithm**%2C%20within%20groups.%20The%20log%20does%20for%20a%20ratio%20what%20HB's%20centering%20does%3A%20log%2010%20and%20log%200.1%20are%20equally%20far%20from%200%2C%20so%20ten%20times%20the%20usual%20and%20a%20tenth%20of%20it%20score%20the%20same.%20In%20R%20it%20reads%20%60group_by(boat_hull)%20%7C%3E%20mutate(z%20%3D%20(log(r)%20-%20median(log(r)))%20%2F%20mad(log(r)))%60.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%20Exercise%208.6.%20A%20ratio%20out%20of%20line%20with%20its%20group%0A%0A%20%20%20%20**Why%20it%20matters%3A**%20a%20ratio%20check%20catches%20errors%20that%20keep%20each%20item%20plausible%20alone%3A%20a%20dimension%20in%20the%20wrong%20unit%2C%20a%20tonnage%20computed%20wrong%2C%20a%20count%20copied%20into%20the%20wrong%20column.%20One%20function%20serves%20every%20pair%20of%20items.%0A%0A%20%20%20%20**Input%20%E2%86%92%20output%3A**%20a%20Series%20%60numerator%60%2C%20a%20Series%20%60denominator%60%2C%20a%20Series%20of%20%60groups%60%20and%20a%20%60cutoff%60%20%E2%86%92%20a%20Series%20of%20True%2FFalse%3A%20True%20where%20the%20robust%20z%20of%20log(numerator%20%2F%20denominator)%20within%20the%20group%20is%20beyond%20the%20cutoff%20on%20either%20side.%20Where%20a%20group's%20MAD%20is%200%2C%20or%20the%20ratio%20is%20missing%2C%20nothing%20is%20flagged.%0A%0A%20%20%20%20%7C%20group%20%7C%20ratios%20%7C%20cutoff%203.5%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20A%20%7C%201.0%2C%201.1%2C%200.9%2C%201.05%2C%200.95%2C%20**10**%2C%20**0.1**%20%7C%2010%20and%200.1%20both%20flagged%20%7C%0A%20%20%20%20%7C%20B%20%7C%202%2C%202%2C%202%2C%203%20%7C%20MAD%200%3A%20nothing%20flagged%20%7C%0A%0A%20%20%20%20**Tools%3A**%20%60np.log%60%2C%20the%20robust%20z%20of%20Exercise%208.3%20(write%20it%20again%20here%2C%20or%20call%20your%20%60robust_z%60)%2C%20%60abs()%60%2C%20%60fillna%60%20or%20a%20comparison%20that%20is%20False%20on%20missing%20values.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20ratio_outliers(numerator%3A%20pd.Series%2C%20denominator%3A%20pd.Series%2C%20groups%3A%20pd.Series%2C%20cutoff%3A%20float%20%3D%203.5)%20-%3E%20pd.Series%3A%0A%20%20%20%20%22%22%22True%20where%20the%20robust%20z%20of%20log(numerator%20%2F%20denominator)%20within%20its%20group%20is%20beyond%20the%20cutoff%20on%20either%20side.%22%22%22%0A%20%20%20%20%23%20Exercise%208.6%3A%20the%20task%20and%20examples%20are%20in%20the%20text%20above%20this%20cell.%0A%20%20%20%20%23%20Step%201%3A%20the%20log%20of%20the%20ratio%2C%20np.log(numerator%20%2F%20denominator)%0A%20%20%20%20%23%20Step%202%3A%20its%20robust%20z%20within%20the%20groups%20(median%20and%20MAD%20by%20transform%2C%201.4826%2C%20missing%20where%20the%20MAD%20is%200)%0A%20%20%20%20%23%20Step%203%3A%20return%20True%20where%20the%20absolute%20robust%20z%20is%20over%20the%20cutoff%20(a%20missing%20z%20gives%20False)%0A%20%20%20%20raise%20NotImplementedError(%22Exercise%208.6%22)%0A%0A%0A%40app.function%0Adef%20pairs_fixture()%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22Pairs%20of%20values%20in%20two%20groups%3A%20A%20has%20a%20ratio%20ten%20times%20the%20usual%20and%20one%20a%20tenth%20of%20it%2C%20B%20has%20ratios%20mostly%0A%20%20%20%20equal%20(a%20MAD%20of%200)%2C%20and%20one%20pair%20has%20no%20denominator.%22%22%22%0A%20%20%20%20return%20pd.DataFrame(%7B%0A%20%20%20%20%20%20%20%20%22group%22%3A%20%5B%22A%22%5D%20*%208%20%2B%20%5B%22B%22%5D%20*%204%2C%0A%20%20%20%20%20%20%20%20%22numerator%22%3A%20%5B1.0%2C%201.1%2C%200.9%2C%201.05%2C%200.95%2C%2010.0%2C%200.1%2C%201.0%2C%204.0%2C%202.0%2C%206.0%2C%209.0%5D%2C%0A%20%20%20%20%20%20%20%20%22denominator%22%3A%20%5B1.0%2C%201.0%2C%201.0%2C%201.0%2C%201.0%2C%201.0%2C%201.0%2C%20None%2C%202.0%2C%201.0%2C%203.0%2C%203.0%5D%2C%0A%20%20%20%20%7D)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(boats)%3A%0A%20%20%20%20_small%20%3D%20pairs_fixture()%0A%20%20%20%20check_cases(%0A%20%20%20%20%20%20%20%20%22Exercise%208.6%22%2C%20ratio_outliers%2C%0A%20%20%20%20%20%20%20%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22two%20small%20groups%3A%20ten%20times%20and%20a%20tenth%20of%20the%20usual%20ratio%2C%20a%20MAD%20of%200%2C%20a%20missing%20denominator%22%2C%20(_small%5B%22numerator%22%5D%2C%20_small%5B%22denominator%22%5D%2C%20_small%5B%22group%22%5D%2C%203.5))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22the%20survey's%20tonnage%20per%20meter%20of%20length%20by%20hull%2C%20cutoff%203.5%22%2C%20(boats%5B%22boat_tonnage%22%5D%2C%20boats%5B%22length_m%22%5D%2C%20boats%5B%22boat_hull%22%5D%2C%203.5))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22the%20same%2C%20cutoff%206%22%2C%20(boats%5B%22boat_tonnage%22%5D%2C%20boats%5B%22length_m%22%5D%2C%20boats%5B%22boat_hull%22%5D%2C%206.0))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22the%20final%20against%20the%20design%20weight%20by%20stratum%2C%20cutoff%203.5%22%2C%20(households%5B%22sel_final_weight%22%5D%2C%20households%5B%22sel_design_weight%22%5D%2C%20households%5B%22sel_stratum%22%5D%2C%203.5))%2C%0A%20%20%20%20%20%20%20%20%5D%2C%0A%20%20%20%20%20%20%20%20what%3D%22sets%20of%20pairs%20and%20groups%22%2C%20summarize%3Das_values%2C%0A%20%20%20%20%20%20%20%20hint%3D%22Take%20the%20log%20of%20the%20ratio%2C%20then%20its%20robust%20z%20within%20each%20group%20(missing%20where%20the%20MAD%20is%200)%2C%20and%20flag%20where%20its%20absolute%20value%20is%20over%20the%20cutoff.%22%2C%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(boats)%3A%0A%20%20%20%20%23%20Try%20it%3A%20the%20boats%20whose%20tonnage%20per%20meter%20is%20out%20of%20line%20with%20their%20hull's%0A%20%20%20%20def%20_try_ratios()%3A%0A%20%20%20%20%20%20%20%20try%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20_flags%20%3D%20ratio_outliers(boats%5B%22boat_tonnage%22%5D%2C%20boats%5B%22length_m%22%5D%2C%20boats%5B%22boat_hull%22%5D%2C%203.5)%0A%20%20%20%20%20%20%20%20except%20NotImplementedError%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20mo.callout(mo.md(%22**Not%20written%20yet.**%20Write%20your%20code%2C%20delete%20the%20%60raise%60%20line%2C%20and%20run%20the%20function's%20cell.%22)%2C%20kind%3D%22neutral%22)%0A%20%20%20%20%20%20%20%20return%20boats%5B_flags%5D.assign(tons_per_m%3Dboats%5B%22boat_tonnage%22%5D%20%2F%20boats%5B%22length_m%22%5D)%5B%5B*CASE%2C%20%22boat_hull%22%2C%20%22length_m%22%2C%20%22boat_tonnage%22%2C%20%22tons_per_m%22%5D%5D.round(3)%0A%0A%20%20%20%20_try_ratios()%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.accordion(%7B%22If%20the%20check%20is%20red%3A%20common%20causes%20for%20Exercise%208.6%22%3A%20mo.md(r%22%22%22%0A%20%20%20%20%7C%20what%20you%20see%20%7C%20the%20usual%20cause%20%7C%20look%20at%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20The%20ratio%20of%2010%20flagged%2C%20the%20ratio%20of%200.1%20not%20%7C%20The%20robust%20z%20of%20the%20ratio%2C%20not%20of%20its%20log.%20%7C%20%60np.log(numerator%20%2F%20denominator)%60%20%7C%0A%20%20%20%20%7C%20Group%20B's%20ratios%20flagged%20%7C%20The%20MAD%20of%200%20gave%20infinity.%20%7C%20%60.where(mad%20%3E%200)%60%20%7C%0A%20%20%20%20%7C%20The%20pair%20with%20no%20denominator%20flagged%20%7C%20A%20missing%20z%20compared%20as%20True%2C%20or%20filled%20with%20a%20large%20number.%20%7C%20%60z.abs()%20%3E%20cutoff%60%20is%20False%20on%20a%20missing%20value%20%7C%0A%20%20%20%20%7C%20Only%20one%20side%20flagged%20%7C%20%60z%20%3E%20cutoff%60%20without%20%60abs()%60.%20%7C%20%60z.abs()%60%20%7C%0A%20%20%20%20%7C%20*RuntimeWarning%3A%20divide%20by%20zero%20encountered%20in%20log*%20%7C%20A%20numerator%20of%200%3A%20its%20log%20is%20minus%20infinity.%20Check%20the%20data%3B%20the%20survey's%20boats%20have%20no%20tonnage%20of%200.%20%7C%20the%20ratio's%20numerator%20%7C%0A%0A%20%20%20%20Still%20stuck%20after%20ten%20minutes%3F%20Ask%20your%20table%2C%20then%20raise%20your%20hand.%0A%20%20%20%20%22%22%22)%7D)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(boats)%3A%0A%20%20%20%20%23%20The%20flagged%20boats%20with%20the%20defects%20planted%20in%20their%20household%3A%20what%20the%20ratio%20check%20finds%0A%20%20%20%20_flags%20%3D%20attempt(ratio_outliers%2C%20boats%5B%22boat_tonnage%22%5D%2C%20boats%5B%22length_m%22%5D%2C%20boats%5B%22boat_hull%22%5D%2C%203.5)%0A%20%20%20%20mo.stop(_flags%20is%20NOT_YET%2C%20waiting_for(%22Exercise%208.6%22))%0A%20%20%20%20_found%20%3D%20boats%5B_flags%5D.merge(households%5B%5B*CASE%2C%20%22sel_stratum%22%5D%5D%2C%20on%3DCASE%2C%20how%3D%22left%22).merge(planted_kinds%2C%20on%3DCASE%2C%20how%3D%22left%22)%0A%20%20%20%20_found%5B%22planted%22%5D%20%3D%20_found%5B%22planted%22%5D.fillna(%22(none%20planted)%22)%0A%20%20%20%20_honest%20%3D%20_found%5B_found%5B%22planted%22%5D%20%3D%3D%20%22(none%20planted)%22%5D%0A%20%20%20%20_register%20%3D%20_honest%5B(_honest%5B%22sel_stratum%22%5D%20%3D%3D%20%22F%22)%20%26%20(_honest%5B%22boat_hull%22%5D%20%3D%3D%202)%5D%0A%20%20%20%20_outriggers%20%3D%20boats%5Bboats%5B%22boat_hull%22%5D%20%3D%3D%202%5D.merge(households%5B%5B*CASE%2C%20%22sel_stratum%22%5D%5D%2C%20on%3DCASE%2C%20how%3D%22left%22)%0A%20%20%20%20_width%20%3D%20(_outriggers%5B%22beam_m%22%5D%20*%20_outriggers%5B%22depth_m%22%5D).groupby(_outriggers%5B%22sel_stratum%22%5D%20%3D%3D%20%22F%22).median()%0A%20%20%20%20mo.vstack(%5B%0A%20%20%20%20%20%20%20%20_found%5B%5B*CASE%2C%20%22sel_stratum%22%2C%20%22boat_hull%22%2C%20%22length_m%22%2C%20%22boat_tonnage%22%2C%20%22planted%22%5D%5D.round(2)%2C%0A%20%20%20%20%20%20%20%20mo.md(f%22%22%22%0A%20%20%20%20%20%20%20%20The%20check%20finds%20the%20feet%20recorded%20as%20meters%2C%20the%20tonnages%20stored%20without%20the%20factor%20and%20the%20long%20outrigger%20boats%20with%20no%20engine%2C%20without%20being%20told%20any%20of%20them.%20It%20also%20flags%20**%7Blen(_honest)%7D%20boats%20where%20nothing%20was%20planted**.%20Outrigger%20boats%20of%20households%20in%20stratum%20F%2C%20the%20register%20of%20large%20units%2C%20account%20for%20%7Blen(_register)%7D%20of%20them%2C%20between%20%7B_register%5B'length_m'%5D.min()%3A.1f%7D%20and%20%7B_register%5B'length_m'%5D.max()%3A.1f%7D%20meters.%20They%20are%20right%2C%20and%20unusual%3A%20most%20outrigger%20boats%20belong%20to%20small-scale%20units%2C%20and%20the%20register's%20outrigger%20boats%20are%20broader%20and%20deeper%20(a%20median%20beam%20%C3%97%20depth%20of%20%7B_width%5BTrue%5D%3A.2f%7D%20square%20meters%2C%20against%20%7B_width%5BFalse%5D%3A.2f%7D%20for%20the%20others)%2C%20so%20they%20carry%20more%20tonnage%20per%20meter%20than%20their%20group.%20A%20group%20that%20mixes%20two%20kinds%20of%20unit%20makes%20the%20rarer%20kind%20look%20odd%3B%20a%20group%20of%20hull%20within%20stratum%20would%20not.%0A%20%20%20%20%20%20%20%20%22%22%22)%2C%0A%20%20%20%20%5D)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%20Weight%20outliers%0A%0A%20%20%20%20A%20weight%20can%20be%20wrong%20in%20two%20ways%2C%20and%20section%202%20found%20the%20first%3A%20a%20final%20weight%20out%20of%20line%20with%20its%20province's%20adjustments%2C%20an%20error.%20The%20second%20kind%20is%20a%20weight%20that%20is%20right%20for%20its%20stratum%20and%20wrong%20for%20its%20**domain**%2C%20the%20group%20an%20estimate%20is%20made%20for%20(module%206).%0A%0A%20%20%20%20A%20household%20selected%20in%20an%20area%20stratum%20(A%2C%20B%20or%20C)%20stands%20for%20many%20households%3A%20its%20final%20weight%20says%20how%20many.%20Suppose%20it%20turns%20out%20to%20run%20a%20large-scale%20unit%2C%20with%20a%20boat%20of%205.0%20gross%20tons%20or%20more.%20It%20now%20enters%20the%20estimates%20for%20large-scale%20units%2C%20where%20the%20households%20of%20stratum%20F%2C%20the%20register%20of%20large%20units%20taken%20in%20full%2C%20each%20stand%20for%20about%20one.%20That%20household%20alone%20counts%20as%20much%20as%20several%20of%20the%20register's%20households%3B%20the%20table%20below%20gives%20each%20stratum's%20median%20weight%20in%20each%20domain.%20The%20survey%20has%20no%20item%20that%20marks%20such%20households%3B%20the%20stratum%20it%20was%20selected%20in%2C%20beside%20its%20units'%20scale%2C%20says%20it.%20Statistics%20find%20them%20too%2C%20and%20they%20find%20heavy%20weights%20no%20stratum%20would%20explain.%0A%0A%20%20%20%20Two%20numbers%20say%20how%20much%20a%20household's%20weight%20dominates%20its%20domain%3A%0A%0A%20%20%20%20%7C%20indicator%20%7C%20formula%20%7C%20what%20it%20says%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20%60vs_median%60%20%7C%20the%20weight%20%2F%20the%20domain's%20median%20weight%20%7C%20how%20many%20typical%20households%20of%20the%20domain%20this%20one%20counts%20as%20%7C%0A%20%20%20%20%7C%20%60share%60%20%7C%20the%20weight%20%2F%20the%20domain's%20total%20weight%20%7C%20the%20share%20of%20an%20estimated%20count%20of%20the%20domain%20that%20rests%20on%20this%20one%20answer%20%7C%0A%0A%20%20%20%20The%20domain%20here%20is%20the%20scale%3A%20large-scale%20when%20any%20of%20the%20household's%20fishing%20units%20is%20large-scale%20(%60unit_scale%20%3D%202%60)%2C%20small-scale%20otherwise.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20%23%20Each%20household's%20domain%3A%20large-scale%20when%20any%20of%20its%20fishing%20units%20is%20large-scale%20(unit_scale%20%3D%202)%0A%20%20%20%20_large%20%3D%20units.groupby(CASE)%5B%22unit_scale%22%5D.max().eq(2).rename(%22large%22).reset_index()%0A%20%20%20%20domains%20%3D%20households%5B%5B*CASE%2C%20%22sel_stratum%22%2C%20%22sel_final_weight%22%5D%5D.merge(_large%2C%20on%3DCASE%2C%20how%3D%22left%22%2C%20validate%3D%22one_to_one%22)%0A%20%20%20%20domains%5B%22domain%22%5D%20%3D%20np.where(domains%5B%22large%22%5D.fillna(False).astype(bool)%2C%20%22large-scale%22%2C%20%22small-scale%22)%0A%20%20%20%20_summary%20%3D%20%7B%22households%22%3A%20(%22hh_id%22%2C%20%22size%22)%2C%20%22median_weight%22%3A%20(%22sel_final_weight%22%2C%20%22median%22)%2C%20%22total_weight%22%3A%20(%22sel_final_weight%22%2C%20%22sum%22)%7D%0A%20%20%20%20mo.vstack(%5B%0A%20%20%20%20%20%20%20%20domains.groupby(%22domain%22).agg(**_summary).round(2)%2C%0A%20%20%20%20%20%20%20%20mo.md(%22The%20same%20by%20the%20stratum%20each%20household%20was%20selected%20in%3A%22)%2C%0A%20%20%20%20%20%20%20%20domains.groupby(%5B%22domain%22%2C%20%22sel_stratum%22%5D).agg(**_summary).round(2)%2C%0A%20%20%20%20%5D)%0A%20%20%20%20return%20(domains%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%20Exercise%208.7.%20How%20much%20a%20weight%20dominates%20its%20domain%0A%0A%20%20%20%20**Why%20it%20matters%3A**%20an%20estimate%20for%20a%20small%20domain%20can%20rest%20on%20a%20few%20households%20with%20large%20weights.%20Before%20an%20estimate%20is%20published%2C%20the%20estimation%20team%20needs%20to%20know%20which%20households%20carry%20it%2C%20and%20whether%20they%20belong%20in%20it.%0A%0A%20%20%20%20**Input%20%E2%86%92%20output%3A**%20a%20Series%20of%20%60weights%60%20and%20a%20Series%20of%20%60domains%60%20%E2%86%92%20a%20table%20with%20one%20row%20per%20household%2C%20in%20the%20same%20order%2C%20and%20two%20columns%3A%20%60vs_median%60%20(the%20weight%20over%20its%20domain's%20**median**%20weight)%20and%20%60share%60%20(the%20weight%20over%20its%20domain's%20**total**%20weight).%0A%0A%20%20%20%20%7C%20domain%20%7C%20weight%20%7C%20vs_median%20%7C%20share%20%7C%0A%20%20%20%20%7C%3A--%7C--%3A%7C--%3A%7C--%3A%7C%0A%20%20%20%20%7C%20A%20%7C%201%20%7C%201.0%20%7C%200.0435%20%7C%0A%20%20%20%20%7C%20A%20%7C%201%20%7C%201.0%20%7C%200.0435%20%7C%0A%20%20%20%20%7C%20A%20%7C%201%20%7C%201.0%20%7C%200.0435%20%7C%0A%20%20%20%20%7C%20A%20%7C%2020%20%7C%20**20.0**%20%7C%20**0.8696**%20%7C%0A%0A%20%20%20%20The%20median%2C%20not%20the%20mean%3A%20A's%20mean%20weight%20is%205.75%2C%20which%20the%20heavy%20household%20itself%20pulled%20up.%0A%0A%20%20%20%20**Tools%3A**%20%60groupby(domains).transform(%22median%22)%60%20and%20%60transform(%22sum%22)%60%2C%20%60pd.DataFrame(%7B...%7D)%60%2C%20above.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20weight_influence(weights%3A%20pd.Series%2C%20domains%3A%20pd.Series)%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22Each%20weight%20over%20its%20domain's%20median%20weight%20(vs_median)%20and%20over%20its%20domain's%20total%20weight%20(share).%22%22%22%0A%20%20%20%20%23%20Exercise%208.7%3A%20the%20task%20and%20examples%20are%20in%20the%20text%20above%20this%20cell.%0A%20%20%20%20%23%20Step%201%3A%20each%20weight's%20domain%20median%20and%20domain%20total%20(transform)%0A%20%20%20%20%23%20Step%202%3A%20return%20a%20DataFrame%20with%20vs_median%20%3D%20weight%20%2F%20median%20and%20share%20%3D%20weight%20%2F%20total%0A%20%20%20%20raise%20NotImplementedError(%22Exercise%208.7%22)%0A%0A%0A%40app.function%0Adef%20weights_fixture()%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22Seven%20weights%20in%20two%20domains%3A%20A%20has%20one%20heavy%20household%20among%20light%20ones%3B%20B's%20weights%20are%20all%20alike.%22%22%22%0A%20%20%20%20return%20pd.DataFrame(%7B%22domain%22%3A%20%5B%22A%22%2C%20%22A%22%2C%20%22A%22%2C%20%22A%22%2C%20%22B%22%2C%20%22B%22%2C%20%22B%22%5D%2C%20%22weight%22%3A%20%5B1.0%2C%201.0%2C%201.0%2C%2020.0%2C%2020.0%2C%2021.0%2C%2022.0%5D%7D)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(domains)%3A%0A%20%20%20%20_small%20%3D%20weights_fixture()%0A%20%20%20%20check_cases(%0A%20%20%20%20%20%20%20%20%22Exercise%208.7%22%2C%20weight_influence%2C%0A%20%20%20%20%20%20%20%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22seven%20weights%20in%20two%20domains%2C%20one%20heavy%20among%20light%20ones%22%2C%20(_small%5B%22weight%22%5D%2C%20_small%5B%22domain%22%5D))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22the%20survey's%20final%20weights%20by%20scale%22%2C%20(domains%5B%22sel_final_weight%22%5D%2C%20domains%5B%22domain%22%5D))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22the%20final%20weights%20by%20stratum%22%2C%20(households%5B%22sel_final_weight%22%5D%2C%20households%5B%22sel_stratum%22%5D))%2C%0A%20%20%20%20%20%20%20%20%5D%2C%0A%20%20%20%20%20%20%20%20what%3D%22sets%20of%20weights%20and%20domains%22%2C%20summarize%3Dlambda%20table%3A%20as_rows(table.reset_index(drop%3DTrue).reset_index()%2C%20%22index%22)%2C%0A%20%20%20%20%20%20%20%20hint%3D%22Divide%20each%20weight%20by%20its%20domain's%20median%20(vs_median)%20and%20by%20its%20domain's%20total%20(share)%2C%20both%20from%20transform.%22%2C%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(domains)%3A%0A%20%20%20%20%23%20Try%20it%3A%20the%20households%20that%20count%20most%20in%20their%20domain%0A%20%20%20%20def%20_try_influence()%3A%0A%20%20%20%20%20%20%20%20try%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20_influence%20%3D%20weight_influence(domains%5B%22sel_final_weight%22%5D%2C%20domains%5B%22domain%22%5D)%0A%20%20%20%20%20%20%20%20except%20NotImplementedError%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20mo.callout(mo.md(%22**Not%20written%20yet.**%20Write%20your%20code%2C%20delete%20the%20%60raise%60%20line%2C%20and%20run%20the%20function's%20cell.%22)%2C%20kind%3D%22neutral%22)%0A%20%20%20%20%20%20%20%20return%20domains.join(_influence).sort_values(%22vs_median%22%2C%20ascending%3DFalse).head(8).round(3)%0A%0A%20%20%20%20_try_influence()%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.accordion(%7B%22If%20the%20check%20is%20red%3A%20common%20causes%20for%20Exercise%208.7%22%3A%20mo.md(r%22%22%22%0A%20%20%20%20%7C%20what%20you%20see%20%7C%20the%20usual%20cause%20%7C%20look%20at%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%203.48%20for%20the%20heavy%20household%2C%20not%2020%20%7C%20The%20mean%2C%20not%20the%20median.%20%7C%20%60transform(%22median%22)%60%20%7C%0A%20%20%20%20%7C%20%60share%60%20adds%20up%20to%201%20over%20all%20rows%2C%20not%20per%20domain%20%7C%20The%20total%20of%20all%20weights.%20%7C%20%60transform(%22sum%22)%60%20within%20domains%20%7C%0A%20%20%20%20%7C%20One%20row%20per%20domain%20%7C%20%60.median()%60%20or%20%60.agg()%60%20instead%20of%20%60transform%60.%20%7C%20%60transform%60%20%7C%0A%20%20%20%20%7C%20*KeyError%3A%20'vs_median'*%20%7C%20The%20columns%20are%20named%20otherwise.%20%7C%20%60vs_median%60%20and%20%60share%60%20%7C%0A%0A%20%20%20%20Still%20stuck%20after%20ten%20minutes%3F%20Ask%20your%20table%2C%20then%20raise%20your%20hand.%0A%20%20%20%20%22%22%22)%7D)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(domains)%3A%0A%20%20%20%20%23%20The%20households%20whose%20weight%20is%20more%20than%20four%20times%20their%20domain's%20median%2C%20by%20domain%2C%20planted%20defects%20and%20the%0A%20%20%20%20%23%20stratum%20they%20were%20selected%20in%0A%20%20%20%20_influence%20%3D%20attempt(weight_influence%2C%20domains%5B%22sel_final_weight%22%5D%2C%20domains%5B%22domain%22%5D)%0A%20%20%20%20mo.stop(_influence%20is%20NOT_YET%2C%20waiting_for(%22Exercise%208.7%22))%0A%20%20%20%20_all%20%3D%20domains.join(_influence)%0A%20%20%20%20_twice%20%3D%20_all%5B(_all%5B%22domain%22%5D%20%3D%3D%20%22small-scale%22)%20%26%20(_all%5B%22vs_median%22%5D%20%3E%202)%5D%0A%20%20%20%20_heavy%20%3D%20_all%5B_all%5B%22vs_median%22%5D%20%3E%204%5D.merge(planted_kinds%2C%20on%3DCASE%2C%20how%3D%22left%22)%0A%20%20%20%20_heavy%5B%22planted%22%5D%20%3D%20_heavy%5B%22planted%22%5D.fillna(%22(none%20planted)%22)%0A%20%20%20%20_large%20%3D%20_heavy%5B_heavy%5B%22domain%22%5D%20%3D%3D%20%22large-scale%22%5D%0A%20%20%20%20_typical%20%3D%20_all.groupby(%5B%22domain%22%2C%20%22sel_stratum%22%5D)%5B%22vs_median%22%5D.median()%20%20%23%20each%20stratum's%20usual%20weight%2C%20in%20its%20domain's%20medians%0A%20%20%20%20_boat_error%20%3D%20_large%5B%22planted%22%5D.str.contains(%22feet_recorded_as_meters%7Cno_engine_large_boat%22)%0A%20%20%20%20mo.vstack(%5B%0A%20%20%20%20%20%20%20%20pd.crosstab(%5B_heavy%5B%22domain%22%5D%2C%20_heavy%5B%22planted%22%5D%5D%2C%20_heavy%5B%22sel_stratum%22%5D.rename(%22sel_stratum%22)%2C%20margins%3DTrue%2C%20margins_name%3D%22all%22).rename(columns%3Dstr)%2C%0A%20%20%20%20%20%20%20%20mo.md(f%22%22%22%0A%20%20%20%20%20%20%20%20**Why%20four%20times%2C%20not%20twice.**%20By%20design%20a%20household%20of%20stratum%20C%20weighs%20about%20%7B_typical%5B'small-scale'%2C%20'C'%5D%3A.1f%7D%20times%20the%20small-scale%20domain's%20median%2C%20so%20%22twice%20the%20median%22%20would%20send%20%7Blen(_twice)%3A%2C%7D%20small-scale%20households%20to%20a%20reviewer%2C%20nearly%20all%20with%20right%20weights.%20In%20the%20large-scale%20domain%20the%20households%20of%20stratum%20B%20weigh%20about%20%7B_typical%5B'large-scale'%2C%20'B'%5D%3A.1f%7D%20times%20its%20median%20and%20those%20of%20stratum%20C%20about%20%7B_typical%5B'large-scale'%2C%20'C'%5D%3A.1f%7D%20times%3B%20four%20keeps%20the%20second%20and%20not%20the%20first.%20Like%20HB's%20settings%2C%20the%20threshold%20was%20chosen%20by%20looking%20at%20this%20survey's%20weights.%0A%0A%20%20%20%20%20%20%20%20In%20the%20large-scale%20domain%2C%20**%7Blen(_large)%7D%20households**%20weigh%20more%20than%20four%20times%20the%20median%3B%20together%20they%20carry%20**%7B_large%5B'share'%5D.sum()%3A.0%25%7D**%20of%20the%20domain's%20total%20weight.%20Every%20one%20was%20selected%20in%20stratum%20%7B'%2C%20'.join(sorted(_large%5B'sel_stratum'%5D.unique()))%7D%3A%20the%20course's%20list%20names%20them%20%60large_unit_in_area_stratum%60%2C%20a%20kind%20derived%20from%20the%20design%2C%20not%20planted.%20%7Bint(_boat_error.sum())%7D%20of%20them%20also%20carry%20a%20planted%20boat%20error%20that%20made%20a%20unit%20large-scale%3A%20the%20feet%20recorded%20as%20meters%20and%20the%20long%20outrigger%20boats%20with%20no%20engine.%20Their%20weight%20is%20right%3B%20their%20domain%20is%20wrong%20because%20a%20boat%20is.%20In%20the%20small-scale%20domain%2C%20the%20heavy%20households%20are%20the%20weights%20section%202%20found.%0A%0A%20%20%20%20%20%20%20%20**What%20happens%20next%20is%20not%20an%20edit%20of%20the%20weight.**%20The%20weight%20of%20a%20household%20that%20was%20selected%20in%20an%20area%20stratum%20and%20runs%20a%20large-scale%20unit%20is%20right%20by%20design%3B%20the%20review%20confirms%20that%20its%20unit%20is%20large-scale%2C%20and%20the%20estimation%20team%20decides%20how%20the%20domain's%20estimate%20treats%20it%20(the%20design%20effect%2C%20in%20the%20stretch%20S2%2C%20shows%20the%20cost).%20A%20household%20whose%20boat%20error%20made%20it%20large-scale%20goes%20back%20to%20the%20boat.%0A%20%20%20%20%20%20%20%20%22%22%22)%2C%0A%20%20%20%20%5D)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%204%20%C2%B7%20Interviewer-level%20indicators%0A%0A%20%20%20%20**The%20problem.**%20After%20the%20rules%20and%20the%20outlier%20checks%2C%20some%20wrong%20records%20can%20remain%20that%20no%20check%20on%20one%20record%20can%20find%3A%20interviews%20never%20held%2C%20filled%20in%20by%20the%20interviewer.%20Each%20answer%20is%20plausible%2C%20and%20the%20household%20is%20ordinary.%20What%20gives%20such%20interviews%20away%20is%20the%20interviewer's%20work%20as%20a%20whole%3A%20interviews%20too%20short%2C%20or%20too%20many%20in%20a%20day%3B%20ages%20that%20end%20in%200%20or%205%20more%20often%20than%20ages%20do%3B%20the%20same%20boat%20and%20the%20same%20catch%20from%20one%20household%20to%20the%20next%3B%20items%20left%20blank%20that%20others%20fill%20in.%20Survey%20methodologists%20call%20it%20falsification%2C%20or%20curbstoning.%0A%0A%20%20%20%20Schwanh%C3%A4user%2C%20Sakshaug%20and%20Kosyakova%20(2022%2C%20*Public%20Opinion%20Quarterly*%2086(1))%20compared%2032%20indicators%20of%20falsification%2C%20among%20them%20interview%20duration%2C%20rounding%2C%20item%20nonresponse%20and%20nondifferentiation%20(giving%20the%20same%20answers%20to%20everyone)%2C%20all%20computed%20**at%20the%20interviewer%20level**.%20Time-related%20indicators%20mattered%20most.%20They%20advise%20confirming%20any%20statistical%20flag%20by%20other%20means%2C%20such%20as%20a%20reinterview.%20This%20section%20computes%20five%20such%20indicators%20for%20each%20of%20the%20survey's%20interviewers%3A%0A%0A%20%20%20%20%7C%20indicator%20%7C%20what%20it%20measures%20%7C%20for%20each%20interviewer%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20busiest%20day%20%7C%20pace%20%7C%20the%20most%20interviews%20on%20one%20day%20%7C%0A%20%20%20%20%7C%20interview%20length%20%7C%20pace%20%7C%20the%20median%20minutes%20%7C%0A%20%20%20%20%7C%20age%20heaping%20%7C%20rounding%20%7C%20Whipple's%20index%20on%20members'%20ages%20%7C%0A%20%20%20%20%7C%20repeated%20catch%20%7C%20nondifferentiation%20%7C%20the%20share%20of%20interviews%20whose%20list%20of%20species%20another%20of%20the%20interviewer's%20interviews%20also%20has%20%7C%0A%20%20%20%20%7C%20item%20nonresponse%20%7C%20answers%20left%20blank%20%7C%20the%20share%20of%20landing-site%20rows%20with%20no%20name%20%7C%0A%0A%20%20%20%20%23%23%23%20Whipple's%20index%0A%0A%20%20%20%20People%20who%20do%20not%20know%20an%20age%2C%20or%20do%20not%20ask%2C%20round%20it%3A%2040%2C%2045%2C%2050.%20Among%20ages%2023%20to%2062%2C%20a%20fifth%20end%20in%200%20or%205%20when%20nobody%20rounds.%20**Whipple's%20index**%20compares%20the%20count%20that%20does%20with%20that%20fifth%3A%0A%0A%20%20%20%20Whipple%20%3D%20100%20%C3%97%20(ages%2023%20to%2062%20ending%20in%200%20or%205)%20%2F%20((all%20ages%2023%20to%2062)%20%2F%205)%0A%0A%20%20%20%20It%20is%20100%20when%20there%20is%20no%20heaping%20and%20500%20when%20every%20age%20ends%20in%200%20or%205.%20Demographers%20use%20it%20to%20judge%20how%20well%20a%20population's%20ages%20were%20reported%3B%20here%20it%20judges%20each%20interviewer's.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%20Exercise%208.8.%20Whipple's%20index%0A%0A%20%20%20%20**Why%20it%20matters%3A**%20age%20heaping%20is%20the%20rounding%20indicator%20that%20needs%20nothing%20but%20the%20roster%2C%20and%20the%20survey%20has%20ages%20for%20every%20member.%20Computed%20per%20interviewer%2C%20it%20shows%20whose%20ages%20are%20guessed.%0A%0A%20%20%20%20**Input%20%E2%86%92%20output%3A**%20a%20Series%20of%20ages%20%E2%86%92%20one%20number%2C%20Whipple's%20index%20on%20the%20ages%20from%20**23%20to%2062%2C%20both%20included**.%20Ages%20outside%20that%20range%2C%20and%20missing%20ages%2C%20do%20not%20count.%0A%0A%20%20%20%20%7C%20ages%20%7C%20Whipple%20%7C%0A%20%20%20%20%7C%3A--%7C--%3A%7C%0A%20%20%20%20%7C%2023%2C%2024%2C%20%E2%80%A6%2C%2062%20(each%20once)%20%7C%20100.0%20%7C%0A%20%20%20%20%7C%2025%2C%2030%2C%2035%2C%2040%20%7C%20500.0%20%7C%0A%20%20%20%20%7C%2020%2C%2023%2C%2025%2C%2063%2C%2065%2C%2070%20%7C%2023%20and%2025%20count%3A%20100%20%C3%97%201%20%2F%20(2%20%2F%205)%20%3D%20250.0%20%7C%0A%0A%20%20%20%20**Tools%3A**%20boolean%20masks%20with%20%60between%60%20or%20%60%3E%3D%60%20and%20%60%3C%3D%60%2C%20%60%25%205%60%2C%20%60.sum()%60%2C%20%60len()%60.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20whipple(ages%3A%20pd.Series)%20-%3E%20float%3A%0A%20%20%20%20%22%22%22Whipple's%20index%20of%20age%20heaping%3A%20100%20*%20(ages%2023-62%20ending%20in%200%20or%205)%20%2F%20(ages%2023-62%20%2F%205).%22%22%22%0A%20%20%20%20%23%20Exercise%208.8%3A%20the%20task%20and%20examples%20are%20in%20the%20text%20above%20this%20cell.%0A%20%20%20%20%23%20Step%201%3A%20keep%20the%20ages%20from%2023%20to%2062%2C%20both%20included%20(missing%20ages%20drop%20out%20of%20a%20comparison)%0A%20%20%20%20%23%20Step%202%3A%20count%20those%20that%20end%20in%200%20or%205%20(age%20%25%205%20%3D%3D%200)%0A%20%20%20%20%23%20Step%203%3A%20return%20100%20times%20that%20count%2C%20divided%20by%20a%20fifth%20of%20the%20ages%20kept%0A%20%20%20%20raise%20NotImplementedError(%22Exercise%208.8%22)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20_by_interviewer%20%3D%20members.merge(households%5B%5B*CASE%2C%20%22hh_interviewer%22%5D%5D%2C%20on%3DCASE)%0A%20%20%20%20check_cases(%0A%20%20%20%20%20%20%20%20%22Exercise%208.8%22%2C%20whipple%2C%0A%20%20%20%20%20%20%20%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22the%20ages%2023%20to%2062%2C%20each%20once%22%2C%20(pd.Series(range(23%2C%2063))%2C))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22ages%2025%2C%2030%2C%2035%20and%2040%22%2C%20(pd.Series(%5B25%2C%2030%2C%2035%2C%2040%5D)%2C))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22ages%2020%2C%2023%2C%2025%2C%2063%2C%2065%20and%2070%3A%20only%2023%20and%2025%20count%22%2C%20(pd.Series(%5B20%2C%2023%2C%2025%2C%2063%2C%2065%2C%2070%5D)%2C))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22ages%2022%20to%2063%2C%20with%20a%20missing%20age%22%2C%20(pd.Series(%5B*range(22%2C%2064)%2C%20None%5D%2C%20dtype%3D%22Float64%22)%2C))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22every%20member%20of%20the%20survey%22%2C%20(members%5B%22mem_age%22%5D%2C))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22the%20members%20of%20interviewer%20INT019's%20households%22%2C%20(_by_interviewer.loc%5B_by_interviewer%5B%22hh_interviewer%22%5D%20%3D%3D%20%22INT019%22%2C%20%22mem_age%22%5D%2C))%2C%0A%20%20%20%20%20%20%20%20%5D%2C%0A%20%20%20%20%20%20%20%20what%3D%22sets%20of%20ages%22%2C%20summarize%3Dlambda%20number%3A%20round(float(number)%2C%204)%2C%0A%20%20%20%20%20%20%20%20hint%3D%22Keep%20the%20ages%20from%2023%20to%2062%20(both%20included)%2C%20count%20those%20ending%20in%200%20or%205%2C%20and%20divide%20100%20times%20that%20count%20by%20a%20fifth%20of%20the%20ages%20kept.%22%2C%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20%23%20Try%20it%3A%20the%20index%20for%20all%20members%20of%20the%20survey%3B%20it%20should%20be%20near%20100%0A%20%20%20%20try_it(whipple%2C%20members%5B%22mem_age%22%5D)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.accordion(%7B%22If%20the%20check%20is%20red%3A%20common%20causes%20for%20Exercise%208.8%22%3A%20mo.md(r%22%22%22%0A%20%20%20%20%7C%20what%20you%20see%20%7C%20the%20usual%20cause%20%7C%20look%20at%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%200.2%20for%20the%20ages%2023%20to%2062%20%7C%20Divided%20by%20all%20the%20ages%20kept%2C%20not%20a%20fifth%20of%20them%2C%20and%20not%20times%20100.%20%7C%20%60100%20*%20count%20%2F%20(len(kept)%20%2F%205)%60%20%7C%0A%20%20%20%20%7C%2097.56%20for%20the%20ages%2023%20to%2062%20%7C%20One%20end%20of%20the%20range%20left%20out%20(%60%3E%2023%60%20or%20%60%3C%2062%60)%2C%20so%2040%20years%20became%2039.%20%7C%20%60%3E%3D%2023%60%20and%20%60%3C%3D%2062%60%20%7C%0A%20%20%20%20%7C%20333.33%20for%20the%20third%20case%20%7C%20Ages%20outside%2023%20to%2062%20counted.%20%7C%20the%20range%20mask%20first%20%7C%0A%20%20%20%20%7C%20*TypeError%3A%20unsupported%20operand%20type(s)%20for%20%26*%20%7C%20A%20comparison%20without%20brackets.%20%7C%20%60(ages%20%3E%3D%2023)%20%26%20(ages%20%3C%3D%2062)%60%20%7C%0A%0A%20%20%20%20Still%20stuck%20after%20ten%20minutes%3F%20Ask%20your%20table%2C%20then%20raise%20your%20hand.%0A%20%20%20%20%22%22%22)%7D)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%20Exercise%208.9.%20Pace%20and%20repeated%20answers%2C%20interviewer%20by%20interviewer%0A%0A%20%20%20%20**Why%20it%20matters%3A**%20the%20time%20indicators%20were%20the%20strongest%20in%20the%20comparison%20above%2C%20and%20the%20repeated%20answers%20are%20what%20a%20falsifier%20saves%20time%20with.%20Together%20they%20are%20the%20table%20a%20field%20supervisor%20reads%20each%20week.%0A%0A%20%20%20%20**Input%20%E2%86%92%20output%3A**%20the%20%60households%60%20table%20(it%20needs%20%60region_code%60%2C%20%60hh_id%60%2C%20%60hh_interviewer%60%2C%20%60hh_interview_date%60%20and%20%60hh_interview_minutes%60)%20and%20the%20%60catch%60%20table%20(it%20needs%20%60region_code%60%2C%20%60hh_id%60%20and%20%60catch_species%60)%20%E2%86%92%20one%20row%20per%20%60hh_interviewer%60%20with%3A%0A%0A%20%20%20%20-%20%60interviews%60%3A%20how%20many%20households%20the%20interviewer%20interviewed%3B%0A%20%20%20%20-%20%60busiest_day%60%3A%20the%20most%20interviews%20the%20interviewer%20held%20on%20one%20date%3B%0A%20%20%20%20-%20%60median_minutes%60%3A%20the%20median%20of%20their%20interviews'%20minutes%3B%0A%20%20%20%20-%20%60repeated_catch%60%3A%20the%20share%20of%20their%20interviews%20whose%20**catch%20list**%20is%20the%20same%20as%20the%20catch%20list%20of%20at%20least%20one%20**other%20interview%20by%20the%20same%20interviewer**.%20A%20household's%20catch%20list%20is%20its%20species%20codes%2C%20each%20once%2C%20sorted%2C%20as%20text%3A%20%60%22103%2C104%2C304%22%60.%20An%20interview%20with%20no%20catch%20row%20has%20no%20list%2C%20and%20counts%20as%20not%20repeated.%0A%0A%20%20%20%20%7C%20hh_interviewer%20%7C%20interviews%20%7C%20busiest_day%20%7C%20median_minutes%20%7C%20repeated_catch%20%7C%0A%20%20%20%20%7C%3A--%7C--%3A%7C--%3A%7C--%3A%7C--%3A%7C%0A%20%20%20%20%7C%20I1%20%7C%204%20%7C%203%20%7C%20%E2%80%A6%20%7C%201.0%20%7C%0A%20%20%20%20%7C%20I2%20%7C%202%20%7C%201%20%7C%20%E2%80%A6%20%7C%200.0%20(its%20one%20list%20is%20also%20I1's%2C%20but%20no%20other%20of%20I2's)%20%7C%0A%0A%20%20%20%20A%20household%20is%20a%20region%20**and**%20an%20id%3A%20join%20the%20catch%20to%20the%20households%20on%20both%2C%20or%20a%20household%20of%20region%2007%20takes%20the%20catch%20of%20the%20household%20with%20the%20same%20id%20in%20region%2001.%0A%0A%20%20%20%20**Tools%3A**%20%60groupby%60%20with%20two%20keys%20and%20%60.size()%60%2C%20a%20grouped%20%60max%60%2C%20%60agg%60%20with%20a%20function%20that%20builds%20the%20list%20(%60lambda%20s%3A%20%22%2C%22.join(...)%60)%2C%20%60merge%60%20on%20%60CASE%60%2C%20%60transform(%22size%22)%60.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20interviewer_indicators(households%3A%20pd.DataFrame%2C%20catch%3A%20pd.DataFrame)%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22One%20row%20per%20interviewer%3A%20interviews%2C%20busiest_day%2C%20median_minutes%20and%20repeated_catch.%22%22%22%0A%20%20%20%20%23%20Exercise%208.9%3A%20the%20task%20and%20examples%20are%20in%20the%20text%20above%20this%20cell.%0A%20%20%20%20%23%20Step%201%3A%20interviews%20and%20median_minutes%20per%20interviewer%20(groupby%2C%20agg)%0A%20%20%20%20%23%20Step%202%3A%20busiest_day%3A%20count%20interviews%20per%20interviewer%20and%20hh_interview_date%2C%20then%20the%20largest%20count%20per%20interviewer%0A%20%20%20%20%23%20Step%203%3A%20each%20household's%20catch%20list%3A%20group%20the%20catch%20by%20CASE%2C%20species%20codes%20each%20once%2C%20sorted%2C%20joined%20with%20%22%2C%22%0A%20%20%20%20%23%20Step%204%3A%20put%20each%20household's%20list%20beside%20its%20interviewer%20(merge%20on%20CASE%2C%20keeping%20every%20interview)%3B%20an%20interview's%0A%20%20%20%20%23%20%20%20%20%20%20%20%20%20list%20is%20repeated%20when%20its%20interviewer%20has%20it%20more%20than%20once%20(groupby%20interviewer%20and%20list%2C%20transform(%22size%22)%20%3E%201)%0A%20%20%20%20%23%20Step%205%3A%20repeated_catch%20%3D%20the%20share%20of%20each%20interviewer's%20interviews%20that%20are%20repeated%3B%20return%20the%20table%0A%20%20%20%20raise%20NotImplementedError(%22Exercise%208.9%22)%0A%0A%0A%40app.function%0Adef%20fieldwork_fixture()%20-%3E%20tuple%5Bpd.DataFrame%2C%20pd.DataFrame%5D%3A%0A%20%20%20%20%22%22%22Six%20interviews%20by%20two%20interviewers%20and%20their%20catch%3A%20I1%20holds%20three%20on%20one%20day%2C%20two%20of%20its%20lists%20are%20the%20same%20in%0A%20%20%20%20another%20row%20order%2C%20two%20more%20are%20the%20same%20once%20a%20duplicated%20row%20is%20counted%20once%3B%20I2's%20only%20list%20is%20also%20I1's%2C%20one%20of%0A%20%20%20%20I2's%20households%20has%20no%20catch%2C%20and%20one%20has%20the%20id%20of%20an%20I1%20household%20in%20another%20region.%22%22%22%0A%20%20%20%20interviews%20%3D%20pd.DataFrame(%7B%0A%20%20%20%20%20%20%20%20%22region_code%22%3A%20%5B%2201%22%2C%20%2201%22%2C%20%2201%22%2C%20%2201%22%2C%20%2207%22%2C%20%2207%22%5D%2C%0A%20%20%20%20%20%20%20%20%22hh_id%22%3A%20%5B1%2C%202%2C%203%2C%204%2C%201%2C%205%5D%2C%0A%20%20%20%20%20%20%20%20%22hh_interviewer%22%3A%20%5B%22I1%22%2C%20%22I1%22%2C%20%22I1%22%2C%20%22I1%22%2C%20%22I2%22%2C%20%22I2%22%5D%2C%0A%20%20%20%20%20%20%20%20%22hh_interview_date%22%3A%20pd.to_datetime(%5B%222026-03-02%22%2C%20%222026-03-02%22%2C%20%222026-03-02%22%2C%20%222026-03-03%22%2C%20%222026-03-02%22%2C%20%222026-03-04%22%5D)%2C%0A%20%20%20%20%20%20%20%20%22hh_interview_minutes%22%3A%20%5B12%2C%2010%2C%2014%2C%2050%2C%2045%2C%2052%5D%2C%0A%20%20%20%20%7D)%0A%20%20%20%20fish%20%3D%20pd.DataFrame(%7B%0A%20%20%20%20%20%20%20%20%22region_code%22%3A%20%5B%2201%22%2C%20%2201%22%2C%20%2201%22%2C%20%2201%22%2C%20%2201%22%2C%20%2201%22%2C%20%2201%22%2C%20%2207%22%2C%20%2207%22%5D%2C%0A%20%20%20%20%20%20%20%20%22hh_id%22%3A%20%5B1%2C%201%2C%202%2C%202%2C%203%2C%203%2C%204%2C%201%2C%201%5D%2C%0A%20%20%20%20%20%20%20%20%22catch_species%22%3A%20%5B103%2C%20304%2C%20304%2C%20103%2C%20103%2C%20103%2C%20103%2C%20103%2C%20304%5D%2C%0A%20%20%20%20%7D)%0A%20%20%20%20return%20interviews%2C%20fish%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20_interviews%2C%20_fish%20%3D%20fieldwork_fixture()%0A%20%20%20%20check_cases(%0A%20%20%20%20%20%20%20%20%22Exercise%208.9%22%2C%20interviewer_indicators%2C%0A%20%20%20%20%20%20%20%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22six%20interviews%20by%20two%20interviewers%3A%20repeated%20lists%20in%20another%20order%2C%20a%20duplicated%20row%2C%20an%20id%20in%20two%20regions%2C%20a%20household%20with%20no%20catch%22%2C%20(_interviews%2C%20_fish))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22all%20the%20survey's%20interviews%20and%20catch%22%2C%20(households%2C%20catch))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22region%2007's%20interviews%20and%20catch%22%2C%20(households%5Bhouseholds%5B%22region_code%22%5D%20%3D%3D%20%2207%22%5D%2C%20catch%5Bcatch%5B%22region_code%22%5D%20%3D%3D%20%2207%22%5D))%2C%0A%20%20%20%20%20%20%20%20%5D%2C%0A%20%20%20%20%20%20%20%20what%3D%22sets%20of%20interviews%20and%20catch%22%2C%20summarize%3Dlambda%20table%3A%20as_rows(table%2C%20%22hh_interviewer%22)%2C%0A%20%20%20%20%20%20%20%20hint%3D%22Count%20per%20interviewer%20and%20date%20for%20the%20busiest%20day%3B%20build%20each%20household's%20sorted%20list%20of%20species%20as%20text%3B%20a%20list%20is%20repeated%20when%20the%20same%20interviewer%20has%20it%20more%20than%20once.%22%2C%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20%23%20Try%20it%3A%20your%20indicators%20for%20every%20interviewer%2C%20the%20busiest%20first%0A%20%20%20%20def%20_try_indicators()%3A%0A%20%20%20%20%20%20%20%20try%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20_table%20%3D%20interviewer_indicators(households%2C%20catch)%0A%20%20%20%20%20%20%20%20except%20NotImplementedError%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20mo.callout(mo.md(%22**Not%20written%20yet.**%20Write%20your%20code%2C%20delete%20the%20%60raise%60%20line%2C%20and%20run%20the%20function's%20cell.%22)%2C%20kind%3D%22neutral%22)%0A%20%20%20%20%20%20%20%20return%20_table.sort_values(%22busiest_day%22%2C%20ascending%3DFalse).round(3)%0A%0A%20%20%20%20_try_indicators()%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.accordion(%7B%22If%20the%20check%20is%20red%3A%20common%20causes%20for%20Exercise%208.9%22%3A%20mo.md(r%22%22%22%0A%20%20%20%20%7C%20what%20you%20see%20%7C%20the%20usual%20cause%20%7C%20look%20at%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20I1's%20%60repeated_catch%60%20is%20below%201.0%20%7C%20The%20lists%20were%20built%20from%20the%20rows%20in%20their%20order%2C%20or%20with%20the%20duplicated%20row%20twice%3A%20%60%22304%2C103%22%60%20is%20not%20%60%22103%2C304%22%60%2C%20and%20%60%22103%2C103%22%60%20is%20not%20%60%22103%22%60.%20%7C%20%60sorted(set(s))%60%20%7C%0A%20%20%20%20%7C%20I2's%20%60repeated_catch%60%20is%20above%200%20%7C%20Lists%20were%20counted%20across%20interviewers.%20%7C%20%60groupby(%5B%22hh_interviewer%22%2C%20%22catch_list%22%5D)%60%20%7C%0A%20%20%20%20%7C%20I2%20has%201%20interview%2C%20or%20its%20share%20is%20out%20of%201%20%7C%20The%20household%20with%20no%20catch%20was%20dropped%20by%20an%20inner%20join.%20%7C%20%60merge(...%2C%20how%3D%22left%22)%60%20%7C%0A%20%20%20%20%7C%20I2's%20household%201%20has%20I1's%20catch%2C%20or%20more%20rows%20than%20interviews%20%7C%20The%20catch%20was%20joined%20on%20%60hh_id%60%20alone.%20%7C%20%60on%3DCASE%60%20%7C%0A%20%20%20%20%7C%20%60busiest_day%60%20equals%20%60interviews%60%20%7C%20Counted%20per%20interviewer%2C%20not%20per%20interviewer%20and%20date.%20%7C%20%60groupby(%5B%22hh_interviewer%22%2C%20%22hh_interview_date%22%5D).size()%60%20%7C%0A%0A%20%20%20%20Still%20stuck%20after%20ten%20minutes%3F%20Ask%20your%20table%2C%20then%20raise%20your%20hand.%0A%20%20%20%20%22%22%22)%7D)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20%23%20Every%20interviewer's%20indicators%20side%20by%20side%3A%20your%20two%20exercises%2C%20item%20nonresponse%2C%20and%20a%20summary%20score%0A%20%20%20%20_pace%20%3D%20attempt(interviewer_indicators%2C%20households%2C%20catch)%0A%20%20%20%20mo.stop(_pace%20is%20NOT_YET%2C%20waiting_for(%22Exercise%208.9%22))%0A%20%20%20%20_ages%20%3D%20members.merge(households%5B%5B*CASE%2C%20%22hh_interviewer%22%5D%5D%2C%20on%3DCASE)%0A%20%20%20%20_heaping%20%3D%20_ages.groupby(%22hh_interviewer%22)%5B%22mem_age%22%5D.apply(lambda%20a%3A%20attempt(whipple%2C%20a))%0A%20%20%20%20mo.stop(any(value%20is%20NOT_YET%20for%20value%20in%20_heaping)%2C%20waiting_for(%22Exercise%208.8%22))%0A%20%20%20%20_sites%20%3D%20landing.merge(households%5B%5B*CASE%2C%20%22hh_interviewer%22%5D%5D%2C%20on%3DCASE)%0A%20%20%20%20indicators%20%3D%20_pace.assign(%0A%20%20%20%20%20%20%20%20whipple%3D_heaping.astype(%22float64%22)%2C%0A%20%20%20%20%20%20%20%20blank_landing_name%3D_sites%5B%22land_name%22%5D.fillna(%22%22).str.strip().eq(%22%22).groupby(_sites%5B%22hh_interviewer%22%5D).mean()%2C%0A%20%20%20%20)%0A%20%20%20%20%23%20The%20summary%20score%3A%20each%20indicator's%20z-score%20across%20interviewers%2C%20turned%20so%20that%20a%20high%20value%20is%20suspect%2C%20summed%0A%20%20%20%20_direction%20%3D%20%7B%22busiest_day%22%3A%201%2C%20%22median_minutes%22%3A%20-1%2C%20%22whipple%22%3A%201%2C%20%22repeated_catch%22%3A%201%2C%20%22blank_landing_name%22%3A%201%7D%0A%20%20%20%20indicators%5B%22score%22%5D%20%3D%20sum(sign%20*%20(indicators%5Bname%5D%20-%20indicators%5Bname%5D.mean())%20%2F%20indicators%5Bname%5D.std()%20for%20name%2C%20sign%20in%20_direction.items())%0A%20%20%20%20indicators%20%3D%20indicators.sort_values(%22score%22%2C%20ascending%3DFalse)%0A%20%20%20%20indicators.round(3)%0A%20%20%20%20return%20(indicators%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(indicators)%3A%0A%20%20%20%20_top%2C%20_second%20%3D%20indicators.index%5B0%5D%2C%20indicators.index%5B1%5D%0A%20%20%20%20_others%20%3D%20indicators.drop(_top)%0A%20%20%20%20mo.md(f%22%22%22%0A%20%20%20%20**One%20interviewer%20stands%20apart%20on%20every%20indicator.**%20%7B_top%7D%20held%20up%20to%20**%7Bindicators.loc%5B_top%2C%20'busiest_day'%5D%3A.0f%7D%20interviews%20in%20a%20day**%2C%20where%20no%20other%20interviewer%20held%20more%20than%20%7B_others%5B'busiest_day'%5D.max()%3A.0f%7D%3B%20a%20median%20of%20**%7Bindicators.loc%5B_top%2C%20'median_minutes'%5D%3A.0f%7D%20minutes**%2C%20against%20%7B_others%5B'median_minutes'%5D.min()%3A.0f%7D%20to%20%7B_others%5B'median_minutes'%5D.max()%3A.0f%7D%20for%20the%20others%3B%20a%20Whipple's%20index%20of%20**%7Bindicators.loc%5B_top%2C%20'whipple'%5D%3A.0f%7D**%20against%20%7B_others%5B'whipple'%5D.min()%3A.0f%7D%20to%20%7B_others%5B'whipple'%5D.max()%3A.0f%7D%3B%20**%7Bindicators.loc%5B_top%2C%20'repeated_catch'%5D%3A.0%25%7D**%20of%20interviews%20with%20a%20catch%20list%20repeated%20from%20another%20of%20their%20own%3B%20and%20a%20landing-site%20name%20left%20blank%20in%20**%7Bindicators.loc%5B_top%2C%20'blank_landing_name'%5D%3A.0%25%7D**%20of%20rows%2C%20against%20%7B_others%5B'blank_landing_name'%5D.min()%3A.0%25%7D%20to%20%7B_others%5B'blank_landing_name'%5D.max()%3A.0%25%7D.%20The%20score%20sums%20the%20five%20indicators'%20z-scores%20(each%20indicator's%20distance%20from%20the%20interviewers'%20mean%2C%20in%20standard%20deviations%2C%20turned%20so%20that%20a%20high%20value%20is%20suspect)%3A%20%7B_top%7D%20scores%20%7Bindicators.loc%5B_top%2C%20'score'%5D%3A.1f%7D%2C%20the%20next%20interviewer%20%7Bindicators.loc%5B_second%2C%20'score'%5D%3A.1f%7D.%20Across%20%7Blen(indicators)%7D%20interviewers%20the%20mean%20and%20standard%20deviation%20are%20the%20right%20yardstick%3A%20there%20is%20one%20value%20per%20interviewer%2C%20and%20section%202's%20masking%20needs%20several%20outliers%20in%20a%20small%20group.%0A%0A%20%20%20%20**No%20indicator%20is%20enough%20on%20its%20own.**%20Here%20the%20blank%20landing-site%20names%20separate%20%7B_top%7D%20as%20clearly%20as%20the%20pace%20does%2C%20because%20a%20made-up%20interview%20has%20no%20place%20to%20name.%20Another%20falsifier%20fills%20in%20every%20item%20and%20leaves%20no%20gap%20to%20count%3B%20a%20third%20copies%20real%20households%20and%20keeps%20their%20ages.%20That%20is%20why%20the%20comparison%20above%20used%20many%20indicators%3A%20each%20catches%20some%20falsifiers%20and%20misses%20others%2C%20and%20no%20single%20one%20has%20a%20cutoff%20that%20works%20everywhere.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(indicators)%3A%0A%20%20%20%20%23%20The%20summary%20score%20per%20interviewer%2C%20the%20highest%20in%20orange%20(matplotlib)%0A%20%20%20%20_ordered%20%3D%20indicators%5B%22score%22%5D.sort_values()%0A%20%20%20%20_fig%2C%20_ax%20%3D%20plt.subplots(figsize%3D(6.5%2C%204))%0A%20%20%20%20_ax.barh(_ordered.index%2C%20_ordered.values%2C%20color%3D%5B%22%23e4572e%22%20if%20name%20%3D%3D%20_ordered.index%5B-1%5D%20else%20%22%234c78a8%22%20for%20name%20in%20_ordered.index%5D)%0A%20%20%20%20_ax.axvline(0%2C%20color%3D%22%23888888%22%2C%20linewidth%3D0.8)%0A%20%20%20%20_ax.set_xlabel(%22summary%20score%20(sum%20of%20five%20indicators'%20z-scores)%22)%0A%20%20%20%20_ax.set_title(%22Interviewers%20by%20their%20interviews'%20pace%2C%20heaping%2C%20sameness%20and%20blanks%22)%0A%20%20%20%20_ax.spines%5B%5B%22top%22%2C%20%22right%22%5D%5D.set_visible(False)%0A%20%20%20%20_fig.tight_layout()%0A%20%20%20%20_ax%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%20From%20the%20interviewer%20to%20the%20interview%0A%0A%20%20%20%20The%20indicators%20point%20at%20an%20interviewer%3B%20a%20reviewer%20needs%20the%20interviews.%20Four%20flags%20on%20single%20interviews%20follow%20from%20them%2C%20and%20the%20cell%20below%20raises%20each%20for%20every%20household%20of%20the%20survey%2C%20so%20that%20section%205%20can%20rank%20them%20with%20the%20rest%3A%0A%0A%20%20%20%20-%20**a%20crowded%20day**%3A%20one%20of%2010%20or%20more%20interviews%20by%20its%20interviewer%20that%20day%2C%20more%20than%20any%20other%20interviewer%20held%20on%20one%20day%20in%20the%20table%20above%3B%0A%20%20%20%20-%20**a%20short%20interview**%3A%20under%2015%20minutes%3B%0A%20%20%20%20-%20**a%20repeated%20catch**%3A%20its%20catch%20list%20is%20in%203%20or%20more%20of%20its%20interviewer's%20interviews%3B%0A%20%20%20%20-%20**a%20copy**%3A%20the%20same%20members%20(sex%20and%20age%2C%20line%20by%20line)%20and%20the%20same%20catch%20list%20as%20another%20interview.%20A%20household%20entered%20twice%20under%20two%20ids%20looks%20like%20this%3B%20so%20does%20a%20household%20copied%20to%20fill%20a%20quota.%0A%0A%20%20%20%20A%20flag%20here%20is%20a%20reason%20to%20check%2C%20not%20proof.%20The%20next%20step%20for%20an%20interviewer%20flag%20is%20the%20one%20the%20literature%20names%3A%20a%20supervisor%20calls%20the%20household%20or%20revisits%20it.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20%23%20Interviewer%20flags%20on%20single%20interviews%3A%20each%20a%20table%20of%20the%20households%20it%20applies%20to%2C%20with%20the%20flag's%20wording%0A%20%20%20%20_cases%20%3D%20households%5B%5B*CASE%2C%20%22hh_interviewer%22%2C%20%22hh_interview_date%22%2C%20%22hh_interview_minutes%22%5D%5D.copy()%0A%20%20%20%20_cases%5B%22that_day%22%5D%20%3D%20_cases.groupby(%5B%22hh_interviewer%22%2C%20%22hh_interview_date%22%5D)%5B%22hh_id%22%5D.transform(%22size%22)%0A%20%20%20%20_lists%20%3D%20catch.groupby(CASE)%5B%22catch_species%22%5D.agg(lambda%20s%3A%20%22%2C%22.join(str(code)%20for%20code%20in%20sorted(set(s)))).rename(%22catch_list%22)%0A%20%20%20%20_people%20%3D%20members.sort_values(%5B*CASE%2C%20%22mem_line%22%5D).groupby(CASE)%5B%5B%22mem_sex%22%2C%20%22mem_age%22%5D%5D.agg(lambda%20s%3A%20%22%2C%22.join(map(str%2C%20s)))%0A%20%20%20%20_cases%20%3D%20_cases.join(_lists%2C%20on%3DCASE).join(_people%2C%20on%3DCASE)%0A%20%20%20%20_cases%5B%22same_list%22%5D%20%3D%20_cases.groupby(%5B%22hh_interviewer%22%2C%20%22catch_list%22%5D)%5B%22catch_list%22%5D.transform(%22size%22)%0A%20%20%20%20_cases%5B%22copies%22%5D%20%3D%20_cases.groupby(%5B%22mem_sex%22%2C%20%22mem_age%22%2C%20%22catch_list%22%5D)%5B%22catch_list%22%5D.transform(%22size%22)%0A%0A%0A%20%20%20%20def%20_flag(mask%3A%20pd.Series%2C%20wording%3A%20str)%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%20%20%20%20return%20_cases.loc%5Bmask.fillna(False).astype(bool)%2C%20CASE%5D.assign(source%3D%22interviewer%22%2C%20flag%3Dwording)%0A%0A%0A%20%20%20%20interviewer_flags%20%3D%20pd.concat(%5B%0A%20%20%20%20%20%20%20%20_flag(_cases%5B%22that_day%22%5D%20%3E%3D%2010%2C%20%22one%20of%2010%20or%20more%20interviews%20by%20its%20interviewer%20that%20day%22)%2C%0A%20%20%20%20%20%20%20%20_flag(_cases%5B%22hh_interview_minutes%22%5D%20%3C%2015%2C%20%22interview%20under%2015%20minutes%22)%2C%0A%20%20%20%20%20%20%20%20_flag(_cases%5B%22same_list%22%5D%20%3E%3D%203%2C%20%22catch%20list%20repeated%20in%203%20or%20more%20of%20its%20interviewer's%20interviews%22)%2C%0A%20%20%20%20%20%20%20%20_flag(_cases%5B%22copies%22%5D%20%3E%3D%202%2C%20%22same%20members%20and%20catch%20as%20another%20interview%22)%2C%0A%20%20%20%20%5D%2C%20ignore_index%3DTrue)%0A%20%20%20%20interviewer_flags.groupby(%22flag%22).size().rename(%22households%22).to_frame()%0A%20%20%20%20return%20(interviewer_flags%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%205%20%C2%B7%20Ranking%20flags%20into%20a%20review%20table%0A%0A%20%20%20%20**The%20problem.**%20Module%204's%20rules%20and%20this%20module's%20checks%20each%20raise%20their%20own%20list%2C%20and%20many%20lists%20name%20the%20same%20household%3A%20a%20household%20with%20a%20long%20outrigger%20boat%20is%20flagged%20by%20its%20length%2C%20its%20tonnage%20per%20meter%20and%20its%20weight%20in%20the%20large-scale%20domain.%20A%20reviewer%20given%20the%20lists%20one%20by%20one%20reads%20the%20same%20household%20three%20times%20and%20does%20not%20know%20where%20to%20start.%20They%20need%20**one%20row%20per%20household**%2C%20with%20every%20reason%20on%20it%2C%20the%20households%20most%20likely%20wrong%20first.%20The%20survey%20team%20needs%20two%20more%20numbers%3A%20what%20share%20of%20each%20kind%20of%20defect%20the%20table%20finds%2C%20and%20how%20many%20rows%20a%20reviewer%20reads%20to%20find%20it.%0A%0A%20%20%20%20**One%20long%20table%20of%20flags.**%20Every%20check%20becomes%20rows%20of%20the%20same%20four%20columns%3A%20the%20household%20(%60region_code%60%2C%20%60hh_id%60)%2C%20the%20%60source%60%20(rule%2C%20statistic%20or%20interviewer)%20and%20the%20%60flag%60%20in%20words.%20Long%20tables%20stack%20with%20%60pd.concat%60%2C%20however%20many%20checks%20there%20are.%20The%20cells%20below%20build%20the%20three%20sources%3A%20module%204's%20failure%20report%2C%20this%20module's%20statistics%20(from%20your%20functions)%2C%20and%20section%204's%20interviewer%20flags%20(above).%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20%23%20Module%204's%2025%20rules%20(data%2Fcodebook_rules.csv)%2C%20written%20as%20data%3A%20rule%2C%20the%20table%20it%20reads%20(by%20name%2C%20and%20the%20rows%20it%0A%20%20%20%20%23%20tests)%2C%20what%20happens%20to%20a%20failure%2C%20the%20message%2C%20and%20a%20test%20that%20is%20True%20on%20each%20row%20that%20fails.%20Module%204%20builds%0A%20%20%20%20%23%20this%20specification%20and%20its%20failure%20report%20step%20by%20step%3B%20here%20both%20are%20ready%20to%20run.%0A%20%20%20%20_education%2C%20_species%20%3D%20set(course.codes(%22education%22)%5B%22code%22%5D)%2C%20set(course.codes(%22species%22)%5B%22code%22%5D)%0A%20%20%20%20_roles%20%3D%20%5B%22mem_role_operator%22%2C%20%22mem_role_crew%22%2C%20%22mem_role_hired%22%2C%20%22mem_role_shore%22%5D%0A%20%20%20%20_crew%20%3D%20course.child(%22crew%22)%0A%20%20%20%20_roster%20%3D%20households.join(members.groupby(CASE).size().rename(%22rows%22)%2C%20on%3DCASE)%0A%20%20%20%20_operators%20%3D%20members%5B%5B*CASE%2C%20%22mem_line%22%2C%20%22mem_role_operator%22%2C%20%22mem_units_run%22%5D%5D.rename(columns%3D%7B%22mem_line%22%3A%20%22operator_line%22%7D)%0A%20%20%20%20_naming%20%3D%20units.groupby(%5B*CASE%2C%20%22operator_line%22%5D).size().rename(%22units_naming%22)%0A%20%20%20%20_largest%20%3D%20boat_rows.groupby(UNIT)%5B%22boat_tonnage%22%5D.max().rename(%22largest_gt%22)%0A%20%20%20%20_boat_count%20%3D%20boat_rows.groupby(UNIT).size().rename(%22boat_rows%22)%0A%20%20%20%20_hired%20%3D%20_crew%5B_crew%5B%22crew_hired%22%5D%20%3D%3D%201%5D.groupby(UNIT).size().rename(%22hired_rows%22)%0A%20%20%20%20_units%20%3D%20(units.merge(_operators%2C%20on%3D%5B*CASE%2C%20%22operator_line%22%5D%2C%20how%3D%22left%22)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20.join(_naming%2C%20on%3D%5B*CASE%2C%20%22operator_line%22%5D).join(_largest%2C%20on%3DUNIT).join(_boat_count%2C%20on%3DUNIT).join(_hired%2C%20on%3DUNIT))%0A%20%20%20%20_units%5B%5B%22boat_rows%22%2C%20%22hired_rows%22%5D%5D%20%3D%20_units%5B%5B%22boat_rows%22%2C%20%22hired_rows%22%5D%5D.fillna(0)%0A%20%20%20%20_factor%20%3D%20boat_rows%5B%22boat_measured_in%22%5D.map(%7B2%3A%200.212%2C%203%3A%200.006%7D)%0A%20%20%20%20_boats%20%3D%20boat_rows.assign(recomputed%3D(boat_rows%5B%22boat_length%22%5D%20*%20boat_rows%5B%22boat_beam%22%5D%20*%20boat_rows%5B%22boat_depth%22%5D%20*%20_factor)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20.where(boat_rows%5B%22boat_measured_in%22%5D%20!%3D%201%2C%20boat_rows%5B%22boat_tonnage_reported%22%5D))%0A%20%20%20%20_catch%20%3D%20catch.merge(units%5BUNIT%5D.drop_duplicates()%2C%20on%3DUNIT%2C%20how%3D%22left%22%2C%20indicator%3DTrue)%0A%20%20%20%20_caught%20%3D%20units.merge(catch%5BUNIT%5D.drop_duplicates()%2C%20on%3DUNIT%2C%20how%3D%22left%22%2C%20indicator%3DTrue)%0A%20%20%20%20rules%20%3D%20%5B%20%20%23%20rule%2C%20table%2C%20the%20rows%20it%20tests%2C%20action%2C%20message%2C%20test%0A%20%20%20%20%20%20%20%20(%22M1%22%2C%20%22member%22%2C%20members%2C%20%22refer%22%2C%20%22sex%20is%20not%201%20or%202%22%2C%20lambda%20t%3A%20~t%5B%22mem_sex%22%5D.isin(%5B1%2C%202%5D))%2C%0A%20%20%20%20%20%20%20%20(%22M2%22%2C%20%22member%22%2C%20members%2C%20%22refer%22%2C%20%22age%20is%20not%20between%200%20and%20110%22%2C%20lambda%20t%3A%20~t%5B%22mem_age%22%5D.between(0%2C%20110))%2C%0A%20%20%20%20%20%20%20%20(%22M3%22%2C%20%22member%22%2C%20members%2C%20%22refer%22%2C%20%22fishing%20items%20answered%20for%20a%20member%20under%2010%22%2C%20lambda%20t%3A%20(t%5B%22mem_age%22%5D%20%3C%2010)%20%26%20t%5B%5B%22mem_fished%22%2C%20*_roles%5D%5D.notna().any(axis%3D1))%2C%0A%20%20%20%20%20%20%20%20(%22M4%22%2C%20%22member%22%2C%20members%2C%20%22refer%22%2C%20%22a%20member%20who%20runs%20a%20unit%20is%20under%2016%22%2C%20lambda%20t%3A%20(t%5B%22mem_role_operator%22%5D%20%3D%3D%201)%20%26%20(t%5B%22mem_age%22%5D%20%3C%2016))%2C%0A%20%20%20%20%20%20%20%20(%22M5%22%2C%20%22member%22%2C%20members%2C%20%22refer%22%2C%20%22education%20is%20not%20in%20the%20code%20list%22%2C%20lambda%20t%3A%20(t%5B%22mem_age%22%5D%20%3E%3D%206)%20%26%20~t%5B%22mem_education%22%5D.isin(_education))%2C%0A%20%20%20%20%20%20%20%20(%22M6%22%2C%20%22member%22%2C%20members%2C%20%22refer%22%2C%20%22a%20member%20who%20fished%20has%20no%20role%22%2C%20lambda%20t%3A%20(t%5B%22mem_fished%22%5D%20%3D%3D%201)%20%26%20~(t%5B_roles%5D%20%3D%3D%201).any(axis%3D1))%2C%0A%20%20%20%20%20%20%20%20(%22H1%22%2C%20%22household%22%2C%20_roster%2C%20%22edit%22%2C%20%22members%20count%20differs%20from%20the%20roster's%20rows%22%2C%20lambda%20t%3A%20t%5B%22hh_roster_members%22%5D%20!%3D%20t%5B%22rows%22%5D)%2C%0A%20%20%20%20%20%20%20%20(%22U1%22%2C%20%22fishing_unit%22%2C%20units%2C%20%22edit%22%2C%20%22unit%20key%20is%20not%20unique%22%2C%20lambda%20t%3A%20t.duplicated(UNIT%2C%20keep%3DFalse))%2C%0A%20%20%20%20%20%20%20%20(%22U2%22%2C%20%22fishing_unit%22%2C%20_units%2C%20%22refer%22%2C%20%22operator's%20roster%20line%20does%20not%20agree%20with%20the%20units%22%2C%20lambda%20t%3A%20(t%5B%22mem_role_operator%22%5D.fillna(0)%20!%3D%201)%20%7C%20(t%5B%22mem_units_run%22%5D.fillna(0)%20!%3D%20t%5B%22units_naming%22%5D))%2C%0A%20%20%20%20%20%20%20%20(%22U3%22%2C%20%22fishing_unit%22%2C%20_units%2C%20%22refer%22%2C%20%22scale%20differs%20from%20the%20largest%20boat's%22%2C%20lambda%20t%3A%20t%5B%22unit_scale%22%5D%20!%3D%20np.where(t%5B%22largest_gt%22%5D.fillna(0)%20%3E%3D%205.0%2C%202%2C%201))%2C%0A%20%20%20%20%20%20%20%20(%22U4%22%2C%20%22fishing_unit%22%2C%20_units%2C%20%22edit%22%2C%20%22uses%20a%20boat%20is%20blank%22%2C%20lambda%20t%3A%20t%5B%22unit_uses_boat%22%5D.isna())%2C%0A%20%20%20%20%20%20%20%20(%22U5%22%2C%20%22fishing_unit%22%2C%20_units%2C%20%22edit%22%2C%20%22uses%20a%20boat%20differs%20from%20the%20boat%20rows%22%2C%20lambda%20t%3A%20t%5B%22unit_uses_boat%22%5D.notna()%20%26%20(t%5B%22unit_uses_boat%22%5D%20!%3D%20(t%5B%22boat_rows%22%5D%20%3E%200).astype(int)))%2C%0A%20%20%20%20%20%20%20%20(%22U6%22%2C%20%22fishing_unit%22%2C%20_units%2C%20%22refer%22%2C%20%22took%20on%20crew%20but%20has%20no%20hired%20crew%20row%22%2C%20lambda%20t%3A%20(t%5B%22unit_hired_crew%22%5D%20%3D%3D%201)%20%26%20(t%5B%22hired_rows%22%5D%20%3D%3D%200))%2C%0A%20%20%20%20%20%20%20%20(%22U7%22%2C%20%22fishing_unit%22%2C%20_units%2C%20%22edit%22%2C%20%22number%20of%20boats%20differs%20from%20the%20boat%20rows%22%2C%20lambda%20t%3A%20t%5B%22unit_boats%22%5D.fillna(0)%20!%3D%20t%5B%22boat_rows%22%5D)%2C%0A%20%20%20%20%20%20%20%20(%22U8%22%2C%20%22fishing_unit%22%2C%20units%2C%20%22refer%22%2C%20%22months%20active%20is%20not%20between%201%20and%2012%22%2C%20lambda%20t%3A%20~t%5B%22unit_months_active%22%5D.between(1%2C%2012))%2C%0A%20%20%20%20%20%20%20%20(%22B1%22%2C%20%22boat%22%2C%20boat_rows%2C%20%22refer%22%2C%20%22hull%20or%20engine%20is%20not%20a%20valid%20code%22%2C%20lambda%20t%3A%20~t%5B%22boat_hull%22%5D.isin(%5B1%2C%202%2C%203%5D)%20%7C%20~t%5B%22boat_engine%22%5D.isin(%5B0%2C%201%2C%202%5D))%2C%0A%20%20%20%20%20%20%20%20(%22B2%22%2C%20%22boat%22%2C%20boat_rows%2C%20%22edit%22%2C%20%22a%20boat%20with%20no%20engine%20is%205.0%20gross%20tons%20or%20more%22%2C%20lambda%20t%3A%20(t%5B%22boat_engine%22%5D%20%3D%3D%200)%20%26%20(t%5B%22boat_tonnage%22%5D%20%3E%3D%205.0))%2C%0A%20%20%20%20%20%20%20%20(%22B3%22%2C%20%22boat%22%2C%20_boats%2C%20%22edit%22%2C%20%22stored%20tonnage%20differs%20from%20the%20dimensions%22%2C%20lambda%20t%3A%20(t%5B%22boat_tonnage%22%5D%20-%20t%5B%22recomputed%22%5D).abs()%20%3E%200.01)%2C%0A%20%20%20%20%20%20%20%20(%22B4%22%2C%20%22boat%22%2C%20boat_rows%2C%20%22refer%22%2C%20%22engine%20power%20blank%20where%20there%20is%20an%20engine%2C%20or%20the%20reverse%22%2C%20lambda%20t%3A%20t%5B%22boat_engine_hp%22%5D.isna()%20!%3D%20(t%5B%22boat_engine%22%5D%20%3D%3D%200))%2C%0A%20%20%20%20%20%20%20%20(%22B5%22%2C%20%22boat%22%2C%20boat_rows%2C%20%22refer%22%2C%20%22a%20registration%20number%20is%20on%20two%20boat%20rows%22%2C%20lambda%20t%3A%20t%5B%22boat_reg_no%22%5D.notna()%20%26%20t.duplicated(%5B*CASE%2C%20%22boat_reg_no%22%5D%2C%20keep%3DFalse))%2C%0A%20%20%20%20%20%20%20%20(%22C1%22%2C%20%22catch%22%2C%20catch%2C%20%22refer%22%2C%20%22species%20is%20not%20in%20the%20code%20list%22%2C%20lambda%20t%3A%20~t%5B%22catch_species%22%5D.isin(_species))%2C%0A%20%20%20%20%20%20%20%20(%22C2%22%2C%20%22catch%22%2C%20_catch%2C%20%22refer%22%2C%20%22catch%20row%20matches%20no%20unit%22%2C%20lambda%20t%3A%20t%5B%22_merge%22%5D%20%3D%3D%20%22left_only%22)%2C%0A%20%20%20%20%20%20%20%20(%22C3%22%2C%20%22fishing_unit%22%2C%20_caught%2C%20%22refer%22%2C%20%22unit%20has%20no%20catch%20row%22%2C%20lambda%20t%3A%20t%5B%22_merge%22%5D%20%3D%3D%20%22left_only%22)%2C%0A%20%20%20%20%20%20%20%20(%22C4%22%2C%20%22catch%22%2C%20catch%2C%20%22edit%22%2C%20%22a%20species%20is%20there%20twice%20for%20a%20unit%22%2C%20lambda%20t%3A%20t.duplicated(%5B*UNIT%2C%20%22catch_species%22%5D%2C%20keep%3DFalse))%2C%0A%20%20%20%20%20%20%20%20(%22W1%22%2C%20%22crew%22%2C%20_crew%2C%20%22edit%22%2C%20%22crew%20member's%20sex%20is%20not%201%20or%202%22%2C%20lambda%20t%3A%20~t%5B%22crew_sex%22%5D.isin(%5B1%2C%202%5D))%2C%0A%20%20%20%20%5D%0A%20%20%20%20%23%20The%20failure%20report%2C%20in%20module%204's%20shape%3A%20one%20row%20per%20rule%20and%20household%2C%20with%20how%20many%20of%20its%20rows%20failed%0A%20%20%20%20failure_report%20%3D%20pd.concat(%0A%20%20%20%20%20%20%20%20%5Brows.loc%5Btest(rows).fillna(False).astype(bool)%5D.groupby(CASE).size().rename(%22rows%22).reset_index()%0A%20%20%20%20%20%20%20%20%20.assign(rule%3Drule%2C%20table%3Dname%2C%20action%3Daction%2C%20message%3Dmessage)%0A%20%20%20%20%20%20%20%20%20for%20rule%2C%20name%2C%20rows%2C%20action%2C%20message%2C%20test%20in%20rules%5D%2C%0A%20%20%20%20%20%20%20%20ignore_index%3DTrue%2C%0A%20%20%20%20)%5B%5B%22rule%22%2C%20%22table%22%2C%20*CASE%2C%20%22rows%22%2C%20%22action%22%2C%20%22message%22%5D%5D%0A%20%20%20%20failure_report.head()%0A%20%20%20%20return%20(failure_report%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20The%20failure%20report%20has%20module%204's%20columns%3A%20%60rule%60%2C%20%60table%60%2C%20%60region_code%60%2C%20%60hh_id%60%2C%20%60rows%60%20(how%20many%20of%20the%20household's%20rows%20fail%20it)%2C%20%60action%60%20(%60edit%60%2C%20where%20a%20rule%20says%20what%20the%20value%20should%20be%2C%20or%20%60refer%60)%20and%20%60message%60.%20If%20you%20built%20your%20own%20report%20in%20module%204%2C%20it%20can%20take%20this%20one's%20place.%20The%20review%20table%20needs%20it%20in%20the%20long%20shape%20of%20the%20other%20sources%2C%20one%20row%20per%20household%20and%20flag%3B%20the%20rule%2C%20its%20message%20and%20its%20action%20make%20the%20flag's%20words%2C%20so%20a%20reviewer%20sees%20what%20module%204%20already%20decided%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(failure_report)%3A%0A%20%20%20%20%23%20Module%204's%20failure%20report%20as%20flags%3A%20the%20household%2C%20source%20%22rule%22%2C%20and%20the%20rule%2C%20its%20message%20and%20its%20action%20in%20words%0A%20%20%20%20rule_flags%20%3D%20failure_report%5BCASE%5D.assign(%0A%20%20%20%20%20%20%20%20source%3D%22rule%22%2C%0A%20%20%20%20%20%20%20%20flag%3Dfailure_report%5B%22rule%22%5D%20%2B%20%22%20%22%20%2B%20failure_report%5B%22message%22%5D%20%2B%20%22%20(%22%20%2B%20failure_report%5B%22action%22%5D%20%2B%20%22)%22%2C%0A%20%20%20%20).drop_duplicates()%0A%20%20%20%20rule_flags.groupby(%22flag%22).size().rename(%22households%22).to_frame()%0A%20%20%20%20return%20(rule_flags%2C)%0A%0A%0A%40app.cell%0Adef%20_(boats%2C%20domains%2C%20limits)%3A%0A%20%20%20%20%23%20The%20statistical%20flags%2C%20from%20your%20functions%20in%20sections%201%20to%203%2C%20with%20the%20settings%20those%20sections%20chose%0A%20%20%20%20def%20_statistic_flags()%3A%0A%20%20%20%20%20%20%20%20_labels%20%3D%20attempt(boat_label%2C%20boats%2C%20limits)%0A%20%20%20%20%20%20%20%20_far%20%3D%20attempt(iqr_outliers%2C%20boats%5B%22length_m%22%5D%2C%20boats%5B%22boat_hull%22%5D%2C%203.0)%0A%20%20%20%20%20%20%20%20_per_meter%20%3D%20attempt(ratio_outliers%2C%20boats%5B%22boat_tonnage%22%5D%2C%20boats%5B%22length_m%22%5D%2C%20boats%5B%22boat_hull%22%5D%2C%203.5)%0A%20%20%20%20%20%20%20%20_size%20%3D%20attempt(robust_z%2C%20households%5B%22hh_roster_members%22%5D.astype(%22float64%22)%2C%20households%5B%22sel_stratum%22%5D)%0A%20%20%20%20%20%20%20%20_effects%20%3D%20attempt(hb_effects%2C%20households%5B%22sel_final_weight%22%5D%2C%20households%5B%22sel_design_weight%22%5D%2C%20PROVINCE%2C%200.5)%0A%20%20%20%20%20%20%20%20_hb%20%3D%20NOT_YET%20if%20_effects%20is%20NOT_YET%20else%20attempt(hb_outliers%2C%20_effects%2C%20PROVINCE%2C%206.0%2C%200.05)%0A%20%20%20%20%20%20%20%20_influence%20%3D%20attempt(weight_influence%2C%20domains%5B%22sel_final_weight%22%5D%2C%20domains%5B%22domain%22%5D)%0A%20%20%20%20%20%20%20%20if%20any(result%20is%20NOT_YET%20for%20result%20in%20(_labels%2C%20_far%2C%20_per_meter%2C%20_size%2C%20_hb%2C%20_influence))%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20NOT_YET%0A%20%20%20%20%20%20%20%20found%20%3D%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20(boats%2C%20_labels%20%3D%3D%20%22implausible%22%2C%20%22length%20implausible%20for%20its%20hull%22)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(boats%2C%20_far%2C%20%22length%20far%20out%20for%20its%20hull%20(IQR%2C%20k%20%3D%203)%22)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(boats%2C%20_per_meter%2C%20%22tonnage%20per%20meter%20out%20of%20line%20with%20its%20hull%22)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(households%2C%20_size.abs()%20%3E%203.5%2C%20%22household%20size%20unusual%20for%20its%20stratum%22)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(households%2C%20_hb%2C%20%22final%20weight%20out%20of%20line%20with%20the%20design%20weight%20(HB)%22)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(domains%2C%20_influence%5B%22vs_median%22%5D%20%3E%204%2C%20%22weight%20over%20four%20times%20its%20domain's%20median%22)%2C%0A%20%20%20%20%20%20%20%20%5D%0A%20%20%20%20%20%20%20%20return%20pd.concat(%5Btable.loc%5Bmask.fillna(False).astype(bool)%2C%20CASE%5D.drop_duplicates().assign(source%3D%22statistic%22%2C%20flag%3Dwords)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20for%20table%2C%20mask%2C%20words%20in%20found%5D%2C%20ignore_index%3DTrue)%0A%0A%0A%20%20%20%20statistic_flags%20%3D%20_statistic_flags()%0A%20%20%20%20mo.stop(statistic_flags%20is%20NOT_YET%2C%20waiting_for(%22Exercises%208.1%20to%208.7%22))%0A%20%20%20%20statistic_flags.groupby(%22flag%22).size().rename(%22households%22).to_frame()%0A%20%20%20%20return%20(statistic_flags%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20**Ranking.**%20Which%20household%20first%3F%20Selective%20editing%2C%20as%20statistics%20offices%20practice%20it%2C%20gives%20each%20item%20of%20a%20unit%20a%20local%20score%20and%20combines%20them%20into%20a%20global%20score%20for%20the%20unit%20(INE%20Spain's%20working%20paper%2004%2F2014%20describes%20the%20approach).%20The%20review%20table%20here%20uses%20the%20simplest%20such%20score%2C%20three%20keys%20in%20order%3A%0A%0A%20%20%20%201.%20**sources**%3A%20how%20many%20different%20kinds%20of%20check%20flagged%20the%20household%20(rule%2C%20statistic%2C%20interviewer).%20Two%20kinds%20agreeing%20is%20the%20strongest%20sign%20that%20something%20is%20wrong.%0A%20%20%20%202.%20**flags**%3A%20how%20many%20different%20flags%20it%20has.%0A%20%20%20%203.%20**influence**%3A%20its%20final%20weight%2C%20since%20a%20wrong%20answer%20counts%20as%20many%20times%20as%20the%20household's%20weight%20in%20every%20estimate.%0A%0A%20%20%20%20Ties%20after%20all%20three%20are%20broken%20by%20region%20and%20household%20id%2C%20so%20the%20order%20is%20the%20same%20every%20time%20the%20table%20is%20built.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%20Exercise%208.10.%20One%20row%20per%20household%2C%20ranked%0A%0A%20%20%20%20**Why%20it%20matters%3A**%20this%20is%20the%20table%20the%20reviewer%20works%20through%2C%20top%20to%20bottom%2C%20and%20the%20order%20is%20where%20the%20reviewer's%20day%20goes.%0A%0A%20%20%20%20**Input%20%E2%86%92%20output%3A**%20a%20long%20%60flags%60%20table%20(%60region_code%60%2C%20%60hh_id%60%2C%20%60source%60%2C%20%60flag%60%3B%20a%20household%20can%20have%20the%20same%20flag%20twice%2C%20from%20two%20boats)%20and%20a%20%60weights%60%20table%20(%60region_code%60%2C%20%60hh_id%60%2C%20%60sel_final_weight%60%2C%20one%20row%20per%20household)%20%E2%86%92%20one%20row%20per%20flagged%20household%20with%3A%0A%0A%20%20%20%20%7C%20column%20%7C%20what%20it%20holds%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20%60region_code%60%2C%20%60hh_id%60%20%7C%20the%20household%20%7C%0A%20%20%20%20%7C%20%60sources%60%20%7C%20the%20number%20of%20different%20sources%20%7C%0A%20%20%20%20%7C%20%60flags%60%20%7C%20the%20number%20of%20different%20flags%20%7C%0A%20%20%20%20%7C%20%60reasons%60%20%7C%20the%20different%20flags%2C%20sorted%2C%20joined%20with%20%60%22%3B%20%22%60%20%7C%0A%20%20%20%20%7C%20%60sel_final_weight%60%20%7C%20the%20household's%20final%20weight%20%7C%0A%20%20%20%20%7C%20%60rank%60%20%7C%201%2C%202%2C%203%2C%20%E2%80%A6%20in%20the%20table's%20order%20%7C%0A%0A%20%20%20%20sorted%20by%20%60sources%60%2C%20then%20%60flags%60%2C%20then%20%60sel_final_weight%60%2C%20each%20**largest%20first**%2C%20then%20%60region_code%60%20and%20%60hh_id%60%2C%20each%20**smallest%20first**%3A%0A%0A%20%20%20%20%7C%20region_code%20%7C%20hh_id%20%7C%20sources%20%7C%20flags%20%7C%20reasons%20%7C%20sel_final_weight%20%7C%20rank%20%7C%0A%20%20%20%20%7C%3A--%7C--%3A%7C--%3A%7C--%3A%7C%3A--%7C--%3A%7C--%3A%7C%0A%20%20%20%20%7C%2001%20%7C%201%20%7C%202%20%7C%202%20%7C%20M1%20sex%3B%20weight%20high%20%7C%2020.0%20%7C%201%20%7C%0A%20%20%20%20%7C%2004%20%7C%207%20%7C%201%20%7C%202%20%7C%20crowded%20day%3B%20short%20interview%20%7C%201.0%20%7C%202%20%7C%0A%20%20%20%20%7C%2004%20%7C%202%20%7C%201%20%7C%201%20%7C%20M1%20sex%20%7C%2050.0%20%7C%203%20%7C%0A%20%20%20%20%7C%2007%20%7C%201%20%7C%201%20%7C%201%20%7C%20C1%20species%20%7C%2050.0%20%7C%204%20%7C%0A%20%20%20%20%7C%2001%20%7C%202%20%7C%201%20%7C%201%20%7C%20weight%20high%20%7C%2021.0%20%7C%205%20%7C%0A%0A%20%20%20%20Household%2001-1%20has%20%60M1%20sex%60%20from%20two%20members%3A%20it%20counts%20once.%20Households%2004-2%20and%2007-1%20tie%20on%20everything%20but%20the%20region.%20Household%2007-2%20has%20a%20weight%20and%20no%20flag%2C%20so%20it%20has%20no%20row.%0A%0A%20%20%20%20**Tools%3A**%20%60groupby(CASE).agg()%60%20with%20%60%22nunique%22%60%20and%20a%20%60lambda%60%20that%20joins%2C%20%60merge%60%2C%20%60sort_values(by%3D%5B...%5D%2C%20ascending%3D%5B...%5D)%60%2C%20above.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20review_table(flags%3A%20pd.DataFrame%2C%20weights%3A%20pd.DataFrame)%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22One%20row%20per%20flagged%20household%3A%20sources%2C%20flags%2C%20reasons%2C%20sel_final_weight%20and%20rank%2C%20the%20likeliest%20errors%20first.%22%22%22%0A%20%20%20%20%23%20Exercise%208.10%3A%20the%20task%20and%20examples%20are%20in%20the%20text%20above%20this%20cell.%0A%20%20%20%20%23%20Step%201%3A%20per%20household%20(groupby%20CASE)%3A%20sources%20(nunique%20of%20source)%2C%20flags%20(nunique%20of%20flag)%2C%20reasons%20(the%0A%20%20%20%20%23%20%20%20%20%20%20%20%20%20different%20flags%2C%20sorted%2C%20joined%20with%20%22%3B%20%22)%3B%20reset%20the%20index%0A%20%20%20%20%23%20Step%202%3A%20add%20each%20household's%20sel_final_weight%20from%20weights%20(merge%20on%20CASE)%0A%20%20%20%20%23%20Step%203%3A%20sort%20by%20sources%2C%20flags%2C%20sel_final_weight%20(largest%20first)%2C%20then%20region_code%20and%20hh_id%20(smallest%20first)%2C%0A%20%20%20%20%23%20%20%20%20%20%20%20%20%20number%20the%20rows%20from%200%20again%2C%20add%20rank%20%3D%201%2C%202%2C%203%2C%20...%2C%20and%20return%0A%20%20%20%20raise%20NotImplementedError(%22Exercise%208.10%22)%0A%0A%0A%40app.function%0Adef%20flags_fixture()%20-%3E%20tuple%5Bpd.DataFrame%2C%20pd.DataFrame%5D%3A%0A%20%20%20%20%22%22%22Eight%20flags%20on%20five%20households%2C%20and%20their%20weights%3A%20a%20household%20with%20one%20rule%20failure%20on%20two%20members%2C%20two%0A%20%20%20%20households%20tied%20on%20sources%2C%20flags%20and%20weight%2C%20an%20interviewer%20household%20with%20two%20flags%2C%20and%20a%20household%20with%20no%20flag.%22%22%22%0A%20%20%20%20flags%20%3D%20pd.DataFrame(%7B%0A%20%20%20%20%20%20%20%20%22region_code%22%3A%20%5B%2201%22%2C%20%2201%22%2C%20%2201%22%2C%20%2204%22%2C%20%2204%22%2C%20%2207%22%2C%20%2204%22%2C%20%2201%22%5D%2C%0A%20%20%20%20%20%20%20%20%22hh_id%22%3A%20%5B1%2C%201%2C%201%2C%207%2C%207%2C%201%2C%202%2C%202%5D%2C%0A%20%20%20%20%20%20%20%20%22source%22%3A%20%5B%22rule%22%2C%20%22rule%22%2C%20%22statistic%22%2C%20%22interviewer%22%2C%20%22interviewer%22%2C%20%22rule%22%2C%20%22rule%22%2C%20%22statistic%22%5D%2C%0A%20%20%20%20%20%20%20%20%22flag%22%3A%20%5B%22M1%20sex%22%2C%20%22M1%20sex%22%2C%20%22weight%20high%22%2C%20%22short%20interview%22%2C%20%22crowded%20day%22%2C%20%22C1%20species%22%2C%20%22M1%20sex%22%2C%20%22weight%20high%22%5D%2C%0A%20%20%20%20%7D)%0A%20%20%20%20weights%20%3D%20pd.DataFrame(%7B%0A%20%20%20%20%20%20%20%20%22region_code%22%3A%20%5B%2201%22%2C%20%2201%22%2C%20%2204%22%2C%20%2204%22%2C%20%2207%22%2C%20%2207%22%5D%2C%0A%20%20%20%20%20%20%20%20%22hh_id%22%3A%20%5B1%2C%202%2C%202%2C%207%2C%201%2C%202%5D%2C%0A%20%20%20%20%20%20%20%20%22sel_final_weight%22%3A%20%5B20.0%2C%2021.0%2C%2050.0%2C%201.0%2C%2050.0%2C%203.0%5D%2C%0A%20%20%20%20%7D)%0A%20%20%20%20return%20flags%2C%20weights%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(interviewer_flags%2C%20rule_flags)%3A%0A%20%20%20%20_flags%2C%20_weights%20%3D%20flags_fixture()%0A%20%20%20%20_survey_weights%20%3D%20households%5B%5B*CASE%2C%20%22sel_final_weight%22%5D%5D%0A%20%20%20%20check_cases(%0A%20%20%20%20%20%20%20%20%22Exercise%208.10%22%2C%20review_table%2C%0A%20%20%20%20%20%20%20%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22eight%20flags%20on%20five%20households%3A%20a%20flag%20twice%20on%20one%20household%2C%20ties%20on%20sources%2C%20flags%20and%20weight%22%2C%20(_flags%2C%20_weights))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22the%20survey's%20rule%20and%20interviewer%20flags%22%2C%20(pd.concat(%5Brule_flags%2C%20interviewer_flags%5D%2C%20ignore_index%3DTrue)%2C%20_survey_weights))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22region%2007's%20rule%20flags%22%2C%20(rule_flags%5Brule_flags%5B%22region_code%22%5D%20%3D%3D%20%2207%22%5D%2C%20_survey_weights))%2C%0A%20%20%20%20%20%20%20%20%5D%2C%0A%20%20%20%20%20%20%20%20what%3D%22sets%20of%20flags%22%2C%20summarize%3Dlambda%20table%3A%20as_rows(table%2C%20%22rank%22)%2C%0A%20%20%20%20%20%20%20%20hint%3D%22Count%20different%20sources%20and%20flags%20per%20household%20(nunique)%2C%20join%20the%20different%20flags%20sorted%3B%20sort%20by%20sources%2C%20flags%2C%20weight%20(largest%20first)%2C%20then%20region%20and%20id%3B%20number%20the%20rows%20from%201.%22%2C%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(interviewer_flags%2C%20rule_flags)%3A%0A%20%20%20%20%23%20Try%20it%3A%20the%20review%20table%20of%20the%20rules%20and%20interviewer%20flags%2C%20its%20first%20rows%0A%20%20%20%20try_it(review_table%2C%20pd.concat(%5Brule_flags%2C%20interviewer_flags%5D%2C%20ignore_index%3DTrue)%2C%20households%5B%5B*CASE%2C%20%22sel_final_weight%22%5D%5D)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.accordion(%7B%22If%20the%20check%20is%20red%3A%20common%20causes%20for%20Exercise%208.10%22%3A%20mo.md(r%22%22%22%0A%20%20%20%20%7C%20what%20you%20see%20%7C%20the%20usual%20cause%20%7C%20look%20at%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20Household%2001-1%20has%20%60flags%60%203%20and%20%60M1%20sex%3B%20M1%20sex%3B%20weight%20high%60%20%7C%20The%20same%20flag%20counted%20twice.%20%7C%20%60%22nunique%22%60%2C%20and%20%60sorted(set(f))%60%20%7C%0A%20%20%20%20%7C%2004-2%20and%2007-1%20in%20the%20other%20order%20%7C%20The%20tie%20on%20sources%2C%20flags%20and%20weight%20is%20broken%20by%20row%20order.%20%7C%20%60region_code%60%20and%20%60hh_id%60%20as%20the%20last%20keys%2C%20smallest%20first%20%7C%0A%20%20%20%20%7C%20The%20smallest%20weights%20first%20%7C%20One%20%60ascending%3DFalse%60%20for%20every%20key%2C%20or%20%60True%60%20for%20all.%20%7C%20%60ascending%3D%5BFalse%2C%20False%2C%20False%2C%20True%2C%20True%5D%60%20%7C%0A%20%20%20%20%7C%20%60rank%60%20not%201%2C%202%2C%203%20in%20order%20%7C%20The%20rank%20was%20set%20before%20sorting%2C%20or%20from%20the%20old%20index.%20%7C%20%60reset_index(drop%3DTrue)%60%20after%20sorting%20%7C%0A%20%20%20%20%7C%20The%20household%20with%20no%20flag%20has%20a%20row%20%7C%20The%20weights%20table%20was%20the%20left%20side%20of%20the%20join.%20%7C%20start%20from%20the%20flags%20%7C%0A%0A%20%20%20%20Still%20stuck%20after%20ten%20minutes%3F%20Ask%20your%20table%2C%20then%20raise%20your%20hand.%0A%20%20%20%20%22%22%22)%7D)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%20Judging%20the%20table%20against%20the%20planted%20defects%0A%0A%20%20%20%20**Recall**%20is%20the%20share%20of%20the%20planted%20defects%20of%20a%20kind%20that%20the%20table%20finds%3A%20found%20%2F%20planted.%20A%20reviewer%20reads%20from%20the%20top%20and%20may%20stop%20before%20the%20end%2C%20so%20recall%20depends%20on%20how%20many%20rows%20are%20read.%20Reading%20every%20row%20finds%20everything%20the%20checks%20flag%3B%20the%20question%20is%20how%20far%20down%20each%20kind%20sits.%0A%0A%20%20%20%20The%20list%20needs%20two%20adjustments%20before%20it%20judges%20anything.%20Its%20%60household_removed%60%20and%20%60row_voided%60%20kinds%20are%20what%20module%202's%20filters%20leave%20out%2C%20so%20they%20are%20not%20in%20the%20tables%20at%20all.%20And%20a%20household%20can%20carry%20two%20kinds%20(a%20household%20with%20a%20boat%20error%20can%20also%20be%20one%20of%20an%20area%20stratum%20that%20runs%20a%20large-scale%20unit)%2C%20so%20the%20list%20is%20compared%20household%20by%20household%20within%20each%20kind%2C%20never%20by%20its%20row%20count.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20%23%20The%20planted%20defects%20a%20reviewer%20could%20be%20judged%20against%3A%20kinds%20that%20remain%20in%20the%20clean%20tables%2C%20households%20that%20are%20in%20them%0A%20%20%20%20_kept%20%3D%20defects%5B~defects%5B%22kind%22%5D.isin(%5B%22household_removed%22%2C%20%22row_voided%22%5D)%5D%0A%20%20%20%20defects_to_find%20%3D%20_kept.merge(households%5BCASE%5D%2C%20on%3DCASE%2C%20how%3D%22inner%22)%0A%20%20%20%20_left_out%20%3D%20_kept.merge(households%5BCASE%5D%2C%20on%3DCASE%2C%20how%3D%22left%22%2C%20indicator%3DTrue)%0A%20%20%20%20_outside%20%3D%20int((_left_out%5B%22_merge%22%5D%20%3D%3D%20%22left_only%22).sum())%0A%20%20%20%20mo.md(f%22%7Blen(defects_to_find)%7D%20planted%20defects%20remain%20in%20the%20clean%20tables.%20%22%20%2B%20(%0A%20%20%20%20%20%20%20%20f%22%7B_outside%7D%20more%20are%20on%20households%20the%20removal%20filters%20left%20out%20(a%20household%20can%20be%20removed%20and%20carry%20another%20kind%20too).%22%20if%20_outside%0A%20%20%20%20%20%20%20%20else%20%22None%20of%20the%20other%20kinds%20is%20on%20a%20household%20the%20removal%20filters%20left%20out%2C%20so%20nothing%20else%20drops%20out.%22))%0A%20%20%20%20return%20(defects_to_find%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%20Exercise%208.11.%20Recall%20by%20kind%2C%20for%20the%20rows%20a%20reviewer%20reads%0A%0A%20%20%20%20**Why%20it%20matters%3A**%20%22the%20table%20finds%2090%25%20of%20the%20errors%22%20hides%20which%20errors.%20Recall%20by%20kind%20shows%20what%20the%20checks%20miss%20and%20how%20deep%20each%20kind%20sits%2C%20which%20tells%20a%20survey%20team%20where%20a%20new%20check%20would%20help%20and%20how%20many%20rows%20a%20reviewer%20must%20read.%0A%0A%20%20%20%20**Input%20%E2%86%92%20output%3A**%20a%20%60review%60%20table%20(one%20row%20per%20household%2C%20in%20reading%20order%3B%20it%20needs%20%60region_code%60%20and%20%60hh_id%60)%2C%20the%20planted%20%60defects%60%20(%60region_code%60%2C%20%60hh_id%60%2C%20%60kind%60)%20and%20%60read%60%2C%20how%20many%20rows%20from%20the%20top%20are%20read%20%E2%86%92%20one%20row%20per%20**kind%20of%20the%20defects**%2C%20with%3A%0A%0A%20%20%20%20-%20%60planted%60%3A%20the%20households%20of%20that%20kind%20(each%20household%20once%20per%20kind)%3B%0A%20%20%20%20-%20%60found%60%3A%20how%20many%20of%20them%20are%20in%20the%20first%20%60read%60%20rows%20of%20the%20review%3B%0A%20%20%20%20-%20%60recall%60%3A%20found%20%2F%20planted.%0A%0A%20%20%20%20Every%20kind%20of%20the%20defects%20has%20a%20row%2C%20**including%20those%20with%20nothing%20found**%20(%60found%60%200%2C%20%60recall%60%200).%0A%0A%20%20%20%20%7C%20kind%20%7C%20planted%20%7C%20found%20(read%20%3D%202)%20%7C%20recall%20%7C%0A%20%20%20%20%7C%3A--%7C--%3A%7C--%3A%7C--%3A%7C%0A%20%20%20%20%7C%20final_weight_inflated%20%7C%202%20%7C%201%20%7C%200.5%20%7C%0A%20%20%20%20%7C%20code_not_in_list%20%7C%201%20%7C%200%20%7C%200.0%20%7C%0A%0A%20%20%20%20**Tools%3A**%20%60head(read)%60%2C%20%60merge(...%2C%20how%3D%22left%22%2C%20indicator%3DTrue)%60%20or%20a%20marker%20column%2C%20%60drop_duplicates%60%2C%20%60groupby(%22kind%22).agg()%60.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20recall_by_kind(review%3A%20pd.DataFrame%2C%20defects%3A%20pd.DataFrame%2C%20read%3A%20int)%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22One%20row%20per%20kind%20of%20the%20defects%3A%20planted%2C%20found%20in%20the%20first%20%60read%60%20rows%20of%20the%20review%2C%20and%20recall.%22%22%22%0A%20%20%20%20%23%20Exercise%208.11%3A%20the%20task%20and%20examples%20are%20in%20the%20text%20above%20this%20cell.%0A%20%20%20%20%23%20Step%201%3A%20the%20households%20read%3A%20the%20first%20%60read%60%20rows%20of%20the%20review%2C%20CASE%20columns%20only%2C%20with%20a%20marker%20column%0A%20%20%20%20%23%20Step%202%3A%20the%20defects'%20households%2C%20each%20once%20per%20kind%20(drop_duplicates%20on%20CASE%20and%20kind)%2C%20joined%20to%20the%0A%20%20%20%20%23%20%20%20%20%20%20%20%20%20households%20read%2C%20keeping%20every%20defect%20row%20(how%3D%22left%22)%0A%20%20%20%20%23%20Step%203%3A%20per%20kind%3A%20planted%20(rows)%2C%20found%20(rows%20with%20the%20marker)%2C%20recall%20%3D%20found%20%2F%20planted%3B%20return%20it%0A%20%20%20%20raise%20NotImplementedError(%22Exercise%208.11%22)%0A%0A%0A%40app.function%0Adef%20recall_fixture()%20-%3E%20tuple%5Bpd.DataFrame%2C%20pd.DataFrame%5D%3A%0A%20%20%20%20%22%22%22A%20review%20of%20four%20households%20in%20reading%20order%20and%20seven%20planted%20defects%3A%20a%20household%20listed%20twice%20for%20one%20kind%2C%20a%0A%20%20%20%20household%20with%20two%20kinds%2C%20and%20a%20kind%20no%20row%20of%20the%20review%20has.%22%22%22%0A%20%20%20%20review%20%3D%20pd.DataFrame(%7B%22region_code%22%3A%20%5B%2201%22%2C%20%2204%22%2C%20%2207%22%2C%20%2201%22%5D%2C%20%22hh_id%22%3A%20%5B1%2C%201%2C%203%2C%204%5D%2C%20%22rank%22%3A%20%5B1%2C%202%2C%203%2C%204%5D%7D)%0A%20%20%20%20planted%20%3D%20pd.DataFrame(%7B%0A%20%20%20%20%20%20%20%20%22region_code%22%3A%20%5B%2201%22%2C%20%2201%22%2C%20%2207%22%2C%20%2204%22%2C%20%2204%22%2C%20%2207%22%2C%20%2207%22%5D%2C%0A%20%20%20%20%20%20%20%20%22hh_id%22%3A%20%5B1%2C%201%2C%203%2C%201%2C%208%2C%209%2C%203%5D%2C%0A%20%20%20%20%20%20%20%20%22kind%22%3A%20%5B%22final_weight_inflated%22%2C%20%22final_weight_inflated%22%2C%20%22final_weight_inflated%22%2C%20%22large_unit_in_area_stratum%22%2C%20%22code_not_in_list%22%2C%20%22large_unit_in_area_stratum%22%2C%20%22large_unit_in_area_stratum%22%5D%2C%0A%20%20%20%20%7D)%0A%20%20%20%20return%20review%2C%20planted%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(defects_to_find%2C%20interviewer_flags%2C%20rule_flags)%3A%0A%20%20%20%20_review%2C%20_planted%20%3D%20recall_fixture()%0A%20%20%20%20_order%20%3D%20pd.concat(%5Binterviewer_flags%2C%20rule_flags%5D%2C%20ignore_index%3DTrue)%5BCASE%5D.drop_duplicates()%20%20%23%20a%20reading%20order%20that%20needs%20no%20exercise%0A%20%20%20%20check_cases(%0A%20%20%20%20%20%20%20%20%22Exercise%208.11%22%2C%20recall_by_kind%2C%0A%20%20%20%20%20%20%20%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22a%20review%20of%20four%20households%2C%20read%20%3D%202%3A%20a%20household%20listed%20twice%2C%20a%20household%20of%20two%20kinds%2C%20a%20kind%20not%20found%22%2C%20(_review%2C%20_planted%2C%202))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22the%20same%2C%20read%20%3D%204%22%2C%20(_review%2C%20_planted%2C%204))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22the%20interviewer%20and%20rule%20flags'%20households%20against%20the%20survey's%20planted%20defects%2C%20read%20%3D%2060%22%2C%20(_order%2C%20defects_to_find%2C%2060))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22the%20same%2C%20every%20row%20read%22%2C%20(_order%2C%20defects_to_find%2C%20len(_order)))%2C%0A%20%20%20%20%20%20%20%20%5D%2C%0A%20%20%20%20%20%20%20%20what%3D%22reviews%20and%20reading%20depths%22%2C%20summarize%3Dlambda%20table%3A%20as_rows(table%2C%20%22kind%22)%2C%0A%20%20%20%20%20%20%20%20hint%3D%22Mark%20the%20first%20%60read%60%20households%20of%20the%20review%2C%20left-join%20the%20marks%20onto%20the%20planted%20defects%20(each%20household%20once%20per%20kind)%2C%20and%20count%20per%20kind.%22%2C%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(defects_to_find%2C%20interviewer_flags%2C%20rule_flags)%3A%0A%20%20%20%20%23%20Try%20it%3A%20recall%20by%20kind%20when%20a%20reviewer%20reads%20every%20household%20the%20rules%20and%20interviewer%20flags%20raised%0A%20%20%20%20_order%20%3D%20pd.concat(%5Brule_flags%2C%20interviewer_flags%5D%2C%20ignore_index%3DTrue)%5BCASE%5D.drop_duplicates()%0A%20%20%20%20try_it(recall_by_kind%2C%20_order%2C%20defects_to_find%2C%20len(_order))%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.accordion(%7B%22If%20the%20check%20is%20red%3A%20common%20causes%20for%20Exercise%208.11%22%3A%20mo.md(r%22%22%22%0A%20%20%20%20%7C%20what%20you%20see%20%7C%20the%20usual%20cause%20%7C%20look%20at%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20No%20row%20for%20%60code_not_in_list%60%20%7C%20The%20kinds%20came%20from%20the%20review%2C%20or%20an%20inner%20join%20dropped%20the%20kind%20nothing%20found.%20%7C%20the%20defects%20on%20the%20left%2C%20%60how%3D%22left%22%60%20%7C%0A%20%20%20%20%7C%20%60final_weight_inflated%60%20planted%203%2C%20not%202%20%7C%20Household%2001-1%2C%20listed%20twice%20for%20one%20kind%2C%20was%20counted%20twice.%20%7C%20%60drop_duplicates()%60%20on%20the%20defects'%20household%20and%20kind%20%7C%0A%20%20%20%20%7C%20%60found%60%20equals%20%60planted%60%20in%20every%20row%20%7C%20%60%22size%22%60%20counts%20every%20row%2C%20marked%20or%20not.%20%7C%20%60(%22found%22%2C%20%22count%22)%60%20%7C%0A%20%20%20%20%7C%20The%20read%20%3D%202%20case%20finds%20as%20much%20as%20read%20%3D%204%20%7C%20The%20whole%20review%20was%20used%2C%20not%20its%20first%20rows.%20%7C%20%60review.head(read)%60%20%7C%0A%20%20%20%20%7C%20*MergeError*%20or%20a%20household%20counted%20twice%20%7C%20The%20households%20read%20were%20not%20made%20unique%20before%20the%20join.%20%7C%20%60drop_duplicates()%60%20on%20the%20households%20read%20%7C%0A%0A%20%20%20%20Still%20stuck%20after%20ten%20minutes%3F%20Ask%20your%20table%2C%20then%20raise%20your%20hand.%0A%20%20%20%20%22%22%22)%7D)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(defects_to_find%2C%20interviewer_flags%2C%20rule_flags%2C%20statistic_flags)%3A%0A%20%20%20%20%23%20The%20review%20table%20of%20every%20flag%2C%20and%20its%20recall%20by%20kind%20at%20four%20reading%20depths%0A%20%20%20%20all_flags%20%3D%20pd.concat(%5Brule_flags%2C%20statistic_flags%2C%20interviewer_flags%5D%2C%20ignore_index%3DTrue)%0A%20%20%20%20review%20%3D%20attempt(review_table%2C%20all_flags%2C%20households%5B%5B*CASE%2C%20%22sel_final_weight%22%5D%5D)%0A%20%20%20%20mo.stop(review%20is%20NOT_YET%2C%20waiting_for(%22Exercise%208.10%22))%0A%20%20%20%20_depths%20%3D%20%5B50%2C%20100%2C%20150%2C%20len(review)%5D%0A%20%20%20%20_recalls%20%3D%20%7Bdepth%3A%20attempt(recall_by_kind%2C%20review%2C%20defects_to_find%2C%20depth)%20for%20depth%20in%20_depths%7D%0A%20%20%20%20mo.stop(any(table%20is%20NOT_YET%20for%20table%20in%20_recalls.values())%2C%20waiting_for(%22Exercise%208.11%22))%0A%20%20%20%20_found%20%3D%20pd.concat(%7Bf%22first%20%7Bdepth%7D%22%20if%20depth%20%3C%20len(review)%20else%20f%22all%20%7Bdepth%7D%22%3A%20table%5B%22found%22%5D.astype(str)%20%2B%20%22%20%2F%20%22%20%2B%20table%5B%22planted%22%5D.astype(str)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20for%20depth%2C%20table%20in%20_recalls.items()%7D%2C%20axis%3D1)%0A%20%20%20%20mo.vstack(%5B%0A%20%20%20%20%20%20%20%20mo.md(f%22**%7Blen(all_flags)%7D%20flags%20on%20%7Blen(review)%7D%20households.**%20Found%20%2F%20planted%2C%20by%20kind%2C%20after%20reading%20the%20first%20rows%20of%20the%20review%20table%3A%22)%2C%0A%20%20%20%20%20%20%20%20_found%2C%0A%20%20%20%20%5D)%0A%20%20%20%20return%20all_flags%2C%20review%0A%0A%0A%40app.cell%0Adef%20_(defects_to_find%2C%20review)%3A%0A%20%20%20%20%23%20Recall%20and%20precision%20as%20a%20reviewer%20reads%20down%20the%20table%20(matplotlib)%0A%20%20%20%20_planted_households%20%3D%20defects_to_find%5BCASE%5D.drop_duplicates().assign(planted%3DTrue)%0A%20%20%20%20_marked%20%3D%20review%5BCASE%5D.merge(_planted_households%2C%20on%3DCASE%2C%20how%3D%22left%22)%5B%22planted%22%5D.fillna(False).astype(bool)%0A%20%20%20%20_read%20%3D%20np.arange(1%2C%20len(review)%20%2B%201)%0A%20%20%20%20_recall%20%3D%20_marked.cumsum().to_numpy()%20%2F%20len(_planted_households)%0A%20%20%20%20_precision%20%3D%20_marked.cumsum().to_numpy()%20%2F%20_read%0A%20%20%20%20_fig%2C%20_ax%20%3D%20plt.subplots(figsize%3D(6.5%2C%203.4))%0A%20%20%20%20_ax.plot(_read%2C%20_recall%2C%20color%3D%22%234c78a8%22%2C%20label%3D%22planted%20households%20found%20(recall)%22)%0A%20%20%20%20_ax.plot(_read%2C%20_precision%2C%20color%3D%22%23e4572e%22%2C%20label%3D%22rows%20read%20that%20are%20planted%20(precision)%22)%0A%20%20%20%20_ax.set_ylim(0%2C%201.02)%0A%20%20%20%20_ax.set_xlabel(%22rows%20of%20the%20review%20table%20read%2C%20from%20the%20top%22)%0A%20%20%20%20_ax.set_ylabel(%22share%22)%0A%20%20%20%20_ax.set_title(%22What%20a%20reviewer%20finds%2C%20row%20by%20row%22)%0A%20%20%20%20_ax.legend(frameon%3DFalse%2C%20loc%3D%22lower%20right%22)%0A%20%20%20%20_ax.spines%5B%5B%22top%22%2C%20%22right%22%5D%5D.set_visible(False)%0A%20%20%20%20_fig.tight_layout()%0A%20%20%20%20_ax%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(all_flags%2C%20defects_to_find%2C%20review)%3A%0A%20%20%20%20_planted_households%20%3D%20defects_to_find%5BCASE%5D.drop_duplicates()%0A%20%20%20%20_not_planted%20%3D%20review.merge(_planted_households.assign(planted%3DTrue)%2C%20on%3DCASE%2C%20how%3D%22left%22)%0A%20%20%20%20_not_planted%20%3D%20_not_planted%5B_not_planted%5B%22planted%22%5D.isna()%5D%0A%20%20%20%20_missed%20%3D%20_planted_households.merge(review%5BCASE%5D%2C%20on%3DCASE%2C%20how%3D%22left%22%2C%20indicator%3DTrue)%0A%20%20%20%20_missed%20%3D%20_missed%5B_missed%5B%22_merge%22%5D%20%3D%3D%20%22left_only%22%5D.merge(defects_to_find%2C%20on%3DCASE)%0A%20%20%20%20_by_source%20%3D%20all_flags.drop_duplicates(%5B*CASE%2C%20%22source%22%5D).merge(_planted_households.assign(planted%3DTrue)%2C%20on%3DCASE%2C%20how%3D%22left%22)%0A%20%20%20%20_sources%20%3D%20_by_source.groupby(%22source%22).agg(households%3D(%22planted%22%2C%20%22size%22)%2C%20planted%3D(%22planted%22%2C%20%22count%22))%0A%20%20%20%20_ranks%20%3D%20defects_to_find%5B%5B*CASE%2C%20%22kind%22%5D%5D.drop_duplicates().merge(review%5B%5B*CASE%2C%20%22rank%22%5D%5D%2C%20on%3DCASE)%0A%20%20%20%20_statistical%20%3D%20_ranks%5B_ranks%5B%22kind%22%5D.isin(%5B%22feet_recorded_as_meters%22%2C%20%22made_up_interview%22%2C%20%22final_weight_inflated%22%5D)%5D%5B%22rank%22%5D.max()%0A%20%20%20%20_both%20%3D%20int((review%5B%22sources%22%5D%20%3E%3D%202).sum())%0A%20%20%20%20_missed_kinds%20%3D%20_missed.groupby(%22kind%22).size()%0A%20%20%20%20_found_all%20%3D%20(%22Every%20planted%20household%20still%20in%20the%20tables%20is%20on%20it%22%20if%20len(_missed)%20%3D%3D%200%20else%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20f%22Every%20planted%20household%20still%20in%20the%20tables%20is%20on%20it%20but%20%7B_missed%5BCASE%5D.drop_duplicates().shape%5B0%5D%7D%20(%7B'%2C%20'.join(f'%7Bn%7D%20%7Bkind%7D'%20for%20kind%2C%20n%20in%20_missed_kinds.items())%7D)%22)%0A%20%20%20%20mo.vstack(%5B%0A%20%20%20%20%20%20%20%20mo.md(f%22%22%22%0A%20%20%20%20%20%20%20%20**What%20the%20table%20finds.**%20%7B_found_all%7D%2C%20and%20%7Blen(review)%20-%20len(_not_planted)%7D%20of%20its%20%7Blen(review)%7D%20rows%20are%20planted%20households.%20The%20first%20%7B_both%7D%20rows%20are%20households%20where%20two%20or%20more%20kinds%20of%20check%20agree.%20The%20defects%20no%20rule%20finds%20(the%20boats%20in%20the%20wrong%20unit%2C%20the%20made-up%20interviews%2C%20the%20inflated%20weights)%20are%20all%20within%20the%20first%20%7B_statistical%7D%20rows%2C%20because%20each%20carries%20several%20statistical%20or%20interviewer%20flags.%20Rule%20failures%20that%20nothing%20else%20confirms%20sit%20lower.%20That%20is%20a%20choice%2C%20and%20it%20fits%20a%20survey%20whose%20module%204%20edits%20some%20rule%20failures%20by%20rule%20and%20states%20the%20action%20of%20the%20others%2C%20so%20a%20reviewer's%20time%20goes%20first%20to%20what%20no%20rule%20can%20settle.%0A%0A%20%20%20%20%20%20%20%20**What%20it%20misses**%20is%20found%20elsewhere%20in%20the%20course.%20No%20rule%20of%20the%2025%20reads%20%60unit_ownership%60%2C%20so%20its%20codes%20outside%20the%20list%20are%20module%204's%20frequency%20tables'%20to%20find%3B%20a%20crew%20row%20keyed%20to%20no%20unit%20is%20module%203's%20matching%3B%20an%20area%20in%20two%20strata%20is%20module%204's%20duplicate%20definitions.%20And%20the%20households%20of%20strata%20A%20and%20B%20that%20run%20a%20large-scale%20unit%20weigh%20too%20little%20to%20pass%20four%20times%20the%20large-scale%20domain's%20median%2C%20so%20most%20of%20them%20carry%20no%20flag%20at%20all.%0A%0A%20%20%20%20%20%20%20%20**What%20it%20costs.**%20By%20source%2C%20the%20households%20each%20one%20flags%20and%20how%20many%20of%20them%20are%20planted%3A%0A%20%20%20%20%20%20%20%20%22%22%22)%2C%0A%20%20%20%20%20%20%20%20_sources%2C%0A%20%20%20%20%20%20%20%20mo.md(%22The%20rows%20that%20are%20not%20planted%20households%2C%20by%20their%20reasons%3A%22)%2C%0A%20%20%20%20%20%20%20%20_not_planted%5B%22reasons%22%5D.value_counts().rename(%22rows%22).to_frame()%2C%0A%20%20%20%20%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%20%20%20%20Most%20rows%20here%20are%20not%20mistakes%20of%20the%20checks.%20Large%20households%20are%20unusual%2C%20not%20wrong%3A%20their%20flag%20costs%20a%20reviewer%20a%20look%20each%20and%20finds%20nothing%2C%20which%20is%20the%20price%20of%20a%20check%20for%20the%20unusual.%20A%20crowded%20day%20also%20flags%20the%20interviewer's%20real%20interviews%20of%20that%20day%3A%20the%20flag%20points%20at%20the%20day%2C%20and%20the%20supervisor's%20callback%20tells%20the%20real%20interviews%20from%20the%20others.%20A%20registration%20number%20on%20two%20boat%20rows%20(rule%20B5)%20is%20often%20a%20boat%20two%20units%20of%20the%20household%20honestly%20share%3B%20module%204's%20referral%20list%20sends%20those%20to%20a%20person%20for%20the%20same%20reason.%20The%20interviews%20with%20%22the%20same%20members%20and%20catch%20as%20another%20interview%22%20come%20in%20pairs%3A%20the%20list%20names%20the%20copy%2C%20and%20the%20table%20also%20flags%20the%20original%20it%20was%20copied%20from%2C%20which%20a%20reviewer%20must%20see%20to%20tell%20which%20is%20which.%20A%20list%20of%20planted%20defects%20judges%20a%20table%3B%20it%20does%20not%20replace%20reading%20the%20rows.%0A%20%20%20%20%20%20%20%20%22%22%22)%2C%0A%20%20%20%20%5D)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%20What%20you%20can%20do%20now%0A%0A%20%20%20%20%7C%20to%20%7C%20use%20%7C%20and%20check%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20tell%20invalid%20from%20implausible%20from%20unusual%20%7C%20rules%20for%20invalid%3B%20limits%20or%20statistics%20for%20the%20others%3B%20%60np.select%60%20in%20order%20%7C%20the%20first%20condition%20that%20is%20true%20wins%3A%20test%20the%20hull%20before%20the%20length%20%7C%0A%20%20%20%20%7C%20find%20outliers%20within%20groups%20%7C%20Tukey's%20fences%2C%20the%20robust%20z%20(median%20and%20MAD)%2C%20with%20%60groupby(...).transform%60%20%7C%20the%20group%20is%20the%20comparison%20you%20mean%3B%20a%20MAD%20of%200%20cannot%20measure%20distance%20%7C%0A%20%20%20%20%7C%20compare%20two%20values%20of%20one%20unit%20%7C%20Hidiroglou-Berthelot%3A%20centered%20ratios%2C%20a%20size%20exponent%2C%20quartile%20fences%20with%20a%20minimum%20%7C%20the%20two%20values%20in%20one%20unit%3B%20at%20least%20ten%20units%20per%20group%20%7C%0A%20%20%20%20%7C%20check%20a%20relation%20that%20holds%20approximately%20%7C%20the%20robust%20z%20of%20the%20log%20ratio%20%7C%20the%20log%20makes%20%C3%9710%20and%20%C3%B710%20equally%20far%20%7C%0A%20%20%20%20%7C%20judge%20a%20weight%20%7C%20against%20its%20province's%20adjustment%20(HB)%20and%20against%20its%20domain's%20median%20%7C%20the%20weight%20of%20an%20area%20household%20that%20runs%20a%20large-scale%20unit%20is%20right%3B%20its%20domain%20is%20the%20question%20%7C%0A%20%20%20%20%7C%20find%20an%20interviewer's%20made-up%20interviews%20%7C%20pace%2C%20Whipple's%20index%2C%20repeated%20answers%2C%20blank%20items%2C%20per%20interviewer%3B%20then%20the%20interviews%20behind%20them%20%7C%20several%20indicators%2C%20confirmed%20by%20a%20callback%20%7C%0A%20%20%20%20%7C%20give%20a%20reviewer%20one%20table%20%7C%20flags%20as%20one%20long%20table%2C%20one%20row%20per%20household%2C%20ranked%20by%20sources%2C%20flags%20and%20weight%20%7C%20recall%20by%20kind%20and%20the%20rows%20read%2C%20against%20what%20is%20known%20%7C%0A%0A%20%20%20%20Next%2C%20module%209%20(%6080_anomalies_ml.py%60)%20asks%20the%20same%20questions%20of%20machine%20learning%3A%20unsupervised%20detectors%20that%20score%20every%20row%20at%20once%2C%20judged%20against%20this%20module's%20rules%20and%20statistics%20and%20against%20the%20planted%20defects%2C%20and%20the%20large%20table%20of%20fishing%20units%20from%20every%20region%2C%20where%20reading%20row%20by%20row%20is%20out%20of%20the%20question.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20course.quizzes_at(%228.5%22)%0A%20%20%20%20return%0A%0A%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20app.run()%0A
d2ddbef0b3de4ce2330eaf8329b12a26