import%20marimo%0A%0A__generated_with%20%3D%20%220.25.2%22%0Aapp%20%3D%20marimo.App(width%3D%22medium%22%2C%20app_title%3D%22Stretch%3A%20detection%22)%0A%0Awith%20app.setup%3A%0A%20%20%20%20%23%20The%20setup%20cell%20runs%20first.%20It%20holds%20the%20imports%20and%20the%20survey's%20tables%20every%20other%20cell%20uses.%0A%20%20%20%20import%20logging%0A%20%20%20%20import%20re%0A%0A%20%20%20%20import%20duckdb%0A%20%20%20%20import%20marimo%20as%20mo%0A%20%20%20%20import%20matplotlib.pyplot%20as%20plt%0A%20%20%20%20import%20numpy%20as%20np%0A%20%20%20%20import%20pandas%20as%20pd%0A%20%20%20%20import%20pyarrow.parquet%20as%20pq%0A%20%20%20%20import%20splink.comparison_library%20as%20cl%0A%20%20%20%20from%20sklearn.ensemble%20import%20IsolationForest%2C%20RandomForestClassifier%0A%20%20%20%20from%20splink%20import%20DuckDBAPI%2C%20Linker%2C%20SettingsCreator%2C%20block_on%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%20PATHS%2C%20UNIT_KEY%2C%20attempt%2C%20check_cases%2C%20try_it%2C%20waiting_for%0A%0A%20%20%20%20%23%20The%20survey%2C%20clean%20as%20module%202%20teaches%3A%20every%20region's%20rows%2C%20removed%20households%20and%20voided%20rows%20left%20out%0A%20%20%20%20units%20%3D%20course.units()%0A%20%20%20%20boats%20%3D%20course.child(%22boat%22)%0A%20%20%20%20catch%20%3D%20course.child(%22catch%22)%0A%20%20%20%20gears%20%3D%20course.child(%22gear%22)%0A%20%20%20%20landings%20%3D%20course.child(%22landing%22)%0A%20%20%20%20crew%20%3D%20course.child(%22crew%22)%0A%20%20%20%20members%20%3D%20course.members()%0A%20%20%20%20households%20%3D%20course.households()%0A%20%20%20%20planted%20%3D%20course.planted_defects()%20%20%23%20every%20defect%20the%20course%20planted%3B%20a%20real%20survey%20has%20no%20such%20list%0A%0A%20%20%20%20%23%20Module%209's%20features%3A%20one%20number%20each%20per%20fishing%20unit%0A%20%20%20%20FEATURES%20%3D%20%5B%22n_boats%22%2C%20%22largest_tonnage%22%2C%20%22longest_boat_m%22%2C%20%22sum_tonnage%22%2C%20%22sum_engine_hp%22%2C%20%22n_species%22%2C%20%22n_gears%22%2C%20%22n_landings%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22n_crew%22%2C%20%22hh_size%22%2C%20%22age_of_operator%22%2C%20%22hh_interview_minutes%22%5D%0A%20%20%20%20HOUSEHOLD%20%3D%20%5B%22region_code%22%2C%20%22hh_id%22%5D%20%20%23%20a%20household%20is%20its%20region%20and%20its%20id%3A%20ids%20repeat%20across%20regions%0A%20%20%20%20BLUE%2C%20ORANGE%2C%20GREY%20%3D%20%22%230072B2%22%2C%20%22%23E69F00%22%2C%20%22%23999999%22%20%20%23%20Okabe-Ito%20colors%2C%20which%20read%20for%20every%20kind%20of%20color%20vision%0A%20%20%20%20SPLINK_SEED%20%3D%201%20%20%23%20splink's%20random%20sampling%20of%20pairs%2C%20fixed%20so%20every%20run%20trains%20the%20same%20model%0A%20%20%20%20SCRATCH%20%3D%20PATHS.root%20%2F%20%22scratch%22%20%2F%20%22stretch_detection%22%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%20Stretch%3A%20detection%0A%0A%20%20%20%20For%20when%20you%20have%20finished%20module%209%20(%6080_anomalies_ml.py%60)%20and%20want%20more.%20Four%20exercises%2C%20each%20going%20a%20step%20past%20modules%208%20and%209%2C%20with%20less%20help%3A%20a%20task%2C%20an%20example%2C%20a%20check.%0A%0A%20%20%20%20-%20**S3.1%2C%20a%20detector%20by%20stratum.**%20Module%209%20fitted%20one%20detector%20per%20scale%2C%20to%20compare%20like%20with%20like.%20The%20scale%20is%20an%20answer%2C%20and%20some%20errors%20change%20it.%20The%20stratum%20is%20a%20fact%20of%20the%20design%20that%20no%20answer%20can%20move.%0A%20%20%20%20-%20**S3.2%20and%20S3.3%2C%20near-duplicate%20interviews.**%20Module%208%20found%20a%20copied%20interview%20when%20its%20members%20and%20its%20catch%20were%20exactly%20another's.%20A%20copy%20with%20one%20answer%20changed%20is%20not%20exact.%20Two%20tools%20look%20for%20near-duplicates%3A%20the%20**maximum%20percent%20match**%2C%20computed%20with%20numpy%2C%20and%20**splink**%2C%20a%20record-linkage%20library%20that%20runs%20in%20DuckDB.%20Both%20are%20judged%20against%20the%20copies%20the%20course%20planted.%0A%20%20%20%20-%20**S3.4%2C%20the%20large%20table%20end%20to%20end.**%20Module%209%20scored%20the%20large%20table%20across%20several%20cells.%20A%20review%20list%20for%20the%20next%20round%20is%20one%20function%2C%20from%20the%20file%20to%20the%20ranked%20list%2C%20that%20gives%20the%20same%20list%20whatever%20the%20chunk%20size.%0A%0A%20%20%20%20**The%20tables.**%20The%20setup%20cell%20reads%20the%20survey%20clean%2C%20as%20in%20module%209%2C%20and%20%60planted%60%20is%20the%20course's%20list%20of%20planted%20defects.%20The%20functions%20below%20are%20module%209's%20own%2C%20copied%20(%60survey_features%60%2C%20%60truth_units%60%2C%20%60rule_flags%60%20and%20the%20rest)%2C%20so%20this%20notebook%20runs%20on%20its%20own.%0A%0A%20%20%20%20**Seeds.**%20Every%20method%20that%20draws%20random%20numbers%20gets%20a%20fixed%20seed%3A%20%60random_state%3D0%60%20for%20the%20forests%2C%20%60SPLINK_SEED%60%20for%20splink.%20The%20checks%20compare%20exact%20results%2C%20and%20depend%20on%20that.%0A%0A%20%20%20%20Exercises%20work%20as%20in%20the%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.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20boat_features(boats%3A%20pd.DataFrame)%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22One%20row%20per%20unit%20that%20has%20boats%3A%20n_boats%2C%20largest_tonnage%2C%20longest_boat_m%2C%20sum_tonnage%20and%20sum_engine_hp%2C%20keyed%20on%20UNIT_KEY.%22%22%22%0A%20%20%20%20measured%20%3D%20boats%5B%22boat_measured_in%22%5D%20%20%23%201%20%3D%20from%20the%20papers%2C%20no%20length%3B%202%20%3D%20measured%20in%20meters%3B%203%20%3D%20measured%20in%20feet%0A%20%20%20%20length_m%20%3D%20np.select(%5Bmeasured%20%3D%3D%202%2C%20measured%20%3D%3D%203%5D%2C%20%5Bboats%5B%22boat_length%22%5D%2C%20boats%5B%22boat_length%22%5D%20%2F%203.281%5D%2C%20default%3Dnp.nan)%0A%20%20%20%20return%20(boats%5BUNIT_KEY%5D.assign(gt%3Dboats%5B%22boat_tonnage%22%5D%2C%20length_m%3Dlength_m%2C%20hp%3Dboats%5B%22boat_engine_hp%22%5D.fillna(0))%0A%20%20%20%20%20%20%20%20%20%20%20%20.groupby(UNIT_KEY%2C%20as_index%3DFalse)%0A%20%20%20%20%20%20%20%20%20%20%20%20.agg(n_boats%3D(%22gt%22%2C%20%22size%22)%2C%20largest_tonnage%3D(%22gt%22%2C%20%22max%22)%2C%20longest_boat_m%3D(%22length_m%22%2C%20%22max%22)%2C%20sum_tonnage%3D(%22gt%22%2C%20%22sum%22)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20sum_engine_hp%3D(%22hp%22%2C%20%22sum%22)))%0A%0A%0A%40app.function%0Adef%20unit_ids(table%3A%20pd.DataFrame)%20-%3E%20pd.Series%3A%0A%20%20%20%20%22%22%22One%20text%20id%20per%20fishing%20unit%2C%20from%20its%20region%2C%20household%20and%20key%20parts%3A%20%2201-5012-1-1%22.%22%22%22%0A%20%20%20%20parts%20%3D%20%5Btable%5B%22region_code%22%5D.astype(str).str.zfill(2)%2C%20*(table%5Bc%5D.astype(int).astype(str)%20for%20c%20in%20UNIT_KEY%5B1%3A%5D)%5D%0A%20%20%20%20out%20%3D%20parts%5B0%5D%0A%20%20%20%20for%20part%20in%20parts%5B1%3A%5D%3A%0A%20%20%20%20%20%20%20%20out%20%3D%20out%20%2B%20%22-%22%20%2B%20part%0A%20%20%20%20return%20out.rename(%22unit_id%22)%0A%0A%0A%40app.function%0Adef%20survey_features()%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22One%20row%20per%20fishing%20unit%20of%20the%20clean%20survey%3A%20unit_id%2C%20the%20unit's%20key%20and%20scale%20(unit_scale)%2C%20the%20FEATURES%2C%20and%0A%20%20%20%20beside%20them%2C%20not%20features%2C%20the%20household's%20interviewer%2C%20stratum%20and%20final%20weight.%22%22%22%0A%20%20%20%20%23%201.%20The%20units%2C%20each%20once%3A%20module%204%20found%20units%20entered%20twice%0A%20%20%20%20out%20%3D%20units%5B%5B*UNIT_KEY%2C%20%22unit_scale%22%5D%5D.drop_duplicates(UNIT_KEY)%0A%20%20%20%20%23%202.%20The%20interview%20and%20the%20design%20from%20the%20household%2C%20joined%20on%20the%20household%20(region%20and%20id)%0A%20%20%20%20out%20%3D%20out.merge(households%5B%5B*HOUSEHOLD%2C%20%22hh_interview_minutes%22%2C%20%22hh_interviewer%22%2C%20%22sel_stratum%22%2C%20%22sel_final_weight%22%5D%5D%2C%20on%3DHOUSEHOLD%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20how%3D%22left%22%2C%20validate%3D%22many_to_one%22)%0A%20%20%20%20%23%203.%20The%20child%20tables'%20features%3A%20boats%2C%20then%20a%20count%20of%20distinct%20species%2C%20and%20of%20gears%2C%20landing%20sites%20and%20crew%20per%20unit%0A%20%20%20%20counts%20%3D%20%5B%0A%20%20%20%20%20%20%20%20boat_features(boats)%2C%0A%20%20%20%20%20%20%20%20catch.groupby(UNIT_KEY%2C%20as_index%3DFalse).agg(n_species%3D(%22catch_species%22%2C%20%22nunique%22))%2C%0A%20%20%20%20%20%20%20%20gears.groupby(UNIT_KEY%2C%20as_index%3DFalse).agg(n_gears%3D(%22gear_type%22%2C%20%22size%22))%2C%0A%20%20%20%20%20%20%20%20landings.groupby(UNIT_KEY%2C%20as_index%3DFalse).agg(n_landings%3D(%22land_line%22%2C%20%22size%22))%2C%0A%20%20%20%20%20%20%20%20crew.groupby(UNIT_KEY%2C%20as_index%3DFalse).agg(n_crew%3D(%22crew_line%22%2C%20%22size%22))%2C%0A%20%20%20%20%5D%0A%20%20%20%20for%20part%20in%20counts%3A%0A%20%20%20%20%20%20%20%20out%20%3D%20out.merge(part%2C%20on%3DUNIT_KEY%2C%20how%3D%22left%22%2C%20validate%3D%22one_to_one%22)%0A%20%20%20%20%23%20A%20unit%20with%20no%20row%20in%20a%20child%20table%20has%20none%20of%20it%3A%20a%20missing%20count%20is%20a%20count%20of%200%0A%20%20%20%20zero%20%3D%20%5B%22n_boats%22%2C%20%22largest_tonnage%22%2C%20%22sum_tonnage%22%2C%20%22sum_engine_hp%22%2C%20%22n_species%22%2C%20%22n_gears%22%2C%20%22n_landings%22%2C%20%22n_crew%22%5D%0A%20%20%20%20out%5Bzero%5D%20%3D%20out%5Bzero%5D.fillna(0)%0A%20%20%20%20%23%204.%20The%20household's%20member%20rows%2C%20and%20the%20operator%3A%20the%20member%20whose%20roster%20line%20is%20operator_line%0A%20%20%20%20size%20%3D%20members.groupby(HOUSEHOLD%2C%20as_index%3DFalse).agg(hh_size%3D(%22mem_line%22%2C%20%22size%22))%0A%20%20%20%20operator%20%3D%20members%5B%5B*HOUSEHOLD%2C%20%22mem_line%22%2C%20%22mem_age%22%5D%5D.rename(columns%3D%7B%22mem_line%22%3A%20%22operator_line%22%2C%20%22mem_age%22%3A%20%22age_of_operator%22%7D)%0A%20%20%20%20out%20%3D%20out.merge(size%2C%20on%3DHOUSEHOLD%2C%20how%3D%22left%22%2C%20validate%3D%22many_to_one%22).merge(operator%2C%20on%3D%5B*HOUSEHOLD%2C%20%22operator_line%22%5D%2C%20how%3D%22left%22%2C%20validate%3D%22many_to_one%22)%0A%20%20%20%20%23%205.%20A%20length%20for%20every%20unit%3A%200%20with%20no%20boat%3B%20the%20scale's%20median%20where%20no%20boat%20was%20measured%20(sized%20from%20its%20papers)%0A%20%20%20%20no_length%20%3D%20out%5B%22longest_boat_m%22%5D.isna()%20%26%20(out%5B%22n_boats%22%5D%20%3E%200)%0A%20%20%20%20out%5B%22longest_boat_m%22%5D%20%3D%20out%5B%22longest_boat_m%22%5D.fillna(out.groupby(%22unit_scale%22)%5B%22longest_boat_m%22%5D.transform(%22median%22)).where(out%5B%22n_boats%22%5D%20%3E%200%2C%200.0)%0A%20%20%20%20out%5B%22length_imputed%22%5D%20%3D%20no_length%0A%20%20%20%20whole%20%3D%20%5B%22n_boats%22%2C%20%22n_species%22%2C%20%22n_gears%22%2C%20%22n_landings%22%2C%20%22n_crew%22%2C%20%22hh_size%22%2C%20%22age_of_operator%22%5D%0A%20%20%20%20out%5Bwhole%5D%20%3D%20out%5Bwhole%5D.astype(int)%0A%20%20%20%20out.insert(0%2C%20%22unit_id%22%2C%20unit_ids(out))%0A%20%20%20%20return%20out.reset_index(drop%3DTrue)%0A%0A%0A%40app.function%0Adef%20shown_by_features()%20-%3E%20dict%3A%0A%20%20%20%20%22%22%22The%20planted%20kinds%20that%20change%20at%20least%20one%20of%20a%20unit's%20features%2C%20and%20which%20feature.%22%22%22%0A%20%20%20%20return%20%7B%0A%20%20%20%20%20%20%20%20%22age_out_of_range%22%3A%20%22age_of_operator%3A%20the%20head%2C%20who%20runs%20the%20household's%20first%20unit%2C%20is%20over%20110%22%2C%0A%20%20%20%20%20%20%20%20%22operator_too_young%22%3A%20%22age_of_operator%3A%20the%20operator%20is%20under%2010%22%2C%0A%20%20%20%20%20%20%20%20%22scale_contradicts_boats%22%3A%20%22largest_tonnage%3A%20a%20boat%20of%205.0%20gross%20tons%20or%20more%20in%20a%20small-scale%20unit%22%2C%0A%20%20%20%20%20%20%20%20%22no_engine_large_boat%22%3A%20%22largest_tonnage%3A%20an%20outrigger%20boat%20with%20no%20engine%2C%20of%205.0%20gross%20tons%20or%20more%22%2C%0A%20%20%20%20%20%20%20%20%22stored_tonnage_wrong%22%3A%20%22largest_tonnage%2C%20sum_tonnage%3A%20the%20stored%20tonnage%20is%20the%20dimensions%20multiplied%20without%20the%20factor%22%2C%0A%20%20%20%20%20%20%20%20%22feet_recorded_as_meters%22%3A%20%22largest_tonnage%2C%20longest_boat_m%3A%20feet%20recorded%20as%20meters%2C%20a%20small%20boat%20of%20many%20tons%22%2C%0A%20%20%20%20%20%20%20%20%22made_up_interview%22%3A%20%22hh_interview_minutes%2C%20age_of_operator%3A%208%20to%2013%20minutes%2C%20ages%20rounded%20to%20fives%2C%20the%20same%20boat%20and%20catch%22%2C%0A%20%20%20%20%20%20%20%20%22unit_without_catch%22%3A%20%22n_species%3A%20no%20catch%20rows%22%2C%0A%20%20%20%20%20%20%20%20%22catch_rows_all_astray%22%3A%20%22n_species%3A%20every%20catch%20row%20keyed%20to%20another%20unit%2C%20so%20none%20matched%22%2C%0A%20%20%20%20%7D%0A%0A%0A%40app.function%0Adef%20truth_units(features%3A%20pd.DataFrame)%20-%3E%20set%3A%0A%20%20%20%20%22%22%22The%20unit_ids%20of%20the%20units%20a%20planted%20defect%20of%20a%20kind%20in%20shown_by_features()%20changed.%20A%20defect%20recorded%20on%20a%20unit's%0A%20%20%20%20key%20names%20that%20unit%3B%20one%20recorded%20on%20a%20member%20names%20every%20unit%20of%20the%20household.%22%22%22%0A%20%20%20%20kinds%20%3D%20planted%5Bplanted%5B%22kind%22%5D.isin(shown_by_features())%5D%0A%20%20%20%20on_unit%20%3D%20kinds%5Bkinds%5B%22operator_line%22%5D.notna()%5D%0A%20%20%20%20on_household%20%3D%20kinds%5Bkinds%5B%22operator_line%22%5D.isna()%5D%0A%20%20%20%20named%20%3D%20set(unit_ids(on_unit))%0A%20%20%20%20in_household%20%3D%20set(features.merge(on_household%5BHOUSEHOLD%5D.drop_duplicates()%2C%20on%3DHOUSEHOLD)%5B%22unit_id%22%5D)%0A%20%20%20%20return%20(named%20%7C%20in_household)%20%26%20set(features%5B%22unit_id%22%5D)%0A%0A%0A%40app.function%0Adef%20rule_flags(features%3A%20pd.DataFrame)%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22One%20column%20per%20rule%2C%20True%20where%20the%20unit%20fails%20it%2C%20in%20the%20features'%20row%20order%3A%20modules%204%20and%208's%20checks%20on%20one%0A%20%20%20%20unit%2C%20written%20on%20the%20survey's%20tables.%22%22%22%0A%20%20%20%20measured%20%3D%20boats%5B%22boat_measured_in%22%5D%0A%20%20%20%20volume%20%3D%20boats%5B%22boat_length%22%5D%20*%20boats%5B%22boat_beam%22%5D%20*%20boats%5B%22boat_depth%22%5D%0A%20%20%20%20computed%20%3D%20np.select(%5Bmeasured%20%3D%3D%202%2C%20measured%20%3D%3D%203%5D%2C%20%5Bvolume%20*%200.212%2C%20volume%20*%200.006%5D%2C%20default%3Dboats%5B%22boat_tonnage_reported%22%5D)%0A%20%20%20%20length%20%3D%20pd.Series(np.select(%5Bmeasured%20%3D%3D%202%2C%20measured%20%3D%3D%203%5D%2C%20%5Bboats%5B%22boat_length%22%5D%2C%20boats%5B%22boat_length%22%5D%20%2F%203.281%5D%2C%20default%3Dnp.nan)%2C%20index%3Dboats.index)%0A%20%20%20%20median%20%3D%20length.groupby(boats%5B%22boat_hull%22%5D).transform(%22median%22)%0A%20%20%20%20mad%20%3D%20(length%20-%20median).abs().groupby(boats%5B%22boat_hull%22%5D).transform(%22median%22)%0A%20%20%20%20per_boat%20%3D%20boats%5BUNIT_KEY%5D.assign(%0A%20%20%20%20%20%20%20%20boat%3D((boats%5B%22boat_engine%22%5D%20%3D%3D%200)%20%26%20(boats%5B%22boat_tonnage%22%5D%20%3E%3D%205))%20%7C%20((boats%5B%22boat_tonnage%22%5D%20-%20computed).abs()%20%3E%200.01%20%2B%201e-9)%2C%20%20%23%20B2%2C%20B3%0A%20%20%20%20%20%20%20%20boat_length%3D((length%20-%20median).abs()%20%2F%20(1.4826%20*%20mad))%20%3E%203.5%2C%20%20%23%20module%208's%20robust%20z%2C%20within%20the%20boat's%20hull%0A%20%20%20%20)%0A%20%20%20%20per_unit%20%3D%20per_boat.groupby(UNIT_KEY%2C%20as_index%3DFalse)%5B%5B%22boat%22%2C%20%22boat_length%22%5D%5D.any()%0A%20%20%20%20joined%20%3D%20features%5BUNIT_KEY%5D.merge(per_unit%2C%20on%3DUNIT_KEY%2C%20how%3D%22left%22).fillna(%7B%22boat%22%3A%20False%2C%20%22boat_length%22%3A%20False%7D)%0A%20%20%20%20stratum_median%20%3D%20features.groupby(%5B%22region_code%22%2C%20%22sel_stratum%22%5D)%5B%22sel_final_weight%22%5D.transform(%22median%22)%0A%20%20%20%20return%20pd.DataFrame(%7B%0A%20%20%20%20%20%20%20%20%22operator%20age%22%3A%20(features%5B%22age_of_operator%22%5D%20%3C%2016)%20%7C%20(features%5B%22age_of_operator%22%5D%20%3E%20110)%2C%0A%20%20%20%20%20%20%20%20%22scale%22%3A%20((features%5B%22unit_scale%22%5D%20%3D%3D%201)%20%26%20(features%5B%22largest_tonnage%22%5D%20%3E%3D%205))%20%7C%20((features%5B%22unit_scale%22%5D%20%3D%3D%202)%20%26%20(features%5B%22largest_tonnage%22%5D%20%3C%205))%2C%0A%20%20%20%20%20%20%20%20%22boat%20engine%20or%20tonnage%22%3A%20joined%5B%22boat%22%5D.astype(bool).to_numpy()%2C%0A%20%20%20%20%20%20%20%20%22boat%20length%22%3A%20joined%5B%22boat_length%22%5D.astype(bool).to_numpy()%2C%0A%20%20%20%20%20%20%20%20%22no%20catch%22%3A%20features%5B%22n_species%22%5D%20%3D%3D%200%2C%0A%20%20%20%20%20%20%20%20%22short%20interview%22%3A%20features%5B%22hh_interview_minutes%22%5D%20%3C%2020%2C%0A%20%20%20%20%20%20%20%20%22weight%22%3A%20features%5B%22sel_final_weight%22%5D%20%3E%203%20*%20stratum_median%2C%0A%20%20%20%20%7D%2C%20index%3Dfeatures.index)%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20%23%20Module%209's%20features%2C%20and%20the%20units%20whose%20features%20show%20a%20planted%20defect%0A%20%20%20%20features%20%3D%20survey_features()%0A%20%20%20%20truth%20%3D%20truth_units(features)%0A%20%20%20%20mo.md(f%22**%7Blen(features)%3A%2C%7D%20fishing%20units**%2C%20%7Blen(truth)%7D%20of%20them%20with%20a%20planted%20defect%20their%20features%20show%3A%20module%209's%20truth.%20The%20table%20carries%20%60sel_stratum%60%20beside%20the%20features%2C%20for%20S3.1.%22)%0A%20%20%20%20return%20features%2C%20truth%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%20S3.1%20%C2%B7%20A%20detector%20by%20stratum%0A%0A%20%20%20%20**The%20problem.**%20Module%209%20fitted%20an%20Isolation%20Forest%20per%20scale%20(%60unit_scale%60)%2C%20so%20that%20small%20boats%20were%20compared%20with%20small%20boats.%20The%20scale%20is%20an%20**answer**%2C%20and%20an%20error%20can%20change%20it.%20A%20boat%20measured%20in%20feet%20and%20recorded%20as%20meters%20becomes%20a%20boat%20of%20many%20tons%2C%20and%20the%20unit's%20scale%20says%20large-scale%20(%60feet_recorded_as_meters%60%20in%20the%20planted%20list).%20Among%20the%20large-scale%20units%20it%20looks%20like%20any%20other%20large%20boat%2C%20so%20a%20detector%20fitted%20on%20that%20scale%20has%20little%20to%20find.%0A%0A%20%20%20%20The%20**stratum**%20is%20a%20fact%20of%20the%20design%20(%60sel_stratum%60%3A%20F%20for%20the%20registered%20large%20units%2C%20taken%20whole%3B%20A%2C%20B%20and%20C%20for%20the%20sampled%20areas).%20A%20household%20was%20listed%20and%20sampled%20in%20it%20before%20anyone%20measured%20a%20boat%2C%20and%20no%20answer%20moves%20it.%20A%20large%20boat%20in%20an%20area%20stratum%20is%20unusual%20there%2C%20whether%20it%20is%20an%20error%20or%20a%20real%20large%20unit%20that%20turned%20up%20in%20a%20sampled%20area%20(module%206%20met%20these%20households%3A%20an%20area%20stratum's%20household%20that%20runs%20a%20large-scale%20unit).%20The%20design%20columns%20stay%20out%20of%20the%20features%2C%20as%20module%209%20said%3B%20here%20the%20stratum%20decides%20**who%20is%20compared%20with%20whom**%2C%20not%20what%20is%20compared.%0A%0A%20%20%20%20**Input%20%E2%86%92%20output%3A**%20a%20matrix%20of%20features%20%60X%60%20(a%20table)%2C%20the%20group%20of%20each%20row%20%60groups%60%20(a%20Series%20with%20%60X%60's%20index)%2C%20the%20ids%20of%20its%20rows%20%60ids%60%20(a%20Series%20with%20%60X%60's%20index)%20and%20a%20%60contamination%60%20%E2%86%92%20the%20**sorted**%20list%20of%20the%20ids%20flagged%20when%20one%20%60IsolationForest(n_estimators%3D200%2C%20contamination%3Dcontamination%2C%20random_state%3D0)%60%20is%20fitted%20on%20each%20group's%20rows%20and%20flags%20that%20group's%20rows.%0A%0A%20%20%20%20%60%60%60python%0A%20%20%20%20flags_by_group(features%5BFEATURES%5D%2C%20features%5B%22sel_stratum%22%5D%2C%20features%5B%22unit_id%22%5D%2C%200.05)%20%20%20%23%20%5B'01-5022-1-1'%2C%20'01-5027-5-1'%2C%20...%5D%0A%20%20%20%20%60%60%60%0A%0A%20%20%20%20The%20rows%20of%20%60X%60%20need%20not%20be%20sorted%20by%20group%2C%20and%20its%20index%20need%20not%20run%200%2C%201%2C%202%3A%20a%20flag%20must%20land%20on%20the%20row%20it%20belongs%20to.%0A%0A%20%20%20%20**Tools%3A**%20%60X.groupby(groups)%60%20(each%20group's%20rows%2C%20with%20their%20own%20index%20labels)%2C%20%60IsolationForest%60%2C%20%60.fit(...)%60%20and%20%60.predict(...)%60%20(-1%20is%20flagged)%2C%20%60ids.loc%5B...%5D%60%20by%20those%20labels%2C%20%60sorted%60.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20flags_by_group(X%3A%20pd.DataFrame%2C%20groups%3A%20pd.Series%2C%20ids%3A%20pd.Series%2C%20contamination%3A%20float)%20-%3E%20list%3A%0A%20%20%20%20%22%22%22The%20sorted%20ids%20flagged%20by%20one%20Isolation%20Forest%20(200%20trees%2C%20random_state%3D0)%20per%20group%2C%20each%20fitted%20on%20its%20group's%20rows.%22%22%22%0A%20%20%20%20%23%20Exercise%20S3.1%3A%20the%20task%20and%20examples%20are%20in%20the%20text%20above%20this%20cell.%0A%20%20%20%20%23%20Step%201%3A%20for%20each%20group's%20rows%20of%20X%20(X.groupby(groups))%3A%0A%20%20%20%20%23%20%20%20%20%20%20%20%20%20fit%20IsolationForest(n_estimators%3D200%2C%20contamination%3Dcontamination%2C%20random_state%3D0)%20on%20them%2C%20and%20predict%20them%0A%20%20%20%20%23%20Step%202%3A%20the%20index%20labels%20of%20the%20rows%20predicted%20-1%3B%20their%20ids%2C%20by%20those%20labels%20(ids.loc%5B...%5D)%0A%20%20%20%20%23%20Step%203%3A%20return%20every%20group's%20flagged%20ids%20together%2C%20sorted%2C%20as%20a%20list%0A%20%20%20%20raise%20NotImplementedError(%22Exercise%20S3.1%22)%0A%0A%0A%40app.function%0Adef%20two_groups()%20-%3E%20tuple%3A%0A%20%20%20%20%22%22%22Sixty%20rows%20of%20two%20groups%2C%20interleaved%2C%20with%20an%20index%20that%20does%20not%20run%200%2C%201%2C%202%3A%20group%20A's%20values%20sit%20near%201%20and%0A%20%20%20%20group%20B's%20near%20100.%20Row%20a17%20is%20100%20in%20group%20A%2C%20ordinary%20in%20B%3B%20row%20b9%20is%201%20in%20group%20B%2C%20ordinary%20in%20A.%20Used%20by%20the%0A%20%20%20%20check%20of%20Exercise%20S3.1.%22%22%22%0A%20%20%20%20rng%20%3D%20np.random.default_rng(20261109)%0A%20%20%20%20group%20%3D%20np.array(%5B%22A%22%2C%20%22B%22%5D%20*%2030)%0A%20%20%20%20x%20%3D%20np.where(group%20%3D%3D%20%22A%22%2C%20rng.normal(1%2C%200.1%2C%2060)%2C%20rng.normal(100%2C%2010%2C%2060))%0A%20%20%20%20y%20%3D%20np.where(group%20%3D%3D%20%22A%22%2C%20rng.normal(5%2C%200.5%2C%2060)%2C%20rng.normal(50%2C%205%2C%2060))%0A%20%20%20%20names%20%3D%20%5Bf%22%7Bg.lower()%7D%7Bk%7D%22%20for%20k%2C%20g%20in%20zip(np.arange(60)%20%2F%2F%202%2C%20group)%5D%0A%20%20%20%20X%20%3D%20pd.DataFrame(%7B%22x%22%3A%20x%2C%20%22y%22%3A%20y%7D%2C%20index%3Dnp.arange(1000%2C%201060))%0A%20%20%20%20X.loc%5B1034%2C%20%5B%22x%22%2C%20%22y%22%5D%5D%20%3D%20%5B100.0%2C%2050.0%5D%20%20%23%20a17%3A%20B's%20values%20in%20group%20A%0A%20%20%20%20X.loc%5B1019%2C%20%5B%22x%22%2C%20%22y%22%5D%5D%20%3D%20%5B1.0%2C%205.0%5D%20%20%23%20b9%3A%20A's%20values%20in%20group%20B%0A%20%20%20%20return%20X%2C%20pd.Series(group%2C%20index%3DX.index)%2C%20pd.Series(names%2C%20index%3DX.index)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(features)%3A%0A%20%20%20%20_seven%20%3D%20features%5B%22region_code%22%5D%20%3D%3D%20%2207%22%0A%20%20%20%20check_cases(%0A%20%20%20%20%20%20%20%20%22Stretch%20S3.1%22%2C%20flags_by_group%2C%0A%20%20%20%20%20%20%20%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22sixty%20rows%20of%20two%20groups%2C%20interleaved%2C%20an%20index%20from%201000%2C%20one%20row%20of%20each%20group%20with%20the%20other's%20values%22%2C%20(*two_groups()%2C%200.05))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22the%20survey's%20units%2C%20one%20forest%20per%20stratum%22%2C%20(features%5BFEATURES%5D%2C%20features%5B%22sel_stratum%22%5D%2C%20features%5B%22unit_id%22%5D%2C%200.05))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22the%20survey's%20units%2C%20one%20forest%20per%20scale%20(module%209's%20grouping)%22%2C%20(features%5BFEATURES%5D%2C%20features%5B%22unit_scale%22%5D%2C%20features%5B%22unit_id%22%5D%2C%200.05))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22region%2007's%20units%20by%20stratum%2C%203%25%22%2C%20(features.loc%5B_seven%2C%20FEATURES%5D%2C%20features.loc%5B_seven%2C%20%22sel_stratum%22%5D%2C%20features.loc%5B_seven%2C%20%22unit_id%22%5D%2C%200.03))%2C%0A%20%20%20%20%20%20%20%20%5D%2C%0A%20%20%20%20%20%20%20%20what%3D%22tables%20and%20groupings%22%2C%20summarize%3Dlambda%20ids%3A%20sorted(map(str%2C%20ids))%2C%0A%20%20%20%20%20%20%20%20hint%3D%22Fit%20one%20IsolationForest(n_estimators%3D200%2C%20contamination%3D...%2C%20random_state%3D0)%20per%20group%20of%20X.groupby(groups)%3B%20read%20the%20flagged%20rows'%20ids%20by%20their%20index%20labels%3B%20sort.%22%2C%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(features)%3A%0A%20%20%20%20%23%20Try%20it%3A%20the%20units%20flagged%20by%20one%20forest%20per%20stratum%0A%20%20%20%20try_it(flags_by_group%2C%20features%5BFEATURES%5D%2C%20features%5B%22sel_stratum%22%5D%2C%20features%5B%22unit_id%22%5D%2C%200.05)%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%20S3.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%20Right%20on%20the%20survey's%20tables%2C%20wrong%20on%20the%20sixty%20rows%20%7C%20The%20flags%20pasted%20back%20in%20group%20order%2C%20onto%20a%20table%20that%20is%20not%20sorted%20by%20group.%20%7C%20read%20the%20ids%20by%20the%20group's%20own%20index%20labels%20%7C%0A%20%20%20%20%7C%20*KeyError*%20naming%20numbers%200%2C%201%2C%202%20%7C%20Positions%20used%20as%20labels%20(%60ids.loc%5Bnp.flatnonzero(...)%5D%60)%3A%20the%20index%20starts%20at%201000.%20%7C%20%60rows.index%5B...%5D%60%2C%20or%20%60ids.iloc%60%20with%20positions%20of%20the%20whole%20table%20%7C%0A%20%20%20%20%7C%20Neither%20a17%20nor%20b9%20flagged%20%7C%20One%20forest%20for%20the%20whole%20table%3A%20each%20of%20them%20is%20ordinary%20in%20the%20other%20group.%20%7C%20one%20forest%20per%20group%20%7C%0A%20%20%20%20%7C%20Flags%20that%20change%20from%20run%20to%20run%20%7C%20%60random_state%60%20missing.%20%7C%20%60random_state%3D0%60%20%7C%0A%20%20%20%20%7C%20Ids%20not%20sorted%2C%20or%20a%20NumPy%20array%20%7C%20The%20list%20was%20not%20sorted%2C%20or%20not%20made%20a%20list.%20%7C%20%60sorted(...)%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_(features%2C%20truth)%3A%0A%20%20%20%20%23%20The%20two%20groupings%2C%20kind%20by%20kind%3A%20what%20each%20finds%20of%20the%20planted%20defects%20their%20features%20show%2C%20and%20of%20the%20large%20units%20in%20area%20strata%0A%20%20%20%20def%20_compare()%3A%0A%20%20%20%20%20%20%20%20groupings%20%3D%20%7B%22by%20scale%20(module%209)%22%3A%20%22unit_scale%22%2C%20%22by%20stratum%22%3A%20%22sel_stratum%22%7D%0A%20%20%20%20%20%20%20%20found%20%3D%20%7B%7D%0A%20%20%20%20%20%20%20%20for%20name%2C%20column%20in%20groupings.items()%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20ids%20%3D%20attempt(flags_by_group%2C%20features%5BFEATURES%5D%2C%20features%5Bcolumn%5D%2C%20features%5B%22unit_id%22%5D%2C%200.05)%0A%20%20%20%20%20%20%20%20%20%20%20%20if%20ids%20is%20NOT_YET%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20return%20waiting_for(%22Exercise%20S3.1%22)%0A%20%20%20%20%20%20%20%20%20%20%20%20found%5Bname%5D%20%3D%20set(ids)%0A%20%20%20%20%20%20%20%20rows%20%3D%20%5B%5D%0A%20%20%20%20%20%20%20%20for%20kind%20in%20shown_by_features()%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20kind_units%20%3D%20set(features.merge(planted.loc%5Bplanted%5B%22kind%22%5D%20%3D%3D%20kind%2C%20HOUSEHOLD%5D.drop_duplicates()%2C%20on%3DHOUSEHOLD)%5B%22unit_id%22%5D)%20%26%20truth%0A%20%20%20%20%20%20%20%20%20%20%20%20rows.append(%7B%22kind%22%3A%20kind%2C%20%22units%22%3A%20len(kind_units)%2C%20**%7Bname%3A%20len(kind_units%20%26%20ids)%20for%20name%2C%20ids%20in%20found.items()%7D%7D)%0A%20%20%20%20%20%20%20%20area_large%20%3D%20set(features.loc%5B(features%5B%22unit_scale%22%5D%20%3D%3D%202)%20%26%20(features%5B%22sel_stratum%22%5D%20!%3D%20%22F%22)%2C%20%22unit_id%22%5D)%20-%20truth%0A%20%20%20%20%20%20%20%20rows.append(%7B%22kind%22%3A%20%22large%20units%20in%20area%20strata%20(real%2C%20not%20planted)%22%2C%20%22units%22%3A%20len(area_large)%2C%20**%7Bname%3A%20len(area_large%20%26%20ids)%20for%20name%2C%20ids%20in%20found.items()%7D%7D)%0A%20%20%20%20%20%20%20%20table%20%3D%20pd.DataFrame(rows)%0A%20%20%20%20%20%20%20%20feet%20%3D%20table.set_index(%22kind%22).loc%5B%22feet_recorded_as_meters%22%5D%0A%20%20%20%20%20%20%20%20(by_scale%2C%20scale_ids)%2C%20(by_stratum%2C%20stratum_ids)%20%3D%20found.items()%0A%20%20%20%20%20%20%20%20rule_units%20%3D%20set(features.loc%5Brule_flags(features).any(axis%3D1)%2C%20%22unit_id%22%5D)%0A%20%20%20%20%20%20%20%20added%20%3D%20(stratum_ids%20%26%20truth)%20-%20rule_units%0A%20%20%20%20%20%20%20%20verdict%20%3D%20(%22**Module%209's%20verdict%20still%20holds**%3A%20every%20defect%20the%20stratum%20forests%20find%2C%20module%209's%20rules%20(copied%20above%20as%20%60rule_flags%60)%20had%20found.%20%22%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20if%20not%20added%20else%20f%22**The%20stratum%20forests%20find%20%7Blen(added)%7D%20defects%20module%209's%20rules%20missed**%3A%20%7B'%2C%20'.join(sorted(added))%7D.%20%22)%0A%20%20%20%20%20%20%20%20summary%20%3D%20(f%22Grouped%20%7Bby_scale%7D%2C%20the%20forests%20flag%20%7Blen(scale_ids)%7D%20units%2C%20%7Blen(scale_ids%20%26%20truth)%7D%20of%20the%20%7Blen(truth)%7D%20in%20the%20truth%20(%7Blen(scale_ids%20%26%20truth)%20%2F%20len(truth)%3A.0%25%7D)%3B%20%22%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20f%22grouped%20%7Bby_stratum%7D%2C%20%7Blen(stratum_ids)%7D%2C%20of%20which%20%7Blen(stratum_ids%20%26%20truth)%7D%20are%20in%20the%20truth%20(%7Blen(stratum_ids%20%26%20truth)%20%2F%20len(truth)%3A.0%25%7D).%22)%0A%20%20%20%20%20%20%20%20return%20mo.vstack(%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20table%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20mo.md(f%22%7Bsummary%7D%20About%20the%20same%20number%20of%20flags%2C%20grouped%20two%20ways.%20**By%20stratum%20the%20forest%20finds%20%7Bfeet%5B'by%20stratum'%5D%7D%20of%20the%20%7Bfeet%5B'units'%5D%7D%20boats%20measured%20in%20feet%2C%20where%20by%20scale%20it%20finds%20%7Bfeet%5B'by%20scale%20(module%209)'%5D%7D**%3A%20the%20error%20had%20moved%20them%20into%20the%20large-scale%20units%2C%20and%20the%20stratum%20keeps%20them%20among%20the%20small%20boats%20of%20the%20sampled%20areas.%20%22%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20f%22It%20also%20flags%20%7Btable.set_index('kind').loc%5B'large%20units%20in%20area%20strata%20(real%2C%20not%20planted)'%2C%20'by%20stratum'%5D%7D%20large%20units%20in%20area%20strata.%20They%20are%20real%20large%20units%2C%20not%20errors%2C%20and%20module%206%20showed%20how%20much%20of%20the%20large-scale%20estimates'%20variance%20such%20households%20carry%3A%20worth%20a%20supervisor's%20look%20for%20that%20reason%2C%20as%20long%20as%20the%20review%20list%20says%20why%20each%20was%20flagged.%20%22%20%2B%20verdict)%2C%0A%20%20%20%20%20%20%20%20%5D)%0A%0A%20%20%20%20_compare()%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%20The%20grouping%20changed%20what%20the%20detector%20reaches%3B%20the%20detector%20is%20the%20same.%20**Group%20by%20what%20the%20design%20fixed%2C%20not%20by%20an%20answer%20the%20errors%20can%20change.**%0A%0A%20%20%20%20%23%23%20S3.2%20%C2%B7%20Near-duplicate%20interviews%3A%20the%20maximum%20percent%20match%0A%0A%20%20%20%20**The%20problem.**%20Module%208%20flagged%20a%20copy%20when%20an%20interview%20had%20another's%20members%20and%20catch%20exactly.%20A%20copy%20typed%20again%20with%20one%20age%20wrong%2C%20or%20a%20household%20copied%20and%20edited%20a%20little%20to%20fill%20a%20quota%2C%20matches%20no%20other%20interview%20as%20a%20whole%2C%20though%20it%20matches%20its%20original%20on%20almost%20every%20answer.%0A%0A%20%20%20%20**The%20idea.**%20Kuriakose%20and%20Robbins%20proposed%20the%20**maximum%20percent%20match**%3A%20for%20each%20interview%2C%20the%20share%20of%20questions%20on%20which%20it%20gives%20the%20same%20answer%20as%20the%20most%20similar%20other%20interview%20in%20the%20survey.%20An%20interview%20copied%20from%20another%20agrees%20with%20it%20on%20nearly%20every%20question%3B%20two%20honest%20interviews%20agree%20on%20fewer.%20They%20flagged%20an%20interview%20whose%20maximum%20percent%20match%20is%20above%2085%25%2C%20and%20took%20more%20than%205%25%20of%20a%20survey's%20interviews%20above%20it%20as%20a%20sign%20of%20fabrication.%0A%0A%20%20%20%20**The%20caveat.**%20Pew%20Research%20Center%20tested%20the%20proposal%20(Simmons%2C%20Mercer%2C%20Schwarzer%20and%20Kennedy%2C%20*Evaluating%20a%20new%20proposal%20for%20detecting%20data%20falsification%20in%20surveys*%2C%2023%20February%202016)%20and%20found%20that%20how%20many%20honest%20interviews%20pass%2085%25%20depends%20on%20the%20questionnaire%20and%20the%20sample%3A%20the%20number%20of%20questions%2C%20the%20number%20of%20answers%20each%20allows%2C%20the%20sample's%20size%20and%20how%20alike%20its%20respondents%20are.%20A%20fixed%20threshold%20flags%20honest%20interviews%20in%20a%20short%20questionnaire%2C%20or%20in%20a%20population%20that%20answers%20alike.%20Pew%20recommends%20several%20indicators%2C%20paradata%20(data%20on%20how%20each%20interview%20was%20done%2C%20such%20as%20its%20length)%20and%20follow-up%20in%20the%20field%20over%20one%20threshold%20for%20every%20survey.%20This%20section%20measures%20that%20on%20the%20survey.%0A%0A%20%20%20%20The%20next%20cells%20lay%20each%20household's%20questionnaire%20out%20as%20one%20row%20of%20answers%2C%20and%20measure%20it.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20answer_table()%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22One%20row%20per%20household%20of%20the%20clean%20survey%2C%20about%20a%20hundred%20columns%20of%20answers%20in%20the%20survey's%20codes%2C%20missing%20where%0A%20%20%20%20a%20question%20was%20not%20asked%3B%20the%20index%20is%20the%20household%20as%20text%2C%20%2201-5012%22.%22%22%22%0A%20%20%20%20index%20%3D%20pd.MultiIndex.from_frame(households%5BHOUSEHOLD%5D)%0A%20%20%20%20parts%20%3D%20%5Bhouseholds.set_index(HOUSEHOLD)%5B%5B%22hh_listed_members%22%2C%20%22hh_roster_members%22%5D%5D%5D%0A%20%20%20%20items%20%3D%20%5B%22mem_relation%22%2C%20%22mem_sex%22%2C%20%22mem_age%22%2C%20%22mem_education%22%2C%20%22mem_fished%22%5D%0A%20%20%20%20roster%20%3D%20members%5Bmembers%5B%22mem_line%22%5D%20%3C%3D%206%5D.pivot_table(index%3DHOUSEHOLD%2C%20columns%3D%22mem_line%22%2C%20values%3Ditems%2C%20aggfunc%3D%22first%22)%0A%20%20%20%20roster.columns%20%3D%20%5Bf%22%7Bitem%7D_line%7Bline%7D%22%20for%20item%2C%20line%20in%20roster.columns%5D%0A%20%20%20%20parts.append(roster%5B%5Bf%22%7Bitem%7D_line%7Bline%7D%22%20for%20line%20in%20range(1%2C%207)%20for%20item%20in%20items%5D%5D)%0A%20%20%20%20first_unit%20%3D%20units.sort_values(UNIT_KEY).drop_duplicates(HOUSEHOLD).set_index(HOUSEHOLD)%0A%20%20%20%20parts.append(first_unit%5B%5B%22unit_sold_share%22%2C%20%22unit_ownership%22%2C%20%22unit_uses_boat%22%2C%20%22unit_boats%22%2C%20%22unit_scale%22%2C%20%22unit_waters%22%2C%20%22unit_months_active%22%2C%20%22unit_hired_crew%22%5D%5D)%0A%20%20%20%20first_boat%20%3D%20boats.sort_values(%5B*UNIT_KEY%2C%20%22boat_no%22%5D).drop_duplicates(HOUSEHOLD).set_index(HOUSEHOLD)%0A%20%20%20%20measured%20%3D%20first_boat%5B%22boat_measured_in%22%5D%0A%20%20%20%20length%20%3D%20np.select(%5Bmeasured%20%3D%3D%202%2C%20measured%20%3D%3D%203%5D%2C%20%5Bfirst_boat%5B%22boat_length%22%5D%2C%20first_boat%5B%22boat_length%22%5D%20%2F%203.281%5D%2C%20default%3Dnp.nan)%0A%20%20%20%20parts.append(first_boat%5B%5B%22boat_hull%22%2C%20%22boat_engine%22%2C%20%22boat_acquired%22%2C%20%22boat_measured_in%22%5D%5D.assign(boat_length_m%3Dnp.round(length%2C%201)))%0A%20%20%20%20species%20%3D%20course.codes(%22species%22)%5B%22code%22%5D%0A%20%20%20%20caught%20%3D%20pd.crosstab(%5Bcatch%5B%22region_code%22%5D%2C%20catch%5B%22hh_id%22%5D%5D%2C%20catch%5B%22catch_species%22%5D).clip(upper%3D1).reindex(columns%3Dspecies%2C%20fill_value%3D0)%0A%20%20%20%20parts.append(caught.add_prefix(%22species_%22))%0A%20%20%20%20gear_codes%20%3D%20course.codes(%22gears%22)%5B%22code%22%5D%0A%20%20%20%20used%20%3D%20pd.crosstab(%5Bgears%5B%22region_code%22%5D%2C%20gears%5B%22hh_id%22%5D%5D%2C%20gears%5B%22gear_type%22%5D).clip(upper%3D1).reindex(columns%3Dgear_codes%2C%20fill_value%3D0)%0A%20%20%20%20parts.append(used.add_prefix(%22gear_%22))%0A%20%20%20%20table%20%3D%20pd.DataFrame(index%3Dindex).join(parts%2C%20how%3D%22left%22).astype(%22float64%22)%0A%20%20%20%20flags%20%3D%20%5Bc%20for%20c%20in%20table.columns%20if%20c.startswith((%22species_%22%2C%20%22gear_%22))%5D%0A%20%20%20%20table%5Bflags%5D%20%3D%20table%5Bflags%5D.fillna(0.0)%20%20%23%20a%20household%20with%20no%20catch%20or%20gear%20row%20caught%20or%20used%20none%0A%20%20%20%20table.index%20%3D%20%5Bf%22%7Bregion%7D-%7Bhh%7D%22%20for%20region%2C%20hh%20in%20index%5D%0A%20%20%20%20return%20table%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20%23%20The%20survey's%20households%20as%20rows%20of%20answers%0A%20%20%20%20answers%20%3D%20answer_table()%0A%20%20%20%20SHORT%20%3D%20%5B%22hh_listed_members%22%2C%20%22hh_roster_members%22%2C%20%22unit_sold_share%22%2C%20%22unit_ownership%22%2C%20%22unit_uses_boat%22%2C%20%22unit_boats%22%2C%20%22unit_scale%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%22unit_waters%22%2C%20%22unit_months_active%22%2C%20%22unit_hired_crew%22%2C%20%22boat_hull%22%2C%20%22boat_engine%22%2C%20%22boat_acquired%22%2C%20%22boat_measured_in%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%22boat_length_m%22%5D%20%20%23%20a%20short%20questionnaire%3A%20the%20household%20and%20its%20first%20unit%20and%20boat%0A%20%20%20%20_species%20%3D%20sum(c.startswith(%22species_%22)%20for%20c%20in%20answers.columns)%0A%20%20%20%20_gears%20%3D%20sum(c.startswith(%22gear_%22)%20for%20c%20in%20answers.columns)%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**The%20answers.**%20%60answer_table()%60%20lays%20each%20household's%20questionnaire%20out%20as%20one%20row%20of%20**%7Banswers.shape%5B1%5D%7D%20answers**%2C%20in%20the%20codes%20the%20survey%20stores%2C%20one%20row%20for%20each%20of%20the%20**%7Blen(answers)%3A%2C%7D%20households**%3A%0A%0A%20%20%20%20%7C%20columns%20%7C%20what%20they%20are%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%202%20%7C%20the%20household%3A%20members%20when%20the%20area%20was%20listed%20(%60hh_listed_members%60)%20and%20on%20the%20questionnaire's%20cover%20(%60hh_roster_members%60)%20%7C%0A%20%20%20%20%7C%2030%20%7C%20the%20roster's%20first%20six%20lines%3A%20relationship%2C%20sex%2C%20age%2C%20education%20and%20whether%20the%20member%20fished%20(%60mem_relation%60%20to%20%60mem_fished%60)%3B%20missing%20past%20the%20household's%20size%20%7C%0A%20%20%20%20%7C%208%20%7C%20the%20first%20fishing%20unit%3A%20catch%20sold%2C%20ownership%2C%20whether%20it%20used%20a%20boat%2C%20its%20boats%2C%20scale%2C%20waters%2C%20months%20active%2C%20hired%20crew%20(%60unit_sold_share%60%20to%20%60unit_hired_crew%60)%20%7C%0A%20%20%20%20%7C%205%20%7C%20the%20first%20boat%3A%20hull%2C%20engine%2C%20how%20it%20was%20acquired%2C%20where%20its%20size%20comes%20from%2C%20and%20its%20length%20in%20meters%20to%20a%20decimal%20%7C%0A%20%20%20%20%7C%20%7B_species%7D%20%7C%20one%20column%20per%20species%20of%20the%20code%20list%3A%201%20if%20the%20household%20caught%20it%2C%200%20if%20not%20%7C%0A%20%20%20%20%7C%20%7B_gears%7D%20%7C%20one%20column%20per%20gear%20of%20the%20code%20list%3A%201%20if%20the%20household%20used%20it%2C%200%20if%20not%20%7C%0A%0A%20%20%20%20%60SHORT%60%20names%20%7Blen(SHORT)%7D%20of%20them%2C%20the%20household's%20two%20and%20its%20first%20unit's%20and%20boat's%3A%20a%20short%20questionnaire%2C%20for%20comparison.%20The%20first%20rows%20and%20columns%3A%0A%20%20%20%20%22%22%22)%2C%0A%20%20%20%20%20%20%20%20answers.iloc%5B%3A5%2C%20%3A12%5D%2C%0A%20%20%20%20%5D)%0A%20%20%20%20return%20SHORT%2C%20answers%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**Input%20%E2%86%92%20output%3A**%20a%20table%20of%20answers%2C%20one%20row%20per%20interview%20(its%20index%20the%20interview's%20id)%2C%20one%20column%20per%20question%2C%20missing%20where%20a%20question%20was%20not%20asked%20%E2%86%92%20a%20table%20with%20the%20same%20index%20and%20two%20columns%3A%0A%0A%20%20%20%20-%20%60max_match%60%3A%20the%20share%20of%20the%20questions%2C%20between%200%20and%201%2C%20on%20which%20the%20row%20gives%20the%20same%20answer%20as%20the%20most%20similar%20**other**%20row%3B%0A%20%20%20%20-%20%60closest%60%3A%20the%20index%20label%20of%20that%20row%3B%20where%20several%20tie%2C%20the%20first%20of%20them%20in%20the%20table's%20order.%0A%0A%20%20%20%20The%20table%20below%20is%20the%20check's%20five%20interviews%2C%20%60answers_fixture()%60.%0A%0A%20%20%20%20**Two%20decisions%2C%20made%20for%20you.**%20A%20question%20both%20rows%20left%20missing%20counts%20as%20the%20same%20answer%3A%20a%20skip%20pattern%20is%20part%20of%20the%20interview%2C%20and%20a%20copy%20repeats%20it.%20The%20share%20is%20out%20of%20every%20column%2C%20so%20all%20rows%20are%20measured%20on%20the%20same%20questionnaire.%0A%0A%20%20%20%20%7C%20id%20%7C%20q1%20%7C%20q2%20%7C%20q3%20%7C%20q4%20%7C%20%E2%86%92%20max_match%20%7C%20%E2%86%92%20closest%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%20h1%20%7C%201%20%7C%202%20%7C%20*missing*%20%7C%204%20%7C%201.00%20%7C%20h2%20%7C%0A%20%20%20%20%7C%20h2%20%7C%201%20%7C%202%20%7C%20*missing*%20%7C%204%20%7C%201.00%20%7C%20h1%20%7C%0A%20%20%20%20%7C%20h3%20%7C%201%20%7C%209%20%7C%203%20%7C%204%20%7C%200.50%20%7C%20h1%20(ties%20with%20h2%20and%20h4%3B%20h1%20is%20first)%20%7C%0A%20%20%20%20%7C%20h4%20%7C%207%20%7C%209%20%7C%203%20%7C%208%20%7C%200.75%20%7C%20h5%20%7C%0A%20%20%20%20%7C%20h5%20%7C%207%20%7C%209%20%7C%205%20%7C%208%20%7C%200.75%20%7C%20h4%20%7C%0A%0A%20%20%20%20**Tools%3A**%20%60table.to_numpy(dtype%3Dfloat)%60%3B%20for%20each%20column%20%60a%60%2C%20%60a%5B%3A%2C%20None%5D%20%3D%3D%20a%5BNone%2C%20%3A%5D%60%20is%20an%20n%20%C3%97%20n%20table%20of%20agreements%2C%20and%20%60np.isnan%60%20gives%20the%20missing%20ones%3B%20add%20them%20up%20over%20the%20columns%20into%20one%20n%20%C3%97%20n%20count%3B%20%60np.fill_diagonal%60%20so%20a%20row%20cannot%20match%20itself%3B%20%60.max(axis%3D1)%60%20and%20%60.argmax(axis%3D1)%60%20(the%20first%20of%20a%20tie).%20Comparing%20all%20columns%20at%20once%2C%20%60a%5B%3A%2C%20None%2C%20%3A%5D%20%3D%3D%20a%5BNone%2C%20%3A%2C%20%3A%5D%60%2C%20works%20on%20small%20tables%20and%20needs%20n%20%C3%97%20n%20%C3%97%20columns%20in%20memory%3A%20hundreds%20of%20megabytes%20for%20the%20survey.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20max_percent_match(answers%3A%20pd.DataFrame)%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22For%20each%20row%2C%20the%20share%20of%20columns%20on%20which%20it%20agrees%20with%20its%20most%20similar%20other%20row%20(both%20missing%20counts%20as%0A%20%20%20%20agreeing)%2C%20and%20that%20row's%20label%20(the%20first%20of%20a%20tie).%22%22%22%0A%20%20%20%20%23%20Exercise%20S3.2%3A%20the%20task%20and%20examples%20are%20in%20the%20text%20above%20this%20cell.%0A%20%20%20%20%23%20Step%201%3A%20the%20answers%20as%20a%20NumPy%20array%20of%20floats%3B%20n%20rows%2C%20k%20columns%0A%20%20%20%20%23%20Step%202%3A%20an%20n%20%C3%97%20n%20array%20of%20zeros%3B%20for%20each%20column%2C%20add%201%20where%20two%20rows%20agree%3A%20equal%2C%20or%20both%20missing%0A%20%20%20%20%23%20Step%203%3A%20fill%20the%20diagonal%20with%20-1%2C%20so%20a%20row%20is%20never%20its%20own%20closest%0A%20%20%20%20%23%20Step%204%3A%20for%20each%20row%2C%20the%20largest%20count%20(divided%20by%20k)%20and%20the%20position%20of%20the%20first%20row%20with%20it%20(argmax)%0A%20%20%20%20%23%20Step%205%3A%20return%20a%20table%20with%20the%20answers'%20index%3A%20max_match%2C%20and%20closest%20(the%20index%20label%20at%20that%20position)%0A%20%20%20%20raise%20NotImplementedError(%22Exercise%20S3.2%22)%0A%0A%0A%40app.function%0Adef%20answers_fixture()%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22Five%20interviews%20of%20four%20questions%2C%20used%20by%20the%20check%20of%20Exercise%20S3.2%3A%20an%20exact%20copy%20with%20a%20missing%20answer%20in%0A%20%20%20%20both%2C%20a%20three-way%20tie%2C%20and%20a%20row%20whose%20closest%20is%20the%20last%20in%20the%20table.%22%22%22%0A%20%20%20%20return%20pd.DataFrame(%0A%20%20%20%20%20%20%20%20%7B%22q1%22%3A%20%5B1%2C%201%2C%201%2C%207%2C%207%5D%2C%20%22q2%22%3A%20%5B2%2C%202%2C%209%2C%209%2C%209%5D%2C%20%22q3%22%3A%20%5Bnp.nan%2C%20np.nan%2C%203%2C%203%2C%205%5D%2C%20%22q4%22%3A%20%5B4%2C%204%2C%204%2C%208%2C%208%5D%7D%2C%0A%20%20%20%20%20%20%20%20index%3D%5B%22h1%22%2C%20%22h2%22%2C%20%22h3%22%2C%20%22h4%22%2C%20%22h5%22%5D%2C%0A%20%20%20%20)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(SHORT%2C%20answers)%3A%0A%20%20%20%20check_cases(%0A%20%20%20%20%20%20%20%20%22Stretch%20S3.2%22%2C%20max_percent_match%2C%0A%20%20%20%20%20%20%20%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22five%20interviews%3A%20a%20copy%20with%20a%20missing%20answer%20in%20both%2C%20a%20tie%2C%20a%20closest%20that%20comes%20last%22%2C%20(answers_fixture()%2C))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22the%20survey's%20households%2C%20every%20answer%22%2C%20(answers%2C))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22the%20survey's%20households%2C%20the%20short%20questionnaire%22%2C%20(answers%5BSHORT%5D%2C))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22region%2007's%20households%22%2C%20(answers%5Banswers.index.str.startswith(%2207-%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%22tables%20of%20answers%22%2C%20summarize%3Dlambda%20table%3A%20%7B%22index%22%3A%20%5Bstr(i)%20for%20i%20in%20table.index%5D%2C%20%22max_match%22%3A%20np.round(table%5B%22max_match%22%5D.to_numpy(dtype%3Dfloat)%2C%206).tolist()%2C%20%22closest%22%3A%20%5Bstr(c)%20for%20c%20in%20table%5B%22closest%22%5D%5D%7D%2C%0A%20%20%20%20%20%20%20%20hint%3D%22Count%20agreements%20per%20pair%20one%20column%20at%20a%20time%20(equal%2C%20or%20both%20missing)%3B%20fill%20the%20diagonal%20with%20-1%3B%20max%20%2F%20number%20of%20columns%2C%20and%20the%20label%20at%20argmax.%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%20five%20interviews%20of%20the%20check%0A%20%20%20%20try_it(max_percent_match%2C%20answers_fixture())%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%20S3.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%20Every%20%60max_match%60%20is%201.0%2C%20every%20row%20its%20own%20closest%20%7C%20The%20diagonal%20was%20left%20in.%20%7C%20%60np.fill_diagonal(counts%2C%20-1)%60%20%7C%0A%20%20%20%20%7C%20h1%20and%20h2%20at%200.75%2C%20not%201.00%20%7C%20Two%20missing%20answers%20counted%20as%20different%3A%20%60nan%20%3D%3D%20nan%60%20is%20False.%20%7C%20add%20%60isnan%20%26%20isnan%60%20%7C%0A%20%20%20%20%7C%20h3's%20closest%20is%20h2%20or%20h4%20%7C%20The%20last%20of%20a%20tie%2C%20not%20the%20first.%20%7C%20%60argmax%60%20%7C%0A%20%20%20%20%7C%20%60closest%60%20holds%20numbers%200%2C%201%2C%202%20%7C%20Positions%20returned%2C%20not%20labels.%20%7C%20%60answers.index%5Bbest%5D%60%20%7C%0A%20%20%20%20%7C%20Values%20above%201%2C%20or%20counts%20%7C%20Not%20divided%20by%20the%20number%20of%20columns.%20%7C%20%60%2F%20k%60%20%7C%0A%20%20%20%20%7C%20*MemoryError*%2C%20or%20a%20slow%20cell%20%7C%20All%20columns%20compared%20at%20once%20on%20the%20survey.%20%7C%20one%20column%20at%20a%20time%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.function%0Adef%20copy_pairs()%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22The%20planted%20copies%20and%20the%20household%20each%20copies%2C%20as%20text%20ids%3A%20the%20list%20names%20the%20copy%2C%20and%20its%20detail%20names%20the%0A%20%20%20%20original%20(%22a%20copy%20of%20household%205421%22)%2C%20in%20the%20same%20region.%22%22%22%0A%20%20%20%20copies%20%3D%20planted%5Bplanted%5B%22kind%22%5D%20%3D%3D%20%22household_copied%22%5D%0A%20%20%20%20original%20%3D%20copies%5B%22detail%22%5D.map(lambda%20text%3A%20int(re.search(r%22household%20(%5Cd%2B)%22%2C%20text).group(1)))%0A%20%20%20%20return%20pd.DataFrame(%7B%22copy%22%3A%20copies%5B%22region_code%22%5D%20%2B%20%22-%22%20%2B%20copies%5B%22hh_id%22%5D.astype(str)%2C%20%22original%22%3A%20copies%5B%22region_code%22%5D%20%2B%20%22-%22%20%2B%20original.astype(str)%7D).reset_index(drop%3DTrue)%0A%0A%0A%40app.function%0Adef%20made_up_interviewer()%20-%3E%20str%3A%0A%20%20%20%20%22%22%22The%20interviewer%20of%20the%20planted%20made-up%20interviews%2C%20as%20the%20survey%20records%20it.%22%22%22%0A%20%20%20%20made%20%3D%20planted.loc%5Bplanted%5B%22kind%22%5D%20%3D%3D%20%22made_up_interview%22%2C%20HOUSEHOLD%5D%0A%20%20%20%20return%20households.merge(made%2C%20on%3DHOUSEHOLD)%5B%22hh_interviewer%22%5D.mode().iloc%5B0%5D%0A%0A%0A%40app.function%0Adef%20made_up_households()%20-%3E%20set%3A%0A%20%20%20%20%22%22%22The%20households%20of%20the%20planted%20made-up%20interviews%20(made_up_interview)%2C%20as%20text%20ids.%22%22%22%0A%20%20%20%20made%20%3D%20planted%5Bplanted%5B%22kind%22%5D%20%3D%3D%20%22made_up_interview%22%5D%0A%20%20%20%20return%20set(made%5B%22region_code%22%5D%20%2B%20%22-%22%20%2B%20made%5B%22hh_id%22%5D.astype(str))%0A%0A%0A%40app.cell%0Adef%20_(SHORT%2C%20answers)%3A%0A%20%20%20%20%23%20Who%20passes%20a%20threshold%3A%20the%20planted%20copies%20and%20their%20originals%2C%20the%20made-up%20interviews%2C%20and%20every%20other%20household%2C%0A%20%20%20%20%23%20on%20all%20the%20survey's%20answers%20and%20on%20the%20short%20questionnaire's%0A%20%20%20%20def%20_thresholds()%3A%0A%20%20%20%20%20%20%20%20long_%20%3D%20attempt(max_percent_match%2C%20answers)%0A%20%20%20%20%20%20%20%20if%20long_%20is%20NOT_YET%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20waiting_for(%22Exercise%20S3.2%22)%0A%20%20%20%20%20%20%20%20short%20%3D%20max_percent_match(answers%5BSHORT%5D)%0A%20%20%20%20%20%20%20%20pairs%20%3D%20copy_pairs()%0A%20%20%20%20%20%20%20%20role%20%3D%20pd.Series(%22other%20household%22%2C%20index%3Danswers.index)%0A%20%20%20%20%20%20%20%20made%20%3D%20f%22made%20up%20(%7Bmade_up_interviewer()%7D)%22%0A%20%20%20%20%20%20%20%20role%5Brole.index.isin(made_up_households())%5D%20%3D%20made%0A%20%20%20%20%20%20%20%20role%5Brole.index.isin(pairs%5B%22copy%22%5D)%20%7C%20role.index.isin(pairs%5B%22original%22%5D)%5D%20%3D%20%22planted%20copy%20or%20its%20original%22%0A%20%20%20%20%20%20%20%20rows%20%3D%20%5B%5D%0A%20%20%20%20%20%20%20%20full%2C%20brief%20%3D%20f%22%7Banswers.shape%5B1%5D%7D%20answers%22%2C%20f%22%7Blen(SHORT)%7D%20answers%22%0A%20%20%20%20%20%20%20%20for%20name%2C%20table%20in%20%5B(full%2C%20long_)%2C%20(brief%2C%20short)%5D%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20for%20threshold%20in%20%5B0.85%2C%200.90%2C%200.95%2C%201.00%5D%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20above%20%3D%20table%5B%22max_match%22%5D%20%3E%3D%20threshold%20-%201e-9%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20rows.append(%7B%22questionnaire%22%3A%20name%2C%20%22threshold%22%3A%20f%22%7Bthreshold%3A.0%25%7D%22%2C%20**above.groupby(role).sum().to_dict()%7D)%0A%20%20%20%20%20%20%20%20counts%20%3D%20pd.DataFrame(rows).fillna(0)%0A%20%20%20%20%20%20%20%20found%20%3D%20long_.loc%5Bpairs%5B%22copy%22%5D%5D.assign(original%3Dpairs%5B%22original%22%5D.to_numpy())%0A%20%20%20%20%20%20%20%20others%20%3D%20role%20%3D%3D%20%22other%20household%22%0A%20%20%20%20%20%20%20%20fig%2C%20ax%20%3D%20plt.subplots(figsize%3D(6.5%2C%203))%0A%20%20%20%20%20%20%20%20bins%20%3D%20np.linspace(0.5%2C%201.0%2C%2051)%0A%20%20%20%20%20%20%20%20ax.hist(long_.loc%5Bothers%2C%20%22max_match%22%5D%20*%20100%2C%20bins%3Dbins%20*%20100%2C%20color%3DBLUE%2C%20alpha%3D0.8%2C%20label%3D%22other%20households%22)%0A%20%20%20%20%20%20%20%20ax.hist(long_.loc%5Brole%20%3D%3D%20made%2C%20%22max_match%22%5D%20*%20100%2C%20bins%3Dbins%20*%20100%2C%20color%3DORANGE%2C%20alpha%3D0.9%2C%20label%3D%22made-up%20interviews%22)%0A%20%20%20%20%20%20%20%20ax.hist(long_.loc%5Brole%20%3D%3D%20%22planted%20copy%20or%20its%20original%22%2C%20%22max_match%22%5D%20*%20100%2C%20bins%3Dbins%20*%20100%2C%20color%3D%22%23000000%22%2C%20label%3D%22planted%20copies%20and%20originals%22)%0A%20%20%20%20%20%20%20%20ax.axvline(85%2C%20color%3DGREY%2C%20linestyle%3D%22--%22%2C%20linewidth%3D0.8)%0A%20%20%20%20%20%20%20%20ax.set_xlabel(f%22maximum%20percent%20match%2C%20%7Bfull%7D%20(%25)%22)%0A%20%20%20%20%20%20%20%20ax.set_ylabel(%22households%22)%0A%20%20%20%20%20%20%20%20ax.set_title(%22Each%20household%20against%20its%20most%20similar%20other%20household%22)%0A%20%20%20%20%20%20%20%20ax.legend(frameon%3DFalse%2C%20loc%3D%22upper%20left%22)%0A%20%20%20%20%20%20%20%20ax.spines%5B%5B%22top%22%2C%20%22right%22%5D%5D.set_visible(False)%0A%20%20%20%20%20%20%20%20fig.tight_layout()%0A%20%20%20%20%20%20%20%20at85%20%3D%20counts.set_index(%5B%22questionnaire%22%2C%20%22threshold%22%5D)%0A%20%20%20%20%20%20%20%20long85%2C%20short85%20%3D%20at85.loc%5B(full%2C%20%2285%25%22)%2C%20%22other%20household%22%5D%2C%20at85.loc%5B(brief%2C%20%2285%25%22)%2C%20%22other%20household%22%5D%0A%20%20%20%20%20%20%20%20closest_is_original%20%3D%20bool((found%5B%22closest%22%5D%20%3D%3D%20found%5B%22original%22%5D).all())%0A%20%20%20%20%20%20%20%20whole%20%3D%20int((long_.loc%5Brole%20!%3D%20%22planted%20copy%20or%20its%20original%22%2C%20%22max_match%22%5D%20%3E%3D%201%20-%201e-9).sum())%0A%20%20%20%20%20%20%20%20made_match%20%3D%20long_.loc%5Brole%20%3D%3D%20made%2C%20%22max_match%22%5D%0A%20%20%20%20%20%20%20%20return%20mo.vstack(%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20found%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20counts%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20ax%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20mo.md(f%22**%7B'Each%20copy'%20if%20closest_is_original%20else%20'Not%20every%20copy'%7D's%20closest%20household%20is%20its%20original%2C%20at%20a%20match%20of%20%7Bfound%5B'max_match'%5D.min()%3A.0%25%7D.**%20On%20the%20survey's%20%7Bfull%7D%2C%20%7B'no%20other%20household%20reaches'%20if%20whole%20%3D%3D%200%20else%20f'%7Bwhole%7D%20other%20households%20reach'%7D%20100%25.%20%22%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20f%22At%20Kuriakose%20and%20Robbins's%2085%25%2C%20though%2C%20**%7Bint(long85)%3A%2C%7D%20of%20the%20%7Bint(others.sum())%3A%2C%7D%20other%20households%20pass**%20as%20well%2C%20and%20on%20the%20short%20questionnaire%20of%20%7Bbrief%7D%20**%7Bint(short85)%3A%2C%7D**%20do%3A%20Pew's%20caveat%2C%20measured.%20%22%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22The%20species%20and%20gear%20columns%20are%20mostly%200%20in%20every%20household%2C%20which%20makes%20any%20two%20households%20agree%20on%20most%20of%20them%3B%20a%20short%20questionnaire%20leaves%20a%20few%20answers%20with%20few%20options%2C%20on%20which%20most%20households%20agree.%20%22%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20f%22The%20made-up%20interviews%20sit%20at%20%7Bmade_match.min()%3A.0%25%7D%20to%20%7Bmade_match.max()%3A.0%25%7D%3A%20their%20interviewer%20kept%20the%20boat%20and%20the%20catch%20the%20same%2C%20but%20their%20members'%20answers%20differ%2C%20so%20the%20percent%20match%20does%20not%20single%20them%20out.%20Module%208's%20interviewer%20indicators%20find%20them.%22)%2C%0A%20%20%20%20%20%20%20%20%5D)%0A%0A%20%20%20%20_thresholds()%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%20The%20threshold%20that%20separates%20the%20copies%20here%20is%20this%20survey's%2C%20found%20by%20looking%20at%20the%20distribution%20with%20the%20list%20of%20planted%20defects%20in%20hand.%20On%20another%20questionnaire%2C%20or%20after%20a%20copy%20with%20one%20answer%20changed%2C%20it%20would%20be%20another.%20A%20maximum%20percent%20match%20ranks%20interviews%20for%20a%20second%20look%3B%20the%20cutoff%20is%20a%20review%20budget%2C%20set%20the%20way%20module%209%20set%20contamination%2C%20and%20the%20list%20goes%20beside%20the%20other%20indicators.%0A%0A%20%20%20%20%23%23%20splink%3A%20probabilistic%20record%20linkage%20on%20DuckDB%0A%0A%20%20%20%20**The%20problem.**%20The%20percent%20match%20compares%20every%20household%20with%20every%20other%3A%20n%20households%20make%20n(n%20%E2%88%92%201)%20%2F%202%20pairs%2C%20which%20numpy%20handles%20for%20a%20survey%20of%20a%20few%20thousand%20households%3B%20a%20survey%20of%20a%20million%20households%20makes%20half%20a%20trillion.%20It%20also%20weighs%20every%20answer%20the%20same%2C%20so%20agreeing%20on%20a%20rare%20species%20counts%20no%20more%20than%20agreeing%20on%20%22no%20hired%20crew%22.%20**Record%20linkage**%20is%20the%20field%20that%20finds%20records%20of%20the%20same%20unit%2C%20and%20it%20answers%20both.%0A%0A%20%20%20%20**The%20tool.**%20splink%20(version%205.0.0%2C%20from%20the%20UK%20Ministry%20of%20Justice's%20analytical%20services)%20does%20probabilistic%20record%20linkage%20on%20the%20Fellegi-Sunter%20model%2C%20inside%20a%20database%20engine%3A%20DuckDB%20here.%20Three%20ideas%20carry%20it%3A%0A%0A%20%20%20%20%7C%20idea%20%7C%20what%20it%20does%20%7C%20here%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20**blocking**%20%7C%20compare%20only%20the%20pairs%20that%20share%20something%2C%20instead%20of%20every%20pair%20%7C%20households%20of%20the%20same%20barangay%20(%60place%60)%3A%20a%20copy%20is%20entered%20where%20the%20original%20was%20%7C%0A%20%20%20%20%7C%20**comparisons**%20%7C%20for%20each%20pair%2C%20how%20alike%20each%20field%20is%2C%20in%20levels%20%7C%20the%20member%20count%2C%20the%20roster%20(each%20member's%20sex%20and%20age)%2C%20the%20catch%20list%2C%20the%20first%20boat%2C%20the%20interview%20dates%20%7C%0A%20%20%20%20%7C%20**m%20and%20u**%20%7C%20for%20each%20level%2C%20how%20often%20a%20true%20match%20reaches%20it%20(m)%20and%20how%20often%20two%20different%20households%20do%20(u)%20%7C%20u%20from%20random%20pairs%2C%20m%20from%20the%20pairs%20of%20households%20one%20interviewer%20visited%2C%20by%20expectation%20maximization%20%7C%0A%0A%20%20%20%20**The%20interviewer%20is%20not%20compared.**%20In%20this%20survey%20one%20interviewer%20visited%20every%20household%20of%20a%20barangay%2C%20so%20within%20a%20block%20the%20interviewer%20never%20differs%3A%20splink%20would%20see%20only%20one%20level%20of%20that%20comparison%20and%20could%20not%20estimate%20the%20other%2C%20and%20it%20says%20so%20in%20a%20warning.%20A%20field%20that%20is%20the%20same%20for%20every%20pair%20a%20block%20makes%20tells%20the%20model%20nothing%3B%20here%20it%20serves%20instead%20to%20group%20the%20pairs%20splink%20learns%20m%20from.%0A%0A%20%20%20%20A%20pair's%20**match%20weight**%20adds%20up%2C%20over%20the%20comparisons%2C%20how%20much%20more%20often%20its%20levels%20occur%20among%20matches%20than%20among%20non-matches%3B%20its%20**match%20probability**%20turns%20that%20into%20a%20share.%20A%20level%20that%20is%20rare%20among%20non-matches%2C%20such%20as%20the%20same%20roster%20of%20six%20members%2C%20weighs%20far%20more%20than%20one%20that%20is%20common.%0A%0A%20%20%20%20**%60dedupe_only%60**%20is%20splink's%20link%20type%20for%20one%20table%3A%20find%20the%20records%20within%20it%20that%20are%20the%20same%20unit.%20(%60link_only%60%20links%20two%20tables%3B%20%60link_and_dedupe%60%20does%20both.)%0A%0A%20%20%20%20**What%20runs%20where.**%20splink%20writes%20its%20comparisons%20as%20SQL%20and%20runs%20them%20in%20the%20DuckDB%20connection%20the%20cell%20opens%2C%20on%20this%20computer.%20The%20cell%20builds%20its%20table%20from%20the%20survey%20in%20memory.%20It%20reads%20none%20of%20splink's%20example%20datasets%2C%20which%20%60splink_datasets%60%20downloads%20from%20the%20web%2C%20and%20draws%20none%20of%20splink's%20charts%2C%20which%20load%20their%20JavaScript%20from%20a%20content%20delivery%20network%3A%20the%20model%20and%20its%20pairs%20are%20read%20as%20tables.%20%60SPLINK_SEED%60%20fixes%20the%20random%20pairs%20splink%20draws%20to%20estimate%20u%2C%20so%20every%20run%20trains%20the%20same%20model.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20linkage_table()%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22One%20row%20per%20household%2C%20the%20fields%20splink%20compares%3A%20its%20id%2C%20region%20and%20barangay%2C%20interviewer%2C%20interview%20date%2C%0A%20%20%20%20member%20count%2C%20roster%20(each%20member's%20sex%20and%20age%2C%20by%20line)%2C%20catch%20list%20(species%20codes%2C%20sorted)%20and%20first%20boat.%20The%0A%20%20%20%20interviewer%20groups%20the%20pairs%20splink%20trains%20on%3B%20it%20is%20not%20compared.%22%22%22%0A%20%20%20%20ordered%20%3D%20members.sort_values(%5B*HOUSEHOLD%2C%20%22mem_line%22%5D)%0A%20%20%20%20roster%20%3D%20(ordered%5B%22mem_sex%22%5D.astype(str)%20%2B%20%22%3A%22%20%2B%20ordered%5B%22mem_age%22%5D.astype(str)).groupby(%5Bordered%5B%22region_code%22%5D%2C%20ordered%5B%22hh_id%22%5D%5D).agg(%22%2C%22.join).rename(%22roster%22)%0A%20%20%20%20caught%20%3D%20catch.groupby(HOUSEHOLD)%5B%22catch_species%22%5D.agg(lambda%20codes%3A%20%22%2C%22.join(map(str%2C%20sorted(set(codes))))).rename(%22catch_list%22)%0A%20%20%20%20first%20%3D%20boats.sort_values(%5B*UNIT_KEY%2C%20%22boat_no%22%5D).drop_duplicates(HOUSEHOLD).set_index(HOUSEHOLD)%0A%20%20%20%20measured%20%3D%20first%5B%22boat_measured_in%22%5D%0A%20%20%20%20length%20%3D%20np.select(%5Bmeasured%20%3D%3D%202%2C%20measured%20%3D%3D%203%5D%2C%20%5Bfirst%5B%22boat_length%22%5D%2C%20first%5B%22boat_length%22%5D%20%2F%203.281%5D%2C%20default%3Dnp.nan)%0A%20%20%20%20boat%20%3D%20(first%5B%22boat_hull%22%5D.astype(str)%20%2B%20%22-%22%20%2B%20pd.Series(np.round(length%2C%201)%2C%20index%3Dfirst.index).astype(str)).rename(%22boat%22)%0A%20%20%20%20table%20%3D%20households%5B%5B*HOUSEHOLD%2C%20%22province_code%22%2C%20%22citymun_code%22%2C%20%22barangay_code%22%2C%20%22hh_interviewer%22%2C%20%22hh_interview_date%22%2C%20%22hh_roster_members%22%5D%5D.set_index(HOUSEHOLD).join(%5Broster%2C%20caught%2C%20boat%5D).reset_index()%0A%20%20%20%20return%20pd.DataFrame(%7B%0A%20%20%20%20%20%20%20%20%22unique_id%22%3A%20table%5B%22region_code%22%5D%20%2B%20%22-%22%20%2B%20table%5B%22hh_id%22%5D.astype(str)%2C%0A%20%20%20%20%20%20%20%20%22region_code%22%3A%20table%5B%22region_code%22%5D%2C%0A%20%20%20%20%20%20%20%20%22place%22%3A%20table%5B%22province_code%22%5D%20%2B%20table%5B%22citymun_code%22%5D%20%2B%20table%5B%22barangay_code%22%5D%2C%0A%20%20%20%20%20%20%20%20%22hh_interviewer%22%3A%20table%5B%22hh_interviewer%22%5D%2C%0A%20%20%20%20%20%20%20%20%22interview_date%22%3A%20pd.to_datetime(table%5B%22hh_interview_date%22%5D)%2C%0A%20%20%20%20%20%20%20%20%22members%22%3A%20table%5B%22hh_roster_members%22%5D%2C%0A%20%20%20%20%20%20%20%20%22roster%22%3A%20table%5B%22roster%22%5D%2C%0A%20%20%20%20%20%20%20%20%22catch_list%22%3A%20table%5B%22catch_list%22%5D%2C%0A%20%20%20%20%20%20%20%20%22boat%22%3A%20table%5B%22boat%22%5D%2C%0A%20%20%20%20%7D)%0A%0A%0A%40app.function%0Adef%20splink_pairs(table%3A%20pd.DataFrame%2C%20threshold%3A%20float)%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22splink's%20dedupe_only%20model%20of%20the%20households%2C%20on%20DuckDB%2C%20trained%20with%20a%20fixed%20seed%3A%20the%20pairs%20whose%20match%0A%20%20%20%20probability%20is%20at%20least%20the%20threshold%2C%20with%20unique_id_l%2C%20unique_id_r%2C%20match_weight%20and%20match_probability.%22%22%22%0A%20%20%20%20settings%20%3D%20SettingsCreator(%0A%20%20%20%20%20%20%20%20link_type%3D%22dedupe_only%22%2C%0A%20%20%20%20%20%20%20%20unique_id_column_name%3D%22unique_id%22%2C%0A%20%20%20%20%20%20%20%20comparisons%3D%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20cl.ExactMatch(%22members%22)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20cl.LevenshteinAtThresholds(%22roster%22%2C%20%5B3%5D)%2C%20%20%23%20the%20same%20roster%2C%20or%20one%20a%20few%20characters%20off%3A%20an%20age%20typed%20again%0A%20%20%20%20%20%20%20%20%20%20%20%20cl.LevenshteinAtThresholds(%22catch_list%22%2C%20%5B5%5D)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20cl.ExactMatch(%22boat%22)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20cl.AbsoluteDateDifferenceAtThresholds(%22interview_date%22%2C%20input_is_string%3DFalse%2C%20metrics%3D%5B%22day%22%5D%2C%20thresholds%3D%5B2%5D)%2C%0A%20%20%20%20%20%20%20%20%5D%2C%0A%20%20%20%20%20%20%20%20blocking_rules_to_generate_predictions%3D%5Bblock_on(%22region_code%22%2C%20%22place%22)%5D%2C%0A%20%20%20%20)%0A%20%20%20%20with%20duckdb.connect()%20as%20con%3A%0A%20%20%20%20%20%20%20%20frame%20%3D%20DuckDBAPI(con).register(table)%0A%20%20%20%20%20%20%20%20linker%20%3D%20Linker(frame%2C%20settings%2C%20log_level%3Dlogging.WARNING)%0A%20%20%20%20%20%20%20%20%23%20The%20share%20of%20pairs%20that%20are%20matches%2C%20from%20the%20pairs%20that%20share%20a%20roster%20and%20a%20catch%20list%2C%20assumed%20to%20find%2090%25%20of%20them%0A%20%20%20%20%20%20%20%20linker.training.estimate_probability_two_random_records_match(%5Bblock_on(%22roster%22%2C%20%22catch_list%22)%5D%2C%20recall%3D0.9)%0A%20%20%20%20%20%20%20%20linker.training.estimate_u_using_random_sampling(max_pairs%3D2e6%2C%20seed%3DSPLINK_SEED)%20%20%23%20u%3A%20two%20million%20random%20pairs%2C%20seeded%0A%20%20%20%20%20%20%20%20linker.training.estimate_parameters_using_expectation_maximisation(block_on(%22hh_interviewer%22))%20%20%23%20m%3A%20within%20each%20interviewer's%20households%0A%20%20%20%20%20%20%20%20pairs%20%3D%20linker.inference.predict(threshold_match_probability%3Dthreshold).as_pandas_dataframe()%0A%20%20%20%20pairs%20%3D%20pairs%5B%5B%22unique_id_l%22%2C%20%22unique_id_r%22%2C%20%22match_weight%22%2C%20%22match_probability%22%5D%5D%0A%20%20%20%20return%20pairs.sort_values(%5B%22match_probability%22%2C%20%22unique_id_l%22%2C%20%22unique_id_r%22%5D%2C%20ascending%3D%5BFalse%2C%20True%2C%20True%5D).reset_index(drop%3DTrue)%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20%23%20splink%20on%20the%20survey's%20households%3A%20the%20pairs%20with%20a%20match%20probability%20of%20at%20least%200.5%0A%20%20%20%20linkage%20%3D%20linkage_table()%0A%20%20%20%20pairs%20%3D%20splink_pairs(linkage%2C%200.5)%0A%20%20%20%20mo.vstack(%5B%0A%20%20%20%20%20%20%20%20mo.md(f%22**%7Blen(linkage)%3A%2C%7D%20households**%20in%2C%20**%7Blen(pairs)%7D%20pairs**%20with%20a%20match%20probability%20of%20at%20least%200.5%20out.%20The%20table%20splink%20compared%2C%20first%20rows%3A%22)%2C%0A%20%20%20%20%20%20%20%20linkage.head(3)%2C%0A%20%20%20%20%20%20%20%20mo.md(%22The%20pairs%2C%20most%20likely%20first%3A%22)%2C%0A%20%20%20%20%20%20%20%20pairs.head(12).round(6)%2C%0A%20%20%20%20%5D)%0A%20%20%20%20return%20linkage%2C%20pairs%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.accordion(%7B%22The%20R%20way%22%3A%20mo.md(r%22%22%22%0A%20%20%20%20R's%20record-linkage%20packages%20are%20%60fastLink%60%20(a%20Fellegi-Sunter%20model%20with%20expectation%20maximization%2C%20as%20splink's)%20and%20%60RecordLinkage%60.%20In%20R%2C%20deduplication%20is%20often%20%60duplicated()%60%20on%20a%20few%20columns%2C%20which%20is%20module%208's%20exact%20match.%20The%20percent%20match%20is%20a%20few%20lines%20in%20either%20language%3A%20%60outer(a%2C%20a%2C%20%22%3D%3D%22)%60%20per%20column%2C%20added%20up%2C%20%60diag(counts)%20%3C-%20-1%60%2C%20%60apply(counts%2C%201%2C%20max)%60.%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%20S3.3%20%C2%B7%20Judging%20the%20pairs%20against%20the%20planted%20copies%0A%0A%20%20%20%20**Why%20it%20matters%3A**%20a%20list%20of%20pairs%20is%20judged%20as%20module%209%20judged%20a%20list%20of%20units%2C%20by%20how%20many%20of%20the%20known%20cases%20it%20finds%20and%20what%20else%20it%20brings.%20Two%20things%20differ.%20A%20pair%20is%20**unordered**%3A%20splink%20lists%20a%20pair%20once%2C%20with%20whichever%20id%20it%20puts%20on%20the%20left%2C%20and%20the%20list%20of%20planted%20defects%20names%20the%20copy%2C%20with%20its%20original%20only%20in%20the%20%60detail%60%20text.%20And%20a%20household%20is%20its%20region%20and%20its%20id%20together%2C%20so%20%6001-5421%60%20and%20%6007-5421%60%20are%20different%20households.%20%60copy_pairs()%60%20above%20reads%20the%20list%20into%20pairs%20of%20text%20ids%3B%20%60made_up_households()%60%20gives%20the%20planted%20made-up%20interviews'%20ids.%0A%0A%20%20%20%20**Input%20%E2%86%92%20output%3A**%20splink's%20pairs%20(%60unique_id_l%60%2C%20%60unique_id_r%60%2C%20%60match_probability%60)%2C%20the%20planted%20copies%20as%20%60copy_pairs()%60%20gives%20them%2C%20and%20the%20made-up%20households%20as%20a%20set%20%E2%86%92%20one%20row%20per%20pair%2C%20with%3A%0A%0A%20%20%20%20%7C%20column%20%7C%20what%20it%20is%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20%60household_a%60%2C%20%60household_b%60%20%7C%20the%20pair's%20two%20ids%2C%20the%20smaller%20first%20(as%20text)%20%7C%0A%20%20%20%20%7C%20%60match_probability%60%20%7C%20splink's%20%7C%0A%20%20%20%20%7C%20%60what%60%20%7C%20%60%22planted%20copy%22%60%20when%20the%20pair%20is%20a%20copy%20and%20its%20original%2C%20in%20either%20order%3B%20%60%22both%20made%20up%22%60%20when%20both%20households%20are%20made-up%20interviews%3B%20%60%22other%22%60%20otherwise%20%7C%0A%0A%20%20%20%20sorted%20by%20%60match_probability%60%2C%20largest%20first%2C%20then%20by%20%60household_a%60%20and%20%60household_b%60%3B%20the%20index%20runs%200%2C%201%2C%202%E2%80%A6%0A%0A%20%20%20%20**Tools%3A**%20%60np.minimum%60%20and%20%60np.maximum%60%20on%20two%20text%20columns%20(or%20%60min%60%2C%20%60max%60%20in%20a%20list%20comprehension)%2C%20%60frozenset%60%20for%20an%20unordered%20pair%2C%20%60isin%60%2C%20%60np.select%60%2C%20%60sort_values%60%20with%20two%20directions.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20label_pairs(pairs%3A%20pd.DataFrame%2C%20copies%3A%20pd.DataFrame%2C%20made_up%3A%20set)%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22Each%20pair%20with%20its%20ids%20ordered%20(household_a%20%3C%20household_b)%2C%20its%20match%20probability%20and%20what%20it%20is%3A%20planted%20copy%2C%0A%20%20%20%20both%20made%20up%2C%20or%20other%3B%20the%20most%20likely%20first.%22%22%22%0A%20%20%20%20%23%20Exercise%20S3.3%3A%20the%20task%20and%20examples%20are%20in%20the%20text%20above%20this%20cell.%0A%20%20%20%20%23%20Step%201%3A%20household_a%2C%20the%20smaller%20of%20unique_id_l%20and%20unique_id_r%2C%20and%20household_b%2C%20the%20larger%0A%20%20%20%20%23%20Step%202%3A%20the%20planted%20copies%20as%20unordered%20pairs%20(a%20frozenset%20of%20copy%20and%20original%20each)%2C%20and%20whether%20each%20pair%20is%20one%0A%20%20%20%20%23%20Step%203%3A%20what%3A%20%22planted%20copy%22%2C%20else%20%22both%20made%20up%22%20(both%20households%20in%20made_up)%2C%20else%20%22other%22%20(np.select)%0A%20%20%20%20%23%20Step%204%3A%20sort%20by%20match_probability%20(largest%20first)%2C%20then%20household_a%2C%20household_b%3B%20reset%20the%20index%3B%20return%20the%20four%20columns%0A%20%20%20%20raise%20NotImplementedError(%22Exercise%20S3.3%22)%0A%0A%0A%40app.function%0Adef%20pairs_fixture()%20-%3E%20tuple%3A%0A%20%20%20%20%22%22%22Five%20pairs%2C%20the%20planted%20copies%20and%20the%20made-up%20households%2C%20used%20by%20the%20check%20of%20Exercise%20S3.3%3A%20a%20planted%20pair%0A%20%20%20%20listed%20copy%20first%2C%20the%20same%20household%20numbers%20in%20another%20region%2C%20two%20made-up%20households%2C%20a%20made-up%20household%20beside%0A%20%20%20%20an%20honest%20one%2C%20and%20a%20tie.%22%22%22%0A%20%20%20%20pairs%20%3D%20pd.DataFrame(%7B%0A%20%20%20%20%20%20%20%20%22unique_id_l%22%3A%20%5B%2201-5807%22%2C%20%2207-5421%22%2C%20%2204-5576%22%2C%20%2204-5591%22%2C%20%2201-5200%22%5D%2C%0A%20%20%20%20%20%20%20%20%22unique_id_r%22%3A%20%5B%2201-5421%22%2C%20%2207-5807%22%2C%20%2204-5591%22%2C%20%2204-5300%22%2C%20%2201-5100%22%5D%2C%0A%20%20%20%20%20%20%20%20%22match_probability%22%3A%20%5B0.99%2C%200.99%2C%200.95%2C%200.60%2C%200.95%5D%2C%0A%20%20%20%20%7D)%0A%20%20%20%20copies%20%3D%20pd.DataFrame(%7B%22copy%22%3A%20%5B%2201-5807%22%2C%20%2204-5851%22%5D%2C%20%22original%22%3A%20%5B%2201-5421%22%2C%20%2204-5394%22%5D%7D)%0A%20%20%20%20return%20pairs%2C%20copies%2C%20%7B%2204-5576%22%2C%20%2204-5591%22%7D%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(linkage%2C%20pairs)%3A%0A%20%20%20%20check_cases(%0A%20%20%20%20%20%20%20%20%22Stretch%20S3.3%22%2C%20label_pairs%2C%0A%20%20%20%20%20%20%20%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22five%20pairs%3A%20a%20copy%20listed%20copy%20first%2C%20the%20same%20numbers%20in%20another%20region%2C%20made-up%20households%2C%20a%20tie%22%2C%20pairs_fixture())%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22splink's%20pairs%20on%20the%20survey%2C%20probability%200.5%20or%20more%22%2C%20(pairs%2C%20copy_pairs()%2C%20made_up_households()))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22splink's%20pairs%20on%20the%20survey's%20region%2007%2C%20probability%200.9%20or%20more%22%2C%20(splink_pairs(linkage%5Blinkage%5B%22region_code%22%5D%20%3D%3D%20%2207%22%5D%2C%200.9)%2C%20copy_pairs()%2C%20made_up_households()))%2C%0A%20%20%20%20%20%20%20%20%5D%2C%0A%20%20%20%20%20%20%20%20what%3D%22lists%20of%20pairs%22%2C%20summarize%3Dlambda%20table%3A%20table%5B%5B%22household_a%22%2C%20%22household_b%22%2C%20%22match_probability%22%2C%20%22what%22%5D%5D.round(6).astype(%7B%22household_a%22%3A%20str%2C%20%22household_b%22%3A%20str%2C%20%22what%22%3A%20str%7D)%2C%0A%20%20%20%20%20%20%20%20hint%3D%22Smaller%20id%20first%20(np.minimum%2C%20np.maximum)%3B%20a%20planted%20pair%20matches%20as%20a%20frozenset%20of%20copy%20and%20original%3B%20then%20'both%20made%20up'%3B%20sort%20by%20probability%20(largest%20first)%2C%20then%20the%20ids.%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%20check's%20five%20pairs%0A%20%20%20%20try_it(label_pairs%2C%20*pairs_fixture())%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%20S3.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%20The%20first%20small%20pair%20is%20%60other%60%20%7C%20The%20copy%20compared%20with%20%60unique_id_l%60%20and%20the%20original%20with%20%60unique_id_r%60%3A%20this%20pair%20is%20listed%20the%20other%20way%20round.%20%7C%20an%20unordered%20pair%3A%20%60frozenset%60%20%7C%0A%20%20%20%20%7C%20%6007-5421%60%20with%20%6007-5807%60%20is%20a%20%60planted%20copy%60%20%7C%20The%20household%20numbers%20compared%20without%20their%20region.%20%7C%20compare%20the%20whole%20ids%20%7C%0A%20%20%20%20%7C%20%60household_a%60%20is%20larger%20than%20%60household_b%60%20in%20some%20rows%20%7C%20splink's%20order%20kept.%20%7C%20%60np.minimum%60%2C%20%60np.maximum%60%20%7C%0A%20%20%20%20%7C%20The%20tie%20at%200.95%20in%20another%20order%20%7C%20Sorted%20by%20probability%20alone.%20%7C%20then%20%60household_a%60%2C%20%60household_b%60%20%7C%0A%20%20%20%20%7C%20The%20pair%20with%20one%20made-up%20household%20is%20%60both%20made%20up%60%20%7C%20%60%7C%60%20where%20%60%26%60%20is%20wanted.%20%7C%20both%20ids%20in%20%60made_up%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_(answers%2C%20pairs)%3A%0A%20%20%20%20%23%20splink's%20pairs%2C%20judged%3A%20what%20they%20are%2C%20and%20each%20planted%20pair's%20percent%20match%20beside%20its%20probability%0A%20%20%20%20def%20_judge()%3A%0A%20%20%20%20%20%20%20%20labeled%20%3D%20attempt(label_pairs%2C%20pairs%2C%20copy_pairs()%2C%20made_up_households())%0A%20%20%20%20%20%20%20%20if%20labeled%20is%20NOT_YET%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20waiting_for(%22Exercise%20S3.3%22)%0A%20%20%20%20%20%20%20%20counts%20%3D%20labeled%5B%22what%22%5D.value_counts().reindex(%5B%22planted%20copy%22%2C%20%22both%20made%20up%22%2C%20%22other%22%5D%2C%20fill_value%3D0)%0A%20%20%20%20%20%20%20%20copies%20%3D%20labeled%5Blabeled%5B%22what%22%5D%20%3D%3D%20%22planted%20copy%22%5D%0A%20%20%20%20%20%20%20%20match%20%3D%20attempt(max_percent_match%2C%20answers)%0A%20%20%20%20%20%20%20%20if%20match%20is%20not%20NOT_YET%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20a%20%3D%20answers.to_numpy(dtype%3Dfloat)%0A%20%20%20%20%20%20%20%20%20%20%20%20position%20%3D%20%7Blabel%3A%20k%20for%20k%2C%20label%20in%20enumerate(answers.index)%7D%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20def%20share(x%2C%20y)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20left%2C%20right%20%3D%20a%5Bposition%5Bx%5D%5D%2C%20a%5Bposition%5By%5D%5D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20return%20float(((left%20%3D%3D%20right)%20%7C%20(np.isnan(left)%20%26%20np.isnan(right))).mean())%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20labeled%20%3D%20labeled.assign(percent_match%3D%5Bshare(x%2C%20y)%20for%20x%2C%20y%20in%20zip(labeled%5B%22household_a%22%5D%2C%20labeled%5B%22household_b%22%5D)%5D)%0A%20%20%20%20%20%20%20%20made%20%3D%20labeled%5Blabeled%5B%22what%22%5D%20%3D%3D%20%22both%20made%20up%22%5D%0A%20%20%20%20%20%20%20%20on_top%20%3D%20bool(labeled.head(len(copies))%5B%22what%22%5D.eq(%22planted%20copy%22).all())%0A%20%20%20%20%20%20%20%20note%20%3D%20(f%22**splink%20finds%20%7Blen(copies)%7D%20of%20the%20%7Blen(copy_pairs())%7D%20planted%20copies**%2C%20each%20at%20a%20probability%20of%20%7Bcopies%5B'match_probability'%5D.min()%3A.6f%7D%20or%20more%2C%20%7B'at%20the%20top%20of%20the%20list'%20if%20on_top%20else%20'among%20the%20first%20pairs'%7D.%20%22%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20f%22Beside%20them%20come%20**%7Bcounts%5B'both%20made%20up'%5D%7D%20pairs%20of%20made-up%20interviews**%3A%20interviewer%20%7Bmade_up_interviewer()%7D's%20households%20share%20the%20boat%20and%20the%20catch%20the%20interviewer%20invented%2C%20and%20interview%20dates%20close%20together%2C%20and%20a%20model%20of%20near-duplicates%20finds%20them%20alike%2C%20which%20they%20are.%20%22%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20f%22%7Bcounts%5B'other'%5D%7D%20pairs%20are%20neither.%22)%0A%20%20%20%20%20%20%20%20if%20%22percent_match%22%20in%20labeled%20and%20len(made)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20copied%20%3D%20labeled.loc%5Blabeled%5B%22what%22%5D%20%3D%3D%20%22planted%20copy%22%2C%20%22percent_match%22%5D%0A%20%20%20%20%20%20%20%20%20%20%20%20note%20%2B%3D%20f%22%20The%20made-up%20pairs'%20percent%20match%20is%20%7Bmade%5B'percent_match'%5D.min()%3A.0%25%7D%20to%20%7Bmade%5B'percent_match'%5D.max()%3A.0%25%7D%20on%20all%20%7Banswers.shape%5B1%5D%7D%20answers%2C%20below%20the%20copies'%20%7Bcopied.min()%3A.0%25%7D%3A%20splink%20weighs%20the%20rare%20agreements%20(the%20same%20boat%2C%20the%20same%20unusual%20catch%20list)%20that%20the%20percent%20match%20counts%20as%20one%20answer%20each.%22%0A%20%20%20%20%20%20%20%20return%20mo.vstack(%5Bcounts.rename(%22pairs%22).to_frame()%2C%20labeled.head(15).round(4)%2C%20mo.md(note)%5D)%0A%0A%20%20%20%20_judge()%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(answers%2C%20linkage)%3A%0A%20%20%20%20%23%20One%20planted%20copy%20with%20one%20age%20typed%20again%2C%20one%20year%20off%3A%20where%20each%20tool%20ranks%20it%20now%0A%20%20%20%20def%20_one_changed()%3A%0A%20%20%20%20%20%20%20%20first%20%3D%20copy_pairs().iloc%5B0%5D%0A%20%20%20%20%20%20%20%20copy%2C%20original%20%3D%20first%5B%22copy%22%5D%2C%20first%5B%22original%22%5D%0A%20%20%20%20%20%20%20%20changed%20%3D%20answers.copy()%0A%20%20%20%20%20%20%20%20changed.loc%5Bcopy%2C%20%22mem_age_line1%22%5D%20%3D%20changed.loc%5Bcopy%2C%20%22mem_age_line1%22%5D%20%2B%201%0A%20%20%20%20%20%20%20%20match%20%3D%20attempt(max_percent_match%2C%20changed)%0A%20%20%20%20%20%20%20%20if%20match%20is%20NOT_YET%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20waiting_for(%22Exercise%20S3.2%22)%0A%20%20%20%20%20%20%20%20higher%20%3D%20set(match.index%5Bmatch%5B%22max_match%22%5D%20%3E%20match.loc%5Bcopy%2C%20%22max_match%22%5D%5D)%0A%20%20%20%20%20%20%20%20others%20%3D%20set(copy_pairs().iloc%5B1%3A%5D%5B%5B%22copy%22%2C%20%22original%22%5D%5D.to_numpy().ravel())%0A%20%20%20%20%20%20%20%20edited%20%3D%20linkage.copy()%0A%20%20%20%20%20%20%20%20row%20%3D%20edited%5B%22unique_id%22%5D%20%3D%3D%20copy%0A%20%20%20%20%20%20%20%20people%20%3D%20edited.loc%5Brow%2C%20%22roster%22%5D.iloc%5B0%5D.split(%22%2C%22)%0A%20%20%20%20%20%20%20%20sex%2C%20age%20%3D%20people%5B0%5D.split(%22%3A%22)%0A%20%20%20%20%20%20%20%20edited.loc%5Brow%2C%20%22roster%22%5D%20%3D%20%22%2C%22.join(%5Bf%22%7Bsex%7D%3A%7Bint(age)%20%2B%201%7D%22%2C%20*people%5B1%3A%5D%5D)%0A%20%20%20%20%20%20%20%20found%20%3D%20splink_pairs(edited%2C%200.01)%0A%20%20%20%20%20%20%20%20hit%20%3D%20found%5B(found%5B%5B%22unique_id_l%22%2C%20%22unique_id_r%22%5D%5D.apply(frozenset%2C%20axis%3D1)%20%3D%3D%20frozenset((copy%2C%20original)))%5D%0A%20%20%20%20%20%20%20%20if%20hit.empty%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20linked%20%3D%20%22In%20splink's%20list%20the%20pair%20is%20gone%3A%20its%20probability%20is%20below%200.01%22%0A%20%20%20%20%20%20%20%20else%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20place%20%3D%20int(hit.index%5B0%5D)%20%2B%201%0A%20%20%20%20%20%20%20%20%20%20%20%20linked%20%3D%20f%22In%20splink's%20list%20the%20pair%20falls%20to%20number%20%7Bplace%7D%20of%20%7Blen(found)%7D%2C%20at%20a%20probability%20of%20%7Bhit%5B'match_probability'%5D.iloc%5B0%5D%3A.4f%7D%22%0A%20%20%20%20%20%20%20%20who%20%3D%20%22the%20other%20planted%20copies%20and%20their%20originals%22%20if%20higher%20%3D%3D%20others%20else%20%22%2C%20%22.join(sorted(higher))%0A%20%20%20%20%20%20%20%20return%20mo.md(f%22Copy%20**%7Bcopy%7D**%20with%20its%20first%20member%20one%20year%20older%3A%20its%20maximum%20percent%20match%20is%20now%20**%7Bmatch.loc%5Bcopy%2C%20'max_match'%5D%3A.1%25%7D**%2C%20its%20closest%20household%20is%20still%20**%7Bmatch.loc%5Bcopy%2C%20'closest'%5D%7D**%2C%20and%20the%20%7Blen(higher)%7D%20households%20that%20score%20higher%20are%20%7Bwho%7D.%20%22%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20f%22%7Blinked%7D%3A%20the%20roster%20is%20no%20longer%20the%20same%2C%20only%20a%20few%20characters%20off%2C%20and%20the%20pair%20loses%20that%20comparison's%20weight.%20The%20percent%20match%20lost%20one%20answer%20in%20%7Banswers.shape%5B1%5D%7D.%22)%0A%0A%20%20%20%20_one_changed()%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**Two%20tools%2C%20two%20kinds%20of%20question.**%20The%20percent%20match%20asks%20how%20alike%20two%20interviews%20are%2C%20answer%20by%20answer%2C%20and%20needs%20no%20model%3A%20it%20is%20the%20check%20to%20run%20on%20every%20round%2C%20and%20its%20threshold%20is%20a%20review%20budget.%20splink%20asks%20how%20likely%20two%20records%20are%20the%20same%20unit%2C%20weighs%20each%20agreement%20by%20how%20rare%20it%20is%2C%20and%20scales%20to%20tables%20numpy%20cannot%20pair%20up%2C%20at%20the%20price%20of%20a%20model%20to%20specify%20and%20train.%20Both%20found%20the%20planted%20copies%20here%20because%20they%20are%20exact%3B%20the%20cell%20above%20shows%20how%20each%20ranks%20a%20copy%20with%20one%20answer%20changed.%20Neither%20says%20why%20two%20interviews%20are%20alike.%20A%20copy%2C%20a%20made-up%20block%20and%20two%20honest%20neighbors%20with%20the%20same%20boat%20can%20all%20look%20like%20near-duplicates%2C%20and%20telling%20them%20apart%20is%20a%20supervisor's%20call%2C%20with%20the%20interviewer%20indicators%20of%20module%208%20beside%20the%20list.%0A%0A%20%20%20%20%23%23%20S3.4%20%C2%B7%20The%20large%20table%2C%20end%20to%20end%0A%0A%20%20%20%20**Why%20it%20matters%3A**%20module%209%20found%20the%20useful%20pipeline%20for%20the%20large%20table%3A%20the%20rules%2C%20which%20find%20every%20scale%20error%2C%20made-up%20interview%20and%20inflated%20weight%2C%20and%20the%202021%20round's%20random%20forest%2C%20which%20finds%20the%20wrong%20units%20of%20measure%20the%20rules%20miss.%20It%20ran%20them%20across%20several%20cells.%20For%20the%20next%20round%2C%20the%20pipeline%20is%20one%20function%2C%20from%20the%20file%20to%20the%20review%20list%2C%20that%20gives%20the%20same%20list%20whatever%20the%20chunk%20size%2C%20so%20that%20the%20list%20does%20not%20depend%20on%20how%20much%20memory%20the%20computer%20had.%0A%0A%20%20%20%20**Input%20%E2%86%92%20output%3A**%20the%20Parquet%20file's%20%60path%60%2C%20the%202021%20round's%20table%20%60past%60%2C%20a%20%60budget%60%20(how%20many%20units%20the%20supervisors%20can%20review)%20and%20%60chunk_rows%60%20%E2%86%92%20the%20review%20list%2C%20a%20table%20of%20at%20most%20%60budget%60%20rows%20with%20the%20unit's%20key%20(%60region_code%60%2C%20%60hh_id%60%2C%20%60operator_line%60%2C%20%60unit_no%60)%2C%20%60rules%60%20(how%20many%20of%20the%20three%20rules%20the%20unit%20fails)%20and%20%60edit_share%60%20(the%20forest's%20probability%20of%20%22edited%22)%2C%20the%20index%200%2C%201%2C%202%E2%80%A6%2C%20by%20these%20steps%3A%0A%0A%20%20%20%201.%20the%20forest%3A%20%60fit_forest(past)%60%2C%20module%209's%20(200%20trees%2C%20%60random_state%3D0%60%2C%20the%20weight%20edits%20left%20out)%3B%0A%20%20%20%202.%20the%20median%20weight%20of%20the%20**whole%20file**%2C%20once%2C%20before%20any%20chunk%3A%20%60SELECT%20median(sel_final_weight)%20FROM%20read_parquet(...)%60%20in%20DuckDB%3B%0A%20%20%20%203.%20for%20each%20chunk%20of%20%60chunk_rows%60%20rows%20(%60pq.ParquetFile(path).iter_batches(batch_size%3Dchunk_rows)%60)%3A%20the%20three%20rules%20(the%20scale%20against%20the%20largest%20boat%2C%20an%20interview%20under%2020%20minutes%2C%20a%20weight%20over%205%20times%20the%20median%20of%20step%202)%2C%20the%20number%20each%20unit%20fails%2C%20and%20%60fit_forest%60's%20probability%20%60predict_proba(tree_matrix(chunk))%5B%3A%2C%201%5D%60%3B%20keep%20the%20units%20that%20fail%20a%20rule%20or%20whose%20probability%20is%20at%20least%200.5%3B%0A%20%20%20%204.%20all%20chunks'%20units%20together%2C%20sorted%20by%20%60rules%60%2C%20then%20%60edit_share%60%2C%20both%20largest%20first%2C%20then%20by%20the%20unit's%20key%3B%20the%20first%20%60budget%60%20rows.%0A%0A%20%20%20%20%7C%20the%20rule%20%7C%20fails%20when%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20scale%20(U3)%20%7C%20%60unit_scale%60%20is%201%20and%20%60largest_tonnage%60%20is%205%20or%20more%2C%20or%20%60unit_scale%60%20is%202%20and%20%60largest_tonnage%60%20is%20under%205%20%7C%0A%20%20%20%20%7C%20short%20interview%20%7C%20%60hh_interview_minutes%60%20under%2020%20%7C%0A%20%20%20%20%7C%20weight%20%7C%20%60sel_final_weight%60%20above%205%20times%20the%20median%20weight%20of%20the%20whole%20file%20(module%209%20set%20this%20cut%20for%20this%20table)%20%7C%0A%0A%20%20%20%20**Tools%3A**%20%60duckdb.connect()%60%20and%20%60.execute(...).fetchone()%60%20(DuckDB%202.0%20has%20no%20%60fetchone%60%20on%20a%20relation)%2C%20%60iter_batches%60%2C%20%60.to_pandas()%60%2C%20boolean%20columns%20added%20up%20as%20numbers%2C%20%60pd.concat%60%2C%20%60sort_values%60%20with%20six%20keys%2C%20%60head%60.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20tree_matrix(table%3A%20pd.DataFrame)%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22The%20features%20for%20trees%3A%20the%20twelve%20as%20they%20are%2C%20and%20large_scale%20(1%20when%20unit_scale%20is%202%2C%20a%20boat%20of%205.0%20gross%20tons%20or%20more).%22%22%22%0A%20%20%20%20return%20table%5BFEATURES%5D.assign(large_scale%3D(table%5B%22unit_scale%22%5D%20%3D%3D%202).astype(int))%0A%0A%0A%40app.function%0Adef%20fit_forest(past%3A%20pd.DataFrame)%20-%3E%20RandomForestClassifier%3A%0A%20%20%20%20%22%22%22The%20forest%20for%20use%3A%20200%20trees%2C%20random_state%3D0%2C%20fitted%20on%20every%20unit%20of%20the%202021%20round%20but%20the%20weight%20edits.%22%22%22%0A%20%20%20%20rows%20%3D%20past%5Bpast%5B%22edit_kind%22%5D%20!%3D%20%22weight_inflated%22%5D%0A%20%20%20%20return%20RandomForestClassifier(n_estimators%3D200%2C%20random_state%3D0).fit(tree_matrix(rows)%2C%20rows%5B%22was_edited%22%5D)%0A%0A%0A%40app.function%0Adef%20review_list(path%2C%20past%3A%20pd.DataFrame%2C%20budget%3A%20int%2C%20chunk_rows%3A%20int)%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22The%20review%20list%20of%20a%20large%20table%3A%20the%20units%20that%20fail%20a%20rule%20or%20that%20the%202021%20round's%20forest%20calls%20edited%2C%20the%0A%20%20%20%20rules%20and%20the%20forest's%20probability%2C%20most%20suspect%20first%2C%20at%20most%20budget%20of%20them%3B%20the%20same%20for%20any%20chunk%20size.%22%22%22%0A%20%20%20%20%23%20Exercise%20S3.4%3A%20the%20task%20and%20examples%20are%20in%20the%20text%20above%20this%20cell.%0A%20%20%20%20%23%20Step%201%3A%20the%20forest%3A%20fit_forest(past)%0A%20%20%20%20%23%20Step%202%3A%20the%20median%20sel_final_weight%20of%20the%20whole%20file%2C%20in%20DuckDB%3A%20con.execute(f%22SELECT%20median(sel_final_weight)%20FROM%20read_parquet('%7Bpath%7D')%22).fetchone()%5B0%5D%0A%20%20%20%20%23%20Step%203%3A%20for%20each%20batch%20of%20chunk_rows%20rows%3A%20as%20pandas%3B%20rules%20%3D%20the%20number%20of%20the%20three%20rules%20it%20fails%3B%20edit_share%20%3D%20the%0A%20%20%20%20%23%20%20%20%20%20%20%20%20%20forest's%20predict_proba(tree_matrix(chunk))%5B%3A%2C%201%5D%3B%20keep%20the%20rows%20with%20rules%20%3E%200%20or%20edit_share%20%3E%3D%200.5%0A%20%20%20%20%23%20Step%204%3A%20concat%20the%20kept%20rows%3B%20sort%20by%20rules%20and%20edit_share%20(largest%20first)%2C%20then%20the%20unit's%20key%20(UNIT_KEY)%3B%0A%20%20%20%20%23%20%20%20%20%20%20%20%20%20the%20first%20budget%20rows%2C%20index%20reset%2C%20columns%20region_code%2C%20hh_id%2C%20operator_line%2C%20unit_no%2C%20rules%2C%20edit_share%0A%20%20%20%20raise%20NotImplementedError(%22Exercise%20S3.4%22)%0A%0A%0A%40app.function%0Adef%20small_round_file()%3A%0A%20%20%20%20%22%22%22A%20Parquet%20file%20of%20eight%20units%20in%20scratch%2F%2C%20read%20three%20rows%20at%20a%20time%20by%20the%20check%20of%20Exercise%20S3.4%3A%20a%20scale%20that%0A%20%20%20%20contradicts%20the%20boat%2C%20a%20short%20interview%2C%20large%20boats%20in%20large-scale%20units%2C%20and%20a%20weight%20that%20is%20over%205%20times%20the%0A%20%20%20%20median%20of%20its%20own%20chunk%20but%20not%20of%20the%20file.%20Returns%20its%20path.%22%22%22%0A%20%20%20%20rows%20%3D%20%5B%0A%20%20%20%20%20%20%20%20%23%20region_code%2C%20hh_id%2C%20operator_line%2C%20unit_no%2C%20unit_scale%2C%20n_boats%2C%20largest_tonnage%2C%20longest_boat_m%2C%20sum_tonnage%2C%20sum_engine_hp%2C%20n_species%2C%20n_gears%2C%20n_landings%2C%20n_crew%2C%20hh_size%2C%20age_of_operator%2C%20hh_interview_minutes%2C%20sel_final_weight%0A%20%20%20%20%20%20%20%20(1%2C%20900001%2C%201%2C%201%2C%201%2C%201%2C%200.9%2C%207.5%2C%200.9%2C%209.0%2C%204%2C%201%2C%201%2C%200%2C%204%2C%2045%2C%2050%2C%2016.0)%2C%0A%20%20%20%20%20%20%20%20(1%2C%20900002%2C%201%2C%201%2C%201%2C%201%2C%209.9%2C%2014.0%2C%209.9%2C%2030.0%2C%203%2C%202%2C%201%2C%201%2C%203%2C%2038%2C%2055%2C%2016.8)%2C%20%20%23%205%20gross%20tons%20or%20more%20in%20a%20small-scale%20unit%0A%20%20%20%20%20%20%20%20(4%2C%20900003%2C%201%2C%201%2C%201%2C%201%2C%201.1%2C%208.0%2C%201.1%2C%2012.0%2C%205%2C%201%2C%201%2C%200%2C%205%2C%2050%2C%2012%2C%2090.0)%2C%20%20%23%20a%20short%20interview%2C%20and%20a%20weight%20over%205%20times%20the%20file's%20median%0A%20%20%20%20%20%20%20%20(4%2C%20900004%2C%201%2C%201%2C%202%2C%201%2C%2034.0%2C%2024.6%2C%2034.0%2C%20300.0%2C%206%2C%201%2C%201%2C%206%2C%205%2C%2051%2C%2047%2C%201.0)%2C%20%20%23%20a%20large%20boat%20in%20a%20large-scale%20unit%0A%20%20%20%20%20%20%20%20(7%2C%20900005%2C%201%2C%201%2C%201%2C%201%2C%200.8%2C%207.0%2C%200.8%2C%200.0%2C%203%2C%201%2C%201%2C%200%2C%204%2C%2040%2C%2045%2C%201.05)%2C%0A%20%20%20%20%20%20%20%20(7%2C%20900006%2C%201%2C%201%2C%201%2C%201%2C%200.7%2C%206.8%2C%200.7%2C%208.0%2C%202%2C%201%2C%201%2C%200%2C%203%2C%2036%2C%2048%2C%206.0)%2C%20%20%23%206.0%20is%20over%205%20times%20its%20chunk's%20median%2C%201.05%2C%20not%20the%20file's%0A%20%20%20%20%20%20%20%20(7%2C%20900007%2C%201%2C%201%2C%202%2C%203%2C%2058.0%2C%2022.0%2C%20140.0%2C%20900.0%2C%207%2C%202%2C%202%2C%208%2C%206%2C%2049%2C%2061%2C%201.0)%2C%0A%20%20%20%20%20%20%20%20(1%2C%20900008%2C%201%2C%201%2C%201%2C%201%2C%201.0%2C%207.2%2C%201.0%2C%2010.0%2C%204%2C%201%2C%201%2C%201%2C%204%2C%2044%2C%2052%2C%2016.4)%2C%0A%20%20%20%20%5D%0A%20%20%20%20table%20%3D%20pd.DataFrame(rows%2C%20columns%3D%5B*UNIT_KEY%2C%20%22unit_scale%22%2C%20*FEATURES%2C%20%22sel_final_weight%22%5D)%0A%20%20%20%20SCRATCH.mkdir(parents%3DTrue%2C%20exist_ok%3DTrue)%0A%20%20%20%20path%20%3D%20SCRATCH%20%2F%20%22small_round.parquet%22%0A%20%20%20%20table.to_parquet(path%2C%20index%3DFalse)%0A%20%20%20%20return%20path%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20%23%20The%202021%20round%2C%20for%20the%20forest%0A%20%20%20%20past%20%3D%20pd.read_parquet(PATHS.edits_2021)%0A%20%20%20%20return%20(past%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(past)%3A%0A%20%20%20%20check_cases(%0A%20%20%20%20%20%20%20%20%22Stretch%20S3.4%22%2C%20review_list%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%20units%2C%20three%20at%20a%20time%3A%20a%20weight%20over%205%20times%20its%20chunk's%20median%20but%20not%20the%20file's%22%2C%20(small_round_file()%2C%20past%2C%2010%2C%203))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22the%20large%20table%2C%20a%20budget%20of%201%2C000%2C%20chunks%20of%2060%2C000%22%2C%20(PATHS.all_regions%2C%20past%2C%201000%2C%2060_000))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22the%20large%20table%2C%20a%20budget%20of%201%2C000%2C%20chunks%20of%2070%2C000%20(the%20last%20is%2030%2C000)%22%2C%20(PATHS.all_regions%2C%20past%2C%201000%2C%2070_000))%2C%0A%20%20%20%20%20%20%20%20%5D%2C%0A%20%20%20%20%20%20%20%20what%3D%22files%20and%20budgets%22%2C%20summarize%3Dlambda%20table%3A%20table%5B%5B*UNIT_KEY%2C%20%22rules%22%2C%20%22edit_share%22%5D%5D.astype(%7B%22region_code%22%3A%20int%2C%20%22hh_id%22%3A%20int%2C%20%22operator_line%22%3A%20int%2C%20%22unit_no%22%3A%20int%2C%20%22rules%22%3A%20int%7D).round(6)%2C%0A%20%20%20%20%20%20%20%20hint%3D%22The%20forest%20and%20the%20file's%20median%20weight%20once%2C%20before%20the%20loop%3B%20rules%20and%20the%20forest's%20probability%20per%20chunk%3B%20keep%20rules%20%3E%200%20or%20probability%20%3E%3D%200.5%3B%20sort%20the%20whole%20list%2C%20then%20take%20the%20budget.%22%2C%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(past)%3A%0A%20%20%20%20%23%20Try%20it%3A%20the%20large%20table's%20review%20list%2C%20a%20budget%20of%201%2C000%0A%20%20%20%20try_it(review_list%2C%20PATHS.all_regions%2C%20past%2C%201000%2C%2060_000)%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%20S3.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%20900006%20on%20the%20small%20file's%20list%20%7C%20The%20weight%20rule%20used%20each%20chunk's%20own%20median.%20%7C%20the%20file's%20median%2C%20once%2C%20before%20the%20loop%20%7C%0A%20%20%20%20%7C%20Right%20for%20chunks%20of%2060%2C000%2C%20wrong%20for%2070%2C000%20%7C%20The%20budget%20applied%20per%20chunk%2C%20or%20the%20last%2C%20shorter%20chunk%20left%20out.%20%7C%20%60head(budget)%60%20after%20the%20whole%20list%20is%20sorted%20%7C%0A%20%20%20%20%7C%20Probabilities%20that%20change%20with%20the%20chunk%20size%20%7C%20A%20forest%20fitted%20inside%20the%20loop.%20%7C%20%60fit_forest%60%20once%20%7C%0A%20%20%20%20%7C%20The%20made-up%20interviews%20and%20weights%20at%20the%20bottom%20%7C%20Sorted%20by%20the%20probability%20first%2C%20or%20%60rules%60%20sorted%20smallest%20first.%20%7C%20%60%5B%22rules%22%2C%20%22edit_share%22%2C%20...%5D%60%2C%20both%20descending%20%7C%0A%20%20%20%20%7C%20*AttributeError%3A%20...%20has%20no%20attribute%20'fetchone'*%20%7C%20%60con.sql(...).fetchone()%60%3A%20DuckDB%202.0's%20relations%20have%20no%20%60fetchone%60.%20%7C%20%60con.execute(...).fetchone()%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_(past)%3A%0A%20%20%20%20%23%20The%20review%20list%20judged%20against%20the%20large%20table's%20list%20of%20defects%3A%20what%20it%20holds%2C%20kind%20by%20kind%2C%20at%20three%20budgets%0A%20%20%20%20def%20_judge()%3A%0A%20%20%20%20%20%20%20%20whole%20%3D%20attempt(review_list%2C%20PATHS.all_regions%2C%20past%2C%2010**9%2C%2060_000)%0A%20%20%20%20%20%20%20%20if%20whole%20is%20NOT_YET%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20waiting_for(%22Exercise%20S3.4%22)%0A%20%20%20%20%20%20%20%20defects%20%3D%20pd.read_csv(PATHS.all_regions_defects)%0A%20%20%20%20%20%20%20%20kinds%20%3D%20sorted(defects%5B%22kind%22%5D.unique())%0A%20%20%20%20%20%20%20%20rows%20%3D%20%5B%5D%0A%20%20%20%20%20%20%20%20for%20budget%20in%20%5B500%2C%201000%2C%20len(whole)%5D%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20listed%20%3D%20whole.head(budget).merge(defects%2C%20on%3DUNIT_KEY%2C%20how%3D%22left%22)%0A%20%20%20%20%20%20%20%20%20%20%20%20found%20%3D%20listed%5B%22kind%22%5D.value_counts()%0A%20%20%20%20%20%20%20%20%20%20%20%20rows.append(%7B%22budget%22%3A%20budget%2C%20**%7Bkind%3A%20f%22%7Bint(found.get(kind%2C%200))%7D%20of%20%7Bint((defects%5B'kind'%5D%20%3D%3D%20kind).sum())%7D%22%20for%20kind%20in%20kinds%7D%2C%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%22not%20planted%22%3A%20int(listed%5B%22kind%22%5D.isna().sum())%7D)%0A%20%20%20%20%20%20%20%20listed%20%3D%20whole.merge(defects%2C%20on%3DUNIT_KEY%2C%20how%3D%22left%22)%0A%20%20%20%20%20%20%20%20missed%20%3D%20len(defects)%20-%20int(listed%5B%22kind%22%5D.notna().sum())%0A%20%20%20%20%20%20%20%20on_no_list%20%3D%20(%22Every%20planted%20defect%20is%20on%20the%20list%20with%20no%20budget%3A%20the%20rules%20and%20the%202021%20round's%20forest%20between%20them%20reach%20all%20four%20kinds.%20%22%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20if%20missed%20%3D%3D%200%20else%20f%22%7Bmissed%7D%20planted%20defects%20are%20on%20no%20list%3A%20they%20fail%20no%20rule%20and%20the%202021%20round's%20forest%20does%20not%20call%20them%20edited.%20%22)%0A%20%20%20%20%20%20%20%20return%20mo.vstack(%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20pd.DataFrame(rows)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20mo.md(f%22With%20no%20budget%20the%20list%20holds%20**%7Blen(whole)%3A%2C%7D%20units**%2C%20%7Bint(listed%5B'kind'%5D.notna().sum())%3A%2C%7D%20of%20them%20planted%20defects.%20The%20rules'%20units%20come%20first%2C%20so%20a%20budget%20that%20cuts%20the%20list%20cuts%20the%20forest's%20wrong%20units%20of%20measure%2C%20which%20fail%20no%20rule%3A%20it%20is%20the%20order%20a%20supervisor%20reads%20in.%20%22%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%2B%20on_no_list%20%2B%20%22Which%20budget%20to%20set%20is%20the%20survey%20team's%20call%3B%20the%20table%20shows%20what%20each%20one%20leaves%20for%20the%20next%20round.%22)%2C%0A%20%20%20%20%20%20%20%20%5D)%0A%0A%20%20%20%20_judge()%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%20these%20four%20add%20up%20to%0A%0A%20%20%20%20%7C%20exercise%20%7C%20the%20step%20past%20the%20modules%20%7C%20the%20habit%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20S3.1%20%7C%20a%20detector%20per%20stratum%20%7C%20group%20by%20what%20the%20design%20fixed%2C%20not%20by%20an%20answer%20an%20error%20can%20change%20%7C%0A%20%20%20%20%7C%20S3.2%20%7C%20near-duplicates%2C%20answer%20by%20answer%20%7C%20the%20maximum%20percent%20match%20ranks%20interviews%3B%20its%20threshold%20depends%20on%20the%20questionnaire%2C%20as%20Pew%20found%20%7C%0A%20%20%20%20%7C%20S3.3%20%7C%20judging%20pairs%20%7C%20a%20pair%20has%20no%20order%20and%20a%20household%20has%20a%20region%3B%20label%20what%20a%20list%20finds%20before%20counting%20it%20%7C%0A%20%20%20%20%7C%20S3.4%20%7C%20the%20large%20table%20in%20one%20function%20%7C%20what%20depends%20on%20the%20whole%20table%20is%20computed%20once%2C%20what%20depends%20on%20a%20row%20per%20chunk%2C%20and%20the%20ranking%20after%20every%20chunk%20%7C%0A%0A%20%20%20%20Next%3A%20the%20stretch%20S4%20(%6086_stretch_agents.py%60)%20is%20a%20showcase%20of%20AI%20agents%20building%20and%20testing%20a%20pipeline%20like%20these%2C%20and%20of%20what%20each%20kind%20of%20agent%20sends%20away.%20Then%20the%20capstone%20(%6099_capstone.py%60).%0A%20%20%20%20%22%22%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
409cce453d4acb27359f9348686c3f51