import%20marimo%0A%0A__generated_with%20%3D%20%220.26.0%22%0Aapp%20%3D%20marimo.App()%0A%0Awith%20app.setup%3A%0A%20%20%20%20import%20sys%0A%20%20%20%20import%20warnings%0A%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%20scipy%0A%20%20%20%20import%20sklearn%0A%20%20%20%20import%20statsmodels%0A%20%20%20%20import%20statsmodels.api%20as%20sm%0A%20%20%20%20import%20statsmodels.formula.api%20as%20smf%0A%20%20%20%20from%20IPython.display%20import%20display%0A%20%20%20%20from%20loguru%20import%20logger%0A%0A%20%20%20%20logger.remove()%0A%20%20%20%20logger.add(sys.stderr%2C%20level%3D%22WARNING%22)%20%20%23%20production%20logs%20at%20DEBUG%3B%20keep%20the%20notebook%20readable%0A%20%20%20%20warnings.filterwarnings(%22ignore%22%2C%20category%3DFutureWarning)%0A%20%20%20%20from%20statsmodels.tools.sm_exceptions%20import%20ConvergenceWarning%0A%0A%20%20%20%20from%20labkit.show%20import%20show%20%20%23%20a%20function's%20real%20source%2C%20linked%20to%20the%20pinned%20commit%0A%0A%20%20%20%20warnings.filterwarnings(%0A%20%20%20%20%20%20%20%20%22ignore%22%2C%20category%3DConvergenceWarning%0A%20%20%20%20)%20%20%23%20MixedLM%20retries%20other%20optimisers%3B%20see%202.5%0A%0A%20%20%20%20print(%0A%20%20%20%20%20%20%20%20%22numpy%22%2C%0A%20%20%20%20%20%20%20%20np.__version__%2C%0A%20%20%20%20%20%20%20%20%22%7C%20pandas%22%2C%0A%20%20%20%20%20%20%20%20pd.__version__%2C%0A%20%20%20%20%20%20%20%20%22%7C%20statsmodels%22%2C%0A%20%20%20%20%20%20%20%20statsmodels.__version__%2C%0A%20%20%20%20%20%20%20%20%22%7C%20scikit-learn%22%2C%0A%20%20%20%20%20%20%20%20sklearn.__version__%2C%0A%20%20%20%20%20%20%20%20%22%7C%20scipy%22%2C%0A%20%20%20%20%20%20%20%20scipy.__version__%2C%0A%20%20%20%20)%0A%0A%20%20%20%20%23%20Imported%20here%20because%20later%20cells%20use%20them%20too.%0A%20%20%20%20from%20area_module.features.joint%20import%20level_trends%0A%20%20%20%20from%20model_foundry.common.modelling.model_core%20import%20GEO_IDENTIFIER%2C%20SEASON_YEAR%2C%20output_column%0A%20%20%20%20from%20sklearn.linear_model%20import%20HuberRegressor%0A%20%20%20%20from%20yield_module.models.mixed%20import%20MixedLm%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20import%20marimo%20as%20mo%0A%0A%20%20%20%20return%20(mo%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%20Trends%3A%20the%20baseline%20under%20every%20area%20and%20yield%20forecast%0A%0A%20%20%20%20**What%20you'll%20learn**%0A%0A%20%20%20%201.%20How%20a%20trend%20is%20fitted%20with%20the%20tools%20the%20documentation%20teaches%3A%20a%20least-squares%20line%2C%20a%20robust%20Huber%20line%2C%20a%20Gaussian%20smoother%20and%20a%20mixed%20model.%0A%20%20%20%202.%20How%20model-foundry's%20production%20trends%20work%2C%20run%20here%20on%20a%20synthetic%20county%20panel%3A%20%60PartialPoolingTrend%60%2C%20%60HuberPoolingTrend%60%2C%20%60GaussianSmoothTrend%60%2C%20%60MixedLm%60%20and%20area's%20walk-forward%20%60level_trends%60.%0A%20%20%20%203.%20Why%20the%20choice%20is%20a%20scientific%20decision.%20You'll%20measure%20pooling%2C%20robustness%20to%20a%20drought%2C%20lag%20at%20the%20end%20of%20a%20series%2C%20and%20how%20much%20a%20trend%20that%20peeks%20at%20the%20future%20flatters%20itself.%0A%0A%20%20%20%20**How%20to%20run%3A**%20follow%20the%20set-up%20in%20%5Bacademy%2FREADME.md%5D(https%3A%2F%2Fgithub.com%2Ftreefera%2Ftf-model-lab%2Fblob%2Fmain%2Facademy%2FREADME.md%23setting-up)%2C%20then%20from%20%60academy%2F%60%2C%20open%20it%20with%20%60uv%20run%20marimo%20edit%202-time-and-signals%2F2.2-trends%2Fnotebook.py%60%3B%20marimo%20runs%20the%20cells%20in%20the%20order%20their%20results%20are%20needed%2C%20and%20re-runs%20the%20cells%20that%20depend%20on%20any%20cell%20you%20change.%20Everything%20runs%20offline%20in%20a%20few%20seconds.%0A%0A%20%20%20%20The%20production%20code%20is%20imported%2C%20not%20copied%3A%20%60model-foundry-common%60%2C%20%60yield-module%60%20and%20%60area-module%60%20are%20pinned%20to%20model-foundry%20%60f69eef9%60%20in%20%60pyproject.toml%60.%20Every%20%60show(...)%60%20cell%20prints%20the%20real%20source%20with%20a%20permalink.%0A%0A%20%20%20%20The%20data%20is%20**synthetic%20and%20labelled%20as%20such**.%20We%20invent%20the%20true%20trend%2C%20so%20we%20can%20measure%20every%20method's%20error%20against%20the%20truth%2C%20which%20real%20data%20never%20allows.%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%20Part%201%3A%20the%20libraries%2C%20as%20their%20documentation%20introduces%20them%0A%0A%20%20%20%20%23%23%201.1%20A%20least-squares%20line%0A%0A%20%20%20%20A%20trend%20is%2C%20at%20heart%2C%20a%20straight%20line%20through%20a%20series%3A%20yield%20%24y_t%24%20against%20season%20%24t%24.%20Ordinary%20least%20squares%20(OLS)%20picks%20the%20slope%20%24b%24%20and%20intercept%20%24a%24%20that%20minimise%20%24%5Csum_t%20(y_t%20-%20a%20-%20b%5C%2Ct)%5E2%24.%20With%20%24x_t%20%3D%20t%20-%20%5Cbar%20t%24%20and%20%24d_t%20%3D%20y_t%20-%20%5Cbar%20y%24%2C%20the%20answer%20is%0A%0A%20%20%20%20%24%24b%20%3D%20%5Cfrac%7B%5Csum_t%20x_t%20d_t%7D%7B%5Csum_t%20x_t%5E2%7D%20%3D%20%5Cfrac%7BS_%7Bxy%7D%7D%7BS_%7Bxx%7D%7D%2C%20%5Cqquad%20%5Cmathrm%7BSE%7D(b)%20%3D%20%5Csqrt%7B%5Cfrac%7B%5Cmathrm%7BRSS%7D%2F(n-2)%7D%7BS_%7Bxx%7D%7D%7D%24%24%0A%0A%20%20%20%20where%20RSS%20is%20the%20residual%20sum%20of%20squares.%20%5B%60numpy.polyfit%60%5D(https%3A%2F%2Fnumpy.org%2Fdoc%2F2.5%2Freference%2Fgenerated%2Fnumpy.polyfit.html)%20gives%20%24b%24%20and%20%24a%24%3B%20%5Bstatsmodels%20OLS%5D(https%3A%2F%2Fwww.statsmodels.org%2Fv0.15.0%2Fregression.html)%20also%20gives%20the%20standard%20error.%20Here%20are%20both%2C%20checked%20against%20the%20formula%20by%20hand%3A%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s1_1_least_squares_line()%3A%0A%20%20%20%20rng%20%3D%20np.random.default_rng(0)%0A%20%20%20%20t%20%3D%20np.arange(2000%2C%202021)%0A%20%20%20%20y%20%3D%20150%20%2B%201.8%20*%20(t%20-%202010)%20%2B%20rng.normal(0%2C%205%2C%20t.size)%0A%0A%20%20%20%20_b%2C%20a%20%3D%20np.polyfit(t%2C%20y%2C%201)%0A%20%20%20%20ols%20%3D%20sm.OLS(y%2C%20sm.add_constant(t)).fit()%0A%20%20%20%20x%2C%20d%20%3D%20t%20-%20t.mean()%2C%20y%20-%20y.mean()%0A%20%20%20%20b_hand%20%3D%20(x%20%40%20d)%20%2F%20(x%20%40%20x)%0A%20%20%20%20se_hand%20%3D%20np.sqrt(((d%20-%20b_hand%20*%20x)%20**%202).sum()%20%2F%20(t.size%20-%202)%20%2F%20(x%20%40%20x))%0A%20%20%20%20print(%0A%20%20%20%20%20%20%20%20f%22polyfit%20slope%20%7B_b%3A.4f%7D%20%7C%20statsmodels%20slope%20%7Bols.params%5B1%5D%3A.4f%7D%20SE%20%7Bols.bse%5B1%5D%3A.4f%7D%20%7C%20by%20hand%20%7Bb_hand%3A.4f%7D%20SE%20%7Bse_hand%3A.4f%7D%22%0A%20%20%20%20)%0A%20%20%20%20return%20ols%2C%20rng%2C%20t%2C%20y%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%201.2%20A%20robust%20line%3A%20Huber%20regression%0A%0A%20%20%20%20OLS%20squares%20every%20residual%2C%20so%20one%20huge%20miss%20(a%20drought)%20pulls%20the%20line%20hard.%20The%20%5BHuber%20loss%5D(https%3A%2F%2Fen.wikipedia.org%2Fwiki%2FHuber_loss)%20is%20quadratic%20for%20small%20residuals%20and%20**linear**%20beyond%20a%20threshold%20%24%5Cepsilon%5Csigma%24%2C%20so%20a%20single%20catastrophe%20counts%20for%20much%20less.%20%5Bscikit-learn's%20%60HuberRegressor%60%5D(https%3A%2F%2Fscikit-learn.org%2F1.9%2Fmodules%2Fgenerated%2Fsklearn.linear_model.HuberRegressor.html)%20implements%20it%2C%20and%20so%20does%20statsmodels'%20%5B%60RLM%60%5D(https%3A%2F%2Fwww.statsmodels.org%2Fv0.15.0%2Fgenerated%2Fstatsmodels.robust.robust_linear_model.RLM.html)%20with%20the%20%5B%60HuberT%60%5D(https%3A%2F%2Fwww.statsmodels.org%2Fv0.15.0%2Fgenerated%2Fstatsmodels.robust.norms.HuberT.html)%20norm.%0A%0A%20%20%20%20The%20fair%20benchmark%20isn't%20the%20true%20slope%201.8%20but%20the%20OLS%20slope%20of%20the%20**same%20sample%20without%20the%20drought**%20(1.1%20above)%3A%20no%20method%20can%20recover%20more%20than%20the%20sample%20holds.%20%60RLM%60%20also%20reports%20a%20**robust%20standard%20error**%20for%20the%20Huber%20slope%20(Huber's%20%22sandwich%22%20formula%2C%20%60cov%3D%22H1%22%60).%20Keep%20it%20in%20mind%20for%20the%20study%20question%20in%203.5%2C%20which%20is%20about%20exactly%20that%20standard%20error.%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s1_2_robust_line_huber(ols%2C%20t%2C%20y)%3A%0A%20%20%20%20from%20sklearn.linear_model%20import%20LinearRegression%0A%0A%20%20%20%20y_drought%20%3D%20y.copy()%0A%20%20%20%20y_drought%5Bt%20%3D%3D%202018%5D%20-%3D%2045%20%20%23%20one%20catastrophic%20season%20near%20the%20end%20of%20the%20series%0A%20%20%20%20X%20%3D%20(t%20-%20t.mean()).reshape(-1%2C%201)%20%20%23%20centred%20season%3A%20see%20the%20next%20cell%20for%20why%0A%20%20%20%20rlm%20%3D%20sm.RLM(y_drought%2C%20sm.add_constant(X)%2C%20M%3Dsm.robust.norms.HuberT()).fit(cov%3D%22H1%22)%0A%20%20%20%20print(%0A%20%20%20%20%20%20%20%20f%22true%20slope%201.8%20%7C%20this%20sample%20without%20the%20drought%2C%20OLS%20%7Bols.params%5B1%5D%3A.3f%7D%20(the%20fair%20benchmark)%22%0A%20%20%20%20)%0A%20%20%20%20print(%0A%20%20%20%20%20%20%20%20f%22with%20the%20drought%3A%20OLS%20%7BLinearRegression().fit(X%2C%20y_drought).coef_%5B0%5D%3A.3f%7D%22%0A%20%20%20%20%20%20%20%20f%22%20%7C%20Huber%2C%20scikit-learn%20%7BHuberRegressor(epsilon%3D1.35).fit(X%2C%20y_drought).coef_%5B0%5D%3A.3f%7D%22%0A%20%20%20%20%20%20%20%20f%22%20%7C%20Huber%2C%20statsmodels%20RLM%20%7Brlm.params%5B1%5D%3A.3f%7D%20(robust%20SE%20%7Brlm.bse%5B1%5D%3A.3f%7D)%22%0A%20%20%20%20)%0A%20%20%20%20return%20(y_drought%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20**Centre%20the%20x-axis%20before%20an%20iterative%20fit.**%20%60HuberRegressor%60%20searches%20step%20by%20step%20(with%20the%20L-BFGS%20optimiser)%20for%20three%20numbers%20at%20once%3A%20the%20slope%2C%20the%20intercept%20and%20the%20noise%20scale%20%CF%83.%20Fed%20raw%20calendar%20years%2C%20around%202000%20to%202020%2C%20the%20right%20intercept%20is%20about%20%E2%88%923%2C000%20while%20the%20slope%20is%20about%202%2C%20so%20the%20problem%20is%20badly%20scaled.%20The%20optimiser%20stops%20early%2C%20because%20its%20convergence%20test%20is%20met%20long%20before%20it%20reaches%20the%20answer.%20The%20tiny%20default%20L2%20penalty%20on%20the%20slope%20(%60alpha%3D1e-4%60)%20plays%20no%20part%3A%20the%20next%20cell%20sets%20%60alpha%3D0%60%20as%20well%2C%20and%20prints%20how%20many%20iterations%20each%20fit%20took.%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s1_2_robust_line_huber_2(t%2C%20y_drought)%3A%0A%20%20%20%20for%20axis%2C%20season_x%20in%20(%0A%20%20%20%20%20%20%20%20(%22raw%20calendar%20years%22%2C%20t)%2C%0A%20%20%20%20%20%20%20%20(%22centred%20seasons%22%2C%20t%20-%20t.mean())%2C%0A%20%20%20%20)%3A%0A%20%20%20%20%20%20%20%20for%20alpha%20in%20(1e-4%2C%200.0)%3A%20%20%23%201e-4%20is%20scikit-learn's%20default%20L2%20penalty%0A%20%20%20%20%20%20%20%20%20%20%20%20_h%20%3D%20HuberRegressor(epsilon%3D1.35%2C%20alpha%3Dalpha).fit(season_x.reshape(-1%2C%201)%2C%20y_drought)%0A%20%20%20%20%20%20%20%20%20%20%20%20print(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20f%22%7Baxis%3A18%7D%20alpha%3D%7Balpha%3A%3C6g%7D%20slope%20%7B_h.coef_%5B0%5D%3A.3f%7D%20%20intercept%20%7B_h.intercept_%3A7.1f%7D%20%20iterations%20%7B_h.n_iter_%7D%22%0A%20%20%20%20%20%20%20%20%20%20%20%20)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20On%20raw%20years%20the%20fit%20stops%20after%2019%20iterations%2C%20well%20short%20of%20its%20100-iteration%20limit%2C%20with%20the%20intercept%20still%20near%200%2C%20so%20the%20slope%20(0.073)%20is%20doing%20the%20intercept's%20job.%20The%20penalty%20changes%20nothing.%20That's%20why%20model-foundry's%20own%20Huber%20fit%20(Part%202)%20works%20on%20seasons%20centred%20on%20each%20geography's%20mean.%20Same%20model%2C%20very%20different%20answers%2C%20purely%20from%20the%20axis.%0A%0A%20%20%20%20%23%23%201.3%20A%20Gaussian%20smoother%0A%0A%20%20%20%20Instead%20of%20a%20line%2C%20smooth%20the%20series%3A%20each%20smoothed%20value%20is%20a%20weighted%20average%20of%20nearby%20seasons%2C%20with%20weights%20from%20a%20bell%20curve%20of%20width%20%24%5Csigma%24%20seasons.%20%5B%60scipy.ndimage.gaussian_filter1d%60%5D(https%3A%2F%2Fdocs.scipy.org%2Fdoc%2Fscipy-1.18.0%2Freference%2Fgenerated%2Fscipy.ndimage.gaussian_filter1d.html)%20does%20it.%20Its%20%60truncate%3D4.0%60%20default%20cuts%20the%20bell%20curve%20at%204%CF%83%2C%20so%20with%20%CF%83%20%3D%204%20each%20smoothed%20value%20averages%20up%20to%2016%20seasons%20either%20side.%20At%20the%20**end**%20of%20a%20series%20there's%20nothing%20to%20the%20right%2C%20and%20%60mode%60%20decides%20how%20the%20edge%20is%20padded%3A%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s1_3_gaussian_smoother(t%2C%20y)%3A%0A%20%20%20%20from%20scipy.ndimage%20import%20gaussian_filter1d%0A%0A%20%20%20%20true_line%20%3D%20150%20%2B%201.8%20*%20(t%20-%202010)%0A%20%20%20%20for%20mode%20in%20(%22nearest%22%2C%20%22reflect%22)%3A%0A%20%20%20%20%20%20%20%20_s%20%3D%20gaussian_filter1d(y%2C%20sigma%3D4%2C%20mode%3Dmode)%0A%20%20%20%20%20%20%20%20print(%0A%20%20%20%20%20%20%20%20%20%20%20%20f%22mode%3D%7Bmode%3A8%7D%20last%20smoothed%20%7B_s%5B-1%5D%3A.1f%7D%20vs%20true%20line%20%7Btrue_line%5B-1%5D%3A.1f%7D%20(gap%20%7B_s%5B-1%5D%20-%20true_line%5B-1%5D%3A%2B.1f%7D)%22%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20pure%20%3D%201.8%20*%20np.arange(35.0)%20%20%23%20a%20noise-free%20line%2C%20slope%201.8%20per%20season%0A%20%20%20%20lag%20%3D%20gaussian_filter1d(pure%2C%20sigma%3D4%2C%20mode%3D%22nearest%22)%5B-1%5D%20-%20pure%5B-1%5D%0A%20%20%20%20theory%20%3D%20-1.8%20*%204%20%2F%20np.sqrt(2%20*%20np.pi)%0A%20%20%20%20print(%0A%20%20%20%20%20%20%20%20f%22noise-free%20line%2C%20mode%3Dnearest%3A%20end%20lag%20%7Blag%3A.3f%7D%3B%20continuous%20approximation%20-b%C2%B7%CF%83%2F%E2%88%9A(2%CF%80)%20%3D%20%7Btheory%3A.3f%7D%3B%22%0A%20%20%20%20%20%20%20%20f%22%20carried%20flat%20one%20more%20season%3A%20%7Blag%20-%201.8%3A.2f%7D%22%0A%20%20%20%20)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20Both%20modes%20sit%20**below**%20a%20rising%20line%20at%20the%20end%2C%20because%20at%20the%20last%20season%20the%20window%20has%20only%20the%20past%20to%20average.%20That%20one-sided%20window%20causes%20the%20lag%3B%20the%20padding%20mode%20only%20decides%20how%20much.%20%60nearest%60%20repeats%20the%20last%20value%2C%20which%20**limits**%20the%20lag%20(%E2%88%923.4)%20compared%20with%20%60reflect%60%20(%E2%88%925.2).%0A%0A%20%20%20%20On%20a%20noise-free%20line%20of%20slope%20%24b%24%2C%20the%20lag%20with%20%60nearest%60%20padding%20is%20close%20to%20%24b%5C%2C%5Csigma%2F%5Csqrt%7B2%5Cpi%7D%24%2C%20the%20value%20for%20a%20continuous%20kernel%3A%202.87%20bu%2Fac%20for%20%24b%20%3D%201.8%24%20and%20%CF%83%20%3D%204%2C%20against%202.86%20from%20SciPy's%20truncated%2C%20discrete%20kernel%2C%20as%20the%20last%20line%20printed.%20Carry%20that%20level%20flat%20into%20the%20next%20season%20and%20you%20lose%20another%20%24b%24%2C%20about%204.66%20in%20all.%20Remember%20that%20gap%3B%20Part%203%20measures%20its%20cost.%0A%0A%20%20%20%20%23%23%201.4%20A%20mixed%20model%0A%0A%20%20%20%20A%20mixed%20(multilevel)%20model%20fits%20one%20shared%20trend%20plus%20a%20random%20deviation%20per%20group%3A%20here%2C%20each%20county's%20own%20intercept%20and%20slope%2C%20drawn%20from%20a%20common%20distribution.%20%5Bstatsmodels%20%60MixedLM%60%5D(https%3A%2F%2Fwww.statsmodels.org%2Fv0.15.0%2Fmixed_linear.html)%20fits%20it%20by%20**restricted%20maximum%20likelihood%20(REML)**.%20Plain%20maximum%20likelihood%20underestimates%20variances%2C%20because%20it%20ignores%20the%20degrees%20of%20freedom%20the%20fixed%20effects%20use%20up.%20REML%20corrects%20for%20them%2C%20as%20dividing%20by%20%24n-2%24%20does%20in%20the%20OLS%20standard%20error%2C%20which%20matters%20most%20when%20there%20are%20few%20groups.%0A%0A%20%20%20%20This%20toy%20uses%20%60re_formula%3D%22~x%22%60%3A%20each%20county%20gets%20a%20random%20intercept%20and%20a%20random%20slope%2C%20and%20the%20two%20may%20be%20**correlated**.%20Production%20%60MixedLm%60%20(2.5)%20is%20built%20differently%3A%20a%20random%20state%20intercept%2C%20plus%20county%20slopes%20and%20county%20intercepts%20as%20**independent**%20variance%20components%20(%5B%60VCSpec%60%5D(https%3A%2F%2Fwww.statsmodels.org%2Fv0.15.0%2Fgenerated%2Fstatsmodels.regression.mixed_linear_model.VCSpec.html))%2C%20with%20no%20intercept%E2%80%93slope%20covariance.%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s1_4_mixed_model(rng)%3A%0A%20%20%20%20import%20copy%0A%0A%20%20%20%20mixed_rng%20%3D%20copy.deepcopy(rng)%20%20%23%201.1's%20generator%2C%20continued%20without%20advancing%20it%0A%20%20%20%20toy%20%3D%20pd.DataFrame(%0A%20%20%20%20%20%20%20%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22county%22%3A%20c%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22season%22%3A%20s%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22bu_ac%22%3A%20150%20%2B%20off%20%2B%20(1.8%20%2B%20dslope)%20*%20(s%20-%202010)%20%2B%20mixed_rng.normal(0%2C%205)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%20%20%20%20for%20c%2C%20off%2C%20dslope%20in%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20(f%22c%7Bi%7D%22%2C%20mixed_rng.normal(0%2C%2010)%2C%20mixed_rng.normal(0%2C%200.4))%20for%20i%20in%20range(8)%0A%20%20%20%20%20%20%20%20%20%20%20%20%5D%0A%20%20%20%20%20%20%20%20%20%20%20%20for%20s%20in%20range(2000%2C%202021)%0A%20%20%20%20%20%20%20%20%5D%0A%20%20%20%20)%0A%20%20%20%20toy%5B%22x%22%5D%20%3D%20toy%5B%22season%22%5D%20-%202010%0A%20%20%20%20fit%20%3D%20smf.mixedlm(%22bu_ac%20~%20x%22%2C%20toy%2C%20groups%3Dtoy%5B%22county%22%5D%2C%20re_formula%3D%22~x%22).fit(reml%3DTrue)%0A%20%20%20%20print(fit.fe_params.round(3).to_dict())%0A%20%20%20%20print(%22county%20c0%20random%20effects%3A%22%2C%20fit.random_effects%5B%22c0%22%5D.round(3).to_dict())%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%20Part%202%3A%20how%20our%20production%20code%20does%20it%0A%0A%20%20%20%20%23%23%202.1%20One%20synthetic%20panel%0A%0A%20%20%20%20Eleven%20long-history%20counties%20in%20two%20states%20(six%20in%20Iowa%2C%20five%20in%20Nebraska)%2C%20seasons%201990%E2%80%932024%20(yields%20in%20bu%2Fac%2C%20like%20corn)%2C%20plus%20one%20short-history%20county%3A%0A%0A%20%20%20%20-%20a%20true%20linear%20technology%20trend%20per%20county%2C%20slope%20around%201.8%20bu%2Fac%2Fyr%20with%20real%20spread%20between%20counties%3B%0A%20%20%20%20-%20weather%20shocks%3A%20a%20state-wide%20part%20plus%20a%20county%20part%3B%0A%20%20%20%20-%20**one%20shared%20drought%20in%202012**%20(%E2%88%9245%20bu%2Fac%20in%20Iowa%2C%20%E2%88%9235%20in%20Nebraska)%3B%0A%20%20%20%20-%20one%20**short-history%20county**%2C%20%60newcounty%60%2C%20with%20only%20six%20noisy%20seasons%20(2019%E2%80%932024)%2C%20to%20show%20pooling.%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s2_1_synthetic_panel()%3A%0A%20%20%20%20SEASONS%20%3D%20np.arange(1990%2C%202025)%0A%20%20%20%20STATES%20%3D%20%7B%0A%20%20%20%20%20%20%20%20%22iowa%22%3A%20%5B%22story%22%2C%20%22polk%22%2C%20%22boone%22%2C%20%22dallas%22%2C%20%22jasper%22%2C%20%22marshall%22%5D%2C%0A%20%20%20%20%20%20%20%20%22nebraska%22%3A%20%5B%22adams%22%2C%20%22clay%22%2C%20%22hall%22%2C%20%22kearney%22%2C%20%22york%22%5D%2C%0A%20%20%20%20%7D%0A%20%20%20%20DROUGHT%20%3D%20%7B%22iowa%22%3A%2045.0%2C%20%22nebraska%22%3A%2035.0%7D%0A%20%20%20%20DROUGHT_YEAR%20%3D%202012%20%20%23%20exercise%202%20moves%20it%20to%202000%0A%0A%20%20%20%20def%20make_panel(seed%3D3)%3A%0A%20%20%20%20%20%20%20%20%23%20Synthetic%20county%20yields%3A%20true%20per-county%20lines%2C%20weather%20shocks%2C%20a%20DROUGHT_YEAR%20drought%2C%20one%20short%20county.%0A%20%20%20%20%20%20%20%20r%20%3D%20np.random.default_rng(seed)%0A%20%20%20%20%20%20%20%20rows%2C%20truth%20%3D%20%5B%5D%2C%20%7B%7D%0A%20%20%20%20%20%20%20%20for%20state%2C%20names%20in%20STATES.items()%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20state_shock%20%3D%20dict(zip(SEASONS%2C%20r.normal(0%2C%204%2C%20SEASONS.size)%2C%20strict%3DFalse))%0A%20%20%20%20%20%20%20%20%20%20%20%20for%20name%20in%20names%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20geo%20%3D%20f%22ADM0%3AUSA%2FADM1%3A%7Bstate%7D%2FADM2%3A%7Bname%7D%22%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20slope%2C%20level%2C%20area%20%3D%20(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%201.8%20%2B%20r.normal(0%2C%200.6)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20160%20%2B%20r.normal(0%2C%2012)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20r.uniform(4e4%2C%201.2e5)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20truth%5Bgeo%5D%20%3D%20slope%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20for%20season%20in%20SEASONS%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20shock%20%3D%20(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20state_shock%5Bseason%5D%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%2B%20r.normal(0%2C%204)%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(DROUGHT%5Bstate%5D%20if%20season%20%3D%3D%20DROUGHT_YEAR%20else%200.0)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20line%20%3D%20level%20%2B%20slope%20*%20(season%20-%202010)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20rows.append(%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%7B%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%20GEO_IDENTIFIER%3A%20geo%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%20SEASON_YEAR%3A%20int(season)%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%22yield%22%3A%20line%20%2B%20shock%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%22true_trend%22%3A%20line%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%22area%22%3A%20area%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%7D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20geo%2C%20slope%20%3D%20%22ADM0%3AUSA%2FADM1%3Aiowa%2FADM2%3Anewcounty%22%2C%202.6%0A%20%20%20%20%20%20%20%20truth%5Bgeo%5D%20%3D%20slope%0A%20%20%20%20%20%20%20%20for%20season%20in%20range(2019%2C%202025)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20line%20%3D%20150%20%2B%20slope%20*%20(season%20-%202010)%0A%20%20%20%20%20%20%20%20%20%20%20%20rows.append(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20GEO_IDENTIFIER%3A%20geo%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20SEASON_YEAR%3A%20season%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22yield%22%3A%20line%20%2B%20r.normal(0%2C%209)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22true_trend%22%3A%20line%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22area%22%3A%203e4%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20return%20pd.DataFrame(rows)%2C%20pd.Series(truth%2C%20name%3D%22true_slope%22)%0A%0A%20%20%20%20panel%2C%20true_slope%20%3D%20make_panel()%0A%20%20%20%20SHORT%20%3D%20%22ADM0%3AUSA%2FADM1%3Aiowa%2FADM2%3Anewcounty%22%0A%20%20%20%20STORY%20%3D%20%22ADM0%3AUSA%2FADM1%3Aiowa%2FADM2%3Astory%22%0A%20%20%20%20print(panel.shape%2C%20%22rows%3B%22%2C%20panel%5BGEO_IDENTIFIER%5D.nunique()%2C%20%22geographies%22)%0A%20%20%20%20panel.head(3)%0A%20%20%20%20return%20(%0A%20%20%20%20%20%20%20%20DROUGHT%2C%0A%20%20%20%20%20%20%20%20DROUGHT_YEAR%2C%0A%20%20%20%20%20%20%20%20SHORT%2C%0A%20%20%20%20%20%20%20%20STATES%2C%0A%20%20%20%20%20%20%20%20STORY%2C%0A%20%20%20%20%20%20%20%20make_panel%2C%0A%20%20%20%20%20%20%20%20panel%2C%0A%20%20%20%20%20%20%20%20true_slope%2C%0A%20%20%20%20)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%202.2%20%60PartialPoolingTrend%60%3A%20a%20line%20per%20county%2C%20shrunk%20towards%20the%20nation%0A%0A%20%20%20%20The%20shared%20trend%20in%20%60model_foundry.common%60.%20Read%20%60fit%60%20first%2C%20then%20the%20three%20helpers%20it%20calls.%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s2_2_partialpoolingtrend_line_per()%3A%0A%20%20%20%20from%20model_foundry.common.quantity_modules.trend%20import%20(%0A%20%20%20%20%20%20%20%20PartialPoolingTrend%2C%0A%20%20%20%20%20%20%20%20geography_slopes%2C%0A%20%20%20%20%20%20%20%20national_slope%2C%0A%20%20%20%20%20%20%20%20shrink_towards%2C%0A%20%20%20%20)%0A%0A%20%20%20%20show(PartialPoolingTrend)%0A%20%20%20%20show(national_slope)%0A%20%20%20%20show(geography_slopes)%0A%20%20%20%20show(shrink_towards)%0A%20%20%20%20return%20(%0A%20%20%20%20%20%20%20%20PartialPoolingTrend%2C%0A%20%20%20%20%20%20%20%20geography_slopes%2C%0A%20%20%20%20%20%20%20%20national_slope%2C%0A%20%20%20%20%20%20%20%20shrink_towards%2C%0A%20%20%20%20)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20In%20words%2C%20%60fit%60%20does%20four%20things%3A%0A%0A%20%20%20%201.%20**The%20national%20slope%3A**%20an%20OLS%20line%20through%20the%20area-weighted%20national%20mean%20of%20each%20season%2C%20over%20the%20last%20%60national_window_seasons%60%20seasons.%0A%20%20%20%202.%20**Each%20county's%20own%20OLS%20slope%20and%20its%20standard%20error**%2C%20for%20counties%20with%20at%20least%20%60min_seasons%60%20seasons.%20This%20is%20the%20closed%20form%20from%201.1%2C%20vectorised%20with%20%60groupby%60.%0A%20%20%20%203.%20**Shrinkage%3A**%20each%20county%20slope%20becomes%20%24%5Clambda_i%20b_i%20%2B%20(1-%5Clambda_i)%20b_%7B%5Ctext%7Bnat%7D%7D%24%20with%20%24%5Clambda_i%20%3D%20%5Ctau%5E2%2F(%5Ctau%5E2%20%2B%20%5Cmathrm%7BSE%7D_i%5E2)%24.%20%24%5Ctau%5E2%24%2C%20the%20real%20spread%20of%20slopes%20between%20counties%2C%20is%20estimated%20as%20var(slopes)%20%E2%88%92%20mean(SE%C2%B2).%20A%20noisy%20county%20(big%20SE)%20gets%20%24%5Clambda%20%5Cto%200%24%20and%20takes%20the%20national%20slope%3B%20a%20precise%20one%20keeps%20its%20own.%20This%20is%20a%20**James%E2%80%93Stein-type%20empirical-Bayes%20estimator**%2C%20in%20the%20unequal-variance%20form%20of%20Efron%20and%20Morris%2C%20and%20of%20Fay%20and%20Herriot.%20%22Empirical%20Bayes%22%20means%20the%20prior%20(the%20spread%20%CF%84%C2%B2%20of%20true%20slopes)%20is%20estimated%20from%20the%20data%20itself.%20Two%20details%20to%20notice%3A%0A%20%20%20%20%20%20%20-%20%CF%84%C2%B2%20is%20measured%20around%20the%20**sample%20mean**%20of%20the%20county%20slopes%20(%60np.var%60)%2C%20but%20the%20slopes%20are%20shrunk%20towards%20the%20**national**%20slope.%20The%20two%20centres%20differ.%0A%20%20%20%20%20%20%20-%20%CF%84%C2%B2%20is%20plugged%20in%20as%20if%20known.%20With%2011%20or%2012%20counties%20it%20is%20itself%20uncertain%2C%20and%20nothing%20allows%20for%20that.%0A%20%20%20%204.%20**Intercepts%3A**%20the%20mean%20of%20%24y%20-%20b%5C%2Ct%24%20per%20county%2C%20with%20the%20*shrunk*%20slope.%0A%0A%20%20%20%20%60predict%60%20reads%20intercept%20%2B%20slope%20%C3%97%20season.%20The%20fitted%20state%20is%20just%20two%20numbers%20per%20county%2C%20plus%20the%20national%20slope%2C%20saved%20as%20a%20%60ModelState%60%20table.%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s2_2_partialpoolingtrend_line_per_2(PartialPoolingTrend%2C%20SHORT%2C%20panel)%3A%0A%20%20%20%20train%20%3D%20panel%5Bpanel%5BSEASON_YEAR%5D%20%3C%3D%202023%5D%0A%20%20%20%20trend%20%3D%20PartialPoolingTrend(%0A%20%20%20%20%20%20%20%20source%3D%22yield%22%2C%20weight%3D%22area%22%2C%20min_seasons%3D3%2C%20national_window_seasons%3D30%0A%20%20%20%20).fit(train)%0A%20%20%20%20print(f%22national%20slope%20%7Btrend.national_slope%3A.3f%7D%22)%0A%20%20%20%20intercepts%20%3D%20trend.geographies%5B%22intercept%22%5D%0A%20%20%20%20print(%0A%20%20%20%20%20%20%20%20f%22intercepts%3A%20long-history%20counties%20%7Bintercepts.drop(SHORT).min()%3A%2C.0f%7D%20to%20%7Bintercepts.drop(SHORT).max()%3A%2C.0f%7D%3B%20newcounty%20%7Bintercepts%5BSHORT%5D%3A%2C.0f%7D%22%0A%20%20%20%20)%0A%20%20%20%20trend.geographies.round(3)%0A%20%20%20%20return%20(train%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20Why%20are%20the%20intercepts%20such%20large%20negative%20numbers%3A%20%E2%88%925%2C265%20to%20%E2%88%92708%20for%20the%20long-history%20counties%2C%20and%20%E2%88%928%2C216%20for%20%60newcounty%60%3F%20Because%20model-foundry's%20x-axis%20is%20the%20raw%20calendar%20year%2C%20so%20the%20intercept%20is%20the%20line's%20value%20in%20year%200.%20For%20a%20county%20whose%20line%20sits%20at%20level%20%24L%24%20in%202010%20with%20slope%20%24b%24%2C%20the%20intercept%20is%20%24L%20-%202010%5C%2Cb%24%3A%20steeper%20counties%20have%20more%20negative%20intercepts%2C%20and%20%60newcounty%60's%20inflated%20slope%20gives%20the%20most%20negative%20of%20all.%20Predictions%20are%20unaffected%2C%20since%20intercept%20%2B%20slope%20%C3%97%202024%20is%20the%20same%20number%20however%20you%20shift%20the%20axis.%20But%20the%20stored%20intercept%20isn't%20human-readable.%20%60MixedLm%60%20centres%20on%202000%20(%60CENTRE_SEASON%60)%2C%20and%20area's%20%60level_trends%60%20counts%20days%20since%201980%2C%20so%20three%20conventions%20coexist%20in%20model-foundry.%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%202.3%20%60HuberPoolingTrend%60%3A%20the%20same%2C%20with%20robust%20county%20slopes%0A%0A%20%20%20%20Used%20by%20%60yield_corn_usa%60%20(%60slope%3A%20huber%60).%20It%20subclasses%20%60PartialPoolingTrend%60%20and%20swaps%20only%20the%20county-slope%20step%2C%20using%20its%20own%20iteratively%20reweighted%20least-squares%20Huber%20fit.%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s2_3_huberpoolingtrend_same_robust()%3A%0A%20%20%20%20from%20yield_module.models.huber_trend%20import%20(%0A%20%20%20%20%20%20%20%20HuberPoolingTrend%2C%0A%20%20%20%20%20%20%20%20huber_geography_slopes%2C%0A%20%20%20%20%20%20%20%20huber_slope%2C%0A%20%20%20%20)%0A%0A%20%20%20%20show(HuberPoolingTrend.fit)%0A%20%20%20%20show(huber_slope)%0A%20%20%20%20return%20HuberPoolingTrend%2C%20huber_geography_slopes%2C%20huber_slope%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%60huber_slope%60%20is%20the%20textbook%20IRLS%20(iteratively%20reweighted%20least%20squares)%20loop%3A%0A%0A%20%20%20%201.%20Fit%20a%20weighted%20line.%0A%20%20%20%202.%20Measure%20the%20residuals'%20scale%20robustly.%20The%20**MAD**%20(median%20absolute%20deviation)%20is%20the%20median%20of%20%24%7Cr%20-%20%5Cmathrm%7Bmedian%7D(r)%7C%24%3B%20one%20huge%20residual%20barely%20moves%20it.%20%241.4826%20%5Ctimes%20%5Cmathrm%7BMAD%7D%24%20is%20a%20consistent%20estimator%20of%20%CF%83%20for%20normal%20noise%3A%20it%20converges%20to%20%CF%83%20as%20the%20sample%20grows%2C%20because%20%241.4826%20%3D%201%2F%5CPhi%5E%7B-1%7D(0.75)%24.%0A%20%20%20%203.%20Down-weight%20every%20point%20whose%20residual%20exceeds%20%24%5Cepsilon%20%5Ctimes%24%20scale%2C%20with%20weight%20%24%5Cepsilon%5C%2C%5Ctext%7Bscale%7D%2F%7Cr%7C%24.%0A%20%20%20%204.%20Repeat%20until%20the%20fit%20stops%20moving.%0A%0A%20%20%20%20%60np.polyfit%60%20takes%20%60w%3Dnp.sqrt(weights)%60%2C%20because%20it%20multiplies%20residuals%20by%20%60w%60%20before%20squaring.%20Remember%20that%20line%3A%20area's%20%60level_trends%60%20(2.6)%20calls%20the%20same%20%60np.polyfit%60%20**without**%20the%20square%20root.%0A%0A%20%20%20%20Only%20the%20slope%20is%20robust.%20**F8**%20(%5Bissue%2035%5D(https%3A%2F%2Fgithub.com%2Ftreefera%2Ftf-model-lab%2Fissues%2F35))%20names%20three%20parts%20of%20%60HuberPoolingTrend%60%20that%20aren't%3A%0A%0A%20%20%20%201.%20**The%20standard%20error**%20is%20the%20OLS%20formula%20applied%20to%20the%20Huber%20residuals%2C%20so%20the%20drought%20still%20enters%20it%20squared%20(measured%20in%203.5).%0A%20%20%20%202.%20**The%20intercept**%20is%20the%20plain%20mean%20of%20%24y%20-%20b%5C%2Ct%24%2C%20inherited%20line%20for%20line%20from%20%60PartialPoolingTrend.fit%60%2C%20so%20the%20drought%20enters%20it%20at%20full%20weight%20(3.3b).%0A%20%20%20%203.%20**The%20shrinkage%20target**%20is%20%60national_slope%60%2C%20an%20OLS%20line%20through%20the%20national%20means%2C%20which%20the%20drought%20drags%20down%20too%20(3.5b).%0A%0A%20%20%20%20%23%23%202.4%20%60GaussianSmoothTrend%60%3A%20a%20level%2C%20not%20a%20line%0A%0A%20%20%20%20Used%20by%20%60yield_cotton_usa%60%20(%60method%3A%20gaussian_smooth%2C%20sigma_seasons%3A%204%2C%20fit_level%3A%201%60).%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s2_4_gaussiansmoothtrend_level_not()%3A%0A%20%20%20%20from%20yield_module.models.gaussian_smooth_trend%20import%20GaussianSmoothTrend%2C%20smooth%0A%0A%20%20%20%20show(GaussianSmoothTrend.predict)%0A%20%20%20%20show(smooth)%0A%20%20%20%20return%20(GaussianSmoothTrend%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20Note%20%60predict%60%3A%20a%20season%20inside%20the%20training%20range%20gets%20its%20own%20smoothed%20value%2C%20and%20a%20**new%20season%20gets%20the%20last%20smoothed%20level%20carried%20forward%2C%20flat**.%20It%20has%20no%20slope%20to%20extrapolate%20with.%0A%0A%20%20%20%20%23%23%202.5%20%60MixedLm%60%3A%20trend%20and%20weather%20in%20one%20multilevel%20regression%0A%0A%20%20%20%20The%20yield%20recipe%20can%20replace%20%22trend%20%2B%20residual%20regression%22%20with%20one%20REML%20fit%3A%20a%20fixed%20national%20intercept%20and%20trend%3B%20a%20random%20intercept%20per%20state%3B%20and%20random%20county%20intercepts%20and%20slopes.%20Here%20it's%20shown%20with%20no%20weather%20features%2C%20so%20that%20it's%20a%20pure%20trend%3A%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s2_5_mixedlm_trend_weather(SHORT%2C%20panel)%3A%0A%20%20%20%20show(MixedLm.fit)%0A%20%20%20%20known%20%3D%20panel%5Bpanel%5BGEO_IDENTIFIER%5D%20!%3D%20SHORT%5D%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%2C%20ConvergenceWarning)%0A%20%20%20%20%20%20%20%20_mixed%20%3D%20MixedLm(source%3D%22yield%22%2C%20features%3D%5B%5D%2C%20weight%3D%22area%22%2C%20fit_level%3D2).fit(%0A%20%20%20%20%20%20%20%20%20%20%20%20known%5Bknown%5BSEASON_YEAR%5D%20%3C%3D%202023%5D%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20messages%20%3D%20%5Bstr(w.message)%20for%20w%20in%20caught%20if%20issubclass(w.category%2C%20ConvergenceWarning)%5D%0A%20%20%20%20print(f%22%7Blen(messages)%7D%20convergence%20warning(s)%20raised%20by%20this%20fit%3A%22)%0A%20%20%20%20for%20message%20in%20messages%3A%0A%20%20%20%20%20%20%20%20print(%22%20%20-%22%2C%20message)%0A%20%20%20%20if%20not%20messages%3A%0A%20%20%20%20%20%20%20%20print(%22%20%20none%3A%20the%20first%20optimiser%20in%20the%20list%20converged%22)%0A%20%20%20%20print(%22fixed%20effects%3A%22%2C%20_mixed.fixed.round(3).to_dict())%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%202.6%20Area's%20walk-forward%20%60level_trends%60%0A%0A%20%20%20%20Every%20area%20recipe%20at%20%60f69eef9%60%20is%20a%20**joint**%20model%2C%20and%20its%20trend%20is%20a%20feature%20built%20by%20%60level_trends%60.%20That's%20a%20line%20per%20geography%20through%20the%20seasons%20strictly%20before%20the%20target%2C%20read%20at%201%20July.%20Under%20%60ewm_linear%60%20it's%20recency-weighted%20with%20a%20half-life.%20Corn%20uses%20%60linear%60%3B%20soy%20USA%20uses%20%60ewm_linear%60%20with%20a%20half-life%20of%2010%2C%20cotton%206%2C%20Brazil%20soy%203%2C%20winter%20wheat%2020.%0A%0A%20%20%20%20**F7**%20(%5Bissue%2034%5D(https%3A%2F%2Fgithub.com%2Ftreefera%2Ftf-model-lab%2Fissues%2F34))%3A%20the%20half-life%20acts%20as%20**half**%20its%20stated%20value.%20The%20%60polyfit%60%20weight%20%24w%24%20halves%20every%20half-life%2C%20but%20%60np.polyfit%60%20multiplies%20each%20residual%20by%20%24w%24%20*before*%20squaring%2C%20so%20a%20season's%20share%20of%20the%20squared-error%20loss%20is%20%24w%5E2%24%2C%20which%20halves%20every%20half-life%20%2F%202.%20%60huber_slope%60%20(2.3)%20passes%20%60np.sqrt(weights)%60%20for%20exactly%20this%20reason%3B%20%60level_trends%60%20doesn't.%20After%20the%20source%2C%20the%20next%20cell%20checks%20it%20in%20two%20steps%20on%20one%20county%3A%0A%0A%20%20%20%201.%20%60np.polyfit(...%2C%20w%3Dw)%60%20gives%20the%20same%20slope%20as%20statsmodels%20%5B%60WLS%60%5D(https%3A%2F%2Fwww.statsmodels.org%2Fv0.15.0%2Fgenerated%2Fstatsmodels.regression.linear_model.WLS.html)%20with%20weights%20%24w%5E2%24%2C%20not%20%24w%24.%0A%20%20%20%202.%20%60level_trends%60%20with%20half-life%20%24h%24%20gives%20exactly%20the%20line%20of%20a%20WLS%20whose%20loss%20weights%20halve%20every%20%24h%2F2%24%20seasons.%0A%0A%20%20%20%20It%20also%20counts%20how%20many%20equally%20weighted%20seasons%20the%20weights%20are%20worth%20(Kish's%20effective%20sample%20size%2C%20%24(%5Csum%20%5Clambda)%5E2%2F%5Csum%20%5Clambda%5E2%24%20for%20loss%20weights%20%24%5Clambda%24).%0A%0A%20%20%20%20The%20recipes'%203%2C%206%2C%2010%20and%2020%20act%20as%201.5%2C%203%2C%205%20and%2010.%20The%20issue%20leaves%20the%20remedy%20open.%20If%20the%20half-lives%20were%20tuned%20by%20trying%20values%20against%20hindcast%20scores%2C%20the%20forecasts%20are%20right%20and%20only%20the%20labels%20are%20wrong%2C%20so%20the%20honest%20fix%20may%20be%20to%20relabel%20them%20rather%20than%20change%20%60w%60.%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s2_6_areas_walk_forward(mo%2C%20train)%3A%0A%20%20%20%20show(level_trends)%0A%20%20%20%20observed%20%3D%20train.set_index(%5BGEO_IDENTIFIER%2C%20SEASON_YEAR%5D)%5B%22yield%22%5D%0A%20%20%20%20_lines%20%3D%20level_trends(observed%2C%20%5B2024%5D%2C%20%22ewm_linear%22%2C%2010.0%2C%20gap%3D1)%0A%20%20%20%20mo.output.append(_lines.round(1).head())%0A%0A%0A%40app.cell%0Adef%20s2_6_areas_walk_forward_2(STORY%2C%20train)%3A%0A%20%20%20%20%23%20F7%2C%20step%201%3A%20np.polyfit%20multiplies%20each%20residual%20by%20w%2C%20so%20it%20is%20weighted%20least%20squares%20with%20weights%20w%C2%B2.%0A%20%20%20%20story_rows%20%3D%20train%5Btrain%5BGEO_IDENTIFIER%5D%20%3D%3D%20STORY%5D%0A%20%20%20%20years%2C%20story_yield%20%3D%20story_rows%5BSEASON_YEAR%5D.to_numpy()%2C%20story_rows%5B%22yield%22%5D.to_numpy(float)%0A%20%20%20%20ages%20%3D%20years.max()%20-%20years%0A%20%20%20%20w%20%3D%200.5%20**%20(ages%20%2F%2010)%20%20%23%20the%20weight%20level_trends%20builds%20for%20a%20half-life%20of%2010%0A%20%20%20%20print(f%22polyfit%20slope%20with%20w%3Dw%3A%20%7Bnp.polyfit(years%2C%20story_yield%2C%201%2C%20w%3Dw)%5B0%5D%3A.4f%7D%22)%0A%20%20%20%20for%20_label%2C%20loss_weight%20in%20((%22w%20%22%2C%20w)%2C%20(%22w%C2%B2%22%2C%20w**2))%3A%0A%20%20%20%20%20%20%20%20wls%20%3D%20sm.WLS(story_yield%2C%20sm.add_constant(years.astype(float))%2C%20weights%3Dloss_weight).fit()%0A%20%20%20%20%20%20%20%20print(f%22statsmodels%20WLS%20slope%2C%20weights%20%7B_label%7D%3A%20%7Bwls.params%5B1%5D%3A.4f%7D%22)%0A%0A%20%20%20%20%23%20F7%2C%20step%202%3A%20so%20level_trends%20with%20half-life%20h%20is%20WLS%20whose%20loss%20weights%20halve%20every%20h%2F2%20seasons.%0A%20%20%20%20days%20%3D%20(%0A%20%20%20%20%20%20%20%20pd.to_datetime(%7B%22year%22%3A%20years%2C%20%22month%22%3A%201%2C%20%22day%22%3A%201%7D)%20-%20pd.Timestamp(%221980-01-01%22)%0A%20%20%20%20).dt.days%0A%20%20%20%20read_at%20%3D%20(pd.Timestamp(%222024-07-01%22)%20-%20pd.Timestamp(%221980-01-01%22)).days%0A%20%20%20%20story_series%20%3D%20story_rows.set_index(%5BGEO_IDENTIFIER%2C%20SEASON_YEAR%5D)%5B%22yield%22%5D%0A%20%20%20%20for%20_h%20in%20(3.0%2C%2010.0)%3A%0A%20%20%20%20%20%20%20%20production%20%3D%20level_trends(story_series%2C%20%5B2024%5D%2C%20%22ewm_linear%22%2C%20_h%2C%20gap%3D1).iloc%5B0%5D%0A%20%20%20%20%20%20%20%20reads%20%3D%20%7B%7D%0A%20%20%20%20%20%20%20%20for%20_label%2C%20loss_half_life%20in%20((%22h%22%2C%20_h)%2C%20(%22h%2F2%22%2C%20_h%20%2F%202))%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20loss_weight%20%3D%200.5%20**%20(ages%20%2F%20loss_half_life)%0A%20%20%20%20%20%20%20%20%20%20%20%20wls%20%3D%20sm.WLS(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20story_yield%2C%20sm.add_constant(days.to_numpy(float))%2C%20weights%3Dloss_weight%0A%20%20%20%20%20%20%20%20%20%20%20%20).fit()%0A%20%20%20%20%20%20%20%20%20%20%20%20reads%5B_label%5D%20%3D%20wls.params%5B0%5D%20%2B%20wls.params%5B1%5D%20*%20read_at%0A%20%20%20%20%20%20%20%20print(%0A%20%20%20%20%20%20%20%20%20%20%20%20f%22half-life%20%7B_h%3Ag%7D%3A%20level_trends%20%7Bproduction%3A.4f%7D%20%7C%20WLS%2C%20loss%20half-life%20h%20%7Breads%5B'h'%5D%3A.4f%7D%22%0A%20%20%20%20%20%20%20%20%20%20%20%20f%22%20%7C%20WLS%2C%20loss%20half-life%20h%2F2%20%7Breads%5B'h%2F2'%5D%3A.4f%7D%22%0A%20%20%20%20%20%20%20%20)%0A%0A%20%20%20%20def%20effective_seasons(loss_weight)%3A%0A%20%20%20%20%20%20%20%20%23%20Kish's%20effective%20sample%20size%3A%20how%20many%20equally%20weighted%20seasons%20the%20weights%20are%20worth.%0A%20%20%20%20%20%20%20%20return%20loss_weight.sum()%20**%202%20%2F%20(loss_weight**2).sum()%0A%0A%20%20%20%20print(%0A%20%20%20%20%20%20%20%20f%22%7Bages.size%7D%20seasons%2C%20half-life%2010%3A%20worth%20%7Beffective_seasons(w**2)%3A.1f%7D%20equal%20seasons%20as%20coded%2C%22%0A%20%20%20%20%20%20%20%20f%22%20%7Beffective_seasons(w)%3A.1f%7D%20if%20the%20stated%20half-life%20held%22%0A%20%20%20%20)%0A%20%20%20%20recipes%20%3D%20%7B%22Brazil%20soy%22%3A%203%2C%20%22cotton%22%3A%206%2C%20%22soy%20USA%22%3A%2010%2C%20%22winter%20wheat%22%3A%2020%7D%0A%20%20%20%20print(%0A%20%20%20%20%20%20%20%20%22recipe%20half-lives%20as%20stated%20%E2%86%92%20as%20they%20act%3A%22%2C%0A%20%20%20%20%20%20%20%20%7Bk%3A%20f%22%7Bv%7D%20%E2%86%92%20%7Bv%20%2F%202%3Ag%7D%22%20for%20k%2C%20v%20in%20recipes.items()%7D%2C%0A%20%20%20%20)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%202.7%20How%20a%20trend%20plugs%20into%20the%20yield%20model%0A%0A%20%20%20%20%60build_point_model%60%20picks%20the%20trend%20from%20the%20recipe%20and%20wraps%20it%20in%20%60Additive%60%3A%20the%20trend%20is%20the%20**baseline**%2C%20and%20the%20regression%20(LightGBM%20or%20PCA-ridge)%20is%20fitted%20to%20what%20the%20trend%20misses.%20So%20the%20regression%20learns%20weather-driven%20deviations%20from%20trend.%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s2_7_trend_plugs_yield()%3A%0A%20%20%20%20from%20yield_module.chain%20import%20build_point_model%0A%0A%20%20%20%20show(build_point_model)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%20Part%203%3A%20the%20science%0A%0A%20%20%20%20%23%23%203.1%20Pooling%3A%20who%20keeps%20their%20own%20slope%3F%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s3_1_pooling_who_keeps(%0A%20%20%20%20geography_slopes%2C%0A%20%20%20%20national_slope%2C%0A%20%20%20%20shrink_towards%2C%0A%20%20%20%20train%2C%0A%20%20%20%20true_slope%2C%0A)%3A%0A%20%20%20%20raw%20%3D%20geography_slopes(train%2C%20%22yield%22%2C%203)%0A%20%20%20%20nat%20%3D%20national_slope(train%2C%20%22yield%22%2C%20%22area%22%2C%2030)%0A%20%20%20%20shrunk%20%3D%20shrink_towards(raw%5B%22slope%22%5D%2C%20raw%5B%22standard_error%22%5D%2C%20nat)%0A%20%20%20%20tau2%20%3D%20max(float(np.var(raw%5B%22slope%22%5D)%20-%20np.mean(raw%5B%22standard_error%22%5D%20**%202))%2C%201e-30)%0A%20%20%20%20pooling%20%3D%20pd.DataFrame(%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%22true%20slope%22%3A%20true_slope%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22own%20OLS%20slope%22%3A%20raw%5B%22slope%22%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22SE%22%3A%20raw%5B%22standard_error%22%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22trust%20%CE%BB%22%3A%20tau2%20%2F%20(tau2%20%2B%20raw%5B%22standard_error%22%5D%20**%202)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22shrunk%20slope%22%3A%20shrunk%2C%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20).rename(index%3Dlambda%20g%3A%20g.rsplit(%22%3A%22%2C%201)%5B1%5D)%0A%20%20%20%20print(f%22national%20slope%20%7Bnat%3A.3f%7D%3B%20%CF%84%C2%B2%20%7Btau2%3A.3f%7D%22)%0A%20%20%20%20pooling.round(3)%0A%20%20%20%20return%20nat%2C%20pooling%2C%20raw%2C%20tau2%0A%0A%0A%40app.cell%0Adef%20s3_1_pooling_who_keeps_2(nat%2C%20pooling)%3A%0A%20%20%20%20_fig%2C%20_ax%20%3D%20plt.subplots(figsize%3D(7%2C%204.5))%0A%20%20%20%20_ax.scatter(pooling%5B%22true%20slope%22%5D%2C%20pooling%5B%22own%20OLS%20slope%22%5D%2C%20label%3D%22own%20OLS%20slope%20(no%20pooling)%22)%0A%20%20%20%20_ax.scatter(%0A%20%20%20%20%20%20%20%20pooling%5B%22true%20slope%22%5D%2C%20pooling%5B%22shrunk%20slope%22%5D%2C%20label%3D%22shrunk%20slope%20(partial%20pooling)%22%0A%20%20%20%20)%0A%20%20%20%20_ax.axhline(nat%2C%20color%3D%22grey%22%2C%20ls%3D%22--%22%2C%20label%3D%22national%20slope%20(complete%20pooling)%22)%0A%20%20%20%20lims%20%3D%20%5B%0A%20%20%20%20%20%20%20%20pooling%5B%5B%22true%20slope%22%2C%20%22own%20OLS%20slope%22%2C%20%22shrunk%20slope%22%5D%5D.min().min()%20-%200.2%2C%0A%20%20%20%20%20%20%20%20pooling%5B%5B%22true%20slope%22%2C%20%22own%20OLS%20slope%22%2C%20%22shrunk%20slope%22%5D%5D.max().max()%20%2B%200.2%2C%0A%20%20%20%20%5D%0A%20%20%20%20_ax.plot(lims%2C%20lims%2C%20color%3D%22black%22%2C%20lw%3D0.8%2C%20label%3D%22perfect%22)%0A%20%20%20%20_ax.annotate(%0A%20%20%20%20%20%20%20%20%22newcounty%22%2C%0A%20%20%20%20%20%20%20%20(pooling.loc%5B%22newcounty%22%2C%20%22true%20slope%22%5D%2C%20pooling.loc%5B%22newcounty%22%2C%20%22own%20OLS%20slope%22%5D)%2C%0A%20%20%20%20)%0A%20%20%20%20_ax.set_xlabel(%22true%20slope%20(bu%2Fac%2Fyr)%22)%0A%20%20%20%20_ax.set_ylabel(%22estimated%20slope%22)%0A%20%20%20%20_ax.set_title(%22Shrinkage%20pulls%20only%20newcounty%20towards%20the%20nation%2C%20and%20only%20halfway%22)%0A%20%20%20%20_ax.legend(fontsize%3D8)%0A%20%20%20%20plt.show()%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20**Read%20the%20plot.**%20The%20long-history%20counties'%20blue%20and%20orange%20dots%20sit%20almost%20on%20top%20of%20each%20other%2C%20close%20to%20the%20diagonal%3A%20shrinkage%20barely%20moves%20them.%20Only%20%60newcounty%60's%20slope%20moves%2C%20and%20only%20about%20halfway%20towards%20the%20national%20line.%0A%0A%20%20%20%20**Read%20the%20%60newcounty%60%20row.**%20Its%20five%20noisy%20seasons%20give%20a%20wild%20slope%2C%20about%206.5%20against%20a%20true%202.6%2C%20with%20a%20large%20standard%20error.%20You%20might%20expect%20%CE%BB%20%E2%89%88%200%2C%20meaning%20full%20trust%20in%20the%20nation.%20Instead%20%CE%BB%20%E2%89%88%200.5%2C%20because%20%CF%84%C2%B2%20is%20estimated%20*from%20the%20slopes%20themselves*%2C%20as%20var(slopes)%20%E2%88%92%20mean(SE%C2%B2)%2C%20and%20that%20one%20wild%20slope%20inflates%20var(slopes).%20A%20bigger%20%CF%84%C2%B2%20means%20more%20trust%20for%20everyone%2C%20so%20%60newcounty%60%20keeps%20half%20its%20error.%20The%20next%20cell%20recomputes%20%CF%84%C2%B2%20without%20it%3A%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s3_1_pooling_who_keeps_3(SHORT%2C%20raw%2C%20tau2)%3A%0A%20%20%20%20others%20%3D%20raw.drop(index%3DSHORT)%0A%20%20%20%20tau2_without%20%3D%20max(%0A%20%20%20%20%20%20%20%20float(np.var(others%5B%22slope%22%5D)%20-%20np.mean(others%5B%22standard_error%22%5D%20**%202))%2C%201e-30%0A%20%20%20%20)%0A%20%20%20%20se_new%20%3D%20raw.loc%5BSHORT%2C%20%22standard_error%22%5D%0A%20%20%20%20print(f%22%CF%84%C2%B2%20with%20newcounty%20%7Btau2%3A.3f%7D%2C%20without%20%7Btau2_without%3A.3f%7D%22)%0A%20%20%20%20print(%0A%20%20%20%20%20%20%20%20f%22newcounty%20trust%20%CE%BB%3A%20%7Btau2%20%2F%20(tau2%20%2B%20se_new**2)%3A.3f%7D%20%E2%86%92%20%7Btau2_without%20%2F%20(tau2_without%20%2B%20se_new**2)%3A.3f%7D%22%0A%20%20%20%20)%0A%20%20%20%20return%20others%2C%20se_new%0A%0A%0A%40app.cell%0Adef%20s3_1_pooling_who_keeps_4(%0A%20%20%20%20SHORT%2C%0A%20%20%20%20STATES%2C%0A%20%20%20%20others%2C%0A%20%20%20%20raw%2C%0A%20%20%20%20se_new%2C%0A%20%20%20%20tau2%2C%0A%20%20%20%20true_slope%2C%0A)%3A%0A%20%20%20%20%23%20How%20unlucky%20was%20newcounty's%20draw%3F%20Redraw%20its%20slope%20noise%20at%20its%20own%20SE%2C%2020%2C000%20times.%0A%20%20%20%20error_in_se%20%3D%20(raw.loc%5BSHORT%2C%20%22slope%22%5D%20-%20true_slope%5BSHORT%5D)%20%2F%20se_new%0A%20%20%20%20redraw%20%3D%20np.random.default_rng(1)%0A%20%20%20%20new_slopes%20%3D%20true_slope%5BSHORT%5D%20%2B%20redraw.normal(0%2C%20se_new%2C%2020_000)%0A%20%20%20%20_slopes%20%3D%20np.column_stack(%0A%20%20%20%20%20%20%20%20%5Bnp.tile(others%5B%22slope%22%5D.to_numpy()%2C%20(new_slopes.size%2C%201))%2C%20new_slopes%5D%0A%20%20%20%20)%0A%20%20%20%20mean_se2%20%3D%20np.mean(raw%5B%22standard_error%22%5D.to_numpy()%20**%202)%0A%20%20%20%20tau2_draws%20%3D%20np.maximum(_slopes.var(axis%3D1)%20-%20mean_se2%2C%201e-30)%0A%20%20%20%20print(f%22newcounty's%20slope%20error%20is%20%7Berror_in_se%3A.2f%7D%20standard%20errors%22)%0A%20%20%20%20print(%0A%20%20%20%20%20%20%20%20f%22%CF%84%C2%B2%20over%20redraws%3A%20mean%20%7Btau2_draws.mean()%3A.3f%7D%2C%20median%20%7Bnp.median(tau2_draws)%3A.3f%7D%2C%22%0A%20%20%20%20%20%20%20%20f%22%205%E2%80%9395%20%25%20%7Bnp.percentile(tau2_draws%2C%205)%3A.2f%7D%E2%80%93%7Bnp.percentile(tau2_draws%2C%2095)%3A.2f%7D%3B%20this%20draw%20%7Btau2%3A.3f%7D%22%0A%20%20%20%20)%0A%0A%20%20%20%20%23%20DerSimonian%E2%80%93Laird%3A%20the%20same%20moment%20idea%2C%20but%20each%20slope%20weighted%20by%20its%20precision%201%2FSE%C2%B2.%0A%20%20%20%20precision%20%3D%201%20%2F%20raw%5B%22standard_error%22%5D%20**%202%0A%20%20%20%20centre%20%3D%20(precision%20*%20raw%5B%22slope%22%5D).sum()%20%2F%20precision.sum()%0A%20%20%20%20q%20%3D%20(precision%20*%20(raw%5B%22slope%22%5D%20-%20centre)%20**%202).sum()%0A%20%20%20%20tau2_dl%20%3D%20max(%0A%20%20%20%20%20%20%20%200.0%2C%20(q%20-%20(len(raw)%20-%201))%20%2F%20(precision.sum()%20-%20(precision**2).sum()%20%2F%20precision.sum())%0A%20%20%20%20)%0A%20%20%20%20print(%0A%20%20%20%20%20%20%20%20f%22DerSimonian%E2%80%93Laird%20%CF%84%C2%B2%2C%20newcounty%20included%3A%20%7Btau2_dl%3A.3f%7D%3B%22%0A%20%20%20%20%20%20%20%20f%22%20newcounty%20%CE%BB%20would%20be%20%7Btau2_dl%20%2F%20(tau2_dl%20%2B%20se_new**2)%3A.3f%7D%22%0A%20%20%20%20)%0A%0A%20%20%20%20%23%20Shared%20shocks%3A%20a%20state-wide%20shock%20cancels%20from%20var(b_i)%20only%20in%20proportion%20%CE%A3(share%20of%20counties%20per%20state)%C2%B2.%0A%20%20%20%20def%20tau2_gap(state_sd%2C%20county_sd%2C%20panels%3D300%2C%20seed%3D0)%3A%0A%20%20%20%20%20%20%20%20%23%20Mean%20of%20(%CF%84%C2%B2%20estimate%20%E2%88%92%20variance%20of%20the%20true%20slopes)%20over%20synthetic%20panels%20without%20a%20drought.%0A%20%20%20%20%20%20%20%20r%20%3D%20np.random.default_rng(seed)%0A%20%20%20%20%20%20%20%20x%20%3D%20np.arange(1990%2C%202024)%20-%20np.arange(1990%2C%202024).mean()%0A%20%20%20%20%20%20%20%20sizes%20%3D%20%5Blen(names)%20for%20names%20in%20STATES.values()%5D%0A%20%20%20%20%20%20%20%20gaps%20%3D%20%5B%5D%0A%20%20%20%20%20%20%20%20for%20_%20in%20range(panels)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20true%20%3D%201.8%20%2B%20r.normal(0%2C%200.6%2C%20sum(sizes))%0A%20%20%20%20%20%20%20%20%20%20%20%20state%20%3D%20np.repeat(r.normal(0%2C%20state_sd%2C%20(len(sizes)%2C%20x.size))%2C%20sizes%2C%20axis%3D0)%0A%20%20%20%20%20%20%20%20%20%20%20%20d%20%3D%20true%5B%3A%2C%20None%5D%20*%20x%20%2B%20state%20%2B%20r.normal(0%2C%20county_sd%2C%20(sum(sizes)%2C%20x.size))%0A%20%20%20%20%20%20%20%20%20%20%20%20d%20%3D%20d%20-%20d.mean(axis%3D1%2C%20keepdims%3DTrue)%0A%20%20%20%20%20%20%20%20%20%20%20%20b%20%3D%20d%20%40%20x%20%2F%20(x%20%40%20x)%0A%20%20%20%20%20%20%20%20%20%20%20%20se2%20%3D%20((d%20-%20b%5B%3A%2C%20None%5D%20*%20x)%20**%202).sum(axis%3D1)%20%2F%20(x.size%20-%202)%20%2F%20(x%20%40%20x)%0A%20%20%20%20%20%20%20%20%20%20%20%20gaps.append(max(b.var()%20-%20se2.mean()%2C%201e-30)%20-%20true.var())%0A%20%20%20%20%20%20%20%20return%20float(np.mean(gaps))%2C%20x%20%40%20x%0A%0A%20%20%20%20sizes%20%3D%20%5Blen(names)%20for%20names%20in%20STATES.values()%5D%0A%20%20%20%20_shared%20%3D%20sum((n%20%2F%20sum(sizes))%20**%202%20for%20n%20in%20sizes)%0A%20%20%20%20print(f%22share%20of%20a%20state%20shock%20that%20cancels%20from%20var(b_i)%3A%20%7B_shared%3A.2f%7D%22)%0A%20%20%20%20for%20state_sd%2C%20county_sd%20in%20((4.0%2C%204.0)%2C%20(8.0%2C%200.1))%3A%0A%20%20%20%20%20%20%20%20gap%2C%20sxx34%20%3D%20tau2_gap(state_sd%2C%20county_sd)%0A%20%20%20%20%20%20%20%20print(%0A%20%20%20%20%20%20%20%20%20%20%20%20f%22state%20shock%20sd%20%7Bstate_sd%3Ag%7D%2C%20county%20sd%20%7Bcounty_sd%3Ag%7D%3A%20mean%20%CF%84%C2%B2%20bias%20%7Bgap%3A%2B.4f%7D%22%0A%20%20%20%20%20%20%20%20%20%20%20%20f%22%20(predicted%20before%20flooring%2C%20%E2%88%92(%7B_shared%3A.2f%7D%C2%B7%CF%83_state%C2%B2%20%2B%20%CF%83_county%C2%B2%2F%7Bsum(sizes)%7D)%2FSxx%22%0A%20%20%20%20%20%20%20%20%20%20%20%20f%22%20%3D%20%7B-(_shared%20*%20state_sd**2%20%2B%20county_sd**2%20%2F%20sum(sizes))%20%2F%20sxx34%3A%2B.4f%7D)%22%0A%20%20%20%20%20%20%20%20)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20Is%20that%20a%20systematic%20bias%2C%20or%20bad%20luck%3F%20The%20formula%20already%20subtracts%20%60newcounty%60's%20large%20SE%C2%B2%2C%20so%20**on%20average**%20it%20allows%20for%20that%20county's%20noise.%20The%20cell%20above%20redraws%20%60newcounty%60's%20slope%20noise%2020%2C000%20times%20at%20its%20own%20SE.%20This%20draw%20is%202.86%20standard%20errors%20from%20the%20truth%2C%20a%20tail%20event.%20Over%20the%20redraws%2C%20%CF%84%C2%B2%20has%20a%20mean%20of%200.441%20and%20a%20median%20of%200.331%2C%20and%202.08%20lies%20far%20beyond%20the%2095th%20percentile%20(1.04).%0A%0A%20%20%20%20So%20the%20real%20defect%20is%20that%20the%20moment%20estimator%20is%20**unweighted**.%20It%20is%20roughly%20unbiased%2C%20but%20noisy%2C%20and%20a%20single%20short%20series%20can%20swing%20it%2C%20as%20here.%20The%20principled%20fix%20weights%20each%20slope%20by%20its%20precision%20%241%2F%5Cmathrm%7BSE%7D%5E2%24.%20The%20DerSimonian%E2%80%93Laird%20estimator%20does%20that%2C%20and%20gives%20%CF%84%C2%B2%20%3D%200.433%20with%20%60newcounty%60%20included%2C%20so%20its%20%CE%BB%20would%20be%200.193%20instead%20of%200.534.%20REML%2C%20as%20in%20the%20Fay%E2%80%93Herriot%20model%2C%20is%20the%20likelihood-based%20version.%20The%20long-history%20counties%20barely%20notice%20either%20way%2C%20because%20their%20SEs%20are%20small.%0A%0A%20%20%20%20**Shared%20shocks.**%20A%20state-wide%20shock%20moves%20every%20county%20slope%20in%20that%20state%20together.%20Only%20part%20of%20it%20cancels%20out%20of%20var(%24b_i%24)%3A%20the%20sum%20of%20each%20state's%20squared%20share%20of%20the%20counties%2C%200.50%20here.%20The%20formula%20still%20subtracts%20the%20whole%20SE%C2%B2.%20So%20%CF%84%C2%B2%20comes%20out%20slightly%20small.%20The%20last%20lines%20measure%20how%20small%2C%20on%20300%20synthetic%20panels%20with%20no%20drought%3A%20%E2%88%920.004%20with%20this%20panel's%20shocks%20(predicted%20%E2%88%920.003)%20and%20%E2%88%920.009%20with%20large%20state%20shocks%20and%20almost%20no%20county%20noise%20(predicted%20%E2%88%920.010).%20The%20prediction%20has%20a%20second%2C%20smaller%20term%3A%20var(%24b_i%24)%20is%20measured%20about%20the%20counties'%20own%20mean%2C%20which%20also%20removes%201%2Fn%20of%20the%20independent%20county%20noise.%20Against%20a%20%CF%84%C2%B2%20of%20about%200.4%20that%20is%20negligible.%20It%20matters%20only%20when%20one%20big%20shared%20shock%20inflates%20every%20SE%C2%B2%2C%20which%20is%20the%20drought%2C%20and%20the%20study%20question%20in%203.5.%0A%0A%20%20%20%20%23%23%203.2%20A%20drought%20at%20the%20end%20of%20the%20window%3A%20OLS%20against%20Huber%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s3_2_drought_end_window(%0A%20%20%20%20DROUGHT%2C%0A%20%20%20%20DROUGHT_YEAR%2C%0A%20%20%20%20SHORT%2C%0A%20%20%20%20STATES%2C%0A%20%20%20%20geography_slopes%2C%0A%20%20%20%20huber_geography_slopes%2C%0A%20%20%20%20panel%2C%0A%20%20%20%20true_slope%2C%0A)%3A%0A%20%20%20%20upto_2013%20%3D%20panel%5B(panel%5BSEASON_YEAR%5D%20%3C%3D%202013)%20%26%20(panel%5BGEO_IDENTIFIER%5D%20!%3D%20SHORT)%5D%0A%20%20%20%20ols_slopes%20%3D%20geography_slopes(upto_2013%2C%20%22yield%22%2C%203)%5B%22slope%22%5D%0A%20%20%20%20huber_slopes%20%3D%20huber_geography_slopes(upto_2013%2C%20%22yield%22%2C%203)%5B%22slope%22%5D%0A%20%20%20%20_err%20%3D%20pd.DataFrame(%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%22OLS%22%3A%20ols_slopes%20-%20true_slope%5Bols_slopes.index%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22Huber%22%3A%20huber_slopes%20-%20true_slope%5Bhuber_slopes.index%5D%2C%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20)%0A%20%20%20%20print(f%22fitted%20on%201990%E2%80%932013%2C%20with%20the%20drought%20in%20%7BDROUGHT_YEAR%7D%22)%0A%20%20%20%20print(%0A%20%20%20%20%20%20%20%20%22mean%20slope%20error%3A%22%2C%0A%20%20%20%20%20%20%20%20_err.mean().round(3).to_dict()%2C%0A%20%20%20%20%20%20%20%20%22%7C%20mean%20%7Cslope%20error%7C%3A%22%2C%0A%20%20%20%20%20%20%20%20_err.abs().mean().round(3).to_dict()%2C%0A%20%20%20%20)%0A%20%20%20%20%23%20One%20residual%20r%20at%20season%20x_k%20moves%20the%20OLS%20slope%20by%20r%C2%B7(x_k%20%E2%88%92%20x%CC%84)%2FSxx.%0A%20%20%20%20fit_seasons%20%3D%20np.arange(1990%2C%202014)%0A%20%20%20%20x_bar%2C%20sxx13%20%3D%20fit_seasons.mean()%2C%20((fit_seasons%20-%20fit_seasons.mean())%20**%202).sum()%0A%20%20%20%20moved%20%3D%20%7Bstate%3A%20-DROUGHT%5Bstate%5D%20*%20(DROUGHT_YEAR%20-%20x_bar)%20%2F%20sxx13%20for%20state%20in%20DROUGHT%7D%0A%20%20%20%20county_mean%20%3D%20sum(moved%5Bstate%5D%20*%20len(names)%20for%20state%2C%20names%20in%20STATES.items())%20%2F%20sum(%0A%20%20%20%20%20%20%20%20len(names)%20for%20names%20in%20STATES.values()%0A%20%20%20%20)%0A%20%20%20%20print(%0A%20%20%20%20%20%20%20%20f%22influence%20formula%3A%20Iowa%20%7Bmoved%5B'iowa'%5D%3A.3f%7D%2C%20Nebraska%20%7Bmoved%5B'nebraska'%5D%3A.3f%7D%2C%20county%20mean%20%7Bcounty_mean%3A.3f%7D%3B%22%0A%20%20%20%20%20%20%20%20f%22%20the%20same%20Iowa%20drought%20in%202000%20would%20move%20it%20by%20%7B-DROUGHT%5B'iowa'%5D%20*%20(2000%20-%20x_bar)%20%2F%20sxx13%3A%2B.3f%7D%22%0A%20%20%20%20)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20A%20drought%20near%20the%20**end**%20of%20the%20window%20has%20high%20**leverage**%3A%20its%20season%20sits%20far%20from%20the%20mean%20season%2C%20so%20it%20tilts%20the%20line.%20Quantitatively%2C%20one%20residual%20%24r%24%20at%20season%20%24x_k%24%20moves%20the%20OLS%20slope%20by%20%24r%5C%2C(x_k%20-%20%5Cbar%20x)%2FS_%7Bxx%7D%24.%20For%20the%202012%20drought%20fitted%20through%202013%20that%20gives%20%E2%88%920.411%20for%20Iowa%20and%20%E2%88%920.320%20for%20Nebraska%2C%20a%20county%20mean%20of%20%E2%88%920.369%2C%20against%20the%20%E2%88%920.36%20measured.%20The%20same%20drought%20in%202000%2C%20near%20the%20middle%2C%20would%20move%20it%20by%20only%20%2B0.059.%20OLS%20lets%20the%20drought%20drag%20every%20county's%20slope%20down%3B%20Huber%20caps%20its%20influence.%20This%20is%20why%20corn%20yield%20uses%20%60slope%3A%20huber%60%3A%202012%20sits%20inside%20every%20corn%20training%20window%20from%202013%20onwards.%0A%0A%20%20%20%20%23%23%203.3%20Walk-forward%20hindcast%3A%20every%20trend%2C%20fitted%20only%20on%20the%20past%0A%0A%20%20%20%20For%20each%20season%20%24y%24%20from%202005%20to%202024%2C%20fit%20on%20seasons%20%E2%89%A4%20%24y-1%24%20and%20predict%20%24y%24.%20That's%20the%20same%20rule%20as%20model-foundry's%20%60expanding_season_folds%60%2C%20and%20the%20same%20rule%20%60level_trends%60%20builds%20in%20with%20%60gap%60.%20We%20score%20against%20the%20**true**%20trend%20(which%20only%20synthetic%20data%20allows)%20and%20against%20observed%20yield.%0A%0A%20%20%20%20Two%20rules%20keep%20the%20comparison%20fair%3A%0A%0A%20%20%20%20-%20**Every%20model%20is%20scored%20on%20the%20same%20rows.**%20%60MixedLm%60%20can't%20place%20%60newcounty%60%20(we%20fit%20it%20on%20the%20long-history%20counties%20only)%2C%20and%20%60level_trends%60%20returns%20nothing%20for%20a%20geography%20with%20fewer%20than%20three%20prior%20seasons.%20So%20the%20main%20columns%20use%20only%20the%20geography-seasons%20every%20model%20predicts.%20The%20last%20column%20shows%20each%20model's%20error%20on%20all%20the%20rows%20it%20can%20predict%2C%20for%20comparison.%0A%20%20%20%20-%20**%60level_trends%60%20is%20scored%20at%20two%20read%20dates.**%20In%20production%20it%20fits%20each%20season%20at%201%20January%20and%20reads%20the%20line%20at%20**1%20July**%20of%20the%20target%20season%3A%20half%20a%20season%20further%20along%20the%20trend%20than%20a%20model%20that%20predicts%20the%20season%20itself.%20In%20production%20the%20trend%20is%20an%20input%20feature%2C%20and%20the%20joint%20model%20largely%20learns%20around%20the%20offset%20(not%20measured%20here).%20The%20offset%20is%20half%20of%20each%20geography's%20own%20slope%2C%20so%20it%20differs%20by%20geography.%20Scored%20as%20a%20forecast%2C%20it%20adds%20about%20half%20a%20year's%20slope.%20So%20we%20also%20re-read%20the%20same%20line%20at%201%20January%2C%20using%20only%20production%20calls%3A%20asking%20%60level_trends%60%20for%20season%20%24y%2B1%24%20as%20well%20returns%20the%20same%20line%20one%20year%20on%2C%20and%20the%20difference%20is%20its%20slope.%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s3_3_walk_forward_hindcast(%0A%20%20%20%20GaussianSmoothTrend%2C%0A%20%20%20%20HuberPoolingTrend%2C%0A%20%20%20%20PartialPoolingTrend%2C%0A%20%20%20%20SHORT%2C%0A%20%20%20%20panel%2C%0A)%3A%0A%20%20%20%20sigma_seasons%20%3D%204.0%20%20%23%20exercise%203%20changes%20this%3B%20the%20label%20below%20follows%20it%0A%20%20%20%20GAUSSIAN%20%3D%20f%22GaussianSmoothTrend%20%CF%83%3D%7Bsigma_seasons%3Ag%7D%20(county%20level%2C%20unweighted)%22%0A%20%20%20%20from%20datetime%20import%20date%0A%0A%20%20%20%20def%20production_trends()%3A%0A%20%20%20%20%20%20%20%20return%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%22PartialPoolingTrend%22%3A%20PartialPoolingTrend(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20source%3D%22yield%22%2C%20weight%3D%22area%22%2C%20min_seasons%3D3%2C%20national_window_seasons%3D30%0A%20%20%20%20%20%20%20%20%20%20%20%20)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22HuberPoolingTrend%22%3A%20HuberPoolingTrend(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20source%3D%22yield%22%2C%20weight%3D%22area%22%2C%20min_seasons%3D3%2C%20national_window_seasons%3D30%0A%20%20%20%20%20%20%20%20%20%20%20%20)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20GAUSSIAN%3A%20GaussianSmoothTrend(source%3D%22yield%22%2C%20sigma_seasons%3Dsigma_seasons)%2C%0A%20%20%20%20%20%20%20%20%7D%0A%0A%20%20%20%20rows%20%3D%20%5B%5D%0A%0A%20%20%20%20def%20keep(name%2C%20season%2C%20geos%2C%20predicted)%3A%0A%20%20%20%20%20%20%20%20rows.extend(%0A%20%20%20%20%20%20%20%20%20%20%20%20%7B%22model%22%3A%20name%2C%20GEO_IDENTIFIER%3A%20g%2C%20%22season%22%3A%20season%2C%20%22predicted%22%3A%20float(v)%7D%0A%20%20%20%20%20%20%20%20%20%20%20%20for%20g%2C%20v%20in%20zip(geos%2C%20predicted%2C%20strict%3DFalse)%0A%20%20%20%20%20%20%20%20)%0A%0A%20%20%20%20for%20_season%20in%20range(2005%2C%202025)%3A%0A%20%20%20%20%20%20%20%20_past%20%3D%20panel%5Bpanel%5BSEASON_YEAR%5D%20%3C%3D%20_season%20-%201%5D%0A%20%20%20%20%20%20%20%20_now%20%3D%20panel%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20(panel%5BSEASON_YEAR%5D%20%3D%3D%20_season)%0A%20%20%20%20%20%20%20%20%20%20%20%20%26%20panel%5BGEO_IDENTIFIER%5D.isin(_past%5BGEO_IDENTIFIER%5D.unique())%0A%20%20%20%20%20%20%20%20%5D%0A%20%20%20%20%20%20%20%20for%20_name%2C%20_model%20in%20production_trends().items()%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20keep(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20_name%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20_season%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20_now%5BGEO_IDENTIFIER%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20_model.fit(_past).predict(_now)%5Boutput_column(_model)%5D.to_numpy()%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20%23%20level_trends%20fits%20on%201%20January%20and%20reads%20at%201%20July.%20Asking%20for%20season%2B1%20too%20reuses%20the%20same%20line%0A%20%20%20%20%20%20%20%20%23%20(past%20stops%20at%20season-1)%2C%20so%20the%20difference%20between%20the%20two%20reads%20is%20the%20line's%20slope%20per%20year.%0A%20%20%20%20%20%20%20%20series%20%3D%20_past.set_index(%5BGEO_IDENTIFIER%2C%20SEASON_YEAR%5D)%5B%22yield%22%5D%0A%20%20%20%20%20%20%20%20half_year%20%3D%20(date(_season%2C%207%2C%201)%20-%20date(_season%2C%201%2C%201)).days%20%2F%20(%0A%20%20%20%20%20%20%20%20%20%20%20%20date(_season%20%2B%201%2C%207%2C%201)%20-%20date(_season%2C%207%2C%201)%0A%20%20%20%20%20%20%20%20).days%0A%20%20%20%20%20%20%20%20for%20method%2C%20half_life%20in%20((%22linear%22%2C%20None)%2C%20(%22ewm_linear%22%2C%2010.0)%2C%20(%22ewm_linear%22%2C%2020.0))%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20_lines%20%3D%20level_trends(series%2C%20%5B_season%2C%20_season%20%2B%201%5D%2C%20method%2C%20half_life%2C%20gap%3D1).unstack(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20SEASON_YEAR%0A%20%20%20%20%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20%20%20%20%20_label%20%3D%20(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22level_trends%20linear%22%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20if%20half_life%20is%20None%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20else%20f%22level_trends%20ewm_linear%20(half-life%20%7Bhalf_life%3Ag%7D%2C%20acts%20as%20%7Bhalf_life%20%2F%202%3Ag%7D%3A%20F7)%22%0A%20%20%20%20%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20%20%20%20%20july%2C%20_slope%20%3D%20_lines%5B_season%5D%2C%20_lines%5B_season%20%2B%201%5D%20-%20_lines%5B_season%5D%0A%20%20%20%20%20%20%20%20%20%20%20%20keep(f%22%7B_label%7D%2C%20read%201%20July%22%2C%20_season%2C%20july.index%2C%20july.to_numpy())%0A%20%20%20%20%20%20%20%20%20%20%20%20keep(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20f%22%7B_label%7D%2C%20read%201%20January%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20_season%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20july.index%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20(july%20-%20_slope%20*%20half_year).to_numpy()%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20known_past%2C%20known_now%20%3D%20(%0A%20%20%20%20%20%20%20%20%20%20%20%20_past%5B_past%5BGEO_IDENTIFIER%5D%20!%3D%20SHORT%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20_now%5B_now%5BGEO_IDENTIFIER%5D%20!%3D%20SHORT%5D%2C%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20_mixed%20%3D%20MixedLm(source%3D%22yield%22%2C%20features%3D%5B%5D%2C%20weight%3D%22area%22%2C%20fit_level%3D2).fit(known_past)%0A%20%20%20%20%20%20%20%20keep(%0A%20%20%20%20%20%20%20%20%20%20%20%20%22MixedLm%20(no%20features)%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20_season%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20known_now%5BGEO_IDENTIFIER%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20_mixed.predict(known_now)%5Boutput_column(_mixed)%5D.to_numpy()%2C%0A%20%20%20%20%20%20%20%20)%0A%0A%20%20%20%20truth%20%3D%20panel.rename(columns%3D%7BSEASON_YEAR%3A%20%22season%22%7D)%5B%0A%20%20%20%20%20%20%20%20%5BGEO_IDENTIFIER%2C%20%22season%22%2C%20%22yield%22%2C%20%22true_trend%22%5D%0A%20%20%20%20%5D%0A%20%20%20%20preds%20%3D%20pd.DataFrame(rows).merge(truth%2C%20on%3D%5BGEO_IDENTIFIER%2C%20%22season%22%5D)%0A%20%20%20%20preds%5B%22error%20vs%20true%20trend%22%5D%20%3D%20(preds%5B%22predicted%22%5D%20-%20preds%5B%22true_trend%22%5D).abs()%0A%20%20%20%20preds%5B%22error%20vs%20observed%22%5D%20%3D%20(preds%5B%22predicted%22%5D%20-%20preds%5B%22yield%22%5D).abs()%0A%20%20%20%20preds%5B%22bias%20vs%20true%20trend%22%5D%20%3D%20preds%5B%22predicted%22%5D%20-%20preds%5B%22true_trend%22%5D%0A%0A%20%20%20%20%23%20Score%20every%20model%20on%20the%20geography-seasons%20that%20every%20model%20predicts.%0A%20%20%20%20key%20%3D%20%5BGEO_IDENTIFIER%2C%20%22season%22%5D%0A%20%20%20%20n_models%20%3D%20preds%5B%22model%22%5D.nunique()%0A%20%20%20%20_shared%20%3D%20preds.groupby(key)%5B%22model%22%5D.transform(%22nunique%22)%20%3D%3D%20n_models%0A%20%20%20%20common%20%3D%20preds%5B_shared%5D%0A%20%20%20%20print(%0A%20%20%20%20%20%20%20%20f%22%7Bcommon.groupby(key).ngroups%7D%20geography-seasons%20every%20model%20predicts%2C%20out%20of%20%7Bpreds.groupby(key).ngroups%7D%22%0A%20%20%20%20)%0A%0A%20%20%20%20hindcast%20%3D%20common.groupby(%5B%22model%22%2C%20%22season%22%5D%2C%20as_index%3DFalse)%5B%22bias%20vs%20true%20trend%22%5D.mean()%0A%20%20%20%20summary%20%3D%20common.groupby(%22model%22)%5B%0A%20%20%20%20%20%20%20%20%5B%22error%20vs%20true%20trend%22%2C%20%22error%20vs%20observed%22%2C%20%22bias%20vs%20true%20trend%22%5D%0A%20%20%20%20%5D.mean()%0A%20%20%20%20summary%5B%22rows%20it%20predicts%22%5D%20%3D%20preds.groupby(%22model%22).size()%0A%20%20%20%20summary%5B%22error%20vs%20true%20trend%2C%20all%20its%20rows%22%5D%20%3D%20preds.groupby(%22model%22)%5B%0A%20%20%20%20%20%20%20%20%22error%20vs%20true%20trend%22%0A%20%20%20%20%5D.mean()%0A%20%20%20%20summary.sort_values(%22error%20vs%20true%20trend%22).round(2)%0A%20%20%20%20return%20GAUSSIAN%2C%20hindcast%2C%20preds%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20Seven%20lessons%20are%20in%20that%20table%3A%0A%0A%20%20%20%20-%20The%20**error%20vs%20observed**%20column%20is%20dominated%20by%20weather%2C%20which%20no%20trend%20can%20know.%20The%20trend's%20job%20is%20the%20*other*%20column.%0A%20%20%20%20-%20**%60HuberPoolingTrend%60%20has%20the%20smallest%20error%20(2.17)%20and%20the%20smallest%20bias%20(%E2%88%920.34).**%20The%202012%20drought%20sits%20inside%20every%20window%20from%202013%20onwards%20and%20drags%20the%20OLS-based%20lines%20(%60PartialPoolingTrend%60%2C%20%60MixedLm%60%2C%20plain%20%60linear%60)%20low%20for%20years.%20Huber%20caps%20it.%0A%20%20%20%20-%20**The%20read%20date%20flatters%20%60level_trends%60.**%20Read%20at%201%20July%2C%20as%20production%20reads%20it%2C%20plain%20%60linear%60%20scores%202.94%20with%20bias%20%E2%88%920.80.%20Re-read%20at%201%20January%20it%20scores%202.98%20with%20bias%20%E2%88%921.53%2C%20the%20same%20as%20the%20other%20OLS-based%20lines.%20The%20half-season%20of%20extra%20slope%20happens%20to%20cancel%20part%20of%20the%20drought's%20downward%20pull%3B%20it%20isn't%20a%20better%20method.%0A%20%20%20%20-%20**Pooling%20barely%20matters%20for%20counties%20with%20long%20histories.**%20On%20these%20rows%2C%20per-county%20%60linear%60%20(2.98)%2C%20%60PartialPoolingTrend%60%20(2.96)%20and%20%60MixedLm%60%20(2.97)%20are%20within%200.02%20of%20each%20other.%20Pooling%20earns%20its%20keep%20on%20short%20histories%20(3.1).%0A%20%20%20%20-%20**%60GaussianSmoothTrend%60%20carries%20a%20level%20flat**%2C%20so%20on%20a%20rising%20series%20it%20lags.%20Its%20large%20negative%20bias%20(%E2%88%925.23)%20is%201.3's%20edge%20effect%20plus%20the%20missing%20slope%2C%20paid%20every%20season.%20It%20suits%20a%20crop%20without%20a%20steady%20technology%20gain%3B%20it's%20the%20wrong%20tool%20for%20a%20steep%20one.%20This%20row%20smooths%20each%20county%2C%20unweighted%3B%20production%20cotton%20smooths%20states%20(%60fit_level%3A%201%60)%2C%20area-weighted%20and%20IQR-trimmed.%0A%20%20%20%20-%20**%60ewm_linear%60%20does%20worse%20than%20plain%20%60linear%60%20here**%20(3.63%20against%202.98%20at%20the%20same%20read%20date)%2C%20for%20two%20reasons.%20First%2C%20our%20true%20slopes%20never%20change%2C%20so%20down-weighting%20old%20seasons%20only%20throws%20information%20away.%20On%20a%20real%20crop%20whose%20trend%20bends%20(new%20varieties%2C%20a%20policy%20change)%2C%20that%20recency%20is%20the%20point.%20A%20synthetic%20test%20can%20only%20reward%20what%20the%20synthetic%20world%20contains.%0A%20%20%20%20-%20**About%20half%20of%20that%20deficit%20is%20F7.**%20%22Half-life%2010%22%20acts%20as%205.%20Passing%2020%2C%20which%20acts%20as%20the%20intended%2010%2C%20scores%203.31%20at%201%20January%3A%20it%20recovers%200.32%20of%20the%200.65%20gap.%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s3_3_walk_forward_hindcast_2(DROUGHT_YEAR%2C%20GAUSSIAN%2C%20hindcast)%3A%0A%20%20%20%20_fig%2C%20_ax%20%3D%20plt.subplots(figsize%3D(9%2C%204))%0A%20%20%20%20highlight%20%3D%20%7B%0A%20%20%20%20%20%20%20%20%22HuberPoolingTrend%22%3A%20%22C0%22%2C%0A%20%20%20%20%20%20%20%20%22PartialPoolingTrend%22%3A%20%22C1%22%2C%0A%20%20%20%20%20%20%20%20%22level_trends%20linear%2C%20read%201%20January%22%3A%20%22C2%22%2C%0A%20%20%20%20%20%20%20%20GAUSSIAN%3A%20%22C3%22%2C%0A%20%20%20%20%7D%0A%20%20%20%20for%20_name%2C%20group%20in%20hindcast.groupby(%22model%22)%3A%0A%20%20%20%20%20%20%20%20style%20%3D%20(%0A%20%20%20%20%20%20%20%20%20%20%20%20%7B%22color%22%3A%20highlight%5B_name%5D%2C%20%22lw%22%3A%202%2C%20%22label%22%3A%20_name%7D%0A%20%20%20%20%20%20%20%20%20%20%20%20if%20_name%20in%20highlight%0A%20%20%20%20%20%20%20%20%20%20%20%20else%20%7B%22color%22%3A%20%22lightgrey%22%2C%20%22lw%22%3A%201%7D%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20_ax.plot(group%5B%22season%22%5D%2C%20group%5B%22bias%20vs%20true%20trend%22%5D%2C%20marker%3D%22o%22%2C%20ms%3D3%2C%20**style)%0A%20%20%20%20_ax.plot(%5B%5D%2C%20%5B%5D%2C%20color%3D%22lightgrey%22%2C%20label%3D%22other%20trends%22)%0A%20%20%20%20_ax.axhline(0%2C%20color%3D%22black%22%2C%20lw%3D0.8)%0A%20%20%20%20_ax.axvline(DROUGHT_YEAR%2C%20color%3D%22red%22%2C%20ls%3D%22%3A%22%2C%20lw%3D1)%0A%20%20%20%20_ax.set_xlabel(%22season%22)%0A%20%20%20%20_ax.set_ylabel(%22bias%20vs%20true%20trend%20(bu%2Fac)%22)%0A%20%20%20%20_ax.set_title(%0A%20%20%20%20%20%20%20%20f%22After%20the%20%7BDROUGHT_YEAR%7D%20drought%20every%20OLS-based%20line%20runs%20low%20for%20years%3B%20Huber%20far%20less%22%0A%20%20%20%20)%0A%20%20%20%20_ax.legend(fontsize%3D7)%0A%20%20%20%20plt.show()%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20**Read%20the%20plot.**%20Before%202012%20the%20lines%20sit%20a%20little%20above%20the%20truth.%20From%202013%20every%20OLS-based%20line%20(%60PartialPoolingTrend%60%2C%20%60MixedLm%60%2C%20plain%20%60linear%60)%20runs%20several%20bu%2Fac%20low%20for%20most%20of%20a%20decade%2C%20because%20the%20drought%20is%20inside%20its%20window.%20Huber's%20line%20runs%20low%20too%2C%20but%20by%20much%20less.%20The%20Gaussian%20level%20lags%20in%20every%20season%2C%20drought%20or%20not.%0A%0A%20%20%20%20%23%23%23%203.3b%20The%20Huber%20trend's%20intercept%20is%20not%20robust%20(F8%2C%20part%202)%0A%0A%20%20%20%20%60HuberPoolingTrend.fit%60%20makes%20its%20slope%20robust%2C%20then%20computes%20each%20county's%20intercept%20as%20the%20plain%20**mean**%20of%20%24y%20-%20b%5C%2Ct%24%20(%5Bhuber_trend.py%20L38%E2%80%9344%5D(https%3A%2F%2Fgithub.com%2Ftreefera%2Fmodel-foundry%2Fblob%2Ff69eef9df9e6a707c68f0292d940ccfc082cdaf5%2Fmodel_foundry%2Fmodels%2Fyield_module%2Fsrc%2Fyield_module%2Fmodels%2Fhuber_trend.py%23L38-L44))%2C%20as%20%60PartialPoolingTrend%60%20does.%20A%20mean%20gives%20the%20drought%20full%20weight%3A%20it%20lowers%20every%20later%20level%20by%20about%20%24D%2Fn%24%2C%20where%20%24D%24%20is%20the%20drought%20and%20%24n%24%20the%20number%20of%20seasons%20fitted.%20A%20**median**%20ignores%20one%20extreme%20season.%20The%20next%20cell%20reruns%20the%20hindcast%20for%20the%2011%20long-history%20counties%20with%20the%20production%20class%2C%20and%20swaps%20only%20the%20intercept%20rule.%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s3_3b_huber_trends_intercept(%0A%20%20%20%20DROUGHT%2C%0A%20%20%20%20HuberPoolingTrend%2C%0A%20%20%20%20PartialPoolingTrend%2C%0A%20%20%20%20SHORT%2C%0A%20%20%20%20panel%2C%0A)%3A%0A%20%20%20%20def%20intercept_hindcast(model_class%2C%20intercept)%3A%0A%20%20%20%20%20%20%20%20errors%20%3D%20%5B%5D%0A%20%20%20%20%20%20%20%20for%20season%20in%20range(2005%2C%202025)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20past%20%3D%20panel%5Bpanel%5BSEASON_YEAR%5D%20%3C%3D%20season%20-%201%5D%0A%20%20%20%20%20%20%20%20%20%20%20%20now%20%3D%20panel%5B(panel%5BSEASON_YEAR%5D%20%3D%3D%20season)%20%26%20(panel%5BGEO_IDENTIFIER%5D%20!%3D%20SHORT)%5D%0A%20%20%20%20%20%20%20%20%20%20%20%20model%20%3D%20model_class(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20source%3D%22yield%22%2C%20weight%3D%22area%22%2C%20min_seasons%3D3%2C%20national_window_seasons%3D30%0A%20%20%20%20%20%20%20%20%20%20%20%20).fit(past)%0A%20%20%20%20%20%20%20%20%20%20%20%20if%20intercept%20%3D%3D%20%22median%22%3A%20%20%23%20same%20slopes%3B%20only%20the%20intercept%20rule%20changes%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20row_slope%20%3D%20past%5BGEO_IDENTIFIER%5D.map(model.geographies%5B%22slope%22%5D)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20model.geographies%5B%22intercept%22%5D%20%3D%20(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20(past%5B%22yield%22%5D%20-%20row_slope%20*%20past%5BSEASON_YEAR%5D)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20.groupby(past%5BGEO_IDENTIFIER%5D)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20.median()%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20%20%20%20%20predicted%20%3D%20model.predict(now)%5Boutput_column(model)%5D%0A%20%20%20%20%20%20%20%20%20%20%20%20errors.append(pd.DataFrame(%7B%22season%22%3A%20season%2C%20%22error%22%3A%20predicted%20-%20now%5B%22true_trend%22%5D%7D))%0A%20%20%20%20%20%20%20%20errors%20%3D%20pd.concat(errors)%0A%20%20%20%20%20%20%20%20return%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%22MAE%20vs%20true%20trend%22%3A%20errors%5B%22error%22%5D.abs().mean()%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22mean%20bias%22%3A%20errors%5B%22error%22%5D.mean()%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22bias%202013%E2%80%932024%22%3A%20errors.loc%5Berrors%5B%22season%22%5D%20%3E%3D%202013%2C%20%22error%22%5D.mean()%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22rows%22%3A%20len(errors)%2C%0A%20%20%20%20%20%20%20%20%7D%0A%0A%20%20%20%20print(%0A%20%20%20%20%20%20%20%20f%22the%20drought's%20pull%20on%20an%20Iowa%20intercept%20in%20the%202013%20fit%2C%20D%2Fn%3A%20%7B-DROUGHT%5B'iowa'%5D%20%2F%2023%3A.2f%7D%20bu%2Fac%22%0A%20%20%20%20)%0A%20%20%20%20pd.DataFrame(%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%22HuberPoolingTrend%20(production%3A%20mean%20intercept)%22%3A%20intercept_hindcast(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20HuberPoolingTrend%2C%20%22mean%22%0A%20%20%20%20%20%20%20%20%20%20%20%20)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22same%20slopes%2C%20median%20intercept%22%3A%20intercept_hindcast(HuberPoolingTrend%2C%20%22median%22)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22PartialPoolingTrend%20(mean%20intercept)%22%3A%20intercept_hindcast(PartialPoolingTrend%2C%20%22mean%22)%2C%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20).T.round(2)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20Same%20slopes%2C%20a%20median%20intercept%3A%20the%20mean%20error%20against%20the%20true%20trend%20falls%20from%202.17%20to%201.82%2C%20and%20the%20bias%20after%20the%20drought%20from%20%E2%88%921.37%20to%20%E2%88%920.06%20bu%2Fac.%20Most%20of%20the%20Huber%20trend's%20remaining%20bias%20after%202012%20is%20its%20**intercept**%2C%20not%20its%20slope%20or%20its%20standard%20error.%20%60PartialPoolingTrend%60%20(%E2%88%923.43)%20suffers%20twice%2C%20through%20its%20slope%20and%20its%20intercept.%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%203.4%20The%20leak%3A%20a%20trend%20that%20sees%20the%20season%20it%20predicts%0A%0A%20%20%20%20Fit%20**including**%20the%20target%20season%20and%20the%20trend%20partly%20absorbs%20that%20season's%20weather%2C%20so%20it%20looks%20better%20than%20it%20can%20ever%20be%20in%20real%20use%3A%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s3_4_leak_trend_sees(DROUGHT_YEAR%2C%20PartialPoolingTrend%2C%20panel)%3A%0A%20%20%20%20leak%20%3D%20%5B%5D%0A%20%20%20%20for%20_season%20in%20range(2005%2C%202025)%3A%0A%20%20%20%20%20%20%20%20_now%20%3D%20panel%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20(panel%5BSEASON_YEAR%5D%20%3D%3D%20_season)%0A%20%20%20%20%20%20%20%20%20%20%20%20%26%20panel%5BGEO_IDENTIFIER%5D.isin(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20panel.loc%5Bpanel%5BSEASON_YEAR%5D%20%3C%3D%20_season%20-%201%2C%20GEO_IDENTIFIER%5D%0A%20%20%20%20%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20%5D%0A%20%20%20%20%20%20%20%20for%20_label%2C%20_cutoff%20in%20(%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22honest%3A%20fit%20on%20%E2%89%A4%20y%E2%88%921%22%2C%20_season%20-%201)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22leaky%3A%20fit%20on%20%E2%89%A4%20y%22%2C%20_season)%2C%0A%20%20%20%20%20%20%20%20)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20_model%20%3D%20PartialPoolingTrend(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20source%3D%22yield%22%2C%20weight%3D%22area%22%2C%20min_seasons%3D3%2C%20national_window_seasons%3D30%0A%20%20%20%20%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20%20%20%20%20_predicted%20%3D%20_model.fit(panel%5Bpanel%5BSEASON_YEAR%5D%20%3C%3D%20_cutoff%5D).predict(_now)%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20output_column(_model)%0A%20%20%20%20%20%20%20%20%20%20%20%20%5D%0A%20%20%20%20%20%20%20%20%20%20%20%20leak.append(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22fit%22%3A%20_label%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22season%22%3A%20_season%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22error%20vs%20observed%22%3A%20float((_predicted%20-%20_now%5B%22yield%22%5D).abs().mean())%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%20%20%20%20)%0A%20%20%20%20leak%20%3D%20pd.DataFrame(leak)%0A%20%20%20%20print(leak.groupby(%22fit%22)%5B%22error%20vs%20observed%22%5D.mean().round(2).to_dict())%0A%20%20%20%20print(%0A%20%20%20%20%20%20%20%20f%22%7BDROUGHT_YEAR%7D%20only%3A%22%2C%0A%20%20%20%20%20%20%20%20leak%5Bleak%5B%22season%22%5D%20%3D%3D%20DROUGHT_YEAR%5D%0A%20%20%20%20%20%20%20%20.set_index(%22fit%22)%5B%22error%20vs%20observed%22%5D%0A%20%20%20%20%20%20%20%20.round(2)%0A%20%20%20%20%20%20%20%20.to_dict()%2C%0A%20%20%20%20)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20The%20leaky%20fit%20looks%20better%20on%20average%2C%20and%20much%20better%20in%20the%20drought%20year%2C%20because%20the%20drought%20pulled%20its%20own%20line%20down.%20That's%20information%20no%20forecaster%20has%20on%20the%20day.%20model-foundry%20guards%20against%20it%3A%20%60expanding_season_folds%60%20trains%20each%20fold%20on%20seasons%20%E2%89%A4%20y%E2%88%921%2C%20and%20%60level_trends%60%20only%20uses%20seasons%20%E2%89%A4%20y%20%E2%88%92%20%60gap%60%2C%20where%20%60gap%60%20is%20how%20late%20the%20truth%20is%20published.%20The%20guard%20isn't%20complete%2C%20though.%20Early%20in%20season%20%24y%24%2C%20NASS%20hasn't%20yet%20published%20season%20%24y-1%24's%20county%20yields.%20%60expanding_season_folds%60%20can%20train%20only%20on%20what%20was%20published%2C%20through%20its%20%60known_through%60%20column%2C%20but%20the%20yield%20module%20doesn't%20pass%20one%20(**F9**%2C%20%5Bissue%2036%5D(https%3A%2F%2Fgithub.com%2Ftreefera%2Ftf-model-lab%2Fissues%2F36)).%20So%20the%20yield%20hindcast%20trains%20on%20%24y-1%24%20yields%20a%20live%20forecast%20wouldn't%20have%20yet%3A%20a%20smaller%20leak%20of%20the%20same%20kind.%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%203.5%20The%20study%20question%2C%20run%3A%20is%20the%20Huber%20trend%20shrinking%20too%20hard%3F%0A%0A%20%20%20%20%60HuberPoolingTrend%60%20fits%20robust%20county%20slopes%2C%20but%20takes%20each%20slope's%20standard%20error%20from%20the%20OLS%20formula%20on%20the%20Huber%20residuals%20(%5Bhuber_trend.py%20L57%E2%80%9359%5D(https%3A%2F%2Fgithub.com%2Ftreefera%2Fmodel-foundry%2Fblob%2Ff69eef9df9e6a707c68f0292d940ccfc082cdaf5%2Fmodel_foundry%2Fmodels%2Fyield_module%2Fsrc%2Fyield_module%2Fmodels%2Fhuber_trend.py%23L57-L59)).%20The%20drought%20that%20the%20slope%20fit%20down-weights%20still%20enters%20that%20residual%20sum%2C%20squared.%20So%20the%20SE%20is%20inflated%2C%20trust%20%CE%BB%20is%20smaller%2C%20and%20the%20county%20is%20shrunk%20harder%20towards%20the%20national%20slope.%20That%20is%20the%20first%20part%20of%20F8%20(%5Bissue%2035%5D(https%3A%2F%2Fgithub.com%2Ftreefera%2Ftf-model-lab%2Fissues%2F35)).%0A%0A%20%20%20%20The%20path%20not%20taken%20is%20a%20**robust%20standard%20error**.%20The%20simplest%20divides%20a%20robust%20scale%20by%20%24%5Csqrt%7BS_%7Bxx%7D%7D%24%3A%20%241.4826%5C%2C%5Cmathrm%7BMAD%7D(r)%2F%5Csqrt%7BS_%7Bxx%7D%7D%24.%20statsmodels'%20%5B%60robust.scale.mad%60%5D(https%3A%2F%2Fwww.statsmodels.org%2Fv0.15.0%2Fgenerated%2Fstatsmodels.robust.scale.mad.html)%20computes%20%241.4826%5C%2C%5Cmathrm%7BMAD%7D%24%20directly%20(it%20divides%20by%20%24%5CPhi%5E%7B-1%7D(0.75)%20%3D%200.6745%24).%20Huber's%20sandwich%20SE%20from%20%60RLM%60%2C%20met%20in%201.2%2C%20is%20the%20textbook%20version%3B%20the%20MAD%20form%20is%20a%20simpler%20proxy.%20It%20has%20to%20be%20on%20the%20right%20scale%2C%20not%20just%20rank%20the%20counties%20in%20the%20right%20order%3A%20%60shrink_towards%60%20uses%20the%20SE%C2%B2%20values%20themselves%2C%20both%20to%20estimate%20%CF%84%C2%B2%20and%20in%20each%20trust%20%CE%BB%20%3D%20%CF%84%C2%B2%2F(%CF%84%C2%B2%20%2B%20SE%C2%B2).%20For%20slopes%200%20and%202%2C%20SEs%20of%200.25%20and%200.5%20give%20%CE%BB%20%3D%200.93%20and%200.77%3B%20doubling%20both%20keeps%20their%20order%20but%20gives%200.60%20and%200.27.%20For%20normal%20noise%201.4826%20%C3%97%20MAD%20estimates%20%CF%83%2C%20so%20without%20outliers%20the%20MAD%20form%20sits%20on%20the%20same%20scale%20as%20the%20OLS%20form%2C%20which%20is%20what%20makes%20the%20comparison%20below%20fair.%0A%0A%20%20%20%20The%20next%20cell%20holds%20everything%20else%20fixed.%20It%20takes%20model-foundry's%20Huber%20slopes%2C%20on%20its%20season%20axis%2C%20and%20their%20residuals%20%24r%20%3D%20d_y%20-%20b%5C%2Cd_x%24%2C%20exactly%20as%20%60huber_geography_slopes%60%20computes%20them.%20It%20builds%20both%20standard%20errors%20from%20those%20same%20residuals%3A%0A%0A%20%20%20%20-%20the%20OLS%20form%2C%20as%20model-foundry%20does%20it%3A%20%24%5Csqrt%7B%5Csum%20r%5E2%2F(n-2)%5C%2C%2F%5C%2CS_%7Bxx%7D%7D%24%3B%0A%20%20%20%20-%20the%20MAD%20form%3A%20%241.4826%5C%2C%5Cmathrm%7BMAD%7D(r)%2F%5Csqrt%7BS_%7Bxx%7D%7D%24.%0A%0A%20%20%20%20The%20MAD%20subtracts%20the%20median%20residual%20first%2C%20so%20it's%20unchanged%20by%20any%20shift%20of%20the%20residuals%2C%20and%20the%20intercept%20can't%20leak%20in.%20Both%20go%20through%20the%20production%20%60shrink_towards%60%20with%20the%20same%20slopes%20and%20the%20same%20national%20slope.%20We%20fit%20the%2011%20long-history%20counties%20twice%3A%20through%202013%2C%20with%20the%20drought%20second%20from%20last%2C%20and%20through%202023%2C%20with%20it%20mid-window.%20Errors%20are%20compared%20**county%20by%20county**%20(paired)%2C%20not%20only%20as%20averages.%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s3_5_study_question_run(%0A%20%20%20%20DROUGHT_YEAR%2C%0A%20%20%20%20SHORT%2C%0A%20%20%20%20huber_geography_slopes%2C%0A%20%20%20%20national_slope%2C%0A%20%20%20%20panel%2C%0A%20%20%20%20shrink_towards%2C%0A%20%20%20%20true_slope%2C%0A)%3A%0A%20%20%20%20from%20statsmodels.robust.scale%20import%20mad%20as%20robust_mad%20%20%23%20median%20%7Cr%20-%20median(r)%7C%20%2F%200.6745%0A%0A%20%20%20%20def%20controlled_se(window)%3A%0A%20%20%20%20%20%20%20%20%23%20model-foundry's%20Huber%20slopes%20and%20residuals%2C%20and%20two%20standard%20errors%20built%20from%20the%20same%20residuals.%0A%20%20%20%20%20%20%20%20rows%20%3D%20window%5Bwindow.groupby(GEO_IDENTIFIER)%5B%22yield%22%5D.transform(%22size%22)%20%3E%3D%203%5D%0A%20%20%20%20%20%20%20%20geos%20%3D%20rows%5BGEO_IDENTIFIER%5D%0A%20%20%20%20%20%20%20%20dx%20%3D%20rows%5BSEASON_YEAR%5D%20-%20rows.groupby(geos)%5BSEASON_YEAR%5D.transform(%22mean%22)%0A%20%20%20%20%20%20%20%20dy%20%3D%20rows%5B%22yield%22%5D%20-%20rows.groupby(geos)%5B%22yield%22%5D.transform(%22mean%22)%0A%20%20%20%20%20%20%20%20mf%20%3D%20huber_geography_slopes(window%2C%20%22yield%22%2C%203)%0A%20%20%20%20%20%20%20%20resid%20%3D%20dy%20-%20geos.map(mf%5B%22slope%22%5D)%20*%20dx%0A%20%20%20%20%20%20%20%20sxx%20%3D%20(dx%20*%20dx).groupby(geos).sum()%0A%20%20%20%20%20%20%20%20n%20%3D%20geos.groupby(geos).size()%0A%20%20%20%20%20%20%20%20se_ols%20%3D%20np.sqrt((resid**2).groupby(geos).sum()%20%2F%20(n%20-%202)%20%2F%20sxx)%0A%20%20%20%20%20%20%20%20se_mad%20%3D%20resid.groupby(geos).apply(robust_mad)%20%2F%20np.sqrt(sxx)%0A%20%20%20%20%20%20%20%20assert%20np.allclose(se_ols%2C%20mf%5B%22standard_error%22%5D)%20%20%23%20same%20residuals%20as%20production's%20own%20SE%0A%20%20%20%20%20%20%20%20return%20mf%5B%22slope%22%5D%2C%20se_ols%2C%20se_mad%0A%0A%20%20%20%20controlled%20%3D%20%5B%5D%0A%20%20%20%20for%20_cutoff%2C%20window_label%20in%20(%0A%20%20%20%20%20%20%20%20(2013%2C%20f%222013%20(drought%20in%20%7BDROUGHT_YEAR%7D)%22)%2C%0A%20%20%20%20%20%20%20%20(2023%2C%20f%222023%20(drought%20in%20%7BDROUGHT_YEAR%7D)%22)%2C%0A%20%20%20%20)%3A%0A%20%20%20%20%20%20%20%20_window%20%3D%20panel%5B(panel%5BSEASON_YEAR%5D%20%3C%3D%20_cutoff)%20%26%20(panel%5BGEO_IDENTIFIER%5D%20!%3D%20SHORT)%5D%0A%20%20%20%20%20%20%20%20nat_1%20%3D%20national_slope(_window%2C%20%22yield%22%2C%20%22area%22%2C%2030)%0A%20%20%20%20%20%20%20%20_b%2C%20_se_ols%2C%20_se_mad%20%3D%20controlled_se(_window)%0A%20%20%20%20%20%20%20%20_truth_b%20%3D%20true_slope.reindex(_b.index)%0A%20%20%20%20%20%20%20%20row%2C%20_err%20%3D%20(%0A%20%20%20%20%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22fitted%20through%22%3A%20window_label%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22mean%20%7Craw%20slope%20error%7C%22%3A%20float((_b%20-%20_truth_b).abs().mean())%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%7B%7D%2C%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20for%20_name%2C%20se%20in%20((%22OLS-form%20SE%22%2C%20_se_ols)%2C%20(%22MAD%20SE%22%2C%20_se_mad))%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20tau2_se%20%3D%20max(float(np.var(_b)%20-%20np.mean(se**2))%2C%201e-30)%0A%20%20%20%20%20%20%20%20%20%20%20%20_err%5B_name%5D%20%3D%20(shrink_towards(_b%2C%20se%2C%20nat_1)%20-%20_truth_b).abs()%0A%20%20%20%20%20%20%20%20%20%20%20%20row%20%7C%3D%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20f%22mean%20SE%2C%20%7B_name%7D%22%3A%20float(se.mean())%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20f%22%CF%84%C2%B2%2C%20%7B_name%7D%22%3A%20tau2_se%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20f%22mean%20%CE%BB%2C%20%7B_name%7D%22%3A%20float((tau2_se%20%2F%20(tau2_se%20%2B%20se**2)).mean())%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20f%22mean%20%7Cshrunk%20error%7C%2C%20%7B_name%7D%22%3A%20float(_err%5B_name%5D.mean())%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20diff%20%3D%20_err%5B%22OLS-form%20SE%22%5D%20-%20_err%5B%22MAD%20SE%22%5D%0A%20%20%20%20%20%20%20%20row%20%7C%3D%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%22paired%20diff%20(OLS%20%E2%88%92%20MAD)%2C%20mean%22%3A%20float(diff.mean())%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22paired%20diff%2C%20sd%22%3A%20float(diff.std(ddof%3D1))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22counties%20where%20MAD%20SE%20is%20better%22%3A%20f%22%7Bint((diff%20%3E%200).sum())%7D%20of%20%7Blen(diff)%7D%22%2C%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20controlled.append(row)%0A%0A%20%20%20%20pd.DataFrame(controlled).set_index(%22fitted%20through%22).round(3).T%0A%20%20%20%20return%20(controlled_se%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%203.5b%20The%20shrinkage%20target%20is%20not%20robust%20either%20(F8%2C%20part%203)%2C%20and%20the%20result%20over%2030%20draws%0A%0A%20%20%20%20Both%20arms%20above%20shrink%20towards%20the%20same%20%60national_slope%60%3A%20an%20**OLS**%20line%20through%20the%20area-weighted%20national%20means%20(%5Btrend.py%20L82%E2%80%9395%5D(https%3A%2F%2Fgithub.com%2Ftreefera%2Fmodel-foundry%2Fblob%2Ff69eef9df9e6a707c68f0292d940ccfc082cdaf5%2Fmodel_foundry%2Fcommon%2Fquantity_modules%2Ftrend.py%23L82-L95)).%20Through%202013%20the%20drought%20sits%20second%20from%20last%20in%20that%20national%20series%20too%2C%20so%20the%20target%20itself%20is%20pulled%20down.%20The%20next%20cell%20compares%20it%20with%20a%20Huber%20line%20through%20the%20same%20national%20means%2C%20then%20crosses%20both%20targets%20with%20both%20standard%20errors.%20Finally%20it%20reruns%20the%20controlled%202013%20comparison%20on%2030%20synthetic%20panels%2C%20and%20the%20hindcast%20of%20%60HuberPoolingTrend%60%20against%20%60PartialPoolingTrend%60%20on%2010%2C%20so%20the%20conclusion%20doesn't%20rest%20on%20one%20draw.%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s3_5b_shrinkage_target_not(%0A%20%20%20%20HuberPoolingTrend%2C%0A%20%20%20%20PartialPoolingTrend%2C%0A%20%20%20%20SHORT%2C%0A%20%20%20%20controlled_se%2C%0A%20%20%20%20huber_slope%2C%0A%20%20%20%20make_panel%2C%0A%20%20%20%20national_slope%2C%0A%20%20%20%20panel%2C%0A%20%20%20%20shrink_towards%2C%0A%20%20%20%20true_slope%2C%0A)%3A%0A%20%20%20%20def%20national_means(window)%3A%0A%20%20%20%20%20%20%20%20%23%20The%20area-weighted%20national%20mean%20of%20each%20season%2C%20as%20national_slope%20computes%20it.%0A%20%20%20%20%20%20%20%20weights%20%3D%20window%5B%22area%22%5D.where(window%5B%22area%22%5D%20%3E%200%2C%201.0)%0A%20%20%20%20%20%20%20%20seasons%20%3D%20window%5BSEASON_YEAR%5D%0A%20%20%20%20%20%20%20%20means%20%3D%20(window%5B%22yield%22%5D%20*%20weights).groupby(seasons).sum()%20%2F%20weights.groupby(seasons).sum()%0A%20%20%20%20%20%20%20%20return%20means.sort_index().iloc%5B-30%3A%5D%0A%0A%20%20%20%20def%20huber_national_slope(window)%3A%0A%20%20%20%20%20%20%20%20means%20%3D%20national_means(window)%0A%20%20%20%20%20%20%20%20x%20%3D%20means.index.to_numpy(float)%0A%20%20%20%20%20%20%20%20return%20huber_slope(x%20-%20x.mean()%2C%20means.to_numpy()%20-%20means.mean())%0A%0A%20%20%20%20_window%20%3D%20panel%5B(panel%5BSEASON_YEAR%5D%20%3C%3D%202013)%20%26%20(panel%5BGEO_IDENTIFIER%5D%20!%3D%20SHORT)%5D%0A%20%20%20%20areas%20%3D%20_window.groupby(GEO_IDENTIFIER)%5B%22area%22%5D.first()%0A%20%20%20%20targets%20%3D%20%7B%0A%20%20%20%20%20%20%20%20%22OLS%20national%20target%20(production)%22%3A%20national_slope(_window%2C%20%22yield%22%2C%20%22area%22%2C%2030)%2C%0A%20%20%20%20%20%20%20%20%22Huber%20national%20target%22%3A%20huber_national_slope(_window)%2C%0A%20%20%20%20%7D%0A%20%20%20%20print(%0A%20%20%20%20%20%20%20%20%22through%202013%3A%22%2C%0A%20%20%20%20%20%20%20%20%7Bname%3A%20round(value%2C%203)%20for%20name%2C%20value%20in%20targets.items()%7D%2C%0A%20%20%20%20%20%20%20%20f%22%7C%20area-weighted%20mean%20true%20slope%20%7B(true_slope%5Bareas.index%5D%20*%20areas).sum()%20%2F%20areas.sum()%3A.3f%7D%22%2C%0A%20%20%20%20)%0A%20%20%20%20_b%2C%20_se_ols%2C%20_se_mad%20%3D%20controlled_se(_window)%0A%20%20%20%20_truth_b%20%3D%20true_slope.reindex(_b.index)%0A%20%20%20%20crossed%20%3D%20pd.DataFrame(%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20target_name%3A%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20se_name%3A%20float((shrink_towards(_b%2C%20se%2C%20target)%20-%20_truth_b).abs().mean())%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20for%20se_name%2C%20se%20in%20((%22OLS-form%20SE%20(production)%22%2C%20_se_ols)%2C%20(%22MAD%20SE%22%2C%20_se_mad))%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%20%20%20%20for%20target_name%2C%20target%20in%20targets.items()%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20)%0A%20%20%20%20display(crossed.round(3))%0A%0A%20%20%20%20%23%20The%20controlled%202013%20comparison%20(production%20target)%20over%2030%20synthetic%20panels.%0A%20%20%20%20gains%20%3D%20%5B%5D%0A%20%20%20%20for%20seed%20in%20range(30)%3A%0A%20%20%20%20%20%20%20%20seed_panel%2C%20seed_truth%20%3D%20make_panel(seed)%0A%20%20%20%20%20%20%20%20seed_window%20%3D%20seed_panel%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20(seed_panel%5BSEASON_YEAR%5D%20%3C%3D%202013)%20%26%20(seed_panel%5BGEO_IDENTIFIER%5D%20!%3D%20SHORT)%0A%20%20%20%20%20%20%20%20%5D%0A%20%20%20%20%20%20%20%20seed_nat%20%3D%20national_slope(seed_window%2C%20%22yield%22%2C%20%22area%22%2C%2030)%0A%20%20%20%20%20%20%20%20b_s%2C%20se_ols_s%2C%20se_mad_s%20%3D%20controlled_se(seed_window)%0A%20%20%20%20%20%20%20%20truth_s%20%3D%20seed_truth.reindex(b_s.index)%0A%20%20%20%20%20%20%20%20gains.append(%0A%20%20%20%20%20%20%20%20%20%20%20%20float(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20(shrink_towards(b_s%2C%20se_ols_s%2C%20seed_nat)%20-%20truth_s).abs().mean()%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20-%20(shrink_towards(b_s%2C%20se_mad_s%2C%20seed_nat)%20-%20truth_s).abs().mean()%0A%20%20%20%20%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20gains%20%3D%20np.array(gains)%0A%20%20%20%20print(%0A%20%20%20%20%20%20%20%20f%2230%20panels%2C%202013%3A%20mean%20gain%20from%20the%20MAD%20SE%20%7Bgains.mean()%3A.3f%7D%20(sd%20%7Bgains.std(ddof%3D1)%3A.3f%7D)%3B%22%0A%20%20%20%20%20%20%20%20f%22%20MAD%20better%20in%20%7B(gains%20%3E%200).sum()%7D%20of%2030%22%0A%20%20%20%20)%0A%0A%20%20%20%20%23%20The%20hindcast%2C%20HuberPoolingTrend%20against%20PartialPoolingTrend%2C%20over%2010%20synthetic%20panels.%0A%20%20%20%20scores%20%3D%20%7B%22HuberPoolingTrend%22%3A%20%5B%5D%2C%20%22PartialPoolingTrend%22%3A%20%5B%5D%7D%0A%20%20%20%20for%20seed%20in%20range(10)%3A%0A%20%20%20%20%20%20%20%20seed_panel%2C%20_%20%3D%20make_panel(seed)%0A%20%20%20%20%20%20%20%20for%20_name%2C%20model_class%20in%20(%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22HuberPoolingTrend%22%2C%20HuberPoolingTrend)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22PartialPoolingTrend%22%2C%20PartialPoolingTrend)%2C%0A%20%20%20%20%20%20%20%20)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20errors%20%3D%20%5B%5D%0A%20%20%20%20%20%20%20%20%20%20%20%20for%20_season%20in%20range(2005%2C%202025)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20_past%20%3D%20seed_panel%5Bseed_panel%5BSEASON_YEAR%5D%20%3C%3D%20_season%20-%201%5D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20_now%20%3D%20seed_panel%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20(seed_panel%5BSEASON_YEAR%5D%20%3D%3D%20_season)%20%26%20(seed_panel%5BGEO_IDENTIFIER%5D%20!%3D%20SHORT)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%5D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20_model%20%3D%20model_class(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20source%3D%22yield%22%2C%20weight%3D%22area%22%2C%20min_seasons%3D3%2C%20national_window_seasons%3D30%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20).fit(_past)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20errors.append(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20(_model.predict(_now)%5Boutput_column(_model)%5D%20-%20_now%5B%22true_trend%22%5D).abs()%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20%20%20%20%20scores%5B_name%5D.append(float(pd.concat(errors).mean()))%0A%20%20%20%20huber_wins%20%3D%20sum(%0A%20%20%20%20%20%20%20%20h%20%3C%20p%0A%20%20%20%20%20%20%20%20for%20h%2C%20p%20in%20zip(scores%5B%22HuberPoolingTrend%22%5D%2C%20scores%5B%22PartialPoolingTrend%22%5D%2C%20strict%3DTrue)%0A%20%20%20%20)%0A%20%20%20%20print(%0A%20%20%20%20%20%20%20%20f%2210%20panels%2C%20hindcast%20MAE%20vs%20true%20trend%3A%20HuberPoolingTrend%20%7Bnp.mean(scores%5B'HuberPoolingTrend'%5D)%3A.2f%7D%2C%22%0A%20%20%20%20%20%20%20%20f%22%20PartialPoolingTrend%20%7Bnp.mean(scores%5B'PartialPoolingTrend'%5D)%3A.2f%7D%3B%20Huber%20better%20in%20%7Bhuber_wins%7D%20of%2010%22%0A%20%20%20%20)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20**Reading%203.5**%20(model-foundry's%20slopes%20and%20residuals%20in%20both%20arms%3B%20only%20the%20SE%20formula%20changes)%3A%0A%0A%20%20%20%20-%20**With%20the%20drought%20near%20the%20end%20of%20the%20window%2C%20the%20OLS-form%20SE%20is%20about%20twice%20the%20MAD%20SE**%20(0.300%20against%200.159).%20%CF%84%C2%B2%20comes%20out%20smaller%20(0.391%20against%200.456)%2C%20so%20trust%20%CE%BB%20is%20smaller%20(0.813%20against%200.946)%2C%20and%20the%20shrunk%20slopes%20are%20worse%20on%20average%3A%200.209%20against%200.158%2C%20from%20raw%20slope%20errors%20of%200.174.%0A%20%20%20%20-%20**County%20by%20county%2C%20the%20effect%20is%20noisy%20on%20one%20draw.**%20The%20paired%20difference%20(OLS-form%20minus%20MAD)%20is%200.050%20%C2%B1%200.106%2C%20and%20the%20MAD%20SE%20is%20better%20in%207%20of%2011%20counties.%0A%20%20%20%20-%20**Over%2030%20synthetic%20panels%20it%20is%20firm**%20(3.5b)%3A%20the%20MAD%20SE%20is%20better%20in%2027%20of%2030%2C%20with%20a%20mean%20gain%20of%200.067%20(sd%200.056).%0A%20%20%20%20-%20**With%20the%20drought%20mid-window%2C%20there%20is%20no%20effect%3A**%200.119%20against%200.118%2C%20a%20paired%20difference%20of%200.001%20%C2%B1%200.025%2C%20MAD%20better%20in%206%20of%2011.%0A%0A%20%20%20%20**Reading%203.5b%2C%20the%20target**%20(F8%20part%203)%3A%20through%202013%20the%20production%20national%20slope%20is%201.225%2C%20against%20an%20area-weighted%20mean%20true%20slope%20of%201.572.%20A%20Huber%20line%20through%20the%20same%20national%20means%20gives%201.545.%20Shrinking%20towards%20it%20cuts%20the%20mean%20shrunk%20error%20from%200.209%20to%200.183%20with%20the%20OLS-form%20SE%2C%20and%20from%200.158%20to%200.149%20with%20the%20MAD%20SE.%20So%20the%20SE%20effect%20survives%20a%20better%20target%2C%20but%20part%20of%20the%20over-shrinking%20harm%20comes%20from%20the%20drought-biased%20target%2C%20not%20from%20the%20SE.%0A%0A%20%20%20%20**The%20intercept**%20(3.3b%2C%20F8%20part%202)%20is%20a%20third%2C%20separate%20effect.%20It%20shifts%20every%20level%2C%20not%20the%20slope%2C%20and%20it%20carries%20most%20of%20the%20Huber%20trend's%20bias%20after%202012%20in%20the%20hindcast.%20Neither%20the%20SE%20nor%20the%20target%20can%20explain%20that%20bias.%0A%0A%20%20%20%20**Huber%20against%20plain%20pooling**%20holds%20up%20too%3A%20over%2010%20synthetic%20panels%2C%20%60HuberPoolingTrend%60%20scores%203.35%20against%20%60PartialPoolingTrend%60's%204.10%2C%20and%20wins%20in%20all%2010.%0A%0A%20%20%20%20This%20is%20the%20evidence%20behind%20%5Bscience.md%5D(https%3A%2F%2Fgithub.com%2Ftreefera%2Ftf-model-lab%2Fblob%2Fmain%2Facademy%2F2-time-and-signals%2F2.2-trends%2Fscience.md)'s%20study%20question.%20It%20is%20synthetic%20only%2C%20and%20can't%20tell%20you%20the%20effect%20on%20real%20corn%20data.%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%20Part%204%3A%20paths%20not%20travelled%2C%20tried%20live%0A%0A%20%20%20%20%23%23%204.1%20Other%20trend%20shapes%20on%20one%20county%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s4_1_other_trend_shapes(STORY%2C%20panel)%3A%0A%20%20%20%20from%20scipy.stats%20import%20theilslopes%0A%20%20%20%20from%20statsmodels.nonparametric.smoothers_lowess%20import%20lowess%0A%0A%20%20%20%20county%20%3D%20panel%5B(panel%5BGEO_IDENTIFIER%5D%20%3D%3D%20STORY)%5D.sort_values(SEASON_YEAR)%0A%20%20%20%20_past%2C%20target%20%3D%20county%5Bcounty%5BSEASON_YEAR%5D%20%3C%3D%202023%5D%2C%20county%5Bcounty%5BSEASON_YEAR%5D%20%3D%3D%202024%5D%0A%20%20%20%20_s%2C%20yv%20%3D%20_past%5BSEASON_YEAR%5D.to_numpy(float)%2C%20_past%5B%22yield%22%5D.to_numpy(float)%0A%20%20%20%20truth_2024%20%3D%20float(target%5B%22true_trend%22%5D.iloc%5B0%5D)%0A%0A%20%20%20%20options%20%3D%20%7B%0A%20%20%20%20%20%20%20%20%22OLS%20line%22%3A%20np.polyval(np.polyfit(_s%2C%20yv%2C%201)%2C%202024)%2C%0A%20%20%20%20%20%20%20%20%22quadratic%22%3A%20np.polyval(np.polyfit(_s%20-%202007%2C%20yv%2C%202)%2C%202024%20-%202007)%2C%0A%20%20%20%20%20%20%20%20%22Theil%E2%80%93Sen%20line%22%3A%20(lambda%20r%3A%20r.intercept%20%2B%20r.slope%20*%202024)(theilslopes(yv%2C%20_s))%2C%0A%20%20%20%20%20%20%20%20%22LOESS%20(frac%200.5)%2C%20last%20value%22%3A%20lowess(yv%2C%20_s%2C%20frac%3D0.5)%5B-1%2C%201%5D%2C%0A%20%20%20%20%7D%0A%20%20%20%20ucm%20%3D%20sm.tsa.UnobservedComponents(yv%2C%20level%3D%22local%20linear%20trend%22).fit(disp%3DFalse)%0A%20%20%20%20options%5B%22local%20linear%20trend%20(Kalman)%22%5D%20%3D%20float(ucm.forecast(1)%5B0%5D)%0A%20%20%20%20pd.Series(options%2C%20name%3D%222024%20trend%22).to_frame().assign(%0A%20%20%20%20%20%20%20%20**%7B%22error%20vs%20true%22%3A%20lambda%20f%3A%20f%5B%222024%20trend%22%5D%20-%20truth_2024%7D%0A%20%20%20%20).round(2)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20-%20**Quadratic**%20bends%20to%20fit%20the%20past%2C%20then%20extrapolates%20the%20bend.%0A%20%20%20%20-%20**Theil%E2%80%93Sen**%20(the%20median%20of%20pairwise%20slopes)%20is%20robust%20like%20Huber%2C%20with%20no%20tuning%20constant.%0A%20%20%20%20-%20**LOESS**%20is%20a%20smoother%2C%20so%20it%20has%20the%20same%20end%20problem%20as%20the%20Gaussian.%0A%20%20%20%20-%20**The%20local%20linear%20trend**%20is%20a%20state-space%20model%20whose%20level%20*and*%20slope%20drift%20over%20time%2C%20estimated%20by%20a%20Kalman%20filter.%20It's%20the%20principled%20version%20of%20%22recent%20seasons%20matter%20more%22%2C%20which%20%60ewm_linear%60%20approximates%20with%20a%20half-life.%20Here%20it%20lands%20exactly%20on%20the%20OLS%20line%3A%20given%20a%20series%20with%20a%20genuinely%20constant%20slope%2C%20the%20fit%20estimated%20no%20drift%2C%20so%20it%20collapsed%20to%20a%20fixed%20line.%20That's%20the%20right%20answer%20for%20this%20data.%0A%0A%20%20%20%20On%20one%20county%20all%20five%20land%20within%20about%202.5%20bu%2Fac%20of%20the%20truth%2C%20and%20the%20ranking%20would%20change%20with%20another%20seed.%20The%20differences%20that%20matter%20show%20up%20across%20many%20counties%20and%20seasons%2C%20and%20at%20the%20end%20of%20the%20series%2C%20as%20in%20Part%203.%0A%0A%20%20%20%20%23%23%204.2%20No%20pooling%20against%20complete%20pooling%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s4_2_no_pooling_complete(geography_slopes%2C%20national_slope%2C%20panel%2C%20preds)%3A%0A%20%20%20%20pools%20%3D%20%5B%5D%0A%20%20%20%20for%20_season%20in%20range(2005%2C%202025)%3A%0A%20%20%20%20%20%20%20%20_past%20%3D%20panel%5Bpanel%5BSEASON_YEAR%5D%20%3C%3D%20_season%20-%201%5D%0A%20%20%20%20%20%20%20%20_now%20%3D%20panel%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20(panel%5BSEASON_YEAR%5D%20%3D%3D%20_season)%0A%20%20%20%20%20%20%20%20%20%20%20%20%26%20panel%5BGEO_IDENTIFIER%5D.isin(_past%5BGEO_IDENTIFIER%5D.unique())%0A%20%20%20%20%20%20%20%20%5D%0A%20%20%20%20%20%20%20%20nat_2%20%3D%20national_slope(_past%2C%20%22yield%22%2C%20%22area%22%2C%2030)%0A%20%20%20%20%20%20%20%20own%20%3D%20geography_slopes(_past%2C%20%22yield%22%2C%203)%5B%22slope%22%5D%0A%20%20%20%20%20%20%20%20%23%20Own%20OLS%20needs%203%20prior%20seasons%3B%20a%20county%20without%20them%20would%20silently%20get%20the%20national%20slope%2C%20so%20score%20only%20counties%20that%20have%20one.%0A%20%20%20%20%20%20%20%20_now%20%3D%20_now%5B_now%5BGEO_IDENTIFIER%5D.isin(own.index)%5D%0A%20%20%20%20%20%20%20%20for%20_label%2C%20_slopes%20in%20(%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22no%20pooling%20(own%20OLS)%22%2C%20own)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22complete%20pooling%20(national%20slope)%22%2C%20own%20*%200%20%2B%20nat_2)%2C%0A%20%20%20%20%20%20%20%20)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20_slope%20%3D%20_now%5BGEO_IDENTIFIER%5D.map(_slopes)%0A%20%20%20%20%20%20%20%20%20%20%20%20intercept%20%3D%20(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20(_past%5B%22yield%22%5D%20-%20_past%5BGEO_IDENTIFIER%5D.map(_slopes)%20*%20_past%5BSEASON_YEAR%5D)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20.groupby(_past%5BGEO_IDENTIFIER%5D)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20.mean()%0A%20%20%20%20%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20%20%20%20%20_predicted%20%3D%20_now%5BGEO_IDENTIFIER%5D.map(intercept)%20%2B%20_slope%20*%20_now%5BSEASON_YEAR%5D%0A%20%20%20%20%20%20%20%20%20%20%20%20pools.append(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20pd.DataFrame(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7B%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%22model%22%3A%20_label%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%20GEO_IDENTIFIER%3A%20_now%5BGEO_IDENTIFIER%5D.to_numpy()%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%22season%22%3A%20_season%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%22error%20vs%20true%20trend%22%3A%20(_predicted%20-%20_now%5B%22true_trend%22%5D).abs().to_numpy()%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20%20%20%20%20)%0A%20%20%20%20pooled%20%3D%20pd.concat(pools)%0A%20%20%20%20scored%20%3D%20pooled%5B%5BGEO_IDENTIFIER%2C%20%22season%22%5D%5D.drop_duplicates()%0A%20%20%20%20partial%20%3D%20preds%5Bpreds%5B%22model%22%5D%20%3D%3D%20%22PartialPoolingTrend%22%5D.merge(%0A%20%20%20%20%20%20%20%20scored%2C%20on%3D%5BGEO_IDENTIFIER%2C%20%22season%22%5D%0A%20%20%20%20)%0A%20%20%20%20print(%0A%20%20%20%20%20%20%20%20f%22scored%20on%20%7Blen(scored)%7D%20geography-seasons%3B%20%7Blen(preds%5Bpreds%5B'model'%5D%20%3D%3D%20'PartialPoolingTrend'%5D)%20-%20len(partial)%7D%20rows%20dropped%20because%20the%20county%20had%20fewer%20than%203%20prior%20seasons%22%0A%20%20%20%20)%0A%20%20%20%20pd.concat(%5Bpooled%2C%20partial%5D)%5B%5B%22model%22%2C%20%22error%20vs%20true%20trend%22%5D%5D.groupby(%22model%22)%5B%0A%20%20%20%20%20%20%20%20%22error%20vs%20true%20trend%22%0A%20%20%20%20%5D.agg(%5B%22mean%22%2C%20%22size%22%5D).round(2)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20Complete%20pooling%20uses%20one%20national%20slope%20for%20every%20county%2C%20with%20county%20intercepts%20only.%20It%20wins%20when%20county%20slopes%20really%20are%20the%20same%20and%20loses%20when%20they%20genuinely%20differ.%20In%20this%20panel%20they%20differ%20by%20design.%20Partial%20pooling%20sits%20between%20the%20two%2C%20choosing%20per%20county.%0A%0A%20%20%20%20%23%20Part%205%3A%20exercises%0A%0A%20%20%20%20Change%20one%20thing%2C%20predict%20the%20result%2C%20then%20run%20it.%0A%0A%20%20%20%201.%20In%202.1%2C%20set%20the%20county%20slope%20spread%20(%60r.normal(0%2C%200.6)%60)%20to%20%600.05%60.%20What%20happens%20to%20%CF%84%C2%B2%20and%20to%20the%20shrunk%20slopes%3F%20Which%20pooling%20wins%20in%204.2%20now%3F%20*Check%3A*%20%CF%84%C2%B2%20is%20still%20inflated%20by%20%60newcounty%60%3B%20recompute%20it%20without%20%60newcounty%60%2C%20as%20in%203.1%2C%20and%20it%20falls%20to%20its%20floor.%20Complete%20pooling%20now%20beats%20no%20pooling%20in%204.2.%0A%20%20%20%202.%20In%202.1%2C%20set%20%60DROUGHT_YEAR%20%3D%202000%60%2C%20the%20*middle*%20of%20the%201990%E2%80%932013%20window%2C%20then%20rerun%20from%202.1%20and%20read%203.2.%20Does%20Huber%20still%20help%3F%20Why%20does%20position%20matter%3F%20(Every%20later%20section%20changes%20too.)%20*Check%3A*%20the%20influence%20formula%20in%203.2%20predicts%20an%20OLS%20slope%20error%20of%20about%20%2B0.06%20for%20Iowa%2C%20so%20Huber%20has%20almost%20nothing%20to%20fix.%0A%20%20%20%203.%20Change%20%60sigma_seasons%60%20from%204%20to%201.5%20in%203.3.%20What%20happens%20to%20%60GaussianSmoothTrend%60's%20bias%2C%20and%20to%20its%20error%20vs%20observed%3F%20*Check%3A*%20by%20%24b%5C%2C%5Csigma%2F%5Csqrt%7B2%5Cpi%7D%24%20the%20end%20lag%20shrinks%20in%20proportion%20to%20%CF%83%2C%20so%20the%20bias%20moves%20towards%20zero%2C%20while%20the%20level%20follows%20the%20weather%20of%20each%20season%20more%20closely.%0A%20%20%20%204.%20In%203.3%2C%20change%20the%20%60ewm_linear%60%20half-life%20from%2010%20to%203%2C%20which%20acts%20as%201.5%20(F7).%20Which%20seasons%20does%20it%20now%20track%20best%2C%20and%20why%3F%20*Check%3A*%20with%20an%20effective%20half-life%20of%201.5%20seasons%20the%20line%20rests%20on%20the%20last%20few%20seasons%2C%20so%20it%20is%20noisier%20everywhere%2C%20and%20worst%20just%20after%202012%2C%20when%20the%20drought%20is%20one%20of%20the%20few%20seasons%20it%20trusts.%0A%20%20%20%205.%20Write%20a%20complete-pooling%20trend%20(one%20national%20slope%2C%20county%20intercepts)%20as%20a%20class%20with%20%60fit%60%2C%20%60predict%60%20and%20%60operation%20%3D%20%22trend%22%60%2C%20and%20drop%20it%20into%20the%203.3%20loop.%20*Check%3A*%20against%20the%20true%20trend%20it%20should%20score%20close%20to%20complete%20pooling's%207.41%20in%204.2.%0A%0A%20%20%20%20%23%20Sources%0A%0A%20%20%20%20Documentation%20links%20are%20pinned%20to%20the%20locked%20versions%20(numpy%202.5%2C%20pandas%202.3.3%2C%20scipy%201.18%2C%20scikit-learn%201.9%2C%20statsmodels%200.15.0%3B%20scipy%20publishes%20no%201.18.1%20pages%2C%20so%20its%20links%20point%20at%201.18.0).%0A%0A%20%20%20%20-%20%60numpy.polyfit%60%2C%20whose%20%60w%60%20parameter%20is%20the%20whole%20of%20F7%3A%20https%3A%2F%2Fnumpy.org%2Fdoc%2F2.5%2Freference%2Fgenerated%2Fnumpy.polyfit.html%0A%20%20%20%20-%20statsmodels%20regression%3A%20https%3A%2F%2Fwww.statsmodels.org%2Fv0.15.0%2Fregression.html%20and%20%60WLS%60%3A%20https%3A%2F%2Fwww.statsmodels.org%2Fv0.15.0%2Fgenerated%2Fstatsmodels.regression.linear_model.WLS.html%0A%20%20%20%20-%20scikit-learn%20%60HuberRegressor%60%3A%20https%3A%2F%2Fscikit-learn.org%2F1.9%2Fmodules%2Fgenerated%2Fsklearn.linear_model.HuberRegressor.html%20and%20the%20Huber%20loss%3A%20https%3A%2F%2Fen.wikipedia.org%2Fwiki%2FHuber_loss%0A%20%20%20%20-%20statsmodels%20%60RLM%60%3A%20https%3A%2F%2Fwww.statsmodels.org%2Fv0.15.0%2Fgenerated%2Fstatsmodels.robust.robust_linear_model.RLM.html%20and%20%60HuberT%60%3A%20https%3A%2F%2Fwww.statsmodels.org%2Fv0.15.0%2Fgenerated%2Fstatsmodels.robust.norms.HuberT.html%0A%20%20%20%20-%20%60scipy.ndimage.gaussian_filter1d%60%20(%60mode%60%2C%20%60truncate%60)%3A%20https%3A%2F%2Fdocs.scipy.org%2Fdoc%2Fscipy-1.18.0%2Freference%2Fgenerated%2Fscipy.ndimage.gaussian_filter1d.html%0A%20%20%20%20-%20pandas%20%60DataFrameGroupBy.transform%60%3A%20https%3A%2F%2Fpandas.pydata.org%2Fpandas-docs%2Fversion%2F2.3.3%2Freference%2Fapi%2Fpandas.core.groupby.DataFrameGroupBy.transform.html%0A%20%20%20%20-%20statsmodels%20mixed%20models%3A%20https%3A%2F%2Fwww.statsmodels.org%2Fv0.15.0%2Fmixed_linear.html%20and%20%60VCSpec%60%3A%20https%3A%2F%2Fwww.statsmodels.org%2Fv0.15.0%2Fgenerated%2Fstatsmodels.regression.mixed_linear_model.VCSpec.html%0A%20%20%20%20-%20The%20James%E2%80%93Stein%20estimator%3A%20https%3A%2F%2Fen.wikipedia.org%2Fwiki%2FJames%25E2%2580%2593Stein_estimator%3B%20Efron%20and%20Morris%20(1977)%2C%20%22Stein's%20paradox%20in%20statistics%22%2C%20*Scientific%20American*%20236(5).%20Efron%20and%20Morris%20(1975)%2C%20%22Data%20analysis%20using%20Stein's%20estimator%20and%20its%20generalizations%22%2C%20*JASA*%2070(350)%2C%20311%E2%80%93319.%20Efron%20and%20Hastie%20(2016)%2C%20*Computer%20Age%20Statistical%20Inference*%2C%20chapter%207%20(free%20PDF)%3A%20https%3A%2F%2Fhastie.su.domains%2FCASI%2F%0A%20%20%20%20-%20Fay%20and%20Herriot%20(1979)%2C%20*JASA*%2074(366)%2C%20269%E2%80%93277%3B%20DerSimonian%20and%20Laird%20(1986)%2C%20*Controlled%20Clinical%20Trials*%207(3)%2C%20177%E2%80%93188%3A%20precision-weighted%20%CF%84%C2%B2.%0A%20%20%20%20-%20Theil%E2%80%93Sen%3A%20https%3A%2F%2Fdocs.scipy.org%2Fdoc%2Fscipy-1.18.0%2Freference%2Fgenerated%2Fscipy.stats.theilslopes.html%3B%20LOESS%3A%20https%3A%2F%2Fwww.statsmodels.org%2Fv0.15.0%2Fgenerated%2Fstatsmodels.nonparametric.smoothers_lowess.lowess.html%3B%20local%20linear%20trend%3A%20https%3A%2F%2Fwww.statsmodels.org%2Fv0.15.0%2Fgenerated%2Fstatsmodels.tsa.statespace.structural.UnobservedComponents.html%0A%20%20%20%20-%20Gelman%20and%20Hill%20(2007)%2C%20*Data%20Analysis%20Using%20Regression%20and%20Multilevel%2FHierarchical%20Models*%2C%20Cambridge%20University%20Press%3A%20partial%20pooling%20explained%20properly.%0A%20%20%20%20-%20The%20companion%20reading%20in%20this%20folder%3A%20%5Bcode.md%5D(https%3A%2F%2Fgithub.com%2Ftreefera%2Ftf-model-lab%2Fblob%2Fmain%2Facademy%2F2-time-and-signals%2F2.2-trends%2Fcode.md)%20and%20%5Bscience.md%5D(https%3A%2F%2Fgithub.com%2Ftreefera%2Ftf-model-lab%2Fblob%2Fmain%2Facademy%2F2-time-and-signals%2F2.2-trends%2Fscience.md).%0A%20%20%20%20%22%22%22)%0A%0A%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20app.run()%0A
9e93d80ad0f528f307c77ac9b26a4bb3