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%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%20pyplot%20as%20plt%0A%0A%20%20%20%20BU%2C%20USA%2C%20SEASONS%20%3D%2062.77%2C%20%22ADM0%3AUSA%22%2C%20range(2011%2C%202026)%0A%20%20%20%20RUNS%20%3D%20(%0A%20%20%20%20%20%20%20%20Path.home()%20%2F%20%22data%2Fcorn-yield-usa%2Fruns%22%0A%20%20%20%20)%20%20%23%20local%20hindcast%20replays%3B%20nothing%20is%20refitted%20here%0A%20%20%20%20NEW%20%3D%20%22New%20model%22%0A%20%20%20%20MODELS%20%3D%20%7BNEW%3A%20(%22E1%22%2C%20%22%23257de0%22)%2C%20%22Shipped%20model%22%3A%20(%22M0-replay%22%2C%20%22%23f47230%22)%2C%20%22Spline%20model%22%3A%20(%22spline-52b4073e%22%2C%20%22%2300b894%22)%7D%20%20%23%20fmt%3A%20skip%0A%20%20%20%20INPUTS%20%3D%20%7B%22wb_flower%22%3A%20%22Water%20balance%20at%20flowering%22%2C%20%22edd29_fill%22%3A%20%22Heat%20during%20grain%20fill%22%2C%20%22plant_delay%22%3A%20%22Planting%20delay%22%7D%20%20%23%20fmt%3A%20skip%0A%20%20%20%20DATES%20%3D%20%7B%2228%20Mar%22%3A%20%2203-28%22%2C%20%2230%20May%22%3A%20%2205-30%22%2C%20%2227%20Jun%22%3A%20%2206-27%22%2C%20%221%20Aug%22%3A%20%2208-01%22%2C%20%2229%20Aug%22%3A%20%2208-29%22%2C%20%2226%20Sep%22%3A%20%2209-26%22%2C%20%2231%20Oct%22%3A%20%2210-31%22%7D%20%20%23%20fmt%3A%20skip%0A%20%20%20%20LEVELS%20%3D%20%7B0%3A%20%22Nation%22%2C%201%3A%20%22State%22%2C%202%3A%20%22County%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%0A%20%20%20%20def%20figure(title%2C%20ncols%3D1%2C%20height%3D3.2)%3A%0A%20%20%20%20%20%20%20%20%22%22%22Return%20a%20styled%20figure%20and%20its%20axes%2C%20y%20in%20bu%2Fac.%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(%22bu%2Fac%22)%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%20at_level(errors%2C%20level)%3A%0A%20%20%20%20%20%20%20%20%22%22%22Return%20the%20absolute%20errors%20of%20one%20level's%20rows.%22%22%22%0A%20%20%20%20%20%20%20%20return%20errors%5Berrors.index.get_level_values(%22geo_identifier%22).str.count(%22%2F%22)%20%3D%3D%20level%5D.abs()%0A%0A%20%20%20%20def%20season_mae(errors%2C%20level%2C%20date)%3A%0A%20%20%20%20%20%20%20%20%22%22%22Return%20each%20season's%20mean%20absolute%20error%20at%20one%20level%20and%20forecast%20date.%22%22%22%0A%20%20%20%20%20%20%20%20_e%20%3D%20at_level(errors%2C%20level)%0A%20%20%20%20%20%20%20%20_by%20%3D%20(%0A%20%20%20%20%20%20%20%20%20%20%20%20_e%5B_e.index.get_level_values(%22init_date%22).strftime(%22%25m-%25d%22)%20%3D%3D%20DATES%5Bdate%5D%5D%0A%20%20%20%20%20%20%20%20%20%20%20%20.groupby(%22season_year%22)%0A%20%20%20%20%20%20%20%20%20%20%20%20.mean()%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20assert%20set(_by.index)%20%3D%3D%20set(SEASONS)%2C%20(%0A%20%20%20%20%20%20%20%20%20%20%20%20f%22%7Bdate%7D%3A%20seasons%20%7Bsorted(set(SEASONS)%20-%20set(_by.index))%7D%20have%20no%20forecast%22%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20return%20_by%0A%0A%20%20%20%20def%20plot_paths(national%2C%20nass%2C%20models%2C%20season%3DNone)%3A%0A%20%20%20%20%20%20%20%20%22%22%22Plot%20the%20weekly%20national%20forecasts%2C%20each%20season's%20final%20yield%20as%20a%20black%20bar.%22%22%22%0A%20%20%20%20%20%20%20%20(_ax%2C)%20%3D%20figure(%0A%20%20%20%20%20%20%20%20%20%20%20%20%22Weekly%20national%20forecast%20and%20the%20final%20yield%22%20%2B%20(f%22%2C%20%7Bseason%7D%22%20if%20season%20else%20%22%22)%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20for%20_s%20in%20%5Bseason%5D%20if%20season%20else%20SEASONS%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20_ax.hlines(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20nass%5B_s%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20pd.Timestamp(_s%20-%201%2C%2011%2C%201)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20pd.Timestamp(_s%2C%2010%2C%2031)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20color%3D%22black%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20lw%3D1.5%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%20_m%20in%20models%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20_ax.plot(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20national%5B_m%5D.xs(_s)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20color%3DMODELS%5B_m%5D%5B1%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20lw%3D1.2%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20label%3D_m%20if%20_s%20%3D%3D%20(season%20or%20SEASONS%5B-1%5D)%20else%20None%2C%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_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%20%20%20%20%20_ax.legend(%0A%20%20%20%20%20%20%20%20%20%20%20%20fontsize%3D%22small%22%2C%20frameon%3DFalse%2C%20ncols%3D4%2C%20loc%3D%22lower%20left%22%2C%20bbox_to_anchor%3D(0%2C%201)%0A%20%20%20%20%20%20%20%20)%0A%0A%20%20%20%20def%20plot_trend(forecasts%2C%20truth%2C%20models)%3A%0A%20%20%20%20%20%20%20%20%22%22%22Plot%20each%20model's%201%20November%20forecast%2C%20made%20before%20any%20weather%2C%20against%20the%20final%20yield.%22%22%22%0A%20%20%20%20%20%20%20%20_axes%20%3D%20figure(%22The%20forecast%20on%201%20November%2C%20before%20any%20weather%22%2C%20ncols%3D3)%0A%20%20%20%20%20%20%20%20_ids%20%3D%20pd.Series(forecasts.index.get_level_values(%22geo_identifier%22).unique())%0A%20%20%20%20%20%20%20%20for%20_ax%2C%20_name%20in%20zip(_axes%2C%20%5B%22USA%22%2C%20%22iowa%22%2C%20%22kansas%22%5D%2C%20strict%3DTrue)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20_geo%20%3D%20_ids%5B_ids.str.lower().str.endswith(%22%3A%22%20%2B%20_name.lower())%5D.iloc%5B0%5D%0A%20%20%20%20%20%20%20%20%20%20%20%20_f%20%3D%20forecasts.xs(_geo).groupby(%22season_year%22).first()%20%2F%20BU%0A%20%20%20%20%20%20%20%20%20%20%20%20_ax.plot(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20_f.index%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20truth.loc%5B_geo%5D.reindex(_f.index)%20%2F%20BU%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22o-%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20color%3D%22black%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20ms%3D3%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20label%3D%22Final%20yield%20(USDA)%22%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%20_m%20in%20models%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20_ax.plot(_f.index%2C%20_f%5B_m%5D%2C%20color%3DMODELS%5B_m%5D%5B1%5D%2C%20lw%3D1.8%2C%20label%3D_m)%0A%20%20%20%20%20%20%20%20%20%20%20%20_ax.set_title(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20_name.title()%20if%20_name%20!%3D%20%22USA%22%20else%20%22Nation%22%2C%20loc%3D%22left%22%2C%20fontsize%3D%22medium%22%0A%20%20%20%20%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20_axes%5B0%5D.legend(fontsize%3D%22small%22%2C%20frameon%3DFalse)%0A%0A%20%20%20%20def%20plot_mae(errors%2C%20models)%3A%0A%20%20%20%20%20%20%20%20%22%22%22Plot%20mean%20absolute%20error%20by%20forecast%20date%20at%20each%20level.%22%22%22%0A%20%20%20%20%20%20%20%20_axes%20%3D%20figure(%22Average%20error%20by%20forecast%20date%2C%202011%E2%80%932025%22%2C%20ncols%3D3)%0A%20%20%20%20%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%20%20%20%20%20_t%20%3D%20pd.DataFrame(%7Bd%3A%20season_mae(errors%2C%20_level%2C%20d).mean()%20for%20d%20in%20DATES%7D).T%0A%20%20%20%20%20%20%20%20%20%20%20%20for%20_m%20in%20models%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20_ax.plot(_t.index%2C%20_t%5B_m%5D%2C%20%22o-%22%2C%20ms%3D3%2C%20color%3DMODELS%5B_m%5D%5B1%5D%2C%20label%3D_m)%0A%20%20%20%20%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%20%20%20%20_ax.tick_params(axis%3D%22x%22%2C%20labelrotation%3D45)%0A%20%20%20%20%20%20%20%20_axes%5B0%5D.legend(fontsize%3D%22small%22%2C%20frameon%3DFalse)%0A%0A%20%20%20%20def%20plot_moves(national%2C%20models)%3A%0A%20%20%20%20%20%20%20%20%22%22%22Plot%20each%20season's%20largest%20week-to-week%20change%20in%20the%20national%20forecast%2C%20June%20to%20October.%22%22%22%0A%20%20%20%20%20%20%20%20(_ax%2C)%20%3D%20figure(%22Largest%20weekly%20move%20in%20the%20national%20forecast%2C%20June%20to%20October%22%2C%20height%3D2.8)%0A%20%20%20%20%20%20%20%20_n%20%3D%20national%5Bnational.index.get_level_values(%22init_date%22).month.isin(range(6%2C%2011))%5D%0A%20%20%20%20%20%20%20%20_t%20%3D%20_n.groupby(%22season_year%22).diff().abs().groupby(%22season_year%22).max()%0A%20%20%20%20%20%20%20%20for%20_m%20in%20models%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20_ax.plot(_t.index%2C%20_t%5B_m%5D%2C%20%22o-%22%2C%20ms%3D3%2C%20color%3DMODELS%5B_m%5D%5B1%5D%2C%20label%3D_m)%0A%20%20%20%20%20%20%20%20_ax.legend(fontsize%3D%22small%22%2C%20frameon%3DFalse)%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%20US%20corn%20yield%3A%20the%20new%20forecasting%20model%0A%0A%20%20%20%20**Bottom%20line.**%20The%20new%20model%20is%20much%20better%20than%20the%20other%20two%20for%20counties%20and%20much%20steadier%20from%20week%20to%20week.%20For%20the%20nation%20as%20a%20whole%2C%20the%20three%20cannot%20be%20told%20apart.%0A%0A%20%20%20%20%7C%20%7C%20New%20model%20%7C%20Spline%20model%20%7C%20Shipped%20model%20%7C%0A%20%20%20%20%7C---%7C---%7C---%7C---%7C%0A%20%20%20%20%7C%20What%20it%20is%20%7C%20The%20spline%20model%20with%20three%20changes%20%7C%20The%20proposal%20in%20%5Bmodel-foundry%23629%5D(https%3A%2F%2Fgithub.com%2Ftreefera%2Fmodel-foundry%2Fpull%2F629)%20%7C%20What%20runs%20in%20production%20today%20%7C%0A%20%20%20%20%7C%20Fitted%20on%20%7C%20Counties%20%7C%20States%20%7C%20Counties%20%7C%0A%20%20%20%20%7C%20Long-run%20trend%20%7C%20A%20straight%20line%20per%20county%2C%20pulled%20towards%20the%20national%20slope%20%7C%20A%20straight%20line%20per%20state%2C%20pulled%20towards%20the%20national%20slope%20%7C%20A%20straight%20line%20per%20county%2C%20pulled%20towards%20the%20national%20slope%20of%20the%20last%2030%20seasons%20%7C%0A%20%20%20%20%7C%20Weather%20%7C%20Three%20inputs%20tied%20to%20crop%20stages%2C%20a%20smooth%20curve%20each%20%7C%20Four%20season%20totals%2C%20a%20smooth%20curve%20each%20%7C%20Five%20features%20in%20a%20gradient-boosted%20tree%20model%20%7C%0A%20%20%20%20%7C%20Trend%20and%20weather%20%7C%20Fitted%20together%20%7C%20Fitted%20together%20%7C%20Trend%20first%2C%20weather%20on%20what%20is%20left%20%7C%0A%20%20%20%20%7C%20Weather%20not%20yet%20seen%20%7C%20Counted%20at%20its%20average%20effect%20%7C%20Counted%20at%20its%20average%20value%20%7C%20Counted%20at%20its%20average%20value%20%7C%0A%0A%20%20%20%20**How%20to%20read%20this.**%20Each%20section%20opens%20with%20its%20finding%2C%20then%20shows%20it.%20Sections%201%20to%205%20show%20the%20new%20model%20alone%3B%20section%206%20adds%20the%20other%20two.%20Every%20season%20from%202011%20to%202025%20is%20forecast%20by%20a%20model%20fitted%20only%20on%20earlier%20seasons.%20Yields%20are%20in%20bushels%20per%20acre%20(bu%2Fac).%20The%20truth%20is%20the%20final%20yield%20published%20by%20USDA's%20statistics%20service%2C%20NASS%2C%20and%20error%20is%20forecast%20minus%20that%20figure.%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s0_load()%3A%0A%20%20%20%20def%20_load(run)%3A%0A%20%20%20%20%20%20%20%20_path%20%3D%20RUNS%20%2F%20run%20%2F%20%22hindcast%2Fyield%2Fpredictions.parquet%22%0A%20%20%20%20%20%20%20%20_col%20%3D%20next(c%20for%20c%20in%20pq.read_schema(_path).names%20if%20c.endswith(%22__bottom_up%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%20_path%2C%20columns%3D%5B*KEYS%2C%20_col%5D%2C%20filters%3D%5B(%22season_year%22%2C%20%22in%22%2C%20list(SEASONS))%5D%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20return%20_f.assign(geo_identifier%3D_f.geo_identifier.astype(str)).set_index(KEYS)%5B_col%5D%0A%0A%20%20%20%20%23%20only%20rows%20every%20model%20delivers%2C%20so%20section%206%20compares%20like%20with%20like%0A%20%20%20%20forecasts%20%3D%20pd.DataFrame(%7Bname%3A%20_load(run)%20for%20name%2C%20(run%2C%20_)%20in%20MODELS.items()%7D).dropna()%0A%20%20%20%20_refs%20%3D%20pd.read_parquet(RUNS%20%2F%20%22E1%2Ffeatures%2Freferences.parquet%22).query(%0A%20%20%20%20%20%20%20%20%22source%20%3D%3D%20'nass'%20and%20statistic%20%3D%3D%20'final'%22%0A%20%20%20%20)%0A%20%20%20%20truth%20%3D%20_refs.assign(geo_identifier%3D_refs.geo_identifier.astype(str)).set_index(KEYS%5B%3A2%5D).value%0A%20%20%20%20errors%20%3D%20(%0A%20%20%20%20%20%20%20%20forecasts.sub(truth.reindex(forecasts.index.droplevel(%22init_date%22)).to_numpy()%2C%20axis%3D0)%0A%20%20%20%20%20%20%20%20.div(BU)%0A%20%20%20%20%20%20%20%20.dropna()%0A%20%20%20%20)%0A%20%20%20%20national%20%3D%20forecasts.xs(USA)%20%2F%20BU%0A%20%20%20%20nass%20%3D%20truth.loc%5BUSA%5D%20%2F%20BU%0A%20%20%20%20return%20errors%2C%20forecasts%2C%20nass%2C%20national%2C%20truth%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%20The%20forecast%20closes%20on%20the%20final%20yield%20through%20the%20summer%0A%0A%20%20%20%20Every%20weekly%20national%20forecast.%20A%20season's%20forecasts%20run%20from%201%20November%20to%20the%20next%2031%20October%3B%20the%20black%20bar%20is%20the%20final%20yield%20USDA%20reported%20for%20it.%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s1_paths(nass%2C%20national)%3A%0A%20%20%20%20plot_paths(national%2C%20nass%2C%20%5BNEW%5D)%0A%20%20%20%20plt.show()%0A%0A%0A%40app.cell%0Adef%20s1_paths_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%222012%22%2C%20label%3D%22One%20season%22)%0A%20%20%20%20season%0A%20%20%20%20return%20(season%2C)%0A%0A%0A%40app.cell%0Adef%20s1_paths_3(nass%2C%20national%2C%20season)%3A%0A%20%20%20%20plot_paths(national%2C%20nass%2C%20%5BNEW%5D%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%202%20%C2%B7%20The%20trend%20is%20right%20for%20the%20corn%20belt%20and%20too%20high%20for%20the%20plains%0A%0A%20%20%20%20The%201%20November%20forecast%20is%20made%20before%20any%20of%20the%20season's%20weather%2C%20so%20it%20shows%20the%20trend%20alone.%20Kansas%20yields%20have%20been%20flat%20for%2030%20years%2C%20and%20the%20line%20has%20not%20come%20down%20to%20meet%20them.%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s2_trend(forecasts%2C%20truth)%3A%0A%20%20%20%20plot_trend(forecasts%2C%20truth%2C%20%5BNEW%5D)%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%20Weather%20moves%20the%20forecast%20only%20once%20it%20is%20observed%0A%0A%20%20%20%20What%20each%20weather%20input%20adds%20to%20or%20takes%20from%20the%20national%20forecast%2C%20in%20the%20season%20chosen%20above.%20Until%20an%20input's%20part%20of%20the%20season%20has%20happened%20it%20is%20counted%20at%20its%20average%20effect%2C%20so%20its%20line%20is%20flat.%0A%0A%20%20%20%20-%20**Water%20balance%20at%20flowering%3A**%20rain%20minus%20the%20water%20the%20crop%20needs%2C%20around%20flowering.%0A%20%20%20%20-%20**Heat%20during%20grain%20fill%3A**%20how%20far%20and%20how%20long%20temperatures%20ran%20above%2029%20%C2%B0C%20while%20the%20grain%20filled.%0A%20%20%20%20-%20**Planting%20delay%3A**%20how%20late%20planting%20ran%20against%20the%20usual%20date.%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s3_weather(season)%3A%0A%20%20%20%20_cols%20%3D%20%5Bf%22yield_kg_ha__predicted__%7Bi%7D%22%20for%20i%20in%20INPUTS%5D%0A%20%20%20%20_c%20%3D%20pd.read_parquet(RUNS%20%2F%20%22E1%2Fhindcast%2Fyield%2Fpredictions.parquet%22%2C%20columns%3D%5B*KEYS%2C%20*_cols%5D%2C%20filters%3D%5B(%22season_year%22%2C%20%22%3D%3D%22%2C%20season.value)%5D)%20%20%23%20fmt%3A%20skip%0A%20%20%20%20_c%20%3D%20_c%5B_c.geo_identifier.astype(str)%20%3D%3D%20USA%5D.set_index(%22init_date%22)%5B_cols%5D.sort_index()%20%2F%20BU%0A%20%20%20%20(_ax%2C)%20%3D%20figure(f%22What%20each%20weather%20input%20adds%2C%20nation%2C%20%7Bseason.value%7D%22%2C%20height%3D2.8)%0A%20%20%20%20_ax.plot(_c%2C%20lw%3D1.8%2C%20label%3Dlist(INPUTS.values()))%0A%20%20%20%20_ax.axhline(0%2C%20color%3D%22black%22%2C%20lw%3D0.8)%0A%20%20%20%20_ax.legend(fontsize%3D%22small%22%2C%20frameon%3DFalse%2C%20ncols%3D3)%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%20curves%20the%20model%20learned%3A%20what%20each%20input%20adds%20to%20or%20takes%20from%20a%20county's%20yield%20at%20each%20value.%20Each%20line%20is%20one%20season's%20fit%2C%20so%20close%20lines%20mean%20a%20stable%20curve.%20A%20dry%20flowering%20period%20costs%20the%20most%2C%20a%20very%20wet%20one%20costs%20too%2C%20and%20yield%20falls%20steadily%20with%20heat%20and%20with%20late%20planting.%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s3_weather_2()%3A%0A%20%20%20%20_cols%20%3D%20%7Bi%3A%20f%22yield_kg_ha__predicted__%7Bi%7D%22%20for%20i%20in%20INPUTS%7D%0A%20%20%20%20_filters%20%3D%20%5B(%22season_year%22%2C%20%22in%22%2C%20list(SEASONS))%5D%0A%20%20%20%20_x%20%3D%20pd.read_parquet(%0A%20%20%20%20%20%20%20%20RUNS%20%2F%20%22E1%2Ffeatures%2Fpredict.parquet%22%2C%20columns%3D%5B*KEYS%2C%20*INPUTS%5D%2C%20filters%3D_filters%0A%20%20%20%20)%0A%20%20%20%20_y%20%3D%20pd.read_parquet(RUNS%20%2F%20%22E1%2Fhindcast%2Fyield%2Fpredictions.parquet%22%2C%20columns%3D%5B*KEYS%2C%20*_cols.values()%5D%2C%20filters%3D_filters)%20%20%23%20fmt%3A%20skip%0A%20%20%20%20_xy%20%3D%20_x.merge(_y%2C%20on%3DKEYS)%0A%20%20%20%20%23%20counties%20at%20the%20season's%20last%20forecast%2C%20when%20every%20input%20is%20fully%20observed%0A%20%20%20%20_xy%20%3D%20_xy%5B(_xy.geo_identifier.astype(str).str.count(%22%2F%22)%20%3D%3D%202)%20%26%20(_xy.init_date%20%3D%3D%20_xy.groupby(%22season_year%22).init_date.transform(%22max%22))%5D%20%20%23%20fmt%3A%20skip%0A%20%20%20%20_units%20%3D%20%5B%22mm%20against%20normal%22%2C%20%22degree%20days%20against%20normal%22%2C%20%22days%20against%20usual%22%5D%0A%20%20%20%20_axes%20%3D%20figure(%22What%20each%20input%20adds%20at%20each%20value%2C%20one%20line%20per%20season's%20fit%22%2C%20ncols%3D3)%0A%20%20%20%20for%20_ax%2C%20(_i%2C%20_name)%2C%20_unit%20in%20zip(_axes%2C%20INPUTS.items()%2C%20_units%2C%20strict%3DTrue)%3A%0A%20%20%20%20%20%20%20%20for%20_%2C%20_s%20in%20_xy.groupby(%22season_year%22)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20_s%20%3D%20_s.dropna(subset%3D%5B_i%5D).sort_values(_i)%0A%20%20%20%20%20%20%20%20%20%20%20%20_ax.plot(_s%5B_i%5D%2C%20_s%5B_cols%5B_i%5D%5D%20%2F%20BU%2C%20color%3DMODELS%5BNEW%5D%5B1%5D%2C%20lw%3D0.8%2C%20alpha%3D0.6)%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%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.set_xlabel(_unit)%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%20National%20error%20halves%20between%20spring%20and%20autumn%0A%0A%20%20%20%20The%20average%20size%20of%20the%20error%20at%20seven%20forecast%20dates.%20States%20and%20counties%20improve%20less%2C%20because%20most%20of%20their%20error%20is%20in%20the%20long-run%20level%2C%20not%20the%20season's%20weather.%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s4_good(errors)%3A%0A%20%20%20%20plot_mae(errors%2C%20%5BNEW%5D)%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%20In%20most%20seasons%20the%20forecast%20never%20moves%20more%20than%202%20bu%2Fac%20in%20a%20week%0A%0A%20%20%20%20The%20largest%20change%20from%20one%20week%20to%20the%20next%20in%20each%20season.%202012%20was%20a%20severe%20drought%2C%20so%20the%20large%20move%20there%20is%20correct.%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s5_steady(national)%3A%0A%20%20%20%20plot_moves(national%2C%20%5BNEW%5D)%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%206%20%C2%B7%20Against%20the%20other%20two%3A%20better%20for%20counties%2C%20steadier%2C%20level%20nationally%0A%0A%20%20%20%20First%20the%20big%20picture%3A%20each%20model's%20last%20forecast%20of%20every%20season%20from%202010%20beside%20the%20final%20yield%20(2026%20has%20no%20final%20yield%20yet%2C%20and%20the%20spline%20model%20has%20no%202026%20forecast).%20Then%20the%20same%20four%20charts%20with%20the%20shipped%20model%20and%20the%20spline%20model%20added%3B%20untick%20one%20to%20drop%20it.%20The%20last%20chart%20shows%20the%20new%20model's%20average%20error%20minus%20each%20other%20model's%2C%20season%20by%20season%2C%20with%20a%2095%25%20interval.%20Left%20of%20zero%20means%20the%20new%20model%20is%20better.%20An%20interval%20that%20crosses%20zero%20means%20the%20data%20cannot%20tell%20the%20two%20apart.%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s6_compare(mo)%3A%0A%20%20%20%20others%20%3D%20mo.ui.multiselect(%5Bm%20for%20m%20in%20MODELS%20if%20m%20!%3D%20NEW%5D%2C%20value%3D%5Bm%20for%20m%20in%20MODELS%20if%20m%20!%3D%20NEW%5D%2C%20label%3D%22Compare%20the%20new%20model%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%0Adef%20s6_compare_2(nass%2C%20others)%3A%0A%20%20%20%20(_ax%2C)%20%3D%20figure(%22Each%20model's%20last%20national%20forecast%20of%20the%20season%2C%20and%20the%20final%20yield%22)%0A%20%20%20%20_ax.plot(nass.loc%5B2010%3A%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%5BNEW%2C%20*others.value%5D%3A%0A%20%20%20%20%20%20%20%20_path%20%3D%20RUNS%20%2F%20MODELS%5B_m%5D%5B0%5D%20%2F%20%22hindcast%2Fyield%2Fpredictions.parquet%22%0A%20%20%20%20%20%20%20%20_col%20%3D%20next(c%20for%20c%20in%20pq.read_schema(_path).names%20if%20c.endswith(%22__bottom_up%22))%0A%20%20%20%20%20%20%20%20_f%20%3D%20pd.read_parquet(_path%2C%20columns%3D%5B*KEYS%2C%20_col%5D%2C%20filters%3D%5B(%22season_year%22%2C%20%22%3E%3D%22%2C%202010)%5D)%0A%20%20%20%20%20%20%20%20_f%20%3D%20_f%5B_f.geo_identifier.astype(str)%20%3D%3D%20USA%5D.sort_values(%22init_date%22)%0A%20%20%20%20%20%20%20%20_ax.plot(%0A%20%20%20%20%20%20%20%20%20%20%20%20_f.groupby(%22season_year%22)%5B_col%5D.last()%20%2F%20BU%2C%20%22o-%22%2C%20ms%3D3%2C%20color%3DMODELS%5B_m%5D%5B1%5D%2C%20label%3D_m%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20_ax.legend(fontsize%3D%22small%22%2C%20frameon%3DFalse%2C%20ncols%3D4)%0A%20%20%20%20plt.show()%0A%0A%0A%40app.cell%0Adef%20s6_compare_3(nass%2C%20national%2C%20others%2C%20season)%3A%0A%20%20%20%20plot_paths(national%2C%20nass%2C%20%5BNEW%2C%20*others.value%5D%2C%20season.value)%0A%20%20%20%20plt.show()%0A%0A%0A%40app.cell%0Adef%20s6_compare_4(forecasts%2C%20others%2C%20truth)%3A%0A%20%20%20%20plot_trend(forecasts%2C%20truth%2C%20%5BNEW%2C%20*others.value%5D)%0A%20%20%20%20plt.show()%0A%0A%0A%40app.cell%0Adef%20s6_compare_5(errors%2C%20others)%3A%0A%20%20%20%20plot_mae(errors%2C%20%5BNEW%2C%20*others.value%5D)%0A%20%20%20%20plt.show()%0A%0A%0A%40app.cell%0Adef%20s6_compare_6(national%2C%20others)%3A%0A%20%20%20%20plot_moves(national%2C%20%5BNEW%2C%20*others.value%5D)%0A%20%20%20%20plt.show()%0A%0A%0A%40app.cell%0Adef%20s6_compare_7(errors%2C%20others)%3A%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%2227%20Jun%22%2C%20%2229%20Aug%22%2C%20%2231%20Oct%22%5D%5D%20%20%23%20fmt%3A%20skip%0A%20%20%20%20_axes%20%3D%20figure(%0A%20%20%20%20%20%20%20%20%22New%20model%20minus%20the%20other%3A%20average%20error%2C%2095%25%20interval%22%2C%0A%20%20%20%20%20%20%20%20ncols%3Dmax(len(others.value)%2C%201)%2C%0A%20%20%20%20%20%20%20%20height%3D3.4%2C%0A%20%20%20%20)%0A%20%20%20%20for%20_ax%2C%20_m%20in%20zip(_axes%2C%20others.value%2C%20strict%3DFalse)%3A%0A%20%20%20%20%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%20%20%20%20_s%20%3D%20season_mae(errors%2C%20_level%2C%20_date)%0A%20%20%20%20%20%20%20%20%20%20%20%20_d%20%3D%20_s%5BNEW%5D%20-%20_s%5B_m%5D%0A%20%20%20%20%20%20%20%20%20%20%20%20_half%20%3D%202.145%20*%20_d.std()%20%2F%20np.sqrt(len(_d))%20%20%23%20t%20for%2015%20seasons%0A%20%20%20%20%20%20%20%20%20%20%20%20_ax.errorbar(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20_d.mean()%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20_y%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20xerr%3D_half%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20fmt%3D%22o%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20ms%3D4%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20capsize%3D2%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20color%3DMODELS%5BNEW%5D%5B1%5D%20if%20_d.mean()%20%2B%20_half%20%3C%200%20else%20%220.5%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20_ax.axvline(0%2C%20color%3D%22black%22%2C%20lw%3D0.8)%0A%20%20%20%20%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%20%20%20%20_ax.invert_yaxis()%0A%20%20%20%20%20%20%20%20_ax.grid(axis%3D%22y%22%2C%20visible%3DFalse)%0A%20%20%20%20%20%20%20%20_ax.set_title(f%22against%20%7B_m%7D%22%2C%20loc%3D%22left%22%2C%20fontsize%3D%22medium%22)%0A%20%20%20%20%20%20%20%20_ax.set_ylabel(%22%22)%0A%20%20%20%20%20%20%20%20_ax.set_xlabel(%22bu%2Fac%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%20Limits%0A%0A%20%20%20%20-%20The%20new%20model%20has%20only%20been%20run%20on%20one%20machine%2C%20from%20files%20under%20%60~%2Fdata%2Fcorn-yield-usa%2Fruns%60.%20Of%20its%20three%20changes%2C%20only%20the%20weather%20inputs%20are%20in%20the%20shared%20feature%20library%20so%20far.%0A%20%20%20%20-%20Fifteen%20seasons%20are%20too%20few%20to%20rank%20the%20three%20models%20nationally.%0A%20%20%20%20-%20From%20August%2C%20USDA's%20own%20monthly%20forecast%20is%20more%20accurate%20nationally%20than%20all%20three.%20These%20models%20earn%20their%20place%20before%20August%2C%20for%20states%20and%20counties%2C%20and%20in%20the%20weeks%20between%20USDA's%20releases.%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
0fe6ae538deae055a65b2987f238b512