import%20marimo%0A%0A__generated_with%20%3D%20%220.23.9%22%0Aapp%20%3D%20marimo.App(width%3D%22medium%22)%0A%0A%0A%40app.cell%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(%22%22%22%0A%20%20%20%20%23%20Designation%20vs.%20realized%20water%20%E2%80%94%20the%20block-group%20mechanism%20test%0A%0A%20%20%20%20**Question%3A**%20is%20the%20post-storm%20price%20hit%20tracking%20the%20FEMA%20flood%20*label*%0A%20%20%20%20(a%20parcel's%20flood-zone%20designation)%20or%20*where%20it%20actually%20flooded*%20(realized%20NFIP%0A%20%20%20%20claims)%3F%20The%20parcel%20study%20(F21)%20used%20designation%3B%20this%20brings%20in%20realized%20damage%20at%0A%20%20%20%20**census%20block-group**%20grain%20(~a%20few%20blocks%20%E2%80%94%20the%20finest%20the%20redacted%20claims%20allow).%0A%0A%20%20%20%20**Plumbing%3A**%20%60flood_claims_bg%60%20(NFIP%20claims%20by%20block-group%20%C3%97%20year%2C%20OpenFEMA)%20%2B%0A%20%20%20%20%60parcel_block_group%60%20(parcels%20spatially%20joined%20to%20TIGER%20block%20groups%20%E2%80%94%20100%25%20matched%3B%0A%20%20%20%2098%25%20of%202024%20claim%20volume%20joins%20across%20vintages).%0A%0A%20%20%20%20**Design%3A**%20the%20same%20parcel%20repeat-sales%20(n%E2%89%883%2C357)%2C%20each%20tagged%20with%20its%20*own*%0A%20%20%20%20flood-zone%20designation%20**and**%20its%20block%20group's%20*realized*%202024%20claim%20intensity%0A%20%20%20%20(claims%20per%20parcel).%20Horse%20race%20with%20zip%20fixed%20effects%2C%20SEs%20clustered%20by%20block%20group.%0A%0A%20%20%20%20**Headline%20(F22)%3A**%20both%20predict%20decline%20alone%2C%20but%20they're%20highly%20collinear%0A%20%20%20%20(r%E2%89%88%2B0.67).%20With%20both%20in%2C%20**realized%20claims%20edge%20out%20designation**%20%E2%86%92%20*suggestive*%20the%0A%20%20%20%20market%20prices%20actual%20flooding%20%E2%89%A5%20the%20label.%20Collinearity%20prevents%20a%20clean%20split%3B%0A%20%20%20%20claims%20also%20reflect%20insurance%20take-up%2C%20not%20pure%20water.%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%20from%20pathlib%20import%20Path%0A%0A%20%20%20%20import%20duckdb%0A%20%20%20%20import%20marimo%20as%20mo%0A%20%20%20%20import%20matplotlib.pyplot%20as%20plt%0A%20%20%20%20import%20numpy%20as%20np%0A%20%20%20%20import%20pandas%20as%20pd%0A%20%20%20%20import%20statsmodels.formula.api%20as%20smf%0A%0A%20%20%20%20return%20Path%2C%20duckdb%2C%20mo%2C%20np%2C%20pd%2C%20plt%2C%20smf%0A%0A%0A%40app.cell%0Adef%20_(Path%2C%20duckdb)%3A%0A%20%20%20%20def%20_find_db()%20-%3E%20str%3A%0A%20%20%20%20%20%20%20%20for%20d%20in%20%5BPath.cwd()%2C%20*Path.cwd().parents%5D%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20if%20(d%20%2F%20%22analytics.db%22).exists()%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20return%20str(d%20%2F%20%22analytics.db%22)%0A%20%20%20%20%20%20%20%20raise%20FileNotFoundError(%22analytics.db%20not%20found%20walking%20up%20from%20cwd%22)%0A%0A%20%20%20%20conn%20%3D%20duckdb.connect(_find_db()%2C%20read_only%3DTrue)%0A%20%20%20%20return%20(conn%2C)%0A%0A%0A%40app.cell%0Adef%20_(conn)%3A%0A%20%20%20%20%23%20Parcel%20repeat-sales%20%2B%20own%20flood%20designation%20%2B%20own-BG%20realized%202024%20claim%20intensity.%0A%20%20%20%20df%20%3D%20conn.execute(%0A%20%20%20%20%20%20%20%20%22%22%22%0A%20%20%20%20%20%20%20%20WITH%20bgn%20AS%20(SELECT%20block_group%2C%20count(*)%20n%20FROM%20parcel_block_group%20GROUP%20BY%20block_group)%2C%0A%20%20%20%20%20%20%20%20bgc%20AS%20(SELECT%20fc.block_group%2C%20fc.claim_count%3A%3Adouble%20%2F%20bgn.n%20AS%20cpp%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20FROM%20flood_claims_bg%20fc%20JOIN%20bgn%20USING%20(block_group)%20WHERE%20fc.year%20%3D%202024)%2C%0A%20%20%20%20%20%20%20%20arms%20AS%20(%0A%20%20%20%20%20%20%20%20%20%20SELECT%20p.parcel_id%2C%20p.zip%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20CASE%20WHEN%20upper(p.flood_zone)%20LIKE%20'A%25'%20OR%20upper(p.flood_zone)%20LIKE%20'V%25'%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20THEN%201%20ELSE%200%20END%20AS%20in_sfha%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20s.sale_date%2C%20s.sale_price%0A%20%20%20%20%20%20%20%20%20%20FROM%20sales_history%20s%20JOIN%20parcels%20p%20USING%20(parcel_id)%0A%20%20%20%20%20%20%20%20%20%20WHERE%20s.sale_price%20%3E%3D%2010000%20AND%20(s.buyer%20IS%20NULL%20OR%20s.seller%20IS%20NULL%20OR%20s.buyer%20%3C%3E%20s.seller)%0A%20%20%20%20%20%20%20%20%20%20%20%20AND%20p.sqft_living%20%3E%200%20AND%20s.sale_price%20%2F%20p.sqft_living%20BETWEEN%2020%20AND%202000%0A%20%20%20%20%20%20%20%20%20%20%20%20AND%20p.flood_zone%20IS%20NOT%20NULL%0A%20%20%20%20%20%20%20%20)%2C%0A%20%20%20%20%20%20%20%20pre%20AS%20(SELECT%20parcel_id%2C%20zip%2C%20in_sfha%2C%20sale_date%20pre_date%2C%20sale_price%20pre_price%20FROM%20arms%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20WHERE%20sale_date%20BETWEEN%20DATE%20'2022-01-01'%20AND%20DATE%20'2024-08-31'%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20QUALIFY%20row_number()%20OVER%20(PARTITION%20BY%20parcel_id%20ORDER%20BY%20sale_date%20DESC)%20%3D%201)%2C%0A%20%20%20%20%20%20%20%20post%20AS%20(SELECT%20parcel_id%2C%20sale_date%20post_date%2C%20sale_price%20post_price%20FROM%20arms%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20WHERE%20sale_date%20%3E%3D%20DATE%20'2024-09-01'%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20QUALIFY%20row_number()%20OVER%20(PARTITION%20BY%20parcel_id%20ORDER%20BY%20sale_date%20ASC)%20%3D%201)%0A%20%20%20%20%20%20%20%20SELECT%20pre.zip%2C%20pbg.block_group%2C%20pre.in_sfha%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20(post.post_date%20-%20pre.pre_date)%20AS%20days_held%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20COALESCE(bgc.cpp%2C%200)%20AS%20bg_claims_per_parcel%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20ln(post.post_price%20%2F%20pre.pre_price)%20AS%20logratio%0A%20%20%20%20%20%20%20%20FROM%20pre%20JOIN%20post%20USING%20(parcel_id)%0A%20%20%20%20%20%20%20%20JOIN%20parcel_block_group%20pbg%20ON%20pbg.parcel_id%20%3D%20pre.parcel_id%0A%20%20%20%20%20%20%20%20LEFT%20JOIN%20bgc%20ON%20bgc.block_group%20%3D%20pbg.block_group%0A%20%20%20%20%20%20%20%20WHERE%20(post.post_date%20-%20pre.pre_date)%20%3E%3D%20365%0A%20%20%20%20%20%20%20%20%22%22%22%0A%20%20%20%20).df()%0A%20%20%20%20df%5B%22days_held%22%5D%20%3D%20df%5B%22days_held%22%5D.astype(float)%0A%20%20%20%20df%5B%22bg_claims_z%22%5D%20%3D%20(df%5B%22bg_claims_per_parcel%22%5D%20-%20df%5B%22bg_claims_per_parcel%22%5D.mean())%20%2F%20df%5B%22bg_claims_per_parcel%22%5D.std()%0A%20%20%20%20df%0A%20%20%20%20return%20(df%2C)%0A%0A%0A%40app.cell%0Adef%20_(df%2C%20mo)%3A%0A%20%20%20%20mo.md(f%22%22%22%0A%20%20%20%20**Collinearity%20warning%20up%20front.**%20Designation%20(%60in_sfha%60)%20and%20realized%20BG-claim%0A%20%20%20%20intensity%20correlate%20at%20**r%20%3D%20%7Bdf%5B%5B'in_sfha'%2C%20'bg_claims_per_parcel'%5D%5D.corr().iloc%5B0%2C%201%5D%3A%2B.2f%7D**%0A%20%20%20%20%E2%80%94%20flood-zone%20houses%20sit%20in%20block%20groups%20that%20flooded.%20So%20a%20horse%20race%20*can't*%20fully%0A%20%20%20%20separate%20them%3B%20both%20coefficients%20will%20shrink.%20We%20read%20the%20*relative*%20shrinkage%2C%20not%0A%20%20%20%20absolute%20magnitudes.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(df%2C%20np%2C%20pd%2C%20smf)%3A%0A%20%20%20%20%23%20Each%20predictor%20alone%2C%20then%20both%20together.%20Zip%20FE%3B%20SEs%20clustered%20by%20block%20group.%0A%20%20%20%20def%20_fit(formula)%3A%0A%20%20%20%20%20%20%20%20m%20%3D%20smf.ols(formula%2C%20data%3Ddf).fit(cov_type%3D%22cluster%22%2C%20cov_kwds%3D%7B%22groups%22%3A%20df%5B%22block_group%22%5D%7D)%0A%20%20%20%20%20%20%20%20return%20m%0A%0A%20%20%20%20def%20_row(m%2C%20key)%3A%0A%20%20%20%20%20%20%20%20return%20%7B%22coef%22%3A%20m.params%5Bkey%5D%2C%20%22pct%22%3A%20100%20*%20(np.exp(m.params%5Bkey%5D)%20-%201)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22se%22%3A%20m.bse%5Bkey%5D%2C%20%22p%22%3A%20m.pvalues%5Bkey%5D%7D%0A%0A%20%20%20%20_m_des%20%3D%20_fit(%22logratio%20~%20in_sfha%20%2B%20days_held%20%2B%20C(zip)%22)%0A%20%20%20%20_m_real%20%3D%20_fit(%22logratio%20~%20bg_claims_z%20%2B%20days_held%20%2B%20C(zip)%22)%0A%20%20%20%20_m_both%20%3D%20_fit(%22logratio%20~%20in_sfha%20%2B%20bg_claims_z%20%2B%20days_held%20%2B%20C(zip)%22)%0A%0A%20%20%20%20results%20%3D%20pd.DataFrame(%7B%0A%20%20%20%20%20%20%20%20%22designation%20alone%22%3A%20_row(_m_des%2C%20%22in_sfha%22)%2C%0A%20%20%20%20%20%20%20%20%22realized%20BG-claims%20alone%22%3A%20_row(_m_real%2C%20%22bg_claims_z%22)%2C%0A%20%20%20%20%20%20%20%20%22designation%20(both%20in)%22%3A%20_row(_m_both%2C%20%22in_sfha%22)%2C%0A%20%20%20%20%20%20%20%20%22realized%20BG-claims%20(both%20in)%22%3A%20_row(_m_both%2C%20%22bg_claims_z%22)%2C%0A%20%20%20%20%7D).T.round(4)%0A%20%20%20%20results%0A%20%20%20%20return%20(results%2C)%0A%0A%0A%40app.cell%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(%22%22%22%0A%20%20%20%20**Reading%20the%20horse%20race.**%20Designation%20alone%20and%20realized-claims%20alone%20each%20predict%0A%20%20%20%20the%20decline.%20Put%20both%20in%20and%20*both*%20shrink%20(collinearity)%2C%20but%20**designation%20washes%0A%20%20%20%20out**%20(p%20%E2%89%88%200.24)%20while%20**realized%20claims%20hold%20onto%20marginal%20significance**%20(p%20%E2%89%88%200.08).%0A%20%20%20%20The%20market%20looks%20like%20it's%20responding%20to%20actual%20flooding%20at%20least%20as%20much%20as%20to%20the%0A%20%20%20%20label%20%E2%80%94%20*suggestive*%2C%20not%20proof.%20Two%20caveats%3A%20claims%20measure%20*insured*%20flooding%20(take-up%2C%0A%20%20%20%20not%20pure%20water)%2C%20and%20the%20significance%20is%20sensitive%20to%20the%20clustering%20level%20(clustering%0A%20%20%20%20by%20zip%20rather%20than%20block%20group%20widens%20the%20SEs%20and%20pulls%20designation-alone%20to%20p%20%E2%89%88%200.11).%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(plt%2C%20results)%3A%0A%20%20%20%20%23%20Coefficient%20shrinkage%3A%20each-alone%20vs%20both-in.%0A%20%20%20%20_fig%2C%20_ax%20%3D%20plt.subplots(figsize%3D(8%2C%204))%0A%20%20%20%20_colors%20%3D%20%5B%22%234C72B0%22%2C%20%22%23C44E52%22%2C%20%22%234C72B0%22%2C%20%22%23C44E52%22%5D%0A%20%20%20%20_ax.errorbar(results%5B%22coef%22%5D%2C%20range(len(results))%2C%20xerr%3D1.96%20*%20results%5B%22se%22%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20fmt%3D%22o%22%2C%20capsize%3D4%2C%20ecolor%3D%22gray%22%2C%20mfc%3D%22white%22%2C%20mec%3D%22black%22%2C%20ls%3D%22none%22)%0A%20%20%20%20for%20_i%2C%20(_c%2C%20_col)%20in%20enumerate(zip(results%5B%22coef%22%5D%2C%20_colors))%3A%0A%20%20%20%20%20%20%20%20_ax.scatter(%5B_c%5D%2C%20%5B_i%5D%2C%20color%3D_col%2C%20zorder%3D5%2C%20s%3D60)%0A%20%20%20%20_ax.axvline(0%2C%20color%3D%22gray%22%2C%20lw%3D1)%0A%20%20%20%20_ax.set_yticks(range(len(results)))%0A%20%20%20%20_ax.set_yticklabels(results.index)%0A%20%20%20%20_ax.set_xlabel(%22coefficient%20(log%20points%3B%20%20%3C%200%20%3D%20more%20decline)%22)%0A%20%20%20%20_ax.set_title(%22Designation%20vs.%20realized%20claims%20%E2%80%94%20both%20shrink%2C%20realized%20holds%22)%0A%20%20%20%20_ax.grid(alpha%3D0.3%2C%20axis%3D%22x%22)%0A%20%20%20%20_fig%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(df%2C%20plt)%3A%0A%20%20%20%20%23%20Why%20the%20horse%20race%20is%20hard%3A%20realized%20claim%20intensity%20by%20designation%20(they%20overlap%2Fcorrelate).%0A%20%20%20%20_fig%2C%20_ax%20%3D%20plt.subplots(figsize%3D(7%2C%204))%0A%20%20%20%20_data%20%3D%20%5Bdf.loc%5Bdf%5B%22in_sfha%22%5D%20%3D%3D%200%2C%20%22bg_claims_per_parcel%22%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20df.loc%5Bdf%5B%22in_sfha%22%5D%20%3D%3D%201%2C%20%22bg_claims_per_parcel%22%5D%5D%0A%20%20%20%20_ax.boxplot(_data%2C%20vert%3DFalse%2C%20tick_labels%3D%5B%22dry%20(X)%22%2C%20%22flood%20zone%22%5D%2C%20showfliers%3DFalse)%0A%20%20%20%20_ax.set_xlabel(%22block-group%20realized%202024%20claims%20per%20parcel%22)%0A%20%20%20%20_ax.set_title(%22Realized%20flooding%20by%20designation%20%E2%80%94%20overlapping%2C%20hence%20collinear%20(r%E2%89%880.67)%22)%0A%20%20%20%20_ax.grid(alpha%3D0.3%2C%20axis%3D%22x%22)%0A%20%20%20%20_fig%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(%22%22%22%0A%20%20%20%20%23%23%20Verdict%0A%0A%20%20%20%20Realized%20block-group%20flooding%20**edges%20out**%20the%20FEMA%20designation%20when%20both%20compete%20%E2%80%94%0A%20%20%20%20a%20*hint*%20that%20the%20market%20prices%20where%20the%20water%20actually%20went%20over%20the%20label%20per%20se.%0A%20%20%20%20But%20the%20two%20are%20too%20collinear%20(r%E2%89%880.67)%20to%20separate%20cleanly%2C%20the%20claims%20signal%20carries%0A%20%20%20%20the%20insurance-take-up%20confound%2C%20and%20the%20significance%20is%20fragile.%20So%20this%20is%20a%0A%20%20%20%20**mechanism%20refinement%2C%20not%20a%20new%20headline**%3A%20it%20nudges%20the%20interpretation%20of%20the%0A%20%20%20%20~9%25%20flood%20penalty%20(F21)%20toward%20%22realized%20flooding%22%20rather%20than%20overturning%20anything.%0A%20%20%20%20The%20decision%20read%20is%20unchanged%20%E2%80%94%20flood%20(designation%20*and*%20realized)%20hurts%20value%2C%20the%0A%20%20%20%20ridge%20has%20neither%2C%20so%20dry%20ground%20stays%20the%20sound%20bet.%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
e4db95529fd124257bedf93689a66823