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%20os%0A%20%20%20%20import%20sys%0A%0A%20%20%20%20for%20var%20in%20(%22PROJ_LIB%22%2C%20%22PROJ_DATA%22%2C%20%22GDAL_DATA%22)%3A%0A%20%20%20%20%20%20%20%20os.environ.pop(var%2C%20None)%20%20%23%20a%20conda%20PROJ%20install%20on%20the%20path%20breaks%20rasterio's%20bundled%20one%0A%0A%20%20%20%20import%20time%0A%0A%20%20%20%20import%20lightgbm%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%20sklearn%0A%20%20%20%20import%20xgboost%0A%20%20%20%20from%20loguru%20import%20logger%0A%20%20%20%20from%20sklearn.ensemble%20import%20RandomForestRegressor%0A%20%20%20%20from%20sklearn.tree%20import%20DecisionTreeRegressor%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%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%0A%20%20%20%20def%20rmse(prediction%2C%20truth)%3A%0A%20%20%20%20%20%20%20%20return%20float(np.sqrt(np.mean((np.asarray(prediction)%20-%20np.asarray(truth))%20**%202)))%0A%0A%20%20%20%20print(%0A%20%20%20%20%20%20%20%20%22xgboost%22%2C%0A%20%20%20%20%20%20%20%20xgboost.__version__%2C%0A%20%20%20%20%20%20%20%20%22%7C%20lightgbm%22%2C%0A%20%20%20%20%20%20%20%20lightgbm.__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)%0A%0A%20%20%20%20%23%20Imported%20here%20because%20later%20cells%20use%20them%20too.%0A%20%20%20%20from%20area_module.models.coherence%20import%20project_abandonment%2C%20reconcile_levels%0A%20%20%20%20from%20area_module.models.joint%20import%20OBSERVED%2C%20loss_coefficients%0A%20%20%20%20from%20model_foundry.common.modelling.model_core%20import%20GEO_IDENTIFIER%2C%20INIT_DATE%2C%20SEASON_YEAR%0A%20%20%20%20from%20model_foundry.common.quantity_modules.levels%20import%20row_levels%0A%20%20%20%20from%20yield_module.models.bottom_up%20import%20built_from_children%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%20Gradient-boosted%20trees%20in%20model-foundry%2C%20and%20the%20hierarchy%20they%20forecast%0A%0A%20%20%20%20**What%20you'll%20learn**%0A%0A%20%20%20%201.%20What%20a%20regression%20tree%2C%20a%20random%20forest%20and%20gradient%20boosting%20are%2C%20starting%20from%20the%20libraries'%20own%20introductory%20examples%2C%20and%20boosting%20written%20from%20scratch%20in%20a%20few%20lines.%0A%20%20%20%202.%20How%20model-foundry%20wraps%20XGBoost%20and%20LightGBM%20behind%20one%20small%20contract%2C%20and%20why%20the%20yield%20path's%20predictions%20come%20with%20an%20exact%20per-feature%20explanation%20(the%20area%20joint%20booster%20returns%20predictions%20only).%0A%20%20%20%203.%20What%20the%20trees%20learn%20from%20crop-weather%20features%2C%20and%20what%20the%20paths%20we%20didn't%20take%20(a%20random%20forest%2C%20ridge%20on%20principal%20components%2C%20deeper%20trees%2C%20weights%20in%20the%20wrong%20place)%20would%20change.%0A%20%20%20%204.%20How%20production%20makes%20county%2C%20state%20and%20national%20forecasts%20agree%3A%20yields%20rolled%20up%20bottom-up%2C%20area%20reconciled%20top-down%20by%20least%20squares%2C%20harvested%20held%20at%20most%20planted%2C%20and%20why%20Brazil%20soy's%20state%20weights%20are%20in%20the%20wrong%20units%20(F10).%0A%0A%20%20%20%20**How%20to%20run%3A**%20follow%20the%20set-up%20in%20%60academy%2FREADME.md%60%2C%20then%20from%20%60academy%2F%60%2C%20open%20it%20with%20%60uv%20run%20marimo%20edit%203-model-families%2F3.1-gradient-boosted-trees%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.%20Nothing%20here%20needs%20network%20access%20or%20AWS%20credentials.%20All%20the%20data%20is%20synthetic%20but%20shaped%20like%20county%20yields.%0A%0A%20%20%20%20The%20production%20code%20is%20imported%2C%20not%20copied.%20%60area-module%60%2C%20%60yield-module%60%20and%20%60model-foundry-common%60%20are%20installed%20from%20model-foundry%20at%20%60f69eef9%60%2C%20and%20every%20%60show(...)%60%20cell%20prints%20the%20real%20source%20with%20a%20permalink%20to%20that%20commit.%20XGBoost%20is%20exactly%203.2.0%2C%20the%20version%20production%20pins.%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%20One%20regression%20tree%0A%0A%20%20%20%20A%20regression%20tree%20splits%20the%20data%20with%20yes%2Fno%20questions%20such%20as%20%22is%20extreme%20heat%20above%2060%3F%22%2C%20and%20predicts%20the%20**mean%20target%20of%20the%20training%20rows%20in%20each%20final%20box%20(leaf)**.%20The%20result%20is%20a%20step%20function.%20This%20is%20the%20%5Bscikit-learn%20decision%20tree%5D(https%3A%2F%2Fscikit-learn.org%2F1.9%2Fmodules%2Ftree.html)%20on%20one%20feature%3A%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s1_1_regression_tree()%3A%0A%20%20%20%20_rng%20%3D%20np.random.default_rng(0)%0A%20%20%20%20heat%20%3D%20np.sort(_rng.uniform(0%2C%20120%2C%20300))%0A%20%20%20%20yield_loss%20%3D%20-20.0%20*%20np.maximum(0.0%2C%20heat%20-%2060.0)%20%2B%20_rng.normal(%0A%20%20%20%20%20%20%20%200%2C%20150%2C%20300%0A%20%20%20%20)%20%20%23%20damage%20only%20above%20a%2060%20threshold%0A%0A%20%20%20%20_fig%2C%20_ax%20%3D%20plt.subplots(figsize%3D(8%2C%203.5))%0A%20%20%20%20_ax.scatter(heat%2C%20yield_loss%2C%20s%3D6%2C%20alpha%3D0.4%2C%20label%3D%22observations%22)%0A%20%20%20%20for%20_depth%20in%20(1%2C%202%2C%204)%3A%0A%20%20%20%20%20%20%20%20tree%20%3D%20DecisionTreeRegressor(max_depth%3D_depth).fit(heat%5B%3A%2C%20None%5D%2C%20yield_loss)%0A%20%20%20%20%20%20%20%20_ax.step(%0A%20%20%20%20%20%20%20%20%20%20%20%20heat%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20tree.predict(heat%5B%3A%2C%20None%5D)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20where%3D%22mid%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20label%3Df%22tree%2C%20max_depth%3D%7B_depth%7D%20(%7B2**_depth%7D%20leaves%20at%20most)%22%2C%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20if%20_depth%20%3C%3D%202%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20cuts%20%3D%20sorted(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20t%20for%20t%20in%20tree.tree_.threshold%20if%20t%20!%3D%20-2%0A%20%20%20%20%20%20%20%20%20%20%20%20)%20%20%23%20-2%20marks%20a%20leaf%2C%20which%20has%20no%20cut%0A%20%20%20%20%20%20%20%20%20%20%20%20print(f%22max_depth%3D%7B_depth%7D%3A%20cuts%20at%20%7B%5Bround(float(t)%2C%201)%20for%20t%20in%20cuts%5D%7D%20degree%20days%22)%0A%20%20%20%20_ax.set(%0A%20%20%20%20%20%20%20%20xlabel%3D%22extreme%20heat%20(degree%20days)%22%2C%0A%20%20%20%20%20%20%20%20ylabel%3D%22yield%20anomaly%20(kg%2Fha)%22%2C%0A%20%20%20%20%20%20%20%20title%3D%22One%20cut%20can't%20find%20the%20kink%20at%2060%3B%20deeper%20trees%20close%20in%20on%20it%22%2C%0A%20%20%20%20)%0A%20%20%20%20_ax.legend()%0A%20%20%20%20plt.show()%0A%20%20%20%20return%20heat%2C%20yield_loss%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%20depth-1%20tree%20(a%20%22stump%22)%20asks%20one%20question%2C%20and%20puts%20its%20one%20cut%20where%20it%20best%20separates%20low%20from%20high%20yields%3A%20at%2080.4%20here%2C%20inside%20the%20damage%20region%2C%20not%20at%20the%2060%20threshold.%20For%20a%20hinge%20like%20this%2C%20the%20cut%20that%20most%20reduces%20squared%20error%20lies%20in%20the%20sloping%20part%2C%20not%20at%20the%20kink.%20Deeper%20trees%20add%20cuts%20that%20close%20in%20on%20the%20kink%20(depth%202%20adds%2069.5%20and%20102.0).%20One%20tree%20is%20crude%2C%20and%20a%20deep%20one%20memorises%20noise.%20The%20two%20families%20below%20are%20two%20ways%20of%20combining%20many%20trees.%0A%0A%20%20%20%20%23%23%201.2%20A%20random%20forest%3A%20many%20deep%20trees%2C%20averaged%0A%0A%20%20%20%20A%20%5Brandom%20forest%5D(https%3A%2F%2Fscikit-learn.org%2F1.9%2Fmodules%2Fensemble.html)%20grows%20hundreds%20of%20deep%20trees%2C%20each%20on%20a%20bootstrap%20resample%20of%20the%20rows%2C%20then%20**averages**%20them.%20The%20trees%20are%20independent%2C%20so%20they%20can%20be%20grown%20in%20parallel.%20Breiman's%20original%20recipe%20also%20lets%20each%20split%20try%20only%20a%20random%20subset%20of%20the%20features.%20scikit-learn's%20%60RandomForestRegressor%60%20defaults%20to%20%60max_features%3D1.0%60%2C%20every%20feature%20at%20every%20split%2C%20so%20out%20of%20the%20box%20it%20is%20*bagged%20trees*%3B%20section%204.1%20tries%20both.%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s1_2_random_forest_many(heat%2C%20yield_loss)%3A%0A%20%20%20%20_forest%20%3D%20RandomForestRegressor(n_estimators%3D200%2C%20min_samples_leaf%3D5%2C%20random_state%3D0).fit(%0A%20%20%20%20%20%20%20%20heat%5B%3A%2C%20None%5D%2C%20yield_loss%0A%20%20%20%20)%0A%20%20%20%20print(%0A%20%20%20%20%20%20%20%20%22forest%20RMSE%20on%20its%20own%20training%20data%3A%22%2C%0A%20%20%20%20%20%20%20%20round(rmse(_forest.predict(heat%5B%3A%2C%20None%5D)%2C%20yield_loss)%2C%201)%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%20forest's%20error%20on%20its%20own%20training%20rows%20(129.4)%20is%20*below*%20the%20noise%20level%20(%CF%83%20%3D%20150)%3A%20it%20has%20fitted%20some%20of%20the%20noise%2C%20as%20any%20flexible%20model%20scored%20on%20the%20rows%20it%20learned%20from%20will.%20Only%20rows%20it%20never%20saw%20give%20an%20honest%20score.%0A%0A%20%20%20%20%23%23%201.3%20Gradient%20boosting%20from%20scratch%0A%0A%20%20%20%20Boosting%20grows%20trees%20**one%20after%20another**%2C%20and%20each%20new%20tree%20is%20fitted%20to%20the%20errors%20(residuals)%20of%20everything%20before%20it.%20A%20learning%20rate%20shrinks%20each%20tree's%20contribution%20so%20that%20no%20single%20tree%20dominates.%20With%20squared%20error%2C%20that's%20the%20whole%20algorithm%3A%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s1_3_gradient_boosting_scratch(heat%2C%20yield_loss)%3A%0A%20%20%20%20def%20boost(x%2C%20y%2C%20rounds%2C%20learning_rate%2C%20depth)%3A%0A%20%20%20%20%20%20%20%20prediction%20%3D%20np.zeros_like(%0A%20%20%20%20%20%20%20%20%20%20%20%20y%0A%20%20%20%20%20%20%20%20)%20%20%23%20start%20from%200%2C%20as%20production's%20joint%20booster%20does%20(base_score%200)%0A%20%20%20%20%20%20%20%20for%20_%20in%20range(rounds)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20residual%20%3D%20y%20-%20prediction%20%20%23%20what%20the%20ensemble%20still%20gets%20wrong%0A%20%20%20%20%20%20%20%20%20%20%20%20tree%20%3D%20DecisionTreeRegressor(max_depth%3Ddepth).fit(x%2C%20residual)%0A%20%20%20%20%20%20%20%20%20%20%20%20prediction%20%3D%20prediction%20%2B%20learning_rate%20*%20tree.predict(x)%0A%20%20%20%20%20%20%20%20return%20prediction%0A%0A%20%20%20%20for%20_rounds%20in%20(1%2C%2010%2C%20100)%3A%0A%20%20%20%20%20%20%20%20p%20%3D%20boost(heat%5B%3A%2C%20None%5D%2C%20yield_loss%2C%20rounds%3D_rounds%2C%20learning_rate%3D0.1%2C%20depth%3D2)%0A%20%20%20%20%20%20%20%20print(%0A%20%20%20%20%20%20%20%20%20%20%20%20f%22%7B_rounds%3A%3E3%7D%20rounds%20of%20depth-2%20trees%2C%20learning%20rate%200.1%3A%20RMSE%20%7Brmse(p%2C%20yield_loss)%3A7.1f%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%20After%201%20round%20the%20fit%20has%20only%20moved%20a%20tenth%20of%20the%20way%2C%20and%20after%2010%20it%20has%20the%20shape.%20After%20100%2C%20its%20error%20on%20the%20training%20data%20(about%20124)%20is%20*below*%20the%20noise%20level%20(%CF%83%20%3D%20150)%3A%20it%20has%20started%20to%20fit%20noise%20as%20well%20as%20signal.%20That's%20why%20production%20judges%20every%20model%20on%20seasons%20it%20didn't%20train%20on.%20XGBoost%20and%20LightGBM%20are%20industrial%20versions%20of%20this%20loop%3A%20histogram-based%20split%20finding%2C%20second-order%20(gradient%20*and*%20curvature)%20steps%2C%20regularised%20leaf%20values%2C%20and%20parallel%20C%2B%2B.%0A%0A%20%20%20%20%23%23%201.4%20XGBoost%20and%20LightGBM%2C%20as%20their%20quick%20starts%20show%20them%0A%0A%20%20%20%20XGBoost's%20%5BPython%20package%20introduction%5D(https%3A%2F%2Fxgboost.readthedocs.io%2Fen%2Frelease_3.2.0%2Fpython%2Fpython_intro.html)%20and%20LightGBM's%20%5BPython-package%20introduction%5D(https%3A%2F%2Flightgbm.readthedocs.io%2Fen%2Fv4.7.0%2FPython-Intro.html)%20both%20show%20the%20native%20interface%20production%20uses%3A%20put%20the%20data%20in%20the%20library's%20container%20(%60DMatrix%60%2C%20%60Dataset%60)%2C%20then%20call%20%60train%60.%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s1_4_xgboost_lightgbm_quick(heat%2C%20yield_loss)%3A%0A%20%20%20%20dtrain%20%3D%20xgboost.DMatrix(heat%5B%3A%2C%20None%5D%2C%20label%3Dyield_loss)%0A%20%20%20%20booster%20%3D%20xgboost.train(%7B%22max_depth%22%3A%202%2C%20%22eta%22%3A%200.1%2C%20%22nthread%22%3A%201%7D%2C%20dtrain%2C%20num_boost_round%3D100)%0A%20%20%20%20print(%0A%20%20%20%20%20%20%20%20%22xgboost%20%20RMSE%3A%22%2C%0A%20%20%20%20%20%20%20%20round(rmse(booster.predict(xgboost.DMatrix(heat%5B%3A%2C%20None%5D))%2C%20yield_loss)%2C%201)%2C%0A%20%20%20%20)%0A%0A%20%20%20%20lgb_booster%20%3D%20lightgbm.train(%0A%20%20%20%20%20%20%20%20%7B%22num_leaves%22%3A%204%2C%20%22learning_rate%22%3A%200.1%2C%20%22verbose%22%3A%20-1%7D%2C%0A%20%20%20%20%20%20%20%20lightgbm.Dataset(heat%5B%3A%2C%20None%5D%2C%20yield_loss)%2C%0A%20%20%20%20%20%20%20%20num_boost_round%3D100%2C%0A%20%20%20%20)%0A%20%20%20%20print(%22lightgbm%20RMSE%3A%22%2C%20round(rmse(lgb_booster.predict(heat%5B%3A%2C%20None%5D)%2C%20yield_loss)%2C%201))%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%20contract%2C%20two%20wrappers%0A%0A%20%20%20%20Every%20residual%20regressor%20satisfies%20this%20%60Protocol%60%3A%20a%20type%20that%20lists%20methods%2C%20which%20any%20class%20with%20those%20methods%20satisfies%20without%20inheriting%20from%20it.%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s2_1_contract_two_wrappers()%3A%0A%20%20%20%20from%20area_module.models.xgboost%20import%20XgBoost%0A%20%20%20%20from%20model_foundry.common.quantity_modules.regression%20import%20(%0A%20%20%20%20%20%20%20%20INTERCEPT%2C%0A%20%20%20%20%20%20%20%20FilledRegression%2C%0A%20%20%20%20%20%20%20%20Regressor%2C%0A%20%20%20%20)%0A%20%20%20%20from%20yield_module.models.regressors%20import%20LightGbm%2C%20PcaRidge%0A%0A%20%20%20%20show(Regressor)%0A%20%20%20%20show(XgBoost)%0A%20%20%20%20show(LightGbm)%0A%20%20%20%20return%20FilledRegression%2C%20INTERCEPT%2C%20LightGbm%2C%20PcaRidge%2C%20XgBoost%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%20two%20wrappers%20are%20the%20same%20shape%20line%20for%20line%2C%20with%20only%20the%20library's%20spellings%20changed%20(%60DMatrix%60%20against%20%60Dataset%60%2C%20%60pred_contribs%60%20against%20%60pred_contrib%60).%20The%20one%20behavioural%20difference%20is%20the%20tolerance%20on%20the%20sum%20check%20in%20%60contributions%60.%0A%0A%20%20%20%20%23%23%202.2%20Realistic%20synthetic%20county%20yields%0A%0A%20%20%20%20We%20build%2060%20counties%20over%2025%20seasons%20(2000%E2%80%932024).%20Each%20county%20has%20its%20own%20level%20and%20upward%20trend%2C%20and%20three%20weather%20effects%20that%20look%20like%20the%20corn%20recipe's%20features%3A%0A%0A%20%20%20%20-%20**extreme%20heat**%20(%60edd%60)%3A%20no%20damage%20below%2060%20degree%20days%2C%20then%20%E2%88%9218%20kg%2Fha%20for%20each%20degree%20day%20above%3B%0A%20%20%20%20-%20**rain**%20(%60pr%60%2C%20a%20z-score)%3A%20helps%20up%20to%20a%20point%2C%20then%20hurts%20(a%20bell%20shape)%3B%0A%20%20%20%20-%20**growing%20degree%20days**%20(%60gdd%60)%3A%20mildly%20positive.%0A%0A%20%20%20%20There's%20also%20an%20irrelevant%20%60dry%60%20column%2C%20and%20noise%20of%20%CF%83%20%3D%20400%20kg%2Fha.%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s2_2_realistic_synthetic_county()%3A%0A%20%20%20%20FEATURES%20%3D%20%5B%22edd%22%2C%20%22pr%22%2C%20%22gdd%22%2C%20%22dry%22%5D%0A%0A%20%20%20%20def%20make_counties(seed)%3A%0A%20%20%20%20%20%20%20%20%22%22%22Draw%2060%20counties%20over%202000-2024%20with%20a%20trend%2C%20three%20weather%20effects%20and%20noise.%22%22%22%0A%20%20%20%20%20%20%20%20rng%20%3D%20np.random.default_rng(seed)%0A%20%20%20%20%20%20%20%20rows%20%3D%20%5B%5D%0A%20%20%20%20%20%20%20%20for%20county%20in%20range(60)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20level%2C%20slope%20%3D%20rng.normal(10_000%2C%20900)%2C%20rng.normal(110%2C%2025)%0A%20%20%20%20%20%20%20%20%20%20%20%20for%20season%20in%20range(2000%2C%202025)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20gdd%2C%20pr%20%3D%20rng.normal(0%2C%201)%2C%20rng.normal(0%2C%201)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20edd%20%3D%20max(0.0%2C%20rng.gamma(2.0%2C%2025.0)%20%2B%2015%20*%20gdd)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20weather%20%3D%20-18.0%20*%20max(0.0%2C%20edd%20-%2060.0)%20%2B%20350%20*%20pr%20-%20220%20*%20pr**2%20%2B%20150%20*%20gdd%0A%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%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%22county%22%3A%20county%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%20season%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%22edd%22%3A%20edd%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%22pr%22%3A%20pr%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%22gdd%22%3A%20gdd%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%22dry%22%3A%20rng.normal(0%2C%201)%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%22yield_kg_ha%22%3A%20level%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%20slope%20*%20(season%20-%202000)%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%20weather%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%20rng.normal(0%2C%20400)%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%20frame%20%3D%20pd.DataFrame(rows)%0A%20%20%20%20%20%20%20%20frame%5BGEO_IDENTIFIER%5D%20%3D%20%5Bf%22ADM0%3AUSA%2FADM1%3AS%7Bc%20%25%206%7D%2FADM2%3AC%7Bc%3A03d%7D%22%20for%20c%20in%20frame.county%5D%0A%20%20%20%20%20%20%20%20frame%5BSEASON_YEAR%5D%20%3D%20frame.season%0A%20%20%20%20%20%20%20%20frame%5BINIT_DATE%5D%20%3D%20pd.Timestamp(%222000-09-01%22)%0A%20%20%20%20%20%20%20%20return%20frame%0A%0A%20%20%20%20counties%20%3D%20make_counties(7)%0A%20%20%20%20counties.describe().round(1).loc%5B%0A%20%20%20%20%20%20%20%20%5B%22mean%22%2C%20%22std%22%2C%20%22min%22%2C%20%22max%22%5D%2C%20%5B%22edd%22%2C%20%22pr%22%2C%20%22gdd%22%2C%20%22yield_kg_ha%22%5D%0A%20%20%20%20%5D%0A%20%20%20%20return%20FEATURES%2C%20counties%2C%20make_counties%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%20production%2C%20the%20trees%20never%20see%20raw%20yield%3A%20an%20%60Additive%60%20model%20fits%20a%20trend%20first%2C%20and%20the%20trees%20learn%20the%20**residual**.%20We%20stand%20in%20for%20the%20production%20trend%20with%20a%20straight%20line%20per%20county%2C%20fitted%20on%20the%20training%20seasons%20only.%20A%20hindcast%20must%20never%20let%20a%20test%20season%20shape%20the%20trend.%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s2_2_realistic_synthetic_county_2(counties)%3A%0A%20%20%20%20from%20model_foundry.common.quantity_modules.additive%20import%20Additive%0A%0A%20%20%20%20show(Additive.fit)%0A%0A%20%20%20%20def%20detrend(frame)%3A%0A%20%20%20%20%20%20%20%20%22%22%22Subtract%20a%20per-county%20line%20fitted%20on%202000-2019%20only%3B%20return%20the%20frame%20and%20its%20train%2Ftest%20split.%22%22%22%0A%20%20%20%20%20%20%20%20in_train%20%3D%20frame.season%20%3C%3D%202019%0A%20%20%20%20%20%20%20%20lines%20%3D%20(%0A%20%20%20%20%20%20%20%20%20%20%20%20frame%5Bin_train%5D%0A%20%20%20%20%20%20%20%20%20%20%20%20.groupby(%22county%22)%0A%20%20%20%20%20%20%20%20%20%20%20%20.apply(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20lambda%20g%3A%20pd.Series(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20np.polyfit(g.season%20-%202000%2C%20g.yield_kg_ha%2C%201)%2C%20index%3D%5B%22slope%22%2C%20%22intercept%22%5D%0A%20%20%20%20%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%20%20%20%20include_groups%3DFalse%2C%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%20%20%20%20%20frame%20%3D%20frame.drop(columns%3D%5B%22slope%22%2C%20%22intercept%22%2C%20%22residual%22%5D%2C%20errors%3D%22ignore%22).join(%0A%20%20%20%20%20%20%20%20%20%20%20%20lines%2C%20on%3D%22county%22%0A%20%20%20%20%20%20%20%20)%20%20%23%20re-runnable%0A%20%20%20%20%20%20%20%20frame%5B%22residual%22%5D%20%3D%20frame.yield_kg_ha%20-%20(%0A%20%20%20%20%20%20%20%20%20%20%20%20frame.intercept%20%2B%20frame.slope%20*%20(frame.season%20-%202000)%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20return%20frame%2C%20frame%5Bframe.season%20%3C%3D%202019%5D%2C%20frame%5Bframe.season%20%3E%202019%5D%0A%0A%20%20%20%20counties_1%2C%20train%2C%20test%20%3D%20detrend(counties)%0A%20%20%20%20print(f%22train%3A%20%7Blen(train)%7D%20county-seasons%20(2000%E2%80%932019)%20%7C%20test%3A%20%7Blen(test)%7D%20(2020%E2%80%932024)%22)%0A%20%20%20%20print(%0A%20%20%20%20%20%20%20%20f%22residual%20spread%3A%20%7Bcounties_1.residual.std()%3A.1f%7D%20kg%2Fha%3B%20predicting%200%20for%20the%20test%20seasons%20scores%20RMSE%20%7Brmse(0%2C%20test.residual)%3A.1f%7D%22%0A%20%20%20%20)%0A%20%20%20%20return%20counties_1%2C%20detrend%2C%20test%2C%20train%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%60FilledRegression%60%20around%20a%20production%20wrapper%0A%0A%20%20%20%20%60FilledRegression%60%20is%20the%20layer%20between%20the%20model%20chain%20and%20a%20wrapper.%20It%20fills%20missing%20values%20with%20training%20medians%2C%20pools%20rows%20to%20the%20fit%20level%2C%20and%20turns%20the%20wrapper's%20contributions%20into%20per-feature%20columns%20plus%20an%20intercept.%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s2_3_filledregression_around_production(%0A%20%20%20%20FEATURES%2C%0A%20%20%20%20FilledRegression%2C%0A%20%20%20%20INTERCEPT%2C%0A%20%20%20%20XgBoost%2C%0A%20%20%20%20mo%2C%0A%20%20%20%20test%2C%0A%20%20%20%20train%2C%0A)%3A%0A%20%20%20%20show(FilledRegression.fit)%0A%20%20%20%20show(FilledRegression.predict)%0A%0A%20%20%20%20def%20residual_model(regressor)%3A%0A%20%20%20%20%20%20%20%20return%20FilledRegression(%0A%20%20%20%20%20%20%20%20%20%20%20%20source%3D%22residual%22%2C%20features%3DFEATURES%2C%20regressor%3Dregressor%2C%20weight%3DNone%2C%20fit_level%3D2%0A%20%20%20%20%20%20%20%20)%0A%0A%20%20%20%20xgb_model%20%3D%20residual_model(%0A%20%20%20%20%20%20%20%20XgBoost(rounds%3D400%2C%20params%3D%7B%22max_depth%22%3A%204%2C%20%22eta%22%3A%200.05%2C%20%22seed%22%3A%2042%2C%20%22nthread%22%3A%201%7D)%0A%20%20%20%20).fit(train)%0A%20%20%20%20out%20%3D%20xgb_model.predict(test)%0A%20%20%20%20SHARES%20%3D%20%5Bf%22residual__predicted__%7Bf%7D%22%20for%20f%20in%20%5B*FEATURES%2C%20INTERCEPT%5D%5D%0A%20%20%20%20print(%0A%20%20%20%20%20%20%20%20%22XgBoost%20hindcast%20RMSE%20on%202020%E2%80%932024%3A%22%2C%0A%20%20%20%20%20%20%20%20round(rmse(out.residual__predicted%2C%20test.residual)%2C%201)%2C%0A%20%20%20%20)%0A%20%20%20%20intercept%20%3D%20out%5Bf%22residual__predicted__%7BINTERCEPT%7D%22%5D%0A%20%20%20%20bias%20%3D%20xgb_model.regressor.booster.predict(xgboost.DMatrix(test%5BFEATURES%5D)%2C%20pred_contribs%3DTrue)%5B%0A%20%20%20%20%20%20%20%20%3A%2C%20-1%0A%20%20%20%20%5D%0A%20%20%20%20print(f%22intercept%20column%2C%20every%20row%3A%20%7Bintercept.min()%3A.4f%7D%20to%20%7Bintercept.max()%3A.4f%7D%20kg%2Fha%22)%0A%20%20%20%20print(f%22XGBoost's%20own%20bias%20column%3A%20%20%20%7Bbias.min()%3A.4f%7D%20to%20%7Bbias.max()%3A.4f%7D%20kg%2Fha%22)%0A%20%20%20%20mo.output.append(out%5B%5B%22season%22%2C%20%22residual%22%2C%20%22residual__predicted%22%2C%20*SHARES%5D%5D.head().round(1))%0A%20%20%20%20return%20SHARES%2C%20out%2C%20residual_model%2C%20xgb_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%20The%20intercept%20is%20**the%20same%20on%20every%20row**%2C%20to%20float%20rounding%2C%20and%20it%20is%20XGBoost's%20own%20bias%20column%3A%20the%20model's%20base%20value%20(about%200%20here%2C%20for%20a%20reason%203.1b%20explains).%20A%20constant%20base%20value%20plus%20one%20share%20per%20feature%2C%20adding%20up%20to%20the%20prediction%2C%20is%20what%20TreeSHAP's%20additivity%20buys%20(see%20Part%203).%20Checking%20that%20the%20intercept%20plus%20the%20shares%20equals%20the%20prediction%20would%20prove%20nothing%20here%2C%20because%20%60FilledRegression.predict%60%20*defines*%20the%20intercept%20as%20the%20prediction%20minus%20the%20shares.%20The%20real%20test%20is%20inside%20the%20wrapper%3A%20%60contributions%60%20compares%20the%20shares'%20sum%20with%20the%20prediction%2C%20and%20refuses%20an%20objective%20that%20fails.%20Here%20it%20is%20refusing%20a%20logistic%20objective%2C%20whose%20shares%20explain%20log-odds%20rather%20than%20the%20probability%20it%20predicts%3A%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s2_3_filledregression_around_production_2(FEATURES%2C%20XgBoost%2C%20train)%3A%0A%20%20%20%20X%20%3D%20train%5BFEATURES%5D%0A%20%20%20%20logistic%20%3D%20XgBoost(rounds%3D20%2C%20params%3D%7B%22objective%22%3A%20%22reg%3Alogistic%22%2C%20%22nthread%22%3A%201%7D).fit(%0A%20%20%20%20%20%20%20%20X%2C%20(train.residual%20%3E%200).astype(float)%0A%20%20%20%20)%0A%20%20%20%20try%3A%0A%20%20%20%20%20%20%20%20logistic.contributions(X)%0A%20%20%20%20except%20ValueError%20as%20error%3A%0A%20%20%20%20%20%20%20%20print(%22ValueError%3A%22%2C%20error)%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.4%20The%20live%20area%20path%3A%20a%20custom%20objective%0A%0A%20%20%20%20Every%20area%20recipe%20at%20%60f69eef9%60%20is%20%60kind%3A%20joint%60%2C%20which%20trains%20one%20XGBoost%20booster%20on%20rows%20from%20every%20level%20(county%2C%20state%20and%20nation).%20Each%20row's%20weight%20goes%20into%20a%20**custom%20objective**%3A%20XGBoost%20needs%20only%20each%20row's%20gradient%20and%20curvature%20(hessian)%2C%20so%20you%20can%20supply%20your%20own.%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s2_4_live_area_path()%3A%0A%20%20%20%20from%20area_module.models.joint%20import%20(%0A%20%20%20%20%20%20%20%20LEVEL_WEIGHTS%2C%0A%20%20%20%20%20%20%20%20PARAMS%2C%0A%20%20%20%20%20%20%20%20ROUNDS%2C%0A%20%20%20%20%20%20%20%20MultiLevelBooster%2C%0A%20%20%20%20%20%20%20%20joint_objective%2C%0A%20%20%20%20)%0A%0A%20%20%20%20show(joint_objective)%0A%20%20%20%20print(%22PARAMS%20%3D%22%2C%20PARAMS)%0A%20%20%20%20print(%22ROUNDS%20%3D%22%2C%20ROUNDS%2C%20%22%7C%20LEVEL_WEIGHTS%20%3D%22%2C%20LEVEL_WEIGHTS)%0A%20%20%20%20show(MultiLevelBooster.fit)%0A%20%20%20%20return%20LEVEL_WEIGHTS%2C%20PARAMS%2C%20joint_objective%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%20For%20a%20weighted%20squared%20error%20%C2%BD%C2%B7c%C2%B7(p%20%E2%88%92%20y)%C2%B2%2C%20the%20gradient%20is%20c%C2%B7(p%20%E2%88%92%20y)%20and%20the%20curvature%20is%20c.%20XGBoost%20can%20also%20take%20per-row%20weights%20directly%2C%20in%20the%20%60DMatrix%60.%20On%20paper%20the%20two%20are%20the%20same%20loss.%20Are%20they%20the%20same%20model%3F%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s2_4_live_area_path_2(FEATURES%2C%20joint_objective%2C%20test%2C%20train)%3A%0A%20%20%20%20X_train%2C%20y_train%20%3D%20train%5BFEATURES%5D.to_numpy(np.float32)%2C%20train.residual.to_numpy()%0A%20%20%20%20weights%20%3D%20np.random.default_rng(1).uniform(0.2%2C%203.0%2C%20len(y_train))%0A%20%20%20%20d_test%2C%20y_test%20%3D%20xgboost.DMatrix(test%5BFEATURES%5D.to_numpy(np.float32))%2C%20test.residual.to_numpy()%0A%0A%20%20%20%20for%20_label%2C%20tree_method%2C%20w%2C%20extra%20in%20(%0A%20%20%20%20%20%20%20%20(%22hist%20(the%20default)%22%2C%20%22hist%22%2C%20weights%2C%20%7B%7D)%2C%0A%20%20%20%20%20%20%20%20(%22exact%22%2C%20%22exact%22%2C%20weights%2C%20%7B%7D)%2C%0A%20%20%20%20%20%20%20%20(%22approx%22%2C%20%22approx%22%2C%20weights%2C%20%7B%7D)%2C%0A%20%20%20%20%20%20%20%20(%22control%3A%20hist%2C%20max_bin%3D4096%22%2C%20%22hist%22%2C%20weights%2C%20%7B%22max_bin%22%3A%204096%7D)%2C%20%20%23%20more%20bins%20than%20rows%0A%20%20%20%20%20%20%20%20(%22control%3A%20hist%2C%20every%20weight%201%22%2C%20%22hist%22%2C%20np.ones_like(weights)%2C%20%7B%7D)%2C%0A%20%20%20%20)%3A%0A%20%20%20%20%20%20%20%20params%20%3D%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%22max_depth%22%3A%204%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22eta%22%3A%200.05%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22seed%22%3A%2042%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22nthread%22%3A%201%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22base_score%22%3A%200.0%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22tree_method%22%3A%20tree_method%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20**extra%2C%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20via_objective%20%3D%20xgboost.train(%0A%20%20%20%20%20%20%20%20%20%20%20%20params%2C%20xgboost.DMatrix(X_train%2C%20label%3Dy_train)%2C%20200%2C%20obj%3Djoint_objective(w)%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20via_dmatrix%20%3D%20xgboost.train(params%2C%20xgboost.DMatrix(X_train%2C%20label%3Dy_train%2C%20weight%3Dw)%2C%20200)%0A%20%20%20%20%20%20%20%20_a%2C%20_b%20%3D%20via_objective.predict(d_test)%2C%20via_dmatrix.predict(d_test)%0A%20%20%20%20%20%20%20%20_gap%20%3D%20np.abs(_a%20-%20_b)%0A%20%20%20%20%20%20%20%20print(%0A%20%20%20%20%20%20%20%20%20%20%20%20f%22%7B_label%3A30%7D%3A%20gap%20max%20%7B_gap.max()%3A.2e%7D%2C%20mean%20%7B_gap.mean()%3A.2e%7D%20kg%2Fha%20%7C%20%22%0A%20%20%20%20%20%20%20%20%20%20%20%20f%22test%20RMSE%20%7Brmse(_a%2C%20y_test)%3A.1f%7D%20(objective)%20against%20%7Brmse(_b%2C%20y_test)%3A.1f%7D%20(DMatrix)%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%20With%20%60tree_method%3D%22exact%22%60%2C%20which%20tries%20every%20possible%20split%2C%20the%20two%20agree%20to%20rounding%20error.%20With%20%60hist%60%2C%20the%20**default**%2C%20they%20don't.%20%60hist%60%20first%20bins%20each%20feature%20into%20at%20most%20%60max_bin%60%20(256%20by%20default)%20quantile%20bins%2C%20and%20only%20considers%20splits%20at%20the%20bin%20edges.%20When%20the%20weights%20are%20in%20the%20%60DMatrix%60%2C%20it%20places%20those%20edges%20with%20a%20*weighted*%20quantile%20sketch%3B%20when%20the%20weights%20only%20reach%20the%20gradient%2C%20it%20places%20them%20as%20if%20every%20row%20weighed%20the%20same.%0A%0A%20%20%20%20The%20two%20controls%20prove%20that%20the%20sketch%20is%20the%20cause.%20With%20more%20bins%20than%20rows%20(%60max_bin%3D4096%60)%2C%20every%20value%20is%20a%20candidate%20again%20and%20the%20gap%20shrinks%20to%20%60exact%60's%20rounding%20error.%20With%20every%20weight%201%2C%20the%20two%20sketches%20are%20the%20same%20and%20so%20are%20the%20models.%20%60approx%60%2C%20which%20re-sketches%20before%20every%20tree%20(see%20XGBoost's%20%5Btree%20methods%5D(https%3A%2F%2Fxgboost.readthedocs.io%2Fen%2Frelease_3.2.0%2Ftreemethod.html)%20page)%2C%20doesn't%20close%20the%20gap%20either%2C%20so%20switching%20tree%20method%20isn't%20the%20remedy.%0A%0A%20%20%20%20Read%20the%20size%20of%20the%20gap%20with%20care.%20The%20largest%20gap%20is%20one%20row%20in%20300%3B%20the%20mean%20gap%20is%20a%20tenth%20of%20it%2C%20and%20the%20two%20models'%20test%20RMSEs%20are%20almost%20the%20same.%20The%20gap%20measures%20%22a%20different%20set%20of%20candidate%20splits%20gives%20different%20trees%22%2C%20not%20that%20one%20way%20is%20worse.%0A%0A%20%20%20%20Production's%20joint%20booster%20passes%20its%20row%20weights%20only%20through%20the%20objective%2C%20so%20its%20bins%20are%20placed%20by%20row%20count.%20Which%20rows%20would%20a%20weighted%20sketch%20favour%20instead%3F%20That%20depends%20on%20how%20much%20each%20level%20actually%20weighs%2C%20which%20the%20next%20section%20measures.%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.5%20What%20a%20row%20weighs%2C%20in%20hessian%20units%0A%0A%20%20%20%20XGBoost%20doesn't%20fit%20each%20tree%20to%20the%20residuals%20directly.%20For%20every%20row%20it%20takes%20the%20gradient%20*g*%20(the%20slope%20of%20the%20loss)%20and%20the%20hessian%20*h*%20(its%20curvature).%20For%20a%20candidate%20leaf%20it%20sums%20them%20over%20the%20leaf's%20rows%2C%20*G*%20%3D%20%CE%A3*g*%20and%20*H*%20%3D%20%CE%A3*h*%2C%20and%20then%3A%0A%0A%20%20%20%20-%20the%20leaf's%20value%20is%20*w*%20%3D%20%E2%88%92*G*%20%2F%20(*H*%20%2B%20%CE%BB)%3A%20a%20Newton%20step%2C%20shrunk%20by%20the%20L2%20penalty%20%CE%BB%20(%60lambda%60)%3B%0A%20%20%20%20-%20a%20split%20is%20kept%20only%20if%20its%20**gain**%2C%20%C2%BD%20%5B*G*_L%C2%B2%20%2F%20(*H*_L%20%2B%20%CE%BB)%20%2B%20*G*_R%C2%B2%20%2F%20(*H*_R%20%2B%20%CE%BB)%20%E2%88%92%20*G*%C2%B2%20%2F%20(*H*%20%2B%20%CE%BB)%5D%20%E2%88%92%20%CE%B3%2C%20is%20positive%2C%20where%20L%20and%20R%20are%20the%20two%20children%20and%20%CE%B3%20is%20the%20cost%20of%20a%20split%20(%60gamma%60%2C%200%20by%20default)%3B%0A%20%20%20%20-%20%60min_child_weight%60%20refuses%20any%20leaf%20whose%20*H*%20is%20below%20it.%0A%0A%20%20%20%20For%20plain%20squared%20error%20every%20*h*%20is%201%2C%20so%20*H*%20is%20the%20leaf's%20row%20count%20and%20*w*%20is%20its%20mean%20residual%2C%20shrunk%20a%20little%3A%20the%20from-scratch%20loop%20of%201.3.%20For%20the%20joint%20objective%2C%20a%20row's%20*h*%20is%20its%20loss%20coefficient%20*c*%2C%20so%20*H*%20is%20the%20currency%20all%20three%20rules%20are%20paid%20in.%20A%20leaf%20whose%20*H*%20is%20small%20next%20to%20%CE%BB%20keeps%20only%20*H*%20%2F%20(*H*%20%2B%20%CE%BB)%20of%20its%20Newton%20step.%0A%0A%20%20%20%20How%20big%20is%20*H*%20at%20each%20level%3F%20The%20cell%20below%20builds%20a%20toy%20hierarchy%2C%2012%20states%20of%2080%20counties%20over%2020%20seasons%20with%2026%20weekly%20init%20dates%2C%20and%20runs%20model-foundry's%20own%20%60loss_coefficients%60%20on%20it.%20County%20areas%20are%20lognormal%2C%20so%20most%20counties%20are%20small%20and%20a%20few%20very%20large%2C%20as%20in%20real%20data.%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s2_5_row_weighs_hessian(LEVEL_WEIGHTS%2C%20PARAMS)%3A%0A%20%20%20%20from%20area_module.models.joint%20import%20PLANTED_AREA%2C%20TARGET%0A%0A%20%20%20%20N_STATES%2C%20COUNTIES_PER_STATE%2C%20N_SEASONS%2C%20N_INITS%20%3D%2012%2C%2080%2C%2020%2C%2026%0A%20%20%20%20_rng%20%3D%20np.random.default_rng(0)%0A%20%20%20%20county_area%20%3D%20_rng.lognormal(np.log(15_000)%2C%201.3%2C%20N_STATES%20*%20COUNTIES_PER_STATE)%20%20%23%20ha%0A%20%20%20%20rows%20%3D%20%5B%5D%0A%20%20%20%20for%20season%20in%20range(2000%2C%202000%20%2B%20N_SEASONS)%3A%0A%20%20%20%20%20%20%20%20areas%20%3D%20county_area%20*%20_rng.lognormal(%0A%20%20%20%20%20%20%20%20%20%20%20%200%2C%200.05%2C%20county_area.size%0A%20%20%20%20%20%20%20%20)%20%20%23%20a%20little%20change%20each%20season%0A%20%20%20%20%20%20%20%20for%20s%20in%20range(N_STATES)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20in_state%20%3D%20range(s%20*%20COUNTIES_PER_STATE%2C%20(s%20%2B%201)%20*%20COUNTIES_PER_STATE)%0A%20%20%20%20%20%20%20%20%20%20%20%20rows%20%2B%3D%20%5B(f%22ADM0%3AUSA%2FADM1%3AS%7Bs%3A02d%7D%2FADM2%3AC%7Bc%3A04d%7D%22%2C%20season%2C%20areas%5Bc%5D)%20for%20c%20in%20in_state%5D%0A%20%20%20%20%20%20%20%20%20%20%20%20rows.append((f%22ADM0%3AUSA%2FADM1%3AS%7Bs%3A02d%7D%22%2C%20season%2C%20areas%5Bin_state%5D.sum()))%0A%20%20%20%20%20%20%20%20rows.append((%22ADM0%3AUSA%22%2C%20season%2C%20areas.sum()))%0A%20%20%20%20labels%20%3D%20pd.DataFrame(rows%2C%20columns%3D%5BGEO_IDENTIFIER%2C%20SEASON_YEAR%2C%20OBSERVED%5D)%0A%20%20%20%20hierarchy%20%3D%20labels.loc%5Blabels.index.repeat(N_INITS)%5D.reset_index(%0A%20%20%20%20%20%20%20%20drop%3DTrue%0A%20%20%20%20)%20%20%23%20one%20row%20per%20init%20date%0A%20%20%20%20hierarchy%5BINIT_DATE%5D%20%3D%20np.tile(%0A%20%20%20%20%20%20%20%20pd.date_range(%222020-04-01%22%2C%20periods%3DN_INITS%2C%20freq%3D%227D%22)%2C%20len(labels)%0A%20%20%20%20)%0A%20%20%20%20hierarchy%5BTARGET%5D%20%3D%20%22planted%22%0A%20%20%20%20hierarchy%5BPLANTED_AREA%5D%20%3D%20hierarchy%5BOBSERVED%5D%0A%0A%20%20%20%20coefficient%20%3D%20loss_coefficients(%0A%20%20%20%20%20%20%20%20hierarchy%2C%20LEVEL_WEIGHTS%0A%20%20%20%20)%20%20%23%20each%20row's%20hessian%20in%20joint_objective%0A%20%20%20%20level%20%3D%20row_levels(hierarchy).to_numpy()%0A%20%20%20%20_by_level%20%3D%20%5B%5D%0A%20%20%20%20for%20code%2C%20_name%20in%20((2%2C%20%22county%22)%2C%20(1%2C%20%22state%22)%2C%20(0%2C%20%22nation%22))%3A%0A%20%20%20%20%20%20%20%20c%20%3D%20coefficient%5Blevel%20%3D%3D%20code%5D%0A%20%20%20%20%20%20%20%20_by_level.append(%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%22level%22%3A%20_name%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22level%20weight%22%3A%20LEVEL_WEIGHTS%5Bcode%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22rows%22%3A%20len(c)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22median%20h%20per%20row%22%3A%20f%22%7Bnp.median(c)%3A.2g%7D%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22total%20H%22%3A%20f%22%7Bc.sum()%3A.4g%7D%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22H%20%2F%20(H%20%2B%20%CE%BB)%22%3A%20f%22%7Bc.sum()%20%2F%20(c.sum()%20%2B%20PARAMS%5B'lambda'%5D)%3A.3f%7D%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20per_county_season%20%3D%20coefficient%5Blevel%20%3D%3D%202%5D%20*%20N_INITS%20%20%23%20a%20county-season's%20rows%20summed%0A%20%20%20%20print(%0A%20%20%20%20%20%20%20%20%22a%20county-season's%20H%2C%2010th%2F50th%2F90th%2F99th%20percentiles%3A%22%2C%0A%20%20%20%20%20%20%20%20np.quantile(per_county_season%2C%20%5B0.1%2C%200.5%2C%200.9%2C%200.99%5D).round(2)%2C%0A%20%20%20%20)%0A%20%20%20%20print(%0A%20%20%20%20%20%20%20%20f%22one%20state-season's%20H%3A%20%7Bcoefficient%5Blevel%20%3D%3D%201%5D.sum()%20%2F%20(N_STATES%20*%20N_SEASONS)%3A.4f%7D%3B%20%22%0A%20%20%20%20%20%20%20%20f%22all%20%7B(level%20%3D%3D%201).sum()%3A%2C%7D%20state%20rows%20together%3A%20%7Bcoefficient%5Blevel%20%3D%3D%201%5D.sum()%3A.3f%7D%3B%20%22%0A%20%20%20%20%20%20%20%20f%22min_child_weight%3A%20%7BPARAMS%5B'min_child_weight'%5D%3Ag%7D%22%0A%20%20%20%20)%0A%20%20%20%20pd.DataFrame(_by_level).set_index(%22level%22)%0A%20%20%20%20return%20(hierarchy%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%20Can%20the%20booster%20learn%20something%20that%20only%20the%20states%20show%3F%20Give%20every%20state%20row%20a%20residual%20of%20%2B5%20%25%20of%20its%20area%2C%20every%20other%20row%20noise%20of%20about%202%20%25%2C%20and%20one%20feature%20that%20tells%20a%20state%20from%20a%20county%20(log%20area%2C%20standing%20in%20for%20the%20size-related%20features%20a%20real%20design%20matrix%20carries).%20One%20init%20date%20per%20season%20keeps%20the%20fit%20quick.%20The%20per-level%20totals%20don't%20change%2C%20because%20%60row_weights%60%20divides%20each%20label's%20weight%20by%20its%20number%20of%20init%20dates.%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s2_5_row_weighs_hessian_2(%0A%20%20%20%20LEVEL_WEIGHTS%2C%0A%20%20%20%20PARAMS%2C%0A%20%20%20%20hierarchy%2C%0A%20%20%20%20joint_objective%2C%0A)%3A%0A%20%20%20%20one_date%20%3D%20hierarchy%5Bhierarchy%5BINIT_DATE%5D%20%3D%3D%20hierarchy%5BINIT_DATE%5D.min()%5D.reset_index(drop%3DTrue)%0A%20%20%20%20c_one%20%3D%20loss_coefficients(one_date%2C%20LEVEL_WEIGHTS)%0A%20%20%20%20is_state%20%3D%20(row_levels(one_date)%20%3D%3D%201).to_numpy()%0A%20%20%20%20area%20%3D%20one_date%5BOBSERVED%5D.to_numpy()%0A%20%20%20%20_rng%20%3D%20np.random.default_rng(1)%0A%20%20%20%20residual%20%3D%20np.where(is_state%2C%200.05%2C%200.0)%20*%20area%20%2B%20_rng.normal(0%2C%200.02%2C%20len(area))%20*%20area%0A%20%20%20%20design%20%3D%20np.c_%5Bnp.log(area)%2C%20_rng.normal(size%3Dlen(area))%5D.astype(np.float32)%0A%20%20%20%20print(f%22total%20H%20of%20the%20state%20rows%3A%20%7Bc_one%5Bis_state%5D.sum()%3A.3f%7D%20(as%20above)%22)%0A%20%20%20%20print(%0A%20%20%20%20%20%20%20%20f%22state%20rows'%20mean%20relative%20error%20before%20boosting%3A%20%7Bnp.mean(np.abs(residual%5Bis_state%5D)%20%2F%20area%5Bis_state%5D)%3A.3f%7D%22%0A%20%20%20%20)%0A%20%20%20%20for%20floor%2C%20lam%20in%20(%0A%20%20%20%20%20%20%20%20(PARAMS%5B%22min_child_weight%22%5D%2C%20PARAMS%5B%22lambda%22%5D)%2C%0A%20%20%20%20%20%20%20%20(0.0%2C%20PARAMS%5B%22lambda%22%5D)%2C%0A%20%20%20%20%20%20%20%20(0.0%2C%200.0)%2C%0A%20%20%20%20)%3A%0A%20%20%20%20%20%20%20%20settings%20%3D%20%7B**PARAMS%2C%20%22min_child_weight%22%3A%20floor%2C%20%22lambda%22%3A%20lam%2C%20%22nthread%22%3A%201%7D%0A%20%20%20%20%20%20%20%20toy%20%3D%20xgboost.train(%0A%20%20%20%20%20%20%20%20%20%20%20%20settings%2C%20xgboost.DMatrix(design%2C%20label%3Dresidual)%2C%20200%2C%20obj%3Djoint_objective(c_one)%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20fitted%20%3D%20toy.predict(xgboost.DMatrix(design))%0A%20%20%20%20%20%20%20%20error%20%3D%20np.mean(np.abs(residual%5Bis_state%5D%20-%20fitted%5Bis_state%5D)%20%2F%20area%5Bis_state%5D)%0A%20%20%20%20%20%20%20%20print(f%22min_child_weight%20%7Bfloor%3A%3E4g%7D%2C%20lambda%20%7Blam%3Ag%7D%3A%20after%20200%20rounds%20%7Berror%3A.3f%7D%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%20Read%20the%20table%20as%20XGBoost%20does.%20The%20level%20weights%20say%20a%20state%20row%20counts%2025%20times%20a%20county%20row.%20But%20the%20full%20coefficient%20also%20multiplies%20by%20(mean%20county%20area%20%C3%B7%20the%20row's%20own%20area)%C2%B2%2C%20and%20a%20state%20here%20is%2080%20counties'%20area%2C%20so%20a%20state%20row%20carries%20roughly%2025%20%C3%B7%2080%C2%B2%20%E2%89%88%201%2F256%20of%20what%20a%20mean-sized%20county%20row%20carries.%20The%201%20%3A%2025%20%3A%20250%20weights%20act%20on%20*relative*%20error%3B%20in%20hessian%20units%20the%20states%20and%20the%20nation%20are%20**far%20lighter**%20than%20the%20counties%2C%20not%20heavier.%0A%0A%20%20%20%20That%20has%20three%20consequences%2C%20all%20in%20the%20formulas%20above%3A%0A%0A%20%20%20%20-%20**%60min_child_weight%60%20bars%20any%20state-only%20or%20nation-only%20leaf.**%20All%20state%20rows%20together%20hold%20an%20*H*%20of%20about%201%2C%20against%20a%20floor%20of%2020.%20State%20rows%20can%20only%20sit%20in%20leaves%20that%20county%20rows%20dominate.%0A%20%20%20%20-%20**%CE%BB%20shrinks%20what%20little%20they%20could%20learn.**%20With%20%CE%BB%20%3D%201%2C%20a%20leaf%20holding%20every%20state%20row%20would%20keep%20only%20about%20half%20of%20its%20Newton%20step%3B%20one%20holding%20every%20national%20row%2C%20about%200.5%20%25.%0A%20%20%20%20-%20**The%20study%20question%20of%202.4%20points%20the%20other%20way.**%20A%20sketch%20weighted%20by%20these%20coefficients%20would%20give%20state%20and%20national%20rows%20*fewer*%20bins%20than%20today's%20row-count%20sketch%2C%20not%20more.%0A%0A%20%20%20%20The%20training%20cell%20shows%20the%20first%20two%20at%20work%3A%20with%20production's%20floor%20the%20state%20rows'%20error%20barely%20moves%3B%20with%20the%20floor%20at%200%20the%20booster%20learns%20most%20of%20their%20%2B5%20%25%2C%20and%20with%20%CE%BB%20at%200%20slightly%20more.%20This%20toy%20has%20one%20target%20and%20invented%20areas%2C%20so%20it%20shows%20the%20mechanism%2C%20not%20the%20size%20of%20the%20effect%20on%20a%20real%20recipe.%20This%20is%20finding%20F6%20(%5Bissue%2033%5D(https%3A%2F%2Fgithub.com%2Ftreefera%2Ftf-model-lab%2Fissues%2F33))%2C%20and%20%5Bscience.md%5D(https%3A%2F%2Fgithub.com%2Ftreefera%2Ftf-model-lab%2Fblob%2Fmain%2Facademy%2F3-model-families%2F3.1-gradient-boosted-trees%2Fscience.md)'s%20study%20question%20asks%20how%20to%20test%20it%20on%20real%20rows.%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.6%20Hierarchies%3A%20yields%20roll%20up%20from%20the%20counties%0A%0A%20%20%20%20The%20boosters%20predict%20every%20level%2C%20but%20nothing%20in%20a%20booster%20makes%20counties%20add%20up%20to%20their%20state%20or%20states%20to%20their%20nation.%20Production%20restores%20that%20afterwards%2C%20in%20opposite%20directions%20for%20the%20two%20quantities%3A%0A%0A%20%20%20%20%60%60%60%0A%20%20%20%20yield%20%20(bottom-up)%20%20%20counties%20%E2%94%80%E2%94%80area-weighted%20mean%E2%94%80%E2%94%80%E2%96%B6%20states%20%E2%94%80%E2%94%80%E2%96%B6%20nation%0A%20%20%20%20area%20%20%20(top-down)%20%20%20%20nation%20%E2%94%80%E2%94%80gap%20shared%20out%E2%94%80%E2%94%80%E2%96%B6%20states%20%E2%94%80%E2%94%80scaled%E2%94%80%E2%94%80%E2%96%B6%20counties%0A%20%20%20%20%60%60%60%0A%0A%20%20%20%20Yield%20is%20production%20per%20unit%20area%2C%20so%20a%20state's%20yield%20is%20its%20counties'%20yields%20weighted%20by%20harvested%20area%20(%5B%60weighted_mean%60%2C%20aggregate.py%20L104-108%5D(https%3A%2F%2Fgithub.com%2Ftreefera%2Fmodel-foundry%2Fblob%2Ff69eef9df9e6a707c68f0292d940ccfc082cdaf5%2Fmodel_foundry%2Fcommon%2Fquantity_modules%2Faggregate.py%23L104-L108)%2C%20called%20by%20the%20yield%20chain's%20%60Aggregate%60%2C%20%5Bchain.py%20L60-66%5D(https%3A%2F%2Fgithub.com%2Ftreefera%2Fmodel-foundry%2Fblob%2Ff69eef9df9e6a707c68f0292d940ccfc082cdaf5%2Fmodel_foundry%2Fmodels%2Fyield_module%2Fsrc%2Fyield_module%2Fchain.py%23L60-L66))%3A%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s2_6_hierarchies_yields_roll()%3A%0A%20%20%20%20from%20model_foundry.common.quantity_modules.aggregate%20import%20weighted_mean%0A%0A%20%20%20%20ia_il%20%3D%20pd.DataFrame(%0A%20%20%20%20%20%20%20%20%7B%22state%22%3A%20%5B%22IA%22%2C%20%22IA%22%2C%20%22IL%22%5D%2C%20%22yield%22%3A%20%5B12.5%2C%2010.0%2C%2011.5%5D%2C%20%22harvested_mha%22%3A%20%5B3.0%2C%201.0%2C%204.0%5D%7D%0A%20%20%20%20)%0A%20%20%20%20by_state%20%3D%20weighted_mean(ia_il%5B%22yield%22%5D%2C%20ia_il%5B%22harvested_mha%22%5D%2C%20%5Bia_il%5B%22state%22%5D%5D)%0A%20%20%20%20print(%22area-weighted%20state%20yields%3A%22%2C%20by_state.round(3).to_dict())%0A%20%20%20%20print(%22plain%20mean%20for%20IA%3A%22%2C%20ia_il.query(%22state%20%3D%3D%20'IA'%22)%5B%22yield%22%5D.mean())%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%203%20Mha%20county%20pulls%20Iowa%20towards%20its%2012.5%3B%20a%20plain%20mean%20would%20let%20the%201%20Mha%20county%20count%20as%20much.%0A%0A%20%20%20%20The%20modelled%20counties%20don't%20cover%20all%20of%20a%20state%3A%20small%20ones%20are%20left%20out.%20%60BottomUpParents%60%20rebuilds%20each%20state%20and%20the%20nation%20over%20their%20**whole**%20harvested%20area%2C%20valuing%20the%20left-out%20area%20at%20k%20times%20the%20parent's%20own%20sample%20yield.%20US%20corn%20fits%20k%20within%20%5B0.5%2C%201.2%5D%3B%20every%20other%20recipe%20keeps%20k%20%3D%201.%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s2_6_hierarchies_yields_roll_2()%3A%0A%20%20%20%20show(built_from_children)%0A%20%20%20%20yield_tree%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%20GEO_IDENTIFIER%3A%20%5B%22ADM0%3AUSA%22%2C%20%22ADM0%3AUSA%2FADM1%3AIA%22%2C%20%22ADM0%3AUSA%2FADM1%3AIL%22%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22yield%22%3A%20%5B11.0%2C%2012.0%2C%2011.5%5D%2C%20%20%23%20each%20level's%20own%20modelled%20(sample)%20yield%2C%20t%2Fha%0A%20%20%20%20%20%20%20%20%20%20%20%20%22sample_mha%22%3A%20%5Bnp.nan%2C%205.0%2C%204.0%5D%2C%20%20%23%20harvested%20area%20of%20each%20state's%20modelled%20counties%0A%20%20%20%20%20%20%20%20%20%20%20%20%22whole_state_mha%22%3A%20%5Bnp.nan%2C%205.4%2C%204.6%5D%2C%20%20%23%20each%20state's%20whole%20harvested%20area%0A%20%20%20%20%20%20%20%20%20%20%20%20%22whole_nation_mha%22%3A%20%5B12.0%2C%20np.nan%2C%20np.nan%5D%2C%20%20%23%20the%20nation's%20whole%20harvested%20area%0A%20%20%20%20%20%20%20%20%20%20%20%20SEASON_YEAR%3A%202024%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20INIT_DATE%3A%20pd.Timestamp(%222024-08-01%22)%2C%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20)%0A%20%20%20%20whole_areas%20%3D%20%7B1%3A%20%22whole_state_mha%22%2C%200%3A%20%22whole_nation_mha%22%7D%0A%20%20%20%20for%20k%20in%20(0.0%2C%200.5%2C%201.0)%3A%0A%20%20%20%20%20%20%20%20rebuilt%20%3D%20built_from_children(yield_tree%2C%20%22yield%22%2C%20%22sample_mha%22%2C%20whole_areas%2C%20k)%0A%20%20%20%20%20%20%20%20print(f%22k%20%3D%20%7Bk%7D%3A%20nation%2C%20IA%2C%20IL%20%3D%22%2C%20rebuilt.round(3).tolist())%0A%20%20%20%20at_0%2C%20at_1%20%3D%20(%0A%20%20%20%20%20%20%20%20built_from_children(yield_tree%2C%20%22yield%22%2C%20%22sample_mha%22%2C%20whole_areas%2C%20k)%20for%20k%20in%20(0.0%2C%201.0)%0A%20%20%20%20)%0A%20%20%20%20at_07%20%3D%20built_from_children(yield_tree%2C%20%22yield%22%2C%20%22sample_mha%22%2C%20whole_areas%2C%200.7)%0A%20%20%20%20print(%22linear%20in%20k%3A%22%2C%20np.allclose(at_07%2C%20at_0%20%2B%200.7%20*%20(at_1%20-%20at_0)))%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%20With%20k%20%3D%201%20the%20states%20come%20back%20at%20their%20sample%20yields%20(the%20left-out%20area%20yields%20the%20same)%2C%20and%20the%20nation%20is%20its%20states'%20production%20plus%20its%20own%20left-out%202%20Mha%20at%2011.0%2C%20over%2012%20Mha.%20Each%20rebuilt%20yield%20is%20%60a%20%2B%20b%C2%B7k%60%2C%20so%20production%20evaluates%20it%20at%20k%20%3D%200%20and%20k%20%3D%201%20once%2C%20and%20gets%20every%20k%20on%20its%202%2C001-point%20grid%20from%20those%20two%20(%5Bbottom_up.py%20L94-106%5D(https%3A%2F%2Fgithub.com%2Ftreefera%2Fmodel-foundry%2Fblob%2Ff69eef9df9e6a707c68f0292d940ccfc082cdaf5%2Fmodel_foundry%2Fmodels%2Fyield_module%2Fsrc%2Fyield_module%2Fmodels%2Fbottom_up.py%23L94-L106)).%0A%0A%20%20%20%20%23%23%202.7%20Hierarchies%3A%20area%20shares%20the%20national%20gap%20among%20the%20states%0A%0A%20%20%20%20Area%20is%20additive%2C%20and%20area%20recipes%20reconcile%20**top-down**%3A%20the%20nation's%20forecast%20stands%2C%20and%20the%20gap%20between%20it%20and%20its%20states'%20sum%20is%20shared%20among%20the%20states%2C%20whose%20counties%20are%20then%20scaled%20to%20them.%20Three%20states%2C%2018.7%20Mha%20between%20them%2C%20against%20a%20national%20forecast%20of%2020.0%2C%20shared%20by%20each%20state's%20own%20size%20(%60level%60%2C%20as%20the%20US%20recipes%20do)%3A%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s2_7_hierarchies_area_shares()%3A%0A%20%20%20%20NATION%20%3D%20%22ADM0%3ABRA%22%0A%20%20%20%20STATE_MHA%20%3D%20%7B%22big%22%3A%2012.0%2C%20%22mid%22%3A%206.0%2C%20%22small%22%3A%200.7%7D%0A%20%20%20%20hier_rows%20%3D%20%5B%7BGEO_IDENTIFIER%3A%20NATION%2C%20%22planted%22%3A%2020.0%7D%5D%0A%20%20%20%20for%20_name%2C%20mha%20in%20STATE_MHA.items()%3A%0A%20%20%20%20%20%20%20%20hier_rows.append(%7BGEO_IDENTIFIER%3A%20f%22%7BNATION%7D%2FADM1%3A%7B_name%7D%22%2C%20%22planted%22%3A%20mha%7D)%0A%20%20%20%20%20%20%20%20for%20i%2C%20share%20in%20enumerate((0.6%2C%200.4))%3A%20%20%23%20two%20counties%20per%20state%0A%20%20%20%20%20%20%20%20%20%20%20%20hier_rows.append(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7BGEO_IDENTIFIER%3A%20f%22%7BNATION%7D%2FADM1%3A%7B_name%7D%2FADM2%3Ac%7Bi%7D%22%2C%20%22planted%22%3A%20mha%20*%20share%7D%0A%20%20%20%20%20%20%20%20%20%20%20%20)%0A%20%20%20%20area_tree%20%3D%20pd.DataFrame(hier_rows).assign(%0A%20%20%20%20%20%20%20%20**%7BSEASON_YEAR%3A%202024%2C%20INIT_DATE%3A%20pd.Timestamp(%222024-01-15%22)%7D%0A%20%20%20%20)%0A%20%20%20%20area_level%20%3D%20area_tree%5BGEO_IDENTIFIER%5D.str.count(%22%2FADM%22)%0A%20%20%20%20print(%0A%20%20%20%20%20%20%20%20f%22the%20states%20sum%20to%20%7Barea_tree.loc%5Barea_level%20%3D%3D%201%2C%20'planted'%5D.sum()%3A.1f%7D%20Mha%3B%20the%20nation%20says%20%7Barea_tree.loc%5Barea_level%20%3D%3D%200%2C%20'planted'%5D.item()%3A.1f%7D%22%0A%20%20%20%20)%0A%0A%20%20%20%20_by_level%20%3D%20reconcile_levels(area_tree%2C%20%22planted%22%2C%20None%2C%20%22level%22%2C%20%22proportional%22)%0A%20%20%20%20reconciled%20%3D%20area_tree.assign(reconciled%3D_by_level)%0A%20%20%20%20state_of%20%3D%20reconciled%5BGEO_IDENTIFIER%5D.str.split(%22%2F%22).str%5B%3A2%5D.str.join(%22%2F%22)%0A%20%20%20%20print(%0A%20%20%20%20%20%20%20%20reconciled.loc%5Barea_level%20%3D%3D%201%2C%20%5BGEO_IDENTIFIER%2C%20%22planted%22%2C%20%22reconciled%22%5D%5D%0A%20%20%20%20%20%20%20%20.round(3)%0A%20%20%20%20%20%20%20%20.to_string(index%3DFalse)%0A%20%20%20%20)%0A%20%20%20%20print(%22nation%20after%3A%22%2C%20round(float(_by_level%5Barea_level%20%3D%3D%200%5D.iloc%5B0%5D)%2C%206))%0A%20%20%20%20county_sums%20%3D%20reconciled%5Barea_level%20%3D%3D%202%5D.groupby(state_of%5Barea_level%20%3D%3D%202%5D)%5B%22reconciled%22%5D.sum()%0A%20%20%20%20print(%0A%20%20%20%20%20%20%20%20%22counties%20sum%20to%20their%20state%3A%22%2C%0A%20%20%20%20%20%20%20%20np.allclose(county_sums.to_numpy()%2C%20_by_level%5Barea_level%20%3D%3D%201%5D.to_numpy())%2C%0A%20%20%20%20)%0A%20%20%20%20return%20STATE_MHA%2C%20area_level%2C%20area_tree%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%20Every%20state%20grew%20by%20the%20same%207%20%25%2C%20and%20each%20state's%20counties%20were%20scaled%20with%20it%2C%20so%20all%20three%20levels%20now%20agree.%20science.md%20derives%20the%20general%20rule%3A%20if%20each%20state's%20error%20has%20variance%20v%E1%B5%A2%2C%20the%20least-squares%20share%20of%20the%20gap%20is%20v%E1%B5%A2%20%2F%20%CE%A3v.%20Part%203%20asks%20which%20variance%20production%20passes.%0A%0A%20%20%20%20%23%23%202.8%20Hierarchies%3A%20harvested%20at%20most%20planted%0A%0A%20%20%20%20Planted%20and%20harvested%20are%20forecast%20separately%2C%20so%20harvested%20can%20come%20out%20larger.%20Under%20%60fused_difference%60%20(US%20corn%2C%20cotton%20and%20winter%20wheat%20area)%20the%20two%20are%20moved%20towards%20each%20other%20until%20they%20meet%3A%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s2_8_hierarchies_harvested_most()%3A%0A%20%20%20%20show(project_abandonment)%0A%20%20%20%20planted_in%20%3D%20np.array(%5B100.0%2C%20100.0%2C%20100.0%5D)%0A%20%20%20%20harvested_in%20%3D%20np.array(%5B90.0%2C%20104.0%2C%20104.0%5D)%0A%20%20%20%20planted_var%20%3D%20np.array(%5B4.0%2C%204.0%2C%201.0%5D)%0A%20%20%20%20harvested_var%20%3D%20np.array(%5B4.0%2C%204.0%2C%209.0%5D)%0A%20%20%20%20planted_out%2C%20harvested_out%2C%20abandoned_out%20%3D%20project_abandonment(%0A%20%20%20%20%20%20%20%20planted_in%2C%20harvested_in%2C%20planted_var%2C%20harvested_var%2C%20None%2C%20None%2C%20%22fused_difference%22%0A%20%20%20%20)%0A%20%20%20%20print(%0A%20%20%20%20%20%20%20%20pd.DataFrame(%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%22planted%22%3A%20planted_in%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22harvested%22%3A%20harvested_in%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22var%20planted%22%3A%20planted_var%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22var%20harvested%22%3A%20harvested_var%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22planted%20out%22%3A%20planted_out%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22harvested%20out%22%3A%20harvested_out%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22abandoned%22%3A%20abandoned_out%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20).to_string(index%3DFalse)%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-%20**Row%201**%20is%20fine%3A%20harvested%20is%20below%20planted%2C%20so%20nothing%20moves%20and%2010%20hectares%20are%20abandoned.%0A%20%20%20%20-%20**Row%202**%20violates%20by%204%20with%20equal%20variances%2C%20so%20each%20moves%202%20and%20they%20meet%20at%20102.%0A%20%20%20%20-%20**Row%203**%20violates%20by%204%2C%20but%20harvested%20is%20far%20less%20certain%20(variance%209%20against%201)%2C%20so%20planted%20moves%20only%201%2F10%20of%20the%20gap%20and%20harvested%20the%20other%209%2F10%3A%20both%20end%20at%20100.4.%20Exercise%2010%20asks%20you%20to%20show%20this%20is%20the%20minimum-variance%20way%20to%20meet.%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%203%3A%20the%20science%0A%0A%20%20%20%20%23%23%203.1%20What%20did%20the%20trees%20learn%3F%0A%0A%20%20%20%20The%20mean%20absolute%20share%20per%20feature%20says%20which%20inputs%20move%20predictions%20most%3A%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s3_1_did_trees_learn(SHARES%2C%20out)%3A%0A%20%20%20%20importance%20%3D%20out%5BSHARES%5B%3A-1%5D%5D.abs().mean().rename(lambda%20c%3A%20c.split(%22__%22)%5B-1%5D).sort_values()%0A%20%20%20%20_ax%20%3D%20importance.plot.barh(%0A%20%20%20%20%20%20%20%20figsize%3D(7%2C%202.8)%2C%20title%3D%22mean%20%7CTreeSHAP%20share%7C%20on%20the%202020%E2%80%932024%20hindcast%20(kg%2Fha)%22%0A%20%20%20%20)%0A%20%20%20%20plt.show()%0A%20%20%20%20importance.round(1)%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%60dry%60%20is%20irrelevant%20in%20the%20generator%2C%20and%20it%20gets%20the%20smallest%20share.%20That%20share%20isn't%20zero%2C%20though%3A%20the%20trees%20found%20a%20little%20noise%20to%20split%20on.%20Now%20the%20shape%20of%20the%20heat%20effect.%20A%20**dependence%20plot**%20puts%20each%20row's%20%60edd%60%20share%20against%20its%20%60edd%60%20value%3A%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s3_1_did_trees_learn_2(out%2C%20train)%3A%0A%20%20%20%20def%20true_heat(edd)%3A%0A%20%20%20%20%20%20%20%20return%20-18.0%20*%20np.maximum(0.0%2C%20edd%20-%2060.0)%0A%0A%20%20%20%20_fig%2C%20_ax%20%3D%20plt.subplots(figsize%3D(8%2C%203.5))%0A%20%20%20%20_ax.scatter(%0A%20%20%20%20%20%20%20%20out.edd%2C%0A%20%20%20%20%20%20%20%20out.residual__predicted__edd%2C%0A%20%20%20%20%20%20%20%20s%3D10%2C%0A%20%20%20%20%20%20%20%20alpha%3D0.6%2C%0A%20%20%20%20%20%20%20%20label%3D%22TreeSHAP%20share%20of%20edd%20(kg%2Fha)%22%2C%0A%20%20%20%20)%0A%20%20%20%20_grid%20%3D%20np.linspace(0%2C%20out.edd.max()%2C%20200)%0A%20%20%20%20_ax.plot(%0A%20%20%20%20%20%20%20%20_grid%2C%0A%20%20%20%20%20%20%20%20true_heat(_grid)%20-%20true_heat(train.edd).mean()%2C%20%20%23%20the%20same%20reference%20as%20the%20base%20value%0A%20%20%20%20%20%20%20%20color%3D%22black%22%2C%0A%20%20%20%20%20%20%20%20lw%3D1%2C%0A%20%20%20%20%20%20%20%20label%3D%22the%20generator's%20true%20effect%20minus%20the%20training%20seasons'%20average%22%2C%0A%20%20%20%20)%0A%20%20%20%20_ax.axvline(60%2C%20ls%3D%22%3A%22%2C%20color%3D%22grey%22)%0A%20%20%20%20_ax.set(%0A%20%20%20%20%20%20%20%20xlabel%3D%22extreme%20heat%2C%20edd%20(degree%20days)%22%2C%0A%20%20%20%20%20%20%20%20ylabel%3D%22share%20of%20the%20prediction%20(kg%2Fha)%22%2C%0A%20%20%20%20%20%20%20%20title%3D%22Shares%20are%20flat%20below%2060%20and%20fall%20steeply%20above%20it%2C%20as%20the%20true%20effect%20does%22%2C%0A%20%20%20%20)%0A%20%20%20%20_ax.legend()%0A%20%20%20%20plt.show()%0A%0A%20%20%20%20BANDS%20%3D%20%5B(0%2C%2040)%2C%20(40%2C%2060)%2C%20(60%2C%2080)%2C%20(80%2C%20120)%2C%20(120%2C%20400)%5D%0A%0A%20%20%20%20for%20_lo%2C%20_hi%20in%20BANDS%3A%0A%20%20%20%20%20%20%20%20_band%20%3D%20out%5B(out.edd%20%3E%3D%20_lo)%20%26%20(out.edd%20%3C%20_hi)%5D%0A%20%20%20%20%20%20%20%20print(%0A%20%20%20%20%20%20%20%20%20%20%20%20f%22edd%20%7B_lo%3A%3E3%7D%E2%80%93%7B_hi%3A%3C3%7D%3A%20%7Blen(_band)%3A3d%7D%20rows%2C%20mean%20edd%20share%20%7B_band.residual__predicted__edd.mean()%3A8.1f%7D%20kg%2Fha%2C%22%0A%20%20%20%20%20%20%20%20%20%20%20%20f%22%20true%20heat%20effect%20%7Btrue_heat(_band.edd).mean()%3A8.1f%7D%20kg%2Fha%22%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20return%20BANDS%2C%20true_heat%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%20Nobody%20told%20the%20model%20about%20a%20threshold%20at%2060%2C%20yet%20its%20shares%20are%20flat%20below%20about%2060%20and%20fall%20steeply%20above%20it.%20That's%20the%20nonlinearity%20a%20linear%20model%20can't%20represent%20without%20someone%20hand-crafting%20the%20feature.%0A%0A%20%20%20%20**But%20why%20are%20the%20shares%20*positive*%20below%2060%2C%20where%20the%20true%20heat%20effect%20is%20zero%3F**%20It%20isn't%20a%20heat%20*benefit*.%20A%20TreeSHAP%20share%20measures%20how%20far%20a%20feature%20moves%20the%20prediction%20**away%20from%20the%20base%20value**%2C%20and%20for%20this%20model%2C%20which%20doesn't%20subsample%20rows%2C%20the%20base%20value%20is%20the%20average%20prediction%20over%20the%20training%20seasons%2C%20each%20row%20weighted%20by%20its%20hessian%20(for%20squared%20error%20every%20hessian%20is%201%2C%20so%20it%20is%20simply%20the%20average).%20A%20third%20of%20those%20seasons%20are%20hotter%20than%2060%20degree%20days%20and%20lose%20yield%20to%20heat%2C%20so%20the%20%22average%20season%22%20already%20carries%20the%20average%20heat%20damage.%20A%20cool%20season%20therefore%20sits%20*above*%20that%20average.%20Its%20positive%20share%20is%20consistent%20with%20%22less%20heat%20damage%20than%20an%20average%20year%22%2C%20not%20%22heat%20helped%22.%20It%20is%20still%20the%20**model's%20attribution**%2C%20not%20a%20measured%20effect%20of%20changing%20heat%20alone%3A%20in%20the%20generator%20%60edd%60%20partly%20moves%20with%20%60gdd%60%2C%20which%20also%20affects%20yield%2C%20so%20the%20two%20features%20share%20credit.%0A%0A%20%20%20%20%23%23%203.1b%20Reading%20the%20shares%20against%20the%20right%20baseline%0A%0A%20%20%20%20Compare%20the%20*pattern*%20of%20the%20shares%20with%20the%20generator's%20heat%20effect%20measured%20the%20same%20way%2C%20relative%20to%20the%20training%20seasons'%20average%20heat%20damage.%20Then%20re-centre%20the%20shares%20on%20a%20cool%20season%20instead%2C%20which%20is%20the%20comparison%20an%20agronomist%20usually%20means%3A%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s3_1b_reading_shares_right(%0A%20%20%20%20BANDS%2C%0A%20%20%20%20FEATURES%2C%0A%20%20%20%20INTERCEPT%2C%0A%20%20%20%20out%2C%0A%20%20%20%20train%2C%0A%20%20%20%20true_heat%2C%0A%20%20%20%20xgb_model%2C%0A)%3A%0A%20%20%20%20base_value%20%3D%20out%5B%0A%20%20%20%20%20%20%20%20f%22residual__predicted__%7BINTERCEPT%7D%22%0A%20%20%20%20%5D.mean()%20%20%23%20the%20TreeSHAP%20bias%2C%20recovered%20by%20FilledRegression%0A%20%20%20%20mean_training_prediction%20%3D%20xgb_model.regressor.predict(train%5BFEATURES%5D).mean()%0A%20%20%20%20average_damage%20%3D%20true_heat(train.edd).mean()%0A%20%20%20%20print(%0A%20%20%20%20%20%20%20%20f%22base%20value%20(intercept%20column)%3A%20%7Bbase_value%3A.1f%7D%20kg%2Fha%20%7C%20mean%20training%20prediction%3A%20%7Bmean_training_prediction%3A.1f%7D%20kg%2Fha%22%0A%20%20%20%20)%0A%20%20%20%20print(%0A%20%20%20%20%20%20%20%20f%22average%20true%20heat%20effect%20over%20the%20training%20seasons%3A%20%7Baverage_damage%3A.1f%7D%20kg%2Fha%20%22%0A%20%20%20%20%20%20%20%20f%22(%7B(train.edd%20%3E%2060).mean()%3A.0%25%7D%20of%20them%20are%20above%2060%20degree%20days)%22%0A%20%20%20%20)%0A%20%20%20%20print(%0A%20%20%20%20%20%20%20%20f%22correlation%20of%20edd%20with%20gdd%20over%20the%20training%20seasons%3A%20%7Bnp.corrcoef(train.edd%2C%20train.gdd)%5B0%2C%201%5D%3A.2f%7D%5Cn%22%0A%20%20%20%20)%0A%0A%20%20%20%20cool_share%20%3D%20out%5Bout.edd%20%3C%2040%5D.residual__predicted__edd.mean()%0A%20%20%20%20table%20%3D%20%5B%5D%0A%20%20%20%20for%20_lo%2C%20_hi%20in%20BANDS%3A%0A%20%20%20%20%20%20%20%20_band%20%3D%20out%5B(out.edd%20%3E%3D%20_lo)%20%26%20(out.edd%20%3C%20_hi)%5D%0A%20%20%20%20%20%20%20%20table.append(%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%22edd%20band%22%3A%20f%22%7B_lo%7D%E2%80%93%7B_hi%7D%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22rows%22%3A%20len(_band)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22mean%20share%22%3A%20_band.residual__predicted__edd.mean()%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22standard%20error%22%3A%20_band.residual__predicted__edd.std()%20%2F%20np.sqrt(len(_band))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22true%20effect%20%E2%88%92%20training%20average%22%3A%20(true_heat(_band.edd)%20-%20average_damage).mean()%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22share%20re-centred%20on%20a%20cool%20season%22%3A%20_band.residual__predicted__edd.mean()%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20-%20cool_share%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22true%20effect%22%3A%20true_heat(_band.edd).mean()%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20pd.DataFrame(table).round(1)%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%20base%20value%2C%20read%20from%20the%20intercept%20column%2C%20is%200%20here.%20Each%20county's%20trend%20line%20passes%20through%20its%20average%20training%20season%2C%20heat%20damage%20included%2C%20so%20the%20residuals%20the%20booster%20fits%20average%20zero%2C%20and%20so%20does%20its%20bias.%20Measured%20against%20that%20average%2C%20the%20shares%20follow%20the%20same%20pattern%20as%20the%20generator's%20heat%20effect%3A%20%2B176%20and%20%2B216%20below%20the%20threshold%20against%20a%20true%20%2B194%2C%20and%20%E2%88%92398%20and%20%E2%88%921%2C230%20in%20the%20hot%20bands%20against%20%E2%88%92430%20and%20%E2%88%921%2C378.%20Re-centred%20on%20a%20cool%20season%2C%20they%20are%20close%20to%20the%20true%20effect's%20shape%3A%200%2C%20%2B39%2C%20%E2%88%92207%2C%20%E2%88%92575%20and%20%E2%88%921%2C406%20against%200%2C%200%2C%20%E2%88%92159%2C%20%E2%88%92624%20and%20%E2%88%921%2C572.%20The%20departures%20are%20not%20the%20trees%20rounding%20the%20corner%20at%2060.%20Rounding%20would%20put%20the%2040%E2%80%9360%20band%20slightly%20*below*%20the%20cool%20band%20and%20make%2060%E2%80%9380%20*less*%20negative%20than%20the%20truth%2C%20and%20both%20go%20the%20other%20way.%20They%20come%20from%20two%20things.%20%60edd%60%20shares%20credit%20with%20%60gdd%60%2C%20which%20raises%20yield%20and%20moves%20with%20%60edd%60%20(correlation%200.37).%20And%20the%20bands%20are%20small%3A%2060%E2%80%9380%20holds%2026%20rows%2C%20so%20its%20mean%20share%20has%20a%20standard%20error%20of%2018%20kg%2Fha.%20In%20the%20first%20table%2C%2060%E2%80%9380's%20mean%20share%20(%E2%88%9231)%20even%20has%20the%20opposite%20sign%20to%20the%20true%20effect%20minus%20the%20training%20average%20(%2B35).%20**When%20you%20explain%20a%20share%20to%20a%20client%2C%20say%20what%20it's%20relative%20to.**%0A%0A%20%20%20%20%23%23%203.2%20What%20a%20missing%20feature%20does%20to%20the%20prediction%0A%0A%20%20%20%20%60FilledRegression.predict%60%20fills%20a%20missing%20value%20with%20the%20training%20median%2C%20asks%20the%20wrapper%20for%20shares%2C%20then%20**zeroes%20the%20missing%20feature's%20share**.%20Its%20output%20is%20then%20the%20intercept%20plus%20the%20remaining%20shares%2C%20which%20is%20not%20the%20wrapper's%20own%20prediction%20on%20the%20filled%20row.%20Note%20what%20zeroing%20does%20*not*%20do%3A%20the%20remaining%20shares%20were%20still%20computed%20on%20the%20median-filled%20row%2C%20so%20the%20median%20heat%20value%20can%20steer%20which%20branches%20the%20other%20features'%20shares%20come%20from.%20The%20result%20isn't%20a%20prediction%20independent%20of%20heat%3B%20it's%20the%20intercept%20plus%20the%20retained%20shares%20on%20the%20filled%20row%3A%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s3_2_missing_feature_prediction(FEATURES%2C%20test%2C%20xgb_model)%3A%0A%20%20%20%20row%20%3D%20test.iloc%5B%5B0%5D%5D.copy()%0A%20%20%20%20complete%20%3D%20xgb_model.predict(row)%0A%20%20%20%20row.loc%5B%3A%2C%20%22edd%22%5D%20%3D%20np.nan%0A%20%20%20%20gapped%20%3D%20xgb_model.predict(row)%0A%20%20%20%20filled%20%3D%20row%5BFEATURES%5D.fillna(xgb_model.medians)%0A%0A%20%20%20%20print(%0A%20%20%20%20%20%20%20%20f%22complete%20row%3A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20prediction%20%7Bcomplete.residual__predicted.iloc%5B0%5D%3A8.1f%7D%2C%20edd%20share%20%7Bcomplete.residual__predicted__edd.iloc%5B0%5D%3A7.1f%7D%22%0A%20%20%20%20)%0A%20%20%20%20print(%0A%20%20%20%20%20%20%20%20f%22edd%20missing%2C%20FilledRegression%3A%20%20%20prediction%20%7Bgapped.residual__predicted.iloc%5B0%5D%3A8.1f%7D%2C%20edd%20share%20%7Bgapped.residual__predicted__edd.iloc%5B0%5D%3A7.1f%7D%22%0A%20%20%20%20)%0A%20%20%20%20print(%0A%20%20%20%20%20%20%20%20f%22edd%20missing%2C%20wrapper%20on%20median%3A%20%20prediction%20%7Bxgb_model.regressor.predict(filled)%5B0%5D%3A8.1f%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%20Two%20defensible%20answers%20to%20%22what%20is%20the%20prediction%20when%20heat%20is%20unknown%3F%22%20differ%20by%20the%20median%20row's%20heat%20share.%20The%20production%20rule%20says%20that%20a%20missing%20feature%20contributes%20nothing%2C%20so%20the%20output%20is%20the%20intercept%20plus%20the%20other%20features'%20shares%2C%20computed%20on%20the%20median-filled%20row.%20Is%20that%20the%20right%20scientific%20choice%3F%20It's%20the%20study%20question%20in%20%5Bscience.md%5D(https%3A%2F%2Fgithub.com%2Ftreefera%2Ftf-model-lab%2Fblob%2Fmain%2Facademy%2F3-model-families%2F3.1-gradient-boosted-trees%2Fscience.md).%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.3%20Who%20absorbs%20the%20national%20gap%3F%20(F10)%0A%0A%20%20%20%20The%20%60variance%60%20allocation%20is%20the%20least-squares%20one%20*if*%20its%20weights%20are%20variances%20in%20hectares%C2%B2.%20Brazil%20soy%20uses%20it%20(it's%20the%20default)%2C%20and%20the%20column%20it%20passes%20is%20%60VintageFusion%60's%20%60__variance%60%2C%20built%20from%20%60rel_var%60%3A%20a%20**relative**%20variance%20(%5Bvintage_fusion.py%20L311-313%5D(https%3A%2F%2Fgithub.com%2Ftreefera%2Fmodel-foundry%2Fblob%2Ff69eef9df9e6a707c68f0292d940ccfc082cdaf5%2Fmodel_foundry%2Fmodels%2Farea_module%2Fsrc%2Farea_module%2Fmodels%2Fvintage_fusion.py%23L311-L313)).%20Give%20all%20three%20states%20the%20same%205%20%25%20uncertainty%20and%20compare%20four%20ways%20of%20sharing%20the%20same%201.3%20Mha%20gap%2C%20then%20check%20the%20least-squares%20formula%20against%20a%20numerical%20optimiser%3A%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s3_3_who_absorbs_national(STATE_MHA%2C%20area_level%2C%20area_tree)%3A%0A%20%20%20%20from%20scipy.optimize%20import%20minimize%0A%0A%20%20%20%20relative_var%20%3D%20(%0A%20%20%20%20%20%20%20%200.05**2%0A%20%20%20%20)%20%20%23%20each%20state's%20fused%20forecast%205%20%25%20uncertain%3A%20VintageFusion's%20relative%20variance%0A%20%20%20%20area_tree%5B%22relative_variance%22%5D%20%3D%20relative_var%0A%20%20%20%20area_tree%5B%22absolute_variance%22%5D%20%3D%20(%0A%20%20%20%20%20%20%20%20relative_var%20*%20area_tree%5B%22planted%22%5D%20**%202%0A%20%20%20%20)%20%20%23%20in%20(Mha)%C2%B2%2C%20what%20least%20squares%20needs%0A%0A%20%20%20%20absorbed%20%3D%20%7B%7D%0A%20%20%20%20for%20_label%2C%20allocation%2C%20variance%20in%20%5B%0A%20%20%20%20%20%20%20%20(%22level%20(US%20crops)%22%2C%20%22level%22%2C%20None)%2C%0A%20%20%20%20%20%20%20%20(%22variance%2C%20relative%20(Brazil%20soy%2C%20as%20coded)%22%2C%20%22variance%22%2C%20%22relative_variance%22)%2C%0A%20%20%20%20%20%20%20%20(%22equal%22%2C%20%22equal%22%2C%20None)%2C%0A%20%20%20%20%20%20%20%20(%22variance%2C%20absolute%20(what%20the%20formula%20assumes)%22%2C%20%22variance%22%2C%20%22absolute_variance%22)%2C%0A%20%20%20%20%5D%3A%0A%20%20%20%20%20%20%20%20moved%20%3D%20(%0A%20%20%20%20%20%20%20%20%20%20%20%20reconcile_levels(area_tree%2C%20%22planted%22%2C%20variance%2C%20allocation%2C%20%22proportional%22)%0A%20%20%20%20%20%20%20%20%20%20%20%20-%20area_tree%5B%22planted%22%5D%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20absorbed%5B_label%5D%20%3D%20moved%5Barea_level%20%3D%3D%201%5D.to_numpy()%0A%20%20%20%20absorbed%20%3D%20pd.DataFrame(absorbed%2C%20index%3Dlist(STATE_MHA)).T%0A%20%20%20%20national%20%3D%20area_tree.loc%5Barea_level%20%3D%3D%200%2C%20%22planted%22%5D.item()%20%20%23%2020.0%20unless%20exercise%209%20changes%20it%0A%20%20%20%20_gap%20%3D%20national%20-%20area_tree.loc%5Barea_level%20%3D%3D%201%2C%20%22planted%22%5D.sum()%0A%20%20%20%20print(f%22Mha%20each%20state%20absorbs%20of%20the%20%7B_gap%3A.1f%7D%20Mha%20gap%3A%22)%0A%20%20%20%20print(absorbed.round(3).to_string())%0A%0A%20%20%20%20x_states%20%3D%20area_tree.loc%5Barea_level%20%3D%3D%201%2C%20%22planted%22%5D.to_numpy()%0A%20%20%20%20v_states%20%3D%20area_tree.loc%5Barea_level%20%3D%3D%201%2C%20%22absolute_variance%22%5D.to_numpy()%0A%20%20%20%20closed_form%20%3D%20x_states%20%2B%20v_states%20%2F%20v_states.sum()%20*%20(national%20-%20x_states.sum())%0A%20%20%20%20free%20%3D%20minimize(%0A%20%20%20%20%20%20%20%20lambda%20y%3A%20(((np.append(y%2C%20national%20-%20y.sum())%20-%20x_states)%20**%202)%20%2F%20v_states).sum()%2C%0A%20%20%20%20%20%20%20%20x_states%5B%3A-1%5D%2C%0A%20%20%20%20%20%20%20%20tol%3D1e-14%2C%0A%20%20%20%20)%0A%20%20%20%20print(%0A%20%20%20%20%20%20%20%20%22closed%20form%3A%22%2C%0A%20%20%20%20%20%20%20%20closed_form.round(6)%2C%0A%20%20%20%20%20%20%20%20%22%7C%20numerical%3A%22%2C%0A%20%20%20%20%20%20%20%20np.append(free.x%2C%20national%20-%20free.x.sum()).round(6)%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-%20**As%20coded%2C%20%60variance%60%20behaves%20exactly%20like%20%60equal%60.**%20Equal%20relative%20variances%20make%20equal%20weights%2C%20so%20the%200.7%20Mha%20state%20absorbs%20the%20same%200.433%20Mha%20as%20one%20seventeen%20times%20its%20size%2C%20growing%20by%2062%20%25.%0A%20%20%20%20-%20**With%20absolute%20variances**%20(relative%20%C3%97%20point%C2%B2)%2C%205%20%25%20of%2012%20Mha%20is%20far%20more%20hectares%20of%20doubt%20than%205%20%25%20of%200.7%2C%20so%20the%20big%20state%20takes%20most%20of%20the%20gap%20and%20the%20small%20one%20almost%20none.%0A%20%20%20%20-%20**%60level%60**%20sits%20between%3A%20shares%20proportional%20to%20size%2C%20the%20least-squares%20answer%20when%20a%20state's%20variance%20grows%20in%20proportion%20to%20its%20size.%0A%20%20%20%20-%20**The%20closed%20form%20matches%20the%20optimiser**%20to%20six%20decimals%2C%20so%20%60reconcile_levels%60%20does%20solve%20the%20problem%20science.md%20states.%20Only%20its%20input%20is%20in%20the%20wrong%20units.%0A%0A%20%20%20%20This%20is%20finding%20F10%20(%5Bissue%2037%5D(https%3A%2F%2Fgithub.com%2Ftreefera%2Ftf-model-lab%2Fissues%2F37))%2C%20until%20now%20read%20but%20not%20run.%20The%20toy%20shows%20the%20mechanism%3B%20how%20much%20it%20moves%20Brazil%20soy's%20real%20states%20depends%20on%20how%20unequal%20their%20relative%20variances%20are%2C%20which%20this%20notebook%20can't%20see.%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%20Random%20forest%20against%20the%20production%20choices%0A%0A%20%20%20%20The%20same%20split%2C%20the%20same%20features%2C%20and%20four%20models.%20LightGBM%20gets%20the%20corn%20recipe's%20parameters%2C%20except%20%60min_data_in_leaf%60.%20That%20falls%20from%201%2C000%20to%2050%2C%20because%20this%20whole%20training%20set%20has%20only%201%2C200%20rows.%20Keeping%20%60lambda_l2%60%20at%2050%20then%20changes%20the%20balance%20of%20the%20regularisation.%20LightGBM's%20Huber%20objective%20gives%20every%20row%20a%20hessian%20of%201%2C%20so%20a%20leaf%20of%20at%20least%201%2C000%20rows%20keeps%20at%20least%201%2C000%20%C3%B7%201%2C050%20%E2%89%88%2095%20%25%20of%20its%20Newton%20step%2C%20while%20a%20leaf%20of%2050%20rows%20keeps%20only%20half.%20Read%20LightGBM's%20score%20here%20as%20%22these%20settings%20on%20a%20small%20set%22%2C%20not%20as%20production's%20corn%20model.%0A%0A%20%20%20%20**One%20draw%20isn't%20enough.**%20A%20single%20synthetic%20panel%20scores%20each%20model%20on%20300%20test%20rows%2C%20and%20differences%20of%20a%20few%20kg%2Fha%20are%20within%20the%20noise.%20So%20we%20redraw%20the%20whole%20panel%20five%20times%2C%20with%20a%20different%20seed%20for%20both%20the%20data%20and%20the%20models%2C%20and%20score%20every%20model%20on%20the%20same%20draw%20each%20time.%20That%20makes%20the%20comparison%20*paired*%3A%20a%20model's%20error%20moves%20up%20and%20down%20with%20the%20draw%2C%20and%20what%20matters%20is%20the%20gap%20between%20models%20on%20the%20same%20draw.%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s4_1_random_forest_production(%0A%20%20%20%20FEATURES%2C%0A%20%20%20%20LightGbm%2C%0A%20%20%20%20PcaRidge%2C%0A%20%20%20%20XgBoost%2C%0A%20%20%20%20detrend%2C%0A%20%20%20%20make_counties%2C%0A%20%20%20%20residual_model%2C%0A)%3A%0A%20%20%20%20CORN_LIKE%20%3D%20%7B%0A%20%20%20%20%20%20%20%20%22objective%22%3A%20%22huber%22%2C%0A%20%20%20%20%20%20%20%20%22alpha%22%3A%201200.0%2C%0A%20%20%20%20%20%20%20%20%22learning_rate%22%3A%200.008%2C%0A%20%20%20%20%20%20%20%20%22num_leaves%22%3A%2015%2C%0A%20%20%20%20%20%20%20%20%22max_depth%22%3A%204%2C%0A%20%20%20%20%20%20%20%20%22min_data_in_leaf%22%3A%2050%2C%0A%20%20%20%20%20%20%20%20%22bagging_fraction%22%3A%200.7%2C%0A%20%20%20%20%20%20%20%20%22bagging_freq%22%3A%201%2C%0A%20%20%20%20%20%20%20%20%22feature_fraction%22%3A%200.5%2C%0A%20%20%20%20%20%20%20%20%22lambda_l2%22%3A%2050.0%2C%0A%20%20%20%20%20%20%20%20%22seed%22%3A%2042%2C%0A%20%20%20%20%20%20%20%20%22verbose%22%3A%20-1%2C%0A%20%20%20%20%20%20%20%20%22num_threads%22%3A%201%2C%0A%20%20%20%20%7D%0A%20%20%20%20SEEDS%20%3D%20(7%2C%2011%2C%2023%2C%2031%2C%2047)%0A%20%20%20%20draws%20%3D%20%7Bseed%3A%20detrend(make_counties(seed))%20for%20seed%20in%20SEEDS%7D%0A%0A%20%20%20%20results%2C%20subsampled%20%3D%20%5B%5D%2C%20%7B%7D%0A%20%20%20%20for%20_seed%2C%20(_%2C%20_train_s%2C%20_test_s)%20in%20draws.items()%3A%0A%20%20%20%20%20%20%20%20classic%20%3D%20RandomForestRegressor(%20%20%23%20one%20feature%20in%20four%20tried%20at%20each%20split%0A%20%20%20%20%20%20%20%20%20%20%20%20n_estimators%3D400%2C%20min_samples_leaf%3D5%2C%20max_features%3D1%20%2F%203%2C%20random_state%3D_seed%2C%20n_jobs%3D1%0A%20%20%20%20%20%20%20%20).fit(_train_s%5BFEATURES%5D%2C%20_train_s.residual)%0A%20%20%20%20%20%20%20%20subsampled%5B_seed%5D%20%3D%20rmse(classic.predict(_test_s%5BFEATURES%5D)%2C%20_test_s.residual)%0A%20%20%20%20%20%20%20%20start%20%3D%20time.perf_counter()%0A%20%20%20%20%20%20%20%20_forest%20%3D%20RandomForestRegressor(%0A%20%20%20%20%20%20%20%20%20%20%20%20n_estimators%3D400%2C%20min_samples_leaf%3D5%2C%20random_state%3D_seed%2C%20n_jobs%3D1%0A%20%20%20%20%20%20%20%20).fit(_train_s%5BFEATURES%5D%2C%20_train_s.residual)%0A%20%20%20%20%20%20%20%20results.append(%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%20%20%20%20_seed%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22random%20forest%20(sklearn%2C%20not%20in%20production)%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20rmse(_forest.predict(_test_s%5BFEATURES%5D)%2C%20_test_s.residual)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20time.perf_counter()%20-%20start%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20)%20%20%23%20fit%20%2B%20predict%0A%20%20%20%20%20%20%20%20for%20_name%2C%20_regressor%20in%20(%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%20%20%20%20%22XgBoost%20wrapper%20(depth%204%2C%20eta%200.05%2C%20400%20rounds)%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20XgBoost(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20rounds%3D400%2C%20params%3D%7B%22max_depth%22%3A%204%2C%20%22eta%22%3A%200.05%2C%20%22seed%22%3A%20_seed%2C%20%22nthread%22%3A%201%7D%0A%20%20%20%20%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)%2C%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%20%20%20%20%22LightGbm%20wrapper%20(corn-like%2C%201500%20rounds)%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20LightGbm(rounds%3D1500%2C%20params%3D%7B**CORN_LIKE%2C%20%22seed%22%3A%20_seed%7D)%2C%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(%22PcaRidge%20(3%20components)%22%2C%20PcaRidge(components%3D3%2C%20alpha%3D1.0))%2C%0A%20%20%20%20%20%20%20%20)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20start%20%3D%20time.perf_counter()%0A%20%20%20%20%20%20%20%20%20%20%20%20_prediction%20%3D%20(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20residual_model(_regressor).fit(_train_s).predict(_test_s).residual__predicted%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%20results.append(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20(_seed%2C%20_name%2C%20rmse(_prediction%2C%20_test_s.residual)%2C%20time.perf_counter()%20-%20start)%0A%20%20%20%20%20%20%20%20%20%20%20%20)%20%20%23%20fit%20%2B%20predict%20%2B%20shares%0A%20%20%20%20%20%20%20%20results.append(%0A%20%20%20%20%20%20%20%20%20%20%20%20(_seed%2C%20%22predict%200%20(trend%20only)%22%2C%20rmse(0%2C%20_test_s.residual)%2C%20np.nan)%0A%20%20%20%20%20%20%20%20)%20%20%23%20not%20timed%0A%0A%20%20%20%20results%20%3D%20pd.DataFrame(results%2C%20columns%3D%5B%22seed%22%2C%20%22model%22%2C%20%22rmse%22%2C%20%22pipeline_seconds%22%5D)%0A%20%20%20%20results%5B%22rank%22%5D%20%3D%20results.groupby(%22seed%22).rmse.rank()%0A%20%20%20%20print(%22hindcast%20RMSE%20(kg%2Fha)%20on%20each%20draw%3A%22)%0A%20%20%20%20print(results.pivot(index%3D%22model%22%2C%20columns%3D%22seed%22%2C%20values%3D%22rmse%22).round(1).to_string()%2C%20%22%5Cn%22)%0A%0A%20%20%20%20per_draw%20%3D%20results.pivot(index%3D%22seed%22%2C%20columns%3D%22model%22%2C%20values%3D%22rmse%22)%0A%20%20%20%20xgb_name%20%3D%20%22XgBoost%20wrapper%20(depth%204%2C%20eta%200.05%2C%20400%20rounds)%22%0A%20%20%20%20for%20other%20in%20(%0A%20%20%20%20%20%20%20%20%22random%20forest%20(sklearn%2C%20not%20in%20production)%22%2C%0A%20%20%20%20%20%20%20%20%22LightGbm%20wrapper%20(corn-like%2C%201500%20rounds)%22%2C%0A%20%20%20%20)%3A%0A%20%20%20%20%20%20%20%20_gap%20%3D%20per_draw%5Bother%5D%20-%20per_draw%5Bxgb_name%5D%0A%20%20%20%20%20%20%20%20print(%0A%20%20%20%20%20%20%20%20%20%20%20%20f%22%7Bother.split('%20(')%5B0%5D%7D%20minus%20XgBoost%2C%20per%20draw%3A%20%7B_gap.round(1).tolist()%7D%20%E2%86%92%20%7B_gap.mean()%3A.1f%7D%20%C2%B1%20%7B_gap.std()%3A.1f%7D%22%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20subsampled%20%3D%20pd.Series(subsampled)%0A%20%20%20%20_gap%20%3D%20subsampled%20-%20per_draw%5Bxgb_name%5D%0A%20%20%20%20print(%0A%20%20%20%20%20%20%20%20f%22random%20forest%20with%20max_features%3D1%2F3%3A%20%7Bsubsampled.mean()%3A.1f%7D%20%C2%B1%20%7Bsubsampled.std()%3A.1f%7D%3B%20%22%0A%20%20%20%20%20%20%20%20f%22minus%20XgBoost%2C%20per%20draw%3A%20%7B_gap.round(1).tolist()%7D%5Cn%22%0A%20%20%20%20)%0A%0A%20%20%20%20results.groupby(%22model%22).agg(%0A%20%20%20%20%20%20%20%20mean_rmse%3D(%22rmse%22%2C%20%22mean%22)%2C%0A%20%20%20%20%20%20%20%20sd%3D(%22rmse%22%2C%20%22std%22)%2C%0A%20%20%20%20%20%20%20%20best_rank%3D(%22rank%22%2C%20%22min%22)%2C%0A%20%20%20%20%20%20%20%20worst_rank%3D(%22rank%22%2C%20%22max%22)%2C%0A%20%20%20%20%20%20%20%20mean_pipeline_seconds%3D(%22pipeline_seconds%22%2C%20%22mean%22)%2C%0A%20%20%20%20).sort_values(%22mean_rmse%22).round(2)%0A%20%20%20%20return%20(draws%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%20Read%20the%20paired%20gaps%2C%20not%20the%20means%3A%20each%20model's%20error%20has%20a%20standard%20deviation%20of%2015%20to%2041%20kg%2Fha%20across%20draws%2C%20because%20some%20draws%20are%20simply%20harder.%0A%0A%20%20%20%20-%20**The%20random%20forest%20and%20XGBoost%20show%20no%20consistent%20winner%20in%20these%20five%20draws.**%20The%20gap%20changes%20sign%20between%20draws%20and%20averages%20under%201%20kg%2Fha.%20That%20doesn't%20prove%20the%20true%20gap%20is%20negligible%3B%20five%20draws%20only%20show%20that%20neither%20wins%20every%20time.%20This%20forest%20is%20scikit-learn's%20default%2C%20%60max_features%3D1.0%60%2C%20so%20every%20split%20sees%20every%20feature%3A%20it%20is%20bagged%20trees.%20The%20classic%20random%20forest%2C%20trying%20a%20third%20of%20the%20features%20(here%20one%20of%20four)%20at%20each%20split%2C%20trails%20XGBoost%20in%20every%20draw%20(printed%20below%20the%20table%20of%20draws).%0A%20%20%20%20-%20**LightGBM%20with%20corn-like%20settings%20comes%20third%20in%20every%20draw**%2C%208%20to%2060%20kg%2Fha%20behind%20XGBoost.%20Those%20settings%20were%20chosen%20for%20a%20much%20larger%20training%20set%20(a%20minimum%20leaf%20of%201%2C000%20rows%2C%20a%20learning%20rate%20of%200.008)%2C%20so%20read%20this%20as%20%22settings%20matter%22%2C%20not%20%22LightGBM%20is%20worse%22.%0A%20%20%20%20-%20**All%20three%20tree%20ensembles%20beat%20the%20linear%20%60PcaRidge%60%20in%20every%20draw.**%20It%20can't%20bend%20round%20the%20heat%20threshold%20or%20the%20rain%20curve.%0A%0A%20%20%20%20Production's%20choice%20of%20boosting%20over%20a%20forest%20is%20probably%20about%20what%20boosting%20*also*%20offers%3A%20losses%20a%20forest%20can't%20fit%20(corn's%20Huber%3B%20the%20joint%20booster's%20weighted%20squared%20error%2C%20by%20contrast%2C%20a%20forest%20takes%20as%20%60sample_weight%60)%2C%20TreeSHAP%20built%20into%20the%20library%20(a%20forest%20needs%20the%20separate%20%60shap%60%20package)%2C%20and%20fewer%2C%20shallower%20trees.%20The%20timing%20column%20is%20end-to-end%20**pipeline**%20time%20on%20a%20laptop%2C%20and%20the%20pipelines%20do%20different%20work%3A%20the%20forest%20fits%20and%20predicts%2C%20while%20the%20wrappers%20also%20compute%20TreeSHAP%20shares.%20It%20isn't%20a%20like-for-like%20speed%20test%2C%20and%20the%20trend-only%20row%20isn't%20timed.%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s4_1_random_forest_production_2(XgBoost%2C%20draws%2C%20residual_model)%3A%0A%20%20%20%20_grid%20%3D%20%5B%5D%0A%20%20%20%20for%20_seed%2C%20(_%2C%20_train_s%2C%20_test_s)%20in%20draws.items()%3A%0A%20%20%20%20%20%20%20%20for%20_depth%20in%20(1%2C%203%2C%206)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20for%20eta%2C%20_rounds%20in%20((0.3%2C%2060)%2C%20(0.05%2C%20400))%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20_regressor%20%3D%20XgBoost(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20rounds%3D_rounds%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20params%3D%7B%22max_depth%22%3A%20_depth%2C%20%22eta%22%3A%20eta%2C%20%22seed%22%3A%20_seed%2C%20%22nthread%22%3A%201%7D%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%20_prediction%20%3D%20(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20residual_model(_regressor).fit(_train_s).predict(_test_s).residual__predicted%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%20_grid.append(%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%22seed%22%3A%20_seed%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%22max_depth%22%3A%20_depth%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%22eta%22%3A%20eta%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%22rmse%22%3A%20rmse(_prediction%2C%20_test_s.residual)%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_grid%20%3D%20pd.DataFrame(_grid)%0A%20%20%20%20by_draw%20%3D%20_grid.pivot_table(index%3D%22seed%22%2C%20columns%3D%5B%22max_depth%22%2C%20%22eta%22%5D%2C%20values%3D%22rmse%22)%0A%20%20%20%20for%20_a%2C%20_b%20in%20(((6%2C%200.05)%2C%20(3%2C%200.05))%2C%20((1%2C%200.05)%2C%20(3%2C%200.05))%2C%20((3%2C%200.05)%2C%20(3%2C%200.3)))%3A%0A%20%20%20%20%20%20%20%20_gap%20%3D%20by_draw%5B_a%5D%20-%20by_draw%5B_b%5D%0A%20%20%20%20%20%20%20%20print(%0A%20%20%20%20%20%20%20%20%20%20%20%20f%22depth%20%7B_a%5B0%5D%7D%2C%20eta%20%7B_a%5B1%5D%7D%20minus%20depth%20%7B_b%5B0%5D%7D%2C%20eta%20%7B_b%5B1%5D%7D%2C%20per%20draw%3A%20%7B_gap.round(1).tolist()%7D%20%E2%86%92%20%7B_gap.mean()%3A.1f%7D%20%C2%B1%20%7B_gap.std()%3A.1f%7D%22%0A%20%20%20%20%20%20%20%20)%0A%0A%20%20%20%20summary%20%3D%20_grid.groupby(%5B%22max_depth%22%2C%20%22eta%22%5D).rmse.agg(%5B%22mean%22%2C%20%22std%22%5D)%0A%20%20%20%20summary.apply(lambda%20r%3A%20f%22%7Br%5B'mean'%5D%3A.1f%7D%20%C2%B1%20%7Br%5B'std'%5D%3A.1f%7D%22%2C%20axis%3D1).unstack(%22eta%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-%20**Depth%206%20is%20worse%20than%20depth%203%20in%20all%20five%20paired%20draws**%20(both%20at%20eta%200.05%20with%20400%20rounds)%2C%20by%203%20to%2036%20kg%2Fha.%20Each%20weather%20effect%20here%20is%20a%20function%20of%20one%20feature%2C%20so%20deep%20trees%20spend%20their%20extra%20splits%20fitting%20noise%20as%20interactions.%20On%20the%20means%20alone%20you%20couldn't%20tell%3A%20the%20spread%20between%20draws%20is%20as%20large%20as%20the%20gap.%20The%20pairing%20is%20what%20makes%20it%20visible.%0A%20%20%20%20-%20**Depth%201%20and%20depth%203%20can't%20be%20told%20apart**%3A%20the%20gap%20changes%20sign%20between%20draws.%0A%20%20%20%20-%20**The%20two%20training%20schedules%20scored%20similarly%20at%20depth%203**%3A%20eta%200.05%20with%20400%20rounds%20beat%20eta%200.3%20with%2060%20rounds%20in%20four%20draws%20out%20of%20five%2C%20by%20a%20few%20kg%2Fha.%20Each%20schedule%20changes%20both%20the%20step%20size%20and%20the%20number%20of%20rounds%2C%20so%20this%20grid%20can't%20separate%20the%20learning%20rate%20from%20the%20round%20count.%0A%0A%20%20%20%20The%20joint%20booster%20runs%20depth%206%20at%20eta%200.05%2C%20but%20on%20many%20more%20rows%20and%20with%20real%20interactions%20(heat%20by%20rain%2C%20latitude%20by%20heat)%20that%20this%20synthetic%20data%20lacks.%20Decide%20depth%20from%20the%20data%2C%20with%20paired%20comparisons%2C%20not%20from%20this%20table.%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s4_1_random_forest_production_3(XgBoost%2C%20counties_1)%3A%0A%20%20%20%20one%20%3D%20counties_1%5Bcounties_1.county%20%3D%3D%200%5D%0A%20%20%20%20seasons_train%2C%20seasons_test%20%3D%20one%5Bone.season%20%3C%3D%202019%5D%2C%20one%5Bone.season%20%3E%202019%5D%0A%20%20%20%20trees_on_time%20%3D%20XgBoost(rounds%3D200%2C%20params%3D%7B%22max_depth%22%3A%202%2C%20%22eta%22%3A%200.1%2C%20%22nthread%22%3A%201%7D).fit(%0A%20%20%20%20%20%20%20%20seasons_train%5B%5B%22season%22%5D%5D%2C%20seasons_train.yield_kg_ha%0A%20%20%20%20)%0A%0A%20%20%20%20_fig%2C%20_ax%20%3D%20plt.subplots(figsize%3D(8%2C%203.5))%0A%20%20%20%20_ax.plot(one.season%2C%20one.yield_kg_ha%2C%20%22o%22%2C%20ms%3D4%2C%20label%3D%22county%200%20yield%22)%0A%20%20%20%20_ax.plot(%0A%20%20%20%20%20%20%20%20one.season%2C%0A%20%20%20%20%20%20%20%20trees_on_time.predict(one%5B%5B%22season%22%5D%5D)%2C%0A%20%20%20%20%20%20%20%20label%3D%22trees%20on%20season%20(flat%20after%202019)%22%2C%0A%20%20%20%20)%0A%20%20%20%20_ax.plot(%0A%20%20%20%20%20%20%20%20one.season%2C%0A%20%20%20%20%20%20%20%20one.intercept%20%2B%20one.slope%20*%20(one.season%20-%202000)%2C%0A%20%20%20%20%20%20%20%20label%3D%22linear%20trend%20(keeps%20rising)%22%2C%0A%20%20%20%20)%0A%20%20%20%20_ax.axvline(2019.5%2C%20ls%3D%22%3A%22%2C%20color%3D%22grey%22)%0A%20%20%20%20_ax.set(%0A%20%20%20%20%20%20%20%20xlabel%3D%22season%22%2C%0A%20%20%20%20%20%20%20%20ylabel%3D%22kg%2Fha%22%2C%0A%20%20%20%20%20%20%20%20title%3D%22Trees%20on%20season%20go%20flat%20after%202019%3B%20the%20trend%20keeps%20rising%22%2C%0A%20%20%20%20)%0A%20%20%20%20_ax.legend()%0A%20%20%20%20plt.show()%0A%20%20%20%20print(%0A%20%20%20%20%20%20%20%20%22trees'%20prediction%20for%202020%E2%80%932024%3A%22%2C%20trees_on_time.predict(seasons_test%5B%5B%22season%22%5D%5D).round(0)%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%20This%20is%20why%20both%20production%20chains%20are%20**trend%20plus%20trees**.%20The%20trend%20carries%20yields%20beyond%20anything%20seen%20in%20training%2C%20and%20the%20trees%20explain%20the%20year-to-year%20departures%2C%20which%20stay%20inside%20the%20range%20of%20past%20weather.%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%201.3%2C%20set%20%60learning_rate%3D1.0%60.%20How%20many%20rounds%20does%20it%20now%20need%2C%20and%20what%20happens%20to%20the%20fit%20after%20100%20rounds%3F%20*Check%3A*%20one%20round%20now%20already%20has%20the%20depth-2%20shape%2C%20and%20by%20100%20rounds%20the%20training%20RMSE%20is%20far%20below%20%CF%83%20%3D%20150.%0A%20%20%20%202.%20In%20%60make_counties%60%2C%20add%20an%20extra%20hot-and-dry%20penalty%2C%20%60-%20200%20*%20(pr%20%3C%200)%20*%20(edd%20%3E%2080)%60%2C%20so%20hot%2C%20dry%20seasons%20lose%20more%20than%20hot%2C%20wet%20ones.%20(To%20make%20heat%20hurt%20*only*%20when%20it's%20dry%2C%20gate%20the%20original%20heat%20term%20instead.)%20marimo%20then%20re-runs%20every%20cell%20that%20uses%20%60make_counties%60%20or%20its%20panel%3A%20the%20detrending%20cell%20(%60s2_2_realistic_synthetic_county_2%60)%2C%20sections%202.3%20to%203.2%2C%20and%204.1's%20model%20and%20depth%20comparisons%20(%60s4_1_random_forest_production%60%20and%20%60s4_1_random_forest_production_2%60)%2C%20which%20redraw%20their%20own%20panels.%20Does%20depth%201%20still%20keep%20up%20with%20depth%203%3F%20Why%20not%3F%20*Check%3A*%20you%20can%20say%20why%20a%20stump%2C%20which%20splits%20on%20one%20feature%2C%20can't%20represent%20a%20penalty%20that%20needs%20two.%0A%20%20%20%203.%20In%202.4%2C%20give%20a%20tenth%20of%20the%20rows%20a%20weight%20of%20250%20(heavily%20weighted%20rows)%20and%20re-run%20it.%20Compare%20each%20tree%20method's%20objective-versus-%60DMatrix%60%20gap%20with%20the%20values%20printed%20before%20the%20edit.%20Does%20the%20gap%20under%20%60hist%60%20grow%3F%20*Check%3A*%20%60exact%60%20still%20agrees%20to%20rounding%20error%2C%20as%20the%20control.%0A%20%20%20%204.%20In%203.2%2C%20make%20*every*%20feature%20missing%20for%20one%20row.%20What%20does%20%60FilledRegression%60%20predict%2C%20and%20why%3F%20*Check%3A*%20the%20prediction%20and%20the%20intercept%20are%20both%20exactly%200%2C%20because%20%60predict%60%20sets%20%60intercept%5Bmissing.all(axis%3D1)%5D%20%3D%200.0%60.%0A%20%20%20%205.%20In%204.1%2C%20set%20the%20random%20forest's%20%60min_samples_leaf%3D1%60.%20Predict%20the%20hindcast%20RMSE%20before%20you%20run%20it.%20*Check%3A*%20compare%20the%20change%20with%20the%20paired%20gaps%20between%20models%3A%20is%20it%20larger%20than%20one%20draw's%20noise%3F%0A%20%20%20%206.%20In%204.1%2C%20use%20ten%20seeds%20instead%20of%20five.%20First%20decide%20what%20forest%E2%80%93XGBoost%20gap%20would%20matter%20in%20practice%20(say%2010%20kg%2Fha).%20Then%20compute%20a%2095%20%25%20interval%20for%20the%20*mean*%20paired%20difference%20(mean%20%C2%B1%202.26%20%C3%97%20sd%20%C3%B7%20%E2%88%9An%20for%20ten%20draws)%2C%20not%20the%20sample%20spread.%20Does%20the%20interval%20rule%20out%20a%20gap%20that%20matters%3F%20*Check%3A*%20for%20the%20same%20spread%2C%20ten%20draws%20give%20an%20interval%20about%201.7%20times%20narrower%20than%20five%2C%20(2.78%20%C3%B7%20%E2%88%9A5)%20%C3%B7%20(2.26%20%C3%B7%20%E2%88%9A10).%0A%20%20%20%207.%20In%203.1b%2C%20re-centre%20the%20shares%20on%20the%20hottest%20band%20instead%20of%20the%20coolest.%20What%20does%20a%20cool%20season's%20share%20mean%20now%3F%20*Check%3A*%20every%20band's%20re-centred%20share%20is%20now%20positive%2C%20and%20a%20cool%20season's%20reads%20%22this%20much%20less%20heat%20damage%20than%20the%20hottest%20seasons%22.%0A%20%20%20%208.%20In%202.5%2C%20set%20%60min_child_weight%60%20in%20the%20first%20training%20run%20to%201%2C%20then%202%2C%20then%205.%20How%20much%20of%20the%20state%20effect%20is%20learned%20at%20each%3F%20*Check%3A*%20at%20or%20below%20the%20state%20rows'%20total%20*H*%20(about%201)%20they%20learn%20as%20much%20as%20with%20no%20floor%3B%20above%20it%2C%20only%20what%20they%20can%20share%20with%20county%20rows%20in%20the%20same%20leaf.%0A%20%20%20%209.%20In%202.7%2C%20give%20the%20nation%2016.0%20Mha%20instead%20of%2020.0%2C%20so%20the%20states%20must%20shrink%20by%202.7%20Mha%2C%20and%20share%20the%20gap%20by%20%60equal%60.%20Predict%20what%20happens%20to%20the%20small%20state.%20*Check%3A*%20it%20would%20go%20to%20%E2%88%920.2%2C%20so%20it%20is%20clipped%20to%200%20(%60reconcile_levels%60%20logs%20a%20warning)%2C%20and%20the%20nation%20is%20reset%20to%20the%20states'%20new%20sum%2C%2016.2%2C%20not%2016.0.%20Why%20can't%20%60level%60%20ever%20drive%20a%20state%20negative%3F%0A%20%20%20%2010.%20Show%20that%20%60project_abandonment%60's%20split%20is%20the%20minimum-variance%20meeting%20point%3A%20minimise%20(p%20%E2%88%92%20100)%C2%B2%2Fv%E2%82%9A%20%2B%20(p%20%E2%88%92%20104)%C2%B2%2Fv%E2%82%95%20over%20the%20common%20value%20p.%20*Check%3A*%20p%20%3D%20100%20%2B%204%C2%B7v%E2%82%9A%2F(v%E2%82%9A%20%2B%20v%E2%82%95)%2C%20which%20is%20100.4%20for%20v%E2%82%9A%20%3D%201%2C%20v%E2%82%95%20%3D%209%2C%20as%20in%202.8.%0A%20%20%20%2011.%20Make%203.3's%20variances%20unequal%3A%20the%20small%20state%2020%20%25%20uncertain%2C%20the%20others%205%20%25.%20Re-run%20with%20relative%20and%20with%20absolute%20variances.%20*Check%3A*%20with%20relative%20variances%20the%20small%20state%20now%20absorbs%20most%20of%20the%20gap%2C%201.156%20Mha%2C%20and%20grows%20by%20165%20%25%3B%20with%20absolute%20ones%20it%20takes%200.054%20Mha%2C%20still%20the%20least%20of%20the%20three.%0A%0A%20%20%20%20%23%20Sources%0A%0A%20%20%20%20-%20XGBoost%203.2.0%3A%20%5BPython%20package%20introduction%5D(https%3A%2F%2Fxgboost.readthedocs.io%2Fen%2Frelease_3.2.0%2Fpython%2Fpython_intro.html)%2C%20%5Bgetting%20started%5D(https%3A%2F%2Fxgboost.readthedocs.io%2Fen%2Frelease_3.2.0%2Fget_started.html)%2C%20%5Bintroduction%20to%20boosted%20trees%5D(https%3A%2F%2Fxgboost.readthedocs.io%2Fen%2Frelease_3.2.0%2Ftutorials%2Fmodel.html)%20(the%20leaf%20value%20and%20gain%20formulas)%2C%20%5Bparameters%20(including%20%60tree_method%60%20and%20%60min_child_weight%60)%5D(https%3A%2F%2Fxgboost.readthedocs.io%2Fen%2Frelease_3.2.0%2Fparameter.html)%2C%20%5Btree%20methods%20(%60exact%60%2C%20%60approx%60%2C%20%60hist%60%20and%20their%20sketches)%5D(https%3A%2F%2Fxgboost.readthedocs.io%2Fen%2Frelease_3.2.0%2Ftreemethod.html)%2C%20%5Bcustom%20objectives%5D(https%3A%2F%2Fxgboost.readthedocs.io%2Fen%2Frelease_3.2.0%2Ftutorials%2Fcustom_metric_obj.html)%2C%20%5Bprediction%20(%60pred_contribs%60)%5D(https%3A%2F%2Fxgboost.readthedocs.io%2Fen%2Frelease_3.2.0%2Fprediction.html)%2C%20%5Bmonotonic%20constraints%5D(https%3A%2F%2Fxgboost.readthedocs.io%2Fen%2Frelease_3.2.0%2Ftutorials%2Fmonotonic.html)%0A%20%20%20%20-%20LightGBM%204.7.0%3A%20%5BPython-package%20introduction%5D(https%3A%2F%2Flightgbm.readthedocs.io%2Fen%2Fv4.7.0%2FPython-Intro.html)%2C%20%5Bquick%20start%5D(https%3A%2F%2Flightgbm.readthedocs.io%2Fen%2Fv4.7.0%2FQuick-Start.html)%2C%20%5Bparameters%5D(https%3A%2F%2Flightgbm.readthedocs.io%2Fen%2Fv4.7.0%2FParameters.html)%2C%20%5Bfeatures%20(leaf-wise%20growth)%5D(https%3A%2F%2Flightgbm.readthedocs.io%2Fen%2Fv4.7.0%2FFeatures.html)%2C%20%5B%60Booster.predict%60%20(%60pred_contrib%60)%5D(https%3A%2F%2Flightgbm.readthedocs.io%2Fen%2Fv4.7.0%2Fpythonapi%2Flightgbm.Booster.html)%0A%20%20%20%20-%20scikit-learn%201.9%3A%20%5Bdecision%20trees%5D(https%3A%2F%2Fscikit-learn.org%2F1.9%2Fmodules%2Ftree.html)%2C%20%5Bensembles%2C%20including%20random%20forests%5D(https%3A%2F%2Fscikit-learn.org%2F1.9%2Fmodules%2Fensemble.html)%0A%20%20%20%20-%20Breiman%20(2001)%2C%20%22Random%20forests%22%2C%20*Machine%20Learning*%2045%2C%205%E2%80%9332%3A%20https%3A%2F%2Fdoi.org%2F10.1023%2FA%3A1010933404324%0A%20%20%20%20-%20Chen%20and%20Guestrin%20(2016)%2C%20%22XGBoost%3A%20A%20Scalable%20Tree%20Boosting%20System%22%2C%20%C2%A72.2%20(the%20leaf%20value%20and%20gain)%3A%20https%3A%2F%2Farxiv.org%2Fabs%2F1603.02754%0A%20%20%20%20-%20Ke%20et%20al.%20(2017)%2C%20%22LightGBM%3A%20A%20Highly%20Efficient%20Gradient%20Boosting%20Decision%20Tree%22%2C%20NeurIPS%2030%3A%20https%3A%2F%2Fpapers.nips.cc%2Fpaper%2F2017%2Fhash%2F6449f44a102fde848669bdd9eb6b76fa-Abstract.html%0A%20%20%20%20-%20Lundberg%2C%20Erion%20and%20Lee%20(2018)%2C%20%22Consistent%20Individualized%20Feature%20Attribution%20for%20Tree%20Ensembles%22%20(TreeSHAP)%3A%20https%3A%2F%2Farxiv.org%2Fabs%2F1802.03888%0A%20%20%20%20-%20Grinsztajn%2C%20Oyallon%20and%20Varoquaux%20(2022)%2C%20%22Why%20do%20tree-based%20models%20still%20outperform%20deep%20learning%20on%20tabular%20data%3F%22%3A%20https%3A%2F%2Farxiv.org%2Fabs%2F2207.08815%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%2F3-model-families%2F3.1-gradient-boosted-trees%2Fcode.md)%20and%20%5Bscience.md%5D(https%3A%2F%2Fgithub.com%2Ftreefera%2Ftf-model-lab%2Fblob%2Fmain%2Facademy%2F3-model-families%2F3.1-gradient-boosted-trees%2Fscience.md)%0A%20%20%20%20-%20Hyndman%20and%20Athanasopoulos%2C%20*Forecasting%3A%20Principles%20and%20Practice*%2C%203rd%20edition%2C%20chapter%2011%20(hierarchical%20forecasting%20and%20reconciliation)%3A%20https%3A%2F%2Fotexts.com%2Ffpp3%2Fhierarchical.html%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
d39f013ac805dc71ad410930f0e34dc0