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%22Primer%3A%20survey%20sampling%20in%20one%20page%22%2C%0A)%0A%0Awith%20app.setup%3A%0A%20%20%20%20%23%20The%20setup%20cell%20runs%20first.%20It%20holds%20the%20imports%20every%20other%20cell%20uses.%0A%20%20%20%20import%20warnings%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%20stats%20%20%23%20scipy%20comes%20with%20svy%3B%20stats.t%20gives%20the%20t%20distribution's%20quantiles%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%0A%40app.function%0Adef%20show(example%2C%20lines%3A%20int%20%3D%201)%20-%3E%20mo.Html%3A%0A%20%20%20%20%22%22%22Run%20a%20small%20example%20and%20show%20what%20happened%3A%20every%20warning%20it%20raised%20(the%20code%20ran)%2C%20then%20either%20what%20it%20gave%20back%0A%20%20%20%20or%20the%20error%20that%20stopped%20it%2C%20in%20its%20first%20%60lines%60%20lines.%22%22%22%0A%20%20%20%20with%20warnings.catch_warnings(record%3DTrue)%20as%20caught%3A%0A%20%20%20%20%20%20%20%20warnings.simplefilter(%22always%22)%0A%20%20%20%20%20%20%20%20try%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20result%2C%20error%20%3D%20example()%2C%20None%0A%20%20%20%20%20%20%20%20except%20Exception%20as%20exc%3A%20%20%23%20the%20error%20is%20the%20lesson%3A%20show%20it%20rather%20than%20stop%20the%20notebook%0A%20%20%20%20%20%20%20%20%20%20%20%20result%2C%20error%20%3D%20None%2C%20exc%0A%20%20%20%20parts%20%3D%20%5B%5D%0A%20%20%20%20for%20warning%20in%20caught%3A%0A%20%20%20%20%20%20%20%20first%20%3D%20str(warning.message).strip().splitlines()%5B0%5D%0A%20%20%20%20%20%20%20%20parts.append(mo.callout(mo.md(f%22**Warning%3A%20%7Bwarning.category.__name__%7D.**%20The%20code%20ran%2C%20but%20the%20library%20says%3A%5Cn%5Cn%60%60%60text%5Cn%7Bfirst%7D%5Cn%60%60%60%22)%2C%20kind%3D%22warn%22))%0A%20%20%20%20if%20error%20is%20not%20None%3A%0A%20%20%20%20%20%20%20%20said%20%3D%20%5Bline.rstrip()%20for%20line%20in%20str(error).strip().splitlines()%20if%20line.strip()%5D%5B%3Alines%5D%20or%20%5Btype(error).__name__%5D%0A%20%20%20%20%20%20%20%20parts.append(mo.callout(mo.md(f%22**Error%3A%20%7Btype(error).__name__%7D.**%20The%20code%20stopped%20here%3A%5Cn%5Cn%60%60%60text%5Cn%22%20%2B%20%22%5Cn%22.join(said)%20%2B%20%22%5Cn%60%60%60%22)%2C%20kind%3D%22danger%22))%0A%20%20%20%20elif%20isinstance(result%2C%20(pd.DataFrame%2C%20pd.Series%2C%20pl.DataFrame))%3A%0A%20%20%20%20%20%20%20%20parts.append(result)%0A%20%20%20%20else%3A%0A%20%20%20%20%20%20%20%20parts.append(mo.md(f%22It%20gave%20back%3A%20%60%7Bresult!r%7D%60%22))%0A%20%20%20%20return%20mo.vstack(parts)%0A%0A%0A%40app.function%0Adef%20toy_sample()%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22Eighteen%20households%20of%20one%20province%2C%20drawn%20the%20way%20the%20survey%20was%3A%20every%20household%20on%20the%20register%20of%20large%0A%20%20%20%20units%20(stratum%20F)%2C%204%20of%20the%2010%20areas%20where%20at%20least%20half%20the%20households%20fish%20(A)%2C%202%20of%20the%2010%20where%20a%20tenth%20to%20a%20half%0A%20%20%20%20fish%20(B)%20and%203%20of%20the%2045%20where%20fewer%20fish%20(C)%2C%20every%20fishing%20household%20of%20a%20drawn%20area%20interviewed.%20%60members%60%20is%20the%0A%20%20%20%20household's%20number%20of%20members.%22%22%22%0A%20%20%20%20rows%20%3D%20%5B%0A%20%20%20%20%20%20%20%20%23%20stratum%2C%20psu%2C%20psu_population%2C%20household%2C%20members%0A%20%20%20%20%20%20%20%20(%22F%20listed%20large%20units%22%2C%20%22L1%22%2C%203%2C%20%22H01%22%2C%206)%2C%0A%20%20%20%20%20%20%20%20(%22F%20listed%20large%20units%22%2C%20%22L2%22%2C%203%2C%20%22H02%22%2C%209)%2C%0A%20%20%20%20%20%20%20%20(%22F%20listed%20large%20units%22%2C%20%22L3%22%2C%203%2C%20%22H03%22%2C%204)%2C%0A%20%20%20%20%20%20%20%20(%22A%20half%20or%20more%20fish%22%2C%20%221002%22%2C%2010%2C%20%22H04%22%2C%205)%2C%0A%20%20%20%20%20%20%20%20(%22A%20half%20or%20more%20fish%22%2C%20%221002%22%2C%2010%2C%20%22H05%22%2C%203)%2C%0A%20%20%20%20%20%20%20%20(%22A%20half%20or%20more%20fish%22%2C%20%221005%22%2C%2010%2C%20%22H06%22%2C%207)%2C%0A%20%20%20%20%20%20%20%20(%22A%20half%20or%20more%20fish%22%2C%20%221005%22%2C%2010%2C%20%22H07%22%2C%204)%2C%0A%20%20%20%20%20%20%20%20(%22A%20half%20or%20more%20fish%22%2C%20%221007%22%2C%2010%2C%20%22H08%22%2C%202)%2C%0A%20%20%20%20%20%20%20%20(%22A%20half%20or%20more%20fish%22%2C%20%221007%22%2C%2010%2C%20%22H09%22%2C%206)%2C%0A%20%20%20%20%20%20%20%20(%22A%20half%20or%20more%20fish%22%2C%20%221008%22%2C%2010%2C%20%22H10%22%2C%205)%2C%0A%20%20%20%20%20%20%20%20(%22B%20a%20tenth%20to%20a%20half%22%2C%20%222013%22%2C%2010%2C%20%22H11%22%2C%204)%2C%0A%20%20%20%20%20%20%20%20(%22B%20a%20tenth%20to%20a%20half%22%2C%20%222013%22%2C%2010%2C%20%22H12%22%2C%206)%2C%0A%20%20%20%20%20%20%20%20(%22B%20a%20tenth%20to%20a%20half%22%2C%20%222031%22%2C%2010%2C%20%22H13%22%2C%203)%2C%0A%20%20%20%20%20%20%20%20(%22C%20under%20a%20tenth%22%2C%20%223012%22%2C%2045%2C%20%22H14%22%2C%204)%2C%0A%20%20%20%20%20%20%20%20(%22C%20under%20a%20tenth%22%2C%20%223012%22%2C%2045%2C%20%22H15%22%2C%206)%2C%0A%20%20%20%20%20%20%20%20(%22C%20under%20a%20tenth%22%2C%20%223077%22%2C%2045%2C%20%22H16%22%2C%203)%2C%0A%20%20%20%20%20%20%20%20(%22C%20under%20a%20tenth%22%2C%20%223140%22%2C%2045%2C%20%22H17%22%2C%205)%2C%0A%20%20%20%20%20%20%20%20(%22C%20under%20a%20tenth%22%2C%20%223140%22%2C%2045%2C%20%22H18%22%2C%208)%2C%0A%20%20%20%20%5D%0A%20%20%20%20toy%20%3D%20pd.DataFrame(rows%2C%20columns%3D%5B%22stratum%22%2C%20%22psu%22%2C%20%22psu_population%22%2C%20%22household%22%2C%20%22members%22%5D)%0A%20%20%20%20drawn%20%3D%20toy.groupby(%22stratum%22)%5B%22psu%22%5D.transform(%22nunique%22)%20%20%23%20the%20areas%20drawn%20in%20each%20stratum%0A%20%20%20%20toy.insert(3%2C%20%22weight%22%2C%20toy%5B%22psu_population%22%5D%20%2F%20drawn)%20%20%23%20the%20design%20weight%3A%20N%20%C3%B7%20n%0A%20%20%20%20return%20toy%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%20Primer%3A%20survey%20sampling%20in%20one%20page%0A%0A%20%20%20%20This%20primer%20is%20for%20you%20if%20module%206's%20words%20(strata%2C%20PSUs%2C%20the%20finite%20population%20correction%2C%20a%20standard%20error%20under%20the%20design)%20are%20new%2C%20or%20if%20you%20learned%20them%20long%20ago.%20It%20explains%20each%20with%20one%20small%20sample%20you%20can%20see%20whole%2C%20computes%20its%20estimate%20and%20standard%20error%20twice%2C%20with%20svy%20and%20with%20a%20few%20lines%20of%20numpy%2C%20and%20then%20does%20the%20same%20on%20the%20Survey%20of%20Fishing%20Households.%20If%20you%20work%20with%20ReGenesees%20or%20R's%20%60survey%60%20package%20every%20day%2C%20skim%20parts%201%20and%202%20and%20start%20at%20part%203.%0A%0A%20%20%20%20%7C%20part%20%7C%20what%20it%20covers%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%201%20%C2%B7%20A%20sample%20stands%20for%20a%20population%20%7C%20probability%20samples%2C%20the%20design%20and%20why%20it%20enters%20every%20estimate%20%7C%0A%20%20%20%20%7C%202%20%C2%B7%20Strata%2C%20PSUs%20and%20weights%20%7C%20how%20the%20survey%20was%20drawn%2C%20and%20what%20a%20weight%20is%20%7C%0A%20%20%20%20%7C%203%20%C2%B7%20The%20finite%20population%20correction%20%7C%20a%20fraction%20or%20a%20count%20in%20R%2C%20a%20count%20in%20svy%20(exercise%20P2.1)%20%7C%0A%20%20%20%20%7C%204%20%C2%B7%20The%20standard%20error%20of%20a%20stratified%20total%2C%20and%20the%20CV%20%7C%20the%20formula%2C%20worked%20by%20hand%20and%20by%20svy%20(exercise%20P2.2)%20%7C%0A%20%20%20%20%7C%205%20%C2%B7%20Means%20and%20shares%3A%20Taylor%20linearization%20%7C%20why%20a%20mean's%20standard%20error%20is%20a%20total's%20in%20disguise%20%7C%0A%20%20%20%20%7C%206%20%C2%B7%20The%20same%20on%20the%20survey%20%7C%20what%20svy%20refuses%2C%20and%20your%20function%20beside%20svy%20on%20the%20whole%20survey%20%7C%0A%0A%20%20%20%20The%20formulas%20are%20the%20textbook's.%20For%20a%20fuller%20treatment%2C%20with%20proofs%2C%20read%20Sharon%20L.%20Lohr%2C%20*Sampling%3A%20Design%20and%20Analysis*%2C%20on%20stratified%20sampling%2C%20cluster%20sampling%20and%20variance%20estimation%20by%20linearization.%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%201%20%C2%B7%20A%20sample%20stands%20for%20a%20population%0A%0A%20%20%20%20**The%20problem.**%20The%20Survey%20of%20Fishing%20Households%20did%20not%20visit%20every%20fishing%20household.%20It%20visited%20every%20household%20on%20the%20register%20of%20large%20fishing%20units%20and%20the%20fishing%20households%20of%20a%20few%20areas%20drawn%20by%20lot.%20Yet%20its%20tables%20report%20the%20number%20of%20fishing%20households%20and%20their%20members%20for%20whole%20provinces%2C%20each%20with%20a%20margin%20of%20error.%20Two%20questions%20follow%3A%20how%20do%20a%20few%20hundred%20households%20stand%20for%20a%20province%2C%20and%20how%20far%20from%20the%20truth%20might%20the%20estimate%20be%3F%0A%0A%20%20%20%20**The%20answer%2C%20in%20two%20words%3A%20the%20design.**%20A%20**probability%20sample**%20is%20drawn%20by%20a%20rule%20that%20gives%20every%20unit%20of%20the%20population%20a%20known%20chance%20of%20being%20drawn.%20The%20rule%20is%20the%20**sample%20design**.%20Because%20each%20household's%20chance%20is%20known%2C%20each%20can%20be%20counted%20for%20the%20households%20it%20stands%20for%20(its%20**weight**%2C%20part%202).%20Because%20the%20rule%20is%20known%2C%20you%20can%20work%20out%20how%20much%20the%20estimate%20would%20vary%20had%20another%20sample%20been%20drawn%20by%20the%20same%20rule%20(its%20**standard%20error**%2C%20part%204).%20Both%20come%20from%20the%20design%2C%20not%20from%20the%20data%20alone%2C%20which%20is%20why%20every%20survey%20library%20asks%20for%20the%20design%20before%20it%20estimates%20anything.%0A%0A%20%20%20%20%23%23%202%20%C2%B7%20Strata%2C%20PSUs%20and%20weights%0A%0A%20%20%20%20**Strata.**%20The%20population%20is%20first%20divided%20into%20groups%20that%20are%20sampled%20separately%2C%20the%20**strata**%20(one%20is%20a%20**stratum**).%20The%20survey%20has%20four%20strata%20in%20each%20province%3A%0A%0A%20%20%20%20%7C%20stratum%20%7C%20what%20it%20holds%20%7C%20how%20it%20is%20sampled%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20F%20%C2%B7%20registered%20large%20units%20%7C%20the%20households%20that%20run%20a%20fishing%20unit%20on%20the%20register%20of%20large%20units%2C%20listed%20beforehand%20%7C%20every%20one%20is%20taken%20%7C%0A%20%20%20%20%7C%20A%20%C2%B7%20areas%20where%20at%20least%20half%20the%20households%20fish%20%7C%20enumeration%20areas%20%7C%20drawn%20at%20the%20highest%20rate%20%7C%0A%20%20%20%20%7C%20B%20%C2%B7%20areas%20where%20a%20tenth%20to%20a%20half%20fish%20%7C%20enumeration%20areas%20%7C%20drawn%20at%20a%20lower%20rate%20%7C%0A%20%20%20%20%7C%20C%20%C2%B7%20areas%20where%20fewer%20than%20a%20tenth%20fish%20%7C%20enumeration%20areas%20%7C%20drawn%20at%20the%20lowest%20rate%20%7C%0A%0A%20%20%20%20Stratifying%20makes%20the%20estimate%20more%20precise%3A%20each%20stratum's%20share%20of%20the%20sample%20is%20fixed%20by%20the%20design%2C%20not%20left%20to%20chance%2C%20so%20only%20the%20variation%20**within**%20strata%20adds%20to%20the%20standard%20error.%20A%20stratum%20taken%20whole%20adds%20none.%0A%0A%20%20%20%20**Primary%20sampling%20units.**%20In%20strata%20A%2C%20B%20and%20C%20the%20lottery%20draws%20**areas**%2C%20not%20households.%20The%20unit%20drawn%20at%20the%20first%20stage%20is%20the%20**primary%20sampling%20unit**%2C%20the%20**PSU**%3B%20in%20the%20survey%20it%20is%20the%20enumeration%20area%20(column%20%60sel_ea%60)%2C%20and%20every%20fishing%20household%20of%20a%20drawn%20area%20is%20interviewed.%20A%20sample%20of%20areas%20is%20a%20**cluster%20sample**%3A%20households%20of%20one%20area%20resemble%20each%20other%20(the%20same%20waters%2C%20the%20same%20market)%2C%20so%20twelve%20areas%20tell%20you%20less%20than%20twelve%20households%20drawn%20one%20by%20one%20from%20the%20whole%20province%20would.%20The%20standard%20error%20must%20therefore%20be%20computed%20from%20the%20variation%20**between%20areas**%2C%20never%20from%20the%20variation%20between%20households.%20In%20stratum%20F%2C%20where%20nothing%20is%20drawn%2C%20%60sel_ea%60%20is%20blank%20and%20each%20household%20is%20its%20own%20PSU.%0A%0A%20%20%20%20**Weights.**%20A%20household's%20**design%20weight**%20is%20the%20inverse%20of%20its%20chance%20of%20being%20drawn%3A%20if%204%20of%2010%20areas%20are%20drawn%2C%20each%20household%20of%20a%20drawn%20area%20stands%20for%2010%20%C3%B7%204%20%3D%202.5%20households.%20The%20survey%20then%20adjusts%20the%20design%20weight%20(%60sel_design_weight%60)%20for%20the%20households%20of%20the%20area%20that%20did%20not%20respond%20(%60sel_nr_weight%60)%20and%20calibrates%20it%20to%20each%20province's%20listed%20fishing%20households%20by%20scale%20(%60sel_final_weight%60)%3B%20%60sel_final_weight%60%20is%20the%20weight%20for%20estimates.%20The%20sum%20of%20the%20weights%20estimates%20the%20number%20of%20households%20in%20the%20population.%0A%0A%20%20%20%20The%20survey's%20design%20weights%2C%20by%20stratum%2C%20beside%20the%20counts%20of%20areas%20in%20the%20frame%20and%20in%20the%20sample%20(%60data%2Fstratum_counts.csv%60%3B%20in%20stratum%20F%20its%20counts%20are%20of%20households%2C%20each%20its%20own%20PSU)%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%20households%20%3D%20course.households()%0A%20%20%20%20counts%20%3D%20pd.read_csv(course.PATHS.stratum_counts)%0A%20%20%20%20by_stratum%20%3D%20households.groupby(%22sel_stratum%22).agg(%0A%20%20%20%20%20%20%20%20households%3D(%22hh_id%22%2C%20%22size%22)%2C%0A%20%20%20%20%20%20%20%20sel_design_weight%3D(%22sel_design_weight%22%2C%20%22first%22)%2C%0A%20%20%20%20%20%20%20%20estimated_households%3D(%22sel_final_weight%22%2C%20%22sum%22)%2C%0A%20%20%20%20).join(counts.groupby(%22sel_stratum%22).agg(provinces%3D(%22province_code%22%2C%20%22size%22)%2C%20eas_in_frame%3D(%22eas_in_frame%22%2C%20%22sum%22)%2C%20eas_sampled%3D(%22eas_sampled%22%2C%20%22sum%22)))%0A%20%20%20%20by_stratum.assign(frame_over_sample%3Dlambda%20t%3A%20t%5B%22eas_in_frame%22%5D%20%2F%20t%5B%22eas_sampled%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%20In%20strata%20A%2C%20B%20and%20C%2C%20%60sel_design_weight%60%20is%20the%20number%20of%20areas%20in%20the%20frame%20divided%20by%20the%20number%20drawn%2C%20as%20above.%20The%20rest%20of%20this%20primer%20works%20on%20a%20sample%20small%20enough%20to%20read%20whole%3A%20eighteen%20households%20of%20one%20imaginary%20province%2C%20drawn%20the%20same%20way.%20Its%20%60weight%60%20column%20is%20the%20design%20weight%2C%20N%20%C3%B7%20n%20in%20each%20stratum.%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%20toy%20%3D%20toy_sample()%0A%20%20%20%20toy%0A%20%20%20%20return%20(toy%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(toy)%3A%0A%20%20%20%20_total%20%3D%20(toy%5B%22weight%22%5D%20*%20toy%5B%22members%22%5D).sum()%0A%20%20%20%20_c_weight%20%3D%20toy.loc%5Btoy%5B%22stratum%22%5D.str.startswith(%22C%22)%2C%20%22weight%22%5D.iloc%5B0%5D%0A%20%20%20%20_c%20%3D%20toy%5Btoy%5B%22stratum%22%5D.str.startswith(%22C%22)%5D%0A%20%20%20%20mo.md(rf%22%22%22%0A%20%20%20%20**The%20estimate.**%20Each%20household's%20members%2C%20times%20its%20weight%2C%20summed%3A%20%CE%A3%20w%C2%B7y%20%3D%20**%7B_total%3A%2C.0f%7D**%20members%20in%20the%20province.%20The%20households%20of%20stratum%20C%20count%20most%2C%20each%20standing%20for%20%7B_c_weight%3A%2C.0f%7D%2C%20because%20only%20%7B_c%5B%22psu%22%5D.nunique()%7D%20of%20%7B_c%5B%22psu_population%22%5D.iloc%5B0%5D%7D%20areas%20were%20drawn.%20That%20is%20the%20estimate.%20The%20rest%20of%20the%20primer%20is%20about%20its%20standard%20error.%0A%0A%20%20%20%20%23%23%203%20%C2%B7%20The%20finite%20population%20correction%0A%0A%20%20%20%20**The%20problem.**%20Drawing%204%20areas%20of%2010%20is%20different%20from%20drawing%204%20areas%20of%204%2C000.%20Once%20a%20large%20share%20of%20the%20areas%20is%20in%20the%20sample%2C%20another%20sample%20cannot%20differ%20much%20from%20this%20one%3A%20the%20two%20would%20share%20many%20areas.%20The%20variance%20formula%20starts%20from%20the%20case%20of%20an%20endless%20population%20and%20multiplies%20each%20stratum's%20part%20by%20%241%20-%20n_h%20%2F%20N_h%24%2C%20where%20%24n_h%24%20is%20the%20number%20of%20PSUs%20drawn%20in%20stratum%20%24h%24%20and%20%24N_h%24%20the%20number%20in%20its%20population.%20This%20factor%20is%20the%20**finite%20population%20correction**%2C%20the%20**fpc**.%20It%20is%200%20for%20a%20stratum%20taken%20whole%20(nothing%20left%20to%20chance)%20and%20close%20to%201%20when%20a%20small%20share%20is%20drawn.%0A%0A%20%20%20%20**The%20tool.**%20The%20design%20needs%20%24N_h%24%20for%20each%20stratum.%20R's%20%60svydesign%60%20and%20ReGenesees'%20%60e.svydesign%60%20take%20it%20either%20as%20the%20population%20count%20or%20as%20the%20**sampling%20fraction**%20%24f_h%20%3D%20n_h%20%2F%20N_h%24%2C%20and%20a%20design%20written%20for%20R%20often%20gives%20the%20fraction.%20**svy%20takes%20counts%20only**%3A%20its%20%60pop_size%3D%60%20names%20a%20column%20holding%20%24N_h%24%20on%20every%20row%2C%20constant%20within%20a%20stratum%2C%20and%20a%20value%20below%201%20stops%20it%20with%20an%20error%20that%20says%20so.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(toy)%3A%0A%20%20%20%20%23%20The%20same%20sample%20with%20the%20fpc%20as%20a%20design%20written%20for%20R%20often%20gives%20it%3A%20a%20sampling%20fraction%0A%20%20%20%20as_fractions%20%3D%20toy.assign(fpc%3Dtoy.groupby(%22stratum%22)%5B%22psu%22%5D.transform(%22nunique%22)%20%2F%20toy%5B%22psu_population%22%5D)%0A%20%20%20%20show(lambda%3A%20svy.Sample(%0A%20%20%20%20%20%20%20%20data%3Dpl.from_pandas(as_fractions)%2C%0A%20%20%20%20%20%20%20%20design%3Dsvy.Design(stratum%3D%22stratum%22%2C%20psu%3D%22psu%22%2C%20wgt%3D%22weight%22%2C%20pop_size%3D%22fpc%22)%2C%0A%20%20%20%20).estimation.total(y%3D%22members%22)%2C%20lines%3D5)%0A%20%20%20%20return%20(as_fractions%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%20Read%20it%20from%20the%20top%2C%20as%20svy's%20errors%20are%20written%3A%20the%20first%20line%20names%20the%20problem%20and%20its%20code%2C%20the%20lines%20after%20it%20say%20where%20and%20what%20was%20expected.%20Turning%20a%20fraction%20back%20into%20a%20count%20is%20one%20division%2C%20%24N_h%20%3D%20n_h%20%2F%20f_h%24%2C%20with%20%24n_h%24%20counted%20as%20**distinct%20PSUs**%20in%20the%20stratum.%20Count%20them%20in%20the%20data%20you%20estimate%20from%2C%20not%20in%20the%20sampling%20plan%3A%20then%20svy's%20factor%20%241%20-%20n_h%2FN_h%24%20equals%20R's%20%241%20-%20f_h%24%20even%20in%20a%20stratum%20that%20lost%20an%20area%20or%20gained%20one%2C%20and%20the%20two%20give%20the%20same%20standard%20error.%20A%20fraction%20written%20rounded%2C%20such%20as%200.0667%20for%2010%20of%20150%2C%20gives%20a%20count%20a%20little%20off%20a%20whole%20number%3B%20module%206%20rounds%20it.%0A%0A%20%20%20%20%23%23%23%20Exercise%20P2.1.%20Population%20counts%20from%20sampling%20fractions%0A%0A%20%20%20%20**Why%20it%20matters%3A**%20a%20design%20written%20for%20R%20with%20fractions%20runs%20in%20svy%20only%20once%20each%20fraction%20is%20a%20count.%20Done%20wrong%2C%20svy%20runs%20without%20complaint%20and%20every%20standard%20error%20is%20off.%0A%0A%20%20%20%20**Input%20%E2%86%92%20output%3A**%20a%20sample%20with%20the%20columns%20%60stratum%60%2C%20%60psu%60%20and%20%60fpc%60%20(the%20sampling%20fraction%20of%20PSUs%2C%20the%20same%20on%20every%20row%20of%20a%20stratum)%20%E2%86%92%20a%20column%20named%20%60psu_population%60%2C%20one%20value%20per%20row%2C%20with%20the%20same%20row%20labels%3A%20the%20stratum's%20distinct%20PSUs%20divided%20by%20its%20fraction.%20Some%20rows%20of%20the%20sample%20above%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(as_fractions)%3A%0A%20%20%20%20_a%20%3D%20as_fractions%5Bas_fractions%5B%22stratum%22%5D.str.startswith(%22A%22)%5D%0A%20%20%20%20_c%20%3D%20as_fractions%5Bas_fractions%5B%22stratum%22%5D.str.startswith(%22C%22)%5D.head(1)%0A%20%20%20%20_rows%20%3D%20%22%5Cn%22.join(f%22%7C%20%7Br.stratum%7D%20%7C%20%7Br.psu%7D%20%7C%20%7Br.fpc%3A.4g%7D%20%7C%20%7Br.psu_population%7D%20%7C%22%20for%20r%20in%20pd.concat(%5B_a%2C%20_c%5D).itertuples())%0A%20%20%20%20mo.md(%0A%20%20%20%20%20%20%20%20%22%7C%20stratum%20%7C%20psu%20%7C%20fpc%20%7C%20%E2%86%92%20psu_population%20%7C%5Cn%7C%3A--%7C%3A--%7C--%3A%7C--%3A%7C%5Cn%22%20%2B%20_rows%20%2B%20%22%5Cn%5Cn%22%0A%20%20%20%20%20%20%20%20f%22Stratum%20A%20has%20%7Blen(_a)%7D%20rows%20but%20%7B_a%5B'psu'%5D.nunique()%7D%20distinct%20PSUs%3A%20%7B_a%5B'psu'%5D.nunique()%7D%20%C3%B7%20%7B_a%5B'fpc'%5D.iloc%5B0%5D%3A.4g%7D%20%3D%20%7B_a%5B'psu_population'%5D.iloc%5B0%5D%7D.%20%22%0A%20%20%20%20%20%20%20%20%22PSU%20names%20can%20repeat%20in%20different%20strata%20(area%201002%20of%20one%20stratum%20is%20not%20area%201002%20of%20another).%5Cn%5Cn%22%0A%20%20%20%20%20%20%20%20%22**Tools%3A**%20%60groupby(%5C%22stratum%5C%22)%5B%5C%22psu%5C%22%5D.transform(%5C%22nunique%5C%22)%60%2C%20which%20gives%20each%20row%20its%20stratum's%20count%20of%20distinct%20PSUs%3B%20a%20division%20of%20two%20columns%3B%20%60.rename(%5C%22psu_population%5C%22)%60.%22%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20psu_counts(sample%3A%20pd.DataFrame)%20-%3E%20pd.Series%3A%0A%20%20%20%20%22%22%22Each%20row's%20stratum%20population%20of%20PSUs%2C%20N%20%3D%20n%20%C3%B7%20f%2C%20with%20n%20the%20stratum's%20distinct%20PSUs%20in%20the%20sample.%22%22%22%0A%20%20%20%20%23%20Exercise%20P2.1%3A%20the%20task%20and%20examples%20are%20in%20the%20text%20above%20this%20cell.%0A%20%20%20%20%23%20Step%201%3A%20for%20every%20row%2C%20the%20number%20of%20distinct%20PSUs%20in%20its%20stratum%20(groupby%2C%20then%20transform)%0A%20%20%20%20%23%20Step%202%3A%20divide%20it%20by%20the%20row's%20fpc%0A%20%20%20%20%23%20Step%203%3A%20return%20the%20result%20as%20a%20column%20named%20psu_population%0A%20%20%20%20raise%20NotImplementedError(%22Exercise%20P2.1%22)%0A%0A%0A%40app.function%0Adef%20repeated_names()%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22Ten%20households%20in%20three%20strata%20whose%20PSU%20names%20repeat%20across%20strata%2C%20used%20by%20the%20checks%3A%20a%20stratum%20taken%20whole%0A%20%20%20%20(fraction%201)%2C%20one%20with%20four%20households%20in%20two%20areas%2C%20one%20with%20three%20areas.%22%22%22%0A%20%20%20%20return%20pd.DataFrame(%7B%0A%20%20%20%20%20%20%20%20%22stratum%22%3A%20%5B%22F%22%2C%20%22F%22%2C%20%22A%22%2C%20%22A%22%2C%20%22A%22%2C%20%22A%22%2C%20%22C%22%2C%20%22C%22%2C%20%22C%22%2C%20%22C%22%5D%2C%0A%20%20%20%20%20%20%20%20%22psu%22%3A%20%5B%221%22%2C%20%222%22%2C%20%221%22%2C%20%221%22%2C%20%221%22%2C%20%222%22%2C%20%221%22%2C%20%222%22%2C%20%223%22%2C%20%223%22%5D%2C%0A%20%20%20%20%20%20%20%20%22fpc%22%3A%20%5B1.0%2C%201.0%2C%200.25%2C%200.25%2C%200.25%2C%200.25%2C%200.0125%2C%200.0125%2C%200.0125%2C%200.0125%5D%2C%0A%20%20%20%20%7D%2C%20index%3D%5B10%2C%2011%2C%2012%2C%2013%2C%2014%2C%2015%2C%2016%2C%2017%2C%2018%2C%2019%5D)%0A%0A%0A%40app.function%0Adef%20survey_fractions()%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22The%20survey's%20households%20as%20a%20design%20written%20for%20R%20would%20describe%20them%3A%20strata%2C%20PSUs%20and%20the%20sampling%20fraction%20of%0A%20%20%20%20PSUs%20from%20the%20stratum%20counts%2C%20eas_sampled%20%C3%B7%20eas_in_frame.%22%22%22%0A%20%20%20%20frame%20%3D%20course.design_frame()%0A%20%20%20%20plan%20%3D%20pd.read_csv(course.PATHS.stratum_counts%2C%20dtype%3D%7B%22region_code%22%3A%20str%2C%20%22province_code%22%3A%20str%7D)%0A%20%20%20%20plan%5B%22region_code%22%5D%2C%20plan%5B%22province_code%22%5D%20%3D%20plan%5B%22region_code%22%5D.str.zfill(2)%2C%20plan%5B%22province_code%22%5D.str.zfill(3)%20%20%23%20codes%20as%20text%2C%20as%20module%202%20keeps%20them%0A%20%20%20%20out%20%3D%20frame.merge(plan%5B%5B%22region_code%22%2C%20%22province_code%22%2C%20%22sel_stratum%22%2C%20%22eas_sampled%22%5D%5D%2C%20on%3D%5B%22region_code%22%2C%20%22province_code%22%2C%20%22sel_stratum%22%5D%2C%20how%3D%22left%22%2C%20validate%3D%22many_to_one%22)%0A%20%20%20%20return%20pd.DataFrame(%7B%22stratum%22%3A%20out%5B%22stratum_id%22%5D%2C%20%22psu%22%3A%20out%5B%22psu_id%22%5D%2C%20%22fpc%22%3A%20out%5B%22eas_sampled%22%5D%20%2F%20out%5B%22psus_in_stratum%22%5D%7D)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(as_fractions)%3A%0A%20%20%20%20check_cases(%0A%20%20%20%20%20%20%20%20%22Exercise%20P2.1%22%2C%20psu_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(%22the%20sample%20of%20part%202%2C%20with%20its%20fpc%20as%20a%20fraction%22%2C%20(as_fractions%2C))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22a%20small%20table%3A%20PSU%20names%20that%20repeat%20across%20strata%2C%20a%20stratum%20taken%20whole%2C%20row%20labels%2010%20to%2019%22%2C%20(repeated_names()%2C))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22the%20survey's%20households%20with%20the%20stratum%20counts'%20fractions%22%2C%20(survey_fractions()%2C))%2C%0A%20%20%20%20%20%20%20%20%5D%2C%0A%20%20%20%20%20%20%20%20what%3D%22samples%22%2C%0A%20%20%20%20%20%20%20%20hint%3D%22n%20is%20the%20stratum's%20distinct%20PSUs%2C%20groupby('stratum')%5B'psu'%5D.transform('nunique')%3B%20then%20n%20%C3%B7%20fpc%20on%20every%20row.%22%2C%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(as_fractions)%3A%0A%20%20%20%20%23%20Try%20it%3A%20the%20sample%20of%20part%202.%20Every%20row%20of%20a%20stratum%20should%20get%20the%20same%20count.%0A%20%20%20%20try_it(psu_counts%2C%20as_fractions)%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%20P2.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%2017.5%20for%20stratum%20A%20of%20the%20sample%2C%20not%2010%20%7C%20The%20households%20are%20counted%2C%20not%20the%20distinct%20PSUs%20(7%20%C3%B7%200.4).%20%7C%20step%201%3A%20%60%22nunique%22%60%20on%20%60psu%60%20%7C%0A%20%20%20%20%7C%204%20rows%20instead%20of%2018%20%7C%20The%20counts%20are%20per%20stratum%2C%20from%20%60agg%60%20or%20%60nunique()%60%20after%20%60groupby%60%2C%20not%20one%20per%20row.%20%7C%20step%201%3A%20%60transform(%22nunique%22)%60%20keeps%20every%20row%20%7C%0A%20%20%20%20%7C%20Wrong%20counts%20in%20the%20small%20table%20only%20%7C%20Distinct%20PSUs%20counted%20over%20the%20whole%20table%2C%20so%20%60%221%22%60%20of%20stratum%20A%20and%20%60%221%22%60%20of%20stratum%20C%20are%20one%20PSU.%20%7C%20step%201%3A%20group%20by%20%60stratum%60%20first%20%7C%0A%20%20%20%20%7C%201.6%20or%200.1%20for%20stratum%20A%20%7C%20The%20fraction%20multiplied%20by%20n%2C%20or%20divided%20by%20it.%20%7C%20step%202%3A%20n%20%C3%B7%20fpc%20%7C%0A%20%20%20%20%7C%20The%20values%20are%20right%2C%20the%20check%20red%20%7C%20The%20column%20is%20not%20named%20%60psu_population%60%2C%20or%20the%20row%20labels%20were%20reset.%20%7C%20step%203%3A%20%60.rename(%22psu_population%22)%60%3B%20no%20%60reset_index%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%204%20%C2%B7%20The%20standard%20error%20of%20a%20stratified%20total%2C%20and%20the%20CV%0A%0A%20%20%20%20**The%20problem.**%20The%20estimate%20of%20part%202%20would%20come%20out%20differently%20had%20other%20areas%20been%20drawn.%20The%20**standard%20error**%20(SE)%20measures%20by%20how%20much%2C%20typically%3A%20it%20is%20the%20standard%20deviation%20of%20the%20estimate%20over%20all%20the%20samples%20the%20design%20could%20have%20drawn.%20You%20have%20one%20sample%2C%20so%20the%20SE%20is%20itself%20estimated%20from%20it%2C%20from%20how%20much%20the%20PSUs%20of%20each%20stratum%20differ%20from%20one%20another.%0A%0A%20%20%20%20**The%20formula.**%20Write%20%24t_%7Bhi%7D%24%20for%20the%20**weighted%20total%20of%20PSU%20%24i%24**%20in%20stratum%20%24h%24%2C%20the%20sum%20of%20%24w%20%5Ccdot%20y%24%20over%20its%20households%2C%20and%20%24%5Cbar%20t_h%24%20for%20the%20mean%20of%20those%20totals%20in%20the%20stratum.%20The%20estimated%20total%20is%20the%20sum%20of%20all%20PSU%20totals%2C%20and%20its%20variance%20is%0A%0A%20%20%20%20%24%24%5Chat%20V(%5Chat%20t)%20%3D%20%5Csum_h%20%5Cleft(1%20-%20%5Cfrac%7Bn_h%7D%7BN_h%7D%5Cright)%20%5Cfrac%7Bn_h%7D%7Bn_h%20-%201%7D%20%5Csum_%7Bi%3D1%7D%5E%7Bn_h%7D%20%5Cleft(t_%7Bhi%7D%20-%20%5Cbar%20t_h%5Cright)%5E2%24%24%0A%0A%20%20%20%20Read%20it%20from%20the%20inside%20out%3A%0A%0A%20%20%20%201.%20%24%5Cleft(t_%7Bhi%7D%20-%20%5Cbar%20t_h%5Cright)%5E2%24%3A%20how%20far%20each%20PSU's%20total%20is%20from%20its%20stratum's%20mean.%20Areas%20that%20differ%20a%20lot%20make%20an%20uncertain%20estimate.%0A%20%20%20%202.%20%24%5Cfrac%7Bn_h%7D%7Bn_h%20-%201%7D%20%5Csum_i%24%3A%20those%20squares%20added%20up%20within%20the%20stratum%2C%20scaled%20as%20a%20sample%20variance%20is.%20A%20stratum%20with%20one%20PSU%20has%20nothing%20to%20compare%20it%20with%2C%20and%20%24n_h%20-%201%20%3D%200%24%3A%20that%20is%20the%20**lonely%20PSU**%20problem%20(svy%20stops%20and%20asks%20for%20a%20rule%3B%20%60singleton%3D%22skip%22%60%20leaves%20such%20a%20stratum%20out%20of%20the%20variance%2C%20as%20%60options(survey.lonely.psu%20%3D%20%22remove%22)%60%20does%20in%20R's%20%60survey%60%20and%20%60options(RG.lonely.psu%20%3D%20%22remove%22)%60%20in%20ReGenesees).%0A%20%20%20%203.%20%241%20-%20n_h%2FN_h%24%3A%20the%20fpc%20of%20part%203.%20Stratum%20F%2C%20taken%20whole%2C%20has%20%24n_h%20%3D%20N_h%24%20and%20adds%20nothing.%0A%20%20%20%204.%20%24%5Csum_h%24%3A%20the%20strata%20are%20drawn%20independently%2C%20so%20their%20variances%20add%20up.%0A%0A%20%20%20%20The%20**standard%20error**%20is%20the%20square%20root%2C%20%24%5Cmathrm%7BSE%7D%20%3D%20%5Csqrt%7B%5Chat%20V%7D%24.%20The%20**coefficient%20of%20variation**%20is%20the%20SE%20as%20a%20share%20of%20the%20estimate%2C%20%24%5Cmathrm%7BCV%7D%20%3D%20%5Cmathrm%7BSE%7D%20%2F%20%5Chat%20t%24%3A%20a%20CV%20of%200.05%20says%20the%20estimate%20is%20uncertain%20by%20about%205%25%20of%20itself%2C%20which%20lets%20you%20compare%20a%20total%20of%20thousands%20with%20one%20of%20millions.%20A%20published%20table%20often%20flags%20or%20withholds%20an%20estimate%20whose%20CV%20is%20over%20a%20threshold%3B%20the%20tabulation%20stretch%20S2%20applies%20the%20survey's%20own%20(%5Bits%20CV%20flags%5D(%2F%3Ffile%3D61_stretch_tabulation.py)).%20A%20**95%25%20confidence%20interval**%20is%20%24%5Chat%20t%20%5Cpm%20t_%7B0.975%2C%5C%2Cdf%7D%20%5Ccdot%20%5Cmathrm%7BSE%7D%24%2C%20where%20%24df%24%2C%20the%20design's%20degrees%20of%20freedom%2C%20is%20the%20number%20of%20PSUs%20minus%20the%20number%20of%20strata.%0A%0A%20%20%20%20When%20every%20weight%20in%20a%20stratum%20is%20the%20design%20weight%20%24N_h%20%2F%20n_h%24%2C%20the%20formula%20is%20the%20textbook's%20variance%20of%20a%20stratified%20total%2C%20%24%5Csum_h%20(1%20-%20n_h%2FN_h)%5C%2C%20N_h%5E2%20s_h%5E2%20%2F%20n_h%24%2C%20with%20%24s_h%5E2%24%20the%20sample%20variance%20of%20the%20PSUs'%20unweighted%20totals.%20Computing%20only%20from%20the%20PSU%20totals%2C%20whatever%20happens%20inside%20a%20PSU%2C%20is%20the%20**ultimate%20cluster**%20method%3A%20it%20is%20what%20svy%20and%20R's%20%60survey%60%20do%20when%20a%20design%20names%20PSUs%20and%20strata.%0A%0A%20%20%20%20**By%20hand%2C**%20on%20the%20sample%20of%20part%202.%20First%20each%20PSU's%20weighted%20total%2C%20then%20each%20stratum's%20part%20of%20the%20variance%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(toy)%3A%0A%20%20%20%20w%20%3D%20toy%5B%22weight%22%5D.to_numpy()%0A%20%20%20%20y%20%3D%20toy%5B%22members%22%5D.to_numpy()%0A%20%20%20%20psu_totals%20%3D%20toy.assign(wy%3Dw%20*%20y).groupby(%5B%22stratum%22%2C%20%22psu%22%5D%2C%20as_index%3DFalse).agg(t%3D(%22wy%22%2C%20%22sum%22)%2C%20N%3D(%22psu_population%22%2C%20%22first%22))%0A%0A%20%20%20%20parts%20%3D%20%5B%5D%0A%20%20%20%20for%20name%2C%20stratum%20in%20psu_totals.groupby(%22stratum%22)%3A%0A%20%20%20%20%20%20%20%20t%20%3D%20stratum%5B%22t%22%5D.to_numpy()%20%20%23%20the%20weighted%20totals%20of%20the%20stratum's%20PSUs%0A%20%20%20%20%20%20%20%20n%2C%20N%20%3D%20len(t)%2C%20stratum%5B%22N%22%5D.iloc%5B0%5D%0A%20%20%20%20%20%20%20%20between%20%3D%20n%20%2F%20(n%20-%201)%20*%20np.sum((t%20-%20t.mean())%20**%202)%0A%20%20%20%20%20%20%20%20parts.append(%7B%22stratum%22%3A%20name%2C%20%22n%22%3A%20n%2C%20%22N%22%3A%20N%2C%20%22fpc_factor%22%3A%201%20-%20n%20%2F%20N%2C%20%22between_psus%22%3A%20between%2C%20%22variance%22%3A%20(1%20-%20n%20%2F%20N)%20*%20between%7D)%0A%20%20%20%20by_hand%20%3D%20pd.DataFrame(parts)%0A%0A%20%20%20%20total%20%3D%20np.sum(w%20*%20y)%0A%20%20%20%20se%20%3D%20np.sqrt(by_hand%5B%22variance%22%5D.sum())%0A%20%20%20%20df%20%3D%20toy%5B%22psu%22%5D.nunique()%20-%20toy%5B%22stratum%22%5D.nunique()%0A%20%20%20%20q%20%3D%20stats.t.ppf(0.975%2C%20df)%0A%20%20%20%20mine%20%3D%20pd.DataFrame(%7B%22est%22%3A%20%5Btotal%5D%2C%20%22se%22%3A%20%5Bse%5D%2C%20%22lci%22%3A%20%5Btotal%20-%20q%20*%20se%5D%2C%20%22uci%22%3A%20%5Btotal%20%2B%20q%20*%20se%5D%2C%20%22cv%22%3A%20%5Bse%20%2F%20total%5D%2C%20%22df%22%3A%20%5Bdf%5D%7D)%0A%20%20%20%20mo.vstack(%5Bpsu_totals%2C%20by_hand%5D)%0A%20%20%20%20return%20by_hand%2C%20mine%2C%20psu_totals%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(by_hand)%3A%0A%20%20%20%20_largest%20%3D%20by_hand.loc%5Bby_hand%5B%22variance%22%5D.idxmax()%5D%0A%20%20%20%20_share%20%3D%20_largest%5B%22variance%22%5D%20%2F%20by_hand%5B%22variance%22%5D.sum()%0A%20%20%20%20_taken_whole%20%3D%20%5Bname.split()%5B0%5D%20for%20name%20in%20by_hand.loc%5Bby_hand%5B%22fpc_factor%22%5D%20%3D%3D%200%2C%20%22stratum%22%5D%5D%20%20%23%20the%20stratum's%20letter%0A%20%20%20%20mo.md(rf%22%22%22%0A%20%20%20%20Stratum%20**%7B_largest%5B%22stratum%22%5D.split()%5B0%5D%7D**%20carries%20**%7B_share%3A.0%25%7D**%20of%20the%20variance%3A%20its%20%7B_largest%5B%22n%22%5D%7D%20areas%20differ%2C%20and%20each%20of%20its%20households%20counts%20many%20times.%20The%20part%20of%20stratum%20%7B%22%2C%20%22.join(_taken_whole)%7D%20is%200%2C%20as%20the%20fpc%20says.%20**With%20svy%2C**%20the%20same%20sample%20in%20three%20lines%2C%20beside%20the%20numpy%20result%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(mine%2C%20toy)%3A%0A%20%20%20%20design%20%3D%20svy.Design(stratum%3D%22stratum%22%2C%20psu%3D%22psu%22%2C%20wgt%3D%22weight%22%2C%20pop_size%3D%22psu_population%22)%0A%20%20%20%20toy_sample_svy%20%3D%20svy.Sample(data%3Dpl.from_pandas(toy)%2C%20design%3Ddesign)%0A%20%20%20%20with_svy%20%3D%20toy_sample_svy.estimation.total(y%3D%22members%22).to_polars()%0A%20%20%20%20mo.vstack(%5B%0A%20%20%20%20%20%20%20%20mo.md(%22**svy**%20(%60sample.estimation.total(y%3D%5C%22members%5C%22)%60)%22)%2C%20with_svy%2C%0A%20%20%20%20%20%20%20%20mo.md(%22**numpy**%2C%20the%20cell%20above%22)%2C%20mine%2C%0A%20%20%20%20%5D)%0A%20%20%20%20return%20(toy_sample_svy%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20The%20same%20estimate%2C%20SE%2C%20interval%20and%20CV%2C%20to%20the%20last%20digit%20shown.%20svy%20computes%20nothing%20more%20mysterious%20than%20the%20cell%20above%3B%20it%20adds%20the%20checks%20(a%20fraction%20refused%2C%20a%20lonely%20PSU%20caught%2C%20%24N_h%24%20constant%20within%20a%20stratum)%20and%20the%20domains%20of%20module%206.%0A%0A%20%20%20%20%23%23%23%20Exercise%20P2.2.%20A%20stratified%20total%2C%20its%20SE%20and%20CV%2C%20without%20svy%0A%0A%20%20%20%20**Why%20it%20matters%3A**%20a%20function%20you%20wrote%20from%20the%20formula%20is%20how%20you%20confirm%20a%20library's%20number%2C%20and%20how%20you%20compute%20one%20the%20library%20does%20not%20offer.%20Module%206%20builds%20on%20it%20for%20the%20ReGenesees%20calls%20no%20Python%20library%20implements.%0A%0A%20%20%20%20**Input%20%E2%86%92%20output%3A**%20a%20sample%20with%20the%20columns%20%60stratum%60%2C%20%60psu%60%2C%20%60psu_population%60%20and%20%60weight%60%2C%20and%20the%20name%20of%20the%20variable%20%60y%60%20%E2%86%92%20a%20dictionary%20with%20three%20numbers%3A%0A%0A%20%20%20%20%7C%20key%20%7C%20what%20it%20is%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20%60%22total%22%60%20%7C%20%CE%A3%20w%C2%B7y%20over%20every%20row%20%7C%0A%20%20%20%20%7C%20%60%22se%22%60%20%7C%20the%20square%20root%20of%20the%20variance%20formula%20above%20%7C%0A%20%20%20%20%7C%20%60%22cv%22%60%20%7C%20se%20%C3%B7%20total%20%7C%0A%0A%20%20%20%20For%20the%20sample%20of%20part%202%20it%20returns%20the%20numbers%20of%20the%20cells%20above.%20Two%20rules%20the%20formula%20needs%3A%20a%20stratum%20with%20a%20**single%20PSU**%20adds%20nothing%20to%20the%20variance%20(svy's%20%60singleton%3D%22skip%22%60)%2C%20and%20**PSU%20names%20can%20repeat%20in%20different%20strata**%2C%20so%20a%20PSU%20is%20its%20stratum%20and%20its%20name%20together.%20Weights%20may%20differ%20within%20a%20PSU%2C%20as%20calibrated%20weights%20do.%0A%0A%20%20%20%20**Tools%3A**%20the%20cell%20above%2C%20written%20as%20a%20function%3A%20%60groupby(%5B%22stratum%22%2C%20%22psu%22%5D)%60%20for%20the%20PSU%20totals%2C%20then%20a%20loop%20over%20the%20strata%2C%20or%20a%20second%20%60groupby%60%3B%20%60np.sqrt%60.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20stratified_total(sample%3A%20pd.DataFrame%2C%20y%3A%20str)%20-%3E%20dict%3A%0A%20%20%20%20%22%22%22The%20estimated%20total%20of%20y%20under%20a%20stratified%20cluster%20design%2C%20with%20its%20standard%20error%20and%20coefficient%20of%20variation.%22%22%22%0A%20%20%20%20%23%20Exercise%20P2.2%3A%20the%20task%20and%20examples%20are%20in%20the%20text%20above%20this%20cell.%0A%20%20%20%20%23%20Step%201%3A%20a%20column%20of%20weight%20times%20y%3B%20the%20total%20is%20its%20sum%0A%20%20%20%20%23%20Step%202%3A%20each%20PSU's%20weighted%20total%2C%20grouping%20by%20stratum%20AND%20psu%2C%20with%20the%20stratum's%20psu_population%0A%20%20%20%20%23%20Step%203%3A%20for%20each%20stratum%20with%20two%20PSUs%20or%20more%3A%20(1%20-%20n%2FN)%20*%20n%2F(n%20-%201)%20*%20the%20sum%20of%20squared%20differences%20from%20the%20mean%0A%20%20%20%20%23%20Step%204%3A%20se%20is%20the%20square%20root%20of%20the%20strata's%20parts%20added%20up%3B%20cv%20is%20se%20%2F%20total%0A%20%20%20%20%23%20Step%205%3A%20return%20%7B%22total%22%3A%20...%2C%20%22se%22%3A%20...%2C%20%22cv%22%3A%20...%7D%0A%20%20%20%20raise%20NotImplementedError(%22Exercise%20P2.2%22)%0A%0A%0A%40app.function%0Adef%20shared_names()%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22The%20sample%20of%20part%202%20with%20stratum%20C's%20areas%20renamed%201002%2C%201005%20and%201007%2C%20names%20stratum%20A%20also%20uses.%22%22%22%0A%20%20%20%20toy%20%3D%20toy_sample()%0A%20%20%20%20toy%5B%22psu%22%5D%20%3D%20toy%5B%22psu%22%5D.replace(%7B%223012%22%3A%20%221002%22%2C%20%223077%22%3A%20%221005%22%2C%20%223140%22%3A%20%221007%22%7D)%0A%20%20%20%20return%20toy%0A%0A%0A%40app.function%0Adef%20calibrated_lonely()%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22The%20sample%20of%20part%202%20with%20weights%20that%20differ%20within%20an%20area%2C%20as%20calibrated%20weights%20do%2C%20and%20stratum%20C%20reduced%20to%0A%20%20%20%20one%20area%2C%20a%20lonely%20PSU.%22%22%22%0A%20%20%20%20toy%20%3D%20toy_sample()%0A%20%20%20%20toy%20%3D%20toy%5B~toy%5B%22psu%22%5D.isin(%5B%223077%22%2C%20%223140%22%5D)%5D.reset_index(drop%3DTrue)%0A%20%20%20%20toy%5B%22weight%22%5D%20%3D%20toy%5B%22weight%22%5D%20*%20np.array(%5B1.0%2C%201.0%2C%201.0%2C%201.1%2C%200.9%2C%201.05%2C%201.0%2C%200.95%2C%201.2%2C%201.0%2C%201.1%2C%200.9%2C%201.0%2C%201.0%2C%201.1%5D)%0A%20%20%20%20return%20toy%0A%0A%0A%40app.function%0Adef%20survey_sample()%20-%3E%20pd.DataFrame%3A%0A%20%20%20%20%22%22%22The%20survey's%20households%20in%20the%20columns%20stratified_total%20reads%2C%20with%20sel_final_weight%20as%20the%20weight%2C%20without%20the%0A%20%20%20%20copied%20interviews%20module%204%20finds%20(part%206).%22%22%22%0A%20%20%20%20frame%20%3D%20course.design_frame()%0A%20%20%20%20copies%20%3D%20course.planted_defects().query(%22kind%20%3D%3D%20'household_copied'%22)%5B%5B%22region_code%22%2C%20%22hh_id%22%5D%5D%0A%20%20%20%20found%20%3D%20frame.merge(copies%2C%20on%3D%5B%22region_code%22%2C%20%22hh_id%22%5D%2C%20how%3D%22left%22%2C%20indicator%3DTrue)%5B%22_merge%22%5D.eq(%22both%22).to_numpy()%0A%20%20%20%20frame%20%3D%20frame%5B~found%5D.reset_index(drop%3DTrue)%0A%20%20%20%20return%20pd.DataFrame(%7B%0A%20%20%20%20%20%20%20%20%22region_code%22%3A%20frame%5B%22region_code%22%5D%2C%20%22hh_id%22%3A%20frame%5B%22hh_id%22%5D%2C%20%22stratum%22%3A%20frame%5B%22stratum_id%22%5D%2C%20%22psu%22%3A%20frame%5B%22psu_id%22%5D%2C%0A%20%20%20%20%20%20%20%20%22psu_population%22%3A%20frame%5B%22psus_in_stratum%22%5D%2C%20%22weight%22%3A%20frame%5B%22sel_final_weight%22%5D%2C%20%22members%22%3A%20frame%5B%22hh_roster_members%22%5D%2C%0A%20%20%20%20%7D)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20check_cases(%0A%20%20%20%20%20%20%20%20%22Exercise%20P2.2%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%20sample%20of%20part%202%2C%20members%22%2C%20(toy_sample()%2C%20%22members%22))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22the%20same%20sample%20with%20PSU%20names%20that%20repeat%20across%20strata%22%2C%20(shared_names()%2C%20%22members%22))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22calibrated%20weights%20that%20differ%20within%20an%20area%2C%20and%20a%20stratum%20with%20one%20PSU%22%2C%20(calibrated_lonely()%2C%20%22members%22))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22the%20survey's%20households%2C%20members%2C%20weighted%20by%20sel_final_weight%22%2C%20(survey_sample()%2C%20%22members%22))%2C%0A%20%20%20%20%20%20%20%20%5D%2C%0A%20%20%20%20%20%20%20%20what%3D%22samples%22%2C%0A%20%20%20%20%20%20%20%20hint%3D%22Weighted%20totals%20per%20PSU%20(group%20by%20stratum%20and%20psu)%2C%20then%20(1%20-%20n%2FN)%20%C2%B7%20n%2F(n%20-%201)%20%C2%B7%20%CE%A3(t%20-%20mean)%C2%B2%20per%20stratum%20with%20two%20PSUs%20or%20more.%22%2C%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(toy)%3A%0A%20%20%20%20%23%20Try%20it%3A%20the%20sample%20of%20part%202.%20Compare%20with%20svy's%20row%20above.%0A%20%20%20%20try_it(stratified_total%2C%20toy%2C%20%22members%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%20P2.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%20The%20total%20right%2C%20the%20SE%20far%20too%20small%20%7C%20The%20variance%20is%20computed%20from%20households%2C%20not%20from%20PSU%20totals.%20%7C%20step%202%3A%20one%20row%20per%20PSU%20first%20%7C%0A%20%20%20%20%7C%20The%20second%20case%20red%2C%20the%20first%20green%20%7C%20PSUs%20grouped%20by%20%60psu%60%20alone%2C%20so%20areas%20of%20different%20strata%20that%20share%20a%20name%20are%20added%20together.%20%7C%20step%202%3A%20%60groupby(%5B%22stratum%22%2C%20%22psu%22%5D)%60%20%7C%0A%20%20%20%20%7C%20*ZeroDivisionError*%2C%20or%20%60nan%60%20for%20the%20SE%20in%20the%20third%20case%20%7C%20A%20stratum%20with%20one%20PSU%20reaches%20%60n%20%2F%20(n%20-%201)%60.%20%7C%20step%203%3A%20only%20strata%20with%20%60n%20%3E%201%60%20%7C%0A%20%20%20%20%7C%20The%20SE%20a%20little%20too%20large%20%7C%20The%20fpc%20is%20missing%3A%20every%20stratum's%20part%20is%20too%20large%2C%20and%20stratum%20F%20adds%20variance%20it%20should%20not.%20%7C%20step%203%3A%20%60(1%20-%20n%20%2F%20N)%60%20%7C%0A%20%20%20%20%7C%20The%20SE%20too%20small%20by%20a%20factor%20near%201.2%20to%201.7%20%7C%20%60n%20%2F%20(n%20-%201)%60%20is%20missing%2C%20or%20%60np.var%60%20with%20its%20default%20%60ddof%3D0%60%20used.%20%7C%20step%203%3A%20%60n%20%2F%20(n%20-%201)%60%20times%20the%20sum%20of%20squares%20%7C%0A%20%20%20%20%7C%20The%20cv%20100%20times%20too%20large%20%7C%20The%20CV%20given%20in%20percent.%20%7C%20step%204%3A%20%60se%20%2F%20total%60%2C%20a%20fraction%20as%20svy%20gives%20it%20%7C%0A%20%20%20%20%7C%20*KeyError%3A%20'members'*%20%7C%20The%20variable's%20name%20typed%20in%20instead%20of%20taken%20from%20%60y%60.%20%7C%20%60sample%5By%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%20%23%23%205%20%C2%B7%20Means%20and%20shares%3A%20Taylor%20linearization%0A%0A%20%20%20%20**The%20problem.**%20The%20mean%20number%20of%20members%20per%20household%20is%20a%20**ratio%20of%20two%20totals**%3A%20the%20estimated%20total%20of%20members%20divided%20by%20the%20estimated%20number%20of%20households%2C%20%24%5Cbar%20y%20%3D%20%5Chat%20t_y%20%2F%20%5Chat%20t_1%24%2C%20where%20%24%5Chat%20t_1%20%3D%20%5Csum%20w%24.%20The%20formula%20of%20part%204%20is%20for%20a%20total%3B%20a%20ratio's%20denominator%20varies%20from%20sample%20to%20sample%20too.%0A%0A%20%20%20%20**The%20tool.**%20**Taylor%20linearization**%20replaces%20the%20ratio%20by%20a%20total%20that%20varies%20the%20same%20way%2C%20to%20a%20first%20approximation.%20Each%20household%20gets%20a%20**linearized%20value**%0A%0A%20%20%20%20%24%24z_k%20%3D%20%5Cfrac%7Bw_k%20%5Cleft(y_k%20-%20%5Cbar%20y%5Cright)%7D%7B%5Csum%20w%7D%24%24%0A%0A%20%20%20%20and%20the%20variance%20of%20the%20mean%20is%20the%20variance%20of%20the%20total%20of%20the%20%24z_k%24%2C%20computed%20with%20the%20formula%20of%20part%204%20(each%20%24z_k%24%20already%20holds%20its%20weight).%20A%20proportion%20is%20the%20mean%20of%20a%20variable%20that%20is%201%20for%20the%20households%20in%20the%20category%20and%200%20for%20the%20others%2C%20so%20it%20is%20linearized%20the%20same%20way.%20svy%2C%20R's%20%60survey%60%20and%20ReGenesees%20all%20estimate%20the%20SE%20of%20a%20mean%2C%20a%20share%20or%20a%20ratio%20this%20way%20unless%20told%20to%20use%20replicate%20weights.%20The%20derivation%20is%20in%20Lohr's%20treatment%20of%20variance%20estimation%20by%20linearization%3B%20module%206's%20ReGenesees%20mapping%20writes%20%60svystatL%60's%20linearization%20the%20same%20way%20for%20any%20function%20of%20totals.%0A%0A%20%20%20%20The%20mean%20of%20the%20sample%20of%20part%202%2C%20with%20svy%20and%20with%20the%20linearized%20values%20in%20numpy%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(psu_totals%2C%20toy%2C%20toy_sample_svy)%3A%0A%20%20%20%20_w%2C%20_y%20%3D%20toy%5B%22weight%22%5D.to_numpy()%2C%20toy%5B%22members%22%5D.to_numpy()%0A%20%20%20%20mean%20%3D%20np.sum(_w%20*%20_y)%20%2F%20np.sum(_w)%0A%20%20%20%20z%20%3D%20_w%20*%20(_y%20-%20mean)%20%2F%20np.sum(_w)%20%20%23%20the%20linearized%20values%3A%20each%20already%20carries%20its%20weight%0A%20%20%20%20z_totals%20%3D%20toy.assign(z%3Dz).groupby(%5B%22stratum%22%2C%20%22psu%22%5D%2C%20as_index%3DFalse).agg(t%3D(%22z%22%2C%20%22sum%22))%0A%20%20%20%20_variance%20%3D%200.0%0A%20%20%20%20for%20_%2C%20_stratum%20in%20z_totals.merge(psu_totals%5B%5B%22stratum%22%2C%20%22psu%22%2C%20%22N%22%5D%5D%2C%20on%3D%5B%22stratum%22%2C%20%22psu%22%5D).groupby(%22stratum%22)%3A%0A%20%20%20%20%20%20%20%20_t%2C%20_n%2C%20_N%20%3D%20_stratum%5B%22t%22%5D.to_numpy()%2C%20len(_stratum)%2C%20_stratum%5B%22N%22%5D.iloc%5B0%5D%0A%20%20%20%20%20%20%20%20_variance%20%2B%3D%20(1%20-%20_n%20%2F%20_N)%20*%20_n%20%2F%20(_n%20-%201)%20*%20np.sum((_t%20-%20_t.mean())%20**%202)%0A%20%20%20%20mo.vstack(%5B%0A%20%20%20%20%20%20%20%20mo.md(%22**svy**%20(%60sample.estimation.mean(y%3D%5C%22members%5C%22)%60)%22)%2C%0A%20%20%20%20%20%20%20%20toy_sample_svy.estimation.mean(y%3D%22members%22).to_polars().select(%22est%22%2C%20%22se%22%2C%20%22cv%22)%2C%0A%20%20%20%20%20%20%20%20mo.md(%22**numpy**%2C%20from%20the%20linearized%20values%22)%2C%0A%20%20%20%20%20%20%20%20pd.DataFrame(%7B%22est%22%3A%20%5Bmean%5D%2C%20%22se%22%3A%20%5Bnp.sqrt(_variance)%5D%2C%20%22cv%22%3A%20%5Bnp.sqrt(_variance)%20%2F%20mean%5D%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%206%20%C2%B7%20The%20same%20on%20the%20survey%0A%0A%20%20%20%20**The%20problem.**%20The%20survey%20is%20the%20sample%20of%20part%202%20at%20full%20size%3A%20six%20provinces%2C%20four%20strata%20each.%20%60course.design_frame()%60%20gives%20its%20households%20with%20%60stratum_id%60%20(region%2C%20province%20and%20stratum%20letter)%2C%20%60psu_id%60%20(the%20area%20pasted%20to%20its%20stratum%2C%20since%20area%20numbers%20repeat%20across%20provinces%3B%20the%20household%20itself%20in%20stratum%20F)%20and%20%60psus_in_stratum%60%20from%20the%20stratum%20counts.%20Handed%20to%20svy%20as%20it%20is%2C%20the%20design%20is%20refused%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%20design_frame%20%3D%20course.design_frame()%0A%20%20%20%20_columns%20%3D%20%5B%22stratum_id%22%2C%20%22psu_id%22%2C%20%22psus_in_stratum%22%2C%20%22sel_final_weight%22%2C%20%22hh_roster_members%22%5D%0A%20%20%20%20show(lambda%3A%20svy.Sample(%0A%20%20%20%20%20%20%20%20data%3Dpl.from_pandas(design_frame%5B_columns%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%0A%20%20%20%20).estimation.total(y%3D%22hh_roster_members%22)%2C%20lines%3D3)%0A%20%20%20%20return%20(design_frame%2C)%0A%0A%0A%40app.cell%0Adef%20_(design_frame)%3A%0A%20%20%20%20%23%20Which%20stratum%20has%20more%20PSUs%20in%20the%20data%20than%20in%20the%20population%3F%0A%20%20%20%20_psus%20%3D%20design_frame.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))%0A%20%20%20%20too_many%20%3D%20_psus%5B_psus%5B%22n%22%5D%20%3E%20_psus%5B%22N%22%5D%5D%0A%20%20%20%20too_many%0A%20%20%20%20return%20(too_many%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(design_frame%2C%20too_many)%3A%0A%20%20%20%20_stratum%20%3D%20too_many.index%5B0%5D%20if%20len(too_many)%20else%20%22none%22%0A%20%20%20%20_households%20%3D%20design_frame.loc%5Bdesign_frame%5B%22stratum_id%22%5D%20%3D%3D%20_stratum%2C%20%22hh_id%22%5D.nunique()%0A%20%20%20%20_copies%20%3D%20course.planted_defects().query(%22kind%20%3D%3D%20'household_copied'%22)%0A%20%20%20%20mo.md(rf%22%22%22%0A%20%20%20%20The%20refusal%20is%20information.%20In%20stratum%20**%7B_stratum%7D**%2C%20a%20stratum%20taken%20whole%2C%20the%20data%20holds%20**%7B_households%7D**%20households%20where%20the%20stratum%20counts%20list%20**%7Bint(too_many%5B%22N%22%5D.iloc%5B0%5D)%20if%20len(too_many)%20else%200%7D**%20on%20the%20register%3A%20one%20interview%20was%20entered%20twice.%20Module%204%20finds%20such%20copies%20from%20the%20answers%20themselves%3B%20here%20the%20course's%20list%20of%20planted%20defects%20names%20the%20**%7Blen(_copies)%7D**%20copies%20in%20the%20survey%2C%20one%20in%20each%20region%2C%20and%20%60survey_sample()%60%20leaves%20them%20all%20out.%20A%20count%20of%20PSUs%20that%20exceeds%20its%20population%20is%20a%20check%20the%20fpc%20gives%20you%20for%20free%2C%20though%20it%20catches%20a%20copy%20only%20where%20the%20copy%20pushes%20the%20count%20over%20the%20frame's%3B%20a%20design%20given%20as%20fractions%2C%20%24f%20%3D%201%24%20for%20this%20stratum%2C%20would%20have%20hidden%20even%20that%20one.%0A%0A%20%20%20%20With%20the%20copies%20out%2C%20the%20total%20number%20of%20household%20members%2C%20by%20svy%20and%20by%20your%20function%20of%20exercise%20P2.2%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%20survey%20%3D%20survey_sample()%0A%20%20%20%20survey_svy%20%3D%20svy.Sample(%0A%20%20%20%20%20%20%20%20data%3Dpl.from_pandas(survey)%2C%0A%20%20%20%20%20%20%20%20design%3Dsvy.Design(stratum%3D%22stratum%22%2C%20psu%3D%22psu%22%2C%20wgt%3D%22weight%22%2C%20pop_size%3D%22psu_population%22)%2C%0A%20%20%20%20)%0A%20%20%20%20survey_total%20%3D%20survey_svy.estimation.total(y%3D%22members%22).to_polars()%0A%20%20%20%20survey_total%0A%20%20%20%20return%20survey%2C%20survey_total%0A%0A%0A%40app.cell%0Adef%20_(survey%2C%20survey_total)%3A%0A%20%20%20%20_mine%20%3D%20attempt(stratified_total%2C%20survey%2C%20%22members%22)%0A%20%20%20%20mo.stop(_mine%20is%20NOT_YET%2C%20waiting_for(%22Exercise%20P2.2%22))%0A%20%20%20%20_theirs%20%3D%20survey_total.row(0%2C%20named%3DTrue)%0A%20%20%20%20_same%20%3D%20abs(_mine%5B%22total%22%5D%20-%20_theirs%5B%22est%22%5D)%20%3C%201e-6%20*%20_theirs%5B%22est%22%5D%20and%20abs(_mine%5B%22se%22%5D%20-%20_theirs%5B%22se%22%5D)%20%3C%201e-6%20*%20_theirs%5B%22se%22%5D%0A%20%20%20%20mo.vstack(%5B%0A%20%20%20%20%20%20%20%20pd.DataFrame(%7B%22svy%22%3A%20%5B_theirs%5B%22est%22%5D%2C%20_theirs%5B%22se%22%5D%2C%20_theirs%5B%22cv%22%5D%5D%2C%20%22your%20function%22%3A%20%5B_mine%5B%22total%22%5D%2C%20_mine%5B%22se%22%5D%2C%20_mine%5B%22cv%22%5D%5D%7D%2C%20index%3D%5B%22total%22%2C%20%22se%22%2C%20%22cv%22%5D)%2C%0A%20%20%20%20%20%20%20%20mo.callout(mo.md(%22**The%20same%20as%20svy.**%20A%20few%20lines%20of%20pandas%20and%20numpy%2C%20written%20from%20the%20formula%2C%20give%20the%20library's%20standard%20error%20on%20the%20whole%20survey.%22%20if%20_same%20else%20%22**Not%20the%20same%20as%20svy%20yet.**%20Compare%20your%20function%20with%20the%20check%20above.%22)%2C%20kind%3D%22success%22%20if%20_same%20else%20%22warn%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**Which%20column%20is%20the%20PSU%2C%20and%20why%20the%20fpc%20is%20a%20count.**%20The%20standard%20error%20depends%20on%20what%20the%20design%20calls%20a%20PSU%20and%20on%20whether%20it%20knows%20%24N_h%24.%20A%20design%20that%20names%20no%20cluster%20(in%20R's%20%60svydesign%60%2C%20%60ids%20%3D%20~1%60)%20treats%20each%20household%20as%20drawn%20on%20its%20own%2C%20though%20the%20areas%20were%20drawn%3B%20a%20design%20without%20%60pop_size%60%20drops%20the%20fpc%2C%20as%20R's%20%60svydesign%60%20does%20without%20%60fpc%60.%20The%20cell%20below%20computes%20the%20same%20total's%20SE%20three%20ways%2C%20so%20that%20a%20difference%20between%20an%20R%20table%20and%20a%20Python%20one%20can%20be%20traced%20to%20its%20cause.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(survey)%3A%0A%20%20%20%20_data%20%3D%20pl.from_pandas(survey.assign(household%3Dsurvey%5B%22region_code%22%5D%20%2B%20%22-%22%20%2B%20survey%5B%22hh_id%22%5D.astype(%22string%22)))%0A%0A%20%20%20%20def%20_se(psu%3A%20str%2C%20fpc%3A%20bool)%20-%3E%20float%3A%0A%20%20%20%20%20%20%20%20_design%20%3D%20svy.Design(stratum%3D%22stratum%22%2C%20psu%3Dpsu%2C%20wgt%3D%22weight%22%2C%20pop_size%3D%22psu_population%22%20if%20fpc%20else%20None)%0A%20%20%20%20%20%20%20%20return%20svy.Sample(data%3D_data%2C%20design%3D_design).estimation.total(y%3D%22members%22).to_polars()%5B%22se%22%5D%5B0%5D%0A%0A%20%20%20%20pd.DataFrame(%7B%0A%20%20%20%20%20%20%20%20%22the%20PSU%22%3A%20%5B%22the%20area%20(the%20survey's%20design)%22%2C%20%22the%20area%22%2C%20%22each%20household%20on%20its%20own%22%5D%2C%0A%20%20%20%20%20%20%20%20%22fpc%22%3A%20%5B%22yes%2C%20psu_population%22%2C%20%22no%22%2C%20%22no%22%5D%2C%0A%20%20%20%20%20%20%20%20%22se%22%3A%20%5B_se(%22psu%22%2C%20True)%2C%20_se(%22psu%22%2C%20False)%2C%20_se(%22household%22%2C%20False)%5D%2C%0A%20%20%20%20%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%20One%20more%20difference%20to%20know%20when%20comparing%20with%20ReGenesees.%20svy%20is%20given%20%60sel_final_weight%60%20as%20a%20column%3A%20it%20does%20not%20know%20the%20weight%20was%20calibrated%2C%20so%20it%20treats%20the%20weights%20as%20fixed%2C%20as%20R's%20%60svydesign%60%20does%20with%20a%20calibrated%20weight%20column.%20ReGenesees'%20%60e.calibrate%60%20computes%20the%20weights%20itself%20and%2C%20knowing%20the%20calibration%2C%20gives%20a%20standard%20error%20that%20accounts%20for%20it%2C%20so%20the%20two%20can%20differ.%20svy%20accounts%20for%20a%20calibration%20too%20when%20it%20does%20the%20calibration%20itself%20(%60sample.weighting.calibrate%60).%0A%0A%20%20%20%20%23%23%20What%20you%20can%20do%20now%0A%0A%20%20%20%20%7C%20the%20term%20%7C%20what%20you%20can%20now%20do%20with%20it%20%7C%0A%20%20%20%20%7C%3A--%7C%3A--%7C%0A%20%20%20%20%7C%20design%2C%20strata%2C%20PSUs%20%7C%20say%20how%20the%20survey%20was%20drawn%2C%20and%20why%20the%20SE%20comes%20from%20the%20variation%20between%20PSUs%20within%20strata%20%7C%0A%20%20%20%20%7C%20weights%20%7C%20read%20%60sel_design_weight%60%20as%20N%20%C3%B7%20n%20and%20%60sel_final_weight%60%20as%20the%20weight%20for%20estimates%3B%20compute%20%CE%A3%20w%C2%B7y%20%7C%0A%20%20%20%20%7C%20fpc%20%7C%20turn%20sampling%20fractions%20written%20for%20R%20into%20the%20counts%20svy's%20%60pop_size%60%20wants%20(P2.1)%20%7C%0A%20%20%20%20%7C%20stratified%20Taylor%20variance%20%7C%20compute%20a%20total's%20SE%20and%20CV%20in%20a%20few%20lines%20of%20numpy%20and%20match%20svy's%20(P2.2)%3B%20linearize%20a%20mean%20%7C%0A%20%20%20%20%7C%20CV%20and%20confidence%20interval%20%7C%20read%20svy's%20%60cv%60%2C%20%60lci%60%2C%20%60uci%60%20and%20%60df%60%20%7C%0A%20%20%20%20%7C%20svy's%20refusals%20%7C%20read%20them%20as%20checks%20on%20the%20design%3A%20a%20fraction%2C%20a%20lonely%20PSU%2C%20more%20PSUs%20than%20the%20population%20%7C%0A%0A%20%20%20%20**Next%3A**%20module%206%2C%20%5BEstimate%20under%20the%20survey%20design%5D(%2F%3Ffile%3D50_design_estimates.py)%2C%20estimates%20totals%2C%20means%20and%20proportions%20by%20domain%20with%20their%20SE%20and%20CV%2C%20and%20maps%20each%20ReGenesees%20call%20to%20svy%20and%20to%20a%20function%20of%20your%20own.%20The%20R%20side%20of%20every%20step%20is%20in%20primer%20%5BP1%2C%20from%20R%20to%20Python%5D(%2F%3Ffile%3D95_primer_r_to_python.py).%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20app.run()%0A
c709f724f2e24393766d98772af45ec3