import%20marimo%0A%0A__generated_with%20%3D%20%220.25.2%22%0Aapp%20%3D%20marimo.App(%0A%20%20%20%20width%3D%22medium%22%2C%0A%20%20%20%20app_title%3D%22Module%206%3A%20Estimate%20under%20the%20survey%20design%22%2C%0A)%0A%0Awith%20app.setup%3A%0A%20%20%20%20%23%20The%20setup%20cell%20runs%20first.%20It%20holds%20the%20imports%20and%20the%20tables%20every%20other%20cell%20uses.%0A%20%20%20%20import%20itertools%0A%0A%20%20%20%20import%20marimo%20as%20mo%0A%20%20%20%20import%20numpy%20as%20np%0A%20%20%20%20import%20pandas%20as%20pd%0A%20%20%20%20import%20polars%20as%20pl%0A%20%20%20%20import%20svy%0A%20%20%20%20from%20scipy%20import%20special%2C%20stats%0A%0A%20%20%20%20import%20course%20%20%23%20this%20course's%20helpers%3A%20the%20survey's%20tables%2C%20file%20locations%20and%20exercise%20checks%0A%20%20%20%20from%20course%20import%20NOT_YET%2C%20attempt%2C%20check_cases%2C%20try_it%2C%20waiting_for%0A%0A%20%20%20%20%23%20The%20households%20as%20module%202%20reads%20them%20(removed%20households%20and%20voided%20rows%20left%20out)%2C%20one%20row%20each%2C%20with%20the%0A%20%20%20%20%23%20design's%20columns%20that%20course.design_frame()%20adds%3A%20stratum_id%2C%20psu_id%20and%20psus_in_stratum.%20Section%201%20checks%20them%0A%20%20%20%20%23%20before%20anything%20is%20estimated.%0A%20%20%20%20households%20%3D%20course.design_frame()%0A%20%20%20%20%23%20The%20provinces'%20names%2C%20by%20their%20codes%0A%20%20%20%20provinces%20%3D%20course.codes(%22geography%22)%5B%5B%22region_code%22%2C%20%22province_code%22%2C%20%22province%22%5D%5D.drop_duplicates()%0A%20%20%20%20HOUSEHOLD%20%3D%20%5B%22region_code%22%2C%20%22hh_id%22%5D%20%20%23%20a%20household%3A%20its%20region%20and%20its%20id%2C%20since%20an%20id%20repeats%20across%20regions%0A%20%20%20%20%23%20The%20sampling%20fractions%20of%20the%20four%20strata%2C%20as%20a%20design%20document%20states%20them%3A%20F%20is%20taken%20in%20full%2C%20A%20draws%204%20areas%0A%20%20%20%20%23%20of%2010%2C%20B%209%20of%2045%20and%20C%2010%20of%20150%2C%20which%20is%20written%20rounded%20to%20four%20decimals%0A%20%20%20%20FRACTIONS%20%3D%20%7B%22F%22%3A%201.0%2C%20%22A%22%3A%200.4%2C%20%22B%22%3A%200.2%2C%20%22C%22%3A%200.0667%7D%0A%20%20%20%20%23%20The%20two%20catch-all%20categories%20of%20the%20course's%20recodes%2C%20and%20the%20categories%20of%20an%20operator's%20sex%20and%20scale%2C%20in%20the%0A%20%20%20%20%23%20order%20a%20table%20lists%20them%0A%20%20%20%20INVALID%2C%20NO_ANSWER%20%3D%20%22Invalid%20value%22%2C%20%22No%20answer%22%0A%20%20%20%20SEXES%20%3D%20%5B%22Male%22%2C%20%22Female%22%2C%20INVALID%2C%20NO_ANSWER%5D%0A%20%20%20%20SCALES%20%3D%20%5B%22Small-scale%22%2C%20%22Large-scale%22%2C%20INVALID%2C%20NO_ANSWER%5D%0A%20%20%20%20%23%20The%20three%20levels%20of%20summary%2C%20each%20with%20the%20columns%20its%20domains%20are%20named%20by%20(Exercise%206.7)%0A%20%20%20%20LEVELS%20%3D%20%7B%22national%22%3A%20%5B%5D%2C%20%22regional%22%3A%20%5B%22region_code%22%5D%2C%20%22provincial%22%3A%20%5B%22region_code%22%2C%20%22province%22%5D%7D%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%20Module%206%3A%20Estimate%20under%20the%20survey%20design%0A%0A%20%20%20%20**Where%20this%20is%20in%20processing%20a%20survey.**%20By%20now%20the%20data%20is%20in%20one%20place%2C%20checked%2C%20edited%2C%20and%20its%20variables%20derived%2C%20and%20the%20sampling%20side%20has%20calculated%20the%20weights.%20This%20step%20calculates%20the%20estimates%20from%20them%3A%20every%20total%2C%20mean%20and%20ratio%20with%20its%20standard%20error%2C%20coefficient%20of%20variation%20and%20confidence%20interval%20under%20the%20survey's%20design%2C%20and%20the%20national%2C%20regional%20and%20provincial%20summaries%20with%20the%20sample's%20own%20counts%20beside%20the%20weighted%20figures.%20In%20R%20this%20is%20the%20part%20of%20a%20script%20that%20keeps%20the%20rows%20the%20design%20can%20use%2C%20sets%20a%20rule%20for%20lonely%20PSUs%2C%20declares%20the%20design%20(%60e.svydesign%60%20in%20ReGenesees%2C%20%60svydesign%60%20in%20R's%20survey%20package)%20and%20calls%20%60svystatTM%60%20by%20one%20domain%20or%20several.%0A%0A%20%20%20%20Module%205%20built%20the%20frame%20an%20estimate%20is%20computed%20from%2C%20and%20a%20weighted%20total%20tells%20you%20**how%20many**%20fishing%20households%20run%20a%20large-scale%20unit.%20It%20does%20not%20tell%20you%20**how%20sure**%20that%20number%20is.%20Two%20samples%20of%20the%20same%20size%20can%20give%20the%20same%20total%20with%20very%20different%20precision%2C%20depending%20on%20how%20they%20were%20drawn.%20A%20survey%20publishes%20each%20estimate%20with%20its%20standard%20error%2C%20its%20coefficient%20of%20variation%20and%20a%20confidence%20interval%2C%20and%20suppresses%20the%20ones%20that%20are%20too%20uncertain.%20All%20of%20that%20needs%20the%20**design**%3A%20which%20households%20were%20drawn%20together%2C%20from%20which%20list%2C%20and%20out%20of%20how%20many.%0A%0A%20%20%20%20%7C%20section%20%7C%20what%20you%20do%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%201%20%C2%B7%20The%20design%20in%20words%20and%20in%20svy%20%7C%20weights%2C%20strata%2C%20PSUs%2C%20the%20finite%20population%20correction%20as%20counts%3B%20the%20rows%20the%20design%20can%20use%20and%20its%20lonely%20PSUs%20%7C%0A%20%20%20%20%7C%202%20%C2%B7%20Totals%2C%20means%20and%20proportions%20%7C%20each%20with%20its%20SE%2C%20CV%20and%2095%25%20confidence%20interval%2C%20for%20the%20whole%20survey%2C%20by%20domain%2C%20and%20for%20every%20combination%20of%20two%20or%20three%20domains%20%7C%0A%20%20%20%20%7C%203%20%C2%B7%20Weighted%20and%20unweighted%20%7C%20weighted%20estimates%20beside%20the%20sample's%20own%20counts%2C%20national%2C%20regional%20and%20provincial%3B%20what%20the%20weights%20change%2C%20and%20where%20the%20large%20CVs%20come%20from%20%7C%0A%20%20%20%20%7C%204%20%C2%B7%20From%20ReGenesees%20to%20svy%2C%20call%20by%20call%20%7C%20every%20ReGenesees%20call%20of%20an%20estimation%20script%2C%20in%20svy%20and%20in%20a%20function%20you%20write%20%7C%0A%0A%20%20%20%20**Two%20ways%2C%20every%20time.**%20Every%20estimate%20in%20this%20module%20is%20computed%20twice%3A%20with%20**svy**%2C%20a%20maintained%20Python%20library%20for%20survey%20estimation%20(version%200.36.0%20here)%2C%20and%20with%20a%20short%20function%20written%20from%20the%20formula%20with%20numpy%20and%20pandas.%20They%20agree%20to%20the%20last%20printed%20digit%2C%20and%20the%20course's%20tests%20prove%20it.%20The%20library%20is%20what%20you%20run%20in%20production.%20The%20function%20is%20how%20you%20understand%20what%20the%20library%20does%2C%20and%20what%20you%20write%20when%20no%20library%20has%20the%20call%3A%20section%204%20has%20three%20such%20calls.%20If%20you%20understand%20the%20problem%2C%20answering%20it%20in%20Python%20is%20the%20easy%20part%2C%20whether%20you%20come%20from%20R%20or%20from%20another%20language.%0A%0A%20%20%20%20**The%20data.**%20One%20row%20per%20household%20of%20the%20Survey%20of%20Fishing%20Households%202025%20(%60households%60%2C%20from%20%60course.design_frame()%60)%2C%20with%20its%20design%3A%20%60stratum_id%60%2C%20%60psu_id%60%2C%20%60psus_in_stratum%60%20and%20the%20final%20weight%20%60sel_final_weight%60.%20Section%201%20checks%20it%2C%20and%20builds%20%60frame%60%2C%20the%20table%20every%20later%20cell%20estimates%20from.%0A%0A%20%20%20%20Exercises%20work%20as%20in%20the%20modules%20before%3A%20write%20the%20function%2C%20delete%20the%20%60raise%60%20line%2C%20use%20the%20**Try%20it**%20cell%2C%20read%20the%20check.%20If%20something%20goes%20red%2C%20read%20the%20error's%20**last%20line**%20first%3A%20it%20names%20what%20went%20wrong.%20svy's%20errors%20put%20a%20code%20in%20square%20brackets%20on%20that%20line%2C%20such%20as%20%60%5BSINGLETON_ERROR%5D%60%2C%20and%20a%20hint%20at%20the%20end.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20course.quizzes_at(%226.0%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%201%20%C2%B7%20The%20design%20in%20words%20and%20in%20svy%3A%20weights%2C%20strata%2C%20PSUs%2C%20fpc%2C%20lonely%20PSUs%0A%0A%20%20%20%20**The%20problem.**%20The%20survey%20did%20not%20interview%20households%20one%20by%20one%20at%20random.%20In%20each%20province%20it%20took%20every%20unit%20of%20a%20register%20of%20large%20fishing%20units%2C%20sorted%20the%20enumeration%20areas%20into%20strata%20by%20how%20many%20of%20their%20households%20fish%2C%20drew%20some%20areas%20from%20each%2C%20and%20interviewed%20every%20fishing%20household%20in%20the%20areas%20it%20drew.%20Households%20of%20one%20area%20share%20a%20coast%2C%20a%20market%20and%20a%20season%2C%20so%20they%20tend%20to%20answer%20alike%3A%20interviewing%20fifteen%20of%20them%20tells%20you%20less%20than%20fifteen%20households%20drawn%20from%20fifteen%20areas%20would.%20A%20variance%20formula%20that%20ignores%20this%20is%20too%20optimistic.%20Before%20any%20estimate%2C%20the%20design%20has%20to%20be%20written%20down%20in%20a%20form%20a%20program%20can%20use.%0A%0A%20%20%20%20**The%20design%20in%20words.**%20Four%20terms%20carry%20it%3A%0A%0A%20%20%20%20%7C%20term%20%7C%20what%20it%20means%20%7C%20in%20this%20survey%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20**stratum**%20%7C%20a%20part%20of%20the%20population%20sampled%20on%20its%20own%2C%20with%20its%20own%20rules%20%7C%20province%20%C3%97%20stratum%20letter%20(%60sel_stratum%60)%3A%20six%20provinces%20%C3%97%20four%20strata%20%3D%2024%20strata%2C%20in%20the%20column%20%60stratum_id%60%20%7C%0A%20%20%20%20%7C%20**PSU**%20(primary%20sampling%20unit)%20%7C%20what%20is%20drawn%20at%20the%20first%20stage%3A%20here%2C%20an%20enumeration%20area%20%7C%20strata%20A%2C%20B%20and%20C%3A%20the%20area%20(%60sel_ea%60)%3B%20stratum%20F%3A%20each%20household%20is%20its%20own%20PSU%20%7C%0A%20%20%20%20%7C%20**weight**%20%7C%20how%20many%20households%20of%20the%20population%20one%20interviewed%20household%20stands%20for%20%7C%20%60sel_final_weight%60%2C%20the%20final%20weight%2C%20calibrated%20to%20each%20province's%20listed%20fishing%20households%20by%20scale%20%7C%0A%20%20%20%20%7C%20**fpc**%20(finite%20population%20correction)%20%7C%20how%20much%20of%20a%20stratum's%20PSUs%20were%20drawn%3A%20drawing%20all%20of%20them%20leaves%20no%20sampling%20error%20%7C%20(N%20%E2%88%92%20n)%20%2F%20N%2C%20with%20N%20the%20stratum's%20PSUs%20in%20the%20population%20(%60stratum_counts.csv%60's%20%60eas_in_frame%60%2C%20%60psus_in_stratum%60%20in%20the%20frame)%20and%20n%20the%20PSUs%20drawn%20%7C%0A%0A%20%20%20%20The%20four%20strata%20of%20each%20province%20follow%20four%20rules.%20Stratum%20F%20lists%20the%20large%20fishing%20units%20and%20takes%20every%20one%20of%20them%3B%20stratum%20A%20draws%204%20of%20its%2010%20areas%2C%20stratum%20B%209%20of%20its%2045%20and%20stratum%20C%2010%20of%20its%20150.%20In%20a%20drawn%20area%20every%20fishing%20household%20is%20interviewed.%20The%20cell%20below%20counts%20it%20from%20the%20data.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20%23%20The%20four%20kinds%20of%20stratum%2C%20from%20the%20data%3A%20strata%2C%20PSUs%20in%20the%20population%20and%20in%20the%20sample%2C%20households%2C%20weights%0A%20%20%20%20_by_stratum%20%3D%20households.groupby(%5B%22sel_stratum%22%2C%20%22stratum_id%22%5D).agg(%0A%20%20%20%20%20%20%20%20population%3D(%22psus_in_stratum%22%2C%20%22first%22)%2C%20drawn%3D(%22psu_id%22%2C%20%22nunique%22)%2C%20interviewed%3D(%22psu_id%22%2C%20%22size%22)%2C%0A%20%20%20%20%20%20%20%20design_weight%3D(%22sel_design_weight%22%2C%20%22mean%22)%2C%20weight%3D(%22sel_final_weight%22%2C%20%22mean%22)%2C%0A%20%20%20%20)%0A%20%20%20%20_by_stratum.groupby(level%3D%22sel_stratum%22).agg(%0A%20%20%20%20%20%20%20%20strata%3D(%22drawn%22%2C%20%22size%22)%2C%0A%20%20%20%20%20%20%20%20psus_in_population%3D(%22population%22%2C%20%22median%22)%2C%0A%20%20%20%20%20%20%20%20psus_drawn%3D(%22drawn%22%2C%20%22median%22)%2C%0A%20%20%20%20%20%20%20%20households_interviewed%3D(%22interviewed%22%2C%20%22sum%22)%2C%0A%20%20%20%20%20%20%20%20mean_design_weight%3D(%22design_weight%22%2C%20%22mean%22)%2C%0A%20%20%20%20%20%20%20%20mean_final_weight%3D(%22weight%22%2C%20%22mean%22)%2C%0A%20%20%20%20).loc%5B%5B%22F%22%2C%20%22A%22%2C%20%22B%22%2C%20%22C%22%5D%5D.round(3)%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%20weights%20tell%20the%20design's%20story.%20A%20household%20of%20stratum%20F%20stands%20for%20about%20one%20household%3A%20itself.%20A%20household%20of%20stratum%20C%20stands%20for%20fifteen%20before%20any%20adjustment%2C%20because%20its%20area%20is%20one%20of%20fifteen%20(10%20of%20150)%2C%20and%20a%20little%20more%20once%20the%20nonresponse%20of%20its%20area%20is%20made%20up%20for.%20So%20the%20households%20of%20the%20drawn%20areas%20carry%20most%20of%20the%20population%2C%20and%20the%20large%20units%2C%20all%20interviewed%2C%20carry%20almost%20none%20of%20the%20uncertainty.%20Stratum%20B's%20mean%20design%20weight%20sits%20just%20below%205%2C%20where%20every%20one%20of%20its%20areas%20is%2045%20%2F%209%20%3D%205%3A%20section%201%20finds%20the%20household%20that%20pulls%20it%20down.%0A%0A%20%20%20%20%23%23%23%20The%20R%20way%20and%20the%20Python%20way%0A%0A%20%20%20%20In%20R%20you%20declare%20the%20design%20with%20the%20survey%20package's%20%60svydesign%60%2C%20or%20with%20ReGenesees'%20%60e.svydesign%60%2C%20which%20takes%20the%20same%20arguments.%20In%20ReGenesees%20you%20write%2C%20with%20the%20fpc%20given%20as%20each%20stratum's%20**sampling%20fraction**%3A%0A%0A%20%20%20%20%60%60%60r%0A%20%20%20%20hh%24fpc%20%3C-%20c(F%20%3D%201%2C%20A%20%3D%200.4%2C%20B%20%3D%200.2%2C%20C%20%3D%200.0667)%5Bhh%24sel_stratum%5D%20%20%20%23%20the%20share%20of%20each%20stratum's%20areas%20that%20was%20drawn%0A%20%20%20%20hh%24stratum_id%20%3C-%20factor(hh%24stratum_id)%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%23%20e.svydesign%20wants%20the%20strata%20as%20a%20factor%0A%20%20%20%20des%20%3C-%20e.svydesign(data%20%3D%20hh%2C%20ids%20%3D%20~psu_id%2C%20strata%20%3D%20~stratum_id%2C%20weights%20%3D%20~sel_final_weight%2C%20fpc%20%3D%20~fpc)%0A%20%20%20%20%60%60%60%0A%0A%20%20%20%20ReGenesees'%20help%20page%20for%20%60e.svydesign%60%20says%20the%20fpc%20can%20be%20given%20either%20as%20each%20stratum's%20population%20size%20or%20as%20the%20fraction%20sampled%20(ReGenesees%202.5%2C%20its%20%60man%2Fe.svydesign.Rd%60%2C%20read%2020%20November%202026).%20svy%20declares%20the%20same%20design%20in%20two%20objects%2C%20a%20%60Design%60%20that%20names%20the%20columns%20and%20a%20%60Sample%60%20that%20binds%20it%20to%20the%20data%3A%0A%0A%20%20%20%20%60%60%60python%0A%20%20%20%20design%20%3D%20svy.Design(stratum%3D%22stratum_id%22%2C%20psu%3D%22psu_id%22%2C%20wgt%3D%22sel_final_weight%22%2C%20pop_size%3D%22psus_in_stratum%22%2C%20singleton%3D%22skip%22)%0A%20%20%20%20sample%20%3D%20svy.Sample(data%3Dpl.from_pandas(frame)%2C%20design%3Ddesign)%0A%20%20%20%20%60%60%60%0A%0A%20%20%20%20Three%20things%20differ%20from%20the%20R%20way%2C%20and%20each%20is%20shown%20below%3A%20svy%20takes%20the%20fpc%20as%20**population%20counts**%2C%20not%20fractions%20(%60pop_size%60)%3B%20it%20takes%20a%20**polars**%20table%2C%20not%20pandas%3B%20and%20it%20decides%20what%20to%20do%20with%20a%20**lonely%20PSU**%20only%20when%20you%20tell%20it%20(%60singleton%3D%60).%20An%20area's%20number%2C%20%60sel_ea%60%2C%20is%20numbered%20within%20its%20province%2C%20so%20the%20same%20number%20can%20name%20an%20area%20of%20La%20Union%20and%20another%20of%20Cebu.%20%60course.design_frame()%60%20pastes%20the%20stratum%20in%20front%20of%20it%20to%20make%20%60psu_id%60%2C%20so%20one%20id%20names%20one%20area.%20Stratum%20F%20needs%20care%3A%20its%20%60sel_ea%60%20is%20blank%2C%20because%20its%20households%20were%20taken%20from%20a%20register%20and%20not%20by%20area%2C%20so%20%60course.design_frame()%60%20gives%20each%20of%20them%20its%20own%20PSU.%0A%0A%20%20%20%20%23%23%23%20The%20design%2C%20checked%20before%20it%20is%20used%0A%0A%20%20%20%20Give%20svy%20the%20households%20as%20they%20are%2C%20and%20it%20refuses.%20Read%20the%20message%20from%20the%20line%20with%20the%20code%20in%20square%20brackets%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20svy_error(attempt_it)%20-%3E%20str%3A%0A%20%20%20%20%22%22%22Run%20a%20call%20that%20may%20fail%20and%20return%20what%20it%20raised%2C%20as%20a%20reader%20sees%20it%3A%20the%20error's%20kind%20and%20svy's%20message.%22%22%22%0A%20%20%20%20try%3A%0A%20%20%20%20%20%20%20%20attempt_it()%0A%20%20%20%20except%20Exception%20as%20error%3A%20%20%23%20the%20point%20is%20to%20show%20the%20message%0A%20%20%20%20%20%20%20%20return%20f%22%7Btype(error).__name__%7D%3A%20%7Bstr(error).strip()%7D%22%0A%20%20%20%20return%20%22no%20error%22%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20%23%20The%20households%20as%20they%20are%2C%20with%20population%20counts%20from%20stratum_counts.csv%0A%20%20%20%20_first_try%20%3D%20svy_error(lambda%3A%20svy.Sample(%0A%20%20%20%20%20%20%20%20data%3Dpl.from_pandas(households%5B%5B%22stratum_id%22%2C%20%22psu_id%22%2C%20%22psus_in_stratum%22%2C%20%22sel_final_weight%22%5D%5D)%2C%0A%20%20%20%20%20%20%20%20design%3Dsvy.Design(stratum%3D%22stratum_id%22%2C%20psu%3D%22psu_id%22%2C%20wgt%3D%22sel_final_weight%22%2C%20pop_size%3D%22psus_in_stratum%22%2C%20singleton%3D%22skip%22)%2C%0A%20%20%20%20).estimation.total(y%3D%22sel_final_weight%22))%0A%20%20%20%20mo.md(f%22%60%60%60%5Cn%7B_first_try%7D%5Cn%60%60%60%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%60FPC_POP_LT_SAMPLE%60%3A%20in%20one%20stratum%20the%20data%20holds%20more%20PSUs%20than%20the%20stratum%20has%20in%20the%20population.%20A%20design%20can't%20draw%2012%20units%20out%20of%2011.%20Either%20the%20frame's%20count%20is%20wrong%20or%20the%20data%20holds%20a%20unit%20that%20shouldn't%20be%20there%2C%20and%20an%20estimate%20can't%20be%20trusted%20until%20you%20know%20which.%20svy%20names%20the%20stratum%20by%20its%20position%3B%20the%20cell%20below%20finds%20it%20by%20its%20code.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20as_rows(result%2C%20keys%2C%20digits%3A%20int%20%3D%204)%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22A%20result%20table%20in%20one%20shape%20for%20the%20checks%3A%20a%20polars%20table%20as%20pandas%2C%20its%20keys%20as%20columns%2C%20rows%20sorted%20by%20them%2C%0A%20%20%20%20columns%20in%20name%20order%2C%20numbers%20rounded.%20So%20a%20correct%20answer%20passes%20whatever%20its%20row%20order%20or%20index.%22%22%22%0A%20%20%20%20table%20%3D%20result.to_pandas()%20if%20type(result).__module__.startswith(%22polars%22)%20else%20result%0A%20%20%20%20table%20%3D%20table.to_frame()%20if%20isinstance(table%2C%20pd.Series)%20else%20table%0A%20%20%20%20keys%20%3D%20%5Bkeys%5D%20if%20isinstance(keys%2C%20str)%20else%20list(keys)%0A%20%20%20%20if%20any(name%20in%20keys%20for%20name%20in%20table.index.names)%3A%0A%20%20%20%20%20%20%20%20table%20%3D%20table.reset_index()%0A%20%20%20%20table%20%3D%20table.sort_values(keys%2C%20na_position%3D%22last%22).reset_index(drop%3DTrue)%5Bsorted(table.columns)%5D%0A%20%20%20%20numbers%20%3D%20table.select_dtypes(%22number%22).columns%0A%20%20%20%20table%5Bnumbers%5D%20%3D%20table%5Bnumbers%5D.astype(%22float64%22).round(digits)%0A%20%20%20%20return%20table%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20_defects%20%3D%20course.planted_defects()%0A%20%20%20%20_copies%20%3D%20_defects.loc%5B_defects%5B%22kind%22%5D%20%3D%3D%20%22household_copied%22%2C%20%5B*HOUSEHOLD%2C%20%22detail%22%5D%5D%0A%20%20%20%20_per%20%3D%20households.groupby(%22stratum_id%22).agg(n%3D(%22psu_id%22%2C%20%22nunique%22)%2C%20N%3D(%22psus_in_stratum%22%2C%20%22first%22)%2C%20region_code%3D(%22region_code%22%2C%20%22first%22)%2C%20province_code%3D(%22province_code%22%2C%20%22first%22))%0A%20%20%20%20_per%20%3D%20_per.reset_index().merge(provinces%2C%20on%3D%5B%22region_code%22%2C%20%22province_code%22%5D%2C%20how%3D%22left%22%2C%20validate%3D%22many_to_one%22).set_index(%22stratum_id%22)%0A%20%20%20%20_over%20%3D%20_per%5B_per%5B%22n%22%5D%20%3E%20_per%5B%22N%22%5D%5D%0A%20%20%20%20_which%20%3D%20%22%3B%20%22.join(f%22%7Brow.province%7D's%20stratum%20F%20(%7Bname%7D)%20holds%20%7Brow.n%7D%20households%20and%20its%20register%20lists%20%7Brow.N%7D%20large%20units%22%20for%20name%2C%20row%20in%20_over.iterrows())%0A%20%20%20%20_copy_rows%20%3D%20households.merge(_copies%2C%20on%3DHOUSEHOLD%2C%20how%3D%22inner%22%2C%20validate%3D%22one_to_one%22)%0A%20%20%20%20_in_over%20%3D%20_copy_rows%5B_copy_rows%5B%22stratum_id%22%5D.isin(_over.index)%5D%0A%20%20%20%20_named%20%3D%20%22%3B%20%22.join(f%22in%20%7Brow.stratum_id%7D%20it%20is%20household%20%7Brow.hh_id%7D%20of%20region%20%7Brow.region_code%7D%2C%20%7Brow.detail.split('%2C')%5B0%5D%7D%22%20for%20row%20in%20_in_over.itertuples())%0A%20%20%20%20_caught%20%3D%20len(_in_over)%0A%20%20%20%20_held%20%3D%20len(_copy_rows)%20-%20_caught%0A%20%20%20%20_misfiled%20%3D%20households.merge(_defects.loc%5B_defects%5B%22kind%22%5D%20%3D%3D%20%22area_in_two_strata%22%2C%20HOUSEHOLD%5D%2C%20on%3DHOUSEHOLD%2C%20how%3D%22inner%22%2C%20validate%3D%22one_to_one%22)%0A%20%20%20%20mo.md(rf%22%22%22%0A%20%20%20%20%23%23%23%20What%20the%20extra%20PSU%20is%0A%0A%20%20%20%20%7B_which%7D.%20In%20stratum%20F%20each%20household%20is%20its%20own%20PSU%2C%20so%20one%20household%20too%20many%20is%20one%20PSU%20too%20many.%20Module%204%20met%20the%20cause%3A%20a%20**copied%20household**%2C%20one%20household%20entered%20twice%20under%20two%20ids%2C%20the%20copy%20two%20days%20later%20and%20a%20few%20minutes%20shorter%3B%20%7B_named%7D.%20The%20survey%20has%20%7Blen(_copy_rows)%7D%20of%20them%2C%20%7Bint((_copy_rows%5B%22sel_stratum%22%5D%20%3D%3D%20%22F%22).sum())%7D%20in%20stratum%20F%2C%20and%20the%20course's%20list%20of%20planted%20defects%20(%60course.planted_defects()%60)%20names%20them.%20In%20%7B_held%7D%20of%20their%20strata%20the%20count%20still%20holds%2C%20because%20the%20data%20already%20had%20one%20household%20fewer%20than%20the%20register%2C%20so%20the%20copy%20made%20the%20number%20up%3B%20svy%20caught%20%7B_caught%7D.%20The%20design%20check%20caught%20what%20the%20duplicate%20check%20exists%20to%20catch%2C%20but%20not%20every%20time%3A%20a%20copy%20is%20found%20for%20sure%20only%20by%20looking%20for%20copies.%0A%0A%20%20%20%20Module%204%20found%20a%20second%20defect%20of%20the%20design%2C%20which%20svy%20can't%20catch%3A%20an%20**area%20in%20two%20strata**.%20In%20%7Blen(_misfiled)%7D%20areas%2C%20one%20in%20each%20region%2C%20one%20household%20of%20a%20stratum-A%20area%20is%20filed%20under%20stratum%20%7B%22%2C%20%22.join(sorted(_misfiled%5B%22sel_stratum%22%5D.unique()))%7D.%20It%20adds%20a%20PSU%20to%20that%20stratum%2C%20which%20still%20holds%20fewer%20PSUs%20than%20its%2045%2C%20and%20its%20design%20weight%20of%20%7B%22%2C%20%22.join(f%22%7Bw%3Ag%7D%22%20for%20w%20in%20sorted(_misfiled%5B%22sel_design_weight%22%5D.unique()))%7D%2C%20stratum%20A's%2C%20is%20what%20pulls%20B's%20mean%20design%20weight%20below%205.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20%23%20The%20copies%20and%20the%20misfiled%20households%2C%20from%20the%20list%20of%20planted%20defects%2C%20beside%20the%20households%20they%20concern%0A%20%20%20%20_defects%20%3D%20course.planted_defects()%0A%20%20%20%20_found%20%3D%20_defects.loc%5B_defects%5B%22kind%22%5D.isin(%5B%22household_copied%22%2C%20%22area_in_two_strata%22%5D)%2C%20%5B*HOUSEHOLD%2C%20%22kind%22%2C%20%22detail%22%5D%5D%0A%20%20%20%20_show%20%3D%20%5B*HOUSEHOLD%2C%20%22stratum_id%22%2C%20%22sel_ea%22%2C%20%22psu_id%22%2C%20%22hh_interviewer%22%2C%20%22hh_interview_date%22%2C%20%22hh_roster_members%22%2C%20%22sel_design_weight%22%2C%20%22sel_final_weight%22%5D%0A%20%20%20%20_found.merge(households%5B_show%5D%2C%20on%3DHOUSEHOLD%2C%20how%3D%22left%22%2C%20validate%3D%22one_to_one%22).sort_values(%5B%22kind%22%2C%20%22region_code%22%5D)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%20The%20frame%20this%20module%20estimates%20from%0A%0A%20%20%20%20%60estimation_frame%60%20takes%20module%204's%20two%20design%20findings%20as%20decided%3A%20the%20copies%20are%20left%20out%2C%20and%20the%20misfiled%20household%20goes%20back%20to%20its%20area's%20stratum%2C%20with%20its%20area's%20%60stratum_id%60%2C%20%60psu_id%60%20and%20%60psus_in_stratum%60.%20It%20then%20adds%20what%20each%20household%20runs%2C%20counted%20from%20the%20fishing%20units%2C%20members%20and%20boats%20tables%2C%20as%20module%205%20taught%3A%20its%20fishing%20%60units%60%2C%20its%20large-scale%20ones%20(%60large_units%60%2C%20units%20whose%20%60unit_scale%60%20is%202%3A%20a%20boat%20of%205.0%20gross%20tons%20or%20more)%20and%20its%20small-scale%20ones%20(%60small_units%60)%2C%20the%20%60large_unit_operators%60%20among%20its%20members%20(the%20distinct%20roster%20lines%20that%20run%20a%20large-scale%20unit)%2C%20its%20%60hh_size%60%20(members)%20and%20%60n_boats%60%2C%20%60runs_large_unit%60%20(1%20if%20it%20runs%20any%20large-scale%20unit)%20and%20%60hh_count%60%20(1%20for%20every%20row%2C%20so%20a%20total%20of%20it%20counts%20households).%20The%20province's%20name%20replaces%20its%20code.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20estimation_frame(households%3A%20pd.DataFrame%2C%20defects%3A%20pd.DataFrame)%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22One%20row%20per%20household%20for%20estimation%3A%20the%20design's%20columns%2C%20the%20province's%20name%2C%20and%20what%20the%20household%20runs%2C%0A%20%20%20%20counted%20from%20the%20fishing%20units%2C%20members%20and%20boats%20tables.%20Module%204's%20design%20findings%20are%20applied%3A%20the%20copied%0A%20%20%20%20households%20are%20left%20out%2C%20and%20a%20household%20filed%20under%20another%20stratum%20than%20its%20area's%20takes%20its%20area's%20design%20columns.%22%22%22%0A%20%20%20%20key%20%3D%20%5B%22region_code%22%2C%20%22hh_id%22%5D%0A%20%20%20%20copies%20%3D%20defects.loc%5Bdefects%5B%22kind%22%5D%20%3D%3D%20%22household_copied%22%2C%20key%5D%0A%20%20%20%20kept%20%3D%20households.merge(copies%2C%20on%3Dkey%2C%20how%3D%22left%22%2C%20indicator%3DTrue)%0A%20%20%20%20kept%20%3D%20kept%5Bkept%5B%22_merge%22%5D%20%3D%3D%20%22left_only%22%5D.drop(columns%3D%22_merge%22).reset_index(drop%3DTrue)%0A%20%20%20%20%23%20The%20misfiled%20household%20takes%20the%20stratum%2C%20PSU%20and%20count%20of%20its%20area's%20other%20households%0A%20%20%20%20area%20%3D%20%5B%22region_code%22%2C%20%22province_code%22%2C%20%22sel_ea%22%5D%0A%20%20%20%20design%20%3D%20%5B%22sel_stratum%22%2C%20%22stratum_id%22%2C%20%22psu_id%22%2C%20%22psus_in_stratum%22%5D%0A%20%20%20%20misfiled%20%3D%20kept%5Bkey%5D.merge(defects.loc%5Bdefects%5B%22kind%22%5D%20%3D%3D%20%22area_in_two_strata%22%2C%20key%5D.assign(misfiled%3DTrue)%2C%20on%3Dkey%2C%20how%3D%22left%22)%5B%22misfiled%22%5D.eq(True).to_numpy()%0A%20%20%20%20others%20%3D%20kept.loc%5B~misfiled%2C%20%5B*area%2C%20*design%5D%5D.drop_duplicates(area)%0A%20%20%20%20kept.loc%5Bmisfiled%2C%20design%5D%20%3D%20kept.loc%5Bmisfiled%2C%20area%5D.merge(others%2C%20on%3Darea%2C%20how%3D%22left%22%2C%20validate%3D%22many_to_one%22)%5Bdesign%5D.to_numpy()%0A%20%20%20%20units%20%3D%20course.units()%0A%20%20%20%20units%20%3D%20units.assign(large%3D(units%5B%22unit_scale%22%5D%20%3D%3D%202).astype(int))%0A%20%20%20%20counts%20%3D%20%5B%0A%20%20%20%20%20%20%20%20units.groupby(key).agg(units%3D(%22unit_no%22%2C%20%22size%22)%2C%20large_units%3D(%22large%22%2C%20%22sum%22)).reset_index()%2C%0A%20%20%20%20%20%20%20%20units%5Bunits%5B%22large%22%5D%20%3D%3D%201%5D.groupby(key).agg(large_unit_operators%3D(%22operator_line%22%2C%20%22nunique%22)).reset_index()%2C%0A%20%20%20%20%20%20%20%20course.members().groupby(key).size().rename(%22hh_size%22).reset_index()%2C%0A%20%20%20%20%20%20%20%20course.child(%22boat%22).groupby(key).size().rename(%22n_boats%22).reset_index()%2C%0A%20%20%20%20%5D%0A%20%20%20%20frame%20%3D%20kept.merge(provinces%2C%20on%3D%5B%22region_code%22%2C%20%22province_code%22%5D%2C%20how%3D%22left%22%2C%20validate%3D%22many_to_one%22)%0A%20%20%20%20for%20table%20in%20counts%3A%0A%20%20%20%20%20%20%20%20frame%20%3D%20frame.merge(table%2C%20on%3Dkey%2C%20how%3D%22left%22%2C%20validate%3D%22one_to_one%22)%0A%20%20%20%20numbers%20%3D%20%5B%22units%22%2C%20%22large_units%22%2C%20%22large_unit_operators%22%2C%20%22hh_size%22%2C%20%22n_boats%22%5D%0A%20%20%20%20frame%5Bnumbers%5D%20%3D%20frame%5Bnumbers%5D.fillna(0).astype(%22int64%22)%20%20%23%20a%20household%20with%20no%20boat%20has%20no%20row%20in%20the%20boats%20table%0A%20%20%20%20frame%5B%22small_units%22%5D%20%3D%20frame%5B%22units%22%5D%20-%20frame%5B%22large_units%22%5D%0A%20%20%20%20frame%5B%22runs_large_unit%22%5D%20%3D%20(frame%5B%22large_units%22%5D%20%3E%200).astype(%22int64%22)%0A%20%20%20%20frame%5B%22hh_count%22%5D%20%3D%201%0A%20%20%20%20columns%20%3D%20%5B%22region_code%22%2C%20%22hh_id%22%2C%20%22province%22%2C%20%22sel_stratum%22%2C%20%22sel_ea%22%2C%20%22stratum_id%22%2C%20%22psu_id%22%2C%20%22psus_in_stratum%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22sel_design_weight%22%2C%20%22sel_final_weight%22%2C%20%22hh_count%22%2C%20%22hh_size%22%2C%20%22units%22%2C%20%22large_units%22%2C%20%22small_units%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22large_unit_operators%22%2C%20%22runs_large_unit%22%2C%20%22n_boats%22%5D%0A%20%20%20%20return%20frame%5Bcolumns%5D.astype(%7B%22psus_in_stratum%22%3A%20%22int64%22%7D).reset_index(drop%3DTrue)%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20%23%20The%20frame%20every%20later%20cell%20estimates%20from%0A%20%20%20%20frame%20%3D%20estimation_frame(households%2C%20course.planted_defects())%0A%20%20%20%20mo.vstack(%5B%0A%20%20%20%20%20%20%20%20mo.md(f%22**%7Blen(frame)%3A%2C%7D%20households**%20(%7Blen(households)%20-%20len(frame)%7D%20copies%20left%20out)%2C%20%7Bframe%5B'stratum_id'%5D.nunique()%7D%20strata%2C%20%7Bframe%5B'psu_id'%5D.nunique()%7D%20PSUs.%22)%2C%0A%20%20%20%20%20%20%20%20frame.head()%2C%0A%20%20%20%20%5D)%0A%20%20%20%20return%20(frame%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%20Lonely%20PSUs%0A%0A%20%20%20%20**The%20problem.**%20The%20variance%20formula%20measures%20how%20much%20a%20stratum's%20PSUs%20differ%20from%20each%20other.%20A%20stratum%20with%20**one**%20PSU%20in%20the%20data%20has%20nothing%20to%20compare%2C%20so%20its%20share%20of%20the%20variance%20can't%20be%20measured.%20This%20happens%20when%20a%20stratum%20draws%20one%20PSU%20by%20design%2C%20when%20all%20but%20one%20of%20its%20PSUs%20fail%20to%20respond%2C%20or%20inside%20a%20domain%20where%20only%20one%20PSU%20of%20a%20stratum%20holds%20a%20member.%0A%0A%20%20%20%20Suppose%20three%20of%20La%20Union's%20four%20stratum-A%20areas%20arrived%20late%20and%20the%20estimates%20run%20without%20them.%20svy%20refuses%20to%20guess%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(frame)%3A%0A%20%20%20%20%23%20La%20Union's%20stratum%20A%20with%20one%20area%20left%3A%20a%20lonely%20PSU%0A%20%20%20%20_late%20%3D%20frame.loc%5Bframe%5B%22stratum_id%22%5D%20%3D%3D%20%2201-033-A%22%2C%20%22psu_id%22%5D.unique()%5B1%3A%5D%0A%20%20%20%20_lonely%20%3D%20frame%5B~frame%5B%22psu_id%22%5D.isin(_late)%5D%0A%0A%20%20%20%20def%20_design(rule)%3A%0A%20%20%20%20%20%20%20%20return%20svy.Design(stratum%3D%22stratum_id%22%2C%20psu%3D%22psu_id%22%2C%20wgt%3D%22sel_final_weight%22%2C%20pop_size%3D%22psus_in_stratum%22%2C%20singleton%3Drule)%0A%0A%20%20%20%20_refused%20%3D%20svy_error(lambda%3A%20svy.Sample(data%3Dpl.from_pandas(_lonely)%2C%20design%3D_design(None)).estimation.total(y%3D%22units%22))%0A%20%20%20%20_rules%20%3D%20pd.DataFrame(%7B%22singleton%3D%22%3A%20%5B%22skip%22%2C%20%22center%22%2C%20%22scale%22%5D%2C%20%22in%20ReGenesees%2C%20options(RG.lonely.psu%20%3D%20...)%22%3A%20%5B'%22remove%22'%2C%20'%22adjust%22'%2C%20'%22average%22'%5D%7D)%0A%20%20%20%20_rules%5B%22SE%20of%20the%20total%20of%20units%22%5D%20%3D%20%5B%0A%20%20%20%20%20%20%20%20svy.Sample(data%3Dpl.from_pandas(_lonely)%2C%20design%3D_design(rule)).estimation.total(y%3D%22units%22).to_polars()%5B%22se%22%5D%5B0%5D%0A%20%20%20%20%20%20%20%20for%20rule%20in%20_rules%5B%22singleton%3D%22%5D%0A%20%20%20%20%5D%0A%20%20%20%20mo.vstack(%5Bmo.md(f%22%60%60%60%5Cn%7B_refused%7D%5Cn%60%60%60%22)%2C%20_rules.round(2)%5D)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20The%20message's%20line%20%60%5BSINGLETON_ERROR%5D%60%20names%20the%20problem%2C%20and%20its%20last%20lines%20list%20the%20rules%20svy%20offers.%20Each%20rule%20is%20a%20decision%20about%20the%20missing%20variance%2C%20and%20each%20gives%20another%20SE%3A%0A%0A%20%20%20%20-%20**%60skip%60**%3A%20the%20lonely%20PSU's%20stratum%20adds%20nothing%20to%20the%20variance.%20It%20is%20right%20for%20a%20stratum%20taken%20with%20certainty%2C%20and%20too%20low%20otherwise.%0A%20%20%20%20-%20**%60center%60**%20and%20**%60scale%60**%3A%20the%20lonely%20PSU's%20deviation%20is%20measured%20from%20the%20overall%20mean%2C%20or%20the%20other%20strata's%20variance%20is%20scaled%20up%20to%20cover%20it%3B%20both%20are%20cautious.%0A%20%20%20%20-%20**%60collapse%60**%3A%20the%20lonely%20stratum%20joins%20another%20(ReGenesees'%20%60collapse.strata()%60).%20With%20a%20population%20count%20per%20stratum%2C%20it%20fails%20here%3A%20La%20Union's%20stratum%20A%20counts%2010%20areas%20and%20stratum%20B%20counts%2045%2C%20and%20svy%20refuses%20one%20stratum%20with%20two%20counts%20(%60%5BFPC_NOT_CONSTANT%5D%60).%20Section%204%20collapses%20a%20design%20without%20counts.%0A%20%20%20%20-%20**%60self_representing%60**%3A%20the%20lonely%20PSU's%20own%20units%20become%20the%20PSUs%3B%20section%204%20shows%20it.%0A%0A%20%20%20%20**The%20same%20choice%20in%20ReGenesees.**%20It%20is%20an%20option%20set%20before%20the%20design%20is%20used%2C%20%60options(RG.lonely.psu%20%3D%20...)%60%2C%20and%20its%20help%20page%20lists%20four%20values%20(ReGenesees%202.5%2C%20%60man%2FReGenesees.options.Rd%60%2C%20read%2020%20November%202026)%3A%20%60%22fail%22%60%2C%20the%20default%2C%20raises%20an%20error%2C%20as%20svy%20does%20with%20no%20rule%3B%20%60%22remove%22%60%20ignores%20the%20lonely%20PSUs%20for%20the%20variance%2C%20svy's%20%60skip%60%3B%20%60%22adjust%22%60%20measures%20deviations%20from%20the%20population%20mean%2C%20svy's%20%60center%60%3B%20%60%22average%22%60%20gives%20the%20stratum%20the%20average%20contribution%20of%20the%20others%2C%20svy's%20%60scale%60.%20The%20help%20page%20of%20%60svystatTM%60%20points%20to%20%60collapse.strata()%60%20as%20a%20better%20alternative%20to%20the%20last%20two.%20svy's%20own%20help%20for%20%60svy.Singleton%60%20names%20the%20same%20pairs.%0A%0A%20%20%20%20**This%20module%20declares%20%60skip%60.**%20The%20design%20as%20drawn%20has%20no%20lonely%20PSU%3A%20stratum%20F%20takes%20every%20registered%20unit%20with%20each%20household%20its%20own%20PSU%2C%20and%20strata%20A%2C%20B%20and%20C%20draw%204%2C%209%20and%2010%20areas.%20So%20a%20lonely%20PSU%20appears%20only%20when%20rows%20go%20missing%2C%20as%20in%20the%20cell%20above%2C%20and%20Exercise%206.1%20marks%20its%20rows%20so%20a%20table's%20notes%20can%20say%20whose%20variance%20its%20SE%20leaves%20out.%20Where%20a%20stratum%20has%20lost%20areas%20and%20you%20want%20the%20SE%20to%20stay%20cautious%2C%20%60center%60%20or%20%60scale%60%20is%20the%20choice%20to%20make%2C%20and%20%60collapse%60%20where%20a%20neighbouring%20stratum%20is%20alike.%20Whichever%20rule%20you%20pick%2C%20write%20it%20into%20the%20script%20where%20a%20reviewer%20sees%20it.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%20Exercise%206.1.%20The%20rows%20the%20design%20can%20use%2C%20and%20its%20lonely%20PSUs%0A%0A%20%20%20%20**Why%20it%20matters%3A**%20this%20is%20the%20step%20before%20a%20design%20is%20declared%2C%20in%20any%20language.%20Keep%20the%20rows%20that%20have%20a%20final%20weight%2C%20a%20stratum%20and%20a%20cluster%20id%20(%60sel_final_weight%60%2C%20%60sel_stratum%60%20and%20%60psu_id%60)%2C%20then%20set%20the%20lonely-PSU%20rule.%20A%20row%20without%20a%20weight%20stands%20for%20no%20household%2C%20and%20a%20row%20without%20a%20stratum%20or%20a%20PSU%20has%20no%20place%20in%20the%20variance%20formula.%20The%20lonely%20PSUs%20are%20found%20**after**%20those%20rows%20are%20dropped%3A%20dropping%20a%20household%20can%20leave%20its%20stratum%20with%20one%20area.%0A%0A%20%20%20%20**Input%20%E2%86%92%20output%3A**%20a%20frame%20of%20households%20with%20%60sel_stratum%60%2C%20%60stratum_id%60%2C%20%60psu_id%60%20and%20%60sel_final_weight%60%20%E2%86%92%20the%20rows%20the%20design%20can%20use%2C%20in%20the%20frame's%20order%2C%20every%20column%20kept%2C%20with%20one%20new%20column%2C%20%60lonely_psu%60%3A%0A%0A%20%20%20%20-%20a%20row%20is%20kept%20when%20%60sel_final_weight%60%2C%20%60sel_stratum%60%20and%20%60psu_id%60%20all%20have%20a%20value%3B%0A%20%20%20%20-%20%60lonely_psu%60%20is%20True%20on%20every%20kept%20row%20of%20a%20stratum%20that%20has%20**one**%20PSU%20among%20the%20kept%20rows%2C%20and%20False%20on%20the%20others.%0A%0A%20%20%20%20%7C%20region_code%20%7C%20hh_id%20%7C%20stratum_id%20%7C%20psu_id%20%7C%20sel_final_weight%20%7C%20%E2%86%92%20kept%20%7C%20%E2%86%92%20lonely_psu%20%7C%0A%20%20%20%20%7C%3A--%7C--%3A%7C%3A--%7C%3A--%7C--%3A%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%2001%20%7C%201%20%7C%20X-A%20%7C%201%20%7C%202.5%20%7C%20yes%20%7C%20False%3A%20X-A%20keeps%20PSUs%201%20and%202%20%7C%0A%20%20%20%20%7C%2001%20%7C%204%20%7C%20Y-A%20%7C%201%20%7C%202.5%20%7C%20yes%20%7C%20True%3A%20Y-A's%20other%20PSU%20had%20one%20household%2C%20with%20no%20weight%20%7C%0A%20%20%20%20%7C%2001%20%7C%205%20%7C%20Y-A%20%7C%202%20%7C%20*missing*%20%7C%20no%20%7C%20%7C%0A%20%20%20%20%7C%2004%20%7C%201%20%7C%20*missing*%20%7C%20*missing*%20%7C%201.0%20%7C%20no%3A%20no%20stratum%20%7C%20%7C%0A%20%20%20%20%7C%2004%20%7C%202%20%7C%20Z-C%20%7C%20*missing*%20%7C%2015.0%20%7C%20no%3A%20no%20PSU%20%7C%20%7C%0A%20%20%20%20%7C%2004%20%7C%203%20%7C%20Z-C%20%7C%207%20%7C%2015.0%20%7C%20yes%20%7C%20True%20%7C%0A%0A%20%20%20%20The%20rows%20of%20a%20lonely%20stratum%20stay%3A%20they%20are%20part%20of%20every%20estimate.%20%60singleton%3D%22skip%22%60%2C%20like%20ReGenesees'%20%60%22remove%22%60%2C%20leaves%20out%20only%20their%20stratum's%20share%20of%20the%20variance%2C%20and%20%60lonely_psu%60%20names%20the%20rows%20that%20share%20comes%20from%2C%20for%20the%20notes%20of%20a%20table.%0A%0A%20%20%20%20**Tools%3A**%20%60.notna()%60%20and%20%60%26%60%20for%20the%20rows%20to%20keep%3B%20%60groupby(%22stratum_id%22)%5B%22psu_id%22%5D.transform(%22nunique%22)%60%20(module%205's%20%60transform%60)%20gives%20each%20row%20its%20stratum's%20count%20of%20PSUs%3B%20%60assign%60.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20design_rows(frame%3A%20pd.DataFrame)%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22The%20rows%20the%20design%20can%20use%20(sel_final_weight%2C%20sel_stratum%20and%20psu_id%20all%20present)%2C%20in%20the%20frame's%20order%2C%20with%0A%20%20%20%20lonely_psu%3A%20True%20on%20every%20row%20of%20a%20stratum%20left%20with%20one%20PSU.%22%22%22%0A%20%20%20%20%23%20Exercise%206.1%3A%20the%20task%20and%20examples%20are%20in%20the%20text%20above%20this%20cell.%0A%20%20%20%20%23%20Step%201%3A%20keep%20the%20rows%20whose%20sel_final_weight%2C%20sel_stratum%20and%20psu_id%20all%20have%20a%20value%20(.notna()%2C%20joined%20with%20%26)%0A%20%20%20%20%23%20Step%202%3A%20each%20kept%20row's%20count%20of%20PSUs%20in%20its%20stratum%3A%20groupby(%22stratum_id%22)%5B%22psu_id%22%5D.transform(%22nunique%22)%0A%20%20%20%20%23%20Step%203%3A%20return%20the%20kept%20rows%20with%20lonely_psu%2C%20True%20where%20that%20count%20is%201%0A%20%20%20%20raise%20NotImplementedError(%22Exercise%206.1%22)%0A%0A%0A%40app.function%0Adef%20rows_fixture()%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22Ten%20households%2C%20used%20by%20the%20check%20of%20Exercise%206.1%3A%20a%20weight%2C%20a%20stratum%20and%20a%20PSU%20missing%2C%20each%20on%20one%20row%3B%20a%0A%20%20%20%20stratum%20(Y-A)%20and%20another%20(Z-C)%20left%20with%20one%20PSU%20once%20those%20rows%20are%20gone%3B%20PSU%20labels%20that%20repeat%20across%20strata%3B%20and%0A%20%20%20%20an%20hh_id%20in%20two%20regions.%22%22%22%0A%20%20%20%20rows%20%3D%20%5B%0A%20%20%20%20%20%20%20%20(%2201%22%2C%201%2C%20%22A%22%2C%20%22X-A%22%2C%20%221%22%2C%202.5)%2C%20(%2201%22%2C%202%2C%20%22A%22%2C%20%22X-A%22%2C%20%221%22%2C%202.5)%2C%20(%2201%22%2C%203%2C%20%22A%22%2C%20%22X-A%22%2C%20%222%22%2C%202.5)%2C%0A%20%20%20%20%20%20%20%20(%2201%22%2C%204%2C%20%22A%22%2C%20%22Y-A%22%2C%20%221%22%2C%202.5)%2C%20(%2201%22%2C%205%2C%20%22A%22%2C%20%22Y-A%22%2C%20%222%22%2C%20None)%2C%0A%20%20%20%20%20%20%20%20(%2204%22%2C%201%2C%20None%2C%20None%2C%20None%2C%201.0)%2C%20(%2204%22%2C%202%2C%20%22C%22%2C%20%22Z-C%22%2C%20None%2C%2015.0)%2C%20(%2204%22%2C%203%2C%20%22C%22%2C%20%22Z-C%22%2C%20%227%22%2C%2015.0)%2C%0A%20%20%20%20%20%20%20%20(%2204%22%2C%204%2C%20%22F%22%2C%20%22W-F%22%2C%20%2204-hh-4%22%2C%201.0)%2C%20(%2204%22%2C%205%2C%20%22F%22%2C%20%22W-F%22%2C%20%2204-hh-5%22%2C%201.0)%2C%0A%20%20%20%20%5D%0A%20%20%20%20table%20%3D%20pd.DataFrame(rows%2C%20columns%3D%5B%22region_code%22%2C%20%22hh_id%22%2C%20%22sel_stratum%22%2C%20%22stratum_id%22%2C%20%22psu_id%22%2C%20%22sel_final_weight%22%5D)%0A%20%20%20%20return%20table.astype(%7B%22sel_stratum%22%3A%20%22string%22%2C%20%22stratum_id%22%3A%20%22string%22%2C%20%22psu_id%22%3A%20%22string%22%2C%20%22sel_final_weight%22%3A%20%22Float64%22%7D)%0A%0A%0A%40app.function%0Adef%20as_sent(frame%3A%20pd.DataFrame)%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22The%20frame%20as%20a%20late%20export%20could%20leave%20it%3A%20La%20Union's%20stratum%20A%20down%20to%20one%20area%20(three%20areas%20not%20arrived)%2C%20and%0A%20%20%20%20the%20first%20three%20households%20of%20stratum%20C%20with%20no%20final%20weight.%22%22%22%0A%20%20%20%20late%20%3D%20frame.loc%5Bframe%5B%22stratum_id%22%5D%20%3D%3D%20%2201-033-A%22%2C%20%22psu_id%22%5D.unique()%5B1%3A%5D%0A%20%20%20%20sent%20%3D%20frame%5B~frame%5B%22psu_id%22%5D.isin(late)%5D.copy()%0A%20%20%20%20sent.loc%5Bsent.index%5Bsent%5B%22sel_stratum%22%5D%20%3D%3D%20%22C%22%5D%5B%3A3%5D%2C%20%22sel_final_weight%22%5D%20%3D%20np.nan%0A%20%20%20%20return%20sent%0A%0A%0A%40app.function%0Adef%20kept_rows(result)%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22Exercise%206.1's%20result%20in%20one%20form%20for%20the%20checks%3A%20each%20kept%20row's%20household%2C%20stratum%2C%20PSU%20and%20lonely_psu%2C%20sorted.%22%22%22%0A%20%20%20%20return%20as_rows(result%5B%5B%22region_code%22%2C%20%22hh_id%22%2C%20%22stratum_id%22%2C%20%22psu_id%22%2C%20%22lonely_psu%22%5D%5D.astype(%7B%22stratum_id%22%3A%20%22string%22%2C%20%22psu_id%22%3A%20%22string%22%2C%20%22lonely_psu%22%3A%20bool%7D)%2C%20%5B%22region_code%22%2C%20%22hh_id%22%5D)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(frame)%3A%0A%20%20%20%20check_cases(%0A%20%20%20%20%20%20%20%20%22Exercise%206.1%22%2C%20design_rows%2C%0A%20%20%20%20%20%20%20%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22ten%20households%3A%20a%20weight%2C%20a%20stratum%20and%20a%20PSU%20missing%3B%20two%20strata%20left%20with%20one%20PSU%22%2C%20(rows_fixture()%2C))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22the%20survey's%20frame%22%2C%20(frame%2C))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22the%20frame%20as%20a%20late%20export%20left%20it%3A%20three%20areas%20missing%2C%20three%20weights%20blank%22%2C%20(as_sent(frame)%2C))%2C%0A%20%20%20%20%20%20%20%20%5D%2C%0A%20%20%20%20%20%20%20%20what%3D%22frames%22%2C%20summarize%3Dkept_rows%2C%0A%20%20%20%20%20%20%20%20hint%3D%22Keep%20the%20rows%20with%20sel_final_weight%2C%20sel_stratum%20and%20psu_id%3B%20then%20count%20PSUs%20per%20stratum%20on%20those%20rows%20(transform('nunique'))%3B%20lonely_psu%20where%20the%20count%20is%201.%22%2C%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(frame)%3A%0A%20%20%20%20%23%20Try%20it%3A%20the%20frame%20as%20a%20late%20export%20left%20it%3B%20the%20lonely%20stratum's%20rows%20first%0A%20%20%20%20def%20_try_rows()%3A%0A%20%20%20%20%20%20%20%20try%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20kept%20%3D%20design_rows(as_sent(frame))%0A%20%20%20%20%20%20%20%20except%20NotImplementedError%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20mo.callout(mo.md(%22**Not%20written%20yet.**%20Write%20your%20code%2C%20delete%20the%20%60raise%60%20line%2C%20and%20run%20the%20function's%20cell.%22)%2C%20kind%3D%22neutral%22)%0A%20%20%20%20%20%20%20%20except%20Exception%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20try_it(design_rows%2C%20as_sent(frame))%20%20%23%20runs%20it%20again%2C%20to%20show%20the%20error%20and%20what%20it%20usually%20means%0A%20%20%20%20%20%20%20%20if%20not%20isinstance(kept%2C%20pd.DataFrame)%20or%20%22lonely_psu%22%20not%20in%20kept.columns%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20try_it(design_rows%2C%20as_sent(frame))%0A%20%20%20%20%20%20%20%20return%20mo.vstack(%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20mo.md(f%22**%7Blen(kept)%3A%2C%7D%20of%20%7Blen(as_sent(frame))%3A%2C%7D%20rows**%20kept%3B%20the%20strata%20with%20one%20PSU%3A%20%7B'%2C%20'.join(sorted(kept.loc%5Bkept%5B'lonely_psu'%5D%2C%20'stratum_id'%5D.unique()))%20or%20'none'%7D.%22)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20kept.sort_values(%22lonely_psu%22%2C%20ascending%3DFalse)%5B%5B%22region_code%22%2C%20%22hh_id%22%2C%20%22stratum_id%22%2C%20%22psu_id%22%2C%20%22sel_final_weight%22%2C%20%22lonely_psu%22%5D%5D.head(8)%2C%0A%20%20%20%20%20%20%20%20%5D)%0A%0A%20%20%20%20_try_rows()%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%206.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%20Y-A%20and%20Z-C%20not%20lonely%20%7C%20The%20PSUs%20were%20counted%20before%20the%20rows%20were%20dropped.%20%7C%20drop%20first%2C%20then%20%60transform(%22nunique%22)%60%20on%20the%20kept%20rows%20%7C%0A%20%20%20%20%7C%20The%20row%20with%20no%20weight%2C%20or%20no%20PSU%2C%20is%20still%20there%20%7C%20One%20of%20the%20three%20tests%20is%20missing.%20%7C%20%60sel_final_weight%60%2C%20%60sel_stratum%60%20and%20%60psu_id%60%2C%20each%20%60.notna()%60%20%7C%0A%20%20%20%20%7C%20The%20lonely%20strata's%20rows%20are%20gone%20%7C%20Their%20rows%20were%20dropped%2C%20as%20if%20%60%22remove%22%60%20dropped%20households.%20%7C%20keep%20them%2C%20with%20%60lonely_psu%60%20True%20%7C%0A%20%20%20%20%7C%20%60lonely_psu%60%20True%20on%20one%20row%20per%20stratum%20%7C%20The%20count%20was%20taken%20with%20%60groupby(...).nunique()%60%20and%20joined%20back%20wrongly%2C%20or%20only%20the%20first%20row%20marked.%20%7C%20%60transform%60%3A%20one%20value%20per%20row%20%7C%0A%20%20%20%20%7C%20*TypeError*%20or%20*ValueError*%20on%20%60%26%60%20%7C%20The%20tests%20were%20joined%20with%20%60and%60%2C%20which%20wants%20single%20values.%20%7C%20%60%26%60%2C%20with%20each%20test%20a%20column%20of%20True%20and%20False%20%7C%0A%0A%20%20%20%20Still%20stuck%20after%20ten%20minutes%3F%20Ask%20your%20table%2C%20then%20raise%20your%20hand.%0A%20%20%20%20%22%22%22)%7D)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20Give%20svy%20%60design_rows(frame)%60%20with%20%60singleton%3D%22skip%22%60%2C%20and%20the%20estimate%20keeps%20every%20household%20with%20a%20weight%20while%20a%20lonely%20stratum%20adds%20nothing%20to%20the%20variance%2C%20as%20ReGenesees%20does%20when%20its%20lonely-PSU%20option%20is%20%60%22remove%22%60%2C%20one%20of%20its%20four%20choices.%20The%20rows%20%60lonely_psu%60%20marks%20are%20the%20ones%20a%20table's%20notes%20name%2C%20because%20their%20stratum's%20sampling%20error%20is%20not%20in%20the%20SE%20printed%20beside%20them.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%20From%20fractions%20to%20counts%0A%0A%20%20%20%20The%20R%20code%20above%20holds%20the%20fpc%20as%20each%20stratum's%20sampling%20fraction%3B%20svy%20wants%20each%20stratum's%20population%20count.%20The%20count%20follows%20from%20the%20fraction%3A%20N%20%3D%20n%20%2F%20f%2C%20with%20n%20the%20stratum's%20PSUs%20in%20the%20sample.%20A%20count%20is%20also%20the%20stronger%20record%3A%20the%20fraction%20of%20a%20take-all%20stratum%20is%201%20whatever%20the%20data%20holds%2C%20so%20a%20copied%20household%20would%20pass%20unseen%2C%20while%20the%20register's%20count%20caught%20it.%0A%0A%20%20%20%20%23%23%23%20Exercise%206.2.%20Population%20counts%20from%20sampling%20fractions%0A%0A%20%20%20%20**Why%20it%20matters%3A**%20this%20is%20the%20first%20line%20of%20any%20translation%20of%20an%20R%20design%20to%20svy.%20Get%20n%20wrong%20(households%20instead%20of%20PSUs)%20and%20the%20fpc%20is%20wrong%20in%20every%20stratum%2C%20silently%3A%20svy%20takes%20any%20count%20of%20at%20least%20n.%0A%0A%20%20%20%20**Input%20%E2%86%92%20output%3A**%20a%20frame%20with%20%60stratum_id%60%2C%20%60psu_id%60%20and%20a%20column%20of%20sampling%20fractions%20(its%20name%20is%20the%20argument%20%60fraction%60)%20%E2%86%92%20a%20column%20of%20whole%20numbers%2C%20one%20per%20row%2C%20each%20row's%20stratum's%20population%20count%20N%20%3D%20n%20%2F%20f%3A%0A%0A%20%20%20%20%7C%20stratum_id%20%7C%20psu_id%20%7C%20fraction%20%7C%20%E2%86%92%20N%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C--%3A%7C--%3A%7C%0A%20%20%20%20%7C%20S-A%20%7C%201%2C%202%2C%203%2C%204%20(several%20households%20each)%20%7C%200.4%20%7C%2010%20%7C%0A%20%20%20%20%7C%20S-F%20%7C%20one%20per%20household%2C%204%20households%20%7C%201%20%7C%204%20%7C%0A%20%20%20%20%7C%20S-C%20%7C%2010%20PSUs%20%7C%200.0667%20(10%20of%20150%2C%20written%20to%20four%20decimals)%20%7C%20150%20%7C%0A%0A%20%20%20%20The%20last%20row%20is%20why%20the%20result%20is%20rounded%20to%20a%20whole%20number%3A%2010%20%2F%200.0667%20is%20149.93.%20It%20is%20this%20survey's%20own%20stratum%20C.%0A%0A%20%20%20%20**Tools%3A**%20%60groupby(...)%5B%22psu_id%22%5D.transform(%22nunique%22)%60%20(module%205's%20%60transform%60)%2C%20division%2C%20%60.round()%60%20and%20%60.astype(%22int64%22)%60.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20population_counts(frame%3A%20pd.DataFrame%2C%20fraction%3A%20str%20%3D%20%22fraction%22)%20-%3E%20pd.Series%3A%0A%20%20%20%20%22%22%22Each%20row's%20stratum's%20population%20count%20of%20PSUs%3A%20the%20PSUs%20in%20the%20sample%20divided%20by%20the%20sampling%20fraction%2C%20rounded.%22%22%22%0A%20%20%20%20%23%20Exercise%206.2%3A%20the%20task%20and%20examples%20are%20in%20the%20text%20above%20this%20cell.%0A%20%20%20%20%23%20Step%201%3A%20n%2C%20each%20row's%20stratum's%20number%20of%20distinct%20PSUs%3A%20groupby(%22stratum_id%22)%5B%22psu_id%22%5D.transform(%22nunique%22)%0A%20%20%20%20%23%20Step%202%3A%20divide%20n%20by%20the%20row's%20fraction%0A%20%20%20%20%23%20Step%203%3A%20round%20to%20a%20whole%20number%20and%20return%20it%20as%20int64%0A%20%20%20%20raise%20NotImplementedError(%22Exercise%206.2%22)%0A%0A%0A%40app.function%0Adef%20fractions_fixture()%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22Three%20strata%20with%20sampling%20fractions%2C%20used%20by%20the%20check%20of%20Exercise%206.2%3A%20four%20areas%20of%20several%20households%2C%20a%0A%20%20%20%20take-all%20stratum%20whose%20households%20are%20their%20own%20PSUs%2C%20and%20ten%20areas%20at%20a%20fraction%20written%20to%20four%20decimals.%22%22%22%0A%20%20%20%20rows%20%3D%20%5B(%22S-A%22%2C%20%221%22%2C%200.4)%5D%20*%203%20%2B%20%5B(%22S-A%22%2C%20%222%22%2C%200.4)%5D%20*%202%20%2B%20%5B(%22S-A%22%2C%20%223%22%2C%200.4)%5D%20*%204%20%2B%20%5B(%22S-A%22%2C%20%224%22%2C%200.4)%5D%20*%202%0A%20%20%20%20rows%20%2B%3D%20%5B(%22S-F%22%2C%20f%22h%7Bk%7D%22%2C%201.0)%20for%20k%20in%20range(1%2C%205)%5D%0A%20%20%20%20rows%20%2B%3D%20%5B(%22S-C%22%2C%20str(k)%2C%200.0667)%20for%20k%20in%20range(1%2C%2011)%5D%20%2B%20%5B(%22S-C%22%2C%20%223%22%2C%200.0667)%2C%20(%22S-C%22%2C%20%228%22%2C%200.0667)%5D%0A%20%20%20%20return%20pd.DataFrame(rows%2C%20columns%3D%5B%22stratum_id%22%2C%20%22psu_id%22%2C%20%22fraction%22%5D)%0A%0A%0A%40app.function%0Adef%20as_values(result%2C%20digits%3A%20int%20%3D%204)%20-%3E%20list%3A%0A%20%20%20%20%22%22%22A%20column's%20values%20in%20order%2C%20rounded%2C%20for%20the%20checks%3A%20its%20name%20and%20index%20don't%20count.%22%22%22%0A%20%20%20%20return%20%5BNone%20if%20pd.isna(value)%20else%20(value%20if%20isinstance(value%2C%20str)%20else%20round(float(value)%2C%20digits))%20for%20value%20in%20pd.Series(result).tolist()%5D%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(frame)%3A%0A%20%20%20%20_with_fractions%20%3D%20frame.assign(fraction%3Dframe%5B%22sel_stratum%22%5D.map(FRACTIONS))%0A%20%20%20%20check_cases(%0A%20%20%20%20%20%20%20%20%22Exercise%206.2%22%2C%20population_counts%2C%0A%20%20%20%20%20%20%20%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22three%20small%20strata%3A%20areas%20of%20several%20households%2C%20a%20take-all%20stratum%2C%20a%20fraction%20written%20as%200.0667%22%2C%20(fractions_fixture()%2C))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22the%20survey%2C%20with%20the%20fractions%201%2C%200.4%2C%200.2%20and%200.0667%22%2C%20(_with_fractions%2C))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22the%20survey's%20region%2004%2C%20the%20fraction%20in%20a%20column%20named%20f%22%2C%20(_with_fractions%5B_with_fractions%5B%22region_code%22%5D%20%3D%3D%20%2204%22%5D.rename(columns%3D%7B%22fraction%22%3A%20%22f%22%7D)%2C%20%22f%22))%2C%0A%20%20%20%20%20%20%20%20%5D%2C%0A%20%20%20%20%20%20%20%20what%3D%22frames%22%2C%20summarize%3Das_values%2C%0A%20%20%20%20%20%20%20%20hint%3D%22n%20is%20the%20number%20of%20distinct%20PSUs%20in%20the%20row's%20stratum%20(transform('nunique'))%3B%20N%20%3D%20n%20%2F%20f%2C%20rounded%2C%20as%20whole%20numbers.%22%2C%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(frame)%3A%0A%20%20%20%20%23%20Try%20it%3A%20the%20counts%20your%20function%20gives%2C%20beside%20the%20frame's%20counts%20from%20stratum_counts.csv%0A%20%20%20%20def%20_try_counts()%3A%0A%20%20%20%20%20%20%20%20with_fractions%20%3D%20frame.assign(fraction%3Dframe%5B%22sel_stratum%22%5D.map(FRACTIONS))%0A%20%20%20%20%20%20%20%20try%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20counts%20%3D%20population_counts(with_fractions)%0A%20%20%20%20%20%20%20%20except%20NotImplementedError%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20mo.callout(mo.md(%22**Not%20written%20yet.**%20Write%20your%20code%2C%20delete%20the%20%60raise%60%20line%2C%20and%20run%20the%20function's%20cell.%22)%2C%20kind%3D%22neutral%22)%0A%20%20%20%20%20%20%20%20return%20with_fractions.assign(from_fraction%3Dcounts).groupby(%22stratum_id%22)%5B%5B%22fraction%22%2C%20%22from_fraction%22%2C%20%22psus_in_stratum%22%5D%5D.first()%0A%0A%20%20%20%20_try_counts()%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%206.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%2028%20for%20the%20four-area%20stratum%2C%20thousands%20for%20the%20survey's%20stratum%20C%20%7C%20Households%20divided%20by%20the%20fraction%2C%20not%20PSUs.%20%7C%20%60transform(%22nunique%22)%60%20on%20%60psu_id%60%20%7C%0A%20%20%20%20%7C%20149%20for%20the%20stratum%20of%200.0667%20%7C%20The%20decimals%20were%20cut%2C%20not%20rounded.%20%7C%20%60.round()%60%20before%20%60.astype(%22int64%22)%60%20%7C%0A%20%20%20%20%7C%20One%20number%20for%20the%20whole%20table%20%7C%20%60nunique()%60%20without%20%60transform%60%3A%20one%20count%2C%20not%20one%20per%20row.%20%7C%20%60groupby(%22stratum_id%22)%5B%22psu_id%22%5D.transform(%22nunique%22)%60%20%7C%0A%20%20%20%20%7C%20*KeyError%3A%20'fraction'*%20on%20the%20last%20check%20%7C%20The%20column%20name%20was%20written%20in%2C%20not%20taken%20from%20the%20argument.%20%7C%20%60frame%5Bfraction%5D%60%20%7C%0A%0A%20%20%20%20Still%20stuck%20after%20ten%20minutes%3F%20Ask%20your%20table%2C%20then%20raise%20your%20hand.%0A%20%20%20%20%22%22%22)%7D)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20In%20the%20try-it%20table%20the%20counts%20from%20the%20fractions%20match%20the%20register's%20counts%20in%20every%20area%20stratum.%20Two%20take-all%20strata%20give%20one%20fewer%20than%20%60psus_in_stratum%60%3A%20a%20household%20of%20each%20was%20removed%20from%20the%20data%2C%20so%20n%20is%20one%20short%20of%20the%20register%2C%20and%20only%20the%20count%20says%20so.%0A%0A%20%20%20%20%23%23%23%20The%20design%20in%20svy%0A%0A%20%20%20%20With%20the%20copies%20out%2C%20the%20design%20is%20declared%20once%2C%20and%20every%20later%20cell%20estimates%20from%20%60sample%60%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20survey_design(weight%3A%20str%20%3D%20%22sel_final_weight%22)%3A%0A%20%20%20%20%22%22%22The%20survey's%20design%3A%2024%20strata%2C%20the%20area%20(or%2C%20in%20stratum%20F%2C%20the%20household)%20as%20PSU%2C%20a%20weight%2C%20PSU%20counts%20as%20the%20fpc.%0A%20%20%20%20A%20new%20Design%20each%20call%3A%20in%20svy%200.36.0%20a%20sample's%20%60update_design%60%20method%20also%20changes%20the%20sample%20it%20is%20called%20on%2C%20so%0A%20%20%20%20the%20cells%20below%20build%20a%20new%20sample%20from%20this%20function%20instead%20of%20updating%20a%20shared%20one.%22%22%22%0A%20%20%20%20return%20svy.Design(stratum%3D%22stratum_id%22%2C%20psu%3D%22psu_id%22%2C%20wgt%3Dweight%2C%20pop_size%3D%22psus_in_stratum%22%2C%20singleton%3D%22skip%22)%0A%0A%0A%40app.cell%0Adef%20_(frame)%3A%0A%20%20%20%20%23%20The%20survey's%20design%20and%20sample%2C%20which%20every%20later%20cell%20estimates%20from%0A%20%20%20%20design%20%3D%20survey_design()%0A%20%20%20%20sample%20%3D%20svy.Sample(data%3Dpl.from_pandas(frame)%2C%20design%3Ddesign)%0A%20%20%20%20mo.md(f%22%60sample%60%3A%20**%7Bsample.n_records%3A%2C%7D%20households%2C%20%7Bsample.n_strata%7D%20strata%2C%20%7Bsample.n_psus%7D%20PSUs.**%20The%20design's%20degrees%20of%20freedom%2C%20PSUs%20minus%20strata%2C%20are%20%7Bsample.n_psus%20-%20sample.n_strata%7D%3A%20section%202%20uses%20them%20for%20the%20confidence%20interval.%22)%0A%20%20%20%20return%20(sample%2C)%0A%0A%0A%40app.cell%0Adef%20_(frame)%3A%0A%20%20%20%20%23%20What%20svy%20says%20to%20the%20two%20R%20habits%3A%20a%20fraction%20as%20the%20fpc%2C%20and%20a%20pandas%20table%0A%20%20%20%20_with_fractions%20%3D%20frame.assign(fraction%3Dframe%5B%22sel_stratum%22%5D.map(FRACTIONS))%0A%20%20%20%20_as_fraction%20%3D%20svy_error(lambda%3A%20svy.Sample(%0A%20%20%20%20%20%20%20%20data%3Dpl.from_pandas(_with_fractions)%2C%20design%3Dsvy.Design(stratum%3D%22stratum_id%22%2C%20psu%3D%22psu_id%22%2C%20wgt%3D%22sel_final_weight%22%2C%20pop_size%3D%22fraction%22)%2C%0A%20%20%20%20).estimation.total(y%3D%22units%22))%0A%20%20%20%20_as_pandas%20%3D%20svy_error(lambda%3A%20svy.Sample(data%3Dframe%2C%20design%3Dsvy.Design(stratum%3D%22stratum_id%22%2C%20psu%3D%22psu_id%22%2C%20wgt%3D%22sel_final_weight%22)))%0A%20%20%20%20mo.md(f%22**The%20fpc%20as%20fractions%3A**%5Cn%5Cn%60%60%60%5Cn%7B_as_fraction%7D%5Cn%60%60%60%5Cn%5Cn**A%20pandas%20table%3A**%5Cn%5Cn%60%60%60%5Cn%7B_as_pandas%7D%5Cn%60%60%60%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%20What%20the%20cluster%20is%0A%0A%20%20%20%20The%20PSU%20id%20decides%20what%20the%20variance%20treats%20as%20one%20draw.%20This%20survey%20drew%20areas%20and%20interviewed%20every%20fishing%20household%20in%20each%2C%20so%20the%20area%20is%20the%20cluster.%20Declare%20each%20household%20as%20its%20own%20PSU%20instead%2C%20and%20the%20variance%20treats%20the%20households%20of%20one%20area%20as%20separate%20draws%2C%20as%20if%20they%20had%20been%20picked%20one%20by%20one.%20The%20cell%20below%20declares%20both%2C%20each%20without%20the%20fpc%20so%20that%20only%20the%20cluster%20differs%2C%20and%20sets%20them%20side%20by%20side.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(frame)%3A%0A%20%20%20%20%23%20The%20same%20survey%20with%20the%20area%20as%20cluster%2C%20and%20with%20each%20household%20as%20its%20own%20cluster%3B%20no%20fpc%20in%20either%0A%20%20%20%20_rows%20%3D%20pl.from_pandas(frame.assign(household_psu%3Dframe%5B%22region_code%22%5D%20%2B%20%22-%22%20%2B%20frame%5B%22hh_id%22%5D.astype(%22string%22)))%0A%20%20%20%20_designs%20%3D%20%7B%0A%20%20%20%20%20%20%20%20%22the%20area%20(psu_id)%22%3A%20svy.Design(stratum%3D%22stratum_id%22%2C%20psu%3D%22psu_id%22%2C%20wgt%3D%22sel_final_weight%22%2C%20singleton%3D%22skip%22)%2C%0A%20%20%20%20%20%20%20%20%22the%20household%22%3A%20svy.Design(stratum%3D%22stratum_id%22%2C%20psu%3D%22household_psu%22%2C%20wgt%3D%22sel_final_weight%22%2C%20singleton%3D%22skip%22)%2C%0A%20%20%20%20%7D%0A%20%20%20%20_table%20%3D%20%5B%5D%0A%20%20%20%20for%20_label%2C%20_design%20in%20_designs.items()%3A%0A%20%20%20%20%20%20%20%20_one%20%3D%20svy.Sample(data%3D_rows%2C%20design%3D_design)%0A%20%20%20%20%20%20%20%20_units%20%3D%20_one.estimation.total(y%3D%22units%22).to_polars()%0A%20%20%20%20%20%20%20%20_large%20%3D%20_one.estimation.total(y%3D%22large_unit_operators%22).to_polars()%0A%20%20%20%20%20%20%20%20_table.append(%7B%22the%20cluster%22%3A%20_label%2C%20%22clusters%22%3A%20_one.n_psus%2C%20%22df%22%3A%20_units%5B%22df%22%5D%5B0%5D%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%22SE%2C%20total%20units%22%3A%20_units%5B%22se%22%5D%5B0%5D%2C%20%22SE%2C%20large-scale%20operators%22%3A%20_large%5B%22se%22%5D%5B0%5D%7D)%0A%20%20%20%20_table%20%3D%20pd.DataFrame(_table)%0A%20%20%20%20_units%20%3D%20_table.loc%5B1%2C%20%22SE%2C%20total%20units%22%5D%20%2F%20_table.loc%5B0%2C%20%22SE%2C%20total%20units%22%5D%0A%20%20%20%20_large%20%3D%20_table.loc%5B1%2C%20%22SE%2C%20large-scale%20operators%22%5D%20%2F%20_table.loc%5B0%2C%20%22SE%2C%20large-scale%20operators%22%5D%0A%20%20%20%20mo.vstack(%5B%0A%20%20%20%20%20%20%20%20_table.round(1)%2C%0A%20%20%20%20%20%20%20%20mo.md(f%22With%20each%20household%20its%20own%20cluster%2C%20the%20degrees%20of%20freedom%20grow%20from%20%7B_table.loc%5B0%2C%20'df'%5D%7D%20to%20%7B_table.loc%5B1%2C%20'df'%5D%3A%2C%7D.%20The%20SE%20of%20the%20total%20of%20units%20is%20%7B_units%3A.0%25%7D%20of%20its%20value%20with%20the%20area%20as%20the%20cluster%2C%20and%20the%20SE%20of%20the%20large-scale%20operators%20%7B_large%3A.0%25%7D.%22)%2C%0A%20%20%20%20%5D)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20The%20estimates%20themselves%20don't%20change%3B%20only%20what%20is%20said%20about%20their%20precision.%20How%20far%20an%20SE%20moves%2C%20and%20which%20way%2C%20depends%20on%20how%20alike%20the%20households%20of%20an%20area%20are%20in%20what%20is%20measured%2C%20so%20the%20move%20differs%20from%20one%20total%20to%20the%20next.%20The%20degrees%20of%20freedom%20always%20grow%2C%20because%20every%20household%20is%20counted%20as%20a%20draw%20of%20its%20own%2C%20and%20a%20confidence%20interval%20narrows%20with%20them.%20Only%20one%20of%20the%20two%20designs%20describes%20how%20the%20survey%20was%20drawn.%0A%0A%20%20%20%20%23%23%202%20%C2%B7%20Totals%2C%20means%20and%20proportions%20with%20SE%2C%20CV%20and%20CI%20by%20domain%0A%0A%20%20%20%20**The%20problem.**%20How%20many%20fishing%20units%20do%20the%20three%20regions'%20households%20run%2C%20and%20how%20sure%20is%20that%20number%3F%20The%20weighted%20total%20answers%20the%20first%20half.%20The%20second%20half%20is%20a%20formula%20with%20the%20design%20in%20it.%0A%0A%20%20%20%20**The%20formula.**%20Write%20w%20for%20the%20weight%20and%20y%20for%20the%20value%20of%20household%20k%20in%20PSU%20i%20of%20stratum%20h.%20The%20estimate%20and%20its%20variance%3A%0A%0A%20%20%20%20%60%60%60%0A%20%20%20%20total%20%20%20%20%20%20%20%20%20%20%20%20%C5%B6%20%20%3D%20%CE%A3_h%20%CE%A3_i%20%CE%A3_k%20%20w%C2%B7y%0A%20%20%20%20PSU%20total%20%20%20%20%20%20%20%20t_hi%20%3D%20%CE%A3_k%20%20w%C2%B7y%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20the%20weighted%20total%20of%20one%20PSU%0A%20%20%20%20variance%20%20%20%20%20%20%20%20%20V%20%20%3D%20%CE%A3_h%20%20(1%20%E2%88%92%20n_h%20%2F%20N_h)%20%C2%B7%20n_h%20%2F%20(n_h%20%E2%88%92%201)%20%C2%B7%20%CE%A3_i%20(t_hi%20%E2%88%92%20t%CC%84_h)%C2%B2%0A%20%20%20%20standard%20error%20%20%20SE%20%3D%20%E2%88%9AV%0A%20%20%20%20coefficient%20of%20variation%20%20%20%20CV%20%3D%20SE%20%2F%20%C5%B6%0A%20%20%20%2095%25%20confidence%20interval%20%20%20%20%20%C5%B6%20%C2%B1%20t(0.975%2C%20df)%20%C2%B7%20SE%2C%20%20%20df%20%3D%20PSUs%20%E2%88%92%20strata%0A%20%20%20%20%60%60%60%0A%0A%20%20%20%20In%20words%3A%20add%20up%20the%20weighted%20values%20to%20a%20total%20per%20PSU%3B%20within%20each%20stratum%2C%20measure%20how%20far%20the%20PSUs'%20totals%20spread%20around%20their%20mean%3B%20scale%20that%20by%20n%20%2F%20(n%20%E2%88%92%201)%2C%20as%20any%20sample%20variance%20is%3B%20shrink%20it%20by%20the%20fpc%2C%20which%20is%200%20where%20every%20PSU%20was%20taken%3B%20add%20up%20the%20strata.%20Only%20the%20spread%20**between%20PSUs**%20enters%3A%20the%20households%20inside%20an%20area%20move%20with%20their%20area's%20total%2C%20so%20their%20differences%20are%20in%20it%20already.%20This%20is%20the%20*ultimate%20cluster*%20estimator%20that%20R's%20survey%20package%2C%20ReGenesees%20and%20svy%20all%20use%20for%20a%20design%20like%20this%20one.%20A%20take-all%20stratum%20whose%20every%20household%20is%20in%20the%20data%20has%20an%20fpc%20of%200%20and%20adds%20nothing%20to%20V.%0A%0A%20%20%20%20%60n_h%60%20and%20%60N_h%60%20are%20the%20stratum's%20PSUs%20in%20the%20sample%20and%20in%20the%20population%3B%20a%20stratum%20with%20one%20PSU%20(n_h%20%3D%201)%20adds%20nothing%2C%20which%20is%20the%20%60skip%60%20rule.%20The%20**CV**%20is%20the%20SE%20as%20a%20share%20of%20the%20estimate%3A%20a%20CV%20of%200.1%20says%20the%20SE%20is%20a%20tenth%20of%20the%20total.%20The%20**degrees%20of%20freedom**%20count%20the%20independent%20pieces%20of%20information%20about%20the%20variance%2C%20one%20per%20PSU%20less%20one%20per%20stratum%3B%20the%20confidence%20interval%20uses%20Student's%20t%20with%20them.%0A%0A%20%20%20%20svy%20computes%20all%20of%20it%20in%20one%20call%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(sample)%3A%0A%20%20%20%20%23%20svy%3A%20the%20total%20of%20fishing%20units%2C%20with%20its%20SE%2C%2095%25%20CI%2C%20CV%20(as%20a%20fraction%3A%200.02%20is%202%25)%2C%20degrees%20of%20freedom%20and%20households%20used%0A%20%20%20%20sample.estimation.total(y%3D%22units%22).to_polars()%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%20Exercise%206.3.%20A%20stratified%20total%20and%20its%20SE%2C%20from%20the%20formula%0A%0A%20%20%20%20**Why%20it%20matters%3A**%20this%20formula%20is%20under%20every%20SE%20this%20course%20prints.%20Written%20once%20from%20the%20formula%2C%20it%20shows%20what%20the%20library%20does%2C%20it%20is%20what%20you%20check%20a%20library%20against%2C%20and%20section%204%20builds%20every%20other%20estimator%20on%20it.%0A%0A%20%20%20%20**Input%20%E2%86%92%20output%3A**%20a%20frame%20with%20%60stratum_id%60%2C%20%60psu_id%60%2C%20%60psus_in_stratum%60%20and%20%60sel_final_weight%60%2C%20and%20the%20name%20of%20a%20column%20%60y%60%20%E2%86%92%20a%20Series%20with%20three%20values%2C%20labeled%20%60est%60%2C%20%60se%60%20and%20%60cv%60%3A%0A%0A%20%20%20%20%7C%20%7C%20est%20%7C%20se%20%7C%20cv%20%7C%0A%20%20%20%20%7C%3A--%7C--%3A%7C--%3A%7C--%3A%7C%0A%20%20%20%20%7C%20the%20small%20design%20below%2C%20%60y%60%20%7C%2040%20%7C%207.4833%20%7C%200.1871%20%7C%0A%0A%20%20%20%20The%20small%20design%20has%20a%20take-all%20stratum%20T%20(3%20households%2C%20each%20its%20own%20PSU%2C%20N%20%3D%203%2C%20weight%201%2C%20y%20%3D%202%2C%205%2C%201)%20and%20a%20stratum%20S%20of%203%20PSUs%20out%20of%20N%20%3D%206%2C%20every%20weight%204%3A%20PSU%20s1%20holds%20y%20%3D%201%20and%200%2C%20s2%20holds%203%2C%20s3%20holds%201%2C%201%20and%202.%20Work%20it%20by%20hand%20once%3A%20T%20adds%208%20to%20the%20total%20and%20nothing%20to%20V%3B%20S's%20PSU%20totals%20are%204%2C%2012%20and%2016.%0A%0A%20%20%20%20**Tools%3A**%20a%20column%20of%20%60w%C2%B7y%60%20(%60assign%60)%2C%20%60groupby(%5B%22stratum_id%22%2C%20%22psu_id%22%5D).agg(...)%60%20for%20the%20PSU%20totals%2C%20%60groupby(%22stratum_id%22)%5B...%5D.transform(%22size%22)%60%20and%20%60transform(%22mean%22)%60%20for%20n_h%20and%20t%CC%84_h%2C%20then%20the%20formula.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20stratified_total(frame%3A%20pd.DataFrame%2C%20y%3A%20str)%20-%3E%20pd.Series%3A%0A%20%20%20%20%22%22%22The%20design-based%20total%20of%20column%20y%2C%20its%20standard%20error%20and%20its%20coefficient%20of%20variation%20(est%2C%20se%2C%20cv).%22%22%22%0A%20%20%20%20%23%20Exercise%206.3%3A%20the%20task%20and%20examples%20are%20in%20the%20text%20above%20this%20cell.%0A%20%20%20%20%23%20Step%201%3A%20each%20row's%20w%C2%B7y%3A%20frame%5B%22sel_final_weight%22%5D%20*%20frame%5By%5D%0A%20%20%20%20%23%20Step%202%3A%20each%20PSU's%20total%20t_hi%2C%20and%20its%20stratum's%20N_h%3A%20groupby(%5B%22stratum_id%22%2C%20%22psu_id%22%5D).agg(t%3D(%22wy%22%2C%20%22sum%22)%2C%20N%3D(%22psus_in_stratum%22%2C%20%22first%22))%0A%20%20%20%20%23%20Step%203%3A%20on%20that%20table%2C%20n_h%20(transform(%22size%22)%20within%20strata)%20and%20t%CC%84_h%20(transform(%22mean%22)%20within%20strata)%0A%20%20%20%20%23%20Step%204%3A%20each%20PSU's%20term%20(1%20-%20n_h%2FN_h)%20*%20n_h%2F(n_h%20-%201)%20*%20(t_hi%20-%20t%CC%84_h)**2%3B%20V%20is%20the%20sum%20over%20strata%20with%20n_h%20%3E%201%0A%20%20%20%20%23%20Step%205%3A%20est%20%3D%20sum%20of%20w%C2%B7y%2C%20se%20%3D%20%E2%88%9AV%2C%20cv%20%3D%20se%20%2F%20est%3B%20return%20pd.Series(%7B%22est%22%3A%20...%2C%20%22se%22%3A%20...%2C%20%22cv%22%3A%20...%7D)%0A%20%20%20%20raise%20NotImplementedError(%22Exercise%206.3%22)%0A%0A%0A%40app.function%0Adef%20small_design()%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22A%20small%20design%2C%20used%20by%20the%20checks%20of%20Exercises%206.3%2C%206.6%20and%206.8%3A%20a%20take-all%20stratum%20T%20of%20three%20households%2C%0A%20%20%20%20each%20its%20own%20PSU%2C%20and%20a%20stratum%20S%20of%203%20PSUs%20out%20of%206%2C%20PSUs%20of%20one%20to%20three%20households.%20Columns%20y%20and%20x%20are%20values%2C%20d%20a%0A%20%20%20%20domain%2C%20province%20two%20provinces%20across%20the%20strata.%22%22%22%0A%20%20%20%20rows%20%3D%20%5B%0A%20%20%20%20%20%20%20%20%23%20stratum_id%2C%20psu_id%2C%20psus_in_stratum%2C%20sel_final_weight%2C%20y%2C%20x%2C%20d%2C%20province%0A%20%20%20%20%20%20%20%20(%22T%22%2C%20%22T-h1%22%2C%203%2C%201.0%2C%202.0%2C%201.0%2C%20True%2C%20%22P%22)%2C%0A%20%20%20%20%20%20%20%20(%22T%22%2C%20%22T-h2%22%2C%203%2C%201.0%2C%205.0%2C%202.0%2C%20False%2C%20%22Q%22)%2C%0A%20%20%20%20%20%20%20%20(%22T%22%2C%20%22T-h3%22%2C%203%2C%201.0%2C%201.0%2C%201.0%2C%20True%2C%20%22Q%22)%2C%0A%20%20%20%20%20%20%20%20(%22S%22%2C%20%22s1%22%2C%206%2C%204.0%2C%201.0%2C%202.0%2C%20True%2C%20%22P%22)%2C%0A%20%20%20%20%20%20%20%20(%22S%22%2C%20%22s1%22%2C%206%2C%204.0%2C%200.0%2C%201.0%2C%20False%2C%20%22P%22)%2C%0A%20%20%20%20%20%20%20%20(%22S%22%2C%20%22s2%22%2C%206%2C%204.0%2C%203.0%2C%202.0%2C%20False%2C%20%22Q%22)%2C%0A%20%20%20%20%20%20%20%20(%22S%22%2C%20%22s3%22%2C%206%2C%204.0%2C%201.0%2C%203.0%2C%20True%2C%20%22P%22)%2C%0A%20%20%20%20%20%20%20%20(%22S%22%2C%20%22s3%22%2C%206%2C%204.0%2C%201.0%2C%201.0%2C%20False%2C%20%22Q%22)%2C%0A%20%20%20%20%20%20%20%20(%22S%22%2C%20%22s3%22%2C%206%2C%204.0%2C%202.0%2C%202.0%2C%20True%2C%20%22Q%22)%2C%0A%20%20%20%20%5D%0A%20%20%20%20return%20pd.DataFrame(rows%2C%20columns%3D%5B%22stratum_id%22%2C%20%22psu_id%22%2C%20%22psus_in_stratum%22%2C%20%22sel_final_weight%22%2C%20%22y%22%2C%20%22x%22%2C%20%22d%22%2C%20%22province%22%5D)%0A%0A%0A%40app.function%0Adef%20as_numbers(result%2C%20keys%3A%20list%5Bstr%5D%2C%20digits%3A%20int%20%3D%204)%20-%3E%20list%3A%0A%20%20%20%20%22%22%22Named%20numbers%20from%20a%20Series%20or%20a%20dict%2C%20in%20the%20order%20of%20keys%2C%20rounded%2C%20for%20the%20checks.%22%22%22%0A%20%20%20%20return%20%5Bround(float(result%5Bkey%5D)%2C%20digits)%20for%20key%20in%20keys%5D%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(frame)%3A%0A%20%20%20%20check_cases(%0A%20%20%20%20%20%20%20%20%22Exercise%206.3%22%2C%20stratified_total%2C%0A%20%20%20%20%20%20%20%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22the%20small%20design%3A%20a%20take-all%20stratum%20and%20a%20stratum%20of%203%20PSUs%20out%20of%206%22%2C%20(small_design()%2C%20%22y%22))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22the%20survey's%20fishing%20units%22%2C%20(frame%2C%20%22units%22))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22the%20survey's%20large-scale%20operators%22%2C%20(frame%2C%20%22large_unit_operators%22))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22the%20survey's%20households%20in%20region%2007%3A%20the%20number%20of%20fishing%20households%22%2C%20(frame%5Bframe%5B%22region_code%22%5D%20%3D%3D%20%2207%22%5D%2C%20%22hh_count%22))%2C%0A%20%20%20%20%20%20%20%20%5D%2C%0A%20%20%20%20%20%20%20%20what%3D%22frames%20and%20columns%22%2C%20summarize%3Dlambda%20result%3A%20as_numbers(result%2C%20%5B%22est%22%2C%20%22se%22%2C%20%22cv%22%5D)%2C%0A%20%20%20%20%20%20%20%20hint%3D%22Add%20up%20w%C2%B7y%20per%20PSU%20first%3B%20within%20each%20stratum%2C%20(1%20-%20n%2FN)%20*%20n%2F(n-1)%20*%20the%20squared%20deviations%20of%20the%20PSU%20totals%3B%20skip%20strata%20with%20one%20PSU.%22%2C%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(frame)%3A%0A%20%20%20%20%23%20Try%20it%3A%20the%20total%20of%20fishing%20units%2C%20by%20your%20formula%3B%20compare%20it%20with%20svy's%20line%20above%0A%20%20%20%20try_it(stratified_total%2C%20frame%2C%20%22units%22)%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%206.3%22%3A%20mo.md(r%22%22%22%0A%20%20%20%20%7C%20what%20you%20see%20%7C%20the%20usual%20cause%20%7C%20look%20at%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20%60se%60%20far%20too%20small%20on%20the%20survey%20%7C%20The%20spread%20of%20households%2C%20not%20of%20PSU%20totals.%20%7C%20group%20by%20%60%5B%22stratum_id%22%2C%20%22psu_id%22%5D%60%20before%20the%20spread%20%7C%0A%20%20%20%20%7C%20%60se%60%20of%2010.58%20on%20the%20small%20design%20%7C%20The%20fpc%20left%20out.%20%7C%20%60(1%20-%20n%20%2F%20N)%60%20%7C%0A%20%20%20%20%7C%20%60se%60%20of%206.11%20on%20the%20small%20design%20%7C%20Divided%20by%20n%2C%20not%20n%20%E2%88%92%201%3A%20the%20n%20%2F%20(n%20%E2%88%92%201)%20factor%20is%20missing.%20%7C%20%60n%20%2F%20(n%20-%201)%60%20%7C%0A%20%20%20%20%7C%20%60se%60%20of%200%20or%20%60nan%60%20%7C%20Every%20stratum%20skipped%2C%20or%20n%20counted%20as%20households%20so%20that%20the%20fpc%20turned%20negative.%20%7C%20%60transform(%22size%22)%60%20on%20the%20PSU%20table%20%7C%0A%20%20%20%20%7C%20%60est%60%20too%20small%20by%20the%20weights%20%7C%20The%20values%20summed%2C%20not%20w%C2%B7y.%20%7C%20%60frame%5B%22sel_final_weight%22%5D%20*%20frame%5By%5D%60%20%7C%0A%20%20%20%20%7C%20*KeyError%3A%20'est'*%20%7C%20The%20result%20is%20not%20labeled%20%60est%60%2C%20%60se%60%2C%20%60cv%60.%20%7C%20%60pd.Series(%7B%22est%22%3A%20...%2C%20%22se%22%3A%20...%2C%20%22cv%22%3A%20...%7D)%60%20%7C%0A%0A%20%20%20%20Still%20stuck%20after%20ten%20minutes%3F%20Ask%20your%20table%2C%20then%20raise%20your%20hand.%0A%20%20%20%20%22%22%22)%7D)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%20Side%20by%20side%0A%0A%20%20%20%20The%20two%20ways%2C%20on%20three%20of%20the%20survey's%20totals.%20The%20last%20column%20is%20the%20largest%20difference%20between%20them%3B%20anything%20below%20a%20millionth%20is%20the%20order%20of%20the%20additions.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20agreement(rows%3A%20list%5Btuple%5Bstr%2C%20float%2C%20float%5D%5D)%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22A%20table%20of%20what%20svy%20gives%20and%20what%20the%20course's%20own%20function%20gives%2C%20and%20the%20difference%2C%20one%20row%20per%20number.%22%22%22%0A%20%20%20%20table%20%3D%20pd.DataFrame(rows%2C%20columns%3D%5B%22number%22%2C%20%22svy%22%2C%20%22own%22%5D)%0A%20%20%20%20table%5B%22difference%22%5D%20%3D%20(table%5B%22svy%22%5D%20-%20table%5B%22own%22%5D).abs()%0A%20%20%20%20return%20table%0A%0A%0A%40app.cell%0Adef%20_(frame%2C%20sample)%3A%0A%20%20%20%20def%20_side_by_side()%3A%0A%20%20%20%20%20%20%20%20rows%20%3D%20%5B%5D%0A%20%20%20%20%20%20%20%20for%20column%20in%20%5B%22units%22%2C%20%22large_unit_operators%22%2C%20%22hh_count%22%5D%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20ours%20%3D%20attempt(stratified_total%2C%20frame%2C%20column)%0A%20%20%20%20%20%20%20%20%20%20%20%20if%20ours%20is%20NOT_YET%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20return%20waiting_for(%22Exercise%206.3%22)%0A%20%20%20%20%20%20%20%20%20%20%20%20theirs%20%3D%20sample.estimation.total(y%3Dcolumn).to_polars()%0A%20%20%20%20%20%20%20%20%20%20%20%20rows%20%2B%3D%20%5B(f%22total%20of%20%7Bcolumn%7D%22%2C%20theirs%5B%22est%22%5D%5B0%5D%2C%20ours%5B%22est%22%5D)%2C%20(%22its%20SE%22%2C%20theirs%5B%22se%22%5D%5B0%5D%2C%20ours%5B%22se%22%5D)%2C%20(%22its%20CV%22%2C%20theirs%5B%22cv%22%5D%5B0%5D%2C%20ours%5B%22cv%22%5D)%5D%0A%20%20%20%20%20%20%20%20return%20agreement(rows)%0A%0A%20%20%20%20_side_by_side()%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%20The%20confidence%20interval%20and%20its%20degrees%20of%20freedom%0A%0A%20%20%20%20A%2095%25%20confidence%20interval%20is%20the%20estimate%20plus%20and%20minus%20t(0.975%2C%20df)%20standard%20errors%2C%20where%20t%20comes%20from%20Student's%20distribution%20with%20the%20design's%20degrees%20of%20freedom%3A%20PSUs%20minus%20strata%2C%20counted%20over%20the%20PSUs%20and%20strata%20that%20hold%20the%20rows%20used.%20With%20many%20degrees%20of%20freedom%20t%20is%20close%20to%201.96%3B%20with%2010%20it%20is%202.23.%20%60scipy.stats.t.ppf(0.975%2C%20df)%60%20gives%20it%2C%20as%20R's%20%60qt(0.975%2C%20df)%60%20does.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20design_df(frame%3A%20pd.DataFrame%2C%20domain%3DNone)%20-%3E%20int%3A%0A%20%20%20%20%22%22%22The%20design's%20degrees%20of%20freedom%3A%20PSUs%20minus%20strata%2C%20counting%20only%20those%20that%20hold%20a%20row%20of%20the%20domain.%22%22%22%0A%20%20%20%20rows%20%3D%20frame%20if%20domain%20is%20None%20else%20frame%5Bnp.asarray(domain%2C%20dtype%3Dbool)%5D%0A%20%20%20%20return%20len(rows%5B%5B%22stratum_id%22%2C%20%22psu_id%22%5D%5D.drop_duplicates())%20-%20rows%5B%22stratum_id%22%5D.nunique()%0A%0A%0A%40app.function%0Adef%20t_interval(est%3A%20float%2C%20se%3A%20float%2C%20df%3A%20int%2C%20level%3A%20float%20%3D%200.95)%20-%3E%20tuple%5Bfloat%2C%20float%5D%3A%0A%20%20%20%20%22%22%22A%20confidence%20interval%3A%20est%20plus%20and%20minus%20Student's%20t%20at%20the%20level's%20quantile%2C%20on%20df%20degrees%20of%20freedom%2C%20times%20se.%22%22%22%0A%20%20%20%20t%20%3D%20stats.t.ppf(1%20-%20(1%20-%20level)%20%2F%202%2C%20df)%0A%20%20%20%20return%20est%20-%20t%20*%20se%2C%20est%20%2B%20t%20*%20se%0A%0A%0A%40app.cell%0Adef%20_(frame%2C%20sample)%3A%0A%20%20%20%20def%20_interval()%3A%0A%20%20%20%20%20%20%20%20ours%20%3D%20attempt(stratified_total%2C%20frame%2C%20%22units%22)%0A%20%20%20%20%20%20%20%20if%20ours%20is%20NOT_YET%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20waiting_for(%22Exercise%206.3%22)%0A%20%20%20%20%20%20%20%20theirs%20%3D%20sample.estimation.total(y%3D%22units%22).to_polars()%0A%20%20%20%20%20%20%20%20df%20%3D%20design_df(frame)%0A%20%20%20%20%20%20%20%20low%2C%20high%20%3D%20t_interval(ours%5B%22est%22%5D%2C%20ours%5B%22se%22%5D%2C%20df)%0A%20%20%20%20%20%20%20%20return%20agreement(%5B(%22degrees%20of%20freedom%22%2C%20theirs%5B%22df%22%5D%5B0%5D%2C%20df)%2C%20(%22lower%20end%20of%20the%2095%25%20CI%22%2C%20theirs%5B%22lci%22%5D%5B0%5D%2C%20low)%2C%20(%22upper%20end%22%2C%20theirs%5B%22uci%22%5D%5B0%5D%2C%20high)%5D)%0A%0A%20%20%20%20_interval()%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%20Means%20and%20proportions%3A%20a%20ratio%2C%20linearized%0A%0A%20%20%20%20**The%20problem.**%20The%20mean%20household%20size%20is%20a%20total%20divided%20by%20a%20total%3A%20%CE%A3%20w%C2%B7y%20%2F%20%CE%A3%20w.%20Its%20variance%20has%20no%20formula%20of%20its%20own%2C%20because%20both%20the%20numerator%20and%20the%20denominator%20vary%20from%20sample%20to%20sample.%0A%0A%20%20%20%20**The%20tool%3A%20linearization.**%20Replace%20the%20ratio%20by%20a%20total%20of%20a%20new%20variable%20that%20moves%20the%20same%20way.%20For%20a%20mean%20m%20%3D%20%C5%B6%20%2F%20N%CC%82%20that%20variable%20is%0A%0A%20%20%20%20%60%60%60%0A%20%20%20%20z%20%3D%20w%20%C2%B7%20(y%20%E2%88%92%20m)%20%2F%20N%CC%82%0A%20%20%20%20%60%60%60%0A%0A%20%20%20%20and%20the%20mean's%20variance%20is%20the%20variance%20of%20the%20total%20of%20z%2C%20by%20the%20same%20formula%20as%20Exercise%206.3%2C%20with%20z%20in%20the%20place%20of%20w%C2%B7y.%20This%20is%20a%20first-order%20Taylor%20expansion%20of%20the%20ratio%2C%20which%20is%20why%20the%20method%20is%20called%20**Taylor%20linearization**.%20A%20proportion%20is%20the%20mean%20of%20a%200%2F1%20column%2C%20so%20it%20needs%20nothing%20new.%20svy%20offers%20%60estimation.mean%60%20and%20%60estimation.prop%60%3B%20%60prop%60%20gives%20one%20row%20per%20level%20and%2C%20by%20default%2C%20a%20confidence%20interval%20computed%20on%20the%20logit%20scale%20and%20turned%20back%2C%20so%20it%20stays%20between%200%20and%201%3A%0A%0A%20%20%20%20%60%60%60%0A%20%20%20%20logit(p)%20%C2%B1%20t%20%C2%B7%20SE%20%2F%20(p%20%C2%B7%20(1%20%E2%88%92%20p))%2C%20%20%20then%20back%20with%201%20%2F%20(1%20%2B%20e%5E(%E2%88%92x))%0A%20%20%20%20%60%60%60%0A%0A%20%20%20%20The%20three%20functions%20below%20are%20the%20course's%20own.%20%60taylor_variance%60%20is%20the%20variance%20step%20of%20Exercise%206.3%20for%20any%20z%3A%20if%20you%20have%20not%20written%20Exercise%206.3%20yet%2C%20do%20it%20before%20reading%20them.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20taylor_variance(frame%3A%20pd.DataFrame%2C%20z)%20-%3E%20float%3A%0A%20%20%20%20%22%22%22The%20variance%20of%20the%20total%20of%20z%20under%20the%20design%3A%20z%20added%20up%20per%20PSU%2C%20the%20PSU%20totals'%20spread%20within%20each%20stratum%2C%0A%20%20%20%20times%20n%2F(n-1)%20and%20the%20fpc%2C%20added%20over%20strata.%20z%20has%20one%20value%20per%20row%20of%20the%20frame.%20A%20frame%20without%20psus_in_stratum%0A%20%20%20%20is%20taken%20as%20drawn%20with%20replacement%20(no%20fpc)%3B%20a%20stratum%20with%20one%20PSU%20adds%20nothing%20(the%20skip%20rule).%22%22%22%0A%20%20%20%20rows%20%3D%20frame%5B%5B%22stratum_id%22%2C%20%22psu_id%22%5D%5D.assign(z%3Dnp.asarray(z%2C%20dtype%3Dfloat))%0A%20%20%20%20rows%5B%22N%22%5D%20%3D%20frame%5B%22psus_in_stratum%22%5D.to_numpy()%20if%20%22psus_in_stratum%22%20in%20frame%20else%20np.inf%0A%20%20%20%20psu%20%3D%20rows.groupby(%5B%22stratum_id%22%2C%20%22psu_id%22%5D).agg(t%3D(%22z%22%2C%20%22sum%22)%2C%20N%3D(%22N%22%2C%20%22first%22)).reset_index()%0A%20%20%20%20stratum%20%3D%20psu.groupby(%22stratum_id%22)%5B%22t%22%5D%0A%20%20%20%20n%20%3D%20stratum.transform(%22size%22)%0A%20%20%20%20terms%20%3D%20(1%20-%20n%20%2F%20psu%5B%22N%22%5D)%20*%20n%20%2F%20(n%20-%201)%20*%20(psu%5B%22t%22%5D%20-%20stratum.transform(%22mean%22))%20**%202%0A%20%20%20%20return%20float(terms%5Bn%20%3E%201%5D.sum())%0A%0A%0A%40app.function%0Adef%20own_mean(frame%3A%20pd.DataFrame%2C%20y%3A%20str)%20-%3E%20pd.Series%3A%0A%20%20%20%20%22%22%22The%20design-based%20mean%20of%20y%2C%20linearized%3A%20m%20%3D%20%CE%A3wy%20%2F%20%CE%A3w%2C%20and%20its%20SE%20from%20z%20%3D%20w(y%20%E2%88%92%20m)%20%2F%20%CE%A3w.%22%22%22%0A%20%20%20%20w%20%3D%20frame%5B%22sel_final_weight%22%5D%0A%20%20%20%20N%20%3D%20w.sum()%0A%20%20%20%20m%20%3D%20(w%20*%20frame%5By%5D).sum()%20%2F%20N%0A%20%20%20%20se%20%3D%20taylor_variance(frame%2C%20w%20*%20(frame%5By%5D%20-%20m)%20%2F%20N)%20**%200.5%0A%20%20%20%20return%20pd.Series(%7B%22est%22%3A%20m%2C%20%22se%22%3A%20se%2C%20%22cv%22%3A%20se%20%2F%20m%7D)%0A%0A%0A%40app.function%0Adef%20own_prop(frame%3A%20pd.DataFrame%2C%20y%3A%20str%2C%20level%3A%20float%20%3D%200.95)%20-%3E%20pd.Series%3A%0A%20%20%20%20%22%22%22The%20design-based%20proportion%20of%20rows%20where%20the%200%2F1%20column%20y%20is%201%2C%20its%20SE%2C%20and%20a%20confidence%20interval%20on%20the%20logit%0A%20%20%20%20scale%2C%20as%20svy's%20estimation.prop%20gives%20by%20default.%22%22%22%0A%20%20%20%20p%2C%20se%2C%20_%20%3D%20own_mean(frame%2C%20y)%0A%20%20%20%20t%20%3D%20stats.t.ppf(1%20-%20(1%20-%20level)%20%2F%202%2C%20design_df(frame))%0A%20%20%20%20half%20%3D%20t%20*%20se%20%2F%20(p%20*%20(1%20-%20p))%0A%20%20%20%20return%20pd.Series(%7B%22est%22%3A%20p%2C%20%22se%22%3A%20se%2C%20%22lci%22%3A%20special.expit(special.logit(p)%20-%20half)%2C%20%22uci%22%3A%20special.expit(special.logit(p)%20%2B%20half)%7D)%0A%0A%0A%40app.cell%0Adef%20_(frame%2C%20sample)%3A%0A%20%20%20%20%23%20The%20mean%20household%20size%20and%20the%20share%20of%20households%20that%20run%20a%20large-scale%20unit%2C%20both%20ways%0A%20%20%20%20_size%20%3D%20sample.estimation.mean(y%3D%22hh_size%22).to_polars()%0A%20%20%20%20_share%20%3D%20sample.estimation.prop(y%3D%22runs_large_unit%22).to_polars().filter(pl.col(%22runs_large_unit%22)%20%3D%3D%201)%0A%20%20%20%20_own_size%2C%20_own_share%20%3D%20own_mean(frame%2C%20%22hh_size%22)%2C%20own_prop(frame%2C%20%22runs_large_unit%22)%0A%20%20%20%20agreement(%5B%0A%20%20%20%20%20%20%20%20(%22mean%20household%20size%22%2C%20_size%5B%22est%22%5D%5B0%5D%2C%20_own_size%5B%22est%22%5D)%2C%0A%20%20%20%20%20%20%20%20(%22its%20SE%22%2C%20_size%5B%22se%22%5D%5B0%5D%2C%20_own_size%5B%22se%22%5D)%2C%0A%20%20%20%20%20%20%20%20(%22share%20running%20a%20large-scale%20unit%22%2C%20_share%5B%22est%22%5D%5B0%5D%2C%20_own_share%5B%22est%22%5D)%2C%0A%20%20%20%20%20%20%20%20(%22its%20SE%22%2C%20_share%5B%22se%22%5D%5B0%5D%2C%20_own_share%5B%22se%22%5D)%2C%0A%20%20%20%20%20%20%20%20(%22its%2095%25%20CI%2C%20lower%20end%20(logit)%22%2C%20_share%5B%22lci%22%5D%5B0%5D%2C%20_own_share%5B%22lci%22%5D)%2C%0A%20%20%20%20%20%20%20%20(%22upper%20end%22%2C%20_share%5B%22uci%22%5D%5B0%5D%2C%20_own_share%5B%22uci%22%5D)%2C%0A%20%20%20%20%5D)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%20Domains%3A%20%60by%3D%60%20and%20%60where%3D%60%2C%20never%20a%20filter%20first%0A%0A%20%20%20%20**The%20problem.**%20A%20*domain*%20is%20a%20part%20of%20the%20population%20you%20estimate%20for%3A%20a%20province%2C%20the%20households%20that%20run%20a%20large-scale%20unit%2C%20the%20women%20operators.%20The%20tempting%20code%20keeps%20the%20domain's%20rows%20and%20estimates%20on%20them.%20The%20estimate%20comes%20out%20right%3B%20its%20SE%20does%20not.%20Filtering%20drops%20every%20PSU%20that%20has%20no%20member%20of%20the%20domain%2C%20and%20the%20formula%20then%20sees%20only%20the%20areas%20that%20happened%20to%20hold%20one%2C%20as%20if%20every%20area%20would.%20The%20spread%20between%20areas%20that%20hold%20such%20households%20and%20areas%20that%20hold%20none%2C%20a%20large%20part%20of%20the%20true%20variance%2C%20is%20gone.%0A%0A%20%20%20%20**The%20tool.**%20Keep%20every%20row%20and%20set%20y%20to%200%20outside%20the%20domain%3A%20the%20domain's%20total%20is%20the%20total%20of%20y%C2%B71%5Bin%20the%20domain%5D%2C%20over%20the%20whole%20design.%20svy%20does%20that%20for%20you%20with%20%60where%3D%60%20(one%20domain)%20and%20%60by%3D%60%20(one%20row%20per%20level)%3B%20a%20domain's%20degrees%20of%20freedom%20count%20only%20the%20PSUs%20and%20strata%20that%20hold%20its%20members.%20Flip%20the%20switch%20to%20see%20what%20a%20filter%20does%20to%20the%20total%20of%20units%20of%20the%20households%20that%20run%20a%20large-scale%20unit%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20filter_first%20%3D%20mo.ui.switch(label%3D%22filter%20the%20rows%20first%2C%20then%20estimate%22%2C%20value%3DFalse)%0A%20%20%20%20filter_first%0A%20%20%20%20return%20(filter_first%2C)%0A%0A%0A%40app.cell%0Adef%20_(filter_first%2C%20frame%2C%20sample)%3A%0A%20%20%20%20%23%20The%20total%20of%20units%20of%20the%20households%20that%20run%20a%20large-scale%20unit%3A%20by%20where%3D%20on%20the%20full%20design%2C%20or%20on%20a%20filtered%20table%0A%20%20%20%20if%20filter_first.value%3A%0A%20%20%20%20%20%20%20%20_result%20%3D%20svy.Sample(data%3Dpl.from_pandas(frame%5Bframe%5B%22runs_large_unit%22%5D%20%3D%3D%201%5D)%2C%20design%3Dsurvey_design()).estimation.total(y%3D%22units%22)%0A%20%20%20%20else%3A%0A%20%20%20%20%20%20%20%20_result%20%3D%20sample.estimation.total(y%3D%22units%22%2C%20where%3Dsvy.col(%22runs_large_unit%22)%20%3D%3D%201)%0A%20%20%20%20_right%20%3D%20sample.estimation.total(y%3D%22units%22%2C%20where%3Dsvy.col(%22runs_large_unit%22)%20%3D%3D%201).to_polars()%5B%22se%22%5D%5B0%5D%0A%20%20%20%20_shown%20%3D%20_result.to_polars()%0A%20%20%20%20mo.vstack(%5B%0A%20%20%20%20%20%20%20%20_shown%2C%0A%20%20%20%20%20%20%20%20mo.md(f%22The%20estimate%20is%20the%20same%20either%20way.%20The%20SE%20is%20**%7B_shown%5B'se'%5D%5B0%5D%3A%2C.1f%7D**%20here%20and%20**%7B_right%3A%2C.1f%7D**%20with%20%60where%3D%60%20on%20the%20full%20design.%22%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%2B%20(f%22%20The%20filter%20kept%20%7Bframe.loc%5Bframe%5B'runs_large_unit'%5D%20%3D%3D%201%2C%20'psu_id'%5D.nunique()%7D%20of%20the%20survey's%20%7Bframe%5B'psu_id'%5D.nunique()%7D%20PSUs%2C%20and%20the%20SE%20fell%20to%20%7B_shown%5B'se'%5D%5B0%5D%20%2F%20_right%3A.0%25%7D%20of%20its%20value.%22%20if%20filter_first.value%20else%20%22%22))%2C%0A%20%20%20%20%5D)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20In%20R%20the%20same%20trap%20is%20%60subset()%60%20on%20the%20data%20before%20%60svydesign%60%3B%20%60subset()%60%20on%20the%20design%20object%2C%20or%20ReGenesees'%20%60by%3D%60%2C%20keeps%20the%20full%20design.%20In%20both%20languages%20the%20rule%20is%20the%20same%3A%20the%20design%20is%20declared%20on%20every%20row%2C%20and%20the%20domain%20is%20chosen%20afterwards.%0A%0A%20%20%20%20%23%23%23%20Exercise%206.4.%20A%20table%20by%20province%2C%20with%20svy%0A%0A%20%20%20%20**Why%20it%20matters%3A**%20this%20is%20the%20table%20a%20report%20prints%3A%20one%20row%20per%20province%2C%20the%20estimate%2C%20its%20SE%2C%20its%20CV%20as%20a%20percentage%2C%20and%20its%2095%25%20confidence%20interval.%20Module%207%20writes%20tables%20like%20it%20to%20a%20workbook.%0A%0A%20%20%20%20**Input%20%E2%86%92%20output%3A**%20a%20svy%20%60Sample%60%20whose%20data%20has%20a%20%60province%60%20column%2C%20and%20the%20name%20of%20a%20column%20%60y%60%20%E2%86%92%20a%20pandas%20DataFrame%2C%20one%20row%20per%20province%20in%20order%20of%20name%2C%20with%20the%20columns%3A%0A%0A%20%20%20%20%7C%20province%20%7C%20est%20%7C%20se%20%7C%20cv_pct%20%7C%20lci%20%7C%20uci%20%7C%0A%20%20%20%20%7C%3A--%7C--%3A%7C--%3A%7C--%3A%7C--%3A%7C--%3A%7C%0A%20%20%20%20%7C%20Batangas%20%7C%20%E2%80%A6%20%7C%20%E2%80%A6%20%7C%2029.9%20%7C%20%E2%80%A6%20%7C%20%E2%80%A6%20%7C%0A%0A%20%20%20%20%60cv_pct%60%20is%20the%20CV%20in%20percent%3A%20svy's%20%60cv%60%20is%20a%20fraction%2C%20so%200.299%20is%2029.9.%0A%0A%20%20%20%20**Tools%3A**%20%60sample.estimation.total(y%3D...%2C%20by%3D...)%60%2C%20%60.to_polars()%60%2C%20%60.to_pandas()%60%2C%20a%20new%20column%2C%20and%20the%20column%20list.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20province_totals(sample%2C%20y%3A%20str)%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22One%20row%20per%20province%3A%20the%20design-based%20total%20of%20y%2C%20its%20SE%2C%20its%20CV%20in%20percent%20and%20its%2095%25%20confidence%20interval.%22%22%22%0A%20%20%20%20%23%20Exercise%206.4%3A%20the%20task%20and%20examples%20are%20in%20the%20text%20above%20this%20cell.%0A%20%20%20%20%23%20Step%201%3A%20the%20total%20of%20y%20by%20province%3A%20sample.estimation.total(y%3Dy%2C%20by%3D%22province%22)%0A%20%20%20%20%23%20Step%202%3A%20to%20a%20pandas%20table%3A%20.to_polars().to_pandas()%0A%20%20%20%20%23%20Step%203%3A%20add%20cv_pct%2C%20the%20cv%20times%20100%0A%20%20%20%20%23%20Step%204%3A%20keep%20province%2C%20est%2C%20se%2C%20cv_pct%2C%20lci%2C%20uci%2C%20sorted%20by%20province%3B%20return%20it%0A%20%20%20%20raise%20NotImplementedError(%22Exercise%206.4%22)%0A%0A%0A%40app.function%0Adef%20small_sample()%3A%0A%20%20%20%20%22%22%22The%20small%20design%20of%20Exercise%206.3%20as%20a%20svy%20Sample%2C%20with%20two%20provinces%2C%20P%20and%20Q%2C%20spread%20over%20both%20strata.%22%22%22%0A%20%20%20%20return%20svy.Sample(data%3Dpl.from_pandas(small_design())%2C%20design%3Dsvy.Design(stratum%3D%22stratum_id%22%2C%20psu%3D%22psu_id%22%2C%20wgt%3D%22sel_final_weight%22%2C%20pop_size%3D%22psus_in_stratum%22%2C%20singleton%3D%22skip%22))%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(sample)%3A%0A%20%20%20%20check_cases(%0A%20%20%20%20%20%20%20%20%22Exercise%206.4%22%2C%20province_totals%2C%0A%20%20%20%20%20%20%20%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22the%20survey's%20large-scale%20operators%22%2C%20(sample%2C%20%22large_unit_operators%22))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22the%20survey's%20fishing%20units%22%2C%20(sample%2C%20%22units%22))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22the%20small%20design%2C%20two%20provinces%20across%20two%20strata%22%2C%20(small_sample()%2C%20%22y%22))%2C%0A%20%20%20%20%20%20%20%20%5D%2C%0A%20%20%20%20%20%20%20%20what%3D%22samples%20and%20columns%22%2C%20summarize%3Dlambda%20table%3A%20as_rows(table%2C%20%22province%22)%2C%0A%20%20%20%20%20%20%20%20hint%3D%22sample.estimation.total(y%3Dy%2C%20by%3D'province')%2C%20to%20pandas%3B%20cv_pct%20is%20cv%20*%20100%3B%20keep%20the%20six%20columns%2C%20sorted%20by%20province.%22%2C%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(sample)%3A%0A%20%20%20%20%23%20Try%20it%3A%20the%20large-scale%20operators%20of%20each%20province%0A%20%20%20%20try_it(province_totals%2C%20sample%2C%20%22large_unit_operators%22)%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%206.4%22%3A%20mo.md(r%22%22%22%0A%20%20%20%20%7C%20what%20you%20see%20%7C%20the%20usual%20cause%20%7C%20look%20at%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20%60cv_pct%60%20below%201%20%7C%20svy's%20fraction%20kept%20as%20it%20is.%20%7C%20%60result%5B%22cv%22%5D%20*%20100%60%20%7C%0A%20%20%20%20%7C%20One%20row%2C%20no%20province%20%7C%20%60by%3D%60%20left%20out%3A%20the%20total%20of%20all%20provinces.%20%7C%20%60by%3D%22province%22%60%20%7C%0A%20%20%20%20%7C%20*UnknownNameError%3A%20Estimate.to_pandas%20does%20not%20exist%20%5BUNKNOWN_NAME%5D*%20%7C%20A%20result%20is%20not%20a%20table%3B%20convert%20it%20first.%20%7C%20%60.to_polars().to_pandas()%60%20%7C%0A%20%20%20%20%7C%20Extra%20columns%20(%60df%60%2C%20%60n%60)%20%7C%20The%20column%20list%20was%20not%20applied.%20%7C%20%60%5B%5B%22province%22%2C%20%22est%22%2C%20%22se%22%2C%20%22cv_pct%22%2C%20%22lci%22%2C%20%22uci%22%5D%5D%60%20%7C%0A%20%20%20%20%7C%20Rows%20in%20another%20order%20%7C%20Not%20sorted.%20%7C%20%60.sort_values(%22province%22)%60%20%7C%0A%0A%20%20%20%20Still%20stuck%20after%20ten%20minutes%3F%20Ask%20your%20table%2C%20then%20raise%20your%20hand.%0A%20%20%20%20%22%22%22)%7D)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%20Every%20combination%20of%20the%20domains%0A%0A%20%20%20%20**The%20problem.**%20A%20survey's%20tables%20cut%20by%20two%2C%20three%20or%20four%20things%20at%20once%3A%20operators%20by%20scale%2C%20sex%2C%20age%20group%20and%20education%20(%60by%20%3D%20~scale%3Asex%3Aage_group%3Aeducation%60%20in%20a%20ReGenesees%20call)%2C%20units%20by%20region%2C%20province%20and%20species.%20svy's%20%60by%3D%60%20takes%20a%20list%20of%20columns%20and%20estimates%20every%20combination%20that%20holds%20a%20sampled%20unit%2C%20each%20as%20a%20domain%20of%20its%20own%20on%20the%20full%20design.%20A%20combination%20nobody%20in%20the%20sample%20fell%20into%20gets%20no%20row.%20A%20published%20table%20still%20needs%20it%3A%20its%20shape%20is%20fixed%20by%20its%20categories%2C%20the%20same%20every%20round%2C%20so%20that%20a%20reader%20can%20find%20a%20cell%20and%20a%20program%20can%20compare%20two%20rounds.%0A%0A%20%20%20%20**The%20data.**%20These%20tables%20count%20operators%2C%20not%20households%3A%20one%20row%20per%20operator%20(a%20member%20whose%20%60mem_role_operator%60%20is%201%2C%20as%20module%205%20found%20them)%2C%20with%20their%20sex%20and%20their%20scale%2C%20each%20operator%20carrying%20their%20household's%20design%20and%20weight.%20An%20operator's%20scale%20is%20that%20of%20the%20largest%20unit%20they%20run%3A%20Large-scale%20if%20one%20of%20their%20units%20is%20(%60unit_scale%60%202)%2C%20Small-scale%20if%20every%20one%20is%20small-scale.%20%60operator_table%60%20builds%20it%20from%20%60frame%60%2C%20so%20the%20copies%20stay%20out%20and%20the%20misfiled%20household%20stays%20in%20its%20area's%20stratum%2C%20and%20%60operator_sample%60%20binds%20it%20to%20the%20survey's%20design.%20The%20design%20was%20drawn%20on%20households%2C%20and%20every%20operator%20in%20a%20drawn%20household%20was%20interviewed%2C%20so%20an%20operator's%20PSU%20and%20weight%20are%20their%20household's.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20sex_label(codes%3A%20pd.Series)%20-%3E%20pd.Series%3A%0A%20%20%20%20%22%22%22Sex%20as%20a%20table's%20label%3A%20Male%2C%20Female%2C%20Invalid%20value%20for%20another%20code%2C%20No%20answer%20for%20none.%22%22%22%0A%20%20%20%20labels%20%3D%20codes.map(%7B1%3A%20%22Male%22%2C%202%3A%20%22Female%22%7D)%0A%20%20%20%20return%20labels.mask(codes.notna()%20%26%20labels.isna()%2C%20INVALID).mask(codes.isna()%2C%20NO_ANSWER).astype(%22string%22)%0A%0A%0A%40app.function%0Adef%20scale_label(codes%3A%20pd.Series)%20-%3E%20pd.Series%3A%0A%20%20%20%20%22%22%22The%20scale%20of%20an%20operator's%20largest%20unit%20as%20a%20table's%20label%3A%20Small-scale%2C%20Large-scale%2C%20Invalid%20value%20for%20another%0A%20%20%20%20code%2C%20No%20answer%20for%20an%20operator%20with%20no%20unit%20in%20the%20units%20table.%22%22%22%0A%20%20%20%20labels%20%3D%20codes.map(%7B1%3A%20%22Small-scale%22%2C%202%3A%20%22Large-scale%22%7D)%0A%20%20%20%20return%20labels.mask(codes.notna()%20%26%20labels.isna()%2C%20INVALID).mask(codes.isna()%2C%20NO_ANSWER).astype(%22string%22)%0A%0A%0A%40app.function%0Adef%20operator_table(frame%3A%20pd.DataFrame)%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22One%20row%20per%20operator%20of%20the%20frame's%20households%3A%20the%20operator's%20household%20and%20roster%20line%2C%20the%20province's%20name%2C%0A%20%20%20%20sex%20and%20scale%20as%20labels%2C%20the%20design%20columns%2C%20and%20three%20columns%20to%20estimate%20from%3A%20%60operators%60%20(1%20on%20every%20row)%2C%0A%20%20%20%20%60female%60%20(1%20for%20a%20woman%2C%200%20otherwise)%20and%20%60large_scale%60%20(1%20for%20an%20operator%20of%20a%20large-scale%20unit).%22%22%22%0A%20%20%20%20key%20%3D%20%5B%22region_code%22%2C%20%22hh_id%22%5D%0A%20%20%20%20members%20%3D%20course.members()%0A%20%20%20%20found%20%3D%20members%5Bmembers%5B%22mem_role_operator%22%5D.eq(1).fillna(False)%5D%0A%20%20%20%20%23%20The%20scale%20of%20the%20largest%20unit%20each%20operator%20runs%2C%20by%20their%20roster%20line%20(the%20units'%20operator_line)%0A%20%20%20%20units%20%3D%20course.units()%0A%20%20%20%20largest%20%3D%20units.groupby(%5B*key%2C%20%22operator_line%22%5D)%5B%22unit_scale%22%5D.max().rename(%22unit_scale%22).reset_index()%0A%20%20%20%20found%20%3D%20found.merge(largest.rename(columns%3D%7B%22operator_line%22%3A%20%22mem_line%22%7D)%2C%20on%3D%5B*key%2C%20%22mem_line%22%5D%2C%20how%3D%22left%22%2C%20validate%3D%22one_to_one%22)%0A%20%20%20%20columns%20%3D%20%5B*key%2C%20%22province%22%2C%20%22stratum_id%22%2C%20%22psu_id%22%2C%20%22psus_in_stratum%22%2C%20%22sel_final_weight%22%5D%0A%20%20%20%20table%20%3D%20found.merge(frame%5Bcolumns%5D%2C%20on%3Dkey%2C%20how%3D%22inner%22%2C%20validate%3D%22many_to_one%22)%20%20%23%20inner%3A%20a%20copy's%20operator%20leaves%20with%20it%0A%20%20%20%20table%20%3D%20table.assign(sex%3Dsex_label(table%5B%22mem_sex%22%5D)%2C%20scale%3Dscale_label(table%5B%22unit_scale%22%5D)%2C%20operators%3D1%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%20female%3D(table%5B%22mem_sex%22%5D%20%3D%3D%202).astype(%22int64%22)%2C%20large_scale%3D(table%5B%22unit_scale%22%5D%20%3D%3D%202).astype(%22int64%22))%0A%20%20%20%20keep%20%3D%20%5B*key%2C%20%22mem_line%22%2C%20%22province%22%2C%20%22sex%22%2C%20%22scale%22%2C%20%22operators%22%2C%20%22female%22%2C%20%22large_scale%22%2C%20%22stratum_id%22%2C%20%22psu_id%22%2C%20%22psus_in_stratum%22%2C%20%22sel_final_weight%22%5D%0A%20%20%20%20return%20table%5Bkeep%5D.reset_index(drop%3DTrue)%0A%0A%0A%40app.function%0Adef%20operator_sample(frame%3A%20pd.DataFrame)%20-%3E%20svy.Sample%3A%0A%20%20%20%20%22%22%22The%20operators%20as%20svy%20reads%20them%3A%20the%20households'%20design%2C%20each%20operator%20carrying%20its%20household's%20weight%20and%20PSU.%22%22%22%0A%20%20%20%20design%20%3D%20svy.Design(stratum%3D%22stratum_id%22%2C%20psu%3D%22psu_id%22%2C%20wgt%3D%22sel_final_weight%22%2C%20pop_size%3D%22psus_in_stratum%22%2C%20singleton%3D%22skip%22)%0A%20%20%20%20return%20svy.Sample(data%3Dpl.from_pandas(frame)%2C%20design%3Ddesign)%0A%0A%0A%40app.cell%0Adef%20_(frame)%3A%0A%20%20%20%20%23%20The%20operators%2C%20the%20copies%20left%20out%2C%20and%20the%20svy%20sample%20the%20next%20exercise%20estimates%20from%0A%20%20%20%20operators%20%3D%20operator_table(frame)%0A%20%20%20%20operators_sample%20%3D%20operator_sample(operators)%0A%20%20%20%20PROVINCES%20%3D%20sorted(operators%5B%22province%22%5D.unique())%0A%20%20%20%20mo.vstack(%5B%0A%20%20%20%20%20%20%20%20mo.md(f%22**%7Blen(operators)%3A%2C%7D%20operators**%20in%20%7Blen(PROVINCES)%7D%20provinces%3B%20svy%20sees%20%7Boperators_sample.n_strata%7D%20strata%20and%20%7Boperators_sample.n_psus%3A%2C%7D%20PSUs.%22)%2C%0A%20%20%20%20%20%20%20%20operators.head()%2C%0A%20%20%20%20%5D)%0A%20%20%20%20return%20PROVINCES%2C%20operators_sample%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%20Exercise%206.5.%20Every%20cell%20of%20a%20cross%20table%0A%0A%20%20%20%20**Why%20it%20matters%3A**%20ask%20svy%20for%20the%20operators%20by%20province%20and%20sex%2C%20and%20it%20returns%20one%20row%20per%20combination%20that%20holds%20a%20sampled%20operator.%20A%20province%20whose%20sample%20has%20no%20operator%20with%20an%20invalid%20sex%20code%20gets%20no%20row%20for%20it%3B%20if%20a%20region's%20database%20lost%20its%20women%20operators%2C%20their%20rows%20would%20vanish%20the%20same%20way.%20A%20cell%20nobody%20fell%20into%20is%20still%20a%20cell%3A%20its%20estimate%20is%200%20and%20it%20has%20no%20sampling%20error%20to%20report.%0A%0A%20%20%20%20**Input%20%E2%86%92%20output%3A**%20a%20svy%20sample%2C%20the%20name%20of%20a%20column%20%60y%60%20to%20total%2C%20and%20%60levels%60%2C%20a%20dictionary%20from%20each%20domain%20variable%20to%20its%20categories%20in%20order%20(one%20variable%2C%20two%2C%20or%20three)%20%E2%86%92%20one%20row%20per%20**combination%20of%20the%20categories**%2C%20in%20the%20order%20%60itertools.product%60%20would%20list%20them%2C%20with%20the%20columns%3A%20the%20domain%20variables%2C%20%60est%60%2C%20%60se%60%2C%20%60cv%60%20and%20%60n%60%3A%0A%0A%20%20%20%20-%20a%20combination%20svy%20returned%3A%20its%20%60est%60%2C%20%60se%60%2C%20%60cv%60%20and%20%60n%60%3B%0A%20%20%20%20-%20a%20combination%20svy%20did%20not%20return%3A%20%60est%60%200%2C%20%60se%60%200%2C%20%60cv%60%20missing%2C%20%60n%60%200.%0A%0A%20%20%20%20The%20checks'%20small%20table%20has%20nine%20operators%20in%20provinces%20P%20and%20Q%2C%20and%20none%20in%20R%20or%20with%20an%20invalid%20sex%20code%3B%20the%20accordion%20under%20this%20exercise%20shows%20it.%20Its%20first%20rows%3A%0A%0A%20%20%20%20%60%60%60python%0A%20%20%20%20domain_cells(operator_sample(small_operators())%2C%20%22operators%22%2C%20%7B%22province%22%3A%20%5B%22P%22%2C%20%22Q%22%2C%20%22R%22%5D%2C%20%22sex%22%3A%20%5B%22Male%22%2C%20%22Female%22%2C%20INVALID%5D%7D)%0A%20%20%20%20%60%60%60%0A%0A%20%20%20%20%7C%20province%20%7C%20sex%20%7C%20est%20%7C%20se%20%7C%20cv%20%7C%20n%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C--%3A%7C--%3A%7C--%3A%7C--%3A%7C%0A%20%20%20%20%7C%20P%20%7C%20Male%20%7C%209.0%20%7C%202.83%20%7C%200.314%20%7C%203%20%7C%0A%20%20%20%20%7C%20P%20%7C%20Female%20%7C%204.0%20%7C%202.83%20%7C%200.707%20%7C%201%20%7C%0A%20%20%20%20%7C%20P%20%7C%20Invalid%20value%20%7C%200.0%20%7C%200.0%20%7C%20*missing*%20%7C%200%20%7C%0A%20%20%20%20%7C%20Q%20%7C%20Male%20%7C%2013.0%20%7C%204.90%20%7C%200.377%20%7C%204%20%7C%0A%20%20%20%20%7C%20Q%20%7C%20Female%20%7C%201.0%20%7C%200.0%20%7C%200.000%20%7C%201%20%7C%0A%20%20%20%20%7C%20%E2%80%A6%20%7C%20%7C%20%7C%20%7C%20%7C%20%7C%0A%20%20%20%20%7C%20R%20%7C%20Invalid%20value%20%7C%200.0%20%7C%200.0%20%7C%20*missing*%20%7C%200%20%7C%0A%0A%20%20%20%20Q's%20woman%20is%20in%20the%20take-all%20stratum%20T%3A%20an%20estimate%20with%20no%20sampling%20error%2C%20a%20CV%20of%200.%0A%0A%20%20%20%20**Tools%3A**%20%60sample.estimation.total(y%3D...%2C%20by%3D%5B...%5D)%60%20and%20%60.to_polars().to_pandas()%60%20(Exercise%206.4)%2C%20%60itertools.product(*lists)%60%20for%20every%20combination%2C%20%60pd.DataFrame(...%2C%20columns%3D...)%60%2C%20%60merge(...%2C%20how%3D%22left%22%2C%20validate%3D...)%60%20(module%203)%2C%20and%20%60fillna%60.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20domain_cells(sample%3A%20svy.Sample%2C%20y%3A%20str%2C%20levels%3A%20dict)%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22One%20row%20per%20combination%20of%20the%20levels%2C%20in%20order%3A%20the%20total%20of%20y%2C%20its%20SE%2C%20CV%20and%20n%2C%20with%200%2C%200%2C%20missing%20and%200%0A%20%20%20%20where%20no%20sampled%20row%20falls%20in%20the%20combination.%22%22%22%0A%20%20%20%20%23%20Exercise%206.5%3A%20the%20task%20and%20examples%20are%20in%20the%20text%20above%20this%20cell.%0A%20%20%20%20%23%20Step%201%3A%20the%20totals%20by%20every%20domain%20variable%3A%20sample.estimation.total(y%3Dy%2C%20by%3Dlist(levels))%2C%20as%20pandas%0A%20%20%20%20%23%20Step%202%3A%20the%20grid%20of%20every%20combination%2C%20as%20a%20table%3A%20pd.DataFrame(itertools.product(*levels.values())%2C%20columns%3Dlist(levels))%0A%20%20%20%20%23%20Step%203%3A%20lay%20svy's%20rows%20on%20the%20grid%3A%20the%20grid%20merged%20with%20est%2C%20se%2C%20cv%20and%20n%20on%20the%20domain%20variables%2C%20how%3D%22left%22%0A%20%20%20%20%23%20Step%204%3A%20fill%20est%20and%20se%20with%200%20and%20n%20with%200%20(as%20whole%20numbers)%3B%20keep%20cv%20missing%3B%20return%20the%20table%0A%20%20%20%20raise%20NotImplementedError(%22Exercise%206.5%22)%0A%0A%0A%40app.function%0Adef%20small_operators()%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22Nine%20operators%20in%20two%20strata%2C%20used%20by%20the%20checks%3A%20a%20take-all%20stratum%20T%20of%20three%20households%2C%20each%20its%20own%20PSU%2C%20and%20a%0A%20%20%20%20stratum%20S%20of%203%20PSUs%20out%20of%206.%20Province%20P%20has%20a%20woman%2C%20Q%20has%20one%20in%20the%20take-all%20stratum%2C%20nobody's%20sex%20code%20is%0A%20%20%20%20invalid%2C%20and%20province%20R%20has%20no%20operator%20at%20all.%22%22%22%0A%20%20%20%20rows%20%3D%20%5B%0A%20%20%20%20%20%20%20%20%23%20stratum_id%2C%20psu_id%2C%20psus_in_stratum%2C%20sel_final_weight%2C%20province%2C%20sex%2C%20scale%0A%20%20%20%20%20%20%20%20(%22T%22%2C%20%22T-h1%22%2C%203%2C%201.0%2C%20%22P%22%2C%20%22Male%22%2C%20%22Large-scale%22)%2C%0A%20%20%20%20%20%20%20%20(%22T%22%2C%20%22T-h2%22%2C%203%2C%201.0%2C%20%22Q%22%2C%20%22Female%22%2C%20%22Large-scale%22)%2C%0A%20%20%20%20%20%20%20%20(%22T%22%2C%20%22T-h3%22%2C%203%2C%201.0%2C%20%22Q%22%2C%20%22Male%22%2C%20%22Large-scale%22)%2C%0A%20%20%20%20%20%20%20%20(%22S%22%2C%20%22s1%22%2C%206%2C%204.0%2C%20%22P%22%2C%20%22Male%22%2C%20%22Small-scale%22)%2C%0A%20%20%20%20%20%20%20%20(%22S%22%2C%20%22s1%22%2C%206%2C%204.0%2C%20%22P%22%2C%20%22Female%22%2C%20%22Small-scale%22)%2C%0A%20%20%20%20%20%20%20%20(%22S%22%2C%20%22s2%22%2C%206%2C%204.0%2C%20%22Q%22%2C%20%22Male%22%2C%20%22Small-scale%22)%2C%0A%20%20%20%20%20%20%20%20(%22S%22%2C%20%22s3%22%2C%206%2C%204.0%2C%20%22P%22%2C%20%22Male%22%2C%20%22Small-scale%22)%2C%0A%20%20%20%20%20%20%20%20(%22S%22%2C%20%22s3%22%2C%206%2C%204.0%2C%20%22Q%22%2C%20%22Male%22%2C%20%22Large-scale%22)%2C%0A%20%20%20%20%20%20%20%20(%22S%22%2C%20%22s3%22%2C%206%2C%204.0%2C%20%22Q%22%2C%20%22Male%22%2C%20%22Small-scale%22)%2C%0A%20%20%20%20%5D%0A%20%20%20%20table%20%3D%20pd.DataFrame(rows%2C%20columns%3D%5B%22stratum_id%22%2C%20%22psu_id%22%2C%20%22psus_in_stratum%22%2C%20%22sel_final_weight%22%2C%20%22province%22%2C%20%22sex%22%2C%20%22scale%22%5D)%0A%20%20%20%20return%20table.assign(operators%3D1%2C%20female%3D(table%5B%22sex%22%5D%20%3D%3D%20%22Female%22).astype(%22int64%22)%2C%20large_scale%3D(table%5B%22scale%22%5D%20%3D%3D%20%22Large-scale%22).astype(%22int64%22))%0A%0A%0A%40app.function%0Adef%20small_levels()%20-%3E%20dict%3A%0A%20%20%20%20%22%22%22The%20small%20table's%20categories%3A%20province%20R%20and%20the%20sex%20Invalid%20value%20have%20nobody%20in%20them.%22%22%22%0A%20%20%20%20return%20%7B%22province%22%3A%20%5B%22P%22%2C%20%22Q%22%2C%20%22R%22%5D%2C%20%22sex%22%3A%20%5B%22Male%22%2C%20%22Female%22%2C%20INVALID%5D%7D%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.accordion(%7B%22The%20checks'%20small%20table%3A%20nine%20operators%20in%20two%20strata%22%3A%20mo.vstack(%5B%0A%20%20%20%20%20%20%20%20mo.md(%22Stratum%20T%20takes%20all%20three%20of%20its%20households%20(each%20its%20own%20PSU%2C%20weight%201)%3B%20stratum%20S%20drew%203%20of%20its%206%20areas%20(weight%204).%20P's%20woman%20shares%20PSU%20%60s1%60%20with%20a%20man%3B%20Q's%20woman%20is%20in%20stratum%20T.%20Province%20R%20and%20the%20sex%20Invalid%20value%20have%20nobody%20in%20them%2C%20so%20every%20cell%20of%20theirs%20is%20empty.%22)%2C%0A%20%20%20%20%20%20%20%20small_operators()%2C%0A%20%20%20%20%5D)%7D)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20cell_rows(result%3A%20pd.DataFrame%2C%20digits%3A%20int%20%3D%204)%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22A%20table%20of%20cells%20in%20one%20form%20for%20the%20checks%3A%20the%20domain%20variables%20(every%20column%20that%20is%20not%20a%20figure)%20as%20text%2C%0A%20%20%20%20then%20est%2C%20se%2C%20cv%20and%20n%2C%20rounded%2C%20in%20the%20result's%20own%20order%20(the%20order%20is%20part%20of%20the%20answer).%22%22%22%0A%20%20%20%20table%20%3D%20result.reset_index()%20if%20any(name%20is%20not%20None%20for%20name%20in%20result.index.names)%20else%20result%0A%20%20%20%20keys%20%3D%20%5Bc%20for%20c%20in%20table.columns%20if%20c%20not%20in%20(%22est%22%2C%20%22se%22%2C%20%22cv%22%2C%20%22n%22%2C%20%22lci%22%2C%20%22uci%22%2C%20%22df%22)%5D%0A%20%20%20%20out%20%3D%20table%5B%5B*keys%2C%20%22est%22%2C%20%22se%22%2C%20%22cv%22%2C%20%22n%22%5D%5D.reset_index(drop%3DTrue).copy()%0A%20%20%20%20out%5Bkeys%5D%20%3D%20out%5Bkeys%5D.astype(str)%0A%20%20%20%20out%5B%5B%22est%22%2C%20%22se%22%2C%20%22cv%22%5D%5D%20%3D%20out%5B%5B%22est%22%2C%20%22se%22%2C%20%22cv%22%5D%5D.astype(float).round(digits)%0A%20%20%20%20out%5B%22n%22%5D%20%3D%20out%5B%22n%22%5D.astype(%22int64%22)%0A%20%20%20%20return%20out%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(PROVINCES%2C%20operators_sample)%3A%0A%20%20%20%20check_cases(%0A%20%20%20%20%20%20%20%20%22Exercise%206.5%22%2C%20domain_cells%2C%0A%20%20%20%20%20%20%20%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22nine%20operators%3A%20a%20province%20and%20a%20sex%20with%20nobody%20in%20them%22%2C%20(operator_sample(small_operators())%2C%20%22operators%22%2C%20small_levels()))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22the%20survey's%20operators%20by%20province%20and%20sex%22%2C%20(operators_sample%2C%20%22operators%22%2C%20%7B%22province%22%3A%20PROVINCES%2C%20%22sex%22%3A%20SEXES%7D))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22the%20survey's%20operators%20by%20province%2C%20scale%20and%20sex%22%2C%20(operators_sample%2C%20%22operators%22%2C%20%7B%22province%22%3A%20PROVINCES%2C%20%22scale%22%3A%20SCALES%2C%20%22sex%22%3A%20SEXES%7D))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22the%20survey's%20women%20operators%20by%20scale%22%2C%20(operators_sample%2C%20%22female%22%2C%20%7B%22scale%22%3A%20SCALES%7D))%2C%0A%20%20%20%20%20%20%20%20%5D%2C%0A%20%20%20%20%20%20%20%20what%3D%22tables%22%2C%20summarize%3Dcell_rows%2C%0A%20%20%20%20%20%20%20%20hint%3D%22Estimate%20with%20by%3Dlist(levels)%3B%20merge%20svy's%20rows%20onto%20the%20grid%20of%20itertools.product(*levels.values())%2C%20how%3D'left'%3B%20an%20empty%20cell%20is%20est%200%2C%20se%200%2C%20cv%20missing%2C%20n%200.%22%2C%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(PROVINCES%2C%20operators_sample)%3A%0A%20%20%20%20%23%20Try%20it%3A%20the%20operators%20by%20province%20and%20sex%2C%20every%20cell%0A%20%20%20%20try_it(domain_cells%2C%20operators_sample%2C%20%22operators%22%2C%20%7B%22province%22%3A%20PROVINCES%2C%20%22sex%22%3A%20SEXES%7D)%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%206.5%22%3A%20mo.md(r%22%22%22%0A%20%20%20%20%7C%20what%20you%20see%20%7C%20the%20usual%20cause%20%7C%20look%20at%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20Fewer%20rows%20than%20combinations%20%7C%20svy's%20result%20returned%20as%20it%20is%3A%20it%20has%20no%20row%20for%20an%20empty%20cell.%20%7C%20the%20grid%2C%20merged%20%60how%3D%22left%22%60%20%7C%0A%20%20%20%20%7C%20The%20right%20rows%20in%20another%20order%20%7C%20svy%20sorts%20its%20result%20by%20text%20(%60Female%60%20before%20%60Male%60)%3A%20the%20grid%20was%20merged%20onto%20svy's%20rows%20instead%20of%20the%20other%20way%20round%2C%20or%20built%20from%20the%20data's%20categories.%20%7C%20%60grid.merge(found%2C%20...)%60%2C%20the%20grid%20from%20%60levels%60%20%7C%0A%20%20%20%20%7C%20%60est%60%20missing%20in%20empty%20cells%2C%20or%20%60n%60%20a%20decimal%20%7C%20The%20grid's%20empty%20rows%20left%20as%20NaN%2C%20or%20%60n%60%20filled%20but%20not%20turned%20back%20into%20whole%20numbers.%20%7C%20%60fillna(0.0)%60%3B%20%60fillna(0).astype(%22int64%22)%60%20%7C%0A%20%20%20%20%7C%20The%20SEs%20are%20smaller%20than%20svy's%20%7C%20The%20rows%20were%20filtered%20to%20each%20domain%20before%20estimating.%20%7C%20%60by%3D%60%20on%20the%20full%20sample%20%7C%0A%20%20%20%20%7C%20*KeyError*%20naming%20a%20domain%20variable%20%7C%20%60by%60%20was%20given%20the%20dictionary's%20values%2C%20or%20one%20name%20as%20text%20where%20a%20list%20is%20needed.%20%7C%20%60by%3Dlist(levels)%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_(PROVINCES%2C%20operators_sample)%3A%0A%20%20%20%20%23%20How%20the%20cells%20thin%20as%20the%20table%20goes%20from%20one%20variable%20to%20three%0A%20%20%20%20def%20_depth()%3A%0A%20%20%20%20%20%20%20%20cuts%20%3D%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%22province%22%3A%20%7B%22province%22%3A%20PROVINCES%7D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22province%20%C3%97%20sex%22%3A%20%7B%22province%22%3A%20PROVINCES%2C%20%22sex%22%3A%20SEXES%5B%3A2%5D%7D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22province%20%C3%97%20scale%20%C3%97%20sex%22%3A%20%7B%22province%22%3A%20PROVINCES%2C%20%22scale%22%3A%20SCALES%5B%3A2%5D%2C%20%22sex%22%3A%20SEXES%5B%3A2%5D%7D%2C%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20rows%20%3D%20%7B%7D%0A%20%20%20%20%20%20%20%20for%20name%2C%20levels%20in%20cuts.items()%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20cells%20%3D%20attempt(domain_cells%2C%20operators_sample%2C%20%22operators%22%2C%20levels)%0A%20%20%20%20%20%20%20%20%20%20%20%20if%20cells%20is%20NOT_YET%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20return%20waiting_for(%22Exercise%206.5%22)%0A%20%20%20%20%20%20%20%20%20%20%20%20rows%5Bname%5D%20%3D%20%7B%22cells%22%3A%20len(cells)%2C%20%22no%20operator%20sampled%22%3A%20int((cells%5B%22n%22%5D%20%3D%3D%200).sum())%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%20%22fewer%20than%2010%20sampled%22%3A%20int(cells%5B%22n%22%5D.between(1%2C%209).sum())%2C%20%22median%20CV%22%3A%20cells%5B%22cv%22%5D.median()%7D%0A%20%20%20%20%20%20%20%20table%20%3D%20pd.DataFrame(rows).T.rename_axis(%22cells%20by%22)%0A%20%20%20%20%20%20%20%20three%20%3D%20table.loc%5B%22province%20%C3%97%20scale%20%C3%97%20sex%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%20table.round(3)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20mo.md(f%22Each%20cut%20divides%20the%20sample%20again.%20By%20province%2C%20scale%20and%20sex%2C%20with%20the%20catch-all%20categories%20left%20out%2C%20**%7Bint(three%5B'no%20operator%20sampled'%5D)%7D%20of%20%7Bint(three%5B'cells'%5D)%7D%20cells**%20hold%20no%20sampled%20operator%20and%20%7Bint(three%5B'fewer%20than%2010%20sampled'%5D)%7D%20hold%20fewer%20than%20ten%2C%20and%20the%20median%20cell's%20CV%20is%20%7Bthree%5B'median%20CV'%5D%3A.0%25%7D.%20Which%20of%20these%20cells%20a%20table%20can%20publish%20is%20a%20rule%20on%20the%20CV%3B%20the%20stretch%20S2%2C%20%5Btabulation%5D(%2F%3Ffile%3D61_stretch_tabulation.py)%2C%20writes%20one%2C%20with%20the%20margins%2C%20the%20intervals%20and%20the%20layout%20of%20a%20cross%20table.%22)%2C%0A%20%20%20%20%20%20%20%20%5D)%0A%0A%20%20%20%20_depth()%0A%20%20%20%20return%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%20%60%60%60r%0A%20%20%20%20svyby(~operators%2C%20~province%20%2B%20sex%2C%20design%2C%20svytotal%2C%20vartype%20%3D%20c(%22se%22%2C%20%22cv%22))%0A%20%20%20%20%60%60%60%0A%0A%20%20%20%20%60svyby%60%20also%20returns%20one%20row%20per%20combination%20it%20finds%3B%20its%20default%20%60drop.empty.groups%20%3D%20TRUE%60%20leaves%20out%20the%20rest.%20In%20ReGenesees%20you%20write%20%60svystatTM(design%2C%20~operators%2C%20by%20%3D%20~province%3Asex)%60%20for%20the%20same%20domains.%20The%20grid%20of%20every%20category%20is%20the%20table's%20definition%20in%20either%20language%2C%20applied%20after%20the%20estimate.%20In%20R%2C%20%60expand.grid(levels)%60%20builds%20it%20and%20a%20%60merge(...%2C%20all.x%20%3D%20TRUE)%60%20lays%20the%20estimates%20on%20it.%0A%20%20%20%20%22%22%22)%7D)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%20Exercise%206.6.%20A%20domain%20mean%2C%20from%20the%20formula%0A%0A%20%20%20%20**Why%20it%20matters%3A**%20%22how%20many%20members%20does%20a%20household%20that%20runs%20a%20large-scale%20unit%20have%2C%20on%20average%3F%22%20is%20a%20mean%20over%20a%20domain%2C%20and%20the%20domain%20is%20the%20trap%3A%20the%20right%20answer%20keeps%20every%20row%20and%20lets%20the%20households%20outside%20the%20domain%20count%20as%20zeros%20in%20the%20linearized%20variable.%20Written%20once%2C%20it%20is%20how%20svy's%20%60mean(...%2C%20where%3D...)%60%20works.%0A%0A%20%20%20%20**Input%20%E2%86%92%20output%3A**%20a%20frame%2C%20the%20name%20of%20a%20column%20%60y%60%2C%20and%20a%20boolean%20Series%20%60domain%60%20(one%20value%20per%20row%2C%20True%20in%20the%20domain)%20%E2%86%92%20a%20Series%20labeled%20%60est%60%20and%20%60se%60%3A%0A%0A%20%20%20%20%60%60%60%0A%20%20%20%201%5Bd%5D%20%20%201%20for%20a%20row%20in%20the%20domain%2C%200%20outside%0A%20%20%20%20N%CC%82_d%20%20%20%3D%20%CE%A3%20w%20%C2%B7%201%5Bd%5D%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20the%20households%20the%20domain%20stands%20for%0A%20%20%20%20m_d%20%20%20%20%3D%20%CE%A3%20w%20%C2%B7%201%5Bd%5D%20%C2%B7%20y%20%2F%20N%CC%82_d%20%20%20%20%20%20%20%20%20%20the%20domain%20mean%0A%20%20%20%20z%20%20%20%20%20%20%3D%20w%20%C2%B7%201%5Bd%5D%20%C2%B7%20(y%20%E2%88%92%20m_d)%20%2F%20N%CC%82_d%20%20%20%200%20for%20every%20row%20outside%20the%20domain%0A%20%20%20%20SE%20%20%20%20%20%3D%20%E2%88%9A%20taylor_variance(frame%2C%20z)%0A%20%20%20%20%60%60%60%0A%0A%20%20%20%20**Tools%3A**%20the%20formula%20above%2C%20with%20%60taylor_variance%60%20(shown%20above).%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20domain_mean(frame%3A%20pd.DataFrame%2C%20y%3A%20str%2C%20domain%3A%20pd.Series)%20-%3E%20pd.Series%3A%0A%20%20%20%20%22%22%22The%20design-based%20mean%20of%20y%20over%20the%20rows%20where%20domain%20is%20True%2C%20and%20its%20SE%2C%20on%20the%20full%20design%20(est%2C%20se).%22%22%22%0A%20%20%20%20%23%20Exercise%206.6%3A%20the%20task%20and%20examples%20are%20in%20the%20text%20above%20this%20cell.%0A%20%20%20%20%23%20Step%201%3A%20d%20%3D%20the%20domain%20as%200%2F1%20numbers%3B%20w%20%3D%20the%20weights%0A%20%20%20%20%23%20Step%202%3A%20N_d%20%3D%20the%20sum%20of%20w%C2%B7d%3B%20m%20%3D%20the%20sum%20of%20w%C2%B7d%C2%B7y%20over%20N_d%0A%20%20%20%20%23%20Step%203%3A%20z%20%3D%20w%C2%B7d%C2%B7(y%20%E2%88%92%20m)%20%2F%20N_d%2C%20a%20value%20on%20every%20row%20(0%20outside%20the%20domain)%0A%20%20%20%20%23%20Step%204%3A%20se%20%3D%20taylor_variance(frame%2C%20z)%20**%200.5%3B%20return%20pd.Series(%7B%22est%22%3A%20m%2C%20%22se%22%3A%20se%7D)%0A%20%20%20%20raise%20NotImplementedError(%22Exercise%206.6%22)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(frame)%3A%0A%20%20%20%20_small%20%3D%20small_design()%0A%20%20%20%20check_cases(%0A%20%20%20%20%20%20%20%20%22Exercise%206.6%22%2C%20domain_mean%2C%0A%20%20%20%20%20%20%20%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22the%20small%20design%2C%20domain%20d%3A%20PSU%20s2%20has%20no%20member%20of%20it%22%2C%20(_small%2C%20%22y%22%2C%20_small%5B%22d%22%5D))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22the%20survey's%20household%20size%20of%20the%20households%20that%20run%20a%20large-scale%20unit%22%2C%20(frame%2C%20%22hh_size%22%2C%20frame%5B%22runs_large_unit%22%5D%20%3D%3D%201))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22the%20survey's%20boats%20of%20the%20households%20of%20Cebu%22%2C%20(frame%2C%20%22n_boats%22%2C%20frame%5B%22province%22%5D%20%3D%3D%20%22Cebu%22))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22the%20survey's%20units%20of%20households%20with%20five%20members%20or%20more%22%2C%20(frame%2C%20%22units%22%2C%20frame%5B%22hh_size%22%5D%20%3E%3D%205))%2C%0A%20%20%20%20%20%20%20%20%5D%2C%0A%20%20%20%20%20%20%20%20what%3D%22frames%20and%20domains%22%2C%20summarize%3Dlambda%20result%3A%20as_numbers(result%2C%20%5B%22est%22%2C%20%22se%22%5D)%2C%0A%20%20%20%20%20%20%20%20hint%3D%22Keep%20every%20row%3A%20z%20%3D%20w%C2%B7d%C2%B7(y%20%E2%88%92%20m)%20%2F%20N_d%20on%20all%20of%20them%2C%20then%20taylor_variance(frame%2C%20z)%20on%20the%20whole%20frame.%22%2C%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(frame)%3A%0A%20%20%20%20%23%20Try%20it%3A%20the%20mean%20size%20of%20a%20household%20that%20runs%20a%20large-scale%20unit%0A%20%20%20%20try_it(domain_mean%2C%20frame%2C%20%22hh_size%22%2C%20frame%5B%22runs_large_unit%22%5D%20%3D%3D%201)%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%206.6%22%3A%20mo.md(r%22%22%22%0A%20%20%20%20%7C%20what%20you%20see%20%7C%20the%20usual%20cause%20%7C%20look%20at%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20%60est%60%20right%2C%20%60se%60%20too%20small%20%7C%20The%20frame%20filtered%20to%20the%20domain%20before%20the%20variance%3A%20the%20PSUs%20with%20no%20member%20are%20gone.%20%7C%20%60z%60%20on%20every%20row%2C%20%60taylor_variance(frame%2C%20z)%60%20%7C%0A%20%20%20%20%7C%20%60est%60%20too%20small%20%7C%20Divided%20by%20all%20households'%20weights%2C%20not%20the%20domain's.%20%7C%20%60N_d%20%3D%20(w%20*%20d).sum()%60%20%7C%0A%20%20%20%20%7C%20%60se%60%20many%20times%20too%20large%20%7C%20z%20left%20as%20w%C2%B7d%C2%B7y%2C%20the%20domain%20total's%20variable%2C%20not%20the%20mean's.%20%7C%20%60(y%20%E2%88%92%20m)%20%2F%20N_d%60%20%7C%0A%20%20%20%20%7C%20*TypeError*%20on%20the%20domain%20%7C%20The%20True%2FFalse%20Series%20used%20in%20arithmetic%20as%20it%20is.%20%7C%20%60np.asarray(domain%2C%20dtype%3Dfloat)%60%20%7C%0A%0A%20%20%20%20Still%20stuck%20after%20ten%20minutes%3F%20Ask%20your%20table%2C%20then%20raise%20your%20hand.%0A%20%20%20%20%22%22%22)%7D)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%203%20%C2%B7%20Weighted%20and%20unweighted%0A%0A%20%20%20%20**The%20problem.**%20The%20sample%20over-represents%20the%20large%20units%20on%20purpose%3A%20stratum%20F%20takes%20all%20of%20them%2C%20the%20area%20strata%20one%20area%20in%202.5%2C%20in%205%20or%20in%2015.%20A%20count%20of%20the%20sample's%20rows%20describes%20the%20sample.%20The%20table%20below%20sets%20the%20unweighted%20figures%20beside%20the%20weighted%20ones.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(frame)%3A%0A%20%20%20%20%23%20The%20sample's%20figures%20and%20the%20population's%3A%20unweighted%20means%20of%20the%20rows%2C%20and%20weighted%20means%0A%20%20%20%20_w%20%3D%20frame%5B%22sel_final_weight%22%5D%0A%20%20%20%20_rows%20%3D%20%7B%0A%20%20%20%20%20%20%20%20%22share%20of%20households%20running%20a%20large-scale%20unit%22%3A%20%22runs_large_unit%22%2C%0A%20%20%20%20%20%20%20%20%22large-scale%20units%20per%20household%22%3A%20%22large_units%22%2C%0A%20%20%20%20%20%20%20%20%22boats%20per%20household%22%3A%20%22n_boats%22%2C%0A%20%20%20%20%20%20%20%20%22members%20per%20household%22%3A%20%22hh_size%22%2C%0A%20%20%20%20%7D%0A%20%20%20%20_table%20%3D%20pd.DataFrame(%0A%20%20%20%20%20%20%20%20%5B(_label%2C%20frame%5B_col%5D.mean()%2C%20(frame%5B_col%5D%20*%20_w).sum()%20%2F%20_w.sum())%20for%20_label%2C%20_col%20in%20_rows.items()%5D%2C%0A%20%20%20%20%20%20%20%20columns%3D%5B%22%22%2C%20%22unweighted%3A%20the%20sample%22%2C%20%22weighted%3A%20the%20households%20it%20stands%20for%22%5D%2C%0A%20%20%20%20)%0A%20%20%20%20_in_f%20%3D%20(frame%5B%22sel_stratum%22%5D%20%3D%3D%20%22F%22).mean()%0A%20%20%20%20_f_weight%20%3D%20_w%5Bframe%5B%22sel_stratum%22%5D%20%3D%3D%20%22F%22%5D.sum()%20%2F%20_w.sum()%0A%20%20%20%20mo.vstack(%5B%0A%20%20%20%20%20%20%20%20_table.round(4)%2C%0A%20%20%20%20%20%20%20%20mo.md(f%22The%20share%20of%20households%20running%20a%20large-scale%20unit%20is%20the%20gap%20to%20see.%20Stratum%20F's%20households%20are%20%7B_in_f%3A.1%25%7D%20of%20the%20sample's%20rows%20and%2C%20at%20a%20weight%20of%20about%20one%2C%20%7B_f_weight%3A.1%25%7D%20of%20the%20households%20they%20stand%20for.%20The%20unweighted%20share%20describes%20the%20survey's%20list%20of%20interviews%3B%20the%20weighted%20share%20describes%20the%20fishing%20households%20of%20the%20six%20provinces.%20The%20members%20per%20household%20barely%20move%2C%20because%20household%20size%20is%20about%20the%20same%20in%20every%20stratum.%20Weighting%20changes%20an%20estimate%20when%20the%20strata%20with%20different%20weights%20also%20differ%20in%20what%20you%20measure.%22)%2C%0A%20%20%20%20%5D)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%20The%20sample's%20own%20counts%0A%0A%20%20%20%20**The%20problem.**%20A%20weighted%20figure%20estimates%20the%20population%3A%20how%20many%20fishing%20households%20a%20province%20has.%20A%20reader%20of%20a%20table%20needs%20a%20second%20figure%20beside%20it%3A%20how%20many%20interviews%20the%20estimate%20rests%20on%2C%20to%20judge%20a%20small%20cell.%20That%20figure%20is%20the%20sample's%20own%20count%2C%20and%20pandas%20gives%20it%20in%20one%20line%3A%20%60frame.groupby(%22province%22)%5B%22hh_count%22%5D.sum()%60%2C%20or%20%60.size()%60%20for%20the%20rows.%20It%20has%20no%20sampling%20error%20to%20report%2C%20because%20it%20is%20not%20an%20estimate%20of%20anything%3A%20it%20is%20what%20the%20sample%20holds.%0A%0A%20%20%20%20**An%20option%20you%20may%20meet%20in%20R.**%20Some%20R%20code%20makes%20the%20sample's%20counts%20with%20a%20second%20design%20declared%20on%20the%20same%20rows%2C%20whose%20every%20weight%20is%201.%20In%20ReGenesees%20you%20write%3A%0A%0A%20%20%20%20%60%60%60r%0A%20%20%20%20hh%24w_one%20%3C-%201%0A%20%20%20%20options(RG.lonely.psu%20%3D%20%22remove%22)%20%20%20%23%20or%20%22adjust%22%20or%20%22average%22%3A%20with%20the%20default%2C%20%22fail%22%2C%20the%20call%20below%20stops%20with%20an%20error%0A%20%20%20%20des_one%20%3C-%20e.svydesign(data%20%3D%20hh%2C%20ids%20%3D%20~w_one%2C%20weights%20%3D%20~w_one)%0A%20%20%20%20svystatTM(des_one%2C%20~hh_count%2C%20vartype%20%3D%20c(%22se%22%2C%20%22cv%22))%0A%20%20%20%20%60%60%60%0A%0A%20%20%20%20The%20column%20of%20ones%20is%20both%20the%20weight%20and%20the%20cluster%20id%2C%20and%20the%20design%20declares%20no%20strata%20and%20no%20fpc.%20As%20the%20weight%2C%20it%20makes%20every%20total%20a%20count%20of%20the%20sample.%20As%20the%20cluster%20id%2C%20it%20makes%20the%20whole%20sample%20**one%20PSU**%2C%20a%20lonely%20PSU%2C%20and%20section%202's%20formula%20has%20no%20second%20PSU%20to%20measure%20a%20spread%20from.%20So%20the%20design%20works%20only%20with%20a%20lonely-PSU%20setting%20other%20than%20ReGenesees'%20default%3A%20the%20course%20ran%20it%20in%20R%20on%20this%20survey's%20data%20(R's%20survey%20package%204.4-2%2C%20ReGenesees%202.5)%2C%20and%20under%20%60%22fail%22%60%20ReGenesees%20stops%20with%20an%20error%2C%20while%20under%20%60%22remove%22%60%2C%20%60%22adjust%22%60%20or%20%60%22average%22%60%20it%20returns%20the%20sample's%20sums%20with%20an%20SE%20of%200.%20R's%20survey%20package%20refuses%20the%20same%20design%20in%20%60svydesign%60.%20Where%20it%20runs%2C%20every%20figure%20gets%20an%20SE%20of%200%20and%20a%20CV%20of%200%2C%20and%20with%20PSUs%20minus%20strata%20at%201%20%E2%88%92%201%20%3D%200%20degrees%20of%20freedom%2C%20no%20confidence%20interval.%20Its%20point%20is%20that%20one%20call%20then%20makes%20a%20weighted%20and%20an%20unweighted%20table%20of%20the%20same%20shape.%20svy%20declares%20it%20in%20the%20same%20words%3A%0A%0A%20%20%20%20%60%60%60python%0A%20%20%20%20rows%20%3D%20frame.assign(w_one%3D1)%0A%20%20%20%20one_weight%20%3D%20svy.Design(psu%3D%22w_one%22%2C%20wgt%3D%22w_one%22)%0A%20%20%20%20%60%60%60%0A%0A%20%20%20%20The%20cell%20below%20sets%20the%20pandas%20count%20beside%20that%20design%20and%20two%20other%20ways%20of%20writing%20a%20design%20with%20weight%201%2C%20all%20on%20the%20survey's%20household%20members.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(frame)%3A%0A%20%20%20%20%23%20The%20households'%20members%20counted%20four%20ways%3A%20pandas%2C%20a%20design%20whose%20every%20weight%20is%201%20and%20one%20PSU%2C%20and%20two%20other%0A%20%20%20%20%23%20designs%20with%20weight%201%0A%20%20%20%20_rows%20%3D%20pl.from_pandas(frame.assign(w_one%3D1))%0A%20%20%20%20_designs%20%3D%20%7B%0A%20%20%20%20%20%20%20%20%22weight%201%2C%20one%20PSU%2C%20no%20fpc%20(ids%20%3D%20~w_one%2C%20weights%20%3D%20~w_one)%22%3A%20svy.Design(psu%3D%22w_one%22%2C%20wgt%3D%22w_one%22)%2C%0A%20%20%20%20%20%20%20%20%22weight%201%2C%20no%20cluster%20id%3A%20every%20household%20its%20own%20PSU%22%3A%20svy.Design(wgt%3D%22w_one%22)%2C%0A%20%20%20%20%20%20%20%20%22weight%201%2C%20the%20survey's%20strata%2C%20PSUs%20and%20fpc%20kept%22%3A%20survey_design(%22w_one%22)%2C%0A%20%20%20%20%7D%0A%20%20%20%20_shown%20%3D%20%5B%22est%22%2C%20%22se%22%2C%20%22cv%22%2C%20%22df%22%2C%20%22lci%22%2C%20%22uci%22%2C%20%22n%22%5D%0A%20%20%20%20_table%20%3D%20pd.DataFrame(%0A%20%20%20%20%20%20%20%20%5B%7B%22how%22%3A%20%22pandas%3A%20frame%5B'hh_size'%5D.sum()%22%2C%20%22est%22%3A%20float(frame%5B%22hh_size%22%5D.sum())%2C%20%22n%22%3A%20len(frame)%7D%5D%0A%20%20%20%20%20%20%20%20%2B%20%5B%7B%22how%22%3A%20_label%2C%20**svy.Sample(data%3D_rows%2C%20design%3D_design).estimation.total(y%3D%22hh_size%22).to_polars().select(_shown).to_dicts()%5B0%5D%7D%0A%20%20%20%20%20%20%20%20%20%20%20for%20_label%2C%20_design%20in%20_designs.items()%5D%0A%20%20%20%20)%0A%20%20%20%20_table.round(4)%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%20All%20four%20give%20the%20sample's%20sum.%20Only%20the%20one-PSU%20design%20gives%20an%20SE%20of%200.%20Without%20a%20cluster%20id%2C%20svy%20takes%20each%20household%20as%20a%20PSU%20drawn%20at%20random%20with%20replacement%2C%20and%20the%20SE%20is%20that%20of%20a%20simple%20random%20sample%20of%20households.%20With%20the%20strata%2C%20PSUs%20and%20fpc%20kept%2C%20it%20is%20the%20survey%20design's%20SE%20for%20a%20weight%20of%201.%20Both%20put%20a%20number%20in%20the%20SE%20column%20that%20describes%20no%20draw%20the%20survey%20made.%20The%20one-PSU%20design%20also%20takes%20no%20lonely-PSU%20rule%3A%20svy%20refuses%20%60singleton%3D%60%20on%20a%20design%20without%20strata%20(%60%5BSINGLETON_WITHOUT_STRATUM%5D%60)%2C%20and%20with%20one%20PSU%20and%20no%20strata%20it%20gives%20the%20SE%20of%200%20without%20one.%0A%0A%20%20%20%20So%20there%20are%20two%20ways%20to%20the%20same%20column.%20The%20pandas%20line%20says%20what%20the%20figure%20is%2C%20a%20count%2C%20and%20puts%20no%20SE%20beside%20it%3B%20the%20one-PSU%20design%20reuses%20the%20estimation%20call%20and%20fills%20the%20SE%20column%20with%20zeros.%20This%20course%20takes%20the%20pandas%20line%2C%20and%20Exercise%206.7%20builds%20the%20table%20with%20it.%20When%20you%20read%20R%20code%20that%20declares%20a%20design%20with%20weight%201%2C%20you%20now%20know%20what%20its%20figures%20are.%0A%0A%20%20%20%20%23%23%23%20Exercise%206.7.%20Weighted%20estimates%20and%20the%20sample's%20counts%2C%20side%20by%20side%20at%20three%20levels%0A%0A%20%20%20%20**Why%20it%20matters%3A**%20this%20is%20the%20summary%20step.%20Every%20table%20is%20made%20national%2C%20regional%20and%20provincial%2C%20and%20each%20weighted%20estimate%20sits%20beside%20the%20sample's%20own%20figures%3A%20the%20sum%20of%20the%20same%20variable%20over%20the%20interviewed%20households%2C%20and%20the%20number%20of%20interviews%20behind%20it.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20_example%20%3D%20own_side_by_side(levels_fixture()%2C%20%22y%22)%0A%0A%20%20%20%20def%20_cell(value%2C%20digits)%3A%0A%20%20%20%20%20%20%20%20return%20%22*missing*%22%20if%20pd.isna(value)%20else%20(value%20if%20isinstance(value%2C%20str)%20else%20f%22%7Bvalue%3A.%7Bdigits%7Df%7D%22)%0A%0A%20%20%20%20_rows%20%3D%20%22%5Cn%22.join(%0A%20%20%20%20%20%20%20%20%22%7C%20%22%20%2B%20%22%20%7C%20%22.join(%5Brow%5B%22level%22%5D%2C%20_cell(row%5B%22region_code%22%5D%2C%200)%2C%20_cell(row%5B%22province%22%5D%2C%200)%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%20_cell(row%5B%22weighted%22%5D%2C%201)%2C%20_cell(row%5B%22weighted_se%22%5D%2C%202)%2C%20_cell(row%5B%22weighted_cv%22%5D%2C%203)%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%20_cell(row%5B%22unweighted%22%5D%2C%200)%2C%20_cell(row%5B%22interviews%22%5D%2C%200)%5D)%20%2B%20%22%20%7C%22%0A%20%20%20%20%20%20%20%20for%20_%2C%20row%20in%20_example.iterrows()%0A%20%20%20%20)%0A%20%20%20%20mo.md(rf%22%22%22%0A%20%20%20%20**Input%20%E2%86%92%20output%3A**%20a%20frame%20of%20households%20with%20the%20design's%20columns%20(%60stratum_id%60%2C%20%60psu_id%60%2C%20%60psus_in_stratum%60%2C%20%60sel_final_weight%60)%2C%20%60region_code%60%20and%20%60province%60%2C%20and%20the%20name%20of%20a%20column%20%60y%60%20%E2%86%92%20one%20table%20of%20the%20total%20of%20%60y%60%2C%20the%20national%20row%20first%2C%20then%20the%20regions%2C%20then%20the%20provinces%2C%20each%20level's%20rows%20in%20order%20of%20their%20codes%3A%0A%0A%20%20%20%20-%20%60level%60%3A%20%60%22national%22%60%2C%20%60%22regional%22%60%20or%20%60%22provincial%22%60%3B%20%60region_code%60%20and%20%60province%60%3A%20the%20domain%2C%20missing%20where%20the%20level%20has%20none%20(the%20setting%20%60LEVELS%60%20lists%20each%20level's%20columns)%3B%0A%20%20%20%20-%20%60weighted%60%2C%20%60weighted_se%60%2C%20%60weighted_cv%60%3A%20the%20total%2C%20its%20SE%20and%20its%20CV%20from%20the%20survey's%20design%2C%20%60survey_design()%60%3B%0A%20%20%20%20-%20%60unweighted%60%3A%20the%20sum%20of%20%60y%60%20over%20the%20domain's%20rows%3B%20%60interviews%60%3A%20the%20number%20of%20rows%2C%20the%20households%20interviewed%20in%20the%20domain.%20Both%20come%20from%20pandas.%0A%0A%20%20%20%20On%20the%20small%20design%20of%20Exercise%206.3%2C%20its%20households%20given%20two%20regions%20and%20three%20provinces%20(01%20holds%20P%20and%20Q%2C%2004%20holds%20R)%3A%0A%0A%20%20%20%20%7C%20level%20%7C%20region_code%20%7C%20province%20%7C%20weighted%20%7C%20weighted_se%20%7C%20weighted_cv%20%7C%20unweighted%20%7C%20interviews%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C--%3A%7C--%3A%7C--%3A%7C--%3A%7C--%3A%7C%0A%20%20%20%20%7B_rows%7D%0A%0A%20%20%20%20The%20national%20row%20is%20Exercise%206.3's%20total%3A%2040%2C%20with%20an%20SE%20of%207.48.%20The%20unweighted%20column%20adds%20up%20y%20over%20the%20nine%20rows.%0A%0A%20%20%20%20**Tools%3A**%20%60sample.estimation.total(y%3D...%2C%20by%3D...)%60%20and%20%60.to_polars().to_pandas()%60%20(Exercise%206.4)%3B%20%60groupby(by)%5By%5D.agg(unweighted%3D%22sum%22%2C%20interviews%3D%22size%22)%60%20for%20a%20level%20with%20domain%20columns%2C%20and%20%60frame%5By%5D.sum()%60%20and%20%60len(frame)%60%20for%20the%20national%20row%2C%20which%20has%20none%3B%20%60rename%60%3B%20%60merge(...%2C%20on%3D...%2C%20validate%3D%22one_to_one%22)%60%2C%20and%20%60merge(...%2C%20how%3D%22cross%22)%60%20for%20the%20national%20row%3B%20%60pd.concat%60.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20side_by_side(frame%3A%20pd.DataFrame%2C%20y%3A%20str)%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22The%20total%20of%20y%20at%20the%20three%20levels%20of%20LEVELS%3A%20from%20the%20survey's%20design%2C%20and%20the%20sample's%20own%20sum%20and%20count%20of%0A%20%20%20%20interviews%20beside%20it.%20Columns%3A%20level%2C%20region_code%2C%20province%2C%20weighted%2C%20weighted_se%2C%20weighted_cv%2C%20unweighted%2C%0A%20%20%20%20interviews.%22%22%22%0A%20%20%20%20%23%20Exercise%206.7%3A%20the%20task%20and%20examples%20are%20in%20the%20text%20above%20this%20cell.%0A%20%20%20%20%23%20Step%201%3A%20the%20survey's%20sample%20on%20the%20frame%3A%20svy.Sample(data%3Dpl.from_pandas(frame)%2C%20design%3Dsurvey_design())%0A%20%20%20%20%23%20Step%202%3A%20for%20each%20level%20and%20its%20columns%20%60by%60%20in%20LEVELS%3A%20the%20sample's%20total%20of%20y%2C%20by%3Dby%20or%20None%2C%20as%20pandas%2C%0A%20%20%20%20%23%20%20%20%20%20%20%20%20%20keeping%20by%20%2B%20est%2C%20se%2C%20cv%2C%20renamed%20weighted%2C%20weighted_se%2C%20weighted_cv%0A%20%20%20%20%23%20Step%203%3A%20the%20sample's%20own%20figures%20with%20pandas%3A%20frame.groupby(by)%5By%5D.agg(unweighted%3D%22sum%22%2C%20interviews%3D%22size%22)%3B%0A%20%20%20%20%23%20%20%20%20%20%20%20%20%20the%20national%20level%20has%20no%20by%3A%20one%20row%20with%20frame%5By%5D.sum()%20and%20len(frame)%0A%20%20%20%20%23%20Step%204%3A%20the%20two%20side%20by%20side%3A%20merge%20on%20by%20(validate%3D%22one_to_one%22)%3B%20the%20national%20level%3A%20how%3D%22cross%22%0A%20%20%20%20%23%20Step%205%3A%20give%20each%20level's%20table%20its%20level%3B%20pd.concat%20them%20in%20LEVELS'%20order%20and%20return%20the%20columns%0A%20%20%20%20%23%20%20%20%20%20%20%20%20%20level%2C%20region_code%2C%20province%2C%20weighted%2C%20weighted_se%2C%20weighted_cv%2C%20unweighted%2C%20interviews%0A%20%20%20%20raise%20NotImplementedError(%22Exercise%206.7%22)%0A%0A%0A%40app.function%0Adef%20levels_fixture()%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22The%20small%20design%20of%20Exercise%206.3%20(a%20take-all%20stratum%20T%20of%20three%20households%2C%20a%20stratum%20S%20of%203%20PSUs%20out%20of%206)%2C%20its%0A%20%20%20%20households%20given%20two%20regions%20and%20three%20provinces%2C%20used%20by%20the%20check%20of%20Exercise%206.7%3A%2001%20holds%20P%20and%20Q%2C%2004%20holds%20R.%22%22%22%0A%20%20%20%20return%20small_design().drop(columns%3D%5B%22x%22%2C%20%22d%22%5D).assign(%0A%20%20%20%20%20%20%20%20region_code%3D%5B%2201%22%2C%20%2201%22%2C%20%2204%22%2C%20%2201%22%2C%20%2201%22%2C%20%2201%22%2C%20%2204%22%2C%20%2204%22%2C%20%2201%22%5D%2C%0A%20%20%20%20%20%20%20%20province%3D%5B%22P%22%2C%20%22Q%22%2C%20%22R%22%2C%20%22P%22%2C%20%22P%22%2C%20%22Q%22%2C%20%22R%22%2C%20%22R%22%2C%20%22Q%22%5D%2C%0A%20%20%20%20)%0A%0A%0A%40app.function%0Adef%20side_rows(result%3A%20pd.DataFrame%2C%20digits%3A%20int%20%3D%204)%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22Exercise%206.7's%20table%20in%20one%20order%20for%20the%20checks%3A%20national%2C%20regional%2C%20provincial%2C%20then%20by%20code%20and%20name%3B%20the%0A%20%20%20%20columns%20in%20the%20task's%20order%3B%20numbers%20rounded.%20So%20a%20right%20answer%20passes%20whatever%20order%20its%20rows%20came%20in.%22%22%22%0A%20%20%20%20columns%20%3D%20%5B%22level%22%2C%20%22region_code%22%2C%20%22province%22%2C%20%22weighted%22%2C%20%22weighted_se%22%2C%20%22weighted_cv%22%2C%20%22unweighted%22%2C%20%22interviews%22%5D%0A%20%20%20%20table%20%3D%20result.reset_index(drop%3DTrue)%5Bcolumns%5D%0A%20%20%20%20order%20%3D%20table%5B%22level%22%5D.map(%7Blevel%3A%20k%20for%20k%2C%20level%20in%20enumerate(LEVELS)%7D)%0A%20%20%20%20table%20%3D%20table.assign(_order%3Dorder).sort_values(%5B%22_order%22%2C%20%22region_code%22%2C%20%22province%22%5D%2C%20na_position%3D%22first%22).drop(columns%3D%22_order%22)%0A%20%20%20%20table%20%3D%20table.reset_index(drop%3DTrue)%0A%20%20%20%20table%5Bcolumns%5B3%3A%5D%5D%20%3D%20table%5Bcolumns%5B3%3A%5D%5D.astype(float).round(digits)%0A%20%20%20%20table%5B%5B%22region_code%22%2C%20%22province%22%5D%5D%20%3D%20table%5B%5B%22region_code%22%2C%20%22province%22%5D%5D.astype(object).where(table%5B%5B%22region_code%22%2C%20%22province%22%5D%5D.notna()%2C%20None)%0A%20%20%20%20return%20table%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(frame)%3A%0A%20%20%20%20check_cases(%0A%20%20%20%20%20%20%20%20%22Exercise%206.7%22%2C%20side_by_side%2C%0A%20%20%20%20%20%20%20%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22the%20small%20design%3A%20two%20regions%2C%20three%20provinces%22%2C%20(levels_fixture()%2C%20%22y%22))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22the%20survey's%20fishing%20households%22%2C%20(frame%2C%20%22hh_count%22))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22the%20survey's%20fishing%20units%22%2C%20(frame%2C%20%22units%22))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22the%20survey's%20large-scale%20operators%22%2C%20(frame%2C%20%22large_unit_operators%22))%2C%0A%20%20%20%20%20%20%20%20%5D%2C%0A%20%20%20%20%20%20%20%20what%3D%22frames%20and%20columns%22%2C%20summarize%3Dside_rows%2C%0A%20%20%20%20%20%20%20%20hint%3D%22Each%20level's%20totals%20from%20survey_design()%3B%20each%20level's%20sum%20and%20size%20of%20y%20with%20pandas%3B%20joined%20on%20the%20level's%20columns%20(how%3D'cross'%20for%20the%20national%20row)%3B%20then%20concat.%22%2C%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(frame)%3A%0A%20%20%20%20%23%20Try%20it%3A%20the%20fishing%20households%2C%20weighted%20and%20as%20interviewed%2C%20at%20every%20level%0A%20%20%20%20try_it(side_by_side%2C%20frame%2C%20%22hh_count%22)%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%206.7%22%3A%20mo.md(r%22%22%22%0A%20%20%20%20%7C%20what%20you%20see%20%7C%20the%20usual%20cause%20%7C%20look%20at%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20%60unweighted%60%20equals%20%60weighted%60%20%7C%20The%20weights%20were%20multiplied%20in%20before%20the%20sum%3A%20that%20is%20the%20weighted%20total%20again.%20%7C%20%60frame.groupby(by)%5By%5D%60%2C%20y%20alone%20%7C%0A%20%20%20%20%7C%20%60interviews%60%20equals%20%60unweighted%60%20for%20units%20or%20operators%20%7C%20The%20count%20is%20of%20y's%20sum%2C%20not%20of%20rows.%20%7C%20%60%22size%22%60%20%7C%0A%20%20%20%20%7C%20*ValueError%3A%20No%20group%20keys%20passed!*%20on%20the%20national%20level%20%7C%20%60frame.groupby(%5B%5D)%60%3A%20the%20national%20level%20has%20no%20column%20to%20group%20by.%20%7C%20one%20row%20of%20%60frame%5By%5D.sum()%60%20and%20%60len(frame)%60%20%7C%0A%20%20%20%20%7C%20*MergeError%3A%20No%20common%20columns%20to%20perform%20merge%20on*%20%7C%20The%20national%20row%20merged%20with%20no%20%60on%3D%60.%20%7C%20%60how%3D%22cross%22%60%20when%20%60by%60%20is%20empty%20%7C%0A%20%20%20%20%7C%20Fewer%20rows%20than%20expected%2C%20or%20the%20national%20row%20missing%20%7C%20A%20level%20skipped%2C%20or%20its%20table%20left%20out%20of%20the%20%60concat%60.%20%7C%20every%20level%20of%20%60LEVELS%60%2C%20each%20table%20added%20to%20the%20list%20%7C%0A%0A%20%20%20%20Still%20stuck%20after%20ten%20minutes%3F%20Ask%20your%20table%2C%20then%20raise%20your%20hand.%0A%20%20%20%20%22%22%22)%7D)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%20Side%20by%20side%2C%20without%20svy%0A%0A%20%20%20%20The%20same%20table%20from%20the%20formula.%20For%20a%20domain%20d%2C%20write%201%5Bd%5D%20for%201%20on%20its%20rows%20and%200%20elsewhere.%20The%20weighted%20total%20is%20%CE%A3%20w%C2%B7y%C2%B71%5Bd%5D%2C%20and%20its%20variance%20is%20section%202's%20formula%20applied%20to%20w%C2%B7y%C2%B71%5Bd%5D%20on%20every%20row%20of%20the%20design%2C%20as%20Exercise%206.6%20did%20for%20a%20mean.%20The%20sample's%20sum%20is%20%CE%A3%20y%C2%B71%5Bd%5D%20and%20its%20count%20%CE%A3%201%5Bd%5D%3A%20no%20weight%20and%20no%20design.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20own_side_by_side(frame%3A%20pd.DataFrame%2C%20y%3A%20str)%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22Exercise%206.7's%20table%20without%20svy%3A%20each%20domain's%20weighted%20total%20of%20y%20and%20its%20SE%20from%20taylor_variance%20on%20the%0A%20%20%20%20survey's%20design%2C%20and%20its%20sum%20of%20y%20and%20number%20of%20rows%20in%20the%20sample.%20Each%20level's%20domains%20in%20order%20of%20their%20codes.%22%22%22%0A%20%20%20%20w%20%3D%20frame%5B%22sel_final_weight%22%5D.to_numpy()%0A%20%20%20%20values%20%3D%20frame%5By%5D.to_numpy(dtype%3Dfloat)%0A%20%20%20%20parts%20%3D%20%5B%5D%0A%20%20%20%20for%20level%2C%20by%20in%20LEVELS.items()%3A%0A%20%20%20%20%20%20%20%20domains%20%3D%20frame%5Bby%5D.drop_duplicates().sort_values(by).to_dict(%22records%22)%20if%20by%20else%20%5B%7B%7D%5D%0A%20%20%20%20%20%20%20%20rows%20%3D%20%5B%5D%0A%20%20%20%20%20%20%20%20for%20domain%20in%20domains%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20inside%20%3D%20np.ones(len(frame)%2C%20dtype%3Dbool)%0A%20%20%20%20%20%20%20%20%20%20%20%20for%20column%2C%20value%20in%20domain.items()%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20inside%20%26%3D%20(frame%5Bcolumn%5D%20%3D%3D%20value).to_numpy()%0A%20%20%20%20%20%20%20%20%20%20%20%20weighted%20%3D%20float((w%20*%20values%20*%20inside).sum())%0A%20%20%20%20%20%20%20%20%20%20%20%20weighted_se%20%3D%20taylor_variance(frame%2C%20w%20*%20values%20*%20inside)%20**%200.5%0A%20%20%20%20%20%20%20%20%20%20%20%20rows.append(%7B**domain%2C%20%22weighted%22%3A%20weighted%2C%20%22weighted_se%22%3A%20weighted_se%2C%20%22weighted_cv%22%3A%20weighted_se%20%2F%20weighted%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%22unweighted%22%3A%20float((values%20*%20inside).sum())%2C%20%22interviews%22%3A%20int(inside.sum())%7D)%0A%20%20%20%20%20%20%20%20parts.append(pd.DataFrame(rows).assign(level%3Dlevel))%0A%20%20%20%20columns%20%3D%20%5B%22level%22%2C%20%22region_code%22%2C%20%22province%22%2C%20%22weighted%22%2C%20%22weighted_se%22%2C%20%22weighted_cv%22%2C%20%22unweighted%22%2C%20%22interviews%22%5D%0A%20%20%20%20return%20pd.concat(parts%2C%20ignore_index%3DTrue).reindex(columns%3Dcolumns)%0A%0A%0A%40app.cell%0Adef%20_(frame)%3A%0A%20%20%20%20%23%20Exercise%206.7%20and%20the%20own%20function%2C%20side%20by%20side%3A%20the%20largest%20difference%20in%20each%20figure%2C%20over%20every%20level%20and%20domain%0A%20%20%20%20def%20_both_ways()%3A%0A%20%20%20%20%20%20%20%20rows%20%3D%20%5B%5D%0A%20%20%20%20%20%20%20%20for%20column%20in%20%5B%22hh_count%22%2C%20%22units%22%2C%20%22large_unit_operators%22%5D%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20theirs%20%3D%20attempt(side_by_side%2C%20frame%2C%20column)%0A%20%20%20%20%20%20%20%20%20%20%20%20if%20theirs%20is%20NOT_YET%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20return%20waiting_for(%22Exercise%206.7%22)%0A%20%20%20%20%20%20%20%20%20%20%20%20theirs%2C%20ours%20%3D%20side_rows(theirs%2C%20digits%3D12)%2C%20side_rows(own_side_by_side(frame%2C%20column)%2C%20digits%3D12)%0A%20%20%20%20%20%20%20%20%20%20%20%20for%20figure%20in%20%5B%22weighted%22%2C%20%22weighted_se%22%2C%20%22unweighted%22%2C%20%22interviews%22%5D%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20rows.append(%7B%22total%20of%22%3A%20column%2C%20%22figure%22%3A%20figure%2C%20%22rows%22%3A%20len(ours)%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%20%20%20%20%22largest%20difference%2C%20svy%20and%20pandas%20against%20own%22%3A%20float((theirs%5Bfigure%5D%20-%20ours%5Bfigure%5D).abs().max())%7D)%0A%20%20%20%20%20%20%20%20return%20pd.DataFrame(rows)%0A%0A%20%20%20%20_both_ways()%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%20How%20far%20the%20design's%20variance%20is%20from%20a%20simple%20random%20sample's%20(the%20**design%20effect**)%2C%20and%20a%20rule%20on%20the%20CV%20that%20decides%20which%20estimates%20a%20table%20publishes%2C%20are%20in%20the%20stretch%20S2%2C%20%5Btabulation%5D(%2F%3Ffile%3D61_stretch_tabulation.py).%20In%20ReGenesees%2C%20%60svystatTM%60%20leaves%20the%20design%20effect%20out%20unless%20you%20ask%20for%20it%20(%60deff%20%3D%20FALSE%60%20is%20its%20default)%2C%20and%20%60vartype%20%3D%20c(%22se%22%2C%20%22cv%22)%60%20prints%20the%20CV%20beside%20each%20estimate.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%20Why%20the%20provinces'%20large-scale%20operators%20have%20large%20CVs%3A%20who%20they%20are%20outside%20stratum%20F%0A%0A%20%20%20%20Every%20large%20unit%20in%20a%20province's%20register%20is%20interviewed%2C%20and%20stratum%20F%20has%20next%20to%20no%20sampling%20error.%20So%20where%20do%20the%20CVs%20of%20the%20table%20below%20come%20from%2C%20every%20one%20of%20them%20above%2020%25%3F%20From%20the%20large-scale%20operators%20found%20**outside**%20stratum%20F%2C%20in%20the%20area%20strata%2C%20where%20each%20household%20carries%20its%20area's%20weight%3A%20about%202.5%20in%20stratum%20A%2C%205%20in%20B%20and%2015%20in%20C%2C%20where%20a%20household%20of%20the%20register%20carries%201.%20Whether%20a%20drawn%20area%20holds%20one%20is%20chance%2C%20so%20they%20bring%20all%20of%20the%20estimate's%20variance.%20They%20are%20of%20two%20kinds%3A%0A%0A%20%20%20%20-%20**Households%20of%20an%20area%20stratum%20that%20run%20a%20large-scale%20unit**%20(survey%20statisticians%20call%20them%20*stratum%20jumpers*)%3A%20listed%20among%20the%20small%20fishers%20of%20an%20area%2C%20they%20turned%20out%20to%20run%20a%20boat%20of%205%20gross%20tons%20or%20more.%20They%20are%20real%20large-scale%20operators%20in%20the%20wrong%20stratum.%0A%20%20%20%20-%20**Households%20whose%20scale%20rests%20on%20a%20boat%20measurement%20error**%20that%20module%208%20looks%20for%3A%20a%20boat%20measured%20in%20feet%20whose%20unit%20code%20says%20meters%20(%60feet_recorded_as_meters%60%20in%20the%20course's%20list%20of%20planted%20defects)%2C%20which%20turns%20a%20small%20outrigger%20boat%20into%20one%20of%20many%20tons.%20Their%20unit's%20%60unit_scale%60%20says%20large-scale%3B%20their%20boat%20says%20otherwise.%0A%0A%20%20%20%20The%20cell%20below%20splits%20each%20province's%20estimate%20into%20the%20three%20sources%2C%20and%20shows%20the%20CV%20with%20the%20second%20kind%20counted%20as%20small-scale%2C%20which%20an%20edit%20of%20their%20dimensions%20would%20do.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(frame%2C%20sample)%3A%0A%20%20%20%20%23%20Each%20province's%20large-scale%20operators%3A%20from%20stratum%20F%2C%20from%20households%20flagged%20for%20a%20boat%20measured%20in%20feet%2C%20from%20the%0A%20%20%20%20%23%20other%20households%20of%20the%20area%20strata%3B%20the%20CV%20as%20estimated%2C%20and%20with%20the%20flagged%20households'%20operators%20counted%20as%200%0A%20%20%20%20_defects%20%3D%20course.planted_defects()%0A%20%20%20%20_flagged_keys%20%3D%20_defects.loc%5B_defects%5B%22kind%22%5D%20%3D%3D%20%22feet_recorded_as_meters%22%2C%20%5B%22region_code%22%2C%20%22hh_id%22%2C%20%22kind%22%5D%5D.drop_duplicates(%5B%22region_code%22%2C%20%22hh_id%22%5D)%0A%20%20%20%20_marked%20%3D%20frame.merge(_flagged_keys%2C%20on%3D%5B%22region_code%22%2C%20%22hh_id%22%5D%2C%20how%3D%22left%22%2C%20validate%3D%22one_to_one%22)%0A%20%20%20%20_area%20%3D%20(_marked%5B%22sel_stratum%22%5D%20!%3D%20%22F%22)%20%26%20(_marked%5B%22large_unit_operators%22%5D%20%3E%200)%0A%20%20%20%20_flagged%20%3D%20_area%20%26%20_marked%5B%22kind%22%5D.notna()%0A%20%20%20%20_source%20%3D%20np.select(%5B_marked%5B%22sel_stratum%22%5D%20%3D%3D%20%22F%22%2C%20_flagged%2C%20_area%5D%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%5B%22from%20stratum%20F%22%2C%20%22from%20a%20boat%20measured%20in%20feet%22%2C%20%22from%20other%20area%20households%22%5D%2C%20default%3D%22none%22)%0A%20%20%20%20_parts%20%3D%20(_marked%5B%22sel_final_weight%22%5D%20*%20_marked%5B%22large_unit_operators%22%5D).groupby(%5B_marked%5B%22province%22%5D%2C%20_source%5D).sum().unstack(fill_value%3D0)%0A%20%20%20%20_parts%20%3D%20_parts.drop(columns%3D%22none%22%2C%20errors%3D%22ignore%22)%0A%20%20%20%20_as_is%20%3D%20sample.estimation.total(y%3D%22large_unit_operators%22%2C%20by%3D%22province%22).to_polars().to_pandas().set_index(%22province%22)%0A%20%20%20%20_edited%20%3D%20frame.assign(large_unit_operators%3Dframe%5B%22large_unit_operators%22%5D.where(~_flagged.to_numpy()%2C%200))%0A%20%20%20%20_after%20%3D%20svy.Sample(data%3Dpl.from_pandas(_edited)%2C%20design%3Dsurvey_design()).estimation.total(y%3D%22large_unit_operators%22%2C%20by%3D%22province%22).to_polars().to_pandas().set_index(%22province%22)%0A%20%20%20%20_parts%5B%22estimate%22%5D%20%3D%20_as_is%5B%22est%22%5D%0A%20%20%20%20_parts%5B%22cv%22%5D%20%3D%20_as_is%5B%22cv%22%5D%0A%20%20%20%20_parts%5B%22cv%2C%20the%20flagged%20boats%20as%20small-scale%22%5D%20%3D%20_after%5B%22cv%22%5D%0A%20%20%20%20_named%20%3D%20_marked%5B_flagged%5D%0A%20%20%20%20mo.vstack(%5B%0A%20%20%20%20%20%20%20%20_parts.round(3)%2C%0A%20%20%20%20%20%20%20%20mo.md(f%22**%7Bint(_area.sum())%7D%20households%20of%20the%20area%20strata**%20run%20a%20large-scale%20unit%3B%20%7Bint(_flagged.sum())%7D%20of%20them%20are%20flagged%20for%20a%20boat%20measured%20in%20feet%20and%20recorded%20in%20meters.%20The%20flagged%20ones%2C%20by%20region%2C%20household%2C%20stratum%20and%20weight%3A%22)%2C%0A%20%20%20%20%20%20%20%20_named%5B%5B%22region_code%22%2C%20%22hh_id%22%2C%20%22province%22%2C%20%22sel_stratum%22%2C%20%22large_unit_operators%22%2C%20%22sel_final_weight%22%5D%5D.sort_values(%5B%22province%22%2C%20%22sel_stratum%22%5D).reset_index(drop%3DTrue)%2C%0A%20%20%20%20%5D)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20Read%20the%20table%20by%20province.%20Stratum%20F's%20share%20of%20each%20estimate%20is%20a%20small%20part%20of%20it%3B%20the%20area%20households'%20share%2C%20at%20weights%20of%205%20or%2015%2C%20is%20most%20of%20it%2C%20and%20it%20is%20what%20moves%20from%20sample%20to%20sample.%20The%20flagged%20boats%20add%20estimate%20and%20variance%20that%20a%20measurement%20error%20put%20there.%20Counting%20them%20as%20small-scale%20lowers%20each%20affected%20province's%20estimate%2C%20and%20its%20SE%20with%20it%3B%20compare%20the%20last%20two%20columns%20to%20see%20that%20the%20CV%20does%20not%20follow%2C%20because%20the%20estimate%20falls%20as%20fast%20as%20the%20SE%20or%20faster.%20An%20edit%20makes%20an%20estimate%20right%2C%20not%20publishable.%20Neither%20change%20is%20this%20module's%20to%20make.%20Editing%20the%20dimensions%20is%20module%208's%20work%2C%20with%20its%20evidence%3B%20moving%20a%20household%20of%20an%20area%20stratum%20that%20runs%20a%20large-scale%20unit%20to%20the%20register's%20stratum%20changes%20the%20weights%2C%20and%20is%20the%20sampling%20side's%20decision%2C%20documented.%20The%20estimate%20here%20keeps%20the%20survey's%20records%20and%20weights%20and%20reports%20what%20they%20give%2C%20with%20a%20CV%20that%20says%20how%20far%20to%20trust%20it.%0A%0A%20%20%20%20%23%23%23%20The%20inflated%20final%20weights%0A%0A%20%20%20%20Module%208%20looks%20for%20weights%20out%20of%20line%20with%20their%20stratum.%20The%20survey%20has%20three%2C%20each%20about%20ten%20times%20its%20stratum's%20weight.%20Where%20are%20they%2C%20and%20what%20do%20they%20move%3F%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(frame%2C%20sample)%3A%0A%20%20%20%20%23%20The%20three%20inflated%20weights%3A%20their%20weight%20beside%20their%20stratum's%20median%2C%20and%20what%20they%20move%0A%20%20%20%20_defects%20%3D%20course.planted_defects()%0A%20%20%20%20_inflated%20%3D%20frame.merge(_defects.loc%5B_defects%5B%22kind%22%5D%20%3D%3D%20%22final_weight_inflated%22%2C%20%5B%22region_code%22%2C%20%22hh_id%22%5D%5D%2C%20on%3D%5B%22region_code%22%2C%20%22hh_id%22%5D)%0A%20%20%20%20_inflated%5B%22stratum_median%22%5D%20%3D%20_inflated%5B%22stratum_id%22%5D.map(frame.groupby(%22stratum_id%22)%5B%22sel_final_weight%22%5D.median())%0A%20%20%20%20_ids%20%3D%20set(zip(_inflated%5B%22region_code%22%5D%2C%20_inflated%5B%22hh_id%22%5D))%0A%20%20%20%20_is_inflated%20%3D%20pd.Series(%5B(r%2C%20h)%20in%20_ids%20for%20r%2C%20h%20in%20zip(frame%5B%22region_code%22%5D%2C%20frame%5B%22hh_id%22%5D)%5D%2C%20index%3Dframe.index)%0A%20%20%20%20_at_median%20%3D%20frame.assign(sel_final_weight%3Dframe%5B%22sel_final_weight%22%5D.where(~_is_inflated%2C%20frame%5B%22stratum_id%22%5D.map(frame.groupby(%22stratum_id%22)%5B%22sel_final_weight%22%5D.median())))%0A%20%20%20%20_as_is%20%3D%20sample.estimation.total(y%3D%22hh_count%22%2C%20by%3D%22province%22).to_polars().to_pandas().set_index(%22province%22)%0A%20%20%20%20_without%20%3D%20svy.Sample(data%3Dpl.from_pandas(_at_median)%2C%20design%3Dsurvey_design()).estimation.total(y%3D%22hh_count%22%2C%20by%3D%22province%22).to_polars().to_pandas().set_index(%22province%22)%0A%20%20%20%20_moved%20%3D%20pd.DataFrame(%7B%22households%2C%20as%20weighted%22%3A%20_as_is%5B%22est%22%5D%2C%20%22cv%22%3A%20_as_is%5B%22cv%22%5D%2C%20%22with%20the%20weights%20at%20their%20stratum's%20median%22%3A%20_without%5B%22est%22%5D%2C%20%22cv%20after%22%3A%20_without%5B%22cv%22%5D%7D)%0A%20%20%20%20mo.vstack(%5B%0A%20%20%20%20%20%20%20%20_inflated%5B%5B%22region_code%22%2C%20%22hh_id%22%2C%20%22province%22%2C%20%22stratum_id%22%2C%20%22sel_final_weight%22%2C%20%22stratum_median%22%2C%20%22large_unit_operators%22%5D%5D%2C%0A%20%20%20%20%20%20%20%20mo.md(f%22**%7Bint((_inflated%5B'large_unit_operators'%5D%20%3E%200).sum())%7D%20of%20the%20three**%20run%20a%20large-scale%20unit%2C%20so%20the%20large-scale%20operators'%20totals%20above%20don't%20move.%20The%20number%20of%20fishing%20households%20in%20their%20provinces%20does%3A%22)%2C%0A%20%20%20%20%20%20%20%20_moved.loc%5Bsorted(_inflated%5B%22province%22%5D.unique())%5D.round(3)%2C%0A%20%20%20%20%5D)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20The%20inflated%20weights%20sit%20in%20stratum%20C%20and%20run%20no%20large-scale%20unit%3A%20they%20inflate%20their%20provinces'%20counts%20of%20fishing%20households%20and%20the%20denominator%20of%20the%20share%20running%20a%20large-scale%20unit%2C%20and%20leave%20the%20large-scale%20operators'%20totals%20alone.%20Setting%20a%20weight%20to%20its%20stratum's%20median%20is%20shown%20here%20to%20measure%20its%20effect%2C%20not%20as%20a%20fix%3A%20trimming%20moves%20weight%2C%20and%20the%20moved%20weight%20has%20to%20go%20somewhere%20(svy's%20%60weighting.trim%60%20redistributes%20it).%20The%20domain%20a%20defect%20matters%20for%20is%20a%20question%20to%20answer%20with%20numbers%2C%20as%20this%20cell%20does%2C%20before%20the%20defect%20is%20edited.%0A%0A%20%20%20%20%23%23%204%20%C2%B7%20From%20ReGenesees%20to%20svy%2C%20call%20by%20call%0A%0A%20%20%20%20**The%20problem.**%20An%20estimation%20script%20written%20with%20ReGenesees%20in%20R%20is%20to%20run%20in%20Python.%20Each%20call%20needs%20an%20answer%3A%20what%20does%20it%20compute%2C%20which%20Python%20call%20computes%20the%20same%2C%20and%20how%20do%20you%20know%20they%20agree%3F%20Where%20no%20library%20computes%20it%2C%20you%20write%20it.%0A%0A%20%20%20%20**The%20tool.**%20Each%20ReGenesees%20call%20solves%20a%20problem%20a%20formula%20states.%20So%20each%20row%20below%20has%20two%20answers%20side%20by%20side%3A%20svy's%20call%2C%20where%20svy%20has%20one%2C%20and%20a%20function%20the%20course%20writes%20with%20numpy%2C%20pandas%20or%20scipy%20from%20the%20formula%2C%20with%20a%20test%20(%60tests%2Ftest_estimates.py%60)%20that%20the%20two%20agree.%20Three%20calls%20have%20no%20svy%20equivalent%2C%20and%20the%20course%20found%20none%20in%20another%20maintained%20Python%20package%20(November%202026)%3A%20for%20those%2C%20the%20own%20function%20stands%20alone%2C%20marked%20**own%20only**.%20The%20ReGenesees%20calls%20are%20written%20as%20its%20help%20pages%20give%20them%20(ReGenesees%202.5%2C%20read%2020%20November%202026).%0A%0A%20%20%20%20%7C%20in%20ReGenesees%20you%20write%20%7C%20what%20it%20computes%20%7C%20svy%200.36.0%20%7C%20the%20course's%20own%20function%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20%60e.svydesign(data%20%3D%20hh%2C%20ids%20%3D%20~psu_id%2C%20strata%20%3D%20~stratum_id%2C%20weights%20%3D%20~sel_final_weight%2C%20fpc%20%3D%20~fpc)%60%20%7C%20the%20design%20%7C%20%60svy.Design(stratum%3D%2C%20psu%3D%2C%20wgt%3D%2C%20pop_size%3D)%60%20and%20%60svy.Sample%60%20%7C%20%60design_rows%60%2C%20%60population_counts%60%20(6.1%2C%206.2)%20%7C%0A%20%20%20%20%7C%20%60options(RG.lonely.psu%20%3D%20%22remove%22)%60%2C%20or%20%60%22adjust%22%60%2C%20%60%22average%22%60%3B%20%60%22fail%22%60%20by%20default%20%7C%20the%20rule%20for%20a%20lonely%20PSU%20%7C%20%60singleton%3D%22skip%22%60%2C%20%60%22center%22%60%2C%20%60%22scale%22%60%3B%20no%20rule%20raises%20%7C%20%60design_rows%60%20marks%20the%20rows%20(6.1)%3B%20%60taylor_variance%60%20skips%20%7C%0A%20%20%20%20%7C%20%60collapse.strata(design)%60%20%7C%20a%20lonely%20PSU's%20stratum%20joined%20to%20another%20%7C%20%60svy.Singleton(%22collapse%22%2C%20using%3D...)%60%20%7C%20%60collapsed_se%60%20%7C%0A%20%20%20%20%7C%20%60e.svydesign(...%2C%20self.rep.str%20%3D%20~sr)%60%20%7C%20self-representing%20strata%3A%20the%20second%20stage's%20units%20as%20the%20variance's%20units%20%7C%20%60singleton%3D%22self_representing%22%60%20(every%20one-PSU%20stratum%2C%20not%20a%20flagged%20set)%20%7C%20%60self_representing_se%60%20%7C%0A%20%20%20%20%7C%20%60svystatTM(design%2C%20~units%2C%20estimator%20%3D%20%22Total%22%2C%20vartype%20%3D%20c(%22se%22%2C%20%22cv%22)%2C%20conf.int%20%3D%20TRUE)%60%20%7C%20a%20total%20with%20SE%2C%20CV%20and%20CI%20%7C%20%60estimation.total(y%3D)%60%20%7C%20%60stratified_total%60%2C%20%60t_interval%60%20(6.3)%20%7C%0A%20%20%20%20%7C%20%60svystatTM(design%2C%20~hh_size%2C%20estimator%20%3D%20%22Mean%22)%60%20%7C%20a%20mean%20%7C%20%60estimation.mean(y%3D)%60%20%7C%20%60own_mean%60%20%7C%0A%20%20%20%20%7C%20%60svystatTM%60%20on%20a%20factor%20%7C%20its%20absolute%20or%20relative%20frequencies%20%7C%20%60estimation.prop(y%3D)%60%20%7C%20%60own_prop%60%20%7C%0A%20%20%20%20%7C%20%60svystatTM(design%2C%20~operators%2C%20by%20%3D%20~province%3Asex)%60%20%7C%20domains%2C%20crossed%20%7C%20%60by%3D%60%2C%20%60where%3D%60%20%7C%20%60domain_cells%60%20(6.5)%2C%20%60domain_mean%60%20(6.6)%2C%20%60design_df%60%20%7C%0A%20%20%20%20%7C%20%60svystatTM(...%2C%20deff%20%3D%20TRUE)%60%20%7C%20design%20effects%3B%20for%20a%20domain%2C%20against%20a%20simple%20random%20sample%20within%20it%20%7C%20%60deff%3D%22wor%22%60%20%7C%20%60domain_design_effect%60%3B%20for%20the%20whole%20survey%2C%20%60design_effect%60%20in%20the%20stretch%20S2%20%7C%0A%20%20%20%20%7C%20%60svystatR(design%2C%20num%20%3D%20~n_boats%2C%20den%20%3D%20~units)%60%20%7C%20a%20ratio%20of%20two%20totals%20%7C%20%60estimation.ratio(y%3D%2C%20x%3D)%60%20%7C%20%60own_ratio%60%20(6.8)%20%7C%0A%20%20%20%20%7C%20%60svystatSR(design%2C%20~units%2C%20classes%20%3D%20~province)%60%20%7C%20ratios%20between%20classes'%20shares%20of%20a%20total%2C%20every%20ordered%20pair%20%7C%20%60estimation.total(y%3D%2C%20by%3D).contrast(svy.estd(a)%20%2F%20svy.estd(b))%60%2C%20one%20pair%20at%20a%20time%20%7C%20%60share_ratio%60%20%7C%0A%20%20%20%20%7C%20%60svystatB(design%2C%20n_boats%20~%20hh_size%20%2B%20units)%60%20%7C%20regression%20coefficients%20%7C%20%60glm.fit(y%3D%2C%20x%3D%2C%20family%3D%22gaussian%22)%60%2C%20one%20domain%20at%20a%20time%20with%20%60where%3D%60%20%7C%20%60regression_coefficients%60%20%7C%0A%20%20%20%20%7C%20%60svystatL(design%2C%20expression((large_units%20-%20small_units)%20%2F%20small_units))%60%20%7C%20any%20smooth%20function%20of%20totals%20%7C%20partly%3A%20%60contrast%60%20within%20one%20result%3B%20across%20variables%2C%20a%20linearized%20column%20you%20derive%2C%20passed%20to%20%60estimation.total%60%20%7C%20%60linearized_estimate%60%20(**own%20only**%20across%20variables)%20%7C%0A%20%20%20%20%7C%20%60e.calibrate(design%2C%20df.population%2C%20calmodel%20%3D%20~province%20%2B%20hh_size%20-%201%2C%20calfun%20%3D%20%22linear%22)%60%20%7C%20calibrated%20weights%20(GREG)%20%7C%20%60weighting.calibrate(controls%3D)%60%20%7C%20%60linear_calibration%60%20(6.9)%20%7C%0A%20%20%20%20%7C%20%60e.calibrate(...%2C%20calfun%20%3D%20%22logit%22%2C%20bounds%20%3D%20c(0.98%2C%201.17))%60%20%7C%20calibrated%20weights%20whose%20factors%20stay%20between%20the%20bounds%20%7C%20none%3A%20%60bounds%3D%60%20is%20%60%5BNOT_SUPPORTED%5D%60%20in%200.36.0%2C%20and%20no%20logit%20distance%20%7C%20%60logit_calibration%60%20(**own%20only**)%20%7C%0A%20%20%20%20%7C%20%60e.calibrate(...%2C%20aggregate.stage%20%3D%201)%60%20%7C%20calibrated%20weights%20equal%20within%20each%20unit%20of%20the%20first%20stage%20%7C%20none%20%7C%20%60aggregate_stage_calibration%60%20(**own%20only**)%20%7C%0A%0A%20%20%20%20The%20first%20rows%20were%20met%20in%20sections%201%20to%203.%20The%20rest%20follow%2C%20each%20with%20its%20problem%2C%20its%20formula%2C%20both%20answers%20and%20their%20agreement.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(frame%2C%20sample)%3A%0A%20%20%20%20%23%20Sections%201%20to%203's%20estimators%2C%20both%20ways%3A%20a%20domain%20mean%20and%20its%20degrees%20of%20freedom%0A%20%20%20%20def%20_so_far()%3A%0A%20%20%20%20%20%20%20%20rows%20%3D%20%5B%5D%0A%20%20%20%20%20%20%20%20ours%20%3D%20attempt(domain_mean%2C%20frame%2C%20%22hh_size%22%2C%20frame%5B%22runs_large_unit%22%5D%20%3D%3D%201)%0A%20%20%20%20%20%20%20%20theirs%20%3D%20sample.estimation.mean(y%3D%22hh_size%22%2C%20where%3Dsvy.col(%22runs_large_unit%22)%20%3D%3D%201).to_polars()%0A%20%20%20%20%20%20%20%20if%20ours%20is%20not%20NOT_YET%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20rows%20%2B%3D%20%5B(%22members%20of%20a%20household%20that%20runs%20a%20large-scale%20unit%2C%20mean%22%2C%20theirs%5B%22est%22%5D%5B0%5D%2C%20ours%5B%22est%22%5D)%2C%20(%22its%20SE%22%2C%20theirs%5B%22se%22%5D%5B0%5D%2C%20ours%5B%22se%22%5D)%5D%0A%20%20%20%20%20%20%20%20rows.append((%22its%20degrees%20of%20freedom%22%2C%20theirs%5B%22df%22%5D%5B0%5D%2C%20design_df(frame%2C%20frame%5B%22runs_large_unit%22%5D%20%3D%3D%201)))%0A%20%20%20%20%20%20%20%20return%20agreement(rows)%0A%0A%20%20%20%20_so_far()%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%20Ratios%3A%20%60svystatR%60%0A%0A%20%20%20%20**The%20problem.**%20How%20many%20boats%20does%20a%20fishing%20unit%20use%2C%20on%20average%20across%20the%20provinces%3F%20Boats%20per%20unit%20is%20the%20total%20of%20boats%20over%20the%20total%20of%20units%3A%20R%20%3D%20%C5%B6%20%2F%20X%CC%82.%20Both%20totals%20vary%2C%20so%20the%20ratio%20is%20linearized%2C%20as%20the%20mean%20was%3A%0A%0A%20%20%20%20%60%60%60%0A%20%20%20%20R%20%3D%20%CE%A3%20w%C2%B7y%20%2F%20%CE%A3%20w%C2%B7x%0A%20%20%20%20z%20%3D%20w%20%C2%B7%20(y%20%E2%88%92%20R%C2%B7x)%20%2F%20X%CC%82%0A%20%20%20%20SE%20%3D%20%E2%88%9A%20taylor_variance(frame%2C%20z)%0A%20%20%20%20%60%60%60%0A%0A%20%20%20%20A%20mean%20is%20the%20ratio%20with%20x%20%3D%201%20on%20every%20row.%20svy%3A%20%60sample.estimation.ratio(y%3D%22n_boats%22%2C%20x%3D%22units%22)%60.%0A%0A%20%20%20%20%23%23%23%20Exercise%206.8.%20A%20ratio%20of%20two%20totals%2C%20from%20the%20formula%0A%0A%20%20%20%20**Why%20it%20matters%3A**%20ratios%20are%20most%20of%20a%20fisheries%20report%3A%20boats%20per%20unit%2C%20catch%20per%20trip%2C%20large-scale%20units%20as%20a%20share%20of%20all.%20Writing%20one%20shows%20why%20its%20SE%20is%20not%20the%20SE%20of%20the%20numerator%20divided%20by%20the%20denominator.%0A%0A%20%20%20%20**Input%20%E2%86%92%20output%3A**%20a%20frame%20and%20the%20names%20of%20two%20columns%20%60y%60%20and%20%60x%60%20%E2%86%92%20a%20Series%20labeled%20%60est%60%20and%20%60se%60.%20On%20the%20small%20design%20of%20Exercise%206.3%2C%20y%20over%20x%20is%200.8333%20with%20an%20SE%20of%200.1543.%0A%0A%20%20%20%20**Tools%3A**%20the%20formula%20above%2C%20with%20%60taylor_variance%60.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20own_ratio(frame%3A%20pd.DataFrame%2C%20y%3A%20str%2C%20x%3A%20str)%20-%3E%20pd.Series%3A%0A%20%20%20%20%22%22%22The%20ratio%20of%20the%20totals%20of%20y%20and%20x%2C%20and%20its%20linearized%20SE%20(est%2C%20se).%22%22%22%0A%20%20%20%20%23%20Exercise%206.8%3A%20the%20task%20and%20examples%20are%20in%20the%20text%20above%20this%20cell.%0A%20%20%20%20%23%20Step%201%3A%20w%3B%20Y%20%3D%20%CE%A3%20w%C2%B7y%20and%20X%20%3D%20%CE%A3%20w%C2%B7x%3B%20R%20%3D%20Y%20%2F%20X%0A%20%20%20%20%23%20Step%202%3A%20z%20%3D%20w%20%C2%B7%20(y%20%E2%88%92%20R%C2%B7x)%20%2F%20X%0A%20%20%20%20%23%20Step%203%3A%20se%20%3D%20taylor_variance(frame%2C%20z)%20**%200.5%3B%20return%20pd.Series(%7B%22est%22%3A%20R%2C%20%22se%22%3A%20se%7D)%0A%20%20%20%20raise%20NotImplementedError(%22Exercise%206.8%22)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(frame)%3A%0A%20%20%20%20check_cases(%0A%20%20%20%20%20%20%20%20%22Exercise%206.8%22%2C%20own_ratio%2C%0A%20%20%20%20%20%20%20%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22the%20small%20design%2C%20y%20over%20x%22%2C%20(small_design()%2C%20%22y%22%2C%20%22x%22))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22the%20survey's%20boats%20per%20unit%22%2C%20(frame%2C%20%22n_boats%22%2C%20%22units%22))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22the%20survey's%20large-scale%20units%20as%20a%20share%20of%20all%20units%22%2C%20(frame%2C%20%22large_units%22%2C%20%22units%22))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22the%20survey's%20members%20per%20household%2C%20in%20La%20Union%22%2C%20(frame%5Bframe%5B%22province%22%5D%20%3D%3D%20%22La%20Union%22%5D%2C%20%22hh_size%22%2C%20%22hh_count%22))%2C%0A%20%20%20%20%20%20%20%20%5D%2C%0A%20%20%20%20%20%20%20%20what%3D%22frames%20and%20pairs%20of%20columns%22%2C%20summarize%3Dlambda%20result%3A%20as_numbers(result%2C%20%5B%22est%22%2C%20%22se%22%5D)%2C%0A%20%20%20%20%20%20%20%20hint%3D%22R%20%3D%20%CE%A3wy%20%2F%20%CE%A3wx%3B%20z%20%3D%20w(y%20%E2%88%92%20R%C2%B7x)%20%2F%20%CE%A3wx%3B%20the%20SE%20is%20the%20square%20root%20of%20taylor_variance(frame%2C%20z).%22%2C%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(frame)%3A%0A%20%20%20%20%23%20Try%20it%3A%20boats%20per%20unit%0A%20%20%20%20try_it(own_ratio%2C%20frame%2C%20%22n_boats%22%2C%20%22units%22)%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%206.8%22%3A%20mo.md(r%22%22%22%0A%20%20%20%20%7C%20what%20you%20see%20%7C%20the%20usual%20cause%20%7C%20look%20at%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20%60est%60%20a%20little%20off%20%7C%20The%20mean%20of%20each%20row's%20y%20%2F%20x%2C%20not%20the%20ratio%20of%20the%20totals.%20%7C%20%60Y%20%2F%20X%60%20%7C%0A%20%20%20%20%7C%20%60se%60%20thousands%20of%20times%20too%20large%20%7C%20z%20not%20divided%20by%20X%CC%82.%20%7C%20%60%2F%20X%60%20%7C%0A%20%20%20%20%7C%20%60se%60%20far%20too%20large%2C%20%60est%60%20right%20%7C%20z%20%3D%20w%C2%B7y%3A%20the%20SE%20of%20the%20numerator's%20total.%20%7C%20%60y%20%E2%88%92%20R%C2%B7x%60%20%7C%0A%20%20%20%20%7C%20*ZeroDivisionError*%20or%20%60inf%60%20%7C%20A%20row's%20y%20%2F%20x%20with%20x%20%3D%200%2C%20from%20dividing%20row%20by%20row.%20%7C%20divide%20the%20totals%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_(frame%2C%20sample)%3A%0A%20%20%20%20%23%20svystatR%2C%20both%20ways%0A%20%20%20%20def%20_ratios()%3A%0A%20%20%20%20%20%20%20%20rows%20%3D%20%5B%5D%0A%20%20%20%20%20%20%20%20for%20y%2C%20x%20in%20%5B(%22n_boats%22%2C%20%22units%22)%2C%20(%22large_units%22%2C%20%22units%22)%5D%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20ours%20%3D%20attempt(own_ratio%2C%20frame%2C%20y%2C%20x)%0A%20%20%20%20%20%20%20%20%20%20%20%20if%20ours%20is%20NOT_YET%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20return%20waiting_for(%22Exercise%206.8%22)%0A%20%20%20%20%20%20%20%20%20%20%20%20theirs%20%3D%20sample.estimation.ratio(y%3Dy%2C%20x%3Dx).to_polars()%0A%20%20%20%20%20%20%20%20%20%20%20%20rows%20%2B%3D%20%5B(f%22%7By%7D%20%2F%20%7Bx%7D%22%2C%20theirs%5B%22est%22%5D%5B0%5D%2C%20ours%5B%22est%22%5D)%2C%20(%22its%20SE%22%2C%20theirs%5B%22se%22%5D%5B0%5D%2C%20ours%5B%22se%22%5D)%5D%0A%20%20%20%20%20%20%20%20return%20agreement(rows)%0A%0A%20%20%20%20_ratios()%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%20Share%20ratios%3A%20%60svystatSR%60%0A%0A%20%20%20%20**The%20problem.**%20How%20many%20times%20as%20many%20fishing%20units%20run%20in%20Cebu%20as%20in%20Bohol%3F%20That%20is%20a%20ratio%20between%20two%20classes'%20totals%20of%20one%20variable.%20ReGenesees'%20%60svystatSR%60%20returns%20it%20for%20every%20ordered%20pair%20of%20classes.%20The%20ratio's%20linearized%20variable%3A%0A%0A%20%20%20%20%60%60%60%0A%20%20%20%20R%20%3D%20%C5%B6_a%20%2F%20%C5%B6_b%0A%20%20%20%20z%20%3D%20w%20%C2%B7%20y%20%C2%B7%20(1%5Ba%5D%20%E2%88%92%20R%20%C2%B7%201%5Bb%5D)%20%2F%20%C5%B6_b%0A%20%20%20%20%60%60%60%0A%0A%20%20%20%20svy%20estimates%20the%20classes'%20totals%20with%20%60by%3D%60%20and%20takes%20their%20ratio%20with%20a%20**contrast**%2C%20which%20carries%20the%20totals'%20covariance%20through%20the%20delta%20method%3A%20%60result.contrast(%7B%22a%2Fb%22%3A%20svy.estd(%22a%22)%20%2F%20svy.estd(%22b%22)%7D)%60%2C%20one%20pair%20at%20a%20time.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20share_ratio(frame%3A%20pd.DataFrame%2C%20y%3A%20str%2C%20classes%3A%20str%2C%20a%3A%20str%2C%20b%3A%20str)%20-%3E%20pd.Series%3A%0A%20%20%20%20%22%22%22The%20ratio%20between%20class%20a's%20and%20class%20b's%20totals%20of%20y%2C%20and%20its%20linearized%20SE%20(est%2C%20se).%22%22%22%0A%20%20%20%20w%20%3D%20frame%5B%22sel_final_weight%22%5D%0A%20%20%20%20in_a%20%3D%20(frame%5Bclasses%5D%20%3D%3D%20a).astype(float)%0A%20%20%20%20in_b%20%3D%20(frame%5Bclasses%5D%20%3D%3D%20b).astype(float)%0A%20%20%20%20Y_a%2C%20Y_b%20%3D%20(w%20*%20frame%5By%5D%20*%20in_a).sum()%2C%20(w%20*%20frame%5By%5D%20*%20in_b).sum()%0A%20%20%20%20R%20%3D%20Y_a%20%2F%20Y_b%0A%20%20%20%20z%20%3D%20w%20*%20frame%5By%5D%20*%20(in_a%20-%20R%20*%20in_b)%20%2F%20Y_b%0A%20%20%20%20return%20pd.Series(%7B%22est%22%3A%20R%2C%20%22se%22%3A%20taylor_variance(frame%2C%20z)%20**%200.5%7D)%0A%0A%0A%40app.cell%0Adef%20_(frame%2C%20sample)%3A%0A%20%20%20%20%23%20svystatSR%2C%20both%20ways%3A%20units%20in%20Cebu%20over%20units%20in%20Bohol%0A%20%20%20%20_theirs%20%3D%20sample.estimation.total(y%3D%22units%22%2C%20by%3D%22province%22).contrast(%7B%22Cebu%2FBohol%22%3A%20svy.estd(%22Cebu%22)%20%2F%20svy.estd(%22Bohol%22)%7D).to_polars()%0A%20%20%20%20_ours%20%3D%20share_ratio(frame%2C%20%22units%22%2C%20%22province%22%2C%20%22Cebu%22%2C%20%22Bohol%22)%0A%20%20%20%20agreement(%5B(%22units%2C%20Cebu%20%2F%20Bohol%22%2C%20_theirs%5B%22est%22%5D%5B0%5D%2C%20_ours%5B%22est%22%5D)%2C%20(%22its%20SE%22%2C%20_theirs%5B%22se%22%5D%5B0%5D%2C%20_ours%5B%22se%22%5D)%5D)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%20Regression%20coefficients%3A%20%60svystatB%60%0A%0A%20%20%20%20**The%20problem.**%20How%20many%20more%20boats%20does%20a%20household%20use%20per%20extra%20member%2C%20or%20per%20extra%20fishing%20unit%2C%20across%20the%20provinces'%20households%3F%20A%20population%20regression's%20coefficients%20are%20estimated%20by%20weighted%20least%20squares%2C%20and%20their%20SEs%20by%20linearizing%20the%20estimating%20equations%3A%0A%0A%20%20%20%20%60%60%60%0A%20%20%20%20A%20%3D%20%CE%A3%20w%20%C2%B7%20x%20x%E1%B5%80%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20x%20is%20a%20row's%20values%2C%20with%201%20first%20for%20the%20intercept%0A%20%20%20%20%CE%B2%20%3D%20A%E2%81%BB%C2%B9%20%C2%B7%20%CE%A3%20w%20%C2%B7%20x%20%C2%B7%20y%0A%20%20%20%20e%20%3D%20y%20%E2%88%92%20x%E1%B5%80%CE%B2%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20the%20row's%20residual%0A%20%20%20%20u%20%3D%20A%E2%81%BB%C2%B9%20%C2%B7%20x%20%C2%B7%20w%20%C2%B7%20e%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20one%20linearized%20variable%20per%20coefficient%0A%20%20%20%20SE(%CE%B2_j)%20%3D%20%E2%88%9A%20taylor_variance(frame%2C%20u_j)%0A%20%20%20%20%60%60%60%0A%0A%20%20%20%20svy%3A%20%60sample.glm.fit(y%3D%22n_boats%22%2C%20x%3D%5B%22hh_size%22%2C%20%22units%22%5D%2C%20family%3D%22gaussian%22)%60.%20svy's%20%60glm.fit%60%20takes%20no%20%60by%3D%60%3B%20a%20domain%20is%20fitted%20with%20%60where%3D%60%2C%20one%20at%20a%20time.%20ReGenesees'%20%60svystatB%60%20also%20gives%20design%20effects%20for%20coefficients%3B%20svy%200.36.0%20does%20not.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20regression_coefficients(frame%3A%20pd.DataFrame%2C%20y%3A%20str%2C%20xs%3A%20list%5Bstr%5D)%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22Design-based%20linear%20regression%20of%20y%20on%20the%20columns%20xs%20and%20an%20intercept%3A%20each%20coefficient%20and%20its%20SE.%22%22%22%0A%20%20%20%20w%20%3D%20frame%5B%22sel_final_weight%22%5D.to_numpy()%0A%20%20%20%20X%20%3D%20np.column_stack(%5Bnp.ones(len(frame))%2C%20frame%5Bxs%5D.to_numpy(dtype%3Dfloat)%5D)%0A%20%20%20%20values%20%3D%20frame%5By%5D.to_numpy(dtype%3Dfloat)%0A%20%20%20%20A%20%3D%20X.T%20%40%20(X%20*%20w%5B%3A%2C%20None%5D)%0A%20%20%20%20beta%20%3D%20np.linalg.solve(A%2C%20X.T%20%40%20(w%20*%20values))%0A%20%20%20%20scores%20%3D%20(X%20*%20(w%20*%20(values%20-%20X%20%40%20beta))%5B%3A%2C%20None%5D)%20%40%20np.linalg.inv(A)%20%20%23%20one%20column%20per%20coefficient%3B%20A%20is%20symmetric%0A%20%20%20%20return%20pd.DataFrame(%7B%22term%22%3A%20%5B%22_intercept_%22%2C%20*xs%5D%2C%20%22estimate%22%3A%20beta%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%22std_err%22%3A%20%5Btaylor_variance(frame%2C%20scores%5B%3A%2C%20j%5D)%20**%200.5%20for%20j%20in%20range(X.shape%5B1%5D)%5D%7D)%0A%0A%0A%40app.cell%0Adef%20_(frame%2C%20sample)%3A%0A%20%20%20%20%23%20svystatB%2C%20both%20ways%0A%20%20%20%20_theirs%20%3D%20sample.glm.fit(y%3D%22n_boats%22%2C%20x%3D%5B%22hh_size%22%2C%20%22units%22%5D%2C%20family%3D%22gaussian%22).to_polars().to_pandas()%0A%20%20%20%20_ours%20%3D%20regression_coefficients(frame%2C%20%22n_boats%22%2C%20%5B%22hh_size%22%2C%20%22units%22%5D)%0A%20%20%20%20agreement(%5B(f%22%7Bterm%7D%2C%20%7Bwhat%7D%22%2C%20_theirs.loc%5Bi%2C%20column%5D%2C%20_ours.loc%5Bi%2C%20column%5D)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20for%20i%2C%20term%20in%20enumerate(_ours%5B%22term%22%5D)%20for%20what%2C%20column%20in%20%5B(%22coefficient%22%2C%20%22estimate%22)%2C%20(%22SE%22%2C%20%22std_err%22)%5D%5D)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%20Complex%20estimators%3A%20%60svystatL%60%0A%0A%20%20%20%20**The%20problem.**%20By%20what%20share%20do%20large-scale%20units%20outnumber%2C%20or%20fall%20short%20of%2C%20small-scale%20ones%3F%20f%20%3D%20(%C5%B6_L%20%E2%88%92%20%C5%B6_S)%20%2F%20%C5%B6_S%20is%20a%20function%20of%20two%20totals%20of%20**two%20variables**.%20ReGenesees'%20%60svystatL%60%20takes%20any%20such%20expression%2C%20written%20with%20each%20total%20as%20its%20variable's%20bare%20name%2C%20and%20linearizes%20it.%20The%20rule%20it%20applies%2C%20for%20a%20function%20f%20of%20the%20totals%20t%E2%82%81%20%E2%80%A6%20t_p%20of%20columns%20y%E2%82%81%20%E2%80%A6%20y_p%3A%0A%0A%20%20%20%20%60%60%60%0A%20%20%20%20z%20%3D%20w%20%C2%B7%20%CE%A3_j%20(%E2%88%82f%20%2F%20%E2%88%82t_j)%20%C2%B7%20y_j%20%20%20%20%20%20%20the%20gradient%20at%20the%20estimated%20totals%0A%20%20%20%20SE%20%3D%20%E2%88%9A%20taylor_variance(frame%2C%20z)%0A%20%20%20%20%60%60%60%0A%0A%20%20%20%20svy%20covers%20part%20of%20it%3A%20a%20**contrast**%20applies%20the%20delta%20method%20to%20the%20estimates%20of%20one%20result%20(the%20provinces%20of%20one%20%60by%3D%60%2C%20say)%2C%20but%20the%20members%20of%20a%20list%20of%20results%20are%20estimated%20apart%20and%20svy%20refuses%20to%20combine%20them%20(%60%5BCONTRAST_LIST_AMBIGUOUS%5D%60).%20Across%20variables%2C%20svy%20takes%20a%20linearized%20column%20you%20derive%2C%20%60z%20%2F%20w%60%2C%20and%20gives%20its%20total's%20SE.%20%60linearized_estimate%60%20derives%20the%20gradient%20numerically%2C%20so%20it%20takes%20any%20function%20of%20totals%3A%20**own%20only**%20for%20the%20general%20case.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20linearized_estimate(frame%3A%20pd.DataFrame%2C%20columns%3A%20list%5Bstr%5D%2C%20f%2C%20step%3A%20float%20%3D%201e-6)%20-%3E%20pd.Series%3A%0A%20%20%20%20%22%22%22Any%20smooth%20function%20f%20of%20the%20totals%20of%20columns%20(passed%20to%20f%20in%20that%20order)%2C%20and%20its%20linearized%20SE.%20The%20gradient%20is%0A%20%20%20%20taken%20by%20central%20differences%20at%20the%20estimated%20totals.%22%22%22%0A%20%20%20%20w%20%3D%20frame%5B%22sel_final_weight%22%5D.to_numpy()%0A%20%20%20%20values%20%3D%20frame%5Bcolumns%5D.to_numpy(dtype%3Dfloat)%0A%20%20%20%20totals%20%3D%20w%20%40%20values%0A%20%20%20%20gradient%20%3D%20np.empty(len(columns))%0A%20%20%20%20for%20j%20in%20range(len(columns))%3A%0A%20%20%20%20%20%20%20%20h%20%3D%20step%20*%20max(abs(totals%5Bj%5D)%2C%201.0)%0A%20%20%20%20%20%20%20%20up%2C%20down%20%3D%20totals.copy()%2C%20totals.copy()%0A%20%20%20%20%20%20%20%20up%5Bj%5D%20%2B%3D%20h%0A%20%20%20%20%20%20%20%20down%5Bj%5D%20-%3D%20h%0A%20%20%20%20%20%20%20%20gradient%5Bj%5D%20%3D%20(f(*up)%20-%20f(*down))%20%2F%20(2%20*%20h)%0A%20%20%20%20z%20%3D%20w%20*%20(values%20%40%20gradient)%0A%20%20%20%20return%20pd.Series(%7B%22est%22%3A%20f(*totals)%2C%20%22se%22%3A%20taylor_variance(frame%2C%20z)%20**%200.5%2C%20%22gradient%22%3A%20gradient%7D)%0A%0A%0A%40app.cell%0Adef%20_(frame%2C%20sample)%3A%0A%20%20%20%20%23%20svystatL%2C%20three%20ways%3A%20own%2C%20svy%20given%20the%20linearized%20column%2C%20and%20a%20check%20through%20svy's%20ratio%20(f%20is%20the%20ratio%20minus%201)%0A%20%20%20%20def%20_difference(large%2C%20small)%3A%0A%20%20%20%20%20%20%20%20return%20(large%20-%20small)%20%2F%20small%0A%0A%20%20%20%20_ours%20%3D%20linearized_estimate(frame%2C%20%5B%22large_units%22%2C%20%22small_units%22%5D%2C%20_difference)%0A%20%20%20%20_u%20%3D%20frame%5B%5B%22large_units%22%2C%20%22small_units%22%5D%5D.to_numpy(dtype%3Dfloat)%20%40%20_ours%5B%22gradient%22%5D%20%20%23%20z%20%2F%20w%0A%20%20%20%20_given_u%20%3D%20svy.Sample(data%3Dpl.from_pandas(frame.assign(u%3D_u))%2C%20design%3Dsurvey_design()).estimation.total(y%3D%22u%22).to_polars()%0A%20%20%20%20_ratio%20%3D%20sample.estimation.ratio(y%3D%22large_units%22%2C%20x%3D%22small_units%22).to_polars()%0A%20%20%20%20_refused%20%3D%20svy_error(lambda%3A%20sample.estimation.total(y%3D%5B%22large_units%22%2C%20%22small_units%22%5D).contrast(svy.estd(%22large_units%22)%20%2F%20svy.estd(%22small_units%22)))%0A%20%20%20%20mo.vstack(%5B%0A%20%20%20%20%20%20%20%20agreement(%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22(large%20%E2%88%92%20small)%20%2F%20small%2C%20via%20svy's%20ratio%20%E2%88%92%201%22%2C%20_ratio%5B%22est%22%5D%5B0%5D%20-%201%2C%20_ours%5B%22est%22%5D)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22its%20SE%2C%20via%20svy's%20ratio%22%2C%20_ratio%5B%22se%22%5D%5B0%5D%2C%20_ours%5B%22se%22%5D)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22its%20SE%2C%20svy%20given%20the%20linearized%20column%22%2C%20_given_u%5B%22se%22%5D%5B0%5D%2C%20_ours%5B%22se%22%5D)%2C%0A%20%20%20%20%20%20%20%20%5D)%2C%0A%20%20%20%20%20%20%20%20mo.md(f%22A%20contrast%20across%20two%20variables%2C%20as%20svy%20answers%20it%3A%5Cn%5Cn%60%60%60%5Cn%7B_refused%7D%5Cn%60%60%60%22)%2C%0A%20%20%20%20%5D)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20This%20f%20is%20the%20ratio%20of%20the%20two%20totals%20minus%201%2C%20which%20is%20how%20the%20first%20two%20rows%20can%20be%20checked%20against%20svy's%20ratio.%20A%20function%20with%20no%20such%20shortcut%20(a%20log%20of%20a%20ratio%20of%20three%20totals)%20has%20only%20the%20last%20two%20routes%2C%20and%20%60linearized_estimate%60%20is%20the%20one%20that%20needs%20no%20calculus.%0A%0A%20%20%20%20%23%23%23%20Design%20effects%20of%20domains%0A%0A%20%20%20%20**The%20problem.**%20A%20**design%20effect**%20(deff)%20is%20the%20design's%20variance%20over%20the%20variance%20a%20simple%20random%20sample%20of%20the%20same%20number%20of%20households%20would%20have%20had%3B%20the%20stretch%20S2%2C%20%5Btabulation%5D(%2F%3Ffile%3D61_stretch_tabulation.py)%2C%20writes%20it%20from%20its%20formula%20for%20the%20whole%20survey.%20For%20a%20domain%2C%20ReGenesees%20compares%20the%20design's%20variance%20with%20that%20of%20a%20simple%20random%20sample%20drawn%20**within**%20the%20domain%3B%20its%20help%20page%20for%20%60svystatTM%60%20says%20so%20and%20names%20Stata's%20%60srssubpop%60%20as%20the%20same%20approach.%20svy's%20documentation%20does%20not%20say%20which%20form%20it%20uses%3B%20its%20code%20and%20a%20run%20show%20it%20uses%20the%20same%20one%3A%20n_d%20and%20N%CC%82_d%20are%20the%20domain's%20households%20and%20their%20weights%2C%20s%C2%B2%20the%20domain's%20weighted%20variance.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20domain_design_effect(frame%3A%20pd.DataFrame%2C%20y%3A%20str%2C%20domain)%20-%3E%20float%3A%0A%20%20%20%20%22%22%22The%20design%20effect%20of%20a%20domain's%20total%20of%20y%2C%20against%20a%20simple%20random%20sample%20without%20replacement%20within%20the%20domain.%22%22%22%0A%20%20%20%20d%20%3D%20np.asarray(domain%2C%20dtype%3Dbool)%0A%20%20%20%20w%20%3D%20frame%5B%22sel_final_weight%22%5D.to_numpy()%0A%20%20%20%20values%20%3D%20frame%5By%5D.to_numpy(dtype%3Dfloat)%0A%20%20%20%20inside%20%3D%20d%20%26%20(w%20%3E%200)%0A%20%20%20%20n%2C%20N%20%3D%20inside.sum()%2C%20w%5Binside%5D.sum()%0A%20%20%20%20mean%20%3D%20(w%20*%20values)%5Binside%5D.sum()%20%2F%20N%0A%20%20%20%20s2%20%3D%20n%20%2F%20(n%20-%201)%20*%20(w%20*%20(values%20-%20mean)%20**%202)%5Binside%5D.sum()%20%2F%20N%0A%20%20%20%20return%20taylor_variance(frame%2C%20w%20*%20values%20*%20d)%20%2F%20(N**2%20*%20(1%20-%20n%20%2F%20N)%20*%20s2%20%2F%20n)%0A%0A%0A%40app.cell%0Adef%20_(frame%2C%20sample)%3A%0A%20%20%20%20%23%20Domain%20deffs%2C%20both%20ways%3A%20units%20of%20the%20households%20that%20run%20a%20large-scale%20unit%2C%20and%20of%20each%20region%0A%20%20%20%20_rows%20%3D%20%5B(%22households%20running%20a%20large-scale%20unit%22%2C%20svy.col(%22runs_large_unit%22)%20%3D%3D%201%2C%20frame%5B%22runs_large_unit%22%5D%20%3D%3D%201)%5D%0A%20%20%20%20_rows%20%2B%3D%20%5B(f%22region%20%7Breg%7D%22%2C%20svy.col(%22region_code%22)%20%3D%3D%20reg%2C%20frame%5B%22region_code%22%5D%20%3D%3D%20reg)%20for%20reg%20in%20course.REGIONS%5D%0A%20%20%20%20agreement(%5B%0A%20%20%20%20%20%20%20%20(f%22deff%2C%20units%20of%20%7Blabel%7D%22%2C%20sample.estimation.total(y%3D%22units%22%2C%20where%3Dwhere%2C%20deff%3D%22wor%22).to_polars()%5B%22deff%22%5D%5B0%5D%2C%20domain_design_effect(frame%2C%20%22units%22%2C%20mask))%0A%20%20%20%20%20%20%20%20for%20label%2C%20where%2C%20mask%20in%20_rows%0A%20%20%20%20%5D)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%20Self-representing%20strata%20and%20collapsed%20strata%3A%20%60self.rep.str%60%20and%20%60collapse.strata%60%0A%0A%20%20%20%20**The%20problem.**%20Some%20designs%20take%20a%20large%20area%20with%20certainty%20as%20a%20stratum%20of%20its%20own%2C%20then%20draw%20households%20or%20segments%20within%20it.%20Such%20a%20stratum%20has%20**one**%20PSU%2C%20so%20the%20between-PSU%20formula%20has%20nothing%20to%20measure%3B%20its%20sampling%20error%20is%20in%20the%20second%20stage.%20ReGenesees'%20help%20page%20for%20%60e.svydesign%60%20says%20that%20in%20such%20a%20stratum%20the%20leading%20contribution%20to%20the%20variance%20comes%20from%20the%20second-stage%20units%2C%20and%20%60self.rep.str%60%20flags%20these%20strata%20so%20that%20their%20variance%20is%20measured%20between%20those%20units%2C%20as%20an%20approximation%20it%20warns%20about.%20svy's%20%60singleton%3D%22self_representing%22%60%20does%20the%20same%20for%20every%20stratum%20with%20one%20PSU%20in%20the%20data%3B%20there%20is%20no%20flag.%20The%20own%20function%20makes%20the%20second-stage%20unit%20the%20unit%20in%20those%20strata%2C%20and%20the%20PSU%20elsewhere%2C%20and%20calls%20the%20same%20formula.%0A%0A%20%20%20%20%60collapse.strata%60%20answers%20a%20lonely%20PSU%20differently%3A%20its%20stratum%20joins%20another%2C%20and%20the%20variance%20is%20measured%20over%20the%20merged%20stratum.%20ReGenesees%20pairs%20each%20lonely%20stratum%20with%20another%20at%20random%2C%20or%20with%20the%20most%20similar%20by%20a%20score%20you%20give%20(%60sim.score%60)%2C%20within%20blocks%20you%20name%20(%60block.vars%60)%3B%20svy's%20%60using%3D%60%20takes%20the%20pairs%20as%20a%20mapping%2C%20or%20a%20rule.%20The%20own%20version%20relabels%20the%20stratum%20and%20calls%20the%20same%20formula.%20The%20survey%20has%20no%20self-representing%20stratum%20and%2C%20with%20population%20counts%2C%20can't%20be%20collapsed%20(section%201)%2C%20so%20both%20run%20on%20a%20small%20synthetic%20design%3A%20four%20strata%20of%20three%20PSUs%2C%20and%20two%20strata%20of%20one%20PSU%20and%20eight%20segments.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20two_stage_sample(seed%3A%20int%20%3D%2020261108)%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22A%20synthetic%20design%20with%20no%20fpc%2C%20for%20self-representing%20and%20collapsed%20strata%3A%20four%20strata%20of%20three%20PSUs%20with%20three%0A%20%20%20%20to%20five%20segments%20each%2C%20and%20two%20self-representing%20strata%20of%20one%20PSU%20and%20eight%20segments.%20Seeded%2C%20so%20it%20is%20always%20the%20same.%22%22%22%0A%20%20%20%20rng%20%3D%20np.random.default_rng(seed)%0A%20%20%20%20rows%20%3D%20%5B%5D%0A%20%20%20%20for%20h%20in%20range(4)%3A%0A%20%20%20%20%20%20%20%20for%20p%20in%20range(3)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20for%20s%20in%20range(int(rng.integers(3%2C%206)))%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20rows%20%2B%3D%20%5B(f%22A%7Bh%7D%22%2C%20f%22A%7Bh%7D-%7Bp%7D%22%2C%20f%22A%7Bh%7D-%7Bp%7D-%7Bs%7D%22%2C%20float(rng.uniform(5%2C%2015))%2C%20float(rng.gamma(2%2C%2010)))%20for%20_%20in%20range(3)%5D%0A%20%20%20%20for%20h%20in%20range(2)%3A%0A%20%20%20%20%20%20%20%20for%20s%20in%20range(8)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20rows%20%2B%3D%20%5B(f%22SR%7Bh%7D%22%2C%20f%22SR%7Bh%7D-0%22%2C%20f%22SR%7Bh%7D-0-%7Bs%7D%22%2C%20float(rng.uniform(1%2C%203))%2C%20float(rng.gamma(2%2C%2010)))%20for%20_%20in%20range(2)%5D%0A%20%20%20%20return%20pd.DataFrame(rows%2C%20columns%3D%5B%22stratum_id%22%2C%20%22psu_id%22%2C%20%22ssu_id%22%2C%20%22sel_final_weight%22%2C%20%22y%22%5D)%0A%0A%0A%40app.function%0Adef%20self_representing_se(table%3A%20pd.DataFrame%2C%20y%3A%20str)%20-%3E%20float%3A%0A%20%20%20%20%22%22%22The%20SE%20of%20the%20total%20of%20y%20with%20self-representing%20strata%3A%20in%20a%20stratum%20with%20one%20PSU%2C%20the%20SSUs%20are%20the%20variance's%20units.%22%22%22%0A%20%20%20%20psus%20%3D%20table.groupby(%22stratum_id%22)%5B%22psu_id%22%5D.transform(%22nunique%22)%0A%20%20%20%20units%20%3D%20table.assign(psu_id%3Dtable%5B%22ssu_id%22%5D.where(psus%20%3D%3D%201%2C%20table%5B%22psu_id%22%5D))%0A%20%20%20%20return%20taylor_variance(units%2C%20table%5B%22sel_final_weight%22%5D%20*%20table%5By%5D)%20**%200.5%0A%0A%0A%40app.function%0Adef%20collapsed_se(table%3A%20pd.DataFrame%2C%20y%3A%20str%2C%20into%3A%20dict%5Bstr%2C%20str%5D)%20-%3E%20float%3A%0A%20%20%20%20%22%22%22The%20SE%20of%20the%20total%20of%20y%20with%20each%20stratum%20in%20%60into%60%20merged%20into%20the%20stratum%20it%20names.%22%22%22%0A%20%20%20%20merged%20%3D%20table.assign(stratum_id%3Dtable%5B%22stratum_id%22%5D.replace(into))%0A%20%20%20%20return%20taylor_variance(merged%2C%20table%5B%22sel_final_weight%22%5D%20*%20table%5By%5D)%20**%200.5%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20%23%20Self-representing%20and%20collapsed%20strata%2C%20both%20ways%2C%20on%20the%20synthetic%20design%0A%20%20%20%20_synthetic%20%3D%20two_stage_sample()%0A%20%20%20%20_sr%20%3D%20svy.Sample(data%3Dpl.from_pandas(_synthetic)%2C%20design%3Dsvy.Design(stratum%3D%22stratum_id%22%2C%20psu%3D%22psu_id%22%2C%20ssu%3D%22ssu_id%22%2C%20wgt%3D%22sel_final_weight%22%2C%20singleton%3D%22self_representing%22))%0A%20%20%20%20_lonely%20%3D%20_synthetic%5B_synthetic%5B%22stratum_id%22%5D.str.startswith(%22A%22)%20%26%20~((_synthetic%5B%22stratum_id%22%5D%20%3D%3D%20%22A0%22)%20%26%20(_synthetic%5B%22psu_id%22%5D%20!%3D%20%22A0-0%22))%5D%0A%20%20%20%20_collapsed%20%3D%20svy.Sample(data%3Dpl.from_pandas(_lonely)%2C%20design%3Dsvy.Design(stratum%3D%22stratum_id%22%2C%20psu%3D%22psu_id%22%2C%20wgt%3D%22sel_final_weight%22%2C%20singleton%3Dsvy.Singleton(%22collapse%22%2C%20using%3D%7B%22A0%22%3A%20%22A1%22%7D)))%0A%20%20%20%20agreement(%5B%0A%20%20%20%20%20%20%20%20(%22SE%2C%20self-representing%20strata%22%2C%20_sr.estimation.total(y%3D%22y%22).to_polars()%5B%22se%22%5D%5B0%5D%2C%20self_representing_se(_synthetic%2C%20%22y%22))%2C%0A%20%20%20%20%20%20%20%20(%22SE%2C%20stratum%20A0%20(one%20PSU%20left)%20collapsed%20into%20A1%22%2C%20_collapsed.estimation.total(y%3D%22y%22).to_polars()%5B%22se%22%5D%5B0%5D%2C%20collapsed_se(_lonely%2C%20%22y%22%2C%20%7B%22A0%22%3A%20%22A1%22%7D))%2C%0A%20%20%20%20%5D)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%20Calibration%3A%20%60e.calibrate%60%0A%0A%20%20%20%20**The%20problem.**%20The%20survey's%20design%20weights%20add%20up%20to%20what%20the%20design%20implies%2C%20not%20to%20what%20is%20known%3A%20each%20province's%20number%20of%20fishing%20households%2C%20the%20number%20of%20household%20members.%20**Calibration**%20adjusts%20each%20weight%20by%20a%20factor%20g%2C%20as%20little%20as%20it%20can%2C%20so%20the%20weighted%20totals%20of%20chosen%20variables%20x%20hit%20known%20totals%20T.%20With%20the%20*linear*%20distance%20(GREG%2C%20ReGenesees'%20%60calfun%20%3D%20%22linear%22%60)%2C%20the%20factors%20have%20a%20closed%20form%3A%0A%0A%20%20%20%20%60%60%60%0A%20%20%20%20w_new%20%3D%20d%20%C2%B7%20(1%20%2B%20x%E1%B5%80%CE%BB)%20%20%20%20%20%20%20%20%20%20%20%20d%20the%20weight%20before%20calibration%0A%20%20%20%20%CE%BB%20%20%20%20%20%3D%20(%CE%A3%20d%20%C2%B7%20x%20x%E1%B5%80)%E2%81%BB%C2%B9%20%C2%B7%20(T%20%E2%88%92%20%CE%A3%20d%20%C2%B7%20x)%0A%20%20%20%20%60%60%60%0A%0A%20%20%20%20One%20linear%20solve.%20Here%20the%20design%20weights%20(%60sel_design_weight%60)%20are%20calibrated%20to%20each%20province's%20households%20and%20to%20the%20number%20of%20members.%20A%20survey%20takes%20such%20totals%20from%20a%20listing%20or%20a%20register%3B%20the%20course%20takes%20them%20from%20the%20final%20weights%2C%20so%20the%20exercise%20has%20totals%20to%20aim%20at.%20In%20ReGenesees%20you%20write%20the%20model%20as%20a%20formula%2C%20%60calmodel%20%3D%20~province%20%2B%20hh_size%20-%201%60%20(one%20column%20per%20province%2C%20no%20intercept%2C%20and%20the%20members)%2C%20with%20the%20totals%20in%20%60df.population%60.%20svy%3A%20%60sample.weighting.calibrate(controls%3D%7Bsvy.Cat(%22province%22)%3A%20%7B...%7D%2C%20%22hh_size%22%3A%20...%7D)%60%3B%20a%20categorical%20control%20becomes%20one%20column%20of%20x%20per%20level%2C%201%20for%20the%20level's%20rows.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20calibration_problem(frame%3A%20pd.DataFrame)%20-%3E%20tuple%5Bnp.ndarray%2C%20pd.DataFrame%2C%20pd.Series%5D%3A%0A%20%20%20%20%22%22%22The%20calibration%20this%20section%20runs%3A%20the%20design%20weights%2C%20the%20auxiliaries%20(one%200%2F1%20column%20per%20province%2C%20then%0A%20%20%20%20hh_size)%20and%20their%20known%20totals%20(each%20province's%20households%20and%20all%20members%2C%20from%20the%20final%20weights%2C%20rounded).%22%22%22%0A%20%20%20%20households_by_province%20%3D%20frame.groupby(%22province%22)%5B%22sel_final_weight%22%5D.sum().round()%0A%20%20%20%20X%20%3D%20pd.get_dummies(frame%5B%22province%22%5D%2C%20dtype%3Dfloat)%20%20%23%20its%20columns%20are%20the%20provinces%20in%20order%20of%20name%2C%20as%20the%20totals%20are%0A%20%20%20%20X%5B%22hh_size%22%5D%20%3D%20frame%5B%22hh_size%22%5D.astype(float)%0A%20%20%20%20totals%20%3D%20pd.concat(%5Bhouseholds_by_province%2C%20pd.Series(%7B%22hh_size%22%3A%20round((frame%5B%22sel_final_weight%22%5D%20*%20frame%5B%22hh_size%22%5D).sum())%7D)%5D)%0A%20%20%20%20return%20frame%5B%22sel_design_weight%22%5D.to_numpy()%2C%20X%2C%20totals%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%20Exercise%206.9.%20Linear%20calibration%2C%20from%20the%20formula%0A%0A%20%20%20%20**Why%20it%20matters%3A**%20calibration%20is%20how%20a%20survey's%20weights%20are%20made%20to%20agree%20with%20what%20is%20known%2C%20and%20the%20linear%20case%20is%20under%20every%20other%3A%20raking%20and%20the%20logit%20distance%20below%20are%20iterations%20of%20the%20same%20solve.%20Written%20once%2C%20the%20weights%20a%20survey%20publishes%20are%20no%20longer%20a%20black%20box.%0A%0A%20%20%20%20**Input%20%E2%86%92%20output%3A**%20the%20starting%20weights%20%60d%60%20(one%20per%20row)%2C%20the%20auxiliaries%20%60X%60%20(a%20table%20or%202-D%20array%2C%20one%20row%20per%20row%20of%20d%2C%20one%20column%20per%20known%20total)%20and%20the%20known%20%60totals%60%20(one%20per%20column%20of%20X)%20%E2%86%92%20a%20numpy%20array%20of%20calibrated%20weights%2C%20one%20per%20row%3A%0A%0A%20%20%20%20%7C%20d%20%7C%20x%20(class)%20%7C%20%E2%86%92%20calibrated%20%7C%0A%20%20%20%20%7C--%3A%7C%3A--%7C--%3A%7C%0A%20%20%20%20%7C%201%2C%201%20%7C%20class%201%2C%20known%20total%204%20%7C%202%2C%202%20%7C%0A%20%20%20%20%7C%202%2C%202%2C%202%20%7C%20class%202%2C%20known%20total%209%20%7C%203%2C%203%2C%203%20%7C%0A%0A%20%20%20%20With%20one%200%2F1%20column%20per%20class%2C%20calibration%20is%20poststratification%3A%20every%20weight%20of%20a%20class%20is%20multiplied%20by%20its%20known%20total%20over%20its%20weighted%20count%20(4%20%2F%202%20and%209%20%2F%206).%0A%0A%20%20%20%20**Tools%3A**%20%60np.asarray(...%2C%20dtype%3Dfloat)%60%2C%20%60%40%60%20(matrix%20product)%2C%20%60%5B%3A%2C%20None%5D%60%20to%20multiply%20each%20row%20of%20X%20by%20its%20weight%2C%20and%20%60np.linalg.solve%60.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20linear_calibration(d%2C%20X%2C%20totals)%20-%3E%20np.ndarray%3A%0A%20%20%20%20%22%22%22Weights%20calibrated%20to%20the%20totals%20with%20the%20linear%20distance%20(GREG)%3A%20d%20%C2%B7%20(1%20%2B%20X%CE%BB)%2C%20%CE%BB%20solving%20the%20calibration%20equations.%22%22%22%0A%20%20%20%20%23%20Exercise%206.9%3A%20the%20task%20and%20examples%20are%20in%20the%20text%20above%20this%20cell.%0A%20%20%20%20%23%20Step%201%3A%20d%2C%20X%20and%20totals%20as%20float%20numpy%20arrays%0A%20%20%20%20%23%20Step%202%3A%20M%20%3D%20X%E1%B5%80%20(d%C2%B7X)%3A%20X.T%20%40%20(X%20*%20d%5B%3A%2C%20None%5D)%3B%20r%20%3D%20totals%20%E2%88%92%20X%E1%B5%80d%0A%20%20%20%20%23%20Step%203%3A%20%CE%BB%20%3D%20np.linalg.solve(M%2C%20r)%0A%20%20%20%20%23%20Step%204%3A%20return%20d%20*%20(1%20%2B%20X%20%40%20%CE%BB)%0A%20%20%20%20raise%20NotImplementedError(%22Exercise%206.9%22)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(frame)%3A%0A%20%20%20%20_d%2C%20_X%2C%20_totals%20%3D%20calibration_problem(frame)%0A%20%20%20%20_region%20%3D%20frame%5B%22region_code%22%5D%20%3D%3D%20%2204%22%0A%20%20%20%20_d4%2C%20_X4%2C%20_totals4%20%3D%20calibration_problem(frame%5B_region%5D.reset_index(drop%3DTrue))%0A%20%20%20%20check_cases(%0A%20%20%20%20%20%20%20%20%22Exercise%206.9%22%2C%20linear_calibration%2C%0A%20%20%20%20%20%20%20%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22two%20classes%2C%20known%20totals%204%20and%209%3A%20poststratification%22%2C%20(np.array(%5B1.0%2C%201.0%2C%202.0%2C%202.0%2C%202.0%5D)%2C%20np.array(%5B%5B1.0%2C%200.0%5D%2C%20%5B1.0%2C%200.0%5D%2C%20%5B0.0%2C%201.0%5D%2C%20%5B0.0%2C%201.0%5D%2C%20%5B0.0%2C%201.0%5D%5D)%2C%20np.array(%5B4.0%2C%209.0%5D)))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22the%20survey's%20design%20weights%2C%20to%20provinces%20and%20members%22%2C%20(_d%2C%20_X%2C%20_totals))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22region%2004's%20design%20weights%2C%20X%20as%20a%20numpy%20array%22%2C%20(_d4%2C%20_X4.to_numpy()%2C%20_totals4.to_numpy()))%2C%0A%20%20%20%20%20%20%20%20%5D%2C%0A%20%20%20%20%20%20%20%20what%3D%22calibration%20problems%22%2C%20summarize%3Dlambda%20weights%3A%20%5Bround(float(v)%2C%204)%20for%20v%20in%20np.asarray(weights)%5D%2C%0A%20%20%20%20%20%20%20%20hint%3D%22%CE%BB%20%3D%20solve(X%E1%B5%80(d%C2%B7X)%2C%20totals%20%E2%88%92%20X%E1%B5%80d)%3B%20the%20weights%20are%20d%20%C2%B7%20(1%20%2B%20X%CE%BB).%22%2C%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(frame)%3A%0A%20%20%20%20%23%20Try%20it%3A%20the%20survey's%20calibrated%20weights%0A%20%20%20%20_d%2C%20_X%2C%20_totals%20%3D%20calibration_problem(frame)%0A%20%20%20%20try_it(linear_calibration%2C%20_d%2C%20_X%2C%20_totals)%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%206.9%22%3A%20mo.md(r%22%22%22%0A%20%20%20%20%7C%20what%20you%20see%20%7C%20the%20usual%20cause%20%7C%20look%20at%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20The%20weights%20come%20back%20unchanged%20%7C%20%CE%BB%20solved%20against%20the%20totals%2C%20not%20the%20gap.%20%7C%20%60totals%20-%20X.T%20%40%20d%60%20%7C%0A%20%20%20%20%7C%20*ValueError%3A%20matmul%3A%20Input%20operand%20...%20has%20a%20mismatch*%20%7C%20X's%20rows%20multiplied%20by%20d%20as%20a%20row%2C%20not%20a%20column.%20%7C%20%60X%20*%20d%5B%3A%2C%20None%5D%60%20%7C%0A%20%20%20%20%7C%20Weights%20of%202%20and%201.5%20instead%20of%202%20and%203%20%7C%20The%20factor%20g%20returned%2C%20not%20the%20weight.%20%7C%20%60d%20*%20(1%20%2B%20X%20%40%20lam)%60%20%7C%0A%20%20%20%20%7C%20*LinAlgError%3A%20Singular%20matrix*%20%7C%20Two%20columns%20of%20X%20that%20say%20the%20same%20thing%3A%20a%200%2F1%20column%20for%20every%20class%20and%20an%20intercept.%20%7C%20X's%20columns%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_(frame)%3A%0A%20%20%20%20%23%20e.calibrate(calfun%20%3D%20%22linear%22)%2C%20both%20ways%3A%20svy's%20calibrated%20weights%20and%20the%20own%20function's%0A%20%20%20%20def%20_calibrated()%3A%0A%20%20%20%20%20%20%20%20d%2C%20X%2C%20totals%20%3D%20calibration_problem(frame)%0A%20%20%20%20%20%20%20%20ours%20%3D%20attempt(linear_calibration%2C%20d%2C%20X%2C%20totals)%0A%20%20%20%20%20%20%20%20if%20ours%20is%20NOT_YET%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20waiting_for(%22Exercise%206.9%22)%0A%20%20%20%20%20%20%20%20start%20%3D%20svy.Sample(data%3Dpl.from_pandas(frame)%2C%20design%3Dsurvey_design(%22sel_design_weight%22))%0A%20%20%20%20%20%20%20%20theirs%20%3D%20start.weighting.calibrate(controls%3D%7Bsvy.Cat(%22province%22)%3A%20totals.drop(%22hh_size%22).to_dict()%2C%20%22hh_size%22%3A%20totals%5B%22hh_size%22%5D%7D%2C%20wgt_name%3D%22calibrated%22).data%5B%22calibrated%22%5D.to_numpy()%0A%20%20%20%20%20%20%20%20g%20%3D%20ours%20%2F%20d%0A%20%20%20%20%20%20%20%20return%20mo.vstack(%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20agreement(%5B(%22the%20largest%20difference%20between%20the%20two%20sets%20of%20weights%22%2C%20float(np.abs(theirs%20-%20ours).max())%2C%200.0)%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(%22the%20largest%20gap%20left%20between%20a%20calibrated%20total%20and%20its%20known%20total%22%2C%20float(np.abs(X.to_numpy().T%20%40%20ours%20-%20totals.to_numpy()).max())%2C%200.0)%5D)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20mo.md(f%22The%20factors%20g%20%3D%20w%20%2F%20d%20run%20from%20**%7Bg.min()%3A.4f%7D%20to%20%7Bg.max()%3A.4f%7D**.%22)%2C%0A%20%20%20%20%20%20%20%20%5D)%0A%0A%20%20%20%20_calibrated()%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%20two%20sets%20of%20weights%20agree%20to%20the%20last%20digit.%20The%20factors%20are%20where%20the%20next%20call%20starts%3A%20a%20survey%20often%20wants%20every%20factor%20kept%20between%20bounds%2C%20so%20that%20no%20household's%20weight%20is%20moved%20too%20far%20from%20its%20design%20weight.%0A%0A%20%20%20%20%23%23%23%20Bounded%20logit%20calibration%3A%20%60e.calibrate(calfun%20%3D%20%22logit%22%2C%20bounds%20%3D%20c(L%2C%20U))%60%20%E2%80%94%20own%20only%0A%0A%20%20%20%20**The%20problem.**%20Keep%20every%20factor%20g%20between%20L%20and%20U%20while%20hitting%20the%20totals.%20The%20*logit*%20distance%20of%20Deville%20and%20S%C3%A4rndal%20does%20it%20by%20shape%3A%20its%20factor%20function%20can't%20leave%20(L%2C%20U)%2C%20whatever%20%CE%BB%20is.%20ReGenesees'%20help%20page%20for%20%60e.calibrate%60%20asks%20for%20finite%20bounds%20when%20the%20distance%20is%20%60%22logit%22%60.%0A%0A%20%20%20%20%60%60%60%0A%20%20%20%20g(u)%20%20%3D%20L%20%2B%20(U%20%E2%88%92%20L)%20%C2%B7%20expit(A%C2%B7u%20%2B%20c)%2C%20%20%20u%20%3D%20x%E1%B5%80%CE%BB%0A%20%20%20%20A%20%20%20%20%20%3D%20(U%20%E2%88%92%20L)%20%2F%20((1%20%E2%88%92%20L)(U%20%E2%88%92%201))%2C%20%20%20%20c%20%3D%20log((1%20%E2%88%92%20L)%20%2F%20(U%20%E2%88%92%201))%20%20%20%20%20so%20that%20g(0)%20%3D%201%0A%20%20%20%20solve%20%CE%A3%20d%20%C2%B7%20g(x%E1%B5%80%CE%BB)%20%C2%B7%20x%20%3D%20T%20%20for%20%CE%BB%2C%20by%20Newton's%20method%3A%0A%20%20%20%20%20%20%20%20%CE%BB%20%E2%86%90%20%CE%BB%20%2B%20(%CE%A3%20d%20%C2%B7%20g'(x%E1%B5%80%CE%BB)%20%C2%B7%20x%20x%E1%B5%80)%E2%81%BB%C2%B9%20%C2%B7%20(T%20%E2%88%92%20%CE%A3%20d%20%C2%B7%20g(x%E1%B5%80%CE%BB)%20%C2%B7%20x)%2C%20%20with%20g'%20%3D%20(U%20%E2%88%92%20L)%C2%B7A%C2%B7s(1%20%E2%88%92%20s)%2C%20s%20%3D%20expit(A%C2%B7u%20%2B%20c)%0A%20%20%20%20%60%60%60%0A%0A%20%20%20%20%60expit(v)%20%3D%201%20%2F%20(1%20%2B%20e%5E(%E2%88%92v))%60%20is%20scipy's%20%60special.expit%60.%20The%20linear%20solve%20of%20Exercise%206.9%20is%20Newton's%20first%20step%20from%20%CE%BB%20%3D%200%3B%20the%20logit%20distance%20takes%20a%20few%20more%2C%20halving%20a%20step%20that%20would%20make%20the%20gap%20larger.%20svy%200.36.0%20has%20no%20logit%20distance%2C%20and%20its%20%60calibrate(bounds%3D)%60%20answers%20%60%5BNOT_SUPPORTED%5D%60%3B%20the%20course%20found%20no%20maintained%20Python%20package%20that%20has%20either%20(November%202026).%20So%20the%20function%20below%20stands%20alone%2C%20and%20its%20test%20checks%20it%20against%20answers%20known%20in%20advance%20(%60tests%2Ftest_estimates.py%60).%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20logit_calibration(d%2C%20X%2C%20totals%2C%20low%3A%20float%2C%20high%3A%20float%2C%20tol%3A%20float%20%3D%201e-10%2C%20max_iter%3A%20int%20%3D%20100)%20-%3E%20np.ndarray%3A%0A%20%20%20%20%22%22%22Weights%20calibrated%20to%20the%20totals%20with%20the%20logit%20distance%2C%20every%20factor%20w%20%2F%20d%20strictly%20between%20low%20and%20high%0A%20%20%20%20(low%20%3C%201%20%3C%20high).%20Newton's%20method%20with%20step%20halving%3B%20raises%20if%20it%20does%20not%20converge%2C%20which%20happens%20when%20the%20bounds%0A%20%20%20%20are%20too%20tight%20for%20the%20totals.%22%22%22%0A%20%20%20%20if%20not%20low%20%3C%201%20%3C%20high%3A%0A%20%20%20%20%20%20%20%20raise%20ValueError(f%22the%20bounds%20must%20hold%201%20between%20them%2C%20got%20(%7Blow%7D%2C%20%7Bhigh%7D)%22)%0A%20%20%20%20d%2C%20X%2C%20totals%20%3D%20np.asarray(d%2C%20dtype%3Dfloat)%2C%20np.asarray(X%2C%20dtype%3Dfloat)%2C%20np.asarray(totals%2C%20dtype%3Dfloat)%0A%20%20%20%20A%20%3D%20(high%20-%20low)%20%2F%20((1%20-%20low)%20*%20(high%20-%201))%0A%20%20%20%20shift%20%3D%20np.log((1%20-%20low)%20%2F%20(high%20-%201))%0A%0A%20%20%20%20def%20g(u)%3A%0A%20%20%20%20%20%20%20%20return%20low%20%2B%20(high%20-%20low)%20*%20special.expit(A%20*%20u%20%2B%20shift)%0A%0A%20%20%20%20def%20slope(u)%3A%0A%20%20%20%20%20%20%20%20s%20%3D%20special.expit(A%20*%20u%20%2B%20shift)%0A%20%20%20%20%20%20%20%20return%20(high%20-%20low)%20*%20A%20*%20s%20*%20(1%20-%20s)%0A%0A%20%20%20%20lam%20%3D%20np.zeros(X.shape%5B1%5D)%0A%20%20%20%20gap%20%3D%20totals%20-%20X.T%20%40%20(d%20*%20g(X%20%40%20lam))%0A%20%20%20%20for%20_%20in%20range(max_iter)%3A%0A%20%20%20%20%20%20%20%20if%20np.abs(gap).max()%20%3C%3D%20tol%20*%20np.abs(totals).max()%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20d%20*%20g(X%20%40%20lam)%0A%20%20%20%20%20%20%20%20try%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20step%20%3D%20np.linalg.solve(X.T%20%40%20(X%20*%20(d%20*%20slope(X%20%40%20lam))%5B%3A%2C%20None%5D)%2C%20gap)%0A%20%20%20%20%20%20%20%20except%20np.linalg.LinAlgError%3A%20%20%23%20every%20factor%20pressed%20against%20a%20bound%3A%20no%20step%20can%20close%20the%20gap%0A%20%20%20%20%20%20%20%20%20%20%20%20break%0A%20%20%20%20%20%20%20%20size%20%3D%201.0%0A%20%20%20%20%20%20%20%20while%20True%3A%20%20%23%20halve%20a%20step%20that%20would%20make%20the%20gap%20larger%0A%20%20%20%20%20%20%20%20%20%20%20%20trial%20%3D%20lam%20%2B%20size%20*%20step%0A%20%20%20%20%20%20%20%20%20%20%20%20trial_gap%20%3D%20totals%20-%20X.T%20%40%20(d%20*%20g(X%20%40%20trial))%0A%20%20%20%20%20%20%20%20%20%20%20%20if%20np.abs(trial_gap).max()%20%3C%20np.abs(gap).max()%20or%20size%20%3C%201e-8%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20break%0A%20%20%20%20%20%20%20%20%20%20%20%20size%20%2F%3D%202%0A%20%20%20%20%20%20%20%20lam%2C%20gap%20%3D%20trial%2C%20trial_gap%0A%20%20%20%20raise%20RuntimeError(f%22logit%20calibration%20did%20not%20converge%20in%20%7Bmax_iter%7D%20steps%3A%20the%20bounds%20(%7Blow%7D%2C%20%7Bhigh%7D)%20may%20be%20too%20tight%20for%20the%20totals%22)%0A%0A%0A%40app.cell%0Adef%20_(frame)%3A%0A%20%20%20%20%23%20The%20survey's%20calibration%20with%20every%20factor%20between%200.98%20and%201.17%3A%20what%20svy%20says%2C%20and%20the%20own%20function%0A%20%20%20%20_d%2C%20_X%2C%20_totals%20%3D%20calibration_problem(frame)%0A%20%20%20%20_start%20%3D%20svy.Sample(data%3Dpl.from_pandas(frame)%2C%20design%3Dsurvey_design(%22sel_design_weight%22))%0A%20%20%20%20_refused%20%3D%20svy_error(lambda%3A%20_start.weighting.calibrate(controls%3D%7Bsvy.Cat(%22province%22)%3A%20_totals.drop(%22hh_size%22).to_dict()%2C%20%22hh_size%22%3A%20_totals%5B%22hh_size%22%5D%7D%2C%20bounds%3D(0.98%2C%201.17)))%0A%20%20%20%20_bounded%20%3D%20logit_calibration(_d%2C%20_X%2C%20_totals%2C%200.98%2C%201.17)%0A%20%20%20%20_linear%20%3D%20attempt(linear_calibration%2C%20_d%2C%20_X%2C%20_totals)%0A%20%20%20%20_rows%20%3D%20%5B(%22logit%2C%20bounds%200.98%20and%201.17%22%2C%20_bounded%20%2F%20_d)%5D%20%2B%20(%5B%5D%20if%20_linear%20is%20NOT_YET%20else%20%5B(%22linear%20(Exercise%206.9)%22%2C%20_linear%20%2F%20_d)%5D)%0A%20%20%20%20mo.vstack(%5B%0A%20%20%20%20%20%20%20%20mo.md(f%22**svy%2C%20asked%20for%20bounds%3A**%5Cn%5Cn%60%60%60%5Cn%7B_refused%7D%5Cn%60%60%60%5Cn%5Cn**The%20own%20function%3A**%22)%2C%0A%20%20%20%20%20%20%20%20pd.DataFrame(%5B%7B%22distance%22%3A%20label%2C%20%22smallest%20factor%22%3A%20g.min()%2C%20%22largest%20factor%22%3A%20g.max()%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%22largest%20gap%20to%20a%20known%20total%22%3A%20float(np.abs(_X.to_numpy().T%20%40%20(g%20*%20_d)%20-%20_totals.to_numpy()).max())%7D%20for%20label%2C%20g%20in%20_rows%5D).round(6)%2C%0A%20%20%20%20%5D)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20The%20linear%20factors%20reach%20past%201.17%3B%20the%20logit%20factors%20stop%20short%20of%20it%20and%20still%20hit%20every%20total.%20Bounds%20that%20are%20too%20tight%20have%20no%20solution%3A%20the%20function%20then%20raises%20and%20says%20so%2C%20rather%20than%20return%20weights%20that%20miss%20the%20totals.%0A%0A%20%20%20%20%23%23%23%20One%20weight%20per%20cluster%3A%20%60e.calibrate(...%2C%20aggregate.stage%20%3D%201)%60%20%E2%80%94%20own%20only%0A%0A%20%20%20%20**The%20problem.**%20Calibrating%20persons%20to%20known%20totals%20of%20persons%20gives%20each%20member%20of%20a%20household%20its%20own%20factor%2C%20and%20the%20household%20ends%20up%20with%20several%20weights.%20Surveys%20often%20want%20one%20weight%20per%20household%20(*integrated%20weighting*)%2C%20so%20that%20a%20household's%20members%20add%20up%20to%20the%20household.%20ReGenesees'%20%60aggregate.stage%60%20asks%20for%20calibrated%20weights%20that%20are%20constant%20within%20the%20units%20of%20a%20stage%3B%20its%20help%20page%20allows%20it%20only%20when%20the%20starting%20weights%20already%20are.%20The%20method%2C%20as%20R's%20survey%20package%20applies%20it%3A%20replace%20each%20row's%20x%20and%20starting%20weight%20by%20their%20**means%20over%20its%20cluster**%2C%20then%20calibrate%20as%20usual.%20Rows%20of%20one%20cluster%20then%20have%20the%20same%20x%2C%20so%20they%20get%20the%20same%20factor%3B%20and%20since%20a%20cluster's%20mean%20times%20its%20size%20is%20its%20sum%2C%20the%20totals%20still%20hold%20for%20the%20original%20x.%0A%0A%20%20%20%20%60%60%60%0A%20%20%20%20x%CC%84_c%20%3D%20mean%20of%20x%20over%20cluster%20c%2C%20%20%20d%CC%84_c%20%3D%20mean%20of%20d%20over%20cluster%20c%20(d%20should%20already%20be%20equal%20within%20c)%0A%20%20%20%20w%20%20%20%3D%20d%CC%84_c%20%C2%B7%20(1%20%2B%20x%CC%84_c%E1%B5%80%CE%BB)%2C%20%20%20%CE%BB%20%3D%20(%CE%A3%20d%CC%84%20%C2%B7%20x%CC%84%20x%CC%84%E1%B5%80)%E2%81%BB%C2%B9%20%C2%B7%20(T%20%E2%88%92%20%CE%A3%20d%CC%84%20%C2%B7%20x%CC%84)%0A%20%20%20%20%60%60%60%0A%0A%20%20%20%20svy%200.36.0%20has%20no%20such%20option%2C%20and%20the%20course%20found%20no%20maintained%20Python%20package%20that%20has%20one%20(November%202026).%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20aggregate_stage_calibration(d%2C%20X%2C%20totals%2C%20cluster)%20-%3E%20np.ndarray%3A%0A%20%20%20%20%22%22%22Linear%20calibration%20with%20one%20weight%20per%20cluster%3A%20x%20and%20the%20starting%20weights%20averaged%20within%20each%20cluster%2C%20then%20the%0A%20%20%20%20linear%20solve.%20The%20starting%20weights%20should%20already%20be%20equal%20within%20each%20cluster%2C%20as%20ReGenesees%20requires.%22%22%22%0A%20%20%20%20clusters%20%3D%20np.asarray(cluster)%0A%20%20%20%20X_bar%20%3D%20pd.DataFrame(np.asarray(X%2C%20dtype%3Dfloat)).groupby(clusters).transform(%22mean%22).to_numpy()%0A%20%20%20%20d_bar%20%3D%20pd.Series(np.asarray(d%2C%20dtype%3Dfloat)).groupby(clusters).transform(%22mean%22).to_numpy()%0A%20%20%20%20totals%20%3D%20np.asarray(totals%2C%20dtype%3Dfloat)%0A%20%20%20%20lam%20%3D%20np.linalg.solve(X_bar.T%20%40%20(X_bar%20*%20d_bar%5B%3A%2C%20None%5D)%2C%20totals%20-%20X_bar.T%20%40%20d_bar)%0A%20%20%20%20return%20d_bar%20*%20(1%20%2B%20X_bar%20%40%20lam)%0A%0A%0A%40app.cell%0Adef%20_(frame)%3A%0A%20%20%20%20%23%20The%20households'%20design%20weights%20calibrated%20with%20one%20weight%20per%20area%20(PSU)%3A%20each%20area's%20households%20keep%20one%20weight%0A%20%20%20%20_d%2C%20_X%2C%20_totals%20%3D%20calibration_problem(frame)%0A%20%20%20%20_one_per_area%20%3D%20aggregate_stage_calibration(_d%2C%20_X%2C%20_totals%2C%20frame%5B%22psu_id%22%5D)%0A%20%20%20%20_linear%20%3D%20attempt(linear_calibration%2C%20_d%2C%20_X%2C%20_totals)%0A%20%20%20%20_rows%20%3D%20%5B(%22one%20weight%20per%20area%22%2C%20_one_per_area)%5D%20%2B%20(%5B%5D%20if%20_linear%20is%20NOT_YET%20else%20%5B(%22linear%2C%20household%20by%20household%22%2C%20_linear)%5D)%0A%20%20%20%20pd.DataFrame(%5B%7B%0A%20%20%20%20%20%20%20%20%22calibration%22%3A%20label%2C%0A%20%20%20%20%20%20%20%20%22areas%20with%20more%20than%20one%20weight%22%3A%20int(pd.Series(w).round(9).groupby(frame%5B%22psu_id%22%5D.to_numpy()).nunique().gt(1).sum())%2C%0A%20%20%20%20%20%20%20%20%22largest%20gap%20to%20a%20known%20total%22%3A%20float(np.abs(_X.to_numpy().T%20%40%20w%20-%20_totals.to_numpy()).max())%2C%0A%20%20%20%20%7D%20for%20label%2C%20w%20in%20_rows%5D)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20The%20area%20is%20the%20cluster%20here%20because%20the%20survey's%20design%20weights%20are%20equal%20within%20an%20area%3B%20for%20persons%20in%20households%2C%20the%20household%20is%20the%20cluster%20and%20the%20members%20are%20the%20rows.%20Both%20calibrations%20hit%20every%20total%3B%20only%20the%20clustered%20one%20leaves%20each%20area%20with%20one%20weight.%0A%0A%20%20%20%20%23%23%20What%20you%20can%20do%20now%0A%0A%20%20%20%20%7C%20to%20%7C%20in%20svy%20%7C%20in%20your%20own%20code%20%7C%20and%20check%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20declare%20a%20design%20%7C%20%60svy.Design(stratum%3D%2C%20psu%3D%2C%20wgt%3D%2C%20pop_size%3D%2C%20singleton%3D)%60%2C%20%60svy.Sample%60%20%7C%20%60design_rows%60%2C%20%60population_counts%60%20%7C%20rows%20with%20a%20weight%2C%20a%20stratum%20and%20a%20PSU%3B%20lonely%20PSUs%20found%20after%2C%20and%20a%20rule%20chosen%3B%20PSU%20counts%2C%20not%20fractions%3B%20what%20the%20cluster%20is%20%7C%0A%20%20%20%20%7C%20estimate%20totals%2C%20means%2C%20proportions%20%7C%20%60estimation.total%60%2C%20%60.mean%60%2C%20%60.prop%60%20%7C%20the%20stratified%20formula%3B%20%60taylor_variance%60%20of%20a%20linearized%20z%20%7C%20SE%2C%20CV%2C%20CI%20on%20the%20design's%20df%20%7C%0A%20%20%20%20%7C%20estimate%20for%20a%20domain%20%7C%20%60by%3D%60%2C%20%60where%3D%60%20%7C%20z%20%3D%200%20outside%20the%20domain%2C%20on%20every%20row%20%7C%20never%20a%20filter%20first%20%7C%0A%20%20%20%20%7C%20summarize%20weighted%20and%20unweighted%2C%20at%20three%20levels%20%7C%20%60survey_design()%60%20for%20the%20weighted%20figures%3B%20pandas%20for%20the%20sample's%20sums%20and%20counts%20%7C%20%60own_side_by_side%60%3A%20%60taylor_variance%60%20and%20plain%20sums%20%7C%20each%20estimate%20beside%20the%20interviews%20behind%20it%3B%20the%20two%20joined%20on%20the%20domain's%20columns%20%7C%0A%20%20%20%20%7C%20estimate%20every%20cell%20of%20a%20cross%20table%20%7C%20%60by%3D%5B...%5D%60%20with%20two%20or%20three%20columns%20%7C%20%60domain_cells%60%3A%20the%20grid%20of%20every%20combination%20%7C%20an%20empty%20cell%20is%200%2C%20with%20no%20CV%20%7C%0A%20%20%20%20%7C%20compare%20with%20simple%20random%20sampling%20%7C%20%60deff%3D%22wor%22%60%20%7C%20%60domain_design_effect%60%20%7C%20a%20domain's%20deff%20is%20within%20the%20domain%20%7C%0A%20%20%20%20%7C%20ratios%2C%20share%20ratios%2C%20regressions%20%7C%20%60ratio%60%2C%20%60contrast%60%2C%20%60glm.fit%60%20%7C%20linearized%20variables%20%7C%20the%20SE%20of%20a%20ratio%20is%20not%20a%20ratio%20of%20SEs%20%7C%0A%20%20%20%20%7C%20functions%20of%20totals%20of%20several%20variables%20%7C%20a%20linearized%20column%20to%20%60total%60%20%7C%20%60linearized_estimate%60%20%7C%20own%20only%2C%20in%20general%20%7C%0A%20%20%20%20%7C%20calibrate%20%7C%20%60weighting.calibrate%60%20%7C%20%60linear_calibration%60%2C%20%60logit_calibration%60%2C%20%60aggregate_stage_calibration%60%20%7C%20totals%20hit%3B%20factors%20within%20bounds%3B%20one%20weight%20per%20cluster%20%7C%0A%0A%20%20%20%20Next%2C%20module%207%20(%6060_publish_tables.py%60)%20writes%20the%20national%2C%20regional%20and%20provincial%20tables%2C%20with%20their%20SEs%2C%20CVs%20and%20the%20sample's%20counts%2C%20to%20a%20workbook%20with%20a%20notes%20sheet%2C%20and%20runs%20the%20tabulation%20as%20one%20script.%20For%20the%20theory%20under%20this%20module's%20formulas%2C%20primer%20%5BP2%2C%20survey%20sampling%20in%20one%20page%5D(%2F%3Ffile%3D96_primer_survey_sampling.py)%3B%20for%20every%20term%2C%20the%20%5Bglossary%5D(%2F%3Ffile%3D97_glossary.py).%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20course.quizzes_at(%226.4%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
f00d72eb1deb79b5737353c03f2e896b