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%20plotly.express%20as%20px%0A%20%20%20%20from%20IPython.display%20import%20display%0A%20%20%20%20from%20loguru%20import%20logger%0A%20%20%20%20from%20model_foundry.common.quantity_modules%20import%20plots%0A%0A%20%20%20%20from%20labkit%20import%20area%2C%20explore%2C%20figures%0A%20%20%20%20from%20labkit.catalogue%20import%20weather_catalogue%0A%0A%20%20%20%20logger.remove()%20%20%23%20production%20logs%20at%20INFO%0A%20%20%20%20m%20%3D%20area.load(%22area_soybeans_brazil%22%2C%20%22area_soy_brazil%22%2C%20%22ADM0%3ABRA%22%2C%20%22Brazil%22)%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%20Brazil%20soybean%20area%0A%0A%20%20%20%20One%20production%20model%2C%20**model-foundry's%20%60area-soy-brazil%60**%2C%20end%20to%20end%3A%20what%20it%20forecasts%20and%20why%2C%20how%2C%20how%20well%2C%20and%20how%20to%20change%20it.%20Each%20section%20is%20a%20short%20explanation%20and%20a%20view%20you%20can%20drive%3B%20change%20a%20view's%20controls%20and%20marimo%20redraws%20it.%0A%0A%20%20%20%20New%20to%20the%20area%20models%3F%20Start%20with%20%5B4.1%20How%20the%20area%20models%20work%5D(https%3A%2F%2Fgithub.com%2Ftreefera%2Ftf-model-lab%2Fblob%2Fmain%2Facademy%2F4-our-model-families%2F4.1-area-and-yield%2F1-area-models.py).%20The%20data%20and%20feature%20stores%2C%20and%20the%20production%20code%20itself%2C%20are%20in%20the%20two%20deep%20dives%20beside%20it.%0A%0A%20%20%20%20**The%20ideas%20behind%20each%20section**%20are%20taught%20in%20tiers%200%20to%203%20of%20%5BModel%20Academy%5D(https%3A%2F%2Fgithub.com%2Ftreefera%2Ftf-model-lab%2Fblob%2Fmain%2Facademy%2FREADME.md)%2C%20before%20this%20tier.%20Read%20the%20units%20for%20the%20sections%20you%20need%3A%0A%0A%20%20%20%20%7C%20Section%20%7C%20Taught%20in%20%7C%0A%20%20%20%20%7C---%7C---%7C%0A%20%20%20%20%7C%20%C2%A71%20What%20we%20forecast%20%7C%20%5B2.1%20Seasons%20and%20calendars%5D(https%3A%2F%2Fgithub.com%2Ftreefera%2Ftf-model-lab%2Fblob%2Fmain%2Facademy%2F2-time-and-signals%2F2.1-the-season-year-calendar%2Fscience.md%23the-science)%3A%20the%20season%20year%2C%20and%20why%20CONAB's%20%222023%2F24%22%20is%20season%202024%3B%20IBGE%20and%20CONAB%20as%20truth%20will%20be%20unit%201.4%20(planned)%20%7C%0A%20%20%20%20%7C%20%C2%A72%20How%20we%20forecast%20it%20%7C%20%5B2.2%20Trends%5D(https%3A%2F%2Fgithub.com%2Ftreefera%2Ftf-model-lab%2Fblob%2Fmain%2Facademy%2F2-time-and-signals%2F2.2-trends%2FREADME.md)%2C%20%5B3.1%20Boosted%20trees%5D(https%3A%2F%2Fgithub.com%2Ftreefera%2Ftf-model-lab%2Fblob%2Fmain%2Facademy%2F3-model-families%2F3.1-gradient-boosted-trees%2FREADME.md)%2C%20and%203.1's%20%5Bhierarchies%20section%5D(https%3A%2F%2Fgithub.com%2Ftreefera%2Ftf-model-lab%2Fblob%2Fmain%2Facademy%2F3-model-families%2F3.1-gradient-boosted-trees%2Fscience.md%23hierarchies-making-county-state-and-nation-agree)%20for%20the%20levels%20%7C%0A%20%20%20%20%7C%20%C2%A73%20The%20trend%20%7C%20%5B2.2%20Trends%5D(https%3A%2F%2Fgithub.com%2Ftreefera%2Ftf-model-lab%2Fblob%2Fmain%2Facademy%2F2-time-and-signals%2F2.2-trends%2Fscience.md%23extrapolation-the-last-season-is-the-hardest)%20and%20its%20%5Bpoint-in-time%20section%5D(https%3A%2F%2Fgithub.com%2Ftreefera%2Ftf-model-lab%2Fblob%2Fmain%2Facademy%2F2-time-and-signals%2F2.2-trends%2Fscience.md%23point-in-time-never-let-the-trend-see-the-future)%20%7C%0A%20%20%20%20%7C%20%C2%A74%20Features%20%7C%20%5B0.1's%20z-score%5D(https%3A%2F%2Fgithub.com%2Ftreefera%2Ftf-model-lab%2Fblob%2Fmain%2Facademy%2F0-foundations%2F0.1-the-gaussian%2Fscience.md%23job-1-a-yardstick-the-z-score)%2C%20%5B1.3%20Weather%20and%20climate%5D(https%3A%2F%2Fgithub.com%2Ftreefera%2Ftf-model-lab%2Fblob%2Fmain%2Facademy%2F1-data%2F1.3-weather-grids%2FREADME.md)%20for%20ERA5%3B%20season-to-date%20anomalies%20will%20be%20unit%202.3%20(planned)%20%7C%0A%20%20%20%20%7C%20%C2%A75%20Model%20architecture%20%7C%203.1's%20%5Bjoint%20booster%5D(https%3A%2F%2Fgithub.com%2Ftreefera%2Ftf-model-lab%2Fblob%2Fmain%2Facademy%2F3-model-families%2F3.1-gradient-boosted-trees%2Fcode.md%236-the-live-xgboost-path-the-joint-booster)%20and%20%5Bwhere%20its%20level%20weights%20end%20up%5D(https%3A%2F%2Fgithub.com%2Ftreefera%2Ftf-model-lab%2Fblob%2Fmain%2Facademy%2F3-model-families%2F3.1-gradient-boosted-trees%2Fscience.md%23study-question-where-do-the-joint-boosters-level-weights-end-up)%20%7C%0A%20%20%20%20%7C%20%C2%A76%20Train%20and%20test%20%7C%202.2's%20%5Bpoint-in-time%20section%5D(https%3A%2F%2Fgithub.com%2Ftreefera%2Ftf-model-lab%2Fblob%2Fmain%2Facademy%2F2-time-and-signals%2F2.2-trends%2Fscience.md%23point-in-time-never-let-the-trend-see-the-future)%3B%20walk-forward%20evaluation%20in%20%5B0.3%5D(https%3A%2F%2Fgithub.com%2Ftreefera%2Ftf-model-lab%2Fblob%2Fmain%2Facademy%2F0-foundations%2F0.3-evaluation-and-uncertainty%2Fscience.md%23walking-forward)%20%7C%0A%20%20%20%20%7C%20%C2%A77%E2%80%93%C2%A78%20How%20good%2C%20and%20why%20it%20moved%20%7C%20%5B0.1's%20bands%5D(https%3A%2F%2Fgithub.com%2Ftreefera%2Ftf-model-lab%2Fblob%2Fmain%2Facademy%2F0-foundations%2F0.1-the-gaussian%2Fscience.md%23where-it-breaks-for-us)%20and%203.1's%20%5Bcontributions%5D(https%3A%2F%2Fgithub.com%2Ftreefera%2Ftf-model-lab%2Fblob%2Fmain%2Facademy%2F3-model-families%2F3.1-gradient-boosted-trees%2Fscience.md%23what-a-contribution-means)%20%7C%0A%20%20%20%20%7C%20%C2%A79%20Change%20the%20recipe%20%7C%202.1's%20%5Bstage%20anchors%5D(https%3A%2F%2Fgithub.com%2Ftreefera%2Ftf-model-lab%2Fblob%2Fmain%2Facademy%2F2-time-and-signals%2F2.1-the-season-year-calendar%2Fcode.md%23how-recipes-use-it)%20and%203.1's%20%5Brecipe%20choices%5D(https%3A%2F%2Fgithub.com%2Ftreefera%2Ftf-model-lab%2Fblob%2Fmain%2Facademy%2F3-model-families%2F3.1-gradient-boosted-trees%2Fcode.md%235-how-a-recipe-chooses-the-regressor)%20%7C%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%201%20%C2%B7%20What%20we%20forecast%2C%20and%20why%0A%0A%20%20%20%20**What.**%20Brazil's%20soybean%20area%3A%20planted%2C%20harvested%2C%20and%20abandoned%20(planted%20minus%20harvested)%2C%20for%20the%20nation%2C%20each%20state%20and%20each%20municipality%2C%20every%20week%20of%20the%20season%20from%201%20May%20to%2030%20April.%20The%20truth%20is%20IBGE's%20municipal%20survey%20(PAM)%2C%20published%20about%2017%20months%20after%20the%20season%20starts.%0A%0A%20%20%20%20**Why.**%20Production%20is%20area%20times%20yield%2C%20and%20Brazil%20is%20the%20world's%20largest%20soybean%20producer.%20Area%20is%20decided%20at%20sowing%2C%20September%20to%20December%2C%20months%20before%20yield%20is%20known%2C%20so%20it%20is%20the%20first%20part%20of%20the%20season's%20supply%20that%20can%20be%20forecast.%20CONAB%20surveys%20it%20monthly%3B%20the%20aim%20is%20to%20match%20CONAB%20earlier%2C%20between%20its%20surveys%2C%20and%20at%20every%20level%2C%20including%20the%20municipalities%20CONAB%20does%20not%20report.%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s1_we_forecast()%3A%0A%20%20%20%20display(%0A%20%20%20%20%20%20%20%20figures.truth_history(%0A%20%20%20%20%20%20%20%20%20%20%20%20m.national.loc%5B1988%3A%5D%2C%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%22area_planted_ha%22%3A%20%22planted%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22area_harvested_ha%22%3A%20%22harvested%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22area_abandoned_ha%22%3A%20%22abandoned%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22Brazil%20soybean%20area%2C%20IBGE%20PAM%22%2C%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20)%0A%20%20%20%20place_choice%20%3D%20explore.place_controls(m.truth%2C%20m.nation)%0A%20%20%20%20return%20(place_choice%2C)%0A%0A%0A%40app.cell%0Adef%20s1_we_forecast_2(mo%2C%20place_choice)%3A%0A%20%20%20%20mo.vstack(%5Bplace_choice%2C%20explore.place_explorer(m.truth%2C%20m.nation%2C%20place_choice.value)%5D)%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%20%C2%B7%20How%20we%20forecast%20it%0A%0A%20%20%20%20Four%20steps%2C%20each%20a%20choice%20in%20the%20recipe%3A%0A%0A%20%20%20%201.%20**Trend**%3A%20a%20line%20through%20each%20place's%20known%20seasons%20(%C2%A73).%0A%20%20%20%202.%20**Booster**%3A%20one%20model%20corrects%20the%20trend%20from%20features%3A%20weather%2C%20prices%20and%20the%20truth's%20own%20history%20(%C2%A74%2C%20%C2%A75).%0A%20%20%20%203.%20**Surveys**%3A%20each%20CONAB%20survey%20is%20fused%20with%20the%20forecast%20once%20it%20is%20published.%0A%20%20%20%204.%20**Levels**%3A%20municipalities%2C%20states%20and%20the%20nation%20are%20reconciled%20so%20they%20add%20up%2C%20and%20prediction%20intervals%20are%20calibrated%20on%20past%20errors.%0A%0A%20%20%20%20The%20recipe%20below%20is%20every%20choice%20this%20model%20makes.%20Improving%20the%20model%20starts%20with%20changing%20it%20(%C2%A79).%0A%0A%20%20%20%20**Background%3A**%20step%204's%20reconciliation%20is%203.1's%20%5Bhierarchies%20section%5D(https%3A%2F%2Fgithub.com%2Ftreefera%2Ftf-model-lab%2Fblob%2Fmain%2Facademy%2F3-model-families%2F3.1-gradient-boosted-trees%2Fscience.md%23hierarchies-making-county-state-and-nation-agree).%20This%20recipe%20shares%20the%20national%20gap%20among%20states%20by%20%60variance%60%2C%20the%20default%2C%20and%20the%20variance%20it%20passes%20is%20relative%2C%20so%20a%20small%20state%20can%20absorb%20as%20many%20hectares%20as%20a%20large%20one%20(finding%20F10%2C%20%5Bissue%2037%5D(https%3A%2F%2Fgithub.com%2Ftreefera%2Ftf-model-lab%2Fissues%2F37)).%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s2_we_forecast()%3A%0A%20%20%20%20m.recipe_table()%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%20%C2%B7%20First%20step%3A%20the%20trend%0A%0A%20%20%20%20The%20trend%20does%20most%20of%20the%20work%2C%20so%20it%20is%20the%20first%20choice.%20There%20are%20two%20kinds%3A%0A%0A%20%20%20%20-%20**Linear**%3A%20a%20straight%20line%20where%20every%20past%20season%20counts%20the%20same.%0A%20%20%20%20-%20**Exponentially%20weighted%20(ewm)**%3A%20a%20season%20%60half-life%60%20seasons%20older%20counts%20half%20as%20much.%0A%0A%20%20%20%20**The%20trade-off.**%20A%20trend%20that%20forgets%20slowly%20lags%20a%20market%20that%20is%20speeding%20up%3B%20one%20that%20forgets%20fast%20chases%20the%20last%20two%20seasons%20and%20overshoots%20every%20turn.%20And%20a%20trend%20can%20only%20use%20truth%20that%20IBGE%20has%20published%3A%20on%20the%20first%20forecast%20of%20season%20N%2C%20the%20newest%20is%20N%E2%88%922%2C%20so%20the%20line's%20end%20is%20two%20seasons%20stale.%0A%0A%20%20%20%20Every%20option%20below%20is%20walk-forward%20on%20the%20national%20series%2C%20as%20the%20first%20forecast%20of%20each%20season%20sees%20it%3A%0A%0A%20%20%20%20**Background%3A**%20%5B2.2%20Trends%5D(https%3A%2F%2Fgithub.com%2Ftreefera%2Ftf-model-lab%2Fblob%2Fmain%2Facademy%2F2-time-and-signals%2F2.2-trends%2Fscience.md).%20This%20recipe's%20%60ewm_linear%60%20trend%20with%20%60half_life_seasons%3A%203.0%60%20behaves%20like%20a%20half-life%20of%201.5%20seasons%2C%20because%20%60np.polyfit%60's%20weights%20are%20squared%20(finding%20F7%2C%20%5Bissue%2034%5D(https%3A%2F%2Fgithub.com%2Ftreefera%2Ftf-model-lab%2Fissues%2F34)).%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s3_first_step_trend(mo)%3A%0A%20%20%20%20trends%2C%20trend_errors%20%3D%20m.trend_comparison()%0A%20%20%20%20display(%0A%20%20%20%20%20%20%20%20figures.trend_options(%0A%20%20%20%20%20%20%20%20%20%20%20%20m.national%5B%22area_planted_ha%22%5D.loc%5B1995%3A%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20trends%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22National%20planted%20area%20and%20each%20walk-forward%20trend%22%2C%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20)%0A%20%20%20%20display(figures.trend_errors(trend_errors.loc%5B2010%3A%5D%2C%20%22Each%20trend's%20error%2C%20%25%20of%20truth%22))%0A%20%20%20%20mo.output.append(%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%20span%3A%20trend_errors.loc%5Bstart%3A%5D.mean()%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20for%20span%2C%20start%20in%20((%222010%E2%80%93%22%2C%202010)%2C%20(%222015%E2%80%93%22%2C%202015)%2C%20(%222020%E2%80%93%22%2C%202020))%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%20%20%20%20%20.round(2)%0A%20%20%20%20%20%20%20%20.style.highlight_min(axis%3D0%2C%20color%3D%22%23cde8cd%22)%0A%20%20%20%20%20%20%20%20.format(%22%7B%3A.2f%7D%25%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**Try%20it%3A**%20fit%20any%20trend%20to%20any%20state.%20Watch%20a%20short%20half-life%20chase%20and%20a%20long%20one%20lag%3B%20change%20the%20gap%20to%20see%20what%20one%20more%20season%20of%20known%20truth%20would%20be%20worth.%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s3_first_step_trend_2()%3A%0A%20%20%20%20trend_choice%20%3D%20explore.trend_controls(m.places)%0A%20%20%20%20return%20(trend_choice%2C)%0A%0A%0A%40app.cell%0Adef%20s3_first_step_trend_3(mo%2C%20trend_choice)%3A%0A%20%20%20%20mo.vstack(%0A%20%20%20%20%20%20%20%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20trend_choice%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20explore.trend_explorer(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20m.truth.set_index(%5B%22geo_identifier%22%2C%20%22season_year%22%5D)%5B%22area_planted_ha%22%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20m.places%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20range(2001%2C%202027)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20trend_choice.value%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20)%2C%0A%20%20%20%20%20%20%20%20%5D%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%204%20%C2%B7%20Features%0A%0A%20%20%20%20The%20booster%20reads%20three%20kinds%20of%20feature%3A%0A%0A%20%20%20%20-%20**Weather**%3A%20season-to-date%20ERA5%20z-scores%2C%20moving%20every%20week.%0A%20%20%20%20-%20**Prices**%3A%20one%20value%20per%20season%2C%20used%20from%206%20October.%0A%20%20%20%20-%20**Truth%20lags**%3A%20the%20trend%20and%20the%20last%20known%20seasons%2C%20stepping%20on%201%20October%20when%20IBGE's%20newest%20season%20becomes%20usable.%0A%0A%20%20%20%20Pick%20a%20feature%20and%20a%20place%3A%20every%20season's%20path%2C%20the%20chosen%20season%20against%20the%20others%2C%20and%20the%20feature%20against%20the%20trend's%20miss%20on%20a%20chosen%20day.%20The%20panel%20says%20what%20the%20feature%20is%20and%20why%20it%20might%20help.%20Definitions%20and%20the%20stores%20behind%20them%3A%20%5B4.1%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).%0A%0A%20%20%20%20**Background%3A**%20%5B0.1's%20robust%20z-score%5D(https%3A%2F%2Fgithub.com%2Ftreefera%2Ftf-model-lab%2Fblob%2Fmain%2Facademy%2F0-foundations%2F0.1-the-gaussian%2Fscience.md%23job-1-a-yardstick-the-z-score)%20and%20%5B1.3%20Weather%20and%20climate%5D(https%3A%2F%2Fgithub.com%2Ftreefera%2Ftf-model-lab%2Fblob%2Fmain%2Facademy%2F1-data%2F1.3-weather-grids%2FREADME.md).%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s4_features(mo)%3A%0A%20%20%20%20notes%20%3D%20m.feature_notes(mo.notebook_dir()%20%2F%20%22features.yaml%22%2C%20weather_catalogue(%22brazil%22%2C%20%22soy%22))%0A%20%20%20%20feature_choice%20%3D%20explore.feature_season_controls(%0A%20%20%20%20%20%20%20%20m.feature_rows%2C%20m.feature_labels()%2C%20m.places%2C%20m.recipe.season_year%0A%20%20%20%20)%0A%20%20%20%20return%20feature_choice%2C%20notes%0A%0A%0A%40app.cell%0Adef%20s4_features_2(feature_choice%2C%20mo%2C%20notes)%3A%0A%20%20%20%20mo.vstack(%0A%20%20%20%20%20%20%20%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20feature_choice%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20explore.feature_season_view(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20m.feature_rows%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20m.feature_labels()%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20m.places%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20m.recipe.season_year%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20notes%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20feature_choice.value%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20)%2C%0A%20%20%20%20%20%20%20%20%5D%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**When%20does%20each%20feature%20carry%20signal%3F**%20Each%20cell%20is%20a%20feature's%20correlation%20with%20the%20trend's%20miss%20on%20the%20first%20forecast%20date%20of%20a%20month.%20A%20useful%20feature%20is%20strongly%20coloured%20in%20the%20months%20that%20matter%20and%20stays%20so%20from%20month%20to%20month.%20With%20about%2030%20seasons%20at%20the%20nation%2C%20%7Cr%7C%20above%20about%200.35%20is%20unlikely%20by%20chance.%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s4_features_3()%3A%0A%20%20%20%20months%20%3D%20%5B5%2C%206%2C%207%2C%208%2C%209%2C%2010%2C%2011%2C%2012%2C%201%2C%202%2C%203%2C%204%5D%0A%20%20%20%20signal%20%3D%20%5B%0A%20%20%20%20%20%20%20%20*m.model_features%2C%0A%20%20%20%20%20%20%20%20*%5Bc%20for%20c%20in%20m.lag_columns%20if%20c.endswith(%22_planted%22)%20and%20c%20!%3D%20%22trend_mean_planted%22%5D%2C%0A%20%20%20%20%5D%0A%20%20%20%20rows%20%3D%20m.feature_rows.assign(%0A%20%20%20%20%20%20%20%20trend_miss%3D100%0A%20%20%20%20%20%20%20%20*%20(m.feature_rows%5B%22area_planted_ha%22%5D%20%2F%20m.feature_rows%5B%22trend_mean_planted%22%5D%20-%201)%0A%20%20%20%20)%0A%20%20%20%20for%20label%2C%20at%20in%20(%0A%20%20%20%20%20%20%20%20(%22Nation%22%2C%20rows%5B%22geo_identifier%22%5D%20%3D%3D%20m.nation)%2C%0A%20%20%20%20%20%20%20%20(%22States%20pooled%22%2C%20rows%5B%22geo_identifier%22%5D%20!%3D%20m.nation)%2C%0A%20%20%20%20)%3A%0A%20%20%20%20%20%20%20%20display(%0A%20%20%20%20%20%20%20%20%20%20%20%20figures.signal_by_month(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20rows%5Bat%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20signal%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22trend_miss%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20months%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20f%22%7Blabel%7D%3A%20each%20feature%20against%20the%20trend's%20miss%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20)%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%20%23%23%205%20%C2%B7%20Model%20architecture%0A%0A%20%20%20%20One%20XGBoost%20booster%20corrects%20the%20trend%20for%20**planted%20and%20abandoned%20at%20once%2C%20at%20every%20level**%3A%20each%20row%20appears%20twice%2C%20once%20per%20target%2C%20with%20that%20target's%20trend%20and%20lags.%20Its%20loss%20is%20**relative**%2C%20so%20a%201%25%20miss%20weighs%20the%20same%20in%20a%20municipality%20and%20in%20the%20nation%2C%20and%20**levels%20are%20weighted**%20so%20the%20one%20national%20row%20is%20not%20drowned%20by%20the%20cohort's%201%2C458%20municipalities.%20The%20booster%20starts%20from%20zero%2C%20so%20its%20correction%20splits%20exactly%20into%20one%20part%20per%20feature%20(%C2%A78).%20Its%20settings%20are%20fixed%20in%20area-module%2C%20not%20in%20the%20recipe%3A%0A%0A%20%20%20%20**Background%3A**%203.1's%20%5Bjoint%20booster%5D(https%3A%2F%2Fgithub.com%2Ftreefera%2Ftf-model-lab%2Fblob%2Fmain%2Facademy%2F3-model-families%2F3.1-gradient-boosted-trees%2Fcode.md%236-the-live-xgboost-path-the-joint-booster).%20The%20level%20weights%20act%20on%20relative%20error%2C%20so%20in%20the%20units%20the%20trees%20are%20grown%20in%20a%20state%20or%20national%20row%20weighs%20far%20less%20than%20a%20municipality%20(finding%20F6%2C%20%5Bissue%2033%5D(https%3A%2F%2Fgithub.com%2Ftreefera%2Ftf-model-lab%2Fissues%2F33)).%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s5_model_architecture()%3A%0A%20%20%20%20area.architecture()%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%206%20%C2%B7%20Train%20and%20test%3A%20the%20walk-forward%20hindcast%0A%0A%20%20%20%20A%20forecast%20can%20only%20use%20what%20was%20known%20on%20its%20date%2C%20so%20the%20test%20is%20a%20**walk-forward%20hindcast**.%20Every%20season%20from%202001%20is%20forecast%20by%20models%20trained%20only%20on%20seasons%20whose%20truth%20was%20public%20on%20the%20forecast%20date.%20The%20split%20moves%20forward%20a%20season%20at%20a%20time%20and%20no%20model%20sees%20its%20own%20future.%20On%201%20October%20the%20next%20season's%20truth%20arrives%2C%20so%20each%20season's%20forecasts%20switch%20to%20a%20newer%20model%20mid-season.%20Post-processing%20calibrates%20on%20the%20first%20nine%20seasons%2C%20so%20scores%20start%20in%202010.%0A%0A%20%20%20%20**Background%3A**%202.2's%20%5Bpoint-in-time%20section%5D(https%3A%2F%2Fgithub.com%2Ftreefera%2Ftf-model-lab%2Fblob%2Fmain%2Facademy%2F2-time-and-signals%2F2.2-trends%2Fscience.md%23point-in-time-never-let-the-trend-see-the-future).%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s6_train_test_walk()%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%207%20%C2%B7%20How%20good%20is%20it%0A%0A%20%20%20%20The%20delivered%20forecast's%20error%20by%20forecast%20date%20and%20level%2C%20then%20each%20state's%20last%20forecast%20of%20every%20season%20against%20its%20truth%2C%20and%20the%20national%20error%20by%20season%20and%20month.%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s7_good(mo)%3A%0A%20%20%20%20display(plots.skill_by_init_date(m.skill()%2C%20m.planted.quantity))%0A%20%20%20%20display(%0A%20%20%20%20%20%20%20%20plots.predicted_vs_observed(m.last_forecasts(level%3D1)%2C%20m.planted.point%2C%20m.planted.quantity)%0A%20%20%20%20)%0A%20%20%20%20by_month%20%3D%20m.national_errors()%0A%20%20%20%20mean%20%3D%20by_month.attrs%5B%22mean%22%5D%0A%20%20%20%20print(%0A%20%20%20%20%20%20%20%20f%22National%20planted%2C%20delivered%3A%20mean%20absolute%20error%20%7Bmean%3A.2f%7D%25%20over%20%7Blen(by_month)%7D%20seasons%22%0A%20%20%20%20)%0A%20%20%20%20mo.output.append(%0A%20%20%20%20%20%20%20%20px.imshow(%0A%20%20%20%20%20%20%20%20%20%20%20%20by_month%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20color_continuous_scale%3D%22Reds%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20aspect%3D%22auto%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20height%3D480%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20labels%3D%7B%22color%22%3A%20%22error%2C%20%25%22%2C%20%22x%22%3A%20%22forecast%20month%22%2C%20%22y%22%3A%20%22season%22%7D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20title%3D%22National%20planted%2C%20delivered%3A%20absolute%20error%20by%20season%20and%20forecast%20month%22%2C%0A%20%20%20%20%20%20%20%20)%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%208%20%C2%B7%20Predictions%20against%20actuals%20over%20time%0A%0A%20%20%20%20The%20forecast%20made%20on%201%20December%20and%201%20March%20of%20every%20season%20against%20IBGE's%20final%20and%20CONAB's%20latest%20survey%20on%20the%20same%20dates%3B%20then%20how%20the%20delivered%20forecast%20compares%20with%20CONAB%20over%20the%20season.%0A%0A%20%20%20%20**Background%3A**%203.1's%20%5Bcontributions%5D(https%3A%2F%2Fgithub.com%2Ftreefera%2Ftf-model-lab%2Fblob%2Fmain%2Facademy%2F3-model-families%2F3.1-gradient-boosted-trees%2Fscience.md%23what-a-contribution-means).%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s8_predictions_actuals_over()%3A%0A%20%20%20%20steps%20%3D%20%7B%0A%20%20%20%20%20%20%20%20%22model%20(trend%20%2B%20booster)%22%3A%20area.JOINT%2C%0A%20%20%20%20%20%20%20%20%22%2B%20CONAB%20surveys%20fused%22%3A%20f%22%7Barea.JOINT%7D__vintage_fused%22%2C%0A%20%20%20%20%20%20%20%20%22delivered%22%3A%20m.planted.point%2C%0A%20%20%20%20%7D%0A%20%20%20%20path%20%3D%20m.conab(m.national_path(steps))%0A%20%20%20%20dates%20%3D%20%5B%2212-01%22%2C%20%2203-01%22%5D%0A%20%20%20%20display(%0A%20%20%20%20%20%20%20%20figures.forecast_vs_truth(%0A%20%20%20%20%20%20%20%20%20%20%20%20m.on_dates(path%2C%20m.planted.point%2C%20dates)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20m.national%5B%22area_planted_ha%22%5D.loc%5B2010%3A%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22National%20planted%3A%20forecast%20on%20each%20date%20against%20the%20truth%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20m.on_dates(path%2C%20%22conab%22%2C%20dates)%2C%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20)%0A%20%20%20%20seasons%2C%20closer%20%3D%20m.against_conab(path)%0A%20%20%20%20print(%0A%20%20%20%20%20%20%20%20f%22Over%20%7Blen(seasons)%7D%20seasons%3A%20delivered%20%7Bseasons%5B'delivered'%5D.mean()%3A.2f%7D%25%2C%20%22%0A%20%20%20%20%20%20%20%20f%22CONAB%20%7Bseasons%5B'CONAB%20latest%20survey'%5D.mean()%3A.2f%7D%25%3B%20%22%0A%20%20%20%20%20%20%20%20f%22delivered%20closer%20than%20CONAB%20in%20%7Bcloser%3A.0%25%7D%20of%20weeks%22%0A%20%20%20%20)%0A%20%20%20%20display(%0A%20%20%20%20%20%20%20%20figures.error_by_season(%0A%20%20%20%20%20%20%20%20%20%20%20%20seasons%2C%20%22National%20planted%2C%20weeks%20with%20a%20CONAB%20survey%3A%20mean%20absolute%20error%22%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20)%0A%20%20%20%20return%20path%2C%20steps%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**Explore%20every%20season.**%20Pick%20a%20season%20and%20slide%20through%20its%20forecast%20dates%3A%20the%20national%20path%20with%20its%2080%25%20band%2C%20CONAB's%20latest%20survey%20and%20IBGE's%20final%2C%20and%20what%20each%20feature%20added%20to%20the%20trend%20on%20that%20date.%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s8_predictions_actuals_over_2(path)%3A%0A%20%20%20%20parts%20%3D%20m.national_parts()%0A%20%20%20%20season_choice%20%3D%20explore.season_controls(path)%0A%20%20%20%20return%20parts%2C%20season_choice%0A%0A%0A%40app.cell%0Adef%20s8_predictions_actuals_over_3(path%2C%20season_choice)%3A%0A%20%20%20%20init_choice%20%3D%20explore.season_init_controls(path%2C%20int(season_choice.value%5B%22season%22%5D))%0A%20%20%20%20return%20(init_choice%2C)%0A%0A%0A%40app.cell%0Adef%20s8_predictions_actuals_over_4(%0A%20%20%20%20init_choice%2C%0A%20%20%20%20mo%2C%0A%20%20%20%20parts%2C%0A%20%20%20%20path%2C%0A%20%20%20%20season_choice%2C%0A%20%20%20%20steps%2C%0A)%3A%0A%20%20%20%20mo.vstack(%0A%20%20%20%20%20%20%20%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20mo.hstack(%5Bseason_choice%2C%20init_choice%5D%2C%20justify%3D%22start%22)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20explore.season_explorer(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20path%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20steps%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20m.planted.bands%5B80%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20m.national%5B%22area_planted_ha%22%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20parts%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20area.part_features(parts)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20int(season_choice.value%5B%22season%22%5D)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20init_choice.value%5B%22init%22%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20shown%3D(%22model%20(trend%20%2B%20booster)%22%2C%20%22delivered%22)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20)%2C%0A%20%20%20%20%20%20%20%20%5D%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**Where%20the%20national%20miss%20comes%20from.**%20For%20any%20forecast%20month%2C%20each%20season's%20error%20split%20into%20the%20largest%20states'%20errors%20and%20the%20rest.%20A%20state%20with%20a%20bar%20of%20one%20sign%20every%20season%20is%20read%20wrongly%20year%20after%20year.%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s8_predictions_actuals_over_5()%3A%0A%20%20%20%20upper_reported%20%3D%20m.reported%5Bm.reported%5B%22level%22%5D%20%3C%3D%201%5D%0A%20%20%20%20state_choice%20%3D%20explore.state_error_controls(upper_reported%2C%20m.planted.point%2C%20%22area_planted_ha%22)%0A%20%20%20%20return%20state_choice%2C%20upper_reported%0A%0A%0A%40app.cell%0Adef%20s8_predictions_actuals_over_6(mo%2C%20state_choice%2C%20upper_reported)%3A%0A%20%20%20%20mo.vstack(%0A%20%20%20%20%20%20%20%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20state_choice%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20explore.state_error_view(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20upper_reported%2C%20m.planted.point%2C%20%22area_planted_ha%22%2C%20m.nation%2C%20state_choice.value%0A%20%20%20%20%20%20%20%20%20%20%20%20)%2C%0A%20%20%20%20%20%20%20%20%5D%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%209%20%C2%B7%20Change%20the%20recipe%0A%0A%20%20%20%20Every%20improvement%20starts%20as%20a%20recipe%20change%3A%20a%20dictionary%20merged%20into%20the%20shipped%20recipe%20and%20run%20by%20the%20production%20package%20on%20the%20same%20inputs.%20A%20variant%20replays%20the%20hindcast%20from%20%60HINDCAST_FROM%60%20(from%202020%20about%204%20minutes%3B%20from%202001%20about%20ten)%2C%20so%20compare%20variants%20with%20each%20other%2C%20not%20with%20the%20full%20reference%20run.%0A%0A%20%20%20%20Each%20hindcast%20worker%20holds%20about%207%20GB%3A%20on%20the%2030%20GB%20box%20set%20%60HINDCAST_WORKERS%60%20to%203%20at%20most.%20Set%20%60RUN_EXPERIMENTS%20%3D%20True%60%20to%20run.%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s9_change_recipe()%3A%0A%20%20%20%20RUN_EXPERIMENTS%20%3D%20False%0A%20%20%20%20HINDCAST_FROM%20%3D%202020%0A%20%20%20%20variants%20%3D%20%7B%0A%20%20%20%20%20%20%20%20%22shipped%22%3A%20%7B%7D%2C%0A%20%20%20%20%20%20%20%20%22half-life%205%22%3A%20%7B%22model%22%3A%20%7B%22trend%22%3A%20%7B%22method%22%3A%20%22ewm_linear%22%2C%20%22half_life_seasons%22%3A%205.0%7D%7D%7D%2C%0A%20%20%20%20%20%20%20%20%22no%20prices%22%3A%20%7B%22model%22%3A%20%7B%22features%22%3A%20m.weather%7D%7D%2C%0A%20%20%20%20%7D%0A%20%20%20%20if%20RUN_EXPERIMENTS%3A%0A%20%20%20%20%20%20%20%20results%20%3D%20pd.concat(%0A%20%20%20%20%20%20%20%20%20%20%20%20%5Bm.run_variant(name%2C%20change%2C%20HINDCAST_FROM)%20for%20name%2C%20change%20in%20variants.items()%5D%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20display(m.compare(results))%0A%0A%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20app.run()%0A
f37c6cf2348a713433f944bf53298ebd