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%20tempfile%0A%20%20%20%20from%20pathlib%20import%20Path%0A%0A%20%20%20%20import%20yaml%0A%0A%20%20%20%20%23%20tf-data-ml-utils'%20feature%20stages%20refuse%20to%20import%20without%20a%20spatial%20registry%3B%20production's%20lives%20in%0A%20%20%20%20%23%20treefera-data-pipelines.%20One%20region%20is%20enough%20for%20the%20code%20shown%20here.%0A%20%20%20%20registry_path%20%3D%20Path(tempfile.mkdtemp())%20%2F%20%22spatial_registry.yaml%22%0A%20%20%20%20registry_path.write_text(%0A%20%20%20%20%20%20%20%20yaml.safe_dump(%7B%22spatial_ids%22%3A%20%7B%22brazil%22%3A%20%7B%22bbox%22%3A%20%5B-74.0%2C%20-34.0%2C%20-34.0%2C%206.0%5D%7D%7D%7D)%0A%20%20%20%20)%0A%20%20%20%20os.environ%5B%22WEATHER_EXTRA_SPATIAL_REGISTRY_PATHS%22%5D%20%3D%20str(registry_path)%0A%0A%20%20%20%20import%20matplotlib.pyplot%20as%20plt%0A%20%20%20%20import%20numpy%20as%20np%0A%20%20%20%20import%20pandas%20as%20pd%0A%20%20%20%20from%20scipy%20import%20stats%0A%20%20%20%20from%20scipy.ndimage%20import%20gaussian_filter%0A%20%20%20%20from%20sklearn.ensemble%20import%20HistGradientBoostingClassifier%2C%20HistGradientBoostingRegressor%0A%20%20%20%20from%20sklearn.metrics%20import%20confusion_matrix%2C%20jaccard_score%0A%20%20%20%20from%20sklearn.model_selection%20import%20GroupKFold%2C%20KFold%2C%20TimeSeriesSplit%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%20plt.rcParams%5B%22figure.figsize%22%5D%20%3D%20(9%2C%203.6)%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%200.3%20Evaluation%20and%20uncertainty%0A%0A%20%20%20%20**What%20you'll%20learn**%0A%0A%20%20%20%201.%20How%20to%20score%20a%20forecast%20honestly%3A%20walk%20forward%20through%20the%20seasons%2C%20let%20each%20forecast%20know%20only%20what%20was%20published%20by%20its%20date%2C%20and%20choose%20the%20shape%20of%20the%20split%20to%20match%20how%20the%20model%20will%20be%20used.%0A%20%20%20%202.%20How%20our%20bands%20get%20their%20width%20from%20earlier%20seasons'%20held-out%20errors%20(split%20conformal)%2C%20and%20why%20seasons%2C%20not%20rows%2C%20are%20what%20they%20rest%20on.%0A%20%20%20%203.%20How%20to%20assess%20a%20map%3A%20what%20IoU%20measures%2C%20why%20counting%20mapped%20pixels%20gives%20the%20wrong%20area%2C%20and%20how%20a%20stratified%20reference%20sample%20(Olofsson%20et%20al.)%20corrects%20it%20with%20an%20honest%20standard%20error.%0A%0A%20%20%20%20**How%20to%20run%3A**%20follow%20the%20set-up%20in%20%5Bacademy%2FREADME.md%5D(https%3A%2F%2Fgithub.com%2Ftreefera%2Ftf-model-lab%2Fblob%2Fmain%2Facademy%2FREADME.md%23setting-up)%2C%20then%20from%20%60academy%2F%60%2C%20open%20it%20with%20%60uv%20run%20marimo%20edit%200-foundations%2F0.3-evaluation-and-uncertainty%2Fnotebook.py%60%3B%20marimo%20runs%20the%20cells%20in%20the%20order%20their%20results%20are%20needed%2C%20and%20re-runs%20the%20cells%20that%20depend%20on%20any%20cell%20you%20change.%20Everything%20runs%20offline%20on%20synthetic%20data%20in%20about%20a%20minute.%0A%0A%20%20%20%20The%20production%20code%20is%20imported%2C%20not%20copied%3A%20model-foundry%20at%20%60f69eef9%60%20and%20tf-data-ml-utils%20at%20%60c0ba9dc%60%2C%20pinned%20in%20%60pyproject.toml%60.%20%60show(...)%60%20prints%20the%20real%20source%20with%20a%20permalink.%20tf-deep-landcover%20isn't%20installed%20here%2C%20so%20its%20two%20area-estimation%20functions%20appear%20as%20labelled%2C%20verbatim%20copies%20with%20their%20permalinks.%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%20Splitting%20data%3A%20%60KFold%60%2C%20%60TimeSeriesSplit%60%20and%20%60GroupKFold%60%0A%0A%20%20%20%20scikit-learn's%20%5Bcross-validation%20guide%5D(https%3A%2F%2Fscikit-learn.org%2F1.9%2Fmodules%2Fcross_validation.html)%20starts%20with%20%60KFold%60%3A%20cut%20the%20rows%20into%20k%20parts%2C%20and%20score%20each%20part%20with%20a%20model%20trained%20on%20the%20rest.%20It%20then%20warns%20that%20this%20assumes%20the%20rows%20are%20independent%20and%20identically%20distributed%2C%20and%20offers%20two%20splitters%20for%20when%20they%20aren't%3A%20%60TimeSeriesSplit%60%2C%20which%20only%20ever%20trains%20on%20the%20past%2C%20and%20%60GroupKFold%60%2C%20which%20keeps%20every%20row%20of%20a%20group%20(a%20field%2C%20a%20region%2C%20a%20season)%20on%20one%20side%20of%20the%20split.%20Ten%20rows%2C%20two%20groups%20of%20five%2C%20show%20the%20difference%3A%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s1_1_splitting_data_kfold()%3A%0A%20%20%20%20ten%20%3D%20np.arange(10)%0A%20%20%20%20groups%20%3D%20np.repeat(%5B%22north%22%2C%20%22south%22%5D%2C%205)%0A%20%20%20%20for%20_name%2C%20splitter%2C%20extra%20in%20(%0A%20%20%20%20%20%20%20%20(%22KFold(5%2C%20shuffle%3DTrue)%22%2C%20KFold(5%2C%20shuffle%3DTrue%2C%20random_state%3D0)%2C%20%7B%7D)%2C%0A%20%20%20%20%20%20%20%20(%22TimeSeriesSplit(4)%22%2C%20TimeSeriesSplit(4)%2C%20%7B%7D)%2C%0A%20%20%20%20%20%20%20%20(%22GroupKFold(2)%22%2C%20GroupKFold(2)%2C%20%7B%22groups%22%3A%20groups%7D)%2C%0A%20%20%20%20)%3A%0A%20%20%20%20%20%20%20%20print(_name)%0A%20%20%20%20%20%20%20%20for%20_train%2C%20_test%20in%20splitter.split(ten%2C%20**extra)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20print(f%22%20%20%20train%20%7B_train.tolist()%7D%20%20test%20%7B_test.tolist()%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%20Only%20%60TimeSeriesSplit%60%20never%20trains%20on%20rows%20after%20the%20ones%20it%20tests.%20Only%20%60GroupKFold%60%20never%20has%20the%20same%20group%20on%20both%20sides.%20A%20forecast%20of%20next%20season%20needs%20the%20first%20property%3B%20a%20map%20of%20a%20region%20the%20model%20has%20never%20seen%20needs%20the%20second.%0A%0A%20%20%20%20%23%23%201.2%20Many%20tests%20at%20once%3A%20%60scipy.stats.false_discovery_control%60%0A%0A%20%20%20%20Test%20enough%20candidate%20features%20and%20some%20will%20pass%20at%20p%20%3C%200.05%20by%20chance%20alone.%20%5B%60false_discovery_control%60%5D(https%3A%2F%2Fdocs.scipy.org%2Fdoc%2Fscipy-1.18.0%2Freference%2Fgenerated%2Fscipy.stats.false_discovery_control.html)%20adjusts%20a%20set%20of%20p-values%20by%20the%20Benjamini%E2%80%93Hochberg%20procedure%2C%20so%20that%20keeping%20those%20below%200.05%20holds%20the%20expected%20share%20of%20false%20discoveries%20among%20them%20to%205%20%25.%20Its%20documentation%20uses%20the%20fifteen%20p-values%20from%20Benjamini%20and%20Hochberg's%20own%20paper%3A%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s1_2_many_tests_once()%3A%0A%20%20%20%20bh_example%20%3D%20%5B%0A%20%20%20%20%20%20%20%200.0001%2C%0A%20%20%20%20%20%20%20%200.0004%2C%0A%20%20%20%20%20%20%20%200.0019%2C%0A%20%20%20%20%20%20%20%200.0095%2C%0A%20%20%20%20%20%20%20%200.0201%2C%0A%20%20%20%20%20%20%20%200.0278%2C%0A%20%20%20%20%20%20%20%200.0298%2C%0A%20%20%20%20%20%20%20%200.0344%2C%0A%20%20%20%20%20%20%20%200.0459%2C%0A%20%20%20%20%20%20%20%200.3240%2C%0A%20%20%20%20%20%20%20%200.4262%2C%0A%20%20%20%20%20%20%20%200.5719%2C%0A%20%20%20%20%20%20%20%200.6528%2C%0A%20%20%20%20%20%20%20%200.7590%2C%0A%20%20%20%20%20%20%20%201.000%2C%0A%20%20%20%20%5D%0A%20%20%20%20adjusted%20%3D%20stats.false_discovery_control(bh_example)%0A%20%20%20%20print(%0A%20%20%20%20%20%20%20%20f%22below%200.05%20before%20adjusting%3A%20%7Bsum(p%20%3C%200.05%20for%20p%20in%20bh_example)%7D%3B%20after%3A%20%7Bint((adjusted%20%3C%200.05).sum())%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%20Nine%20of%20the%20fifteen%20pass%20unadjusted%3B%20four%20survive.%200.1%20introduced%20%60np.quantile%60%20and%20its%20methods%2C%20which%20the%20bands%20in%20Part%202%20rely%20on.%0A%0A%20%20%20%20%23%23%201.3%20A%20confidence%20interval%20by%20resampling%3A%20%60scipy.stats.bootstrap%60%0A%0A%20%20%20%20%5B%60scipy.stats.bootstrap%60%5D(https%3A%2F%2Fdocs.scipy.org%2Fdoc%2Fscipy-1.18.0%2Freference%2Fgenerated%2Fscipy.stats.bootstrap.html)%20redraws%20the%20data%20with%20replacement%20many%20times%2C%20recomputes%20the%20statistic%20each%20time%2C%20and%20reads%20an%20interval%20off%20the%20spread.%20Its%20documentation's%20first%20example%20is%20the%20mean%20of%20a%20sample%3A%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s1_3_confidence_interval_resampling()%3A%0A%20%20%20%20sample%20%3D%20np.random.default_rng(0).normal(10%2C%202%2C%2030)%0A%20%20%20%20boot%20%3D%20stats.bootstrap((sample%2C)%2C%20np.mean%2C%20confidence_level%3D0.95%2C%20random_state%3D0)%0A%20%20%20%20print(%0A%20%20%20%20%20%20%20%20f%22mean%20%7Bsample.mean()%3A.2f%7D%2C%2095%25%20interval%20%7Bboot.confidence_interval.low%3A.2f%7D%20to%20%7Bboot.confidence_interval.high%3A.2f%7D%22%0A%20%20%20%20)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20The%20bootstrap%20assumes%20the%20rows%20it%20redraws%20are%20independent.%20Part%204%20shows%20what%20happens%20when%20they%20aren't.%0A%0A%20%20%20%20%23%23%201.4%20Scoring%20a%20map%3A%20the%20confusion%20matrix%20and%20IoU%0A%0A%20%20%20%20%5B%60confusion_matrix%60%5D(https%3A%2F%2Fscikit-learn.org%2F1.9%2Fmodules%2Fgenerated%2Fsklearn.metrics.confusion_matrix.html)%20counts%20each%20(true%2C%20predicted)%20pair%2C%20true%20classes%20down%20the%20rows.%20%5B%60jaccard_score%60%5D(https%3A%2F%2Fscikit-learn.org%2F1.9%2Fmodules%2Fgenerated%2Fsklearn.metrics.jaccard_score.html)%20is%20IoU%2C%20intersection%20over%20union%3A%20for%20one%20class%2C%20TP%20%2F%20(TP%20%2B%20FP%20%2B%20FN).%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s1_4_scoring_map_confusion()%3A%0A%20%20%20%20truth_px%20%3D%20np.array(%5B0%2C%200%2C%200%2C%200%2C%200%2C%200%2C%201%2C%201%2C%201%2C%201%5D)%0A%20%20%20%20mapped_px%20%3D%20np.array(%5B0%2C%200%2C%200%2C%200%2C%201%2C%201%2C%201%2C%201%2C%201%2C%200%5D)%0A%20%20%20%20print(confusion_matrix(truth_px%2C%20mapped_px))%0A%20%20%20%20print(%0A%20%20%20%20%20%20%20%20f%22IoU%20of%20class%201%3A%20%7Bjaccard_score(truth_px%2C%20mapped_px)%3A.3f%7D%20%20(TP%203%20%2F%20(TP%203%20%2B%20FP%202%20%2B%20FN%201))%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%20Part%202%3A%20our%20production%20code%0A%0A%20%20%20%20%23%23%202.1%20Walking%20forward%3A%20%60expanding_season_folds%60%0A%0A%20%20%20%20Every%20area%20and%20yield%20hindcast%20is%20replayed%20by%20%5B%60expanding_season_folds%60%5D(https%3A%2F%2Fgithub.com%2Ftreefera%2Fmodel-foundry%2Fblob%2Ff69eef9df9e6a707c68f0292d940ccfc082cdaf5%2Fmodel_foundry%2Fcommon%2Fmodelling%2Ffolds.py%23L50-L77)%3A%20one%20fold%20per%20season%2C%20trained%20on%20every%20strictly%20earlier%20season.%20With%20%60known_through%60%2C%20a%20season's%20rows%20split%20further%2C%20one%20fold%20per%20value%20of%20that%20column%2C%20each%20trained%20through%20its%20value%3A%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s2_1_walking_forward_expanding()%3A%0A%20%20%20%20from%20model_foundry.common.modelling.folds%20import%20expanding_season_folds%0A%20%20%20%20from%20model_foundry.common.modelling.model_core%20import%20GEO_IDENTIFIER%2C%20INIT_DATE%2C%20SEASON_YEAR%0A%0A%20%20%20%20show(expanding_season_folds)%0A%20%20%20%20return%20GEO_IDENTIFIER%2C%20INIT_DATE%2C%20SEASON_YEAR%2C%20expanding_season_folds%0A%0A%0A%40app.cell%0Adef%20s2_1_walking_forward_expanding_2(SEASON_YEAR%2C%20expanding_season_folds)%3A%0A%20%20%20%20toy%20%3D%20pd.DataFrame(%7BSEASON_YEAR%3A%20np.repeat(np.arange(2015%2C%202025)%2C%202)%2C%20%22known%22%3A%200%7D)%0A%20%20%20%20toy%5B%22known%22%5D%20%3D%20np.where(%0A%20%20%20%20%20%20%20%20np.arange(len(toy))%20%25%202%20%3D%3D%200%2C%20toy%5BSEASON_YEAR%5D%20-%202%2C%20toy%5BSEASON_YEAR%5D%20-%201%0A%20%20%20%20)%0A%20%20%20%20for%20_name%2C%20column%20in%20((%22without%20known_through%22%2C%20None)%2C%20(%22with%20known_through%22%2C%20%22known%22))%3A%0A%20%20%20%20%20%20%20%20print(_name)%0A%20%20%20%20%20%20%20%20for%20fold%20in%20expanding_season_folds(%0A%20%20%20%20%20%20%20%20%20%20%20%20toy%2C%20toy%2C%20%5B2022%2C%202023%2C%202024%5D%2C%20min_train_seasons%3D3%2C%20known_through%3Dcolumn%0A%20%20%20%20%20%20%20%20)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20print(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20f%22%20%20%20season%20%7Bfold.season_year%7D%3A%20trains%20through%20%7Bfold.cutoff%7D%2C%20predicts%20%7Blen(fold.predict_rows)%7D%20row(s)%22%0A%20%20%20%20%20%20%20%20%20%20%20%20)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20Without%20%60known_through%60%2C%20every%20row%20of%202024%20trains%20through%202023.%20With%20it%2C%20the%20row%20whose%20truth%20for%202023%20wasn't%20yet%20published%20trains%20only%20through%202022.%20The%20fold%20raises%20an%20error%20if%20any%20cutoff%20reaches%20its%20own%20season%2C%20so%20a%20column%20that%20would%20leak%20the%20season%20itself%20can't%20slip%20through.%0A%0A%20%20%20%20%23%23%202.2%20What%20a%20forecast%20date%20may%20know%3A%20%60truth_through%60%20and%20%60TRAINED_THROUGH%60%0A%0A%20%20%20%20The%20area%20recipes%20say%20when%20each%20season's%20truth%20is%20first%20published.%20%5B%60truth_through%60%5D(https%3A%2F%2Fgithub.com%2Ftreefera%2Fmodel-foundry%2Fblob%2Ff69eef9df9e6a707c68f0292d940ccfc082cdaf5%2Fmodel_foundry%2Fmodels%2Farea_module%2Fsrc%2Farea_module%2Ffeatures%2Fjoint.py%23L187-L210)%20turns%20that%20into%2C%20for%20each%20row%2C%20the%20last%20season%20whose%20truth%20was%20out%20by%20its%20forecast%20date.%20Brazil%20soybean's%20recipe%20(%5Bsoybeans_bra.yaml%20L21%E2%80%9325%5D(https%3A%2F%2Fgithub.com%2Ftreefera%2Fmodel-foundry%2Fblob%2Ff69eef9df9e6a707c68f0292d940ccfc082cdaf5%2Fmodel_foundry%2Fmodels%2Farea_soy_brazil%2Fsrc%2Farea_soy_brazil%2Fsoybeans_bra.yaml%23L21-L25))%20dates%20IBGE's%20PAM%20from%201%20October%20two%20seasons%20later%2C%20with%20a%20guard%20for%202008%20to%202014%2C%20when%20PAM%20came%20out%20late%3A%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s2_2_forecast_date_may()%3A%0A%20%20%20%20from%20area_module.features.joint%20import%20truth_through%0A%20%20%20%20from%20area_module.recipe%20import%20TruthPublished%0A%20%20%20%20from%20model_foundry.common.season_year%20import%20SeasonYear%0A%0A%20%20%20%20show(truth_through)%0A%20%20%20%20return%20SeasonYear%2C%20TruthPublished%2C%20truth_through%0A%0A%0A%40app.cell%0Adef%20s2_2_forecast_date_may_2(%0A%20%20%20%20GEO_IDENTIFIER%2C%0A%20%20%20%20INIT_DATE%2C%0A%20%20%20%20SEASON_YEAR%2C%0A%20%20%20%20SeasonYear%2C%0A%20%20%20%20TruthPublished%2C%0A%20%20%20%20truth_through%2C%0A)%3A%0A%20%20%20%20pam%20%3D%20TruthPublished(%0A%20%20%20%20%20%20%20%20month_day%3D%2210-01%22%2C%0A%20%20%20%20%20%20%20%20seasons_later%3D2%2C%0A%20%20%20%20%20%20%20%20by_season%3D%7B%0A%20%20%20%20%20%20%20%20%20%20%20%202008%3A%20%2210-17%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%202009%3A%20%2210-21%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%202010%3A%20%2210-27%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%202011%3A%20%2210-27%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%202012%3A%20%2210-26%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%202013%3A%20%2212-17%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%202014%3A%20%2211-06%22%2C%0A%20%20%20%20%20%20%20%20%7D%2C%0A%20%20%20%20)%0A%20%20%20%20soy_season%20%3D%20SeasonYear(%2204-30%22)%0A%20%20%20%20weekly%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%20SEASON_YEAR%3A%202024%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20INIT_DATE%3A%20pd.date_range(%222023-05-01%22%2C%20%222024-04-30%22%2C%20freq%3D%227D%22)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20GEO_IDENTIFIER%3A%20%22ADM0%3ABRA%22%2C%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20)%0A%20%20%20%20weekly_known%20%3D%20truth_through(weekly%2C%20soy_season%2C%20pam)%0A%20%20%20%20first_2022%20%3D%20weekly.loc%5Bweekly_known%20%3D%3D%202022%2C%20INIT_DATE%5D.min()%0A%20%20%20%20print(%0A%20%20%20%20%20%20%20%20f%22season%202024%3A%20known%20through%20%7Bweekly_known.iloc%5B0%5D%7D%3B%20the%20recipe%20dates%202022's%20truth%20from%201%20Oct%202023%2C%20so%20from%20the%20weekly%20forecast%20of%20%7Bfirst_2022%3A%25d%20%25b%20%25Y%7D%2C%202022%22%0A%20%20%20%20)%0A%20%20%20%20print(%0A%20%20%20%20%20%20%20%20f%22share%20of%20the%20season's%20forecast%20dates%20that%20may%20use%202022%3A%20%7B(weekly_known%20%3D%3D%202022).mean()%3A.0%25%7D%22%0A%20%20%20%20)%0A%20%20%20%20late%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%20SEASON_YEAR%3A%202015%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20INIT_DATE%3A%20pd.date_range(%222014-05-01%22%2C%20%222015-04-30%22%2C%20freq%3D%227D%22)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20GEO_IDENTIFIER%3A%20%22ADM0%3ABRA%22%2C%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20)%0A%20%20%20%20late_known%20%3D%20truth_through(late%2C%20soy_season%2C%20pam)%0A%20%20%20%20print(%0A%20%20%20%20%20%20%20%20f%22season%202015%3A%20the%20recipe%20counts%20PAM%202013%20as%20known%20from%2017%20Dec%202014%2C%20the%20day%20after%20its%20release%3B%20first%20forecast%20date%20on%20this%20weekly%20grid%20that%20may%20use%20it%3A%20%7Blate.loc%5Blate_known%20%3D%3D%202013%2C%20INIT_DATE%5D.min()%3A%25d%20%25b%20%25Y%7D%22%0A%20%20%20%20)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20A%20season's%20truth%20can%20arrive%20at%20different%20times%20for%20different%20levels%3A%20US%20corn's%20recipe%20dates%20NASS's%20national%20and%20state%20figures%20from%2013%20January%20and%20its%20county%20figures%20from%201%20March%20(%5Bcorn_usa.yaml%20L18%E2%80%9324%5D(https%3A%2F%2Fgithub.com%2Ftreefera%2Fmodel-foundry%2Fblob%2Ff69eef9df9e6a707c68f0292d940ccfc082cdaf5%2Fmodel_foundry%2Fmodels%2Farea_corn_usa%2Fsrc%2Farea_corn_usa%2Fcorn_usa.yaml%23L18-L24))%2C%20with%20the%202018%20shutdown%20as%20an%20exception.%20%5B%60assemble_joint%60%5D(https%3A%2F%2Fgithub.com%2Ftreefera%2Fmodel-foundry%2Fblob%2Ff69eef9df9e6a707c68f0292d940ccfc082cdaf5%2Fmodel_foundry%2Fmodels%2Farea_module%2Fsrc%2Farea_module%2Ffeatures%2Fjoint.py%23L443-L445)%20therefore%20sets%20%60TRAINED_THROUGH%60%20to%20the%20*minimum*%20over%20levels%20for%20each%20season%20and%20forecast%20date%2C%20and%20the%20area%20module%20hands%20it%20to%20the%20runner%20as%20%60known_through%60.%20One%20booster%20is%20trained%20on%20every%20level%20at%20once%2C%20so%20it%20may%20only%20learn%20from%20labels%20that%20every%20level%20had%20published.%20The%20study%20questions%20in%20%5Bscience.md%5D(https%3A%2F%2Fgithub.com%2Ftreefera%2Ftf-model-lab%2Fblob%2Fmain%2Facademy%2F0-foundations%2F0.3-evaluation-and-uncertainty%2Fscience.md)%20ask%20what%20would%20go%20wrong%20with%20the%20maximum.%0A%0A%20%20%20%20%23%23%202.3%20Post-processing%20learns%20only%20from%20earlier%20folds%3A%20%60run_hindcast%60%0A%0A%20%20%20%20Bands%20and%20survey%20fusion%20are%20fitted%20on%20held-out%20errors%2C%20so%20they%20need%20the%20folds'%20predictions%20first.%20%5B%60run_hindcast%60%5D(https%3A%2F%2Fgithub.com%2Ftreefera%2Fmodel-foundry%2Fblob%2Ff69eef9df9e6a707c68f0292d940ccfc082cdaf5%2Fmodel_foundry%2Fcommon%2Fmodel_packaging%2Frunner.py%23L63-L134)%20fits%20each%20fold's%20post-processing%20on%20the%20held-out%20predictions%20of%20seasons%20up%20to%20that%20fold's%20cutoff%2C%20never%20its%20own%3A%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s2_3_post_processing_learns()%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%20Read%20lines%2097%E2%80%93113%3A%20%60known%60%20holds%20the%20held-out%20predictions%20of%20every%20season%20at%20or%20before%20the%20fold's%20cutoff%2C%20and%20the%20post-processing%20for%20that%20cutoff%20is%20fitted%20on%20them%20alone.%20A%20consequence%20worth%20knowing%3A%20the%20first%20folds%20have%20no%20earlier%20held-out%20seasons%2C%20so%20%60known%60%20is%20empty%20and%20they%20get%20no%20post-processing%20at%20all.%20Their%20hindcast%20rows%20carry%20no%20bands.%20Production%20then%20refits%20the%20post-processing%20on%20every%20season's%20held-out%20errors%20(line%20117)%20for%20the%20live%20model.%0A%0A%20%20%20%20%23%23%202.4%20Features%20must%20be%20point-in-time%20too%0A%0A%20%20%20%20A%20feature%20can%20leak%20the%20future%20as%20surely%20as%20a%20label.%20tf-data-ml-utils'%20climatologies%20avoid%20a%20fixed%20baseline%3A%20%5B%60_trailing_climatology%60%5D(https%3A%2F%2Fgithub.com%2Ftreefera%2Ftf-data-ml-utils%2Fblob%2Fc0ba9dc454993edc5b83c19377559531c8705bdd%2Ftf_data_ml_utils%2Ffeatures%2Fstages%2Fclimo_indices%2Fbuild.py%23L176-L199)%20builds%20one%20climatology%20per%20anchor%20year%20from%20the%20years%20ending%20at%20it%2C%20and%20%5B%60_score_trailing%60%5D(https%3A%2F%2Fgithub.com%2Ftreefera%2Ftf-data-ml-utils%2Fblob%2Fc0ba9dc454993edc5b83c19377559531c8705bdd%2Ftf_data_ml_utils%2Ffeatures%2Fstages%2Fz_indices%2Fprocess.py%23L206-L222)%20scores%20each%20year%20against%20the%20latest%20anchor%20*before*%20it.%20data-pipelines'%20Brazil%20config%20says%20why%3A%20%22a%20fixed%201991-2020%20baseline%20sees%20the%20hindcast%20seasons%22.%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s2_4_features_must_be()%3A%0A%20%20%20%20from%20tf_data_ml_utils.features.stages.z_indices.process%20import%20_score_trailing%0A%0A%20%20%20%20show(_score_trailing)%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%20anchor%20is%20chosen%20by%20*calendar*%20year.%20For%20a%20season%20that%20crosses%20New%20Year%2C%20such%20as%20Brazil%20soy's%2C%20one%20season's%20cumulative%20trajectory%20is%20scored%20against%20one%20baseline%20before%201%20January%20and%20the%20next%20one%20after%20it%2C%20so%20its%20z-score%20can%20step%20at%20New%20Year%20with%20no%20change%20in%20the%20weather.%20That%20is%20a%20discontinuity%2C%20not%20leakage%20(no%20season's%20statistics%20include%20itself)%3A%20finding%20F5%20(%5Bissue%2032%5D(https%3A%2F%2Fgithub.com%2Ftreefera%2Ftf-model-lab%2Fissues%2F32)).%0A%0A%20%20%20%20%5B%60percentile_rank%60%5D(https%3A%2F%2Fgithub.com%2Ftreefera%2Ftf-data-ml-utils%2Fblob%2Fc0ba9dc454993edc5b83c19377559531c8705bdd%2Ftf_data_ml_utils%2Ffeatures%2Fcore%2Fpercentile_rank.py%23L51-L70)%20shows%20the%20other%20common%20slip.%20It%20leaves%20the%20scored%20season%20out%20of%20its%20own%20baseline%2C%20but%20keeps%20every%20*later*%20season%20in%20it%3A%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s2_4_features_must_be_2()%3A%0A%20%20%20%20import%20xarray%20as%20xr%0A%20%20%20%20from%20tf_data_ml_utils.features.core.percentile_rank%20import%20percentile_rank%0A%0A%20%20%20%20show(percentile_rank)%0A%20%20%20%20rank_values%20%3D%20xr.DataArray(%0A%20%20%20%20%20%20%20%20np.arange(2000%2C%202025%2C%20dtype%3Dfloat)%2C%0A%20%20%20%20%20%20%20%20dims%3D%22season_year%22%2C%0A%20%20%20%20%20%20%20%20coords%3D%7B%22season_year%22%3A%20np.arange(2000%2C%202025)%7D%2C%0A%20%20%20%20)%0A%20%20%20%20print(%0A%20%20%20%20%20%20%20%20f%22a%20steadily%20rising%20series%2C%20season%202012%20ranked%20by%20percentile_rank%3A%20%7Bpercentile_rank(rank_values%2C%20season_year%3D2012)%3A.0f%7D%22%0A%20%20%20%20)%0A%20%20%20%20print(%0A%20%20%20%20%20%20%20%20f%22ranked%20against%20the%20seasons%20known%20in%202012%20only%3A%20%7B100%20*%20(rank_values.sel(season_year%3Dslice(None%2C%202011))%20%3C%202012).mean().item()%3A.0f%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%20It%20has%20no%20callers%20yet.%20Within%20one%20series%20this%20rank%20is%20order-preserving%3A%20it%20rises%20with%20the%20value%20whatever%20the%20baseline%2C%20so%20a%20model%20refitted%20in%20each%20fold%20would%20learn%20the%20same%20splits%20from%20it.%20It%20misleads%20wherever%20the%20number%20itself%20is%20read%20as%20%22how%20unusual%20was%20this%22%3A%20an%20alert%20such%20as%20%22rank%20above%2090%22%2C%20or%20a%20reader%20comparing%20places.%20In%20a%20hindcast%20of%20such%20an%20alert%2C%202012%20would%20count%20as%20a%20middling%20season%20when%2C%20against%20everything%20known%20in%202012%2C%20it%20was%20a%20record.%0A%0A%20%20%20%20%23%23%202.5%20Folds%20that%20refuse%20to%20leak%3A%20%60WalkForwardFold%60%0A%0A%20%20%20%20The%20crop%20classifier's%20field%20bake%20builds%20its%20folds%20in%20tf-data-ml-utils.%20%5B%60WalkForwardFold%60%5D(https%3A%2F%2Fgithub.com%2Ftreefera%2Ftf-data-ml-utils%2Fblob%2Fc0ba9dc454993edc5b83c19377559531c8705bdd%2Ftf_data_ml_utils%2Fsentinel2_l2a%2Fstreaming%2Fwalk_forward.py%23L25-L43)%20refuses%2C%20when%20it%20is%20constructed%2C%20any%20training%20year%20at%20or%20after%20the%20test%20year%2C%20and%20three%20more%20checks%20refuse%20a%20record%2C%20an%20array%20of%20records%20or%20a%20shard%20manifest%20that%20carries%20a%20test-year%20scene%3A%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s2_5_folds_refuse_leak()%3A%0A%20%20%20%20from%20tf_data_ml_utils.sentinel2_l2a.streaming.walk_forward%20import%20WalkForwardFold%0A%0A%20%20%20%20show(WalkForwardFold.__post_init__)%0A%20%20%20%20print(WalkForwardFold(test_year%3D2024%2C%20train_years%3D(2021%2C%202022%2C%202023)).id)%0A%20%20%20%20try%3A%0A%20%20%20%20%20%20%20%20WalkForwardFold(test_year%3D2024%2C%20train_years%3D(2022%2C%202023%2C%202024))%0A%20%20%20%20except%20ValueError%20as%20refused%3A%0A%20%20%20%20%20%20%20%20print(f%22ValueError%3A%20%7Brefused%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%20%23%23%202.6%20Bands%20from%20held-out%20errors%3A%20%60ConformalByLevel%60%0A%0A%20%20%20%20The%20bands%20of%20every%20yield%20and%20production%20run%2C%20and%20of%20area's%20non-joint%20chain%2C%20come%20from%20%5B%60ConformalByLevel%60%5D(https%3A%2F%2Fgithub.com%2Ftreefera%2Fmodel-foundry%2Fblob%2Ff69eef9df9e6a707c68f0292d940ccfc082cdaf5%2Fmodel_foundry%2Fcommon%2Fquantity_modules%2Fbands.py%23L48-L126)%20or%20a%20subclass%3A%20for%20each%20admin%20level%20and%20forecast%20month-day%2C%20the%20half-width%20at%20coverage%20q%20is%20the%20q-quantile%20of%20earlier%20seasons'%20absolute%20held-out%20errors%2C%20and%20a%20group%20needs%20%60warm_up_seasons%60%20seasons%20before%20it%20gets%20a%20width.%20Area's%20joint%20model%20uses%20its%20own%20%60ScaledQuantileBands%60%20instead%20(4.2).%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s2_6_bands_held_out()%3A%0A%20%20%20%20from%20model_foundry.common.quantity_modules.bands%20import%20(%0A%20%20%20%20%20%20%20%20ConformalByLevel%2C%0A%20%20%20%20%20%20%20%20ConformalRelativeByLevel%2C%0A%20%20%20%20)%0A%0A%20%20%20%20show(ConformalByLevel.fit)%0A%20%20%20%20show(ConformalByLevel.predict)%0A%20%20%20%20return%20ConformalByLevel%2C%20ConformalRelativeByLevel%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%20details%20matter%20below.%20The%20quantile%20is%20pandas'%20default%2C%20linear%20interpolation%2C%20with%20no%20(n%20%2B%201)%20correction.%20And%20it%20pools%20every%20row%20of%20a%20group%3A%20two%20hundred%20counties%20in%20one%20season%20count%20as%20two%20hundred%20errors.%0A%0A%20%20%20%20%23%23%202.7%20Assessing%20a%20map%20in%20tf-deep-landcover%0A%0A%20%20%20%20tf-deep-landcover's%20evaluation%20stage%20reports%20IoU%20per%20class%2C%20and%20two%20summaries%3A%20macro%20IoU%2C%20the%20mean%20over%20classes%20present%20in%20the%20truth%2C%20and%20micro%20IoU%2C%20which%20pools%20every%20class's%20counts%20(%5B%60eval%2Fprocess.py%60%20L121%E2%80%93157%5D(https%3A%2F%2Fgithub.com%2Ftreefera%2Ftf-deep-landcover%2Fblob%2F98df5f742f5c8ebec3061c80f123fa4966095b6c%2Fsrc%2Fstages%2Feval%2Fprocess.py%23L121-L157)).%20Pooled%2C%20every%20misclassified%20pixel%20is%20one%20false%20positive%20and%20one%20false%20negative%2C%20so%20micro%20IoU%20%3D%20TP%20%2F%20(TP%20%2B%202%C2%B7errors)%20%3D%20acc%20%2F%20(2%20%E2%88%92%20acc)%3A%20a%20function%20of%20overall%20accuracy%20alone%2C%20dominated%20by%20the%20commonest%20class.%0A%0A%20%20%20%20Its%20model%20package%20also%20holds%20an%20Olofsson%20et%20al.%20(2014)%20stratified%20area%20estimator%2C%20%5B%60cdl%2Feval%2Folofsson.py%60%5D(https%3A%2F%2Fgithub.com%2Ftreefera%2Ftf-deep-landcover%2Fblob%2F98df5f742f5c8ebec3061c80f123fa4966095b6c%2Fshared_packages%2Fmodels%2Fcdl%2Feval%2Folofsson.py%23L41-L282)%2C%20with%20the%20analytic%20standard%20error%20of%20their%20equation%2010%20and%20a%20field-cluster%20bootstrap.%20It%20has%20no%20callers%20anywhere.%20Here%20are%20its%20two%20core%20functions%2C%20verbatim%20from%20%5BL147%E2%80%93214%5D(https%3A%2F%2Fgithub.com%2Ftreefera%2Ftf-deep-landcover%2Fblob%2F98df5f742f5c8ebec3061c80f123fa4966095b6c%2Fshared_packages%2Fmodels%2Fcdl%2Feval%2Folofsson.py%23L147-L214)%3A%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s2_7_assessing_map_tf()%3A%0A%20%20%20%20%23%20Verbatim%20copy%3A%20tf-deep-landcover%2098df5f7%2C%20shared_packages%2Fmodels%2Fcdl%2Feval%2Folofsson.py%20L147-214.%0A%20%20%20%20%23%20tf-deep-landcover%20isn't%20installed%20in%20this%20environment.%0A%20%20%20%20def%20_within_stratum_confusion(%0A%20%20%20%20%20%20%20%20pred_sample%3A%20np.ndarray%2C%0A%20%20%20%20%20%20%20%20ref_sample%3A%20np.ndarray%2C%0A%20%20%20%20%20%20%20%20area_sample%3A%20np.ndarray%2C%0A%20%20%20%20%20%20%20%20n_classes%3A%20int%2C%0A%20%20%20%20%20%20%20%20area_weighted%3A%20bool%2C%0A%20%20%20%20)%20-%3E%20tuple%5Bnp.ndarray%2C%20np.ndarray%5D%3A%0A%20%20%20%20%20%20%20%20%22%22%22Return%20(q%2C%20n_h)%20where%20q%5Bh%2C%20j%5D%20%3D%20fraction%20of%20stratum-h%20sample%20that%20is%20ref%20j.%22%22%22%0A%20%20%20%20%20%20%20%20q%20%3D%20np.zeros((n_classes%2C%20n_classes)%2C%20dtype%3Dnp.float64)%0A%20%20%20%20%20%20%20%20n_h%20%3D%20np.zeros(n_classes%2C%20dtype%3Dnp.float64)%0A%20%20%20%20%20%20%20%20for%20h%20in%20range(n_classes)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20in_h%20%3D%20pred_sample%20%3D%3D%20h%0A%20%20%20%20%20%20%20%20%20%20%20%20n_h%5Bh%5D%20%3D%20in_h.sum()%0A%20%20%20%20%20%20%20%20%20%20%20%20if%20n_h%5Bh%5D%20%3D%3D%200%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20continue%0A%20%20%20%20%20%20%20%20%20%20%20%20w%20%3D%20area_sample%5Bin_h%5D%20if%20area_weighted%20else%20np.ones(int(n_h%5Bh%5D))%0A%20%20%20%20%20%20%20%20%20%20%20%20denom%20%3D%20w.sum()%0A%20%20%20%20%20%20%20%20%20%20%20%20if%20denom%20%3C%3D%200%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20continue%0A%20%20%20%20%20%20%20%20%20%20%20%20for%20j%20in%20range(n_classes)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20q%5Bh%2C%20j%5D%20%3D%20w%5Bref_sample%5Bin_h%5D%20%3D%3D%20j%5D.sum()%20%2F%20denom%0A%20%20%20%20%20%20%20%20return%20q%2C%20n_h%0A%0A%20%20%20%20def%20_point_and_analytic_se(%0A%20%20%20%20%20%20%20%20pred_sample%2C%0A%20%20%20%20%20%20%20%20ref_sample%2C%0A%20%20%20%20%20%20%20%20area_sample%2C%0A%20%20%20%20%20%20%20%20mapped%2C%0A%20%20%20%20%20%20%20%20n_classes%2C%0A%20%20%20%20%20%20%20%20area_weighted%2C%0A%20%20%20%20%20%20%20%20notes%2C%0A%20%20%20%20)%20-%3E%20tuple%5Bnp.ndarray%2C%20np.ndarray%5D%3A%0A%20%20%20%20%20%20%20%20a_total%20%3D%20float(mapped.sum())%0A%20%20%20%20%20%20%20%20if%20a_total%20%3C%3D%200%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20np.zeros(n_classes)%2C%20np.zeros(n_classes)%0A%20%20%20%20%20%20%20%20W%20%3D%20mapped%20%2F%20a_total%20%20%23%20stratum%20weights%0A%20%20%20%20%20%20%20%20q%2C%20n_h%20%3D%20_within_stratum_confusion(%0A%20%20%20%20%20%20%20%20%20%20%20%20pred_sample%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20ref_sample%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20area_sample%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20n_classes%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20area_weighted%2C%0A%20%20%20%20%20%20%20%20)%0A%0A%20%20%20%20%20%20%20%20%23%20Strata%20that%20are%20mapped%20(W%3E0)%20but%20have%20no%20reference%20sample%20%E2%86%92%20cannot%20correct%3B%20fall%20back%20to%20the%20map%20(q_hh%20%3D%201)%20and%20flag%20it.%0A%20%20%20%20%20%20%20%20for%20h%20in%20range(n_classes)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20if%20W%5Bh%5D%20%3E%200%20and%20n_h%5Bh%5D%20%3D%3D%200%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20q%5Bh%2C%20h%5D%20%3D%201.0%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20notes.append(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20f%22stratum%20%7Bh%7D%20has%20mapped%20area%20but%200%20reference%20fields%20%E2%80%94%20%22%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20f%22using%20uncorrected%20map%20area%20for%20it.%22%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20)%0A%0A%20%20%20%20%20%20%20%20%23%20Area%20proportion%20of%20class%20j%3A%20p_j%20%3D%20%CE%A3_h%20W_h%20q_hj.%0A%20%20%20%20%20%20%20%20p%20%3D%20W%20%40%20q%20%20%23%20(n_classes%2C)%0A%20%20%20%20%20%20%20%20area%20%3D%20a_total%20*%20p%0A%0A%20%20%20%20%20%20%20%20%23%20Olofsson%202014%20eq%2010%3A%20S(p_j)%20%3D%20sqrt(%20%CE%A3_h%20W_h%C2%B2%20q_hj(1-q_hj)%2F(n_h%20-%201)%20).%0A%20%20%20%20%20%20%20%20se_p%20%3D%20np.zeros(n_classes)%0A%20%20%20%20%20%20%20%20for%20j%20in%20range(n_classes)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20var%20%3D%200.0%0A%20%20%20%20%20%20%20%20%20%20%20%20for%20h%20in%20range(n_classes)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20if%20n_h%5Bh%5D%20%3E%201%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20var%20%2B%3D%20(W%5Bh%5D%20**%202)%20*%20q%5Bh%2C%20j%5D%20*%20(1.0%20-%20q%5Bh%2C%20j%5D)%20%2F%20(n_h%5Bh%5D%20-%201.0)%0A%20%20%20%20%20%20%20%20%20%20%20%20se_p%5Bj%5D%20%3D%20np.sqrt(var)%0A%20%20%20%20%20%20%20%20area_se%20%3D%20a_total%20*%20se_p%0A%20%20%20%20%20%20%20%20return%20area%2C%20area_se%0A%0A%20%20%20%20olofsson_area%20%3D%20_point_and_analytic_se%0A%20%20%20%20return%20(olofsson_area%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%20Each%20stratum%20is%20a%20mapped%20class.%20W_h%20is%20its%20share%20of%20the%20mapped%20area%2C%20q_hj%20the%20share%20of%20its%20reference%20sample%20that%20is%20really%20class%20j%2C%20so%20the%20corrected%20share%20of%20class%20j%20is%20%CE%A3_h%20W_h%20q_hj.%20Part%203%20puts%20it%20to%20work.%0A%0A%20%20%20%20%23%20Part%203%3A%20the%20science%0A%0A%20%20%20%20%23%23%203.1%20Why%20a%20random%20split%20flatters%20a%20forecast%0A%0A%20%20%20%20Two%20hundred%20counties%20over%20thirty%20seasons%3A%20each%20county%20has%20its%20own%20level%2C%20every%20county%20shares%20the%20trend%20and%20that%20season's%20weather%20shock%20(sd%2010%20bu%2Fac)%2C%20and%20each%20also%20has%20its%20own%20noise%20(sd%205).%20A%20booster%20reads%20the%20season%20and%20the%20county's%20early%20level.%20Scored%20by%20a%20random%20five-fold%20split%2C%20and%20then%20walked%20forward%20through%20the%20production%20splitter%2C%20beside%20a%20plain%20linear%20regression%20on%20the%20same%20two%20features%3A%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s3_1_random_split_flatters(SEASON_YEAR%2C%20expanding_season_folds)%3A%0A%20%20%20%20panel_rng%20%3D%20np.random.default_rng(3)%0A%20%20%20%20panel_seasons%20%3D%20np.arange(1995%2C%202025)%0A%20%20%20%20season_shock%20%3D%20panel_rng.normal(0%2C%2010%2C%20panel_seasons.size)%0A%20%20%20%20county_level%20%3D%20panel_rng.normal(0%2C%2015%2C%20200)%0A%20%20%20%20panel%20%3D%20pd.DataFrame(%0A%20%20%20%20%20%20%20%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20c%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20s%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20150%20%2B%20county_level%5Bc%5D%20%2B%201.8%20*%20(s%20-%202010)%20%2B%20season_shock%5Bi%5D%20%2B%20panel_rng.normal(0%2C%205)%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%20for%20c%20in%20range(200)%0A%20%20%20%20%20%20%20%20%20%20%20%20for%20i%2C%20s%20in%20enumerate(panel_seasons)%0A%20%20%20%20%20%20%20%20%5D%2C%0A%20%20%20%20%20%20%20%20columns%3D%5B%22county%22%2C%20SEASON_YEAR%2C%20%22y%22%5D%2C%0A%20%20%20%20)%0A%20%20%20%20panel%5B%22county_mean%22%5D%20%3D%20panel.groupby(%22county%22)%5B%22y%22%5D.transform(lambda%20v%3A%20v.iloc%5B%3A5%5D.mean())%0A%20%20%20%20features%20%3D%20%5BSEASON_YEAR%2C%20%22county_mean%22%5D%0A%0A%20%20%20%20def%20rmse(errors)%3A%0A%20%20%20%20%20%20%20%20return%20float(np.sqrt(np.mean(np.square(errors))))%0A%0A%20%20%20%20from%20sklearn.linear_model%20import%20LinearRegression%0A%0A%20%20%20%20scored_rows%20%3D%20panel%5BSEASON_YEAR%5D%20%3E%3D%202005%20%20%23%20both%20splits%20score%20the%20same%20seasons%0A%20%20%20%20split_rows%20%3D%20%5B%5D%0A%20%20%20%20for%20_name%2C%20_make%20in%20(%0A%20%20%20%20%20%20%20%20(%22booster%22%2C%20lambda%3A%20HistGradientBoostingRegressor(max_iter%3D200%2C%20random_state%3D0))%2C%0A%20%20%20%20%20%20%20%20(%22linear%20regression%22%2C%20LinearRegression)%2C%0A%20%20%20%20)%3A%0A%20%20%20%20%20%20%20%20_shuffled%20%3D%20np.empty(len(panel))%0A%20%20%20%20%20%20%20%20for%20_train%2C%20_test%20in%20KFold(5%2C%20shuffle%3DTrue%2C%20random_state%3D0).split(panel)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20_shuffled%5B_test%5D%20%3D%20(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20_make()%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20.fit(panel.iloc%5B_train%5D%5Bfeatures%5D%2C%20panel%5B%22y%22%5D.iloc%5B_train%5D)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20.predict(panel.iloc%5B_test%5D%5Bfeatures%5D)%0A%20%20%20%20%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20_walk%20%3D%20%5B%5D%0A%20%20%20%20%20%20%20%20for%20_fold%20in%20expanding_season_folds(%0A%20%20%20%20%20%20%20%20%20%20%20%20panel%2C%20panel%2C%20list(range(2005%2C%202025))%2C%20min_train_seasons%3D10%0A%20%20%20%20%20%20%20%20)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20_model%20%3D%20_make().fit(_fold.train_rows%5Bfeatures%5D%2C%20_fold.train_rows%5B%22y%22%5D)%0A%20%20%20%20%20%20%20%20%20%20%20%20_walk.append(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20_fold.predict_rows.assign(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20error%3D_fold.predict_rows%5B%22y%22%5D%20-%20_model.predict(_fold.predict_rows%5Bfeatures%5D)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20_walk%20%3D%20pd.concat(_walk%2C%20ignore_index%3DTrue)%0A%20%20%20%20%20%20%20%20if%20_name%20%3D%3D%20%22booster%22%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20walk_errors%20%3D%20_walk%0A%20%20%20%20%20%20%20%20split_rows.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%22model%22%3A%20_name%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22random%205-fold%20RMSE%22%3A%20rmse((panel%5B%22y%22%5D%20-%20_shuffled)%5Bscored_rows%5D)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22walk-forward%20RMSE%22%3A%20rmse(_walk%5B%22error%22%5D)%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%20print(%0A%20%20%20%20%20%20%20%20f%22RMS%20of%20the%20scored%20seasons'%20shocks%3A%20%7Bnp.sqrt(np.mean(season_shock%5Bpanel_seasons%20%3E%3D%202005%5D%20**%202))%3A.2f%7D%20bu%2Fac%22%0A%20%20%20%20)%0A%20%20%20%20pd.DataFrame(split_rows).round(2)%0A%20%20%20%20return%20rmse%2C%20walk_errors%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%20things%20go%20wrong%20at%20once.%0A%0A%20%20%20%20-%20**The%20random%20split%20flatters.**%20It%20puts%20other%20counties%20of%20the%20*same*%20season%20in%20training%2C%20so%20the%20booster%20learns%20that%20season's%20shock%20from%20them%20and%20appears%20to%20forecast%20it.%20No%20forecast%20ever%20has%20that.%20Walking%20forward%2C%20the%20shocks%20are%20unforeseeable%3A%20the%20scored%20seasons'%20shocks%20have%20an%20RMS%20of%207.5%20bu%2Fac%2C%20so%20with%20the%20county%20noise%20no%20model%20can%20beat%20about%20%E2%88%9A(7.5%C2%B2%20%2B%205%C2%B2)%20%E2%89%88%209.0.%20The%20booster%20does%20worse%20still%2C%20because%20with%20the%20season%20as%20a%20feature%20it%20can%20only%20extrapolate%20by%20copying%20the%20latest%20seasons'%20level%2C%20shocks%20included.%0A%20%20%20%20-%20**It%20picks%20the%20wrong%20model.**%20The%20random%20split%20ranks%20the%20booster%20well%20ahead%20of%20the%20linear%20regression%3B%20walking%20forward%2C%20the%20linear%20regression%20wins.%20A%20model%20chosen%20by%20a%20random%20split%20is%20the%20one%20best%20at%20a%20task%20we%20never%20ask%20of%20it.%0A%0A%20%20%20%20That%20is%20why%20every%20hindcast%20in%20model-foundry%20walks%20forward.%0A%0A%20%20%20%20%23%23%203.2%20Point-in-time%20truth%3A%20the%20season%20before%20isn't%20out%20yet%0A%0A%20%20%20%20%60truth_through%60%20showed%20that%2C%20through%20September%2C%20a%20Brazil%20soy%20forecast%20for%20season%202024%20can't%20use%202022's%20truth.%20Suppose%20a%20season's%20anomaly%20persists%2C%20a(s)%20%3D%200.6%20%C2%B7%20a(s%20%E2%88%92%201)%20%2B%20noise%2C%20and%20a%20model%20reads%20the%20last%20known%20season's%20anomaly%20as%20a%20feature%2C%20as%20area's%20lag%20features%20do.%20Reading%20last%20season's%20looks%20skilful%3B%20at%20the%20forecast%20date%20only%20the%20season%20before%20last%20is%20published%3A%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s3_2_point_time_truth(rmse)%3A%0A%20%20%20%20persistence_rng%20%3D%20np.random.default_rng(7)%0A%20%20%20%20anomaly%20%3D%20np.zeros(200_000)%20%20%23%20long%20enough%20that%20the%20RMSEs%20settle%20on%20their%20theoretical%20values%0A%20%20%20%20for%20_i%20in%20range(1%2C%20anomaly.size)%3A%0A%20%20%20%20%20%20%20%20anomaly%5B_i%5D%20%3D%200.6%20*%20anomaly%5B_i%20-%201%5D%20%2B%20persistence_rng.normal(0%2C%201)%0A%20%20%20%20unpublished%20%3D%20anomaly%5B2%3A%5D%20-%200.6%20*%20anomaly%5B1%3A-1%5D%0A%20%20%20%20published%20%3D%20anomaly%5B2%3A%5D%20-%200.36%20*%20anomaly%5B%3A-2%5D%0A%20%20%20%20print(f%22RMSE%20from%20last%20season%20(not%20yet%20published)%3A%20%7Brmse(unpublished)%3A.2f%7D%22)%0A%20%20%20%20print(f%22RMSE%20from%20the%20season%20before%20(published)%3A%20%20%20%7Brmse(published)%3A.2f%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%20In%20theory%20the%20errors%20are%201%20and%20%E2%88%9A(1%20%2B%200.6%C2%B2)%20%E2%89%88%201.17%2C%20so%20a%20hindcast%20that%20ignores%20the%20publication%20date%20reports%20an%20error%20about%2014%20%25%20smaller%20than%20the%20model%20can%20deliver.%20(On%20a%20few%20dozen%20seasons%20the%20gap%20is%20noisy%2C%20and%20can%20even%20reverse.)%20Area%20gates%20its%20lag%20features%20with%20%60truth_through%60.%20The%20yield%20module%20has%20no%20%60known_through%60%20(finding%20F9%2C%20%5Bissue%2036%5D(https%3A%2F%2Fgithub.com%2Ftreefera%2Ftf-model-lab%2Fissues%2F36))%3A%20its%20folds%20train%20one%20season%20more%20than%20was%20published%2C%20and%20its%20trends%20run%20through%20last%20season.%20That%20is%20a%20related%20leak%2C%20whose%20size%20is%20not%20yet%20measured.%0A%0A%20%20%20%20%23%23%203.3%20Point-in-time%20features%3A%20a%20baseline%20that%20sees%20the%20hindcast%0A%0A%20%20%20%20A%20warming%20place%20and%20a%20steady%20neighbour%2C%20each%20scored%20as%20z%20against%20a%20fixed%201991%E2%80%932020%20baseline%20and%20against%20a%20trailing%20baseline%20of%20the%2030%20years%20before%20each%20year%2C%20as%20%60_score_trailing%60%20does%3A%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s3_3_point_time_features()%3A%0A%20%20%20%20baseline_rng%20%3D%20np.random.default_rng(11)%0A%20%20%20%20years%20%3D%20np.arange(1981%2C%202025)%0A%20%20%20%20temperature%20%3D%200.03%20*%20(years%20-%201981)%20%2B%20baseline_rng.normal(0%2C%200.3%2C%20years.size)%0A%20%20%20%20steady%20%3D%20baseline_rng.normal(0%2C%200.3%2C%20years.size)%20%20%23%20a%20neighbouring%20place%20with%20no%20trend%0A%20%20%20%20in_fixed%20%3D%20(years%20%3E%3D%201991)%20%26%20(years%20%3C%3D%202020)%0A%20%20%20%20decade%20%3D%20(years%20%3E%3D%202001)%20%26%20(years%20%3C%3D%202010)%0A%0A%20%20%20%20def%20z_scores(series)%3A%0A%20%20%20%20%20%20%20%20_fixed%20%3D%20(series%20-%20series%5Bin_fixed%5D.mean())%20%2F%20series%5Bin_fixed%5D.std()%0A%20%20%20%20%20%20%20%20_trailing%20%3D%20np.full(years.size%2C%20np.nan)%0A%20%20%20%20%20%20%20%20for%20_i%2C%20_year%20in%20enumerate(years)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20_past%20%3D%20(years%20%3E%3D%20_year%20-%2030)%20%26%20(years%20%3C%20_year)%0A%20%20%20%20%20%20%20%20%20%20%20%20if%20_past.sum()%20%3E%3D%2010%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20_trailing%5B_i%5D%20%3D%20(series%5B_i%5D%20-%20series%5B_past%5D.mean())%20%2F%20series%5B_past%5D.std()%0A%20%20%20%20%20%20%20%20return%20_fixed%2C%20_trailing%0A%0A%20%20%20%20z_fixed%2C%20z_trailing%20%3D%20z_scores(temperature)%0A%20%20%20%20steady_fixed%2C%20steady_trailing%20%3D%20z_scores(steady)%0A%20%20%20%20print(%0A%20%20%20%20%20%20%20%20f%222001-2010%20mean%20z%2C%20warming%20place%3A%20fixed%201991-2020%20baseline%20%7Bz_fixed%5Bdecade%5D.mean()%3A%2B.2f%7D%2C%20trailing%20baseline%20%7Bnp.nanmean(z_trailing%5Bdecade%5D)%3A%2B.2f%7D%22%0A%20%20%20%20)%0A%20%20%20%20print(%0A%20%20%20%20%20%20%20%20f%222001-2010%20mean%20z%2C%20steady%20place%3A%20%20fixed%201991-2020%20baseline%20%7Bsteady_fixed%5Bdecade%5D.mean()%3A%2B.2f%7D%2C%20trailing%20baseline%20%7Bnp.nanmean(steady_trailing%5Bdecade%5D)%3A%2B.2f%7D%22%0A%20%20%20%20)%0A%20%20%20%20_fig%2C%20_ax%20%3D%20plt.subplots()%0A%20%20%20%20_ax.plot(years%2C%20z_fixed%2C%20label%3D%22fixed%201991-2020%20baseline%22)%0A%20%20%20%20_ax.plot(years%2C%20z_trailing%2C%20label%3D%22trailing%2030-year%20baseline%22)%0A%20%20%20%20_ax.axhline(0%2C%20color%3D%22grey%22%2C%20lw%3D0.8)%0A%20%20%20%20_ax.set(xlabel%3D%22year%22%2C%20ylabel%3D%22z%22%2C%20title%3D%22The%20same%20temperatures%2C%20scored%20against%20two%20baselines%22)%0A%20%20%20%20_ax.legend()%0A%20%20%20%20plt.show()%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20For%20the%20warming%20place%20the%20two%20baselines%20disagree%20on%20the%20sign%3A%20against%201991%E2%80%932020%2C%20which%20already%20holds%20the%20warmer%202010s%2C%20the%202000s%20look%20cool%3B%20against%20what%20a%20forecaster%20in%202005%20could%20have%20known%2C%20they%20were%20warm.%0A%0A%20%20%20%20For%20one%20place%20on%20its%20own%2C%20that%20need%20not%20leak%3A%20the%20fixed-baseline%20z%20is%20one%20shift%20and%20rescale%20of%20the%20temperature%2C%20so%20a%20model%20refitted%20in%20each%20fold%20learns%20the%20same%20thing%20from%20either.%20It%20leaks%20when%20the%20number%20is%20compared%20*across%20places*%20or%20read%20against%20a%20threshold.%20Pooled%20with%20the%20steady%20place%2C%20the%20warming%20place's%202000s%20score%20below%20its%20neighbour's%20under%20the%20fixed%20baseline%2C%20because%20its%20baseline%20already%20contains%20its%20own%20warmer%20future%2C%20though%20against%20each%20place's%20past%20it%20was%20the%20warmer%20of%20the%20two.%20One%20model%20pooling%20places%20with%20different%20trends%2C%20a%20clip%20at%20%60z_max%60%2C%20or%20an%20alert%20at%20z%20%3E%202%20all%20read%20that%20leaked%20future.%20That%20is%20the%20case%20the%20data-pipelines%20comment%20guards%20against.%0A%0A%20%20%20%20%23%23%203.4%20The%20shape%20of%20the%20split%20in%20space%0A%0A%20%20%20%20A%20crop%20map's%20errors%20are%20spatially%20correlated%3A%20a%20field%20misread%20is%20usually%20next%20to%20another.%20A%20classifier%20given%20a%20pixel's%20coordinates%20and%20a%20noisy%20signal%2C%20on%20a%20120%20%C3%97%20120%20grid%20whose%20crop%20patches%20are%20smooth%2C%20scored%20with%20a%20random%20split%20and%20with%2020%20%C3%97%2020-pixel%20blocks%20held%20out%20whole%20(scikit-learn's%20%60GroupKFold%60%20here%3B%20tf-deep-landcover's%20sampler%20and%20%60fr_crop_type%60's%20evaluation%20build%20their%20own%20spatial-block%20folds).%20Then%20the%20same%20without%20the%20coordinates%3A%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s3_4_shape_split_space()%3A%0A%20%20%20%20map_rng%20%3D%20np.random.default_rng(7)%0A%20%20%20%20_n%20%3D%20120%0A%20%20%20%20field%20%3D%20gaussian_filter(map_rng.normal(size%3D(_n%2C%20_n))%2C%206)%0A%20%20%20%20field%20%3D%20(field%20-%20field.mean())%20%2F%20field.std()%0A%20%20%20%20_yy%2C%20_xx%20%3D%20np.mgrid%5B0%3A_n%2C%200%3A_n%5D%0A%20%20%20%20crop%20%3D%20(field%20%2B%20map_rng.normal(0%2C%200.5%2C%20field.shape)%20%3E%200.8).astype(int)%0A%20%20%20%20pixels_x%20%3D%20np.column_stack(%0A%20%20%20%20%20%20%20%20%5B_xx.ravel()%2C%20_yy.ravel()%2C%20field.ravel()%20%2B%20map_rng.normal(0%2C%201.5%2C%20field.size)%5D%0A%20%20%20%20)%0A%20%20%20%20pixels_y%20%3D%20crop.ravel()%0A%20%20%20%20blocks%20%3D%20(_yy%20%2F%2F%2020%20*%206%20%2B%20_xx%20%2F%2F%2020).ravel()%0A%20%20%20%20split_accuracy%20%3D%20%7B%7D%0A%20%20%20%20for%20_features%2C%20_x%20in%20((%22coordinates%20and%20signal%22%2C%20pixels_x)%2C%20(%22signal%20only%22%2C%20pixels_x%5B%3A%2C%202%3A%5D))%3A%0A%20%20%20%20%20%20%20%20for%20_name%2C%20_split%20in%20(%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22random%20split%22%2C%20KFold(5%2C%20shuffle%3DTrue%2C%20random_state%3D0).split(_x))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(%22spatial%20blocks%22%2C%20GroupKFold(5).split(_x%2C%20pixels_y%2C%20blocks))%2C%0A%20%20%20%20%20%20%20%20)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20_hit%20%3D%20np.empty(pixels_y.size)%0A%20%20%20%20%20%20%20%20%20%20%20%20for%20_train%2C%20_test%20in%20_split%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20_clf%20%3D%20HistGradientBoostingClassifier(max_iter%3D150%2C%20random_state%3D0).fit(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20_x%5B_train%5D%2C%20pixels_y%5B_train%5D%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_hit%5B_test%5D%20%3D%20_clf.predict(_x%5B_test%5D)%20%3D%3D%20pixels_y%5B_test%5D%0A%20%20%20%20%20%20%20%20%20%20%20%20split_accuracy%5B(_features%2C%20_name)%5D%20%3D%20_hit.mean()%0A%20%20%20%20print(%0A%20%20%20%20%20%20%20%20f%22crop%20share%20%7Bpixels_y.mean()%3A.3f%7D%2C%20so%20always%20answering%20'no%20crop'%20scores%20%7B1%20-%20pixels_y.mean()%3A.3f%7D%22%0A%20%20%20%20)%0A%20%20%20%20pd.Series(split_accuracy).unstack().round(3)%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%20a%20random%20split%2C%20every%20test%20pixel%20has%20training%20neighbours%20a%20pixel%20away%2C%20and%20the%20coordinates%20let%20the%20classifier%20copy%20them.%20Held%20out%20in%20blocks%2C%20it%20must%20generalise%20to%20a%20patch%20it%20hasn't%20seen%2C%20which%20is%20the%20job%20a%20crop%20map%20does%20in%20a%20new%20county%3B%20there%20it%20barely%20beats%20always%20answering%20%22no%20crop%22.%20Without%20the%20coordinates%20the%20two%20splits%20agree%3A%20a%20random%20split%20flatters%20only%20a%20model%20whose%20features%20carry%20location%20(coordinates%2C%20context%20windows%2C%20smooth%20covariates).%20Edge%20pixels%20of%20a%20held-out%20block%20still%20have%20training%20neighbours%20across%20the%20border%2C%20so%20careful%20designs%20also%20leave%20a%20buffer%20between%20blocks%20(Roberts%20et%20al.%202017).%20The%20right%20split%20mirrors%20the%20use%3A%20walk%20forward%20in%20time%20for%20a%20forecast%2C%20hold%20out%20space%20for%20a%20map%20of%20unseen%20places%2C%20and%20both%20for%20a%20classifier%20mapping%20next%20year%20in%20a%20new%20region%20(%60crop_classifier_s2%60%20requires%20training%20years%20at%20most%20test%20%E2%88%92%202).%0A%0A%20%20%20%20%23%23%203.5%20Is%20the%20skill%20real%3F%0A%0A%20%20%20%20Forty-five%20seasons%2C%20a%20predictor%20with%20a%20modest%20real%20signal%2C%20scored%20leave-one-out%20against%20climatology%20(each%20season's%20mean%20of%20the%20others)%2C%20with%20a%20bootstrap%20interval%20on%20the%20skill%20score%3A%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s3_5_skill_real()%3A%0A%20%20%20%20skill_rng%20%3D%20np.random.default_rng(7)%0A%20%20%20%20seasons_45%20%3D%2045%0A%20%20%20%20outcome%20%3D%20skill_rng.normal(0%2C%201%2C%20seasons_45)%0A%20%20%20%20predictor%20%3D%200.45%20*%20outcome%20%2B%20skill_rng.normal(0%2C%200.9%2C%20seasons_45)%0A%20%20%20%20loo_forecast%20%3D%20np.empty(seasons_45)%0A%20%20%20%20for%20_i%20in%20range(seasons_45)%3A%0A%20%20%20%20%20%20%20%20_keep%20%3D%20np.arange(seasons_45)%20!%3D%20_i%0A%20%20%20%20%20%20%20%20loo_forecast%5B_i%5D%20%3D%20np.polyval(%0A%20%20%20%20%20%20%20%20%20%20%20%20np.polyfit(predictor%5B_keep%5D%2C%20outcome%5B_keep%5D%2C%201)%2C%20predictor%5B_i%5D%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20climatology%20%3D%20np.array(%5Bnp.delete(outcome%2C%20_i).mean()%20for%20_i%20in%20range(seasons_45)%5D)%0A%0A%20%20%20%20def%20skill_score(index)%3A%0A%20%20%20%20%20%20%20%20return%201%20-%20np.mean((outcome%5Bindex%5D%20-%20loo_forecast%5Bindex%5D)%20**%202)%20%2F%20np.mean(%0A%20%20%20%20%20%20%20%20%20%20%20%20(outcome%5Bindex%5D%20-%20climatology%5Bindex%5D)%20**%202%0A%20%20%20%20%20%20%20%20)%0A%0A%20%20%20%20skill_draws%20%3D%20%5Bskill_score(skill_rng.integers(0%2C%20seasons_45%2C%20seasons_45))%20for%20_%20in%20range(5000)%5D%0A%20%20%20%20print(%0A%20%20%20%20%20%20%20%20f%22leave-one-out%20skill%20score%20%7Bskill_score(np.arange(seasons_45))%3A%2B.3f%7D%3B%2095%25%20bootstrap%20interval%20%7Bnp.percentile(skill_draws%2C%202.5)%3A%2B.3f%7D%20to%20%7Bnp.percentile(skill_draws%2C%2097.5)%3A%2B.3f%7D%22%0A%20%20%20%20)%0A%20%20%20%20null_p%20%3D%20np.array(%0A%20%20%20%20%20%20%20%20%5Bstats.pearsonr(skill_rng.normal(size%3Dseasons_45)%2C%20outcome)%5B1%5D%20for%20_%20in%20range(60)%5D%0A%20%20%20%20)%0A%20%20%20%20print(%0A%20%20%20%20%20%20%20%20f%2260%20features%20with%20no%20signal%3A%20%7Bint((null_p%20%3C%200.05).sum())%7D%20pass%20p%20%3C%200.05%3B%20Benjamini-Hochberg%20at%205%25%20keeps%20%7Bint((stats.false_discovery_control(null_p)%20%3C%200.05).sum())%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%20Leave-one-out%20is%20honest%20here%20only%20because%20these%20synthetic%20seasons%20are%20independent%20and%20have%20no%20trend%3B%20on%20real%20seasons%2C%20walk%20forward.%20The%20population%20skill%20of%20this%20predictor%20is%20about%200.2%20(0.45%C2%B2%20%2F%20(0.45%C2%B2%20%2B%200.81))%2C%20so%20%2B0.35%20is%20a%20lucky%20draw%2C%20and%20a%20positive%20skill%20score%20on%2045%20seasons%20can%20sit%20inside%20an%20interval%20that%20includes%20zero%3A%20the%20season-to-season%20noise%20in%20a%20score%20is%20large%20when%20the%20score%20rests%20on%20a%20few%20dozen%20seasons.%20And%20screening%20many%20candidate%20features%20finds%20some%20that%20pass%20by%20chance%3A%20one%20in%20twenty%20at%20p%20%3C%200.05%20on%20average%2C%20three%20of%2060%20(one%20in%20this%20draw).%20The%20Benjamini%E2%80%93Hochberg%20adjustment%20from%201.2%20controls%20the%20share%20of%20false%20discoveries%20among%20those%20kept%3A%20sort%20the%20m%20p-values%2C%20find%20the%20largest%20k%20whose%20p-value%20is%20at%20most%200.05%C2%B7k%2Fm%2C%20and%20keep%20the%20k%20smallest.%20It%20assumes%20the%20tests%20are%20independent%20or%20positively%20dependent%3B%20for%20correlated%20store%20variables%2C%20%60method%3D%22by%22%60%20is%20the%20safer%20choice.%0A%0A%20%20%20%20%23%23%203.6%20Bands%20that%20cover%3A%20seasons%2C%20not%20rows%0A%0A%20%20%20%20Thirty%20seasons%20of%20held-out%20errors%20for%20200%20municipalities%2C%20each%20season%20with%20a%20shared%20shock%20(sd%208)%20and%20each%20municipality%20its%20own%20noise%20(sd%204)%2C%20as%20%60ConformalByLevel%60%20sees%20them.%20For%20each%20of%20the%20last%2020%20seasons%2C%20bands%20are%20fitted%20on%20the%20seasons%20before%20it%2C%20as%20%60run_hindcast%60%20would%2C%20and%20scored%20on%20that%20season.%20Thirty%20seasons%20is%20a%20small%20sample%2C%20so%20the%20experiment%20is%20repeated%20twelve%20times%3A%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s3_6_bands_cover_seasons(%0A%20%20%20%20ConformalByLevel%2C%0A%20%20%20%20GEO_IDENTIFIER%2C%0A%20%20%20%20INIT_DATE%2C%0A%20%20%20%20SEASON_YEAR%2C%0A)%3A%0A%20%20%20%20municipalities%20%3D%20%5Bf%22ADM0%3ABRA%2FADM1%3AS%7Bc%20%25%2010%7D%2FADM2%3AC%7Bc%7D%22%20for%20c%20in%20range(200)%5D%0A%0A%20%20%20%20def%20season_coverage(seed)%3A%0A%20%20%20%20%20%20%20%20%23%20Fit%2080%25%20bands%20for%20each%20of%20the%20last%2020%20seasons%20on%20the%20seasons%20before%20it%3B%20return%20each%20season's%20coverage.%0A%20%20%20%20%20%20%20%20_rng%20%3D%20np.random.default_rng(seed)%0A%20%20%20%20%20%20%20%20_held%20%3D%20pd.concat(%0A%20%20%20%20%20%20%20%20%20%20%20%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20pd.DataFrame(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20GEO_IDENTIFIER%3A%20municipalities%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%20SEASON_YEAR%3A%20_season%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20INIT_DATE%3A%20pd.Timestamp(f%22%7B_season%7D-01-15%22)%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%22point%22%3A%20100.0%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%22truth%22%3A%20100.0%20%2B%20_rng.normal(0%2C%208)%20%2B%20_rng.normal(0%2C%204%2C%20200)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20for%20_season%20in%20range(1995%2C%202025)%0A%20%20%20%20%20%20%20%20%20%20%20%20%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20ignore_index%3DTrue%2C%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20_coverage%20%3D%20%7B%7D%0A%20%20%20%20%20%20%20%20for%20_season%20in%20range(2005%2C%202025)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20_bands%20%3D%20ConformalByLevel(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20source%3D%22point%22%2C%20truth%3D%22truth%22%2C%20coverages%3D%5B0.8%5D%2C%20warm_up_seasons%3D5%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_bands.fit(_held%5B_held%5BSEASON_YEAR%5D%20%3C%20_season%5D)%0A%20%20%20%20%20%20%20%20%20%20%20%20_scored%20%3D%20_bands.predict(_held%5B_held%5BSEASON_YEAR%5D%20%3D%3D%20_season%5D)%0A%20%20%20%20%20%20%20%20%20%20%20%20_lower%2C%20_upper%20%3D%20_bands.bands()%5B80%5D%0A%20%20%20%20%20%20%20%20%20%20%20%20_coverage%5B_season%5D%20%3D%20(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20(_scored%5B%22truth%22%5D%20%3E%3D%20_scored%5B_lower%5D)%20%26%20(_scored%5B%22truth%22%5D%20%3C%3D%20_scored%5B_upper%5D)%0A%20%20%20%20%20%20%20%20%20%20%20%20).mean()%0A%20%20%20%20%20%20%20%20return%20pd.Series(_coverage)%0A%0A%20%20%20%20replications%20%3D%20pd.concat(%7Bseed%3A%20season_coverage(seed)%20for%20seed%20in%20range(12)%7D)%0A%20%20%20%20print(%0A%20%20%20%20%20%20%20%20f%2280%25%20bands%2C%2012%20replications%20x%2020%20seasons%3A%20share%20of%20all%20rows%20covered%20%7Breplications.mean()%3A.2f%7D%22%0A%20%20%20%20)%0A%20%20%20%20print(%0A%20%20%20%20%20%20%20%20f%22seasons%20covering%20more%20than%2095%25%20of%20municipalities%3A%20%7B(replications%20%3E%200.95).mean()%3A.0%25%7D%3B%20fewer%20than%20half%3A%20%7B(replications%20%3C%200.5).mean()%3A.0%25%7D%22%0A%20%20%20%20)%0A%20%20%20%20_fig%2C%20_ax%20%3D%20plt.subplots()%0A%20%20%20%20_ax.bar(replications%5B0%5D.index%2C%20replications%5B0%5D.values)%0A%20%20%20%20_ax.axhline(0.8%2C%20color%3D%22black%22%2C%20ls%3D%22--%22%2C%20lw%3D1)%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%22share%20of%20municipalities%20covered%22%2C%0A%20%20%20%20%20%20%20%20title%3D%2280%25%20bands%20in%20one%20replication%3A%20right%20on%20average%2C%20rarely%20right%20in%20a%20season%22%2C%0A%20%20%20%20)%0A%20%20%20%20plt.show()%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20Over%20every%20row%20of%20every%20replication%20the%20bands%20cover%2080%20%25%2C%20as%20designed.%20Within%20a%20season%20they%20seldom%20do%3A%20a%20calm%20season%20covers%20nearly%20every%20municipality%20and%20a%20shock%20season%20few%20of%20them%2C%20because%20one%20shock%20moves%20all%20200%20errors%20together.%20Two%20consequences%3A%0A%0A%20%20%20%20-%20**A%20band%20says%20how%20often%20a%20row%20falls%20inside%20over%20many%20seasons**%2C%20not%20that%2080%20%25%20of%20this%20season's%20municipalities%20will.%20Users%20reading%20a%20map%20of%20bands%20for%20one%20season%20should%20be%20told%20so.%0A%20%20%20%20-%20**The%20quantile%20rests%20on%20seasons%2C%20not%20rows.**%20Two%20hundred%20rows%20per%20season%20look%20like%20thousands%20of%20draws%3B%20for%20the%20shared%20part%20of%20the%20error%20there%20are%20only%20as%20many%20as%20there%20are%20seasons.%0A%0A%20%20%20%200.1%20measured%20the%20rest%20of%20the%20story%20(%5Bscience.md%2C%20%22Where%20it%20breaks%20for%20us%22%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))%3A%20with%20few%20calibration%20draws%20a%20plain%20quantile%20under-covers%2C%20and%20the%20split-conformal%20rank%20%E2%8C%88(n%20%2B%201)%C2%B7q%E2%8C%89%20restores%20the%20guarantee%20as%20long%20as%20new%20errors%20are%20exchangeable%20with%20the%20old.%20Here%20n%20is%20the%20number%20of%20seasons%2C%20not%20of%20rows.%20An%20exceptional%20season%20drawn%20from%20the%20same%20distribution%20as%20the%20rest%20doesn't%20break%20the%20guarantee%3A%20the%2080%20%25%20covers%20rare%20extremes%20too%2C%20on%20average%20over%20seasons.%20What%20breaks%20it%20is%20a%20change%20in%20how%20errors%20arise%2C%20such%20as%20a%20trend%20in%20the%20errors%20or%20a%20new%20model%20or%20data%20source%2C%20and%20no%20rank%20can%20repair%20that.%0A%0A%20%20%20%20%23%23%203.7%20Assessing%20a%20map%3A%20count%20pixels%2C%20or%20sample%20them%3F%0A%0A%20%20%20%20A%20map%20of%20200%2C000%20pixels%2C%205%20%25%20of%20them%20crop.%20The%20classifier%20finds%20three-quarters%20of%20the%20crop%20and%20calls%203%20%25%20of%20the%20rest%20crop%2C%20so%20the%20map%20shows%3A%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s3_7_assessing_map_count()%3A%0A%20%20%20%20area_rng%20%3D%20np.random.default_rng(7)%0A%20%20%20%20n_pixels%20%3D%20200_000%0A%20%20%20%20true_crop%20%3D%20area_rng.random(n_pixels)%20%3C%200.05%0A%20%20%20%20mapped_crop%20%3D%20np.where(%0A%20%20%20%20%20%20%20%20true_crop%2C%20area_rng.random(n_pixels)%20%3C%200.75%2C%20area_rng.random(n_pixels)%20%3C%200.03%0A%20%20%20%20)%0A%20%20%20%20print(%0A%20%20%20%20%20%20%20%20f%22true%20crop%20share%20%7Btrue_crop.mean()%3A.4f%7D%3B%20mapped%20crop%20share%20%7Bmapped_crop.mean()%3A.4f%7D%20(pixel%20counting)%22%0A%20%20%20%20)%0A%20%20%20%20print(f%22crop%20hidden%20among%20pixels%20mapped%20non-crop%3A%20%7Btrue_crop%5B~mapped_crop%5D.mean()%3A.4f%7D%22)%0A%20%20%20%20return%20mapped_crop%2C%20n_pixels%2C%20true_crop%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%20Counting%20mapped%20pixels%20overstates%20the%20area%20by%20about%20a%20third%3A%20the%20false%20alarms%20in%20the%20large%20non-crop%20area%20outnumber%20the%20crop%20the%20map%20missed.%20The%20map's%20errors%20don't%20cancel%3B%20they%20depend%20on%20how%20big%20each%20class%20is.%0A%0A%20%20%20%20Olofsson%20et%20al.%20(2014)%20fix%20this%20with%20a%20reference%20sample%20drawn%20from%20each%20mapped%20class%20(each%20*stratum*).%20Here%2C%20a%20fresh%20sample%20of%20100%20reference%20pixels%20per%20stratum%2C%201%2C000%20times%2C%20and%20how%20often%20the%2095%20%25%20interval%2C%20%C2%B11.96%20standard%20errors%2C%20holds%20the%20true%20area%3A%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s3_7_assessing_map_count_2(%0A%20%20%20%20mapped_crop%2C%0A%20%20%20%20n_pixels%2C%0A%20%20%20%20olofsson_area%2C%0A%20%20%20%20true_crop%2C%0A)%3A%0A%20%20%20%20mapped_areas%20%3D%20np.array(%5B(~mapped_crop).sum()%2C%20mapped_crop.sum()%5D%2C%20dtype%3Dfloat)%0A%20%20%20%20strata%20%3D%20%5Bnp.flatnonzero(mapped_crop%20%3D%3D%20h)%20for%20h%20in%20(0%2C%201)%5D%0A%0A%20%20%20%20def%20olofsson_repeats(n_noncrop%2C%20n_crop%2C%20repeats%3D1000%2C%20seed%3D0)%3A%0A%20%20%20%20%20%20%20%20_rng%20%3D%20np.random.default_rng(seed)%20%20%23%20its%20own%20draws%2C%20the%20same%20whichever%20cells%20ran%20before%0A%20%20%20%20%20%20%20%20shares%2C%20ses%2C%20covered%2C%20empty%20%3D%20%5B%5D%2C%20%5B%5D%2C%200%2C%200%0A%20%20%20%20%20%20%20%20for%20_%20in%20range(repeats)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20_idx%20%3D%20np.concatenate(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20_rng.choice(strata%5B0%5D%2C%20n_noncrop%2C%20replace%3DFalse)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20_rng.choice(strata%5B1%5D%2C%20n_crop%2C%20replace%3DFalse)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%5D%0A%20%20%20%20%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20%20%20%20%20_area%2C%20_se%20%3D%20olofsson_area(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20mapped_crop%5B_idx%5D.astype(int)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20true_crop%5B_idx%5D.astype(int)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20np.ones(_idx.size)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20mapped_areas%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%202%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20False%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%5B%5D%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%20shares.append(_area%5B1%5D%20%2F%20n_pixels)%0A%20%20%20%20%20%20%20%20%20%20%20%20ses.append(_se%5B1%5D%20%2F%20n_pixels)%0A%20%20%20%20%20%20%20%20%20%20%20%20covered%20%2B%3D%20abs(_area%5B1%5D%20-%20true_crop.sum())%20%3C%3D%201.96%20*%20_se%5B1%5D%0A%20%20%20%20%20%20%20%20%20%20%20%20empty%20%2B%3D%20true_crop%5B_idx%5B%3An_noncrop%5D%5D.sum()%20%3D%3D%200%0A%20%20%20%20%20%20%20%20return%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%22reference%20pixels%20(non-crop%2C%20crop)%22%3A%20f%22%7Bn_noncrop%7D%2C%20%7Bn_crop%7D%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22mean%20share%22%3A%20np.mean(shares)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22Monte%20Carlo%20SE%20of%20the%20mean%22%3A%20np.std(shares)%20%2F%20np.sqrt(repeats)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22sd%20of%20estimates%22%3A%20np.std(shares)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22mean%20SE%22%3A%20np.mean(ses)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%2295%25%20interval%20covers%22%3A%20covered%20%2F%20repeats%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22non-crop%20samples%20with%20no%20crop%22%3A%20empty%20%2F%20repeats%2C%0A%20%20%20%20%20%20%20%20%7D%0A%0A%20%20%20%20pd.DataFrame(%0A%20%20%20%20%20%20%20%20%5Bolofsson_repeats(100%2C%20100)%2C%20olofsson_repeats(400%2C%20100)%2C%20olofsson_repeats(1000%2C%20100)%5D%0A%20%20%20%20).round(4)%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%20estimate%20is%20unbiased%20whatever%20the%20sample%3A%20the%20mean%20shares%20differ%20from%200.0492%20by%20little%20more%20than%20their%20Monte%20Carlo%20SE%2C%20the%20wobble%20from%20running%20only%201%2C000%20repeats.%20Its%20interval%20is%20not%20honest%20with%20100%20pixels%20in%20the%20non-crop%20stratum%3A%20crop%20hides%20there%20at%20about%201.35%20%25%2C%20so%20about%20a%20quarter%20of%20those%20samples%20contain%20none%20(0.9865%C2%B9%E2%81%B0%E2%81%B0%20%E2%89%88%200.26)%2C%20and%20a%20stratum%20whose%20reference%20proportion%20is%200%20adds%20nothing%20to%20the%20variance%20(q(1%20%E2%88%92%20q)%20%3D%200).%20The%20standard%20error%20then%20collapses%20just%20when%20the%20estimate%20is%20least%20certain.%20Moving%20reference%20pixels%20into%20the%20big%20stratum%2C%20where%20omission%20errors%20hide%2C%20fixes%20both.%20Olofsson%20et%20al.'s%20advice%20is%20the%20same%3A%20allocate%20enough%20sample%20to%20the%20strata%20with%20the%20rare%20errors%20that%20matter%20most.%0A%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%20Bootstrapping%20rows%20when%20seasons%20are%20what's%20independent%0A%0A%20%20%20%203.1's%20walk-forward%20errors%20share%20each%20season's%20shock.%20A%20bootstrap%20that%20redraws%20rows%20treats%204%2C000%20errors%20as%20independent%3B%20one%20that%20redraws%20whole%20seasons%20treats%2020%20seasons%20as%20the%20independent%20draws%20they%20are%3A%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s4_1_bootstrapping_rows_seasons(SEASON_YEAR%2C%20rmse%2C%20walk_errors)%3A%0A%20%20%20%20block_rng%20%3D%20np.random.default_rng(1)%0A%20%20%20%20by_season%20%3D%20%7Bs%3A%20g%5B%22error%22%5D.to_numpy()%20for%20s%2C%20g%20in%20walk_errors.groupby(SEASON_YEAR)%7D%0A%20%20%20%20season_keys%20%3D%20np.array(list(by_season))%0A%20%20%20%20row_draws%20%3D%20%5B%0A%20%20%20%20%20%20%20%20rmse(block_rng.choice(walk_errors%5B%22error%22%5D.to_numpy()%2C%20len(walk_errors)))%0A%20%20%20%20%20%20%20%20for%20_%20in%20range(2000)%0A%20%20%20%20%5D%0A%20%20%20%20season_draws%20%3D%20%5B%0A%20%20%20%20%20%20%20%20rmse(%0A%20%20%20%20%20%20%20%20%20%20%20%20np.concatenate(%5Bby_season%5Bs%5D%20for%20s%20in%20block_rng.choice(season_keys%2C%20season_keys.size)%5D)%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20for%20_%20in%20range(2000)%0A%20%20%20%20%5D%0A%20%20%20%20print(f%22walk-forward%20RMSE%20%7Brmse(walk_errors%5B'error'%5D)%3A.2f%7D%22)%0A%20%20%20%20print(%0A%20%20%20%20%20%20%20%20f%2295%25%20interval%2C%20rows%20redrawn%3A%20%20%20%20%7Bnp.percentile(row_draws%2C%202.5)%3A.2f%7D%20to%20%7Bnp.percentile(row_draws%2C%2097.5)%3A.2f%7D%22%0A%20%20%20%20)%0A%20%20%20%20print(%0A%20%20%20%20%20%20%20%20f%2295%25%20interval%2C%20seasons%20redrawn%3A%20%7Bnp.percentile(season_draws%2C%202.5)%3A.2f%7D%20to%20%7Bnp.percentile(season_draws%2C%2097.5)%3A.2f%7D%22%0A%20%20%20%20)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20The%20row%20bootstrap's%20interval%20is%20about%20a%20tenth%20as%20wide%20as%20it%20should%20be.%20Any%20interval%20on%20a%20hindcast%20score%20should%20redraw%20seasons%20(a%20block%20bootstrap)%2C%20for%20the%20same%20reason%20a%20band's%20quantile%20rests%20on%20seasons.%0A%0A%20%20%20%20%23%23%204.2%20Relative%20bands%20for%20places%20of%20every%20size%0A%0A%20%20%20%20%60ConformalByLevel%60%20pools%20absolute%20errors%20across%20every%20municipality%20of%20a%20level.%20When%20municipalities%20differ%20a%20hundredfold%20in%20size%2C%20small%20ones%20get%20bands%20far%20too%20wide%20and%20large%20ones%20far%20too%20narrow.%20%60ConformalRelativeByLevel%60%2C%20production's%20alternative%2C%20takes%20quantiles%20of%20errors%20as%20a%20share%20of%20the%20point%20instead%3A%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s4_2_relative_bands_places(%0A%20%20%20%20ConformalByLevel%2C%0A%20%20%20%20ConformalRelativeByLevel%2C%0A%20%20%20%20GEO_IDENTIFIER%2C%0A%20%20%20%20INIT_DATE%2C%0A%20%20%20%20SEASON_YEAR%2C%0A)%3A%0A%20%20%20%20size_rng%20%3D%20np.random.default_rng(2)%0A%20%20%20%20sizes%20%3D%20np.exp(size_rng.normal(8%2C%201.2%2C%20300))%0A%20%20%20%20sized%20%3D%20%5B%5D%0A%20%20%20%20for%20_season%20in%20range(1995%2C%202025)%3A%0A%20%20%20%20%20%20%20%20_shock%20%3D%20size_rng.normal(0%2C%200.04)%0A%20%20%20%20%20%20%20%20sized.append(%0A%20%20%20%20%20%20%20%20%20%20%20%20pd.DataFrame(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20GEO_IDENTIFIER%3A%20%5Bf%22ADM0%3ABRA%2FADM1%3AS%7Bc%20%25%2010%7D%2FADM2%3AC%7Bc%7D%22%20for%20c%20in%20range(300)%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20SEASON_YEAR%3A%20_season%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20INIT_DATE%3A%20pd.Timestamp(f%22%7B_season%7D-01-15%22)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22point%22%3A%20sizes%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22truth%22%3A%20sizes%20*%20(1%20%2B%20_shock%20%2B%20size_rng.normal(0%2C%200.08%2C%20300))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20sized%20%3D%20pd.concat(sized%2C%20ignore_index%3DTrue)%0A%20%20%20%20_train%2C%20_test%20%3D%20sized%5Bsized%5BSEASON_YEAR%5D%20%3C%202015%5D%2C%20sized%5Bsized%5BSEASON_YEAR%5D%20%3E%3D%202015%5D%0A%20%20%20%20size_class%20%3D%20pd.qcut(%0A%20%20%20%20%20%20%20%20_test%5B%22point%22%5D%2C%203%2C%20labels%3D%5B%22smallest%20third%22%2C%20%22middle%20third%22%2C%20%22largest%20third%22%5D%0A%20%20%20%20)%0A%20%20%20%20size_rows%20%3D%20%7B%7D%0A%20%20%20%20for%20_name%2C%20_kind%20in%20(%0A%20%20%20%20%20%20%20%20(%22absolute%20(ConformalByLevel)%22%2C%20ConformalByLevel)%2C%0A%20%20%20%20%20%20%20%20(%22relative%20(ConformalRelativeByLevel)%22%2C%20ConformalRelativeByLevel)%2C%0A%20%20%20%20)%3A%0A%20%20%20%20%20%20%20%20_bands%20%3D%20_kind(source%3D%22point%22%2C%20truth%3D%22truth%22%2C%20coverages%3D%5B0.8%5D%2C%20warm_up_seasons%3D5).fit(%0A%20%20%20%20%20%20%20%20%20%20%20%20_train%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20_scored%20%3D%20_bands.predict(_test)%0A%20%20%20%20%20%20%20%20_lower%2C%20_upper%20%3D%20_bands.bands()%5B80%5D%0A%20%20%20%20%20%20%20%20_inside%20%3D%20(_scored%5B%22truth%22%5D%20%3E%3D%20_scored%5B_lower%5D)%20%26%20(_scored%5B%22truth%22%5D%20%3C%3D%20_scored%5B_upper%5D)%0A%20%20%20%20%20%20%20%20size_rows%5B_name%5D%20%3D%20_inside.groupby(size_class%2C%20observed%3DTrue).mean()%0A%20%20%20%20pd.DataFrame(size_rows).round(2)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20With%20absolute%20errors%2C%20the%20small%20municipalities'%20bands%20always%20cover%20and%20the%20large%20ones'%20cover%20less%20than%20half%20the%20time%3A%20the%20pooled%20quantile%20is%20set%20by%20the%20mid-sized.%20Relative%20errors%20put%20every%20size%20on%20one%20scale.%20Area%20goes%20one%20step%20further%20with%20%5B%60ScaledQuantileBands%60%5D(https%3A%2F%2Fgithub.com%2Ftreefera%2Fmodel-foundry%2Fblob%2Ff69eef9df9e6a707c68f0292d940ccfc082cdaf5%2Fmodel_foundry%2Fmodels%2Farea_module%2Fsrc%2Farea_module%2Fmodels%2Fscaled_bands.py%23L68-L165)%3A%20it%20fits%20how%20the%20error%20scales%20with%20size%20on%20a%20log-log%20line%20and%20reads%20quantiles%20within%20size%20strata%2C%20for%20when%20relative%20errors%20aren't%20constant%20either.%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%203.1%2C%20change%20the%20season%20shock's%20sd%20from%2010%20to%202.%20How%20much%20does%20the%20gap%20between%20the%20random%20split%20and%20walking%20forward%20shrink%3F%20**Check%3A**%20the%20random%20split%20barely%20moves%2C%20while%20the%20walk-forward%20RMSE%20falls%20towards%20the%20county%20noise%20plus%20the%20booster's%20error.%0A%20%20%20%202.%20In%20a%20new%20cell%20after%202.2%2C%20build%20a%20recipe%20with%20a%20county%20day%2C%20%60TruthPublished(month_day%3D%2210-01%22%2C%20seasons_later%3D2%2C%20county_month_day%3D%2212-01%22)%60%2C%20and%20a%20frame%20holding%20%60weekly%60's%20dates%20twice%2C%20once%20for%20the%20nation%20and%20once%20for%20a%20municipality%20(%60%22ADM0%3ABRA%2FADM1%3AMT%2FADM2%3ASorriso%22%60).%20Compute%20%60truth_through%60%20for%20it%2C%20then%20%60TRAINED_THROUGH%60%20as%20production%20does%3A%20%60frame.groupby(%5BSEASON_YEAR%2C%20INIT_DATE%5D)%5B...%5D.transform(%22min%22)%60.%20For%20which%20forecast%20dates%20does%20the%20municipality%20know%20less%20than%20the%20nation%2C%20and%20what%20does%20the%20minimum%20then%20do%20to%20the%20national%20row%3F%20**Check%3A**%20on%20the%20weekly%20forecasts%20from%202%20October%20to%2027%20November%20the%20municipality%20knows%202021%20and%20the%20nation%202022%2C%20and%20the%20minimum%20holds%20the%20national%20row%20to%202021.%0A%20%20%20%203.%20In%203.6%2C%20set%20the%20shared%20shock's%20sd%20to%200%20and%20keep%20the%20noise.%20What%20happens%20to%20the%20per-season%20coverage%3F%20**Check%3A**%20every%20season%20lands%20near%2080%20%25%2C%20because%20the%20200%20errors%20are%20now%20independent.%0A%20%20%20%204.%20How%20much%20does%20a%20band's%20width%20depend%20on%20how%20many%20seasons%20it%20was%20fitted%20on%3F%20Fit%20%60ConformalByLevel%60%20(with%20%60warm_up_seasons%3D2%60)%20on%20the%20first%202%2C%203%20and%2020%20seasons%20of%2040%20replications%20of%203.6's%20errors%2C%20and%20compare%20the%20spread%20of%20the%2080%20%25%20half-width%20across%20replications.%20**Check%3A**%20around%20a%20true%20value%20of%20about%2011.5%2C%20the%20half-width's%20sd%20is%20about%204.6%20with%20two%20seasons%20and%20about%201.3%20with%20twenty%3A%20a%20short%20history%20gives%20bands%20that%20are%20right%20on%20average%20and%20wrong%20in%20any%20one%20hindcast.%0A%20%20%20%205.%20In%203.7%2C%20find%20the%20smallest%20non-crop%20sample%20(in%20steps%20of%2050)%20whose%2095%20%25%20interval%20covers%20at%20least%2093%20%25%20of%20the%20time.%20**Check%3A**%20a%20few%20hundred%2C%20not%20100.%0A%20%20%20%206.%20In%203.5%2C%20increase%20the%20number%20of%20seasons%20from%2045%20to%20120.%20Does%20the%20interval%20still%20include%20zero%3F%20**Check%3A**%20no%3A%20it%20narrows%20roughly%20as%201%2F%E2%88%9An%20and%20now%20excludes%20zero%2C%20and%20the%20score%20falls%20towards%20the%20population%20value%20of%20about%200.2.%0A%20%20%20%207.%20In%202.4%2C%20rank%202012%20against%20%60rank_values.sel(season_year%3Dslice(None%2C%202012))%60%20with%20%60percentile_rank%60.%20Is%20that%20the%20point-in-time%20answer%3F%20**Check%3A**%20yes%3A%202012%20drops%20out%20of%20its%20own%20baseline%2C%20and%20no%20later%20season%20is%20left%20in%20it.%0A%0A%20%20%20%20%23%20Sources%0A%0A%20%20%20%20-%20scikit-learn%201.9%2C%20%5Bcross-validation%5D(https%3A%2F%2Fscikit-learn.org%2F1.9%2Fmodules%2Fcross_validation.html)%2C%20including%20%22Cross%20validation%20of%20time%20series%20data%22%20and%20%22Group%20k-fold%22%0A%20%20%20%20-%20SciPy%201.18%2C%20%5B%60stats.bootstrap%60%5D(https%3A%2F%2Fdocs.scipy.org%2Fdoc%2Fscipy-1.18.0%2Freference%2Fgenerated%2Fscipy.stats.bootstrap.html)%20and%20%5B%60stats.false_discovery_control%60%5D(https%3A%2F%2Fdocs.scipy.org%2Fdoc%2Fscipy-1.18.0%2Freference%2Fgenerated%2Fscipy.stats.false_discovery_control.html)%0A%20%20%20%20-%20Angelopoulos%20and%20Bates%20(2021)%2C%20%5B%22A%20gentle%20introduction%20to%20conformal%20prediction%22%5D(https%3A%2F%2Farxiv.org%2Fabs%2F2107.07511)%3B%20Lei%20et%20al.%20(2018)%2C%20%22Distribution-free%20predictive%20inference%20for%20regression%22%2C%20*JASA*%20113(523)%0A%20%20%20%20-%20Olofsson%2C%20Foody%2C%20Herold%2C%20Stehman%2C%20Woodcock%20and%20Wulder%20(2014)%2C%20%22Good%20practices%20for%20estimating%20area%20and%20assessing%20accuracy%20of%20land%20change%22%2C%20*Remote%20Sensing%20of%20Environment*%20148%0A%20%20%20%20-%20Roberts%20et%20al.%20(2017)%2C%20%22Cross-validation%20strategies%20for%20data%20with%20temporal%2C%20spatial%2C%20hierarchical%2C%20or%20phylogenetic%20structure%22%2C%20*Ecography*%2040%0A%20%20%20%20-%20Benjamini%20and%20Hochberg%20(1995)%2C%20%22Controlling%20the%20false%20discovery%20rate%22%2C%20*JRSS%20B*%2057(1)%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%2F0-foundations%2F0.3-evaluation-and-uncertainty%2Fcode.md)%20and%20%5Bscience.md%5D(https%3A%2F%2Fgithub.com%2Ftreefera%2Ftf-model-lab%2Fblob%2Fmain%2Facademy%2F0-foundations%2F0.3-evaluation-and-uncertainty%2Fscience.md)%0A%20%20%20%20%22%22%22)%0A%0A%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20app.run()%0A
3c0543a20806638a5f9e72e6c0f341c2