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%20pandas%20as%20pd%0A%20%20%20%20import%20yaml%0A%20%20%20%20from%20loguru%20import%20logger%0A%0A%20%20%20%20from%20labkit%20import%20area%2C%20figures%0A%20%20%20%20from%20labkit.show%20import%20show%0A%0A%20%20%20%20logger.remove()%0A%20%20%20%20m%20%3D%20area.load(%0A%20%20%20%20%20%20%20%20%22area_soybeans_brazil%22%2C%20%22area_soy_brazil%22%2C%20%22ADM0%3ABRA%22%2C%20%22Brazil%22%0A%20%20%20%20)%20%20%23%20the%20worked%20example%0A%20%20%20%20m.summary()%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%20How%20the%20area%20models%20work%0A%0A%20%20%20%20Read%20once.%20Every%20area%20model%20in%20model-foundry%20(Brazil%20soybean%2C%20US%20corn%2C%20soy%2C%20cotton%20and%20winter%20wheat)%20is%20the%20same%20code%20configured%20by%20a%20recipe%2C%20so%20the%20ideas%20here%20hold%20for%20all%20of%20them.%20Brazil%20soybean%20is%20the%20worked%20example%3B%20its%20own%20notebook%20is%20%5B5.1%20Brazil%20soybean%20area%5D(https%3A%2F%2Fgithub.com%2Ftreefera%2Ftf-model-lab%2Fblob%2Fmain%2Facademy%2F5-applied-models%2F5.1-soybeans-brazil%2Farea.py).%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%20A%20model%20is%20code%20plus%20a%20recipe%0A%0A%20%20%20%20The%20crop%20package%20is%20three%20lines%3A%20the%20area%20module%2C%20a%20commodity%20name%20and%20a%20YAML%20recipe.%20Everything%20specific%20to%20a%20crop%20and%20a%20country%20is%20in%20the%20recipe%3B%20changing%20a%20model%20means%20changing%20its%20recipe%2C%20until%20a%20change%20needs%20new%20code%20in%20area-module.%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20model_code_plus()%3A%0A%20%20%20%20import%20importlib.resources%20as%20resources%0A%0A%20%20%20%20show(m.package.__class__)%0A%20%20%20%20print(resources.files(%22area_soy_brazil%22).joinpath(%22soybeans_bra.yaml%22).read_text())%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%20Truth%2C%20and%20when%20it%20is%20known%0A%0A%20%20%20%20The%20truth%20is%20the%20official%20final%3A%20IBGE's%20PAM%20in%20Brazil%2C%20NASS%20in%20the%20US.%20It%20arrives%20long%20after%20the%20season%20(PAM%20about%2017%20months%20after%20the%20season%20starts)%2C%20so%20a%20forecast%20on%20any%20date%20may%20only%20use%20the%20seasons%20published%20by%20then.%20model-foundry%20stamps%20every%20row%20with%20%60truth_through%60%2C%20the%20newest%20season%20known%20on%20its%20date%20and%20at%20its%20level%2C%20and%20every%20feature%20and%20every%20fit%20respects%20it.%20Getting%20this%20wrong%20is%20the%20commonest%20way%20a%20hindcast%20flatters%20a%20model%3A%20it%20reads%20truth%20that%20did%20not%20yet%20exist.%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%20Which%20places%20are%20forecast%0A%0A%20%20%20%20The%20cohort%20fixes%20each%20season's%20places%20when%20the%20season%20starts%3A%20those%20with%20the%20crop%20in%20recent%20known%20seasons%2C%20then%20the%20largest%20states%20(99%25%20of%20the%20nation)%20and%20municipalities%20(95%25%20of%20their%20state).%20The%20rest%20are%20left%20to%20the%20national%20total%3A%20their%20errors%20run%20to%20thousands%20of%20percent%2C%20and%20including%20them%20would%20make%20every%20metric%20about%20them.%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%20The%20walk-forward%20hindcast%0A%0A%20%20%20%20The%20test%20of%20a%20forecast%20is%20what%20it%20would%20have%20said%20on%20the%20day.%20Each%20season%20is%20forecast%20by%20models%20trained%20only%20on%20seasons%20whose%20truth%20was%20public%20on%20the%20forecast%20date%3B%20when%20new%20truth%20arrives%20mid-season%2C%20the%20forecasts%20switch%20to%20a%20newer%20model.%20Nothing%20is%20ever%20scored%20on%20data%20its%20model%20saw.%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20walk_forward_hindcast()%3A%0A%20%20%20%20figures.walk_forward(%0A%20%20%20%20%20%20%20%20m.folds()%2C%201995%2C%20%22Which%20seasons%20trained%20the%20model%20that%20forecast%20each%20season%22%0A%20%20%20%20)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%20The%20model%3A%20trend%20plus%20a%20correction%0A%0A%20%20%20%201.%20**Trend.**%20A%20line%20through%20each%20place's%20own%20known%20seasons%2C%20read%20at%20this%20season.%20Most%20of%20the%20forecast.%0A%20%20%20%202.%20**Booster.**%20One%20XGBoost%20model%20learns%20the%20trend's%20miss%20from%20features%2C%20for%20planted%20and%20abandoned%20at%20once%2C%20at%20every%20level.%20Its%20loss%20is%20relative%20and%20its%20levels%20weighted%2C%20so%20the%20nation%2C%20the%20states%20and%20the%20municipalities%20all%20count.%0A%20%20%20%203.%20**Post-processing.**%20In%20order%3A%20official%20surveys%20fused%20by%20their%20past%20accuracy%2C%20the%20forecast%20projected%2C%20levels%20reconciled%20so%20they%20add%20up%2C%20abandonment%20capped%2C%20and%20prediction%20intervals%20calibrated%20on%20past%20errors.%0A%0A%20%20%20%20Each%20step%20writes%20its%20own%20column%20in%20the%20predictions%2C%20so%20each%20step's%20effect%20can%20be%20measured.%20The%20steps%20this%20model%20runs%3A%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20model_trend_plus()%3A%0A%20%20%20%20chain%20%3D%20yaml.safe_load(%0A%20%20%20%20%20%20%20%20(m.run%20%2F%20%22hindcast%22%20%2F%20%22area%22%20%2F%20%22post_processing%22%20%2F%20%22chain.json%22).read_text()%0A%20%20%20%20)%0A%20%20%20%20pd.DataFrame(chain%5B%22models%22%5D%20if%20isinstance(chain%2C%20dict)%20and%20%22models%22%20in%20chain%20else%20chain)%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%20Why%20a%20forecast%20moved%0A%0A%20%20%20%20The%20booster%20starts%20from%20zero%2C%20so%20each%20forecast%20is%20exactly%20**trend%20%2B%20one%20part%20per%20feature**%3A%20XGBoost's%20TreeSHAP%20splits%20the%20correction%2C%20and%20the%20parts%20add%20up%20to%20the%20model's%20own%20output%20on%20every%20row.%20That%20makes%20every%20forecast%20explainable%20as%20a%20waterfall.%20One%20national%20forecast%3A%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20forecast_moved()%3A%0A%20%20%20%20parts%20%3D%20m.national_parts()%0A%20%20%20%20row%20%3D%20parts%5B(parts%5B%22season_year%22%5D%20%3D%3D%202024)%20%26%20(parts%5B%22init_date%22%5D%20%3D%3D%20%222023-12-04%22)%5D.iloc%5B0%5D%0A%20%20%20%20features%20%3D%20area.part_features(parts)%0A%20%20%20%20figures.waterfall(%0A%20%20%20%20%20%20%20%20row%5Bfeatures%5D%2C%20row%5B%22trend%22%5D%2C%20%22National%20planted%20area%2C%202023%2F24%2C%20forecast%20of%204%20Dec%202023%22%0A%20%20%20%20)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%20Reading%20a%20model's%20notebook%0A%0A%20%20%20%20Each%20model's%20notebook%20follows%20the%20same%20order%3A%20what%20and%20why%2C%20the%20recipe%2C%20the%20trend%2C%20the%20features%2C%20the%20architecture%2C%20the%20walk-forward%20split%2C%20accuracy%2C%20forecasts%20against%20actuals%20over%20time%2C%20and%20recipe%20experiments.%20The%20two%20deep%20dives%20are%20for%20when%20you%20need%20more%3A%20%5B2%20Data%20and%20features%5D(https%3A%2F%2Fgithub.com%2Ftreefera%2Ftf-model-lab%2Fblob%2Fmain%2Facademy%2F4-our-model-families%2F4.1-area-and-yield%2F2-data-and-features.py)%20(where%20every%20input%20comes%20from%2C%20every%20store%2C%20every%20candidate%20feature)%20and%20%5B3%20Model%20internals%5D(https%3A%2F%2Fgithub.com%2Ftreefera%2Ftf-model-lab%2Fblob%2Fmain%2Facademy%2F4-our-model-families%2F4.1-area-and-yield%2F3-model-internals.py)%20(the%20production%20code%20step%20by%20step).%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
3ab9c0de0ea81089f9688acef271b896