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%20from%20IPython.display%20import%20display%0A%20%20%20%20from%20loguru%20import%20logger%0A%0A%20%20%20%20from%20labkit%20import%20area%2C%20explore%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%20Model%20internals%0A%0A%20%20%20%20A%20deep%20dive%20into%20the%20production%20code%2C%20step%20by%20step%2C%20with%20Brazil%20soybean%20as%20the%20example.%20%60show(...)%60%20prints%20the%20pinned%20source%20with%20a%20permalink.%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%20Truth%20and%20its%20timing%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20truth_timing()%3A%0A%20%20%20%20from%20area_module.features.joint%20import%20lag_features%2C%20level_trends%2C%20truth_through%0A%20%20%20%20from%20model_foundry.common.quantity_modules.truth%20import%20read_truth%0A%0A%20%20%20%20show(read_truth)%0A%20%20%20%20show(truth_through)%0A%20%20%20%20return%20lag_features%2C%20level_trends%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%20trend%20and%20the%20lag%20features%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20trend_lag_features(lag_features%2C%20level_trends)%3A%0A%20%20%20%20show(level_trends)%0A%20%20%20%20show(lag_features)%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%20joint%20booster%0A%0A%20%20%20%20%60JointResidual%60%20stacks%20each%20row%20twice%2C%20once%20per%20target%2C%20and%20fits%20one%20booster%20on%20the%20trend%20residual%20at%20every%20level.%20Each%20row's%20squared%20error%20is%20divided%20by%20its%20own%20area%20squared%20(then%20scaled)%2C%20and%20levels%20are%20weighted.%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20joint_booster(mo)%3A%0A%20%20%20%20from%20area_module.models.joint%20import%20(%0A%20%20%20%20%20%20%20%20JointResidual%2C%0A%20%20%20%20%20%20%20%20MultiLevelBooster%2C%0A%20%20%20%20%20%20%20%20engineer%2C%0A%20%20%20%20%20%20%20%20loss_coefficients%2C%0A%20%20%20%20)%0A%0A%20%20%20%20show(JointResidual.stacked)%0A%20%20%20%20show(MultiLevelBooster.fit)%0A%20%20%20%20show(loss_coefficients)%0A%20%20%20%20show(engineer)%0A%20%20%20%20mo.output.append(area.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%20The%20walk-forward%20runner%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20walk_forward_runner()%3A%0A%20%20%20%20from%20model_foundry.common.model_packaging.runner%20import%20run_hindcast%0A%0A%20%20%20%20show(run_hindcast)%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%20Contributions%2C%20exactly%0A%0A%20%20%20%20The%20booster%20starts%20from%20zero%2C%20so%20TreeSHAP's%20parts%20plus%20the%20trend%20equal%20the%20model's%20output%20on%20every%20row.%20Checked%20here%20against%20the%20run's%20own%20predictions%3A%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20contributions_exactly()%3A%0A%20%20%20%20parts%20%3D%20m.national_parts()%0A%20%20%20%20check%20%3D%20parts.merge(m.hindcast%5B%5B*area.KEYS%2C%20area.JOINT%5D%5D%2C%20on%3Darea.KEYS)%0A%20%20%20%20features%20%3D%20area.part_features(parts)%0A%20%20%20%20rebuilt%20%3D%20check%5B%22trend%22%5D%20%2B%20check%5Bfeatures%5D.sum(axis%3D1)%0A%20%20%20%20gap%20%3D%20(rebuilt%20-%20check%5Barea.JOINT%5D).abs().max()%0A%20%20%20%20print(f%22largest%20gap%20between%20trend%20%2B%20parts%20and%20the%20run's%20own%20model%20output%3A%20%7Bgap%3A.3f%7D%20ha%22)%0A%20%20%20%20display(%0A%20%20%20%20%20%20%20%20figures.importance(%0A%20%20%20%20%20%20%20%20%20%20%20%20parts%5Bparts%5B%22season_year%22%5D%20%3E%3D%202010%5D%5Bfeatures%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22Mean%20%7Ccontribution%7C%2C%20national%20planted%2C%202010%20onwards%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**How%20the%20booster%20uses%20each%20feature.**%20Every%20row's%20contribution%20against%20the%20feature's%20value%2C%20as%20%25%20of%20the%20trend.%20The%20black%20line%20is%20the%20mean%20contribution%20in%20bins%2C%20the%20red%20the%20mean%20%7Ccontribution%7C.%20Both%20near%20zero%20means%20the%20feature%20is%20barely%20used%3B%20a%20flat%20black%20line%20over%20a%20high%20red%20one%20means%20contributions%20of%20both%20signs%20at%20one%20value%2C%20the%20feature%20acting%20through%20others%20(an%20interaction)%2C%20not%20a%20feature%20left%20unused.%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20contributions_exactly_2()%3A%0A%20%20%20%20upper%20%3D%20m.predict%5Bm.predict%5B%22geo_identifier%22%5D.str.count(%22%2F%22)%20%3C%3D%201%5D%0A%20%20%20%20upper_parts%20%3D%20m.fold_contributions(upper)%0A%20%20%20%20dependence_choice%20%3D%20explore.dependence_controls(%0A%20%20%20%20%20%20%20%20upper_parts%2C%20m.feature_rows%2C%20area.part_features(upper_parts)%2C%20m.places%0A%20%20%20%20)%0A%20%20%20%20return%20dependence_choice%2C%20upper_parts%0A%0A%0A%40app.cell%0Adef%20contributions_exactly_3(dependence_choice%2C%20mo%2C%20upper_parts)%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%20dependence_choice%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20explore.dependence_view(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20upper_parts%2C%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%20area.part_features(upper_parts)%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%20dependence_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%20Post-processing%2C%20step%20by%20step%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20post_processing_step(mo)%3A%0A%20%20%20%20from%20area_module.models.vintage_fusion%20import%20VintageFusion%0A%0A%20%20%20%20show(VintageFusion.predict)%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%22%2B%20projected%22%3A%20f%22%7Barea.JOINT%7D__vintage_fused__projected%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.national_path(steps)%0A%20%20%20%20step_errors%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%20name%3A%20(100%20*%20(path%5Bc%5D%20%2F%20path%5B%22area_planted_ha%22%5D%20-%201).abs())%0A%20%20%20%20%20%20%20%20%20%20%20%20.groupby(path%5B%22season_year%22%5D)%0A%20%20%20%20%20%20%20%20%20%20%20%20.mean()%0A%20%20%20%20%20%20%20%20%20%20%20%20for%20name%2C%20c%20in%20steps.items()%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20)%0A%20%20%20%20display(%0A%20%20%20%20%20%20%20%20figures.error_by_season(step_errors%2C%20%22National%20planted%2C%20mean%20absolute%20error%20at%20each%20step%22)%0A%20%20%20%20)%0A%20%20%20%20mo.output.append(step_errors.mean().round(2).rename(%22mean%20absolute%20error%2C%20%25%22).to_frame())%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%20Do%20the%20bands%20mean%20what%20they%20say%3F%0A%0A%20%20%20%20A%20calibrated%2080%25%20band%20holds%20the%20truth%2080%25%20of%20the%20time.%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20bands_mean_they()%3A%0A%20%20%20%20from%20model_foundry.common.quantity_modules%20import%20plots%0A%0A%20%20%20%20display(plots.coverage_by_level(m.coverage()))%0A%20%20%20%20nation%20%3D%20m.reported%5Bm.reported%5B%22geo_identifier%22%5D%20%3D%3D%20m.nation%5D.dropna(subset%3D%5B%22area_planted_ha%22%5D)%0A%20%20%20%20lower%2C%20upper_band%20%3D%20m.planted.bands%5B80%5D%0A%20%20%20%20inside%20%3D%20nation%5B%22area_planted_ha%22%5D.between(nation%5Blower%5D%2C%20nation%5Bupper_band%5D)%0A%20%20%20%20width%20%3D%20100%20*%20(nation%5Bupper_band%5D%20-%20nation%5Blower%5D)%20%2F%20nation%5Bm.planted.point%5D%0A%20%20%20%20print(%0A%20%20%20%20%20%20%20%20f%22national%2080%25%20band%3A%20holds%20the%20truth%20%7Binside.mean()%3A.0%25%7D%20of%20weeks%3B%20%22%0A%20%20%20%20%20%20%20%20f%22median%20width%20%7Bwidth.median()%3A.1f%7D%25%20of%20the%20forecast%22%0A%20%20%20%20)%0A%0A%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20app.run()%0A
506d1fe766c5668ae55081435b80fa15