import%20marimo%0A%0A__generated_with%20%3D%20%220.26.0%22%0Aapp%20%3D%20marimo.App()%0A%0Awith%20app.setup(hide_code%3DTrue)%3A%0A%20%20%20%20from%20pathlib%20import%20Path%0A%0A%20%20%20%20import%20numpy%20as%20np%0A%20%20%20%20import%20pandas%20as%20pd%0A%20%20%20%20import%20pyarrow.parquet%20as%20pq%0A%20%20%20%20from%20matplotlib%20import%20dates%20as%20mdates%0A%20%20%20%20from%20matplotlib%20import%20pyplot%20as%20plt%0A%20%20%20%20from%20matplotlib.patches%20import%20FancyArrowPatch%2C%20FancyBboxPatch%0A%0A%20%20%20%20BU%2C%20USA%2C%20SEASONS%20%3D%2067.25%2C%20%22ADM0%3AUSA%22%2C%20range(2011%2C%202026)%20%20%23%201%20bu%2Fac%20of%20wheat%20(60%20lb)%20%3D%2067.25%20kg%2Fha%0A%20%20%20%20DATA%20%3D%20Path.home()%20%2F%20%22data%2Fww-yield-usa%22%20%20%23%20copies%20of%20scored%20hindcasts%3B%20nothing%20is%20refitted%20here%0A%20%20%20%20RUNS%20%3D%20DATA%20%2F%20%22runs%22%0A%20%20%20%20FINAL%20%3D%20%22Final%20model%22%0A%20%20%20%20ARMS%20%3D%20%7BFINAL%3A%20(%22c2c%22%2C%20%22%230072B2%22)%2C%20%22First%20version%20of%20model-foundry%23644%22%3A%20(%22b%22%2C%20%22%23E69F00%22)%2C%20%22Trend%20and%20anchor%20only%22%3A%20(%22a%22%2C%20%22%23009E73%22)%7D%20%20%23%20fmt%3A%20skip%0A%20%20%20%20LEGACY%20%3D%20%7B%22tmi%22%3A%20(%22Legacy%20system%20(TMI)%22%2C%20%22%23CC79A7%22)%2C%20%22today%22%3A%20(%22Model%20Foundry%20before%20this%20work%22%2C%20%22%23D55E00%22)%7D%20%20%23%20fmt%3A%20skip%0A%20%20%20%20PLAINS%20%3D%20%5B%22Kansas%22%2C%20%22Oklahoma%22%2C%20%22Texas%22%2C%20%22Colorado%22%2C%20%22Nebraska%22%2C%20%22South%20Dakota%22%2C%20%22Montana%22%2C%20%22Wyoming%22%2C%20%22New%20Mexico%22%5D%20%20%23%20fmt%3A%20skip%0A%20%20%20%20DROUGHT%20%3D%20(2011%2C%202013%2C%202014%2C%202018%2C%202022%2C%202023)%20%20%23%20at%20least%20a%20quarter%20of%20WW%20area%20in%20USDM%20D2%2B%20in%20April%0A%20%20%20%20FEATURES%20%3D%20%7B%22z_pr_GrowingSeason_cum%22%3A%20%22Season-to-date%20rain%22%2C%20%22z_DryDay1_GrowingSeason_cum%22%3A%20%22Season-to-date%20dry%20days%22%7D%20%20%23%20fmt%3A%20skip%0A%20%20%20%20DATES%20%3D%20%7B%221%20Oct%22%3A%20%2210-01%22%2C%20%221%20Dec%22%3A%20%2212-01%22%2C%20%221%20Feb%22%3A%20%2202-01%22%2C%20%221%20Apr%22%3A%20%2204-01%22%2C%20%221%20May%22%3A%20%2205-01%22%2C%20%221%20Jun%22%3A%20%2206-01%22%2C%20%221%20Jul%22%3A%20%2207-01%22%7D%20%20%23%20fmt%3A%20skip%0A%20%20%20%20WINDOWS%20%3D%20%7B%22Aug%E2%80%93Dec%22%3A%20(8%2C%209%2C%2010%2C%2011%2C%2012)%2C%20%22Jan%E2%80%93May%22%3A%20(1%2C%202%2C%203%2C%204%2C%205)%2C%20%22Jun%E2%80%93Jul%22%3A%20(6%2C%207)%7D%0A%20%20%20%20LEVELS%20%3D%20%7B0%3A%20%22Nation%22%2C%201%3A%20%22States%22%2C%202%3A%20%22Counties%22%7D%0A%20%20%20%20KEYS%20%3D%20%5B%22geo_identifier%22%2C%20%22season_year%22%2C%20%22init_date%22%5D%0A%20%20%20%20STAGE%20%3D%20%22yield_kg_ha__additive%22%20%20%23%20the%20chain's%20column%20prefix%3B%20each%20stage%20appends%20its%20name%0A%0A%20%20%20%20def%20figure(title%2C%20ncols%3D1%2C%20height%3D3.2%2C%20ylabel%3D%22bu%2Fac%22)%3A%0A%20%20%20%20%20%20%20%20%22%22%22Return%20a%20styled%20row%20of%20axes%20under%20one%20title.%22%22%22%0A%20%20%20%20%20%20%20%20_fig%2C%20_axes%20%3D%20plt.subplots(%0A%20%20%20%20%20%20%20%20%20%20%20%201%2C%20ncols%2C%20figsize%3D(10%2C%20height)%2C%20layout%3D%22constrained%22%2C%20squeeze%3DFalse%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20for%20_ax%20in%20_axes%5B0%5D%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20_ax.spines%5B%5B%22top%22%2C%20%22right%22%5D%5D.set_visible(False)%0A%20%20%20%20%20%20%20%20%20%20%20%20_ax.grid(axis%3D%22y%22%2C%20color%3D%22%23e5e5e5%22%2C%20lw%3D0.8)%0A%20%20%20%20%20%20%20%20%20%20%20%20_ax.set_axisbelow(True)%0A%20%20%20%20%20%20%20%20_axes%5B0%5D%5B0%5D.set_ylabel(ylabel)%0A%20%20%20%20%20%20%20%20_fig.suptitle(title%2C%20x%3D0.01%2C%20ha%3D%22left%22)%0A%20%20%20%20%20%20%20%20return%20_axes%5B0%5D%0A%0A%20%20%20%20def%20legend(ax%2C%20ncols%3D4)%3A%0A%20%20%20%20%20%20%20%20ax.legend(fontsize%3D%22small%22%2C%20frameon%3DFalse%2C%20ncols%3Dncols%2C%20loc%3D%22lower%20left%22%2C%20bbox_to_anchor%3D(0%2C%201))%0A%0A%20%20%20%20def%20column(run%2C%20suffix)%3A%0A%20%20%20%20%20%20%20%20%22%22%22Return%20the%20name%20of%20the%20stage%20column%20of%20%60%60run%60%60%20that%20ends%20with%20%60%60suffix%60%60.%22%22%22%0A%20%20%20%20%20%20%20%20names%20%3D%20pq.read_schema(RUNS%20%2F%20run%20%2F%20%22hindcast%2Fyield%2Fpredictions.parquet%22).names%0A%20%20%20%20%20%20%20%20return%20next(c%20for%20c%20in%20names%20if%20c.endswith(suffix))%0A%0A%20%20%20%20def%20read(run%2C%20columns%2C%20seasons%3DSEASONS)%3A%0A%20%20%20%20%20%20%20%20%22%22%22Return%20%60%60columns%60%60%20of%20a%20run's%20hindcast%20for%20%60%60seasons%60%60%2C%20with%20each%20row's%20level%20and%20state.%22%22%22%0A%20%20%20%20%20%20%20%20_f%20%3D%20pd.read_parquet(%0A%20%20%20%20%20%20%20%20%20%20%20%20RUNS%20%2F%20run%20%2F%20%22hindcast%2Fyield%2Fpredictions.parquet%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20columns%3D%5B*KEYS%2C%20*columns%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20filters%3D%5B(%22season_year%22%2C%20%22in%22%2C%20list(seasons))%5D%2C%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20_g%20%3D%20_f.geo_identifier.astype(str)%0A%20%20%20%20%20%20%20%20return%20_f.assign(%0A%20%20%20%20%20%20%20%20%20%20%20%20geo_identifier%3D_g%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20level%3D_g.str.count(%22%2F%22)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20state%3D_g.str.split(%22ADM1%3A%22).str%5B1%5D.str.split(%22%2F%22).str%5B0%5D%2C%0A%20%20%20%20%20%20%20%20)%0A%0A%20%20%20%20def%20season_day(init_date%2C%20season_year)%3A%0A%20%20%20%20%20%20%20%20%22%22%22Return%20the%20days%20from%20the%20season's%20first%20day%2C%202%20August%20of%20the%20year%20before%20harvest.%22%22%22%0A%20%20%20%20%20%20%20%20return%20(init_date%20-%20pd.to_datetime((season_year%20-%201).astype(str)%20%2B%20%22-08-02%22)).dt.days%0A%0A%20%20%20%20def%20month_day_rows(frame%2C%20label)%3A%0A%20%20%20%20%20%20%20%20%22%22%22Return%20each%20season's%20last%20forecast%20on%20or%20before%20the%20date%20%60%60label%60%60%20names.%22%22%22%0A%20%20%20%20%20%20%20%20_month%2C%20_day%20%3D%20int(DATES%5Blabel%5D%5B%3A2%5D)%2C%20int(DATES%5Blabel%5D%5B3%3A%5D)%0A%20%20%20%20%20%20%20%20_target%20%3D%20(pd.Timestamp(2001%20if%20_month%20%3E%3D%208%20else%202002%2C%20_month%2C%20_day)%20-%20pd.Timestamp(2001%2C%208%2C%202)).days%20%20%23%20fmt%3A%20skip%0A%20%20%20%20%20%20%20%20_early%20%3D%20frame%5Bseason_day(frame.init_date%2C%20frame.season_year)%20%3C%3D%20_target%5D%0A%20%20%20%20%20%20%20%20return%20_early%5B_early.init_date%20%3D%3D%20_early.groupby(%22season_year%22).init_date.transform(%22max%22)%5D%0A%0A%20%20%20%20def%20wmape(group%2C%20point%2C%20truth)%3A%0A%20%20%20%20%20%20%20%20%22%22%22Return%20100%20%C2%B7%20%CE%A3%7Cpoint%20%E2%88%92%20truth%7C%20%2F%20%CE%A3%20truth.%22%22%22%0A%20%20%20%20%20%20%20%20return%20100%20*%20(group%5Bpoint%5D%20-%20group%5Btruth%5D).abs().sum()%20%2F%20group%5Btruth%5D.sum()%0A%0A%20%20%20%20def%20season_axis(ax%2C%20season)%3A%0A%20%20%20%20%20%20%20%20%22%22%22Format%20the%20x%20axis%20as%20a%20season%2C%202%20August%20to%201%20August.%22%22%22%0A%20%20%20%20%20%20%20%20ax.set_xlim(pd.Timestamp(season%20-%201%2C%208%2C%202)%2C%20pd.Timestamp(season%2C%208%2C%201))%0A%20%20%20%20%20%20%20%20ax.xaxis.set_major_formatter(mdates.DateFormatter(%22%25b%22))%0A%0A%0A%40app.cell(hide_code%3DTrue)%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%20US%20winter%20wheat%20yield%3A%20bottom-up%20counties%2C%20a%20national%20anchor%2C%20and%20Plains%20weather%0A%0A%20%20%20%20**Bottom%20line.**%20The%20final%20model%20forecasts%20every%20county%2C%20builds%20states%20and%20the%20nation%20from%20them%2C%20then%20blends%20that%20bottom-up%20nation%20with%20a%20separate%20top-down%20national%20estimate.%20Against%20the%20first%20version%20of%20%5Bmodel-foundry%23644%5D(https%3A%2F%2Fgithub.com%2Ftreefera%2Fmodel-foundry%2Fpull%2F644)%20it%20is%20about%20as%20good%20for%20the%20nation%20and%20clearly%20better%20for%20counties.%20It%20also%20moves%20sooner%20in%20seasons%20that%20leave%20the%20trend%2C%20because%20counties%20in%20the%20Plains%20now%20read%20their%20own%20weather.%0A%0A%20%20%20%20%7C%20%7C%20Final%20model%20%7C%20First%20version%20of%20%23644%20%7C%20Trend%20and%20anchor%20only%20%7C%0A%20%20%20%20%7C---%7C---%7C---%7C---%7C%0A%20%20%20%20%7C%20County%20model%20%7C%20A%20trend%20line%2C%20then%20a%20weather%20correction%20in%20the%209%20Plains%20states%20%7C%20A%20trend%20line%2C%20then%20a%20gradient-boosted%20correction%20on%20last%20seasons'%20yields%20%7C%20A%20trend%20line%20%7C%0A%20%20%20%20%7C%20Weather%20at%20counties%20%7C%20Season-to-date%20rain%20and%20dry%20days%2C%20Plains%20only%20%7C%20None%20%7C%20None%20%7C%0A%20%20%20%20%7C%20States%20and%20nation%20%7C%20Built%20from%20the%20counties%20%7C%20Built%20from%20the%20counties%20%7C%20Built%20from%20the%20counties%20%7C%0A%20%20%20%20%7C%20National%20anchor%20%7C%20The%20same%20in%20all%20three%3A%20the%20bottom-up%20nation%20blended%20with%20a%20national%20trend%20moved%20by%20rain%20and%20dry%20days%20%7C%20%7C%20%7C%0A%0A%20%20%20%20**How%20to%20read%20this.**%20Section%201%20shows%20how%20the%20parts%20fit%20together.%20Sections%202%20to%204%20follow%20one%20season%20through%20each%20stage%3B%20pick%20it%20below.%20Section%205%20checks%20the%20model%20against%20USDA's%20final%20yield%20for%20every%20season%20from%202011%20to%202025%2C%20each%20forecast%20by%20a%20model%20fitted%20only%20on%20seasons%20whose%20truth%20was%20published%20by%20that%20date.%20Yields%20are%20in%20bushels%20per%20acre%20(bu%2Fac%3B%201%20bu%2Fac%20of%20wheat%20is%2067.25%20kg%2Fha).%20The%20truth%20is%20the%20final%20yield%20published%20by%20USDA's%20statistics%20service%2C%20NASS.%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%20How%20the%20parts%20fit%20together%0A%0A%20%20%20%20Every%20county%20gets%20a%20forecast%20first.%20States%20and%20the%20nation%20are%20built%20from%20the%20counties%2C%20so%20they%20can%20never%20disagree%20with%20them.%20The%20national%20anchor%20then%20compares%20that%20bottom-up%20nation%2C%20**B**%2C%20with%20an%20independent%20top-down%20one%2C%20**T**%2C%20and%20blends%20them%20into%20**N**.%20The%20ratio%20N%20%C3%B7%20B%20is%20applied%20to%20every%20county%20and%20state%2C%20which%20keeps%20the%20three%20levels%20a%20single%20roll-up.%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20s1_concept()%3A%0A%20%20%20%20_fig%2C%20_ax%20%3D%20plt.subplots(figsize%3D(10%2C%204.6)%2C%20layout%3D%22constrained%22)%0A%20%20%20%20_ax.set_xlim(0%2C%2010)%0A%20%20%20%20_ax.set_ylim(0%2C%206)%0A%20%20%20%20_ax.axis(%22off%22)%0A%0A%20%20%20%20def%20_box(x%2C%20y%2C%20w%2C%20h%2C%20text%2C%20colour)%3A%0A%20%20%20%20%20%20%20%20_ax.add_patch(FancyBboxPatch((x%2C%20y)%2C%20w%2C%20h%2C%20boxstyle%3D%22round%2Cpad%3D0.05%22%2C%20fc%3Dcolour%2C%20ec%3D%22%23333333%22%2C%20lw%3D0.8))%20%20%23%20fmt%3A%20skip%0A%20%20%20%20%20%20%20%20_ax.text(x%20%2B%20w%20%2F%202%2C%20y%20%2B%20h%20%2F%202%2C%20text%2C%20ha%3D%22center%22%2C%20va%3D%22center%22%2C%20fontsize%3D8.5%2C%20wrap%3DTrue)%0A%0A%20%20%20%20def%20_arrow(a%2C%20b%2C%20text%3DNone%2C%20at%3DNone%2C%20colour%3D%22%23333333%22%2C%20style%3D%22arc3%22)%3A%0A%20%20%20%20%20%20%20%20_ax.add_patch(FancyArrowPatch(a%2C%20b%2C%20arrowstyle%3D%22-%7C%3E%22%2C%20mutation_scale%3D12%2C%20color%3Dcolour%2C%20lw%3D1.1%2C%20connectionstyle%3Dstyle))%20%20%23%20fmt%3A%20skip%0A%20%20%20%20%20%20%20%20if%20text%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20_ax.text(*at%2C%20text%2C%20fontsize%3D7.5%2C%20color%3Dcolour%20if%20colour%20!%3D%20%22%23333333%22%20else%20%22%23555555%22)%0A%0A%20%20%20%20_box(0.1%2C%204.3%2C%202.4%2C%201.2%2C%20%22COUNTIES%5Cntrend%20line%20per%20county%5Cn(pulled%20towards%20its%20neighbours)%22%2C%20%22%23dbe9f6%22)%20%20%23%20fmt%3A%20skip%0A%20%20%20%20_box(0.1%2C%202.5%2C%202.4%2C%201.3%2C%20%22PLAINS%20COUNTIES%20ONLY%5Cn%C3%B7%20exp(ridge%20on%20season-to-date%5Cnrain%20and%20dry%20days%20%C3%97%20progress)%22%2C%20%22%23bcd7ef%22)%20%20%23%20fmt%3A%20skip%0A%20%20%20%20_box(3.4%2C%203.4%2C%202.2%2C%201.2%2C%20%22STATES%5Cnharvested-area-weighted%5Cnmean%20of%20their%20counties%22%2C%20%22%23e8e8e8%22)%20%20%23%20fmt%3A%20skip%0A%20%20%20%20_box(3.4%2C%201.3%2C%202.2%2C%201.2%2C%20%22NATION%2C%20bottom-up%20%20B%5Cnstates%20over%20their%20whole%20area%22%2C%20%22%23e8e8e8%22)%20%20%23%20fmt%3A%20skip%0A%20%20%20%20_box(6.5%2C%204.3%2C%203.3%2C%201.2%2C%20%22NATION%2C%20top-down%20%20T%5Cn25-season%20national%20trend%20%C3%97%5Cnexp(rain%20and%20dry-day%20step)%22%2C%20%22%23fde6c4%22)%20%20%23%20fmt%3A%20skip%0A%20%20%20%20_box(6.5%2C%201.3%2C%203.3%2C%201.4%2C%20%22BLEND%20%20N%5Cnlog%20N%20%3D%20w%C2%B7log%20T%20%2B%20(1%E2%88%92w)%C2%B7(log%20B%20%E2%88%92%20bias)%5Cnw%20from%20past%20errors%2C%20by%20month%22%2C%20%22%23cfe9dc%22)%20%20%23%20fmt%3A%20skip%0A%20%20%20%20_box(6.5%2C%200.05%2C%203.3%2C%200.9%2C%20%22PUSH-DOWN%3A%20every%20county%20and%20state%20%C3%97%20N%20%C3%B7%20B%22%2C%20%22%23f3d0d0%22)%20%20%23%20fmt%3A%20skip%0A%20%20%20%20_arrow((1.3%2C%204.3)%2C%20(1.3%2C%203.8))%0A%20%20%20%20_arrow((2.5%2C%203.2)%2C%20(3.4%2C%203.9)%2C%20%22roll%20up%22%2C%20(2.55%2C%203.75))%0A%20%20%20%20_arrow((4.5%2C%203.4)%2C%20(4.5%2C%202.5)%2C%20%22roll%20up%22%2C%20(4.6%2C%202.9))%0A%20%20%20%20_arrow((5.6%2C%201.9)%2C%20(6.5%2C%201.9))%0A%20%20%20%20_arrow((8.15%2C%204.3)%2C%20(8.15%2C%202.7))%0A%20%20%20%20_arrow((8.15%2C%201.3)%2C%20(8.15%2C%200.95))%0A%20%20%20%20_arrow((6.5%2C%200.5)%2C%20(1.3%2C%202.5)%2C%20%22the%20same%20factor%20back%20down%20to%20every%20county%20and%20state%22%2C%20(1.5%2C%200.6)%2C%20%22%23b03a2e%22%2C%20%22angle%2CangleA%3D180%2CangleB%3D90%2Crad%3D0%22)%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%20Pick%20the%20season%20and%20the%20state%20that%20sections%201%20to%204%20follow.%202023%20was%20a%20Plains%20drought%3B%202016%20a%20record%20crop.%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20s1_concept_2(mo)%3A%0A%20%20%20%20season%20%3D%20mo.ui.dropdown(%7Bstr(s)%3A%20s%20for%20s%20in%20SEASONS%7D%2C%20value%3D%222023%22%2C%20label%3D%22Season%22)%0A%20%20%20%20state%20%3D%20mo.ui.dropdown(sorted(%5B*PLAINS%2C%20%22Washington%22%2C%20%22Illinois%22%2C%20%22Ohio%22%2C%20%22Idaho%22%2C%20%22Missouri%22%5D)%2C%20value%3D%22Kansas%22%2C%20label%3D%22State%22)%20%20%23%20fmt%3A%20skip%0A%20%20%20%20mo.hstack(%5Bseason%2C%20state%5D%2C%20justify%3D%22start%22)%0A%20%20%20%20return%20season%2C%20state%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**The%20roll-up%20holds%20exactly.**%20For%20the%20chosen%20season%2C%20each%20state's%20delivered%20yield%20against%20the%20harvested-area-weighted%20mean%20of%20its%20delivered%20counties%2C%20and%20the%20nation%20against%20its%20states%20weighted%20by%20their%20whole%20area.%20States%20match%20their%20counties%20to%20rounding%20error.%20The%20nation%20is%20within%20a%20few%20hundredths%20of%20a%20percent%20of%20its%20states%2C%20because%203%20to%205%25%20of%20US%20winter%20wheat%20grows%20in%20small%20states%20outside%20the%20sample%2C%20which%20are%20counted%20at%20the%20sample's%20yield.%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20s1_concept_3(season)%3A%0A%20%20%20%20_d%20%3D%20column(%22c2c%22%2C%20%22__national_anchor%22)%0A%20%20%20%20_f%20%3D%20read(%22c2c%22%2C%20%5B_d%2C%20%22area_harvested_in_season_ha%22%2C%20%22area_harvested_in_season_ha__adm1%22%5D%2C%20%5Bseason.value%5D)%20%20%23%20fmt%3A%20skip%0A%20%20%20%20_c%2C%20_s%2C%20_n%20%3D%20(_f%5B_f.level%20%3D%3D%20_l%5D%20for%20_l%20in%20(2%2C%201%2C%200))%0A%20%20%20%20_rolled%20%3D%20(%0A%20%20%20%20%20%20%20%20_c.assign(_q%3D_c%5B_d%5D%20*%20_c.area_harvested_in_season_ha)%0A%20%20%20%20%20%20%20%20.groupby(%5B%22state%22%2C%20%22init_date%22%5D)%0A%20%20%20%20%20%20%20%20.apply(lambda%20g%3A%20g%5B%22_q%22%5D.sum()%20%2F%20g.area_harvested_in_season_ha.sum()%2C%20include_groups%3DFalse)%0A%20%20%20%20)%0A%20%20%20%20_states%20%3D%20_s.set_index(%5B%22state%22%2C%20%22init_date%22%5D)%5B_d%5D%0A%20%20%20%20_nation%20%3D%20(%0A%20%20%20%20%20%20%20%20_s.assign(_q%3D_s%5B_d%5D%20*%20_s.area_harvested_in_season_ha__adm1)%0A%20%20%20%20%20%20%20%20.groupby(%22init_date%22)%0A%20%20%20%20%20%20%20%20.apply(lambda%20g%3A%20g%5B%22_q%22%5D.sum()%20%2F%20g.area_harvested_in_season_ha__adm1.sum()%2C%20include_groups%3DFalse)%0A%20%20%20%20)%0A%20%20%20%20pd.DataFrame(%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%22Check%22%3A%20%5B%22State%20%3D%20its%20counties%22%2C%20%22Nation%20%3D%20its%20states%22%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22Largest%20gap%2C%20%25%22%3A%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20100%20*%20(_rolled%20%2F%20_states.reindex(_rolled.index)%20-%201).abs().max()%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20100%20*%20(_nation%20%2F%20_n.set_index(%22init_date%22)%5B_d%5D%20-%201).abs().max()%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%5D%2C%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20).round(6)%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%20The%20two%20weather%20inputs%0A%0A%20%20%20%20Both%20come%20from%20ERA5%2C%20per%20county%2C%20as%20of%20each%20forecast%20date.%20Each%20is%20cumulative%20over%20the%20growing%20season%20so%20far%2C%20from%20about%20three%20weeks%20after%20sowing%20starts%20to%20just%20before%20ripening%20ends%2C%20and%20expressed%20as%20a%20z-score%20against%20that%20county's%20own%20history%20for%20the%20same%20day%20of%20the%20season%3A%200%20is%20normal%20for%20the%20date%2C%20%2B1%20is%20one%20standard%20deviation%20above%20normal.%0A%0A%20%20%20%20-%20**Season-to-date%20rain**%20(%60z_pr_GrowingSeason_cum%60)%3A%20positive%20means%20wetter%20than%20usual%20so%20far.%0A%20%20%20%20-%20**Season-to-date%20dry%20days**%20(%60z_DryDay1_GrowingSeason_cum%60)%3A%20days%20with%20under%201%20mm%20of%20rain.%20Positive%20means%20more%20dry%20days%20than%20usual%20so%20far.%0A%0A%20%20%20%20First%20the%20nation's%20harvested-area-weighted%20average%2C%20every%20season%20in%20grey%20and%20the%20chosen%20one%20in%20colour.%20Nothing%20is%20known%20before%20sowing%2C%20so%20the%20lines%20start%20in%20October.%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20s2_inputs(season)%3A%0A%20%20%20%20_f%20%3D%20read(%22c2c%22%2C%20%5B*FEATURES%2C%20%22area_harvested_in_season_ha%22%5D%2C%20SEASONS)%0A%20%20%20%20_c%20%3D%20_f%5B_f.level%20%3D%3D%202%5D%0A%20%20%20%20_w%20%3D%20_c.area_harvested_in_season_ha%0A%20%20%20%20_axes%20%3D%20figure(f%22The%20nation's%20season-to-date%20weather%3A%20every%20season%20in%20grey%2C%20%7Bseason.value%7D%20in%20blue%22%2C%20ncols%3D2%2C%20ylabel%3D%22z-score%22)%20%20%23%20fmt%3A%20skip%0A%20%20%20%20for%20_ax%2C%20(_feature%2C%20_name)%20in%20zip(_axes%2C%20FEATURES.items()%2C%20strict%3DTrue)%3A%0A%20%20%20%20%20%20%20%20_m%20%3D%20(%0A%20%20%20%20%20%20%20%20%20%20%20%20_c.assign(_x%3D_c%5B_feature%5D%20*%20_w%2C%20_a%3D_w.where(_c%5B_feature%5D.notna()))%0A%20%20%20%20%20%20%20%20%20%20%20%20.groupby(%5B%22season_year%22%2C%20%22init_date%22%5D)%5B%5B%22_x%22%2C%20%22_a%22%5D%5D%0A%20%20%20%20%20%20%20%20%20%20%20%20.sum()%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20_m%20%3D%20(_m%5B%22_x%22%5D%20%2F%20_m%5B%22_a%22%5D).rename(%22z%22).reset_index()%0A%20%20%20%20%20%20%20%20for%20_s%2C%20_g%20in%20_m.groupby(%22season_year%22)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20_x%20%3D%20_g.init_date%20-%20pd.DateOffset(years%3Dint(_s)%20-%20season.value)%0A%20%20%20%20%20%20%20%20%20%20%20%20_hi%20%3D%20_s%20%3D%3D%20season.value%0A%20%20%20%20%20%20%20%20%20%20%20%20_ax.plot(_x%2C%20_g.z%2C%20color%3D%22%230072B2%22%20if%20_hi%20else%20%22%23bbbbbb%22%2C%20lw%3D2%20if%20_hi%20else%200.8%2C%20zorder%3D3%20if%20_hi%20else%201%2C%20label%3Dstr(_s)%20if%20_hi%20else%20None)%20%20%23%20fmt%3A%20skip%0A%20%20%20%20%20%20%20%20_ax.axhline(0%2C%20color%3D%22black%22%2C%20lw%3D0.8)%0A%20%20%20%20%20%20%20%20season_axis(_ax%2C%20season.value)%0A%20%20%20%20%20%20%20%20_ax.set_title(_name%2C%20loc%3D%22left%22%2C%20fontsize%3D%22medium%22)%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%20The%20same%20rain%20input%20state%20by%20state%20in%20the%20chosen%20season.%20The%209%20Plains%20states%2C%20whose%20counties%20the%20weather%20correction%20reads%2C%20are%20in%20blue%3B%20every%20other%20state%20is%20grey.%20Rain%20is%20where%20the%20Plains%20differ%20from%20the%20rest%3A%20it%20lifts%20Plains%20yields%20and%20tends%20to%20lower%20them%20elsewhere%2C%20so%20the%20correction%20is%20fitted%20and%20applied%20to%20Plains%20counties%20only.%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20s2_inputs_2(season)%3A%0A%20%20%20%20_f%20%3D%20read(%22c2c%22%2C%20%5B%22z_pr_GrowingSeason_cum%22%2C%20%22area_harvested_in_season_ha%22%5D%2C%20%5Bseason.value%5D)%0A%20%20%20%20_c%20%3D%20_f%5B_f.level%20%3D%3D%202%5D.dropna(subset%3D%5B%22z_pr_GrowingSeason_cum%22%5D)%0A%20%20%20%20_m%20%3D%20(%0A%20%20%20%20%20%20%20%20_c.assign(_x%3D_c.z_pr_GrowingSeason_cum%20*%20_c.area_harvested_in_season_ha)%0A%20%20%20%20%20%20%20%20.groupby(%5B%22state%22%2C%20%22init_date%22%5D)%5B%5B%22_x%22%2C%20%22area_harvested_in_season_ha%22%5D%5D%0A%20%20%20%20%20%20%20%20.sum()%0A%20%20%20%20)%0A%20%20%20%20_m%20%3D%20(_m%5B%22_x%22%5D%20%2F%20_m.area_harvested_in_season_ha).rename(%22z%22).reset_index()%0A%20%20%20%20(_ax%2C)%20%3D%20figure(f%22Season-to-date%20rain%20by%20state%2C%20%7Bseason.value%7D%22%2C%20ylabel%3D%22z-score%22)%0A%20%20%20%20for%20_s%2C%20_g%20in%20_m.groupby(%22state%22)%3A%0A%20%20%20%20%20%20%20%20_plains%20%3D%20_s%20in%20PLAINS%0A%20%20%20%20%20%20%20%20_ax.plot(_g.init_date%2C%20_g.z%2C%20color%3D%22%230072B2%22%20if%20_plains%20else%20%22%23bbbbbb%22%2C%20lw%3D1.4%20if%20_plains%20else%200.8%2C%20zorder%3D2%20if%20_plains%20else%201)%20%20%23%20fmt%3A%20skip%0A%20%20%20%20_ax.plot(%5B%5D%2C%20%5B%5D%2C%20color%3D%22%230072B2%22%2C%20label%3D%22Plains%20state%22)%0A%20%20%20%20_ax.plot(%5B%5D%2C%20%5B%5D%2C%20color%3D%22%23bbbbbb%22%2C%20label%3D%22Other%20state%22)%0A%20%20%20%20_ax.axhline(0%2C%20color%3D%22black%22%2C%20lw%3D0.8)%0A%20%20%20%20season_axis(_ax%2C%20season.value)%0A%20%20%20%20legend(_ax)%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%20%23%23%203%20%C2%B7%20Stage%20by%20stage%0A%0A%20%20%20%20%23%23%23%203.1%20Counties%3A%20the%20trend%2C%20then%20the%20Plains%20weather%20correction%0A%0A%20%20%20%20The%20chosen%20state's%20forecast%20at%20each%20stage%2C%20built%20up%20from%20its%20counties.%20The%20**trend**%20is%20a%20line%20per%20county%2C%20its%20slope%20pulled%20towards%20its%20neighbours'%20so%20a%20short%20or%20noisy%20history%20cannot%20produce%20a%20wild%20line.%20In%20a%20Plains%20state%20the%20**weather%20correction**%20then%20divides%20each%20county%20by%20exp(%CE%B2%C2%B7x)%2C%20where%20x%20is%20that%20county's%20season-to-date%20rain%20and%20dry%20days%20times%20season%20progress.%20%CE%B2%20is%20one%20ridge%20fit%20across%20all%20Plains%20counties%20on%20earlier%20seasons%2C%20with%20no%20intercept%2C%20so%20normal%20weather%20moves%20nothing%20and%20the%20effect%20grows%20from%20zero%20at%20sowing.%20Outside%20the%20Plains%20the%20two%20lines%20coincide.%20The%20black%20bar%20is%20NASS's%20final%20yield.%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20s3_stages(season%2C%20state%2C%20truth)%3A%0A%20%20%20%20_trend%2C%20_corr%20%3D%20f%22%7BSTAGE%7D__baseline%22%2C%20column(%22c2c%22%2C%20%22__residual_corrected%22)%0A%20%20%20%20_d%20%3D%20column(%22c2c%22%2C%20%22__national_anchor%22)%0A%20%20%20%20_f%20%3D%20read(%22c2c%22%2C%20%5B_trend%2C%20_corr%2C%20_d%5D%2C%20%5Bseason.value%5D)%0A%20%20%20%20_s%20%3D%20_f%5B(_f.level%20%3D%3D%201)%20%26%20(_f.state%20%3D%3D%20state.value)%5D.sort_values(%22init_date%22)%0A%20%20%20%20_truth%20%3D%20truth.get((f%22ADM0%3AUSA%2FADM1%3A%7Bstate.value%7D%22%2C%20season.value))%0A%20%20%20%20(_ax%2C)%20%3D%20figure(f%22%7Bstate.value%7D%2C%20%7Bseason.value%7D%3A%20each%20stage%20of%20the%20forecast%22)%0A%20%20%20%20for%20_col%2C%20_name%2C%20_colour%20in%20((_trend%2C%20%22Trend%22%2C%20%22%23999999%22)%2C%20(_corr%2C%20%22After%20the%20Plains%20weather%20correction%22%2C%20%22%2356B4E9%22)%2C%20(_d%2C%20%22Delivered%20(after%20the%20anchor's%20push-down)%22%2C%20%22%230072B2%22))%3A%20%20%23%20fmt%3A%20skip%0A%20%20%20%20%20%20%20%20_ax.plot(_s.init_date%2C%20_s%5B_col%5D%20%2F%20BU%2C%20color%3D_colour%2C%20lw%3D1.8%2C%20label%3D_name)%0A%20%20%20%20if%20_truth%20is%20not%20None%3A%0A%20%20%20%20%20%20%20%20_ax.axhline(_truth%20%2F%20BU%2C%20color%3D%22black%22%2C%20lw%3D1.5%2C%20label%3D%22Final%20yield%20(USDA)%22)%0A%20%20%20%20season_axis(_ax%2C%20season.value)%0A%20%20%20%20legend(_ax%2C%202)%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%20%23%23%23%203.2%20What%20the%20weather%20correction%20does%2C%20Plains%20state%20by%20state%0A%0A%20%20%20%20The%20correction's%20effect%20on%20each%20Plains%20state's%20yield%20through%20the%20chosen%20season%2C%20in%20percent%2C%20split%20into%20its%20two%20inputs.%20Each%20state's%20line%20is%20the%20harvested-area-weighted%20mean%20of%20its%20counties'%20effects.%20Below%20zero%20means%20the%20weather%20lowered%20the%20forecast.%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20s3_stages_2(season)%3A%0A%20%20%20%20_shift%20%3D%20column(%22c2c%22%2C%20%22__residual_corrected__shift%22)%0A%20%20%20%20_parts%20%3D%20%7Bf%22%7B_shift%7D__%7B_k%7D%22%3A%20_v%20for%20_k%2C%20_v%20in%20FEATURES.items()%7D%0A%20%20%20%20_f%20%3D%20read(%22c2c%22%2C%20%5B_shift%2C%20*_parts%2C%20%22area_harvested_in_season_ha%22%5D%2C%20%5Bseason.value%5D)%0A%20%20%20%20_c%20%3D%20_f%5B(_f.level%20%3D%3D%202)%20%26%20_f.state.isin(PLAINS)%5D%0A%20%20%20%20_w%20%3D%20_c.area_harvested_in_season_ha%0A%20%20%20%20_axes%20%3D%20figure(f%22The%20Plains%20weather%20correction's%20effect%20on%20state%20yield%2C%20%7Bseason.value%7D%22%2C%20ncols%3D3%2C%20ylabel%3D%22%25%20of%20the%20trend%22)%20%20%23%20fmt%3A%20skip%0A%20%20%20%20for%20_ax%2C%20(_col%2C%20_name)%20in%20zip(_axes%2C%20%7B_shift%3A%20%22Both%20inputs%22%2C%20**_parts%7D.items()%2C%20strict%3DTrue)%3A%0A%20%20%20%20%20%20%20%20_e%20%3D%20100%20*%20(np.exp(-_c%5B_col%5D)%20-%201)%0A%20%20%20%20%20%20%20%20_m%20%3D%20(_c.assign(_x%3D_e%20*%20_w).groupby(%5B%22state%22%2C%20%22init_date%22%5D)%5B%22_x%22%5D.sum()%20%2F%20_c.groupby(%5B%22state%22%2C%20%22init_date%22%5D).area_harvested_in_season_ha.sum()).rename(%22e%22).reset_index()%20%20%23%20fmt%3A%20skip%0A%20%20%20%20%20%20%20%20for%20_i%2C%20(_s%2C%20_g)%20in%20enumerate(_m.groupby(%22state%22))%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20_ax.plot(_g.init_date%2C%20_g.e%2C%20lw%3D1.2%2C%20color%3Dplt.cm.tab10(_i)%2C%20label%3D_s%20if%20_ax%20is%20_axes%5B0%5D%20else%20None)%20%20%23%20fmt%3A%20skip%0A%20%20%20%20%20%20%20%20_ax.axhline(0%2C%20color%3D%22black%22%2C%20lw%3D0.8)%0A%20%20%20%20%20%20%20%20season_axis(_ax%2C%20season.value)%0A%20%20%20%20%20%20%20%20_ax.xaxis.set_major_locator(mdates.MonthLocator(bymonth%3D(10%2C%2012%2C%202%2C%204%2C%206)))%0A%20%20%20%20%20%20%20%20_ax.set_title(_name%2C%20loc%3D%22left%22%2C%20fontsize%3D%22medium%22)%0A%20%20%20%20_axes%5B0%5D.figure.legend(fontsize%3D%22small%22%2C%20frameon%3DFalse%2C%20ncols%3D5%2C%20loc%3D%22outside%20lower%20center%22)%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%20%23%23%23%203.3%20The%20nation%3A%20bottom-up%20B%2C%20top-down%20T%20and%20the%20blend%20N%0A%0A%20%20%20%20The%20national%20forecast's%20three%20ingredients%20in%20the%20chosen%20season.%0A%0A%20%20%20%20-%20**B%2C%20bottom-up%3A**%20the%20nation%20built%20from%20the%20counties%2C%20after%20the%20trend%20and%20the%20Plains%20correction.%0A%20%20%20%20-%20**T%2C%20top-down%3A**%20the%20national%2025-season%20trend%20moved%20by%20a%20walk-forward%20ridge%20on%20the%20nation's%20area-weighted%20rain%20and%20dry%20days.%20It%20does%20not%20look%20at%20counties%20at%20all.%0A%20%20%20%20-%20**N%2C%20delivered%3A**%20log%20N%20%3D%20w%C2%B7log%20T%20%2B%20(1%20%E2%88%92%20w)%C2%B7(log%20B%20%E2%88%92%20bias).%20w%20is%20set%20each%20month%20from%20how%20wrong%20T%20and%20B%20were%20in%20earlier%20seasons%20(their%20error%20covariance)%2C%20and%20the%20bias%20is%20B's%20average%20past%20error.%20B%20has%20run%20a%20few%20percent%20low%20on%20average%2C%20so%20removing%20its%20bias%20lifts%20it%2C%20which%20is%20why%20N%20can%20sit%20above%20both%20B%20and%20T.%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20s3_stages_3(nass%2C%20season)%3A%0A%20%20%20%20_b%2C%20_d%20%3D%20column(%22c2c%22%2C%20%22__bottom_up%22)%2C%20column(%22c2c%22%2C%20%22__national_anchor%22)%0A%20%20%20%20_f%20%3D%20read(%22c2c%22%2C%20%5B_b%2C%20_d%2C%20f%22%7B_d%7D__trend%22%2C%20f%22%7B_d%7D__weather%22%5D%2C%20%5Bseason.value%5D)%0A%20%20%20%20_n%20%3D%20_f%5B_f.level%20%3D%3D%200%5D.sort_values(%22init_date%22)%0A%20%20%20%20_t%20%3D%20_n%5Bf%22%7B_d%7D__trend%22%5D%20*%20np.exp(_n%5Bf%22%7B_d%7D__weather%22%5D%20%2F%20100)%0A%20%20%20%20(_ax%2C)%20%3D%20figure(f%22Nation%2C%20%7Bseason.value%7D%3A%20bottom-up%2C%20top-down%20and%20the%20blend%22)%0A%20%20%20%20_ax.plot(_n.init_date%2C%20_n%5Bf%22%7B_d%7D__trend%22%5D%20%2F%20BU%2C%20color%3D%22%23999999%22%2C%20lw%3D1%2C%20ls%3D%22--%22%2C%20label%3D%22National%2025-season%20trend%22)%20%20%23%20fmt%3A%20skip%0A%20%20%20%20_ax.plot(_n.init_date%2C%20_n%5B_b%5D%20%2F%20BU%2C%20color%3D%22%2356B4E9%22%2C%20lw%3D1.6%2C%20label%3D%22B%2C%20bottom-up%22)%0A%20%20%20%20_ax.plot(_n.init_date%2C%20_t%20%2F%20BU%2C%20color%3D%22%23E69F00%22%2C%20lw%3D1.6%2C%20label%3D%22T%2C%20top-down%22)%0A%20%20%20%20_ax.plot(_n.init_date%2C%20_n%5B_d%5D%20%2F%20BU%2C%20color%3D%22%230072B2%22%2C%20lw%3D2.4%2C%20label%3D%22N%2C%20delivered%22)%0A%20%20%20%20_ax.axhline(nass%5Bseason.value%5D%2C%20color%3D%22black%22%2C%20lw%3D1.5%2C%20label%3D%22Final%20yield%20(USDA)%22)%0A%20%20%20%20season_axis(_ax%2C%20season.value)%0A%20%20%20%20legend(_ax%2C%203)%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%20%23%23%23%203.4%20The%20blend's%20weight%20on%20the%20top-down%20estimate%0A%0A%20%20%20%20w%2C%20the%20share%20of%20the%20blend%20given%20to%20T%2C%20at%20every%20forecast%20date%20of%20every%20season.%20The%20pattern%20is%20the%20same%20each%20year.%20Before%20the%20previous%20season's%20final%20is%20published%20on%201%20October%2C%20both%20estimates%20lean%20on%20the%20same%20old%20trend%20and%20the%20bottom-up%20nation%20is%20trusted.%20From%201%20October%20to%20December%2C%20T%20carries%20most%20of%20the%20weight.%20From%20January%20the%20counties'%20weather%20and%20area%20are%20better%20than%20a%20national%20line%2C%20and%20w%20falls%20back%20to%20about%20a%20tenth.%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20s3_stages_4()%3A%0A%20%20%20%20_d%20%3D%20column(%22c2c%22%2C%20%22__national_anchor%22)%0A%20%20%20%20_f%20%3D%20read(%22c2c%22%2C%20%5Bf%22%7B_d%7D__weight%22%5D%2C%20SEASONS)%0A%20%20%20%20_n%20%3D%20_f%5B_f.level%20%3D%3D%200%5D.assign(_week%3Dlambda%20x%3A%20(x.init_date%20-%20pd.to_datetime((x.season_year%20-%201).astype(str)%20%2B%20%22-08-02%22)).dt.days%20%2F%2F%207)%20%20%23%20fmt%3A%20skip%0A%20%20%20%20_grid%20%3D%20_n.pivot_table(index%3D%22season_year%22%2C%20columns%3D%22_week%22%2C%20values%3Df%22%7B_d%7D__weight%22)%0A%20%20%20%20_fig%2C%20_ax%20%3D%20plt.subplots(figsize%3D(10%2C%203.4)%2C%20layout%3D%22constrained%22)%0A%20%20%20%20_im%20%3D%20_ax.imshow(_grid.to_numpy()%2C%20aspect%3D%22auto%22%2C%20cmap%3D%22Blues%22%2C%20vmin%3D0%2C%20vmax%3D1)%0A%20%20%20%20_ticks%20%3D%20%5B0%2C%209%2C%2017%2C%2026%2C%2035%2C%2043%2C%2052%5D%0A%20%20%20%20_ax.set_xticks(_ticks%2C%20%5B(pd.Timestamp(2001%2C%208%2C%202)%20%2B%20pd.Timedelta(weeks%3Dint(_t))).strftime(%22%25-d%20%25b%22)%20for%20_t%20in%20_ticks%5D)%20%20%23%20fmt%3A%20skip%0A%20%20%20%20_ax.set_yticks(range(len(_grid))%2C%20_grid.index)%0A%20%20%20%20_fig.colorbar(_im%2C%20ax%3D_ax%2C%20label%3D%22w%2C%20weight%20on%20T%22)%0A%20%20%20%20_fig.suptitle(%22The%20anchor's%20weight%20on%20the%20top-down%20estimate%2C%20by%20season%20and%20forecast%20date%22%2C%20x%3D0.01%2C%20ha%3D%22left%22)%20%20%23%20fmt%3A%20skip%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%20%23%23%23%203.5%20The%20push-down%20factor%0A%0A%20%20%20%20N%20%C3%B7%20B%2C%20the%20factor%20every%20county%20and%20state%20is%20multiplied%20by%20so%20the%20levels%20add%20up.%20Every%20season%20in%20grey%2C%20the%20chosen%20one%20in%20colour.%20Above%201%20the%20anchor%20lifts%20the%20counties%3B%20below%201%20it%20lowers%20them.%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20s3_stages_5(season)%3A%0A%20%20%20%20_d%20%3D%20column(%22c2c%22%2C%20%22__national_anchor%22)%0A%20%20%20%20_f%20%3D%20read(%22c2c%22%2C%20%5Bf%22%7B_d%7D__factor%22%5D%2C%20SEASONS)%0A%20%20%20%20_n%20%3D%20_f%5B_f.level%20%3D%3D%200%5D%0A%20%20%20%20(_ax%2C)%20%3D%20figure(%22The%20push-down%20factor%20N%20%C3%B7%20B%2C%20every%20season%22%2C%20ylabel%3D%22factor%22)%0A%20%20%20%20for%20_s%2C%20_g%20in%20_n.groupby(%22season_year%22)%3A%0A%20%20%20%20%20%20%20%20_hi%20%3D%20_s%20%3D%3D%20season.value%0A%20%20%20%20%20%20%20%20_ax.plot(_g.init_date%20-%20pd.DateOffset(years%3Dint(_s)%20-%20season.value)%2C%20_g%5Bf%22%7B_d%7D__factor%22%5D%2C%20color%3D%22%230072B2%22%20if%20_hi%20else%20%22%23bbbbbb%22%2C%20lw%3D2%20if%20_hi%20else%200.8%2C%20zorder%3D3%20if%20_hi%20else%201%2C%20label%3Dstr(_s)%20if%20_hi%20else%20None)%20%20%23%20fmt%3A%20skip%0A%20%20%20%20_ax.axhline(1%2C%20color%3D%22black%22%2C%20lw%3D0.8)%0A%20%20%20%20season_axis(_ax%2C%20season.value)%0A%20%20%20%20legend(_ax%2C%201)%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%20%23%23%204%20%C2%B7%20How%20area%20comes%20in%0A%0A%20%20%20%20Yield%20is%20production%20over%20harvested%20area%2C%20so%20the%20area%20model's%20harvested%20area%20is%20what%20the%20counties%20are%20weighted%20by%20when%20they%20are%20added%20up.%20Its%20forecast%20changes%20through%20the%20season.%20The%20area%20model%20fuses%20each%20NASS%20survey%20on%20the%20day%20it%20is%20released%3A%20Winter%20Wheat%20Seedings%20in%20January%2C%20Prospective%20Plantings%20at%20the%20end%20of%20March%2C%20and%20Acreage%20at%20the%20end%20of%20June%20for%20planted%20area%3B%20the%20May%20and%20June%20Crop%20Production%20reports%20for%20harvested%20area.%20The%20dotted%20lines%20mark%20those%20releases%20in%20the%20chosen%20season.%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20s4_area(area_final%2C%20season)%3A%0A%20%20%20%20_f%20%3D%20read(%22c2c%22%2C%20%5B%22area_harvested_in_season_ha__adm0%22%5D%2C%20%5Bseason.value%5D)%0A%20%20%20%20_n%20%3D%20_f%5B_f.level%20%3D%3D%200%5D.sort_values(%22init_date%22)%0A%20%20%20%20_r%20%3D%20pd.read_parquet(DATA%20%2F%20%22inputs%2Fww_nass_inseason_qa_dated.parquet%22)%0A%20%20%20%20_r%20%3D%20_r%5B(_r.agg_level%20%3D%3D%20%22national%22)%20%26%20(_r.year%20%3D%3D%20season.value)%20%26%20~_r.is_final%20%26%20_r.release_date.notna()%5D%20%20%23%20fmt%3A%20skip%0A%20%20%20%20_final%20%3D%20area_final.get(season.value)%0A%20%20%20%20(_ax%2C)%20%3D%20figure(f%22National%20harvested%20area%20the%20yield%20model%20uses%2C%20%7Bseason.value%7D%22%2C%20ylabel%3D%22million%20acres%22)%20%20%23%20fmt%3A%20skip%0A%20%20%20%20_ax.plot(_n.init_date%2C%20_n.area_harvested_in_season_ha__adm0%20%2F%20404685.6%2C%20color%3D%22%23009E73%22%2C%20lw%3D2%2C%20label%3D%22Area%20model's%20harvested%20area%22)%20%20%23%20fmt%3A%20skip%0A%20%20%20%20if%20_final%20is%20not%20None%3A%0A%20%20%20%20%20%20%20%20_ax.axhline(_final%20%2F%20404685.6%2C%20color%3D%22black%22%2C%20lw%3D1.5%2C%20label%3D%22Final%20harvested%20area%20(USDA)%22)%0A%20%20%20%20for%20_%2C%20_rep%20in%20_r.drop_duplicates(%5B%22release_date%22%2C%20%22variable%22%5D).iterrows()%3A%0A%20%20%20%20%20%20%20%20_when%20%3D%20pd.Timestamp(_rep.release_date)%0A%20%20%20%20%20%20%20%20_ax.axvline(_when%2C%20color%3D%22%23CC79A7%22%20if%20_rep.variable%20%3D%3D%20%22area_harvested_ha%22%20else%20%22%23999999%22%2C%20ls%3D%22%3A%22%2C%20lw%3D1.2)%20%20%23%20fmt%3A%20skip%0A%20%20%20%20_ax.plot(%5B%5D%2C%20%5B%5D%2C%20color%3D%22%23999999%22%2C%20ls%3D%22%3A%22%2C%20label%3D%22Planted-area%20report%22)%0A%20%20%20%20_ax.plot(%5B%5D%2C%20%5B%5D%2C%20color%3D%22%23CC79A7%22%2C%20ls%3D%22%3A%22%2C%20label%3D%22Harvested-area%20report%22)%0A%20%20%20%20season_axis(_ax%2C%20season.value)%0A%20%20%20%20legend(_ax%2C%202)%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%20What%20that%20does%20to%20the%20national%20yield.%20When%20a%20report%20moves%20area%20between%20states%2C%20the%20nation's%20yield%20moves%20too%2C%20because%20states%20yield%20differently%3A%20more%20Kansas%20area%20pulls%20the%20national%20yield%20towards%20Kansas's.%20The%20line%20is%20the%20delivered%20national%20yield%20minus%20the%20same%20state%20yields%20weighted%20by%20the%20area%20known%20at%20the%20season's%20last%20forecast%2C%20so%20it%20isolates%20the%20effect%20of%20the%20area%20mix.%20It%20settles%20to%20zero%20as%20the%20area%20forecast%20reaches%20its%20final%20mix.%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20s4_area_2(season)%3A%0A%20%20%20%20_d%20%3D%20column(%22c2c%22%2C%20%22__national_anchor%22)%0A%20%20%20%20_f%20%3D%20read(%22c2c%22%2C%20%5B_d%2C%20%22area_harvested_in_season_ha__adm1%22%5D%2C%20%5Bseason.value%5D)%0A%20%20%20%20_s%20%3D%20_f%5B_f.level%20%3D%3D%201%5D%0A%20%20%20%20_last%20%3D%20_s%5B_s.init_date%20%3D%3D%20_s.init_date.max()%5D.set_index(%22state%22).area_harvested_in_season_ha__adm1%20%20%23%20fmt%3A%20skip%0A%20%20%20%20_now%20%3D%20_s.groupby(%22init_date%22).apply(lambda%20g%3A%20(g%5B_d%5D%20*%20g.area_harvested_in_season_ha__adm1).sum()%20%2F%20g.area_harvested_in_season_ha__adm1.sum()%2C%20include_groups%3DFalse)%20%20%23%20fmt%3A%20skip%0A%20%20%20%20_fixed%20%3D%20_s.groupby(%22init_date%22).apply(lambda%20g%3A%20(g%5B_d%5D%20*%20g.state.map(_last)).sum()%20%2F%20g.state.map(_last).sum()%2C%20include_groups%3DFalse)%20%20%23%20fmt%3A%20skip%0A%20%20%20%20(_ax%2C)%20%3D%20figure(f%22How%20the%20changing%20area%20mix%20moves%20the%20national%20yield%2C%20%7Bseason.value%7D%22)%0A%20%20%20%20_ax.plot(_now.index%2C%20(_now%20-%20_fixed)%20%2F%20BU%2C%20color%3D%22%23009E73%22%2C%20lw%3D2)%0A%20%20%20%20_ax.axhline(0%2C%20color%3D%22black%22%2C%20lw%3D0.8)%0A%20%20%20%20season_axis(_ax%2C%20season.value)%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%20%23%23%205%20%C2%B7%20Validation%2C%202011%20to%202025%0A%0A%20%20%20%20%23%23%23%205.1%20The%20weekly%20national%20forecast%20against%20the%20final%20yield%0A%0A%20%20%20%20Every%20weekly%20national%20forecast%20of%20every%20season%3B%20the%20black%20bar%20is%20the%20final%20yield.%20Tick%20the%20other%20two%20models%20to%20compare.%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20s5_valid(mo)%3A%0A%20%20%20%20others%20%3D%20mo.ui.multiselect(%5Bm%20for%20m%20in%20ARMS%20if%20m%20!%3D%20FINAL%5D%2C%20value%3D%5Bm%20for%20m%20in%20ARMS%20if%20m%20!%3D%20FINAL%5D%2C%20label%3D%22Compare%20with%22)%20%20%23%20fmt%3A%20skip%0A%20%20%20%20others%0A%20%20%20%20return%20(others%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20s5_valid_2(nass%2C%20national%2C%20others)%3A%0A%20%20%20%20(_ax%2C)%20%3D%20figure(%22Weekly%20national%20forecast%20and%20the%20final%20yield%2C%202011%E2%80%932025%22)%0A%20%20%20%20for%20_s%20in%20SEASONS%3A%0A%20%20%20%20%20%20%20%20_ax.hlines(nass%5B_s%5D%2C%20pd.Timestamp(_s%20-%201%2C%208%2C%202)%2C%20pd.Timestamp(_s%2C%208%2C%201)%2C%20color%3D%22black%22%2C%20lw%3D1.5)%0A%20%20%20%20%20%20%20%20for%20_m%20in%20%5BFINAL%2C%20*others.value%5D%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20_ax.plot(national%5B_m%5D.xs(_s%2C%20level%3D%22season_year%22)%2C%20color%3DARMS%5B_m%5D%5B1%5D%2C%20lw%3D1.1%2C%20label%3D_m%20if%20_s%20%3D%3D%20SEASONS%5B-1%5D%20else%20None)%20%20%23%20fmt%3A%20skip%0A%20%20%20%20_ax.plot(%5B%5D%2C%20%5B%5D%2C%20color%3D%22black%22%2C%20lw%3D1.5%2C%20label%3D%22Final%20yield%20(USDA)%22)%0A%20%20%20%20legend(_ax)%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%20%23%23%23%205.2%20Error%20by%20level%20and%20forecast%20date%0A%0A%20%20%20%20Mean%20absolute%20error%20as%20a%20percentage%20of%20the%20final%20yield%3A%20for%20the%20nation%2C%20the%20mean%20over%20seasons%3B%20for%20states%20and%20counties%2C%20%CE%A3%7Cerror%7C%20%C3%B7%20%CE%A3%20final%20(wMAPE)%2C%20then%20the%20mean%20over%20seasons%2C%20the%20convention%20the%20model-foundry%20evaluation%20uses.%20The%20weather%20correction%20lowers%20state%20and%20county%20error%20at%20every%20date%20from%20December.%20Nationally%20the%20three%20are%20close%3B%20the%20final%20model's%201%20July%20error%20is%20higher%2C%20but%20within%20what%2015%20seasons%20can%20tell%20apart%20(section%205.6).%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20s5_valid_3(errors_by_date%2C%20others)%3A%0A%20%20%20%20_axes%20%3D%20figure(%22Mean%20absolute%20error%20at%20each%20forecast%20date%22%2C%20ncols%3D3%2C%20ylabel%3D%22%25%20of%20the%20final%20yield%22)%20%20%23%20fmt%3A%20skip%0A%20%20%20%20for%20_ax%2C%20(_level%2C%20_name)%20in%20zip(_axes%2C%20LEVELS.items()%2C%20strict%3DTrue)%3A%0A%20%20%20%20%20%20%20%20for%20_m%20in%20%5BFINAL%2C%20*others.value%5D%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20_e%20%3D%20errors_by_date.loc%5B(_m%2C%20_level)%5D%0A%20%20%20%20%20%20%20%20%20%20%20%20_ax.plot(list(DATES)%2C%20_e.groupby(%22date%22%2C%20sort%3DFalse).pct.mean().reindex(list(DATES))%2C%20%22o-%22%2C%20ms%3D3%2C%20color%3DARMS%5B_m%5D%5B1%5D%2C%20label%3D_m%20if%20_level%20%3D%3D%200%20else%20None)%20%20%23%20fmt%3A%20skip%0A%20%20%20%20%20%20%20%20_ax.set_title(_name%2C%20loc%3D%22left%22%2C%20fontsize%3D%22medium%22)%0A%20%20%20%20%20%20%20%20_ax.tick_params(axis%3D%22x%22%2C%20labelsize%3D%22small%22%2C%20rotation%3D45)%0A%20%20%20%20_axes%5B0%5D.figure.legend(fontsize%3D%22small%22%2C%20frameon%3DFalse%2C%20ncols%3D3%2C%20loc%3D%22outside%20lower%20center%22)%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%20%23%23%23%205.3%20Drought%20and%20benign%20seasons%2C%20by%20window%0A%0A%20%20%20%20The%20same%20error%20averaged%20over%20each%20season's%20forecasts%20in%20three%20windows%2C%20for%20drought%20seasons%20(2011%2C%202013%2C%202014%2C%202018%2C%202022%2C%202023%3A%20a%20quarter%20or%20more%20of%20winter%20wheat%20area%20in%20severe%20drought%20in%20April)%20and%20the%20rest.%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20s5_valid_4(errors)%3A%0A%20%20%20%20_rows%20%3D%20%5B%5D%0A%20%20%20%20for%20(_m%2C%20_level)%2C%20_e%20in%20errors.groupby(%5B%22model%22%2C%20%22level%22%5D)%3A%0A%20%20%20%20%20%20%20%20for%20_w%2C%20_months%20in%20%7B%22All%22%3A%20tuple(range(1%2C%2013))%2C%20**WINDOWS%7D.items()%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20_x%20%3D%20_e%5B_e.init_date.dt.month.isin(_months)%5D%0A%20%20%20%20%20%20%20%20%20%20%20%20for%20_kind%2C%20_in%20in%20((%22drought%22%2C%20True)%2C%20(%22benign%22%2C%20False))%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20_y%20%3D%20_x%5B_x.season_year.isin(DROUGHT)%20%3D%3D%20_in%5D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20_rows.append((_m%2C%20LEVELS%5B_level%5D%2C%20_w%2C%20_kind%2C%20_y.groupby(%22season_year%22).pct.mean().mean()))%20%20%23%20fmt%3A%20skip%0A%20%20%20%20_t%20%3D%20pd.DataFrame(_rows%2C%20columns%3D%5B%22Model%22%2C%20%22Level%22%2C%20%22Window%22%2C%20%22Seasons%22%2C%20%22Error%2C%20%25%22%5D)%0A%20%20%20%20_t.pivot_table(index%3D%5B%22Level%22%2C%20%22Seasons%22%2C%20%22Window%22%5D%2C%20columns%3D%22Model%22%2C%20values%3D%22Error%2C%20%25%22%2C%20sort%3DFalse).round(2)%20%20%23%20fmt%3A%20skip%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%23%205.4%20Moving%20off%20the%20trend%20in%20out-of-trend%20seasons%0A%0A%20%20%20%20The%20share%20of%20a%20season's%20eventual%20departure%20from%20trend%20that%20the%20national%20forecast%20has%20already%20made%2C%20at%20each%20date%3A%200%20means%20it%20still%20sits%20on%20the%202%20August%20trend%2C%201%20means%20it%20has%20reached%20the%20final%20yield.%20It%20is%20pooled%20over%20the%20five%20seasons%20that%20ended%20furthest%20from%20their%20trend%20(the%20top%20third%20of%202011%E2%80%932025).%20The%20weather%20correction%20makes%20the%20final%20model%20move%20sooner.%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20s5_valid_5(nass%2C%20national%2C%20others)%3A%0A%20%20%20%20_d%20%3D%20column(%22c2c%22%2C%20%22__national_anchor%22)%0A%20%20%20%20_trend%20%3D%20read(%22c2c%22%2C%20%5Bf%22%7B_d%7D__trend%22%5D%2C%20SEASONS)%0A%20%20%20%20_trend%20%3D%20_trend%5B_trend.level%20%3D%3D%200%5D.sort_values(%22init_date%22).groupby(%22season_year%22)%5Bf%22%7B_d%7D__trend%22%5D.first()%20%20%23%20fmt%3A%20skip%0A%20%20%20%20_dep%20%3D%20np.log(pd.Series(%7B_s%3A%20nass%5B_s%5D%20*%20BU%20for%20_s%20in%20SEASONS%7D)%20%2F%20_trend)%0A%20%20%20%20_oot%20%3D%20_dep.abs().nlargest(5).index%0A%20%20%20%20(_ax%2C)%20%3D%20figure(f%22Share%20of%20the%20departure%20from%20trend%20captured%2C%20seasons%20%7B'%2C%20'.join(map(str%2C%20sorted(_oot)))%7D%22%2C%20ylabel%3D%22share%20captured%22)%20%20%23%20fmt%3A%20skip%0A%20%20%20%20for%20_m%20in%20%5BFINAL%2C%20*others.value%5D%3A%0A%20%20%20%20%20%20%20%20_rows%20%3D%20%5B%5D%0A%20%20%20%20%20%20%20%20for%20_s%20in%20_oot%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20_p%20%3D%20national%5B_m%5D.xs(_s%2C%20level%3D%22season_year%22)%20*%20BU%0A%20%20%20%20%20%20%20%20%20%20%20%20_rows.append(pd.DataFrame(%7B%22week%22%3A%20range(len(_p))%2C%20%22x%22%3A%20np.log(_p.to_numpy()%20%2F%20_trend%5B_s%5D)%20*%20_dep%5B_s%5D%2C%20%22d2%22%3A%20_dep%5B_s%5D%20**%202%7D))%20%20%23%20fmt%3A%20skip%0A%20%20%20%20%20%20%20%20_g%20%3D%20pd.concat(_rows).groupby(%22week%22)%5B%5B%22x%22%2C%20%22d2%22%5D%5D.sum()%0A%20%20%20%20%20%20%20%20_ax.plot(pd.Timestamp(2001%2C%208%2C%202)%20%2B%20pd.to_timedelta(_g.index%20*%207%2C%20%22D%22)%2C%20_g.x%20%2F%20_g.d2%2C%20color%3DARMS%5B_m%5D%5B1%5D%2C%20lw%3D1.8%2C%20label%3D_m)%20%20%23%20fmt%3A%20skip%0A%20%20%20%20_ax.axhline(0%2C%20color%3D%22black%22%2C%20lw%3D0.8)%0A%20%20%20%20_ax.axhline(1%2C%20color%3D%22black%22%2C%20lw%3D0.8%2C%20ls%3D%22%3A%22)%0A%20%20%20%20season_axis(_ax%2C%202002)%0A%20%20%20%20legend(_ax%2C%203)%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%20%23%23%23%205.5%20Against%20the%20systems%20clients%20have%20seen%0A%0A%20%20%20%20Each%20model's%20last%20national%20forecast%20of%20every%20season%2C%20beside%20the%20final%20yield.%20The%20legacy%20system%20(TMI)%20and%20Model%20Foundry%20as%20it%20was%20before%20this%20work%20are%20available%20for%202019%E2%80%932025%20only%2C%20from%20the%20Model%20Lab%20comparison%2C%20each%20on%20its%20own%20forecast%20dates.%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20s5_valid_6(nass%2C%20national%2C%20others)%3A%0A%20%20%20%20(_ax%2C)%20%3D%20figure(%22Last%20national%20forecast%20of%20each%20season%2C%20and%20the%20final%20yield%22)%0A%20%20%20%20_ax.plot(nass.loc%5BSEASONS%5B0%5D%20%3A%20SEASONS%5B-1%5D%5D%2C%20%22o-%22%2C%20color%3D%22black%22%2C%20ms%3D3%2C%20label%3D%22Final%20yield%20(USDA)%22)%0A%20%20%20%20for%20_m%20in%20%5BFINAL%2C%20*others.value%5D%3A%0A%20%20%20%20%20%20%20%20_ax.plot(national%5B_m%5D.groupby(level%3D%22season_year%22).last()%2C%20%22o-%22%2C%20ms%3D3%2C%20color%3DARMS%5B_m%5D%5B1%5D%2C%20label%3D_m)%20%20%23%20fmt%3A%20skip%0A%20%20%20%20_l%20%3D%20pd.read_csv(DATA%20%2F%20%22legacy%2Fnational_yield_paths.csv%22%2C%20parse_dates%3D%5B%22init_date%22%5D)%0A%20%20%20%20for%20_k%2C%20(_name%2C%20_colour)%20in%20LEGACY.items()%3A%0A%20%20%20%20%20%20%20%20_x%20%3D%20_l%5B(_l.system%20%3D%3D%20_k)%20%26%20(_l.season_year%20%3C%3D%20SEASONS%5B-1%5D)%5D.sort_values(%22init_date%22).groupby(%22season_year%22).yield_bu_ac.last()%20%20%23%20fmt%3A%20skip%0A%20%20%20%20%20%20%20%20_ax.plot(_x%2C%20%22s--%22%2C%20ms%3D3%2C%20lw%3D1%2C%20color%3D_colour%2C%20label%3D_name)%0A%20%20%20%20legend(_ax%2C%203)%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%20%23%23%23%205.6%20The%20final%20model%20against%20the%20first%20version%20of%20%23644%2C%20with%20uncertainty%0A%0A%20%20%20%20The%20final%20model's%20average%20error%20minus%20the%20first%20version's%2C%20season%20by%20season%2C%20with%20a%2095%25%20interval%2C%20at%20each%20level%20and%20three%20forecast%20dates.%20Left%20of%20zero%20means%20the%20final%20model%20is%20better%3B%20an%20interval%20crossing%20zero%20means%2015%20seasons%20cannot%20tell%20them%20apart.%20Counties%20improve%20clearly%3B%20the%20nation%20is%20unchanged.%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20s5_valid_7(errors_by_date)%3A%0A%20%20%20%20_other%20%3D%20%22First%20version%20of%20model-foundry%23644%22%0A%20%20%20%20_rows%20%3D%20%5B(f%22%7B_name%7D%2C%20%7B_date%7D%22%2C%20_level%2C%20_date)%20for%20_level%2C%20_name%20in%20LEVELS.items()%20for%20_date%20in%20%5B%221%20Dec%22%2C%20%221%20Apr%22%2C%20%221%20Jun%22%5D%5D%20%20%23%20fmt%3A%20skip%0A%20%20%20%20(_ax%2C)%20%3D%20figure(%22Final%20model%20minus%20the%20first%20version%3A%20average%20error%2C%2095%25%20interval%22%2C%20height%3D3.4%2C%20ylabel%3D%22%22)%20%20%23%20fmt%3A%20skip%0A%20%20%20%20for%20_y%2C%20(_label%2C%20_level%2C%20_date)%20in%20enumerate(_rows)%3A%0A%20%20%20%20%20%20%20%20_ea%2C%20_eb%20%3D%20errors_by_date.loc%5B(FINAL%2C%20_level)%5D%2C%20errors_by_date.loc%5B(_other%2C%20_level)%5D%0A%20%20%20%20%20%20%20%20_a%20%3D%20_ea%5B_ea.date%20%3D%3D%20_date%5D.set_index(%22season_year%22).pct%0A%20%20%20%20%20%20%20%20_b%20%3D%20_eb%5B_eb.date%20%3D%3D%20_date%5D.set_index(%22season_year%22).pct%0A%20%20%20%20%20%20%20%20_diff%20%3D%20(_a%20-%20_b).dropna()%0A%20%20%20%20%20%20%20%20_half%20%3D%202.145%20*%20_diff.std()%20%2F%20np.sqrt(len(_diff))%20%20%23%20t%20for%2015%20seasons%0A%20%20%20%20%20%20%20%20_ax.errorbar(_diff.mean()%2C%20_y%2C%20xerr%3D_half%2C%20fmt%3D%22o%22%2C%20ms%3D4%2C%20capsize%3D2%2C%20color%3D%22%230072B2%22%20if%20_diff.mean()%20%2B%20_half%20%3C%200%20else%20%220.5%22)%20%20%23%20fmt%3A%20skip%0A%20%20%20%20_ax.axvline(0%2C%20color%3D%22black%22%2C%20lw%3D0.8)%0A%20%20%20%20_ax.set_yticks(range(len(_rows))%2C%20%5Br%5B0%5D%20for%20r%20in%20_rows%5D)%0A%20%20%20%20_ax.invert_yaxis()%0A%20%20%20%20_ax.grid(axis%3D%22y%22%2C%20visible%3DFalse)%0A%20%20%20%20_ax.set_xlabel(%22percentage%20points%20of%20the%20final%20yield%22)%0A%20%20%20%20plt.show()%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20s0_load()%3A%0A%20%20%20%20%23%20every%20model's%20delivered%20yield%20at%20every%20level%3B%20truth%20is%20NASS's%20final%2C%20as%20published%0A%20%20%20%20_frames%20%3D%20%5B%5D%0A%20%20%20%20for%20_name%2C%20(_run%2C%20_)%20in%20ARMS.items()%3A%0A%20%20%20%20%20%20%20%20_d%20%3D%20column(_run%2C%20%22__national_anchor%22)%0A%20%20%20%20%20%20%20%20_f%20%3D%20read(_run%2C%20%5B_d%5D)%0A%20%20%20%20%20%20%20%20_frames.append(_f.rename(columns%3D%7B_d%3A%20%22point%22%7D).assign(model%3D_name))%0A%20%20%20%20forecasts%20%3D%20pd.concat(_frames%2C%20ignore_index%3DTrue)%0A%20%20%20%20_refs%20%3D%20pd.read_parquet(RUNS%20%2F%20%22c2c%2Ffeatures%2Freferences.parquet%22).query(%22source%20%3D%3D%20'nass'%20and%20statistic%20%3D%3D%20'final'%22)%20%20%23%20fmt%3A%20skip%0A%20%20%20%20truth%20%3D%20_refs.assign(geo_identifier%3D_refs.geo_identifier.astype(str)).set_index(%5B%22geo_identifier%22%2C%20%22season_year%22%5D).value%20%20%23%20fmt%3A%20skip%0A%20%20%20%20nass%20%3D%20truth.xs(USA)%20%2F%20BU%0A%20%20%20%20_area%20%3D%20pd.read_parquet(RUNS%20%2F%20%22area_final%2Ffeatures%2Freferences.parquet%22).query(%22variable%20%3D%3D%20'area_harvested_ha'%20and%20statistic%20%3D%3D%20'final'%22)%20%20%23%20fmt%3A%20skip%0A%20%20%20%20area_final%20%3D%20_area%5B_area.geo_identifier.astype(str)%20%3D%3D%20USA%5D.set_index(%22season_year%22).value%0A%20%20%20%20national%20%3D%20%7B%0A%20%20%20%20%20%20%20%20_m%3A%20_g%5B_g.level%20%3D%3D%200%5D.set_index(%5B%22season_year%22%2C%20%22init_date%22%5D).point.sort_index()%20%2F%20BU%0A%20%20%20%20%20%20%20%20for%20_m%2C%20_g%20in%20forecasts.groupby(%22model%22)%0A%20%20%20%20%7D%0A%20%20%20%20return%20area_final%2C%20forecasts%2C%20nass%2C%20national%2C%20truth%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20s0_load_2(forecasts%2C%20truth)%3A%0A%20%20%20%20%23%20one%20row%20per%20model%2C%20level%2C%20season%20and%20forecast%20date%3A%20the%20season-level%20error%20the%20evaluation%20reports%0A%20%20%20%20_f%20%3D%20forecasts.assign(truth%3Dtruth.reindex(pd.MultiIndex.from_frame(forecasts%5B%5B%22geo_identifier%22%2C%20%22season_year%22%5D%5D)).to_numpy())%20%20%23%20fmt%3A%20skip%0A%20%20%20%20_f%20%3D%20_f.dropna(subset%3D%5B%22truth%22%5D)%0A%20%20%20%20errors%20%3D%20(%0A%20%20%20%20%20%20%20%20_f.groupby(%5B%22model%22%2C%20%22level%22%2C%20%22season_year%22%2C%20%22init_date%22%5D)%0A%20%20%20%20%20%20%20%20.apply(lambda%20g%3A%20wmape(g%2C%20%22point%22%2C%20%22truth%22)%2C%20include_groups%3DFalse)%0A%20%20%20%20%20%20%20%20.rename(%22pct%22)%0A%20%20%20%20%20%20%20%20.reset_index()%0A%20%20%20%20)%0A%20%20%20%20_by%20%3D%20%5B%5D%0A%20%20%20%20for%20_date%20in%20DATES%3A%0A%20%20%20%20%20%20%20%20_x%20%3D%20month_day_rows(errors%2C%20_date).assign(date%3D_date)%0A%20%20%20%20%20%20%20%20_by.append(_x)%0A%20%20%20%20errors_by_date%20%3D%20pd.concat(_by).set_index(%5B%22model%22%2C%20%22level%22%5D).sort_index()%0A%20%20%20%20return%20errors%2C%20errors_by_date%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%20Limits%0A%0A%20%20%20%20-%20The%20forecasts%20are%20copies%20of%20scored%20hindcasts%20from%20the%20science%20box%20(%60~%2Fdata%2Fww-yield-usa%60)%2C%20made%20by%20the%20experiment%20branch%20whose%20delivered%20numbers%20model-foundry%23644%20reproduces%20bit%20for%20bit.%20The%20notebook%20fits%20nothing.%0A%20%20%20%20-%20Fifteen%20seasons%20are%20too%20few%20to%20rank%20the%20models%20nationally%3A%20there%2C%20the%20final%20model%20and%20the%20first%20version%20tie.%0A%20%20%20%20-%20The%20Plains%20correction%20reads%20only%20rain%20and%20dry%20days.%20Heat%20and%20condition%20ratings%20were%20tested%20and%20left%20out%3A%20heat%20added%20nothing%2C%20and%20condition%20ratings%20are%20kept%20out%20of%20yield%20on%20purpose%2C%20so%20that%20the%20weather%20stays%20this%20model's%20own%20signal.%0A%20%20%20%20-%20The%20legacy%20comparison%20covers%202019%E2%80%932026%20only%20and%20uses%20each%20system's%20own%20forecast%20dates.%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
61ee19d8fc371ca42722f0ccf62ec40e