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%20matplotlib.pyplot%20as%20plt%0A%20%20%20%20import%20numpy%20as%20np%0A%20%20%20%20import%20pandas%20as%20pd%0A%20%20%20%20import%20xarray%20as%20xr%0A%20%20%20%20from%20matplotlib.ticker%20import%20MaxNLocator%0A%20%20%20%20from%20scipy%20import%20ndimage%2C%20optimize%2C%20stats%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.1%20The%20Gaussian%0A%0A%20%20%20%20**What%20you'll%20learn**%0A%0A%20%20%20%201.%20What%20the%20Gaussian%20(normal)%20distribution%20is%2C%20what%20its%20two%20numbers%20mean%2C%20and%20why%20sums%20of%20many%20small%20effects%20end%20up%20bell-shaped.%0A%20%20%20%202.%20The%20three%20jobs%20it%20does%20in%20our%20production%20code%3A%20a%20**yardstick**%20for%20%22how%20unusual%22%20(z-scores)%2C%20a%20**weighting%20shape**%20(smoothers%20and%20phenology%20windows)%2C%20and%20a%20**noise%20assumption**%20(standard%20errors%20and%20bands).%0A%20%20%20%203.%20Where%20the%20assumption%20breaks%20for%20crop%20data%2C%20and%20what%20our%20code%20does%20instead.%0A%0A%20%20%20%20**How%20to%20run%3A**%20follow%20the%20set-up%20in%20%60academy%2FREADME.md%60%2C%20then%20from%20%60academy%2F%60%2C%20open%20it%20with%20%60uv%20run%20marimo%20edit%200-foundations%2F0.1-the-gaussian%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%20in%20under%20a%20minute%2C%20most%20of%20it%20spent%20importing%20the%20production%20packages.%0A%0A%20%20%20%20The%20production%20code%20is%20imported%2C%20not%20copied%3A%20%60tf-data-ml-utils%60%20at%20%60c0ba9dc%60%20and%20model-foundry%20at%20%60f69eef9%60%2C%20pinned%20in%20%60pyproject.toml%60.%20%60show(...)%60%20prints%20the%20real%20source%20with%20a%20permalink.%20One%20short%20function%20comes%20from%20a%20package%20this%20environment%20doesn't%20install%20(cocoa's%20model)%3B%20it%20appears%20as%20a%20labelled%2C%20verbatim%20copy%20with%20its%20permalink.%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%20Part%201%3A%20the%20libraries%2C%20as%20their%20docs%20introduce%20them%0A%0A%20%20%20%20%23%23%201.1%20Drawing%20numbers%20from%20a%20Gaussian%0A%0A%20%20%20%20NumPy's%20%5Brandom%20%60Generator%60%5D(https%3A%2F%2Fnumpy.org%2Fdoc%2Fstable%2Freference%2Frandom%2Fgenerated%2Fnumpy.random.Generator.normal.html)%20draws%20from%20a%20normal%20distribution%20with%20%60normal(loc%2C%20scale%2C%20size)%60.%20%60loc%60%20is%20the%20mean%20%CE%BC%20and%20%60scale%60%20is%20the%20standard%20deviation%20%CF%83.%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s1_1_drawing_numbers_gaussian()%3A%0A%20%20%20%20_rng%20%3D%20np.random.default_rng(2026)%20%20%23%20each%20experiment%20seeds%20its%20own%20generator%0A%20%20%20%20draws%20%3D%20_rng.normal(loc%3D10.0%2C%20scale%3D2.0%2C%20size%3D200_000)%0A%20%20%20%20print(f%22sample%20mean%20%7Bdraws.mean()%3A.3f%7D%20%20(%CE%BC%20%3D%2010)%22)%0A%20%20%20%20print(f%22sample%20std%20%20%7Bdraws.std(ddof%3D1)%3A.3f%7D%20%20(%CF%83%20%3D%202)%22)%0A%20%20%20%20return%20(draws%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%23%201.2%20The%20distribution%20as%20an%20object%3A%20%60scipy.stats.norm%60%0A%0A%20%20%20%20%5B%60scipy.stats.norm%60%5D(https%3A%2F%2Fdocs.scipy.org%2Fdoc%2Fscipy%2Freference%2Fgenerated%2Fscipy.stats.norm.html)%20gives%20the%20three%20functions%20you'll%20use%20most%3A%0A%0A%20%20%20%20%7C%20Method%20%7C%20Answers%20%7C%20Example%20%7C%0A%20%20%20%20%7C---%7C---%7C---%7C%0A%20%20%20%20%7C%20%60pdf(x)%60%20%7C%20how%20dense%20the%20probability%20is%20at%20x%20(the%20height%20of%20the%20bell)%20%7C%20%60norm.pdf(0)%60%20%E2%89%88%200.399%3A%20a%20height%2C%20not%20a%20probability%20%7C%0A%20%20%20%20%7C%20%60cdf(x)%60%20%7C%20the%20probability%20of%20a%20value%20%E2%89%A4%20x%20%7C%20%60norm.cdf(1)%60%20%E2%89%88%200.841%20%7C%0A%20%20%20%20%7C%20%60ppf(p)%60%20%7C%20the%20inverse%20of%20%60cdf%60%3A%20the%20value%20with%20probability%20p%20below%20it%20%7C%20%60norm.ppf(0.975)%60%20%E2%89%88%201.960%20%7C%0A%0A%20%20%20%20Probabilities%20are%20**areas**%20under%20the%20density%2C%20so%20the%20probability%20of%20a%20range%20is%20a%20difference%20of%20%60cdf%60%20values%3A%20%60norm.cdf(1)%20-%20norm.cdf(-1)%60%20is%20the%2068%20%25%20within%20%C2%B11%CF%83.%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s1_2_distribution_object_scipy()%3A%0A%20%20%20%20_z%20%3D%20np.linspace(-4%2C%204%2C%20401)%0A%20%20%20%20_fig%2C%20_ax%20%3D%20plt.subplots()%0A%20%20%20%20_ax.plot(_z%2C%20stats.norm.pdf(_z)%2C%20color%3D%22k%22)%0A%20%20%20%20for%20_k%2C%20shade%20in%20((1%2C%200.35)%2C%20(2%2C%200.2)%2C%20(3%2C%200.1))%3A%0A%20%20%20%20%20%20%20%20_ax.fill_between(_z%2C%20stats.norm.pdf(_z)%2C%20where%3Dnp.abs(_z)%20%3C%3D%20_k%2C%20alpha%3Dshade%2C%20color%3D%22C0%22)%0A%20%20%20%20_ax.set(%0A%20%20%20%20%20%20%20%20xlabel%3D%22z%20%3D%20(x%20%E2%88%92%20%CE%BC)%20%2F%20%CF%83%22%2C%0A%20%20%20%20%20%20%20%20ylabel%3D%22density%22%2C%0A%20%20%20%20%20%20%20%20title%3D%22The%20standard%20normal%3A%20%C2%B11%CF%83%2C%20%C2%B12%CF%83%2C%20%C2%B13%CF%83%20shaded%22%2C%0A%20%20%20%20)%0A%20%20%20%20plt.show()%0A%0A%20%20%20%20for%20_k%20in%20(1%2C%202%2C%203)%3A%0A%20%20%20%20%20%20%20%20inside%20%3D%20stats.norm.cdf(_k)%20-%20stats.norm.cdf(-_k)%0A%20%20%20%20%20%20%20%20print(f%22P(%7Cz%7C%20%E2%89%A4%20%7B_k%7D)%20%3D%20%7Binside%3A.4f%7D%22)%0A%20%20%20%20print(%0A%20%20%20%20%20%20%20%20f%22norm.ppf(0.975)%20%3D%20%7Bstats.norm.ppf(0.975)%3A.4f%7D%20%20%20norm.ppf(0.84)%20%3D%20%7Bstats.norm.ppf(0.84)%3A.4f%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%20%23%23%201.3%20Smoothing%20with%20%60scipy.ndimage.gaussian_filter1d%60%0A%0A%20%20%20%20%5B%60gaussian_filter1d%60%5D(https%3A%2F%2Fdocs.scipy.org%2Fdoc%2Fscipy%2Freference%2Fgenerated%2Fscipy.ndimage.gaussian_filter1d.html)%20replaces%20each%20value%20by%20a%20Gaussian-weighted%20average%20of%20its%20neighbours.%20Three%20arguments%20matter%3A%20%60sigma%60%20(the%20width%2C%20in%20samples)%2C%20%60truncate%60%20(where%20the%20kernel%20stops%2C%204%CF%83%20by%20default)%20and%20%60mode%60%20(what%20to%20assume%20beyond%20the%20ends).%20Smoothing%20a%20single%20spike%20shows%20the%20kernel%20itself%2C%20and%20smoothing%20a%20ramp%20shows%20what%20%60mode%3D%22nearest%22%60%20does%20at%20the%20end%3A%20it%20pads%20with%20copies%20of%20the%20last%20value%2C%20so%20the%20smoothed%20end%20trails%20the%20ramp.%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s1_3_smoothing_scipy_ndimage()%3A%0A%20%20%20%20spike%20%3D%20np.zeros(11)%0A%20%20%20%20spike%5B5%5D%20%3D%201.0%0A%20%20%20%20print(%22a%20spike%2C%20smoothed%20(%CF%83%20%3D%201)%3A%22%2C%20ndimage.gaussian_filter1d(spike%2C%20sigma%3D1).round(3))%0A%20%20%20%20ramp%20%3D%20np.arange(10.0)%0A%20%20%20%20print(%22a%20ramp's%20last%203%20values%3A%20%20%20%22%2C%20ramp%5B-3%3A%5D)%0A%20%20%20%20print(%0A%20%20%20%20%20%20%20%20%22smoothed%2C%20mode%3D'nearest'%3A%22%2C%0A%20%20%20%20%20%20%20%20ndimage.gaussian_filter1d(ramp%2C%20sigma%3D2%2C%20mode%3D%22nearest%22)%5B-3%3A%5D.round(2)%2C%0A%20%20%20%20)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%201.4%20Percentiles%20with%20%60np.quantile%60%2C%20and%20its%20%60method%60%0A%0A%20%20%20%20%5B%60np.quantile%60%5D(https%3A%2F%2Fnumpy.org%2Fdoc%2Fstable%2Freference%2Fgenerated%2Fnumpy.quantile.html)%20has%20several%20definitions%20of%20a%20sample%20quantile%20(Hyndman%20and%20Fan%2C%201996).%20The%20default%2C%20%60%22linear%22%60%2C%20interpolates%20between%20neighbouring%20sorted%20values%3B%20%60%22inverted_cdf%22%60%20returns%20an%20actual%20data%20value.%20The%20difference%20matters%20for%20prediction%20bands%20built%20from%20few%20seasons%20(section%204.2).%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s1_4_percentiles_np_quantile()%3A%0A%20%20%20%20values%20%3D%20np.array(%5B1.0%2C%202.0%2C%203.0%2C%204.0%5D)%0A%20%20%20%20for%20method%20in%20(%22linear%22%2C%20%22inverted_cdf%22)%3A%0A%20%20%20%20%20%20%20%20print(%0A%20%20%20%20%20%20%20%20%20%20%20%20f%22%7Bmethod%3A%3E13%7D%3A%2095th%20percentile%20of%20%7Bvalues%7D%20%3D%20%7Bnp.quantile(values%2C%200.95%2C%20method%3Dmethod)%3A.2f%7D%22%0A%20%20%20%20%20%20%20%20)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%20Part%202%3A%20our%20production%20code%0A%0A%20%20%20%20%23%23%202.1%20The%20yardstick%3A%20z-scores%20in%20%60tf-data-ml-utils%60%0A%0A%20%20%20%20Every%20%60z_%60%20feature%20(%60z_ndvi%60%2C%20%60z_tmax%60%20and%20so%20on)%20comes%20from%20%60compute_zscore%60.%20Its%20spread%20is%20half%20the%20gap%20between%20the%2016th%20and%2084th%20percentiles%3A%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s2_1_yardstick_z_scores()%3A%0A%20%20%20%20from%20tf_data_ml_utils.features.core.climo_helpers%20import%20climatology_spread%2C%20compute_zscore%0A%0A%20%20%20%20show(climatology_spread)%0A%20%20%20%20show(compute_zscore)%0A%20%20%20%20return%20(compute_zscore%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%20Why%2016%20and%2084%3F%20From%201.2%2C%2068%20%25%20of%20a%20Gaussian%20lies%20within%20%C2%B11%CF%83%2C%20leaving%2016%20%25%20in%20each%20tail.%20So%20the%2016th%20and%2084th%20percentiles%20sit%20at%20%CE%BC%20%C2%B1%20%CF%83%20(more%20exactly%20%C2%B10.9945%CF%83%2C%20the%20%60ppf(0.84)%60%20above)%2C%20and%20half%20the%20gap%20between%20them%20approximates%20%CF%83.%20Let's%20check%20on%20the%20draws%20from%201.1%2C%20then%20run%20the%20production%20function%3A%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s2_1_yardstick_z_scores_2(compute_zscore%2C%20draws)%3A%0A%20%20%20%20_q16%2C%20_q84%20%3D%20np.quantile(draws%2C%20%5B0.16%2C%200.84%5D)%0A%20%20%20%20print(f%22(q84%20-%20q16)%20%2F%202%20%3D%20%7B(_q84%20-%20_q16)%20%2F%202%3A.4f%7D%20%20%20sample%20std%20%3D%20%7Bdraws.std(ddof%3D1)%3A.4f%7D%22)%0A%0A%20%20%20%20_clim%20%3D%20xr.Dataset(%0A%20%20%20%20%20%20%20%20%7B%22mean%22%3A%20xr.DataArray(0.6)%2C%20%22q16%22%3A%20xr.DataArray(0.5)%2C%20%22q84%22%3A%20xr.DataArray(0.7)%7D%0A%20%20%20%20)%0A%20%20%20%20obs%20%3D%20xr.DataArray(%5B0.6%2C%200.7%2C%200.4%2C%203.0%5D%2C%20dims%3D%22time%22)%0A%20%20%20%20print(%22compute_zscore%3A%22%2C%20compute_zscore(obs%2C%20_clim).values)%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%20spread%20is%20(0.7%20%E2%88%92%200.5)%20%2F%202%20%3D%200.1%2C%20so%200.7%20scores%20%2B1%20and%200.4%20scores%20%E2%88%922.%203.0%20would%20be%20%2B24%20and%20is%20clipped%20to%20the%20ceiling%2C%20%60DEFAULT_Z_MAX%60%20%3D%2010.%20Under%20a%20true%20Gaussian%20%7Cz%7C%20%3E%2010%20happens%20about%20once%20in%2010%C2%B2%C2%B3%20draws%2C%20so%20a%20score%20that%20large%20says%20the%20Gaussian%20picture%20doesn't%20fit%20that%20value%3A%20bad%20data%2C%20or%20a%20spread%20estimated%20too%20small%20(a%20genuine%20drought%20of%200.30%20against%20a%20mean%20of%200.60%20and%20a%20spread%20of%200.02%20scores%20%E2%88%9215).%20The%20clip%20only%20caps%20the%20score%3B%20it%20doesn't%20judge%20the%20observation.%0A%0A%20%20%20%20%23%23%202.2%20The%20weighting%20shape%3A%20%60GaussianSmoothTrend%60%20in%20%60yield_module%60%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s2_2_weighting_shape_gaussiansmoothtrend()%3A%0A%20%20%20%20from%20model_foundry.common.modelling.model_core%20import%20GEO_IDENTIFIER%2C%20SEASON_YEAR%0A%20%20%20%20from%20yield_module.models.gaussian_smooth_trend%20import%20GaussianSmoothTrend%2C%20smooth%0A%0A%20%20%20%20show(smooth)%0A%20%20%20%20show(GaussianSmoothTrend.predict)%0A%20%20%20%20return%20GEO_IDENTIFIER%2C%20GaussianSmoothTrend%2C%20SEASON_YEAR%0A%0A%0A%40app.cell%0Adef%20s2_2_weighting_shape_gaussiansmoothtrend_2(%0A%20%20%20%20GEO_IDENTIFIER%2C%0A%20%20%20%20GaussianSmoothTrend%2C%0A%20%20%20%20SEASON_YEAR%2C%0A)%3A%0A%20%20%20%20_rng%20%3D%20np.random.default_rng(2026)%20%20%23%20each%20experiment%20seeds%20its%20own%20generator%0A%20%20%20%20seasons%20%3D%20np.arange(2000%2C%202021)%0A%20%20%20%20true_trend%20%3D%20150%20%2B%201.8%20*%20(seasons%20-%202000)%0A%20%20%20%20yields%20%3D%20true_trend%20%2B%20_rng.normal(0%2C%206%2C%20seasons.size)%0A%20%20%20%20yields%5B12%5D%20-%3D%2040%20%20%23%20a%202012-style%20drought%0A%20%20%20%20panel%20%3D%20pd.DataFrame(%7BGEO_IDENTIFIER%3A%20%22USA.17.001%22%2C%20SEASON_YEAR%3A%20seasons%2C%20%22yield%22%3A%20yields%7D)%0A%0A%20%20%20%20sigma_seasons%20%3D%204%20%20%23%20exercise%203%20changes%20this%3B%20the%20fit%20and%20the%20end-effect%20diagnostic%20both%20use%20it%0A%20%20%20%20trend%20%3D%20GaussianSmoothTrend(source%3D%22yield%22%2C%20sigma_seasons%3Dsigma_seasons).fit(panel)%0A%20%20%20%20ahead%20%3D%20trend.predict(pd.DataFrame(%7BGEO_IDENTIFIER%3A%20%5B%22USA.17.001%22%5D%2C%20SEASON_YEAR%3A%20%5B2021%5D%7D))%0A%20%20%20%20print(trend.levels.tail(3).to_string(index%3DFalse))%0A%20%20%20%20print(f%22forecast%20for%202021%3A%20%7Bahead.iloc%5B0%2C%20-1%5D%3A.2f%7D%20%20%20true%20trend%20in%202021%3A%20%7B150%20%2B%201.8%20*%2021%3A.2f%7D%22)%0A%0A%20%20%20%20end_effect%20%3D%20(%0A%20%20%20%20%20%20%20%20true_trend%5B-1%5D%20-%20ndimage.gaussian_filter1d(true_trend%2C%20sigma_seasons%2C%20mode%3D%22nearest%22)%5B-1%5D%0A%20%20%20%20)%0A%20%20%20%20print(%0A%20%20%20%20%20%20%20%20f%22of%20the%20lag%3A%201.80%20from%20carrying%20the%20level%20flat%20one%20season%3B%20%7Bend_effect%3A.2f%7D%20from%20the%20smoother's%20end%20effect%20on%20the%20true%20trend%22%0A%20%20%20%20)%0A%20%20%20%20return%20seasons%2C%20true_trend%2C%20yields%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%20forecast%20lags%20the%20true%202021%20trend%20by%20about%207.7%20bu%2Fac%2C%20for%20two%20systematic%20reasons%3A%0A%0A%20%20%20%201.%20**The%20last%20smoothed%20level%20is%20carried%20flat.**%20That%20costs%20one%20season's%20slope%2C%201.8.%0A%20%20%20%202.%20**That%20last%20level%20is%20already%20low.**%20At%20the%20end%20of%20the%20series%20%60mode%3D%22nearest%22%60%20pads%20the%20future%20with%20copies%20of%20the%20last%20value%2C%20so%20the%20symmetric%20smoother%20becomes%20a%20one-sided%20average%20of%20the%20past%2C%20which%20trails%20a%20rising%20trend%20(2.9%20here%2C%20more%20with%20a%20larger%20%CF%83).%0A%0A%20%20%20%20The%202012%20drought%20and%20this%20seed's%20noise%20make%20up%20the%20rest.%20The%20series%20is%20synthetic%20and%20corn-like%20(bu%2Fac)%3B%20the%20production%20recipe%20smooths%20cotton%20state%20yields%20in%20lb%2Facre.%20Unit%202.2%20compares%20this%20with%20trends%20that%20have%20a%20slope.%0A%0A%20%20%20%20%23%23%202.3%20The%20weighting%20shape%20again%3A%20a%20crop's%20sensitivity%20window%20in%20%60phenoweight%60%0A%0A%20%20%20%20Here%20the%20bell%20curve%20isn't%20a%20probability%20at%20all.%20It's%20a%20*window%20over%20the%20year*%3A%20how%20much%20the%20weather%20on%20each%20day%20of%20the%20year%20matters%20to%20the%20crop.%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s2_3_weighting_shape_crops()%3A%0A%20%20%20%20from%20phenoweight.core%20import%20TYPES%0A%0A%20%20%20%20show(TYPES%5B%22gaussian%22%5D)%0A%20%20%20%20doys%20%3D%20np.arange(1%2C%20366%2C%20dtype%3Dfloat)%0A%20%20%20%20_window%20%3D%20TYPES%5B%22gaussian%22%5D(doys%2C%20mu%3D200%2C%20sigma%3D20%2C%20amplitude%3D1.0)%0A%20%20%20%20print(%0A%20%20%20%20%20%20%20%20f%22peak%20at%20day%20%7Bint(doys%5B_window.argmax()%5D)%7D%3B%20at%20mu%20%C2%B1%20sigma%20the%20window%20is%20%7B_window%5B219%5D%3A.4f%7D%20of%20the%20peak%22%0A%20%20%20%20%20%20%20%20f%22%20(exp(-1%2F2)%20%3D%20%7Bnp.exp(-0.5)%3A.4f%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%20%23%23%202.4%20The%20noise%20assumption%2C%20and%20the%20bands%20that%20avoid%20it%0A%0A%20%20%20%20A%20trend's%20standard%20error%20(unit%202.2)%20needs%20the%20model's%20errors%20to%20be%20uncorrelated%20with%20equal%20variance%3B%20Gaussian%20errors%20add%20only%20that%20the%20usual%20t%20interval%20is%20exact%20with%20few%20seasons.%20Our%20**prediction%20bands%20do%20not**.%20%60ConformalByLevel%60%20takes%20the%20empirical%20quantile%20of%20past%20seasons'%20absolute%20errors.%20That's%20the%20idea%20behind%20*split-conformal%20prediction*%2C%20and%20it%20needs%20no%20Gaussian%20assumption%3A%20the%20widths%20come%20from%20past%20errors.%20Standard%20split%20conformal%2C%20using%20a%20finite-sample-corrected%20rank%2C%20guarantees%20*marginal*%20coverage%20when%20past%20and%20future%20errors%20are%20exchangeable%20(their%20order%20carries%20no%20information).%20%60ConformalByLevel%60%20uses%20the%20plain%20nominal%20quantile%20instead%2C%20so%20its%20bands%20are%20empirically%20calibrated%20rather%20than%20guaranteed%20(section%204.2%20below%20measures%20the%20gap).%20And%20no%20quantile%20rule%20survives%20a%20shift%3A%20if%20future%20seasons'%20errors%20are%20larger%20than%20the%20calibration%20seasons'%2C%20the%20bands%20can%20miss%20far%20more%20often%20than%20nominal.%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s2_4_noise_assumption_bands()%3A%0A%20%20%20%20from%20model_foundry.common.quantity_modules.bands%20import%20ConformalByLevel%0A%0A%20%20%20%20show(ConformalByLevel.errors)%0A%20%20%20%20show(ConformalByLevel.fit)%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%20default%20coverages%20are%20%60%5B0.5%2C%200.68%2C%200.8%2C%200.9%2C%200.95%5D%60%20(%5B%60recipe.py%3A67-71%60%5D(https%3A%2F%2Fgithub.com%2Ftreefera%2Fmodel-foundry%2Fblob%2Ff69eef9df9e6a707c68f0292d940ccfc082cdaf5%2Fmodel_foundry%2Fcommon%2Fquantity_modules%2Frecipe.py%23L67-L71)).%200.68%20and%200.95%20echo%20the%20Gaussian's%20%C2%B11%CF%83%20and%20%C2%B11.96%CF%83%20coverages%2C%20but%20nothing%20in%20the%20code%20depends%20on%20that%3A%20the%20widths%20come%20from%20the%20data%2C%20not%20from%20%CF%83.%20Part%204%20shows%20why%20that's%20the%20right%20call%20for%20crops.%0A%0A%20%20%20%20%23%20Part%203%3A%20the%20science%0A%0A%20%20%20%20%23%23%203.1%20Why%20bell%20curves%20appear%3A%20the%20central%20limit%20theorem%0A%0A%20%20%20%20A%20season's%20yield%20deviation%20is%20roughly%20the%20sum%20of%20many%20small%2C%20independent%20nudges%3A%20a%20warm%20week%2C%20a%20dry%20spell%2C%20a%20late%20frost%2C%20a%20pest%20outbreak.%20Each%20nudge%20can%20have%20any%20shape.%20Here%20each%20is%20drawn%20from%20a%20lopsided%20exponential%20distribution%2C%20nothing%20like%20a%20bell%2C%20yet%20their%20centred%2C%20scaled%20sum%20gets%20closer%20to%20a%20Gaussian%20as%20the%20number%20of%20nudges%20grows%3A%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s3_1_bell_curves_appear()%3A%0A%20%20%20%20_rng%20%3D%20np.random.default_rng(2026)%20%20%23%20each%20experiment%20seeds%20its%20own%20generator%0A%20%20%20%20_z%20%3D%20np.linspace(-4%2C%204%2C%20401)%0A%20%20%20%20_fig%2C%20axes%20%3D%20plt.subplots(1%2C%203%2C%20figsize%3D(12%2C%203.2))%0A%20%20%20%20for%20_ax%2C%20n_effects%20in%20zip(axes%2C%20(1%2C%203%2C%2030)%2C%20strict%3DTrue)%3A%0A%20%20%20%20%20%20%20%20nudges%20%3D%20_rng.exponential(1.0%2C%20size%3D(50_000%2C%20n_effects))%20-%201.0%20%20%23%20each%20nudge%3A%20mean%200%2C%20skewed%0A%20%20%20%20%20%20%20%20total%20%3D%20nudges.sum(axis%3D1)%20%2F%20np.sqrt(n_effects)%0A%20%20%20%20%20%20%20%20_ax.hist(total%2C%20bins%3D120%2C%20density%3DTrue%2C%20color%3D%22C0%22%2C%20alpha%3D0.6)%0A%20%20%20%20%20%20%20%20_ax.plot(_z%2C%20stats.norm.pdf(_z)%2C%20color%3D%22k%22%2C%20label%3D%22standard%20normal%22)%0A%20%20%20%20%20%20%20%20_ax.set(%0A%20%20%20%20%20%20%20%20%20%20%20%20title%3Df%22%7Bn_effects%7D%20nudge(s)%3A%20skew%20%7Bstats.skew(total)%3A.2f%7D%2C%20excess%20kurtosis%20%7Bstats.kurtosis(total)%3A.2f%7D%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20xlabel%3D%22centred%2C%20scaled%20sum%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20ylabel%3D%22density%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20xlim%3D(-4%2C%204)%2C%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20_ax.legend(fontsize%3D8)%0A%20%20%20%20plt.tight_layout()%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%20With%20one%20nudge%20the%20histogram%20is%20a%20lopsided%20ramp.%20With%2030%20it's%20close%20to%20the%20black%20bell%20curve%20and%20the%20skew%20(0%20for%20a%20Gaussian)%20has%20collapsed%2C%20but%20it%20is%20still%20only%20approximately%20Gaussian%3A%20each%20nudge%20is%20at%20least%20%E2%88%921%2C%20so%20the%20scaled%20sum%20can%20never%20fall%20below%20%E2%88%92%E2%88%9A30%20%E2%89%88%20%E2%88%925.5%2C%20while%20a%20Gaussian%20has%20no%20lower%20bound.%20That's%20the%20%5Bcentral%20limit%20theorem%5D(https%3A%2F%2Fen.wikipedia.org%2Fwiki%2FCentral_limit_theorem)%3A%20the%20nudges%20are%20independent%2C%20identically%20distributed%20and%20have%20a%20**finite%20variance**%2C%20so%20their%20centred%2C%20scaled%20sum%20tends%20to%20a%20Gaussian.%20It's%20why%20%22Gaussian%20noise%22%20is%20the%20default%20model%20of%20weather%20noise%20and%20measurement%20error.%0A%0A%20%20%20%20%3E%20**Going%20deeper%3A%20when%20it%20fails.**%0A%20%20%20%20%3E%20-%20**No%20finite%20variance%3A**%20the%20average%20of%20independent%20Cauchy%20draws%20is%20still%20Cauchy%2C%20however%20many%20you%20add.%0A%20%20%20%20%3E%20-%20**One%20term%20dominates%3A**%20with%20X%E1%B5%A2%20%3D%202%E2%81%B1%C2%B7R%E1%B5%A2%20(R%E1%B5%A2%20%3D%20%C2%B11%2C%20equally%20likely)%2C%20the%20last%20term%20is%20as%20big%20as%20all%20the%20others%20together.%20The%20standardised%20sum%20tends%20to%20a%20*uniform*%20distribution%20on%20%5B%E2%88%92%E2%88%9A3%2C%20%E2%88%9A3%5D%20%E2%89%88%20%C2%B11.73%2C%20never%20a%20Gaussian.%0A%20%20%20%20%3E%20-%20**Not%20identically%20distributed%3A**%20Lindeberg's%20condition%20restores%20the%20theorem.%20Roughly%2C%20the%20large%20values%20of%20every%20term%20together%20must%20contribute%20a%20vanishing%20share%20of%20the%20total%20variance.%20Lindeberg's%20condition%20implies%20that%20no%20single%20term%20dominates%3B%20and%20when%20none%20does%2C%20Lindeberg's%20condition%20is%20exactly%20what%20a%20Gaussian%20limit%20needs%20(Feller's%20theorem).%20A%20dominant%20term%20doesn't%20always%20prevent%20a%20Gaussian%20limit%3A%20Gaussian%20terms%20give%20a%20Gaussian%20sum%20however%20lopsided%20their%20variances.%0A%0A%20%20%20%20%23%23%203.2%20The%2068%E2%80%9395%E2%80%9399.7%20rule%2C%20checked%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s3_2_68_95_99()%3A%0A%20%20%20%20_rng%20%3D%20np.random.default_rng(2026)%20%20%23%20each%20experiment%20seeds%20its%20own%20generator%0A%20%20%20%20x%20%3D%20_rng.normal(10%2C%202%2C%20200_000)%0A%20%20%20%20for%20_k%20in%20(1%2C%202%2C%203)%3A%0A%20%20%20%20%20%20%20%20observed%20%3D%20np.mean(np.abs(x%20-%2010)%20%3C%20_k%20*%202)%0A%20%20%20%20%20%20%20%20theory%20%3D%20stats.norm.cdf(_k)%20-%20stats.norm.cdf(-_k)%0A%20%20%20%20%20%20%20%20print(f%22within%20%7B_k%7D%CF%83%3A%20observed%20%7Bobserved%3A.4f%7D%20%20%20theory%20%7Btheory%3A.4f%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%203.3%20z-scores%20on%20a%20seasonal%20signal%2C%20and%20why%20the%20spread%20should%20be%20robust%0A%0A%20%20%20%20A%20z-score%20asks%20%22how%20many%20spreads%20from%20normal%20is%20today%3F%22.%20%22Normal%22%20depends%20on%20the%20day%20of%20the%20year%2C%20so%20a%20climatology%20keeps%20a%20mean%20and%20a%20spread%20*per%20day*.%20Below%2C%2020%20years%20of%20an%20NDVI-like%20seasonal%20curve%3B%20one%20year%20(2012)%20is%20a%20drought.%20We%20score%20a%20new%20year%20against%20the%20other%20years%2C%20with%20the%20classic%20standard%20deviation%20and%20with%20our%20robust%20%60(q84%20%E2%88%92%20q16)%20%2F%202%60%3A%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s3_3_z_scores_seasonal()%3A%0A%20%20%20%20_rng%20%3D%20np.random.default_rng(2026)%20%20%23%20each%20experiment%20seeds%20its%20own%20generator%0A%20%20%20%20days%20%3D%20np.arange(1%2C%20366)%0A%20%20%20%20green%20%3D%200.2%20%2B%200.6%20*%20np.exp(-0.5%20*%20((days%20-%20200)%20%2F%2040)%20**%202)%0A%20%20%20%20years%20%3D%20np.arange(2003%2C%202023)%0A%20%20%20%20history%20%3D%20green%20%2B%20_rng.normal(0%2C%200.03%2C%20(years.size%2C%20days.size))%0A%20%20%20%20history%5Byears%20%3D%3D%202012%5D%20-%3D%200.25%20*%20np.exp(-0.5%20*%20((days%20-%20210)%20%2F%2025)%20**%202)%20%20%23%20the%20drought%0A%0A%20%20%20%20mean%20%3D%20history.mean(axis%3D0)%0A%20%20%20%20std%20%3D%20history.std(axis%3D0%2C%20ddof%3D1)%0A%20%20%20%20_q16%2C%20_q84%20%3D%20np.quantile(history%2C%20%5B0.16%2C%200.84%5D%2C%20axis%3D0)%0A%20%20%20%20robust%20%3D%20(_q84%20-%20_q16)%20%2F%202%0A%0A%20%20%20%20new_year%20%3D%20(%0A%20%20%20%20%20%20%20%20green%20-%200.06%20*%20np.exp(-0.5%20*%20((days%20-%20210)%20%2F%2025)%20**%202)%20%2B%20_rng.normal(0%2C%200.03%2C%20days.size)%0A%20%20%20%20)%0A%20%20%20%20z_classic%20%3D%20(new_year%20-%20mean)%20%2F%20std%0A%20%20%20%20z_robust%20%3D%20(new_year%20-%20mean)%20%2F%20robust%0A%20%20%20%20peak%20%3D%20(days%20%3E%3D%20195)%20%26%20(days%20%3C%3D%20225)%0A%20%20%20%20print(%0A%20%20%20%20%20%20%20%20f%22days%20195%E2%80%93225%3A%20spread%20by%20std%20%7Bstd%5Bpeak%5D.mean()%3A.4f%7D%20%20%20by%20(q84%20-%20q16)%2F2%20%7Brobust%5Bpeak%5D.mean()%3A.4f%7D%22%0A%20%20%20%20)%0A%20%20%20%20true_z%20%3D%20(-0.06%20*%20np.exp(-0.5%20*%20((days%20-%20210)%20%2F%2025)%20**%202))%5B%0A%20%20%20%20%20%20%20%20peak%0A%20%20%20%20%5D.mean()%20%2F%200.03%20%20%23%20how%20the%20new%20year%20was%20generated%0A%20%20%20%20print(%0A%20%20%20%20%20%20%20%20f%22days%20195%E2%80%93225%3A%20mean%20z%20classic%20%7Bz_classic%5Bpeak%5D.mean()%3A.2f%7D%20%20%20robust%20%7Bz_robust%5Bpeak%5D.mean()%3A.2f%7D%20%20%20true%20%7Btrue_z%3A.2f%7D%22%0A%20%20%20%20)%0A%0A%20%20%20%20_calm%20%3D%20_rng.normal(0%2C%201%2C%2019)%0A%20%20%20%20for%20label%2C%20h%20in%20(%0A%20%20%20%20%20%20%20%20(%2219%20calm%20years%20%20%20%20%20%20%22%2C%20_calm)%2C%0A%20%20%20%20%20%20%20%20(%22plus%20one%208%CF%83%20year%20%20%20%22%2C%20np.append(_calm%2C%208.0))%2C%0A%20%20%20%20)%3A%0A%20%20%20%20%20%20%20%20print(%0A%20%20%20%20%20%20%20%20%20%20%20%20f%22%7Blabel%7D%3A%20std%20%7Bh.std(ddof%3D1)%3A.2f%7D%20%20%20(q84%20-%20q16)%2F2%20%7Bnp.diff(np.quantile(h%2C%20%5B0.16%2C%200.84%5D))%5B0%5D%20%2F%202%3A.2f%7D%22%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20return%20days%2C%20green%2C%20history%2C%20new_year%2C%20peak%2C%20true_z%2C%20years%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%20drought%20year%20inflates%20the%20standard%20deviation%20around%20its%20peak%2C%20so%20the%20classic%20z-score%20hides%20most%20of%20the%20new%20year's%20dip%20(%E2%88%920.93%20against%20a%20true%20%E2%88%921.88).%20The%20percentile%20spread%20barely%20notices%20one%20bad%20year%2C%20so%20the%20robust%20z%20gets%20much%20closer.%20It%20slightly%20overshoots%20(%E2%88%922.10)%3A%20from%20only%2020%20values%20the%20percentile%20spread%20is%20biased%20low%2C%20averaging%20about%200.91%CF%83.%20The%208%CF%83%20example%20shows%20what%20the%20robust%20spread%20buys%3A%20one%20freak%20year%20almost%20doubles%20the%20standard%20deviation%20and%20barely%20moves%20the%20percentiles.%0A%0A%20%20%20%20The%20robust%20spread%20has%20limits%20too.%20It%20is%20noisier%20than%20the%20standard%20deviation%20when%20the%20data%20*are*%20Gaussian%2C%20and%20it%20resists%20bad%20years%20only%20up%20to%2016%20%25%20of%20the%20history%2C%20because%20q16%20is%20then%20itself%20a%20bad%20year%20(exercise%202).%20Only%20the%20spread%20is%20robust%3A%20the%20centre%20is%20still%20a%20mean%2C%20which%20the%20drought%20pulls%20down.%0A%0A%20%20%20%20%23%23%23%203.3b%20The%20same%20history%20through%20the%20production%20code%0A%0A%20%20%20%20Production%20doesn't%20compute%20one%20climatology%20per%20day.%20%60compute_windowed_climatology%60%20pools%20a%20%C2%B115-day%20window%20(%60window_size%3D31%60)%20across%20every%20year%2C%20and%20%60compute_zscore%60%20then%20scores%20against%20it.%20Run%20it%20on%20the%20same%20history%3A%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s3_3b_same_history_through(%0A%20%20%20%20compute_zscore%2C%0A%20%20%20%20days%2C%0A%20%20%20%20green%2C%0A%20%20%20%20history%2C%0A%20%20%20%20new_year%2C%0A%20%20%20%20peak%2C%0A%20%20%20%20true_z%2C%0A%20%20%20%20years%2C%0A)%3A%0A%20%20%20%20import%20warnings%0A%0A%20%20%20%20from%20tf_data_ml_utils.features.core.climo_helpers%20import%20compute_windowed_climatology%0A%0A%20%20%20%20%23%20One%20time%20coordinate%20for%20the%2020%20history%20years.%20The%20synthetic%20curve%20is%20indexed%20by%20day%20of%20year%201%E2%80%93365%2C%20and%20production%20buckets%20by%0A%20%20%20%20%23%20%60time.dt.dayofyear%60%2C%20so%20each%20year's%20dates%20are%201%20January%20plus%200%E2%80%93364%20days.%20In%20leap%20years%20that%20includes%2029%20February%20and%20stops%20at%0A%20%20%20%20%23%2030%20December%2C%20which%20keeps%20every%20row%20on%20the%20day%20of%20year%20its%20curve%20value%20was%20generated%20for.%0A%20%20%20%20time%20%3D%20np.concatenate(%0A%20%20%20%20%20%20%20%20%5Bpd.Timestamp(f%22%7Byear%7D-01-01%22)%20%2B%20pd.to_timedelta(days%20-%201%2C%20%22D%22)%20for%20year%20in%20years%5D%0A%20%20%20%20)%0A%20%20%20%20history_da%20%3D%20xr.DataArray(history.reshape(-1%2C%201)%2C%20dims%3D(%22time%22%2C%20%22cell%22)%2C%20coords%3D%7B%22time%22%3A%20time%7D)%0A%20%20%20%20with%20warnings.catch_warnings()%3A%0A%20%20%20%20%20%20%20%20warnings.simplefilter(%0A%20%20%20%20%20%20%20%20%20%20%20%20%22ignore%22%2C%20RuntimeWarning%0A%20%20%20%20%20%20%20%20)%20%20%23%20day%20366%20has%20no%20data%20in%20these%20365-day%20years%0A%20%20%20%20%20%20%20%20_clim%20%3D%20compute_windowed_climatology(history_da).compute()%0A%0A%20%20%20%20new_da%20%3D%20xr.DataArray(%0A%20%20%20%20%20%20%20%20new_year.reshape(-1%2C%201)%2C%0A%20%20%20%20%20%20%20%20dims%3D(%22time%22%2C%20%22cell%22)%2C%0A%20%20%20%20%20%20%20%20coords%3D%7B%22time%22%3A%20pd.Timestamp(%222023-01-01%22)%20%2B%20pd.to_timedelta(days%20-%201%2C%20%22D%22)%7D%2C%0A%20%20%20%20)%0A%20%20%20%20z_production%20%3D%20compute_zscore(new_da%2C%20_clim).isel(cell%3D0).values%0A%20%20%20%20_window%20%3D%20_clim.isel(cell%3D0).sel(dayofyear%3Dslice(1%2C%20365))%0A%20%20%20%20spread_production%20%3D%20((_window%5B%22q84%22%5D%20-%20_window%5B%22q16%22%5D)%20%2F%202).values%0A%20%20%20%20print(f%22days%20195%E2%80%93225%3A%20production%20spread%20%7Bspread_production%5Bpeak%5D.mean()%3A.4f%7D%20%20%20(true%20%CF%83%200.0300)%22)%0A%20%20%20%20print(%0A%20%20%20%20%20%20%20%20f%22days%20195%E2%80%93225%3A%20production%20centre%20minus%20the%20true%20curve%20%7B(_window%5B'mean'%5D.values%20-%20green)%5Bpeak%5D.mean()%3A%2B.4f%7D%22%0A%20%20%20%20)%0A%20%20%20%20print(f%22days%20195%E2%80%93225%3A%20production%20z%20%7Bz_production%5Bpeak%5D.mean()%3A.2f%7D%20%20%20true%20%7Btrue_z%3A.2f%7D%22)%0A%0A%20%20%20%20%23%20Control%3A%20the%20same%20production%20climatology%20on%20the%20history%20*without*%20the%20drought%20year.%0A%20%20%20%20calm_history%20%3D%20history.copy()%0A%20%20%20%20calm_history%5Byears%20%3D%3D%202012%5D%20%2B%3D%200.25%20*%20np.exp(-0.5%20*%20((days%20-%20210)%20%2F%2025)%20**%202)%0A%20%20%20%20calm_da%20%3D%20xr.DataArray(%0A%20%20%20%20%20%20%20%20calm_history.reshape(-1%2C%201)%2C%20dims%3D(%22time%22%2C%20%22cell%22)%2C%20coords%3D%7B%22time%22%3A%20time%7D%0A%20%20%20%20)%0A%20%20%20%20with%20warnings.catch_warnings()%3A%0A%20%20%20%20%20%20%20%20warnings.simplefilter(%22ignore%22%2C%20RuntimeWarning)%0A%20%20%20%20%20%20%20%20_calm%20%3D%20(%0A%20%20%20%20%20%20%20%20%20%20%20%20compute_windowed_climatology(calm_da)%0A%20%20%20%20%20%20%20%20%20%20%20%20.compute()%0A%20%20%20%20%20%20%20%20%20%20%20%20.isel(cell%3D0)%0A%20%20%20%20%20%20%20%20%20%20%20%20.sel(dayofyear%3Dslice(1%2C%20365))%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20print(%0A%20%20%20%20%20%20%20%20f%22days%20195%E2%80%93225%2C%20no%20drought%20year%3A%20production%20spread%20%7B((_calm%5B'q84'%5D%20-%20_calm%5B'q16'%5D)%20%2F%202).values%5Bpeak%5D.mean()%3A.4f%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%20Pooling%2031%20days%20%C3%97%2020%20years%20gives%20about%20620%20values%20per%20day%2C%20and%20in%20this%20run%20the%20spread%20comes%20out%20at%20the%20true%20%CF%83.%20Read%20that%20with%20the%20control%3A%20without%20the%20drought%20year%20it%20is%20about%200.028%2C%20a%20little%20*below*%20%CF%83%2C%20because%20each%20anomaly%20is%20measured%20from%20its%20own%20day's%2020-year%20mean%2C%20which%20absorbs%20a%20share%20of%20the%20noise%20(and%20pooling%20neighbouring%20days%20removes%20the%20seasonal%20curve%20first%2C%20so%20the%20curve%20adds%20nothing).%20The%20drought%20year%2C%205%20%25%20of%20the%20pool%2C%20pushes%20it%20back%20up.%20So%20pooling%20steadies%20the%20spread%2C%20but%20it%20doesn't%20make%20it%20exact%2C%20and%20in%20real%20data%20neighbouring%20days%20are%20correlated%2C%20so%20620%20values%20are%20worth%20fewer%20independent%20ones.%20But%20the%20z-score%20now%20*under*states%20the%20dip%20(%E2%88%921.44%20against%20%E2%88%921.88)%2C%20because%20the%20**centre**%20is%20off%20by%20about%20%E2%88%920.023%3A%0A%0A%20%20%20%20-%20**The%20drought%20pulls%20the%20mean%20down**%20by%20about%200.012%20(one%20year%20in%20twenty%2C%20at%20%E2%88%920.25).%0A%20%20%20%20-%20**The%20window%20flattens%20the%20seasonal%20peak**%20by%20about%20as%20much%20again.%20The%20centre%20is%20the%20*windowed*%20mean%2C%20and%20averaging%20%C2%B115%20days%20across%20a%20curved%20peak%20lands%20below%20its%20top%3A%20for%20a%20curve%20with%20second%20derivative%20f%E2%80%B3%2C%20by%20about%20%C2%BD%20%C2%B7%20f%E2%80%B3%20%C2%B7%20(31%C2%B2%20%E2%88%92%201)%2F12.%20A%20normal%20peak%20day%20therefore%20scores%20slightly%20*positive*.%0A%0A%20%20%20%20How%20much%20the%20window%20matters%20depends%20on%20the%20curve%20and%20the%20window.%20ERA5%20and%20CHIRPS%20configs%20use%20%60window_size%3A%2031%60%2C%20but%20daily%20temperature%20curves%20gently%3A%20about%200.15%20%C2%B0C%20at%20its%20seasonal%20peak%2C%20against%20a%20daily%20spread%20of%20about%203%20%C2%B0C%2C%20or%200.05%20in%20z.%20MODIS%20uses%20%60window_size%3A%201%60%20(data-pipelines%20%60modis%2Fregion.py%60)%2C%20so%20%60z_ndvi%60%20has%20no%20window%20bias%20but%20takes%20its%20percentiles%20from%20the%20years%20alone%3A%20the%20small-sample%20case%20of%20the%20hand-rolled%20version%20above.%0A%0A%20%20%20%20%23%23%203.4%20The%20Gaussian%20as%20a%20smoother%3A%20what%20%CF%83%20does%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s3_4_gaussian_smoother(seasons%2C%20true_trend%2C%20yields)%3A%0A%20%20%20%20_fig%2C%20_ax%20%3D%20plt.subplots()%0A%20%20%20%20_ax.plot(seasons%2C%20yields%2C%20%22o%22%2C%20color%3D%220.4%22%2C%20label%3D%22yield%22)%0A%20%20%20%20_ax.plot(seasons%2C%20true_trend%2C%20%22k--%22%2C%20label%3D%22true%20trend%22)%0A%20%20%20%20for%20_sigma%20in%20(1%2C%202%2C%204)%3A%0A%20%20%20%20%20%20%20%20_ax.plot(%0A%20%20%20%20%20%20%20%20%20%20%20%20seasons%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20ndimage.gaussian_filter1d(yields%2C%20_sigma%2C%20mode%3D%22nearest%22)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20label%3Df%22%CF%83%20%3D%20%7B_sigma%7D%20seasons%22%2C%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20_ax.set(xlabel%3D%22season%22%2C%20ylabel%3D%22yield%22%2C%20title%3D%22Gaussian%20smoothing%3A%20bigger%20%CF%83%2C%20smoother%20line%22)%0A%20%20%20%20_ax.xaxis.set_major_locator(MaxNLocator(integer%3DTrue))%0A%20%20%20%20_ax.legend(fontsize%3D8)%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%20Small%20%CF%83%20follows%20each%20season%2C%20so%20the%20drought%20leaves%20a%20dent.%20Large%20%CF%83%20averages%20over%20many%20seasons%2C%20so%20the%20dent%20spreads%20thin%2C%20but%20the%20line%20flattens%20at%20the%20ends%20because%20%60mode%3D%22nearest%22%60%20pads%20with%20copies%20of%20the%20end%20values.%20A%20kernel's%20full%20width%20at%20half%20maximum%20is%202.355%CF%83%2C%20so%20%CF%83%20%3D%204%20seasons%20means%20about%20a%20decade%20at%20more%20than%20half%20weight.%0A%0A%20%20%20%20%23%23%203.5%20Least%20squares%20*is*%20the%20Gaussian's%20maximum%20likelihood%0A%0A%20%20%20%20If%20residuals%20are%20independent%20and%20Gaussian%20with%20a%20common%20variance%20%CF%83%C2%B2%2C%20the%20most%20likely%20line%20maximises%20%CE%A3%20log%20pdf(residual).%20The%20log%20of%20the%20Gaussian%20pdf%20is%20a%20constant%20minus%20residual%C2%B2%2F(2%CF%83%C2%B2)%2C%20so%20maximising%20it%20means%20*minimising%20the%20sum%20of%20squared%20residuals*%3A%20ordinary%20least%20squares.%20Numerically%3A%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s3_5_least_squares_gaussians(seasons%2C%20yields)%3A%0A%20%20%20%20design%20%3D%20np.column_stack(%5Bnp.ones(seasons.size)%2C%20seasons%20-%202010.0%5D)%0A%20%20%20%20ols%20%3D%20np.linalg.lstsq(design%2C%20yields%2C%20rcond%3DNone)%5B0%5D%0A%0A%20%20%20%20def%20negative_log_likelihood(params)%3A%0A%20%20%20%20%20%20%20%20%22%22%22Return%20minus%20the%20Gaussian%20log-likelihood%20of%20the%20yields%20for%20intercept%2C%20slope%20and%20log%20%CF%83.%22%22%22%0A%20%20%20%20%20%20%20%20return%20-stats.norm.logpdf(yields%2C%20design%20%40%20params%5B%3A2%5D%2C%20np.exp(params%5B2%5D)).sum()%0A%0A%20%20%20%20fitted%20%3D%20optimize.minimize(%0A%20%20%20%20%20%20%20%20negative_log_likelihood%2C%0A%20%20%20%20%20%20%20%20%5B0%2C%200%2C%201%5D%2C%0A%20%20%20%20%20%20%20%20method%3D%22Nelder-Mead%22%2C%0A%20%20%20%20%20%20%20%20options%3D%7B%22xatol%22%3A%201e-9%2C%20%22fatol%22%3A%201e-9%2C%20%22maxiter%22%3A%2020_000%7D%2C%0A%20%20%20%20)%0A%20%20%20%20print(f%22optimiser%3A%20success%3D%7Bfitted.success%7D%2C%20%7Bfitted.message%7D%22)%0A%20%20%20%20print(%22least%20squares%20%20%20%20intercept%2C%20slope%3A%22%2C%20ols.round(4))%0A%20%20%20%20print(%22Gaussian%20maximum%20likelihood%20%20%20%20%20%20%20%3A%22%2C%20fitted.x%5B%3A2%5D.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%20Identical%20to%20four%20decimals.%20Least%20squares%20itself%20assumes%20no%20distribution%3A%20it%20minimises%20squared%20residuals%20whatever%20the%20errors%20look%20like.%20The%20two%20fits%20coincide%20because%2C%20with%20independent%20Gaussian%20errors%20of%20a%20common%20variance%2C%20maximising%20the%20likelihood%20over%20the%20coefficients%20is%20the%20same%20as%20minimising%20the%20residual%20sum%20of%20squares.%20(With%20unequal%20but%20*known*%20variances%2C%20maximum%20likelihood%20minimises%20a%20*weighted*%20sum%20of%20squares%20instead.)%20The%20standard-error%20formula%20needs%20only%20uncorrelated%20errors%20of%20equal%20variance%3B%20what%20Gaussian%20errors%20add%20is%20that%20%22%C2%B1t%20%C3%97%20SE%22%20is%20an%20*exact*%20interval%20with%20few%20seasons.%20Production%20uses%20the%20SE%20differently%20again%3A%20it%20sets%20how%20hard%20each%20county's%20slope%20is%20shrunk%20towards%20the%20national%20one%20(%60shrink_towards%60%2C%20unit%202.2).%20Unit%200.2%20(planned)%20will%20build%20on%20this.%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%20When%20the%20noise%20isn't%20Gaussian%3A%20drought-skewed%20errors%0A%0A%20%20%20%20Crop%20errors%20are%20mostly%20small%2C%20but%20now%20and%20then%20a%20drought%20knocks%20a%20season%20far%20down.%20That's%20a%20**skewed%2C%20heavy-tailed**%20distribution.%20Here's%20one%2C%20compared%20with%20a%20Gaussian%20by%20a%20QQ%20plot%20(%5B%60scipy.stats.probplot%60%5D(https%3A%2F%2Fdocs.scipy.org%2Fdoc%2Fscipy%2Freference%2Fgenerated%2Fscipy.stats.probplot.html))%3A%20if%20the%20errors%20were%20Gaussian%2C%20the%20dots%20would%20sit%20on%20the%20line.%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s4_1_noise_gaussian_drought()%3A%0A%20%20%20%20_rng%20%3D%20np.random.default_rng(2026)%20%20%23%20each%20experiment%20seeds%20its%20own%20generator%0A%20%20%20%20errors%20%3D%20_rng.normal(0%2C%205%2C%204000)%0A%20%20%20%20drought%20%3D%20_rng.random(4000)%20%3C%200.08%0A%20%20%20%20errors%5Bdrought%5D%20-%3D%20_rng.gamma(2%2C%2012%2C%20drought.sum())%0A%0A%20%20%20%20_fig%2C%20_ax%20%3D%20plt.subplots(figsize%3D(5%2C%204))%0A%20%20%20%20stats.probplot(errors%2C%20dist%3D%22norm%22%2C%20plot%3D_ax)%0A%20%20%20%20_ax.set_title(%22QQ%20plot%20of%20drought-skewed%20errors%20against%20a%20Gaussian%22)%0A%20%20%20%20plt.show()%0A%20%20%20%20print(%0A%20%20%20%20%20%20%20%20f%22skew%20%7Bstats.skew(errors)%3A.2f%7D%20%20%20excess%20kurtosis%20%7Bstats.kurtosis(errors)%3A.2f%7D%20%20%20(both%200%20for%20a%20Gaussian)%22%0A%20%20%20%20)%0A%20%20%20%20df%2C%20loc%2C%20scale%20%3D%20stats.t.fit(errors)%0A%20%20%20%20print(%0A%20%20%20%20%20%20%20%20f%22Student-t%20fit%3A%20%7Bdf%3A.2f%7D%20degrees%20of%20freedom%20(a%20Gaussian%20is%20t%20with%20infinite%20degrees%20of%20freedom)%22%0A%20%20%20%20)%0A%20%20%20%20return%20(errors%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%20The%20bottom-left%20dots%20bend%20far%20below%20the%20line%3A%20the%20drought%20tail%20is%20much%20longer%20than%20a%20Gaussian%20allows.%0A%0A%20%20%20%20%23%23%204.2%20Gaussian%20bands%20against%20empirical-quantile%20bands%0A%0A%20%20%20%20Fit%20bands%20on%20the%20first%20half%20of%20the%20errors%20and%20check%20how%20often%20the%20second%20half%20falls%20inside%3A%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s4_2_gaussian_bands_empirical(errors)%3A%0A%20%20%20%20train%2C%20test%20%3D%20errors%5B%3A2000%5D%2C%20errors%5B2000%3A%5D%0A%20%20%20%20rows%20%3D%20%5B%5D%0A%20%20%20%20for%20coverage%20in%20(0.68%2C%200.8%2C%200.9%2C%200.95%2C%200.99)%3A%0A%20%20%20%20%20%20%20%20gaussian_half_width%20%3D%20stats.norm.ppf(0.5%20%2B%20coverage%20%2F%202)%20*%20train.std(ddof%3D1)%0A%20%20%20%20%20%20%20%20empirical_half_width%20%3D%20np.quantile(np.abs(train)%2C%20coverage)%0A%20%20%20%20%20%20%20%20rows.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%22nominal%22%3A%20coverage%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22Gaussian%20covers%22%3A%20round(np.mean(np.abs(test)%20%3C%3D%20gaussian_half_width)%2C%203)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22Gaussian%20misses%20below%22%3A%20round(np.mean(test%20%3C%20-gaussian_half_width)%2C%203)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22Gaussian%20misses%20above%22%3A%20round(np.mean(test%20%3E%20gaussian_half_width)%2C%203)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22empirical%20covers%22%3A%20round(np.mean(np.abs(test)%20%3C%3D%20empirical_half_width)%2C%203)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22empirical%20misses%20below%22%3A%20round(np.mean(test%20%3C%20-empirical_half_width)%2C%203)%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%20pd.DataFrame(rows)%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%20Gaussian%20bands%20are%20**too%20wide%20in%20the%20middle%20and%20too%20narrow%20in%20the%20far%20tail**%3A%20the%20drought%20tail%20inflates%20%CF%83%2C%20so%20a%20nominal%2080%20%25%20band%20covers%20about%2093%20%25%2C%20yet%20at%2099%20%25%20it%20covers%20only%20about%2097%20%25.%20At%2095%20%25%20and%2099%20%25%20**every%20miss%20is%20on%20the%20drought%20side**%3B%20at%2068%20%25%20most%20are%20(9%20%25%20below%2C%202.6%20%25%20above).%20The%20empirical-quantile%20bands%20land%20close%20to%20nominal%20at%20every%20level.%20That's%20the%20case%20for%20%60ConformalByLevel%60's%20design.%20Note%20that%20**both**%20bands%20here%20are%20symmetric%20(one%20half-width%20applied%20to%20%7Cerror%7C)%2C%20as%20%60ConformalByLevel%60's%20are%3A%20the%20empirical%20method%20changes%20how%20the%20width%20is%20estimated%2C%20not%20the%20band's%20shape%2C%20so%20neither%20gives%20the%20long%20drought%20tail%20more%20room%20than%20the%20short%20one.%0A%0A%20%20%20%20**How%20much%20does%20a%20short%20history%20matter%3F**%20A%20group's%20band%20is%20used%20once%20it%20has%20%60warm_up_seasons%60%20seasons%2C%20which%20recipes%20set%20between%201%20and%2010.%20Take%20NumPy's%20default%20quantile%20(linear%20interpolation)%20at%2095%20%25.%20If%20past%20and%20new%20errors%20are%20exchangeable%20and%20untied%2C%20the%20average%20coverage%20over%20calibration%20sets%20falls%20as%20the%20history%20shortens.%20Strict%20split%20conformal%20uses%20rank%20%E2%8C%88(n%20%2B%201)%20%C3%97%200.95%E2%8C%89%2C%20which%20exceeds%20n%20below%2019%20seasons%3A%20the%20honest%2095%20%25%20band%20is%20then%20infinite.%20The%20strict%20rank's%20coverage%20doesn't%20depend%20on%20the%20error%20distribution.%20The%20plain%20quantile's%20depends%20on%20it%20slightly%2C%20because%20it%20interpolates%20between%20two%20past%20errors%2C%20but%20it%20always%20lies%20between%20the%20two%20neighbouring%20ranks.%20The%20next%20cell%20computes%20both%2C%20exactly%20(for%20uniform%20scores)%20and%20by%20simulation.%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s4_2_gaussian_bands_empirical_2()%3A%0A%20%20%20%20level%20%3D%200.95%0A%20%20%20%20_rng%20%3D%20np.random.default_rng(95)%20%20%23%20each%20experiment%20seeds%20its%20own%20generator%0A%20%20%20%20for%20n_seasons%20in%20(5%2C%2010%2C%2020)%3A%0A%20%20%20%20%20%20%20%20position%20%3D%20(n_seasons%20-%201)%20*%20level%20%20%23%20numpy's%20default%20linear%20quantile%2C%20as%20a%200-based%20position%0A%20%20%20%20%20%20%20%20below%20%3D%20int(np.floor(position))%0A%20%20%20%20%20%20%20%20fraction%20%3D%20position%20-%20below%0A%20%20%20%20%20%20%20%20plain%20%3D%20(below%20%2B%201%20%2B%20fraction)%20%2F%20(%0A%20%20%20%20%20%20%20%20%20%20%20%20n_seasons%20%2B%201%0A%20%20%20%20%20%20%20%20)%20%20%23%20E%5BF(k-th%20order%20statistic)%5D%20%3D%20k%20%2F%20(n%20%2B%201)%2C%20exact%20for%20uniform%20scores%0A%20%20%20%20%20%20%20%20strict_rank%20%3D%20int(np.ceil((n_seasons%20%2B%201)%20*%20level))%0A%20%20%20%20%20%20%20%20scores%20%3D%20_rng.random(%0A%20%20%20%20%20%20%20%20%20%20%20%20(200_000%2C%20n_seasons%20%2B%201)%0A%20%20%20%20%20%20%20%20)%20%20%23%20exchangeable%2C%20continuous%3A%20past%20seasons%20plus%20a%20new%20one%0A%20%20%20%20%20%20%20%20past%2C%20new%20%3D%20scores%5B%3A%2C%20%3An_seasons%5D%2C%20scores%5B%3A%2C%20n_seasons%5D%0A%20%20%20%20%20%20%20%20simulated_plain%20%3D%20np.mean(new%20%3C%3D%20np.quantile(past%2C%20level%2C%20axis%3D1))%0A%20%20%20%20%20%20%20%20if%20strict_rank%20%3C%3D%20n_seasons%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20strict%20%3D%20f%22exact%20%7Bstrict_rank%20%2F%20(n_seasons%20%2B%201)%3A.4f%7D%2C%20simulated%20%7Bnp.mean(new%20%3C%3D%20np.sort(past%2C%20axis%3D1)%5B%3A%2C%20strict_rank%20-%201%5D)%3A.4f%7D%22%0A%20%20%20%20%20%20%20%20else%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20strict%20%3D%20f%22rank%20%7Bstrict_rank%7D%20%3E%20%7Bn_seasons%7D%3A%20no%20finite%20band%22%0A%20%20%20%20%20%20%20%20print(%0A%20%20%20%20%20%20%20%20%20%20%20%20f%22%7Bn_seasons%3A%3E2%7D%20seasons%20%20%20plain%2095%20%25%20quantile%3A%20exact%20%7Bplain%3A.4f%7D%2C%20simulated%20%7Bsimulated_plain%3A.4f%7D%20%20%20strict%3A%20%7Bstrict%7D%22%0A%20%20%20%20%20%20%20%20)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%204.3%20Cocoa's%20hand-written%20filter%20against%20scipy's%0A%0A%20%20%20%20model-foundry's%20cocoa%20model%20writes%20its%20own%20Gaussian%20smoother.%20Below%20is%20a%20**verbatim%20copy**%20of%20%5B%60production_cocoa_ghana_civ%2Fmodel.py%3A163-167%60%5D(https%3A%2F%2Fgithub.com%2Ftreefera%2Fmodel-foundry%2Fblob%2Ff69eef9df9e6a707c68f0292d940ccfc082cdaf5%2Fmodel_foundry%2Fmodels%2Fproduction_cocoa_ghana_civ%2Fsrc%2Fproduction_cocoa_ghana_civ%2Fmodel.py%23L163-L167)%2C%20because%20the%20cocoa%20package%20isn't%20installed%20here.%20It's%20compared%20with%20%60scipy.ndimage.gaussian_filter1d%60%3A%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s4_3_cocoas_hand_written()%3A%0A%20%20%20%20_rng%20%3D%20np.random.default_rng(2026)%20%20%23%20each%20experiment%20seeds%20its%20own%20generator%0A%0A%20%20%20%20def%20gaussian_smooth(values%3A%20np.ndarray%2C%20sigma%3A%20float)%20-%3E%20np.ndarray%3A%0A%20%20%20%20%20%20%20%20%22%22%22Return%20%60%60values%60%60%20Gaussian-smoothed%20over%20%60%604%20sigma%60%60%20either%20side%2C%20edges%20held.%22%22%22%0A%20%20%20%20%20%20%20%20radius%20%3D%20int(4%20*%20sigma%20%2B%200.5)%0A%20%20%20%20%20%20%20%20kernel%20%3D%20np.exp(-0.5%20*%20(np.arange(-radius%2C%20radius%20%2B%201)%20%2F%20sigma)%20**%202)%0A%20%20%20%20%20%20%20%20return%20np.convolve(np.pad(values%2C%20radius%2C%20%22edge%22)%2C%20kernel%20%2F%20kernel.sum()%2C%20%22valid%22)%0A%0A%20%20%20%20series%20%3D%20_rng.normal(100%2C%2010%2C%2030)%0A%20%20%20%20for%20_sigma%20in%20(0.7%2C%201.5%2C%203.0)%3A%0A%20%20%20%20%20%20%20%20gap%20%3D%20np.abs(%0A%20%20%20%20%20%20%20%20%20%20%20%20gaussian_smooth(series%2C%20_sigma)%0A%20%20%20%20%20%20%20%20%20%20%20%20-%20ndimage.gaussian_filter1d(series%2C%20_sigma%2C%20mode%3D%22nearest%22)%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20print(f%22%CF%83%20%3D%20%7B_sigma%7D%3A%20largest%20difference%20%7Bgap.max()%3A.1e%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%20On%20floating-point%20input%20they%20agree%20to%20rounding%2C%20about%2010%E2%81%BB%C2%B9%E2%81%B4%2C%20but%20not%20bit%20for%20bit.%20On%20integer%20input%20they%20don't%3A%20scipy%20returns%20integers%20by%20default%20(%60%5B0%2C%201%2C%200%5D%60%20smooths%20to%20%60%5B0%2C%200%2C%200%5D%60)%2C%20while%20the%20hand-written%20version%20returns%20fractions.%20That's%20the%20refactor%20to%20try%20in%20%5Bcode.md%5D(https%3A%2F%2Fgithub.com%2Ftreefera%2Ftf-model-lab%2Fblob%2Fmain%2Facademy%2F0-foundations%2F0.1-the-gaussian%2Fcode.md).%0A%0A%20%20%20%20%23%23%204.4%20Symmetric%20against%20causal%3A%20a%20Gaussian%20that%20can't%20see%20the%20future%0A%0A%20%20%20%20A%20symmetric%20kernel%20at%20season%202012%20also%20weighs%202013%E2%80%932016.%20No%20production%20smoother%20looks%20only%20backwards%2C%20so%20here%20is%20one%2C%20written%20for%20this%20notebook%3A%20a%20**backward-only%20half-Gaussian**%20that%20averages%20each%20season%20with%20earlier%20seasons%20only%20(the%20formula%20is%20in%20%5Bcode.md%5D(https%3A%2F%2Fgithub.com%2Ftreefera%2Ftf-model-lab%2Fblob%2Fmain%2Facademy%2F0-foundations%2F0.1-the-gaussian%2Fcode.md)%2C%20section%205)%3A%0A%20%20%20%20%22%22%22)%0A%0A%0A%40app.cell%0Adef%20s4_4_symmetric_causal_gaussian(seasons%2C%20yields)%3A%0A%20%20%20%20def%20causal_gaussian(values%3A%20np.ndarray%2C%20sigma%3A%20float%2C%20truncate%3A%20float%20%3D%204.0)%20-%3E%20np.ndarray%3A%0A%20%20%20%20%20%20%20%20%22%22%22Backward-only%20Gaussian%20smooth%3A%20each%20season%20averages%20itself%20and%20earlier%20seasons%20only.%22%22%22%0A%20%20%20%20%20%20%20%20values%20%3D%20np.asarray(values%2C%20dtype%3Dfloat)%0A%20%20%20%20%20%20%20%20max_lag%20%3D%20int(np.ceil(truncate%20*%20sigma))%0A%20%20%20%20%20%20%20%20out%20%3D%20np.empty_like(values)%0A%20%20%20%20%20%20%20%20for%20i%20in%20range(values.size)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20lags%20%3D%20np.arange(min(max_lag%2C%20i)%20%2B%201)%20%20%23%200%2C%201%2C%202%20...%20seasons%20back%2C%20never%20forward%0A%20%20%20%20%20%20%20%20%20%20%20%20weights%20%3D%20np.exp(-0.5%20*%20(lags%20%2F%20sigma)%20**%202)%20%20%23%20the%20right%20half%20of%20a%20bell%20curve%0A%20%20%20%20%20%20%20%20%20%20%20%20out%5Bi%5D%20%3D%20np.sum(weights%20*%20values%5Bi%20-%20lags%5D)%20%2F%20weights.sum()%0A%20%20%20%20%20%20%20%20return%20out%0A%0A%20%20%20%20symmetric%20%3D%20ndimage.gaussian_filter1d(yields%2C%202%2C%20mode%3D%22nearest%22)%0A%20%20%20%20causal%20%3D%20causal_gaussian(yields%2C%20sigma%3D2)%0A%20%20%20%20at%20%3D%20seasons%20%3D%3D%202011%0A%20%20%20%20print(f%222011%2C%20the%20season%20before%20the%20drought%3A%20yield%20%7Byields%5Bat%5D%5B0%5D%3A.1f%7D%22)%0A%20%20%20%20print(%0A%20%20%20%20%20%20%20%20f%22%20%20symmetric%20smooth%20%7Bsymmetric%5Bat%5D%5B0%5D%3A.1f%7D%20%20(already%20pulled%20down%20by%20the%202012%20drought%20it%20can%20'see')%22%0A%20%20%20%20)%0A%20%20%20%20print(f%22%20%20causal%20smooth%20%20%20%20%7Bcausal%5Bat%5D%5B0%5D%3A.1f%7D%20%20(knows%20only%202011%20and%20earlier)%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%20The%20symmetric%20smoother%20lowers%202011%20because%20of%20a%20drought%20that%20hadn't%20happened%20yet%3A%20at%20%CF%83%20%3D%202%20with%20the%204%CF%83%20cut-off%20it%20weighs%20every%20season%20up%20to%20eight%20ahead.%20For%20describing%20history%20that's%20harmless.%20For%20anything%20used%20as%20if%20it%20were%20known%20at%20the%20time%2C%20it's%20a%20leak.%20Only%20the%20symmetric%20smoother's%20*last*%20fitted%20season%20is%20backward-looking%20(section%202.2)%3B%20every%20earlier%20fitted%20season%2C%20the%20first%20included%2C%20uses%20later%20seasons.%20The%20causal%20version%20above%20truncates%20at%20%60ceil(4%CF%83)%60%20lags%2C%20while%20scipy%20and%20cocoa%20use%20%60int(4%CF%83%20%2B%200.5)%60%3B%20they%20differ%20for%20some%20%CF%83%2C%20such%20as%201.1%20(5%20against%204).%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%20replace%20%60_rng.exponential(1.0%2C%20...)%20-%201.0%60%20with%20%60_rng.uniform(-np.sqrt(3)%2C%20np.sqrt(3)%2C%20...)%60%20(mean%200%2C%20variance%201%2C%20so%20the%20sum%20matches%20the%20standard-normal%20curve)%2C%20and%20change%20the%20nudge%20counts%20%60(1%2C%203%2C%2030)%60%20to%20%60(6%2C%2012%2C%2018)%60.%20Uniform%20nudges%20have%20skew%200%20at%20every%20n%2C%20so%20watch%20the%20excess%20kurtosis%20in%20the%20titles%20instead%20(0%20for%20a%20Gaussian).%20**Check%3A**%20theory%20says%20it%20is%20%E2%88%921.2%2Fn%2C%20so%20within%20%C2%B10.1%20from%20n%20%3D%2012%3B%20does%20the%20simulation%20agree%3F%20Why%20does%20a%20uniform%20nudge%20get%20there%20faster%20than%20an%20exponential%20one%3F%0A%20%20%20%202.%20In%203.3%2C%20apply%20the%20drought%20to%202%2C%203%2C%204%2C%20%E2%80%A6%20years%20instead%20of%20one%2C%20and%20print%20the%20robust%20spread%20around%20the%20peak%20each%20time.%20**Check%3A**%20it%20barely%20moves%20up%20to%203%20years%20(about%200.038)%2C%20then%20jumps%20at%204%20(about%200.115).%20Why%204%20of%2020%3F%20(Hint%3A%20which%20value%20is%20the%2016th%20percentile%3F)%0A%20%20%20%203.%20In%202.2%2C%20change%20%60sigma_seasons%60%20to%201%20and%20then%208.%20How%20does%20the%202021%20forecast%20change%3F%20**Check%3A**%20a%20bigger%20%CF%83%20lags%20more%2C%20because%20the%20end%20effect%20grows%20with%20%CF%83%20(about%200.65%2C%202.9%20and%205.7%20bu%2Fac%20on%20the%20true%20trend%20at%20%CF%83%20%3D%201%2C%204%20and%208).%0A%20%20%20%204.%20In%204.1%2C%20set%20%60drought%5B%3A%5D%20%3D%20False%60%20straight%20after%20%60drought%60%20is%20drawn%2C%20*before*%20the%20line%20that%20subtracts%20the%20drought%20losses%2C%20then%20re-run%204.1%20and%204.2.%20Do%20the%20Gaussian%20bands%20now%20land%20on%20their%20nominal%20coverage%3F%20**Check%3A**%20every%20%22Gaussian%20covers%22%20value%20should%20be%20within%20about%20two%20points%20of%20nominal.%0A%20%20%20%205.%20In%204.4%2C%20compare%20the%20symmetric%20and%20causal%20smooths%20in%202013%2C%20the%20season%20*after*%20the%20drought.%20Which%20one%20recovers%20faster%2C%20and%20why%3F%20**Check%3A**%20you%20can%20say%20which%20smoother%20is%20still%20depressed%20in%202013%2C%20and%20by%20how%20much.%0A%0A%20%20%20%20%23%20Sources%0A%0A%20%20%20%20-%20NumPy%20%60Generator.normal%60%3A%20https%3A%2F%2Fnumpy.org%2Fdoc%2Fstable%2Freference%2Frandom%2Fgenerated%2Fnumpy.random.Generator.normal.html%0A%20%20%20%20-%20SciPy%20%60stats.norm%60%3A%20https%3A%2F%2Fdocs.scipy.org%2Fdoc%2Fscipy%2Freference%2Fgenerated%2Fscipy.stats.norm.html%2C%20and%20%60ndimage.gaussian_filter1d%60%3A%20https%3A%2F%2Fdocs.scipy.org%2Fdoc%2Fscipy%2Freference%2Fgenerated%2Fscipy.ndimage.gaussian_filter1d.html%0A%20%20%20%20-%20The%20normal%20distribution%3A%20https%3A%2F%2Fen.wikipedia.org%2Fwiki%2FNormal_distribution%3B%20the%2068%E2%80%9395%E2%80%9399.7%20rule%3A%20https%3A%2F%2Fen.wikipedia.org%2Fwiki%2F68%25E2%2580%259395%25E2%2580%259399.7_rule%0A%20%20%20%20-%20The%20central%20limit%20theorem%3A%20https%3A%2F%2Fen.wikipedia.org%2Fwiki%2FCentral_limit_theorem%0A%20%20%20%20-%20NIST%2C%20normal%20probability%20(QQ)%20plots%3A%20https%3A%2F%2Fwww.itl.nist.gov%2Fdiv898%2Fhandbook%2Feda%2Fsection3%2Fnormprpl.htm%0A%20%20%20%20-%20NumPy%20%60quantile%60%20and%20its%20%60method%60%20argument%3A%20https%3A%2F%2Fnumpy.org%2Fdoc%2Fstable%2Freference%2Fgenerated%2Fnumpy.quantile.html%0A%20%20%20%20-%20NIST%20e-Handbook%2C%20measures%20of%20scale%3A%20https%3A%2F%2Fwww.itl.nist.gov%2Fdiv898%2Fhandbook%2Feda%2Fsection3%2Feda356.htm%0A%20%20%20%20-%20Rousseeuw%20and%20Croux%20(1993)%2C%20%22Alternatives%20to%20the%20median%20absolute%20deviation%22%2C%20*JASA*%2088(424)%0A%20%20%20%20-%20Angelopoulos%20and%20Bates%20(2021)%2C%20%22A%20Gentle%20Introduction%20to%20Conformal%20Prediction%22%3A%20https%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-%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.1-the-gaussian%2Fcode.md)%20and%20%5Bscience.md%5D(https%3A%2F%2Fgithub.com%2Ftreefera%2Ftf-model-lab%2Fblob%2Fmain%2Facademy%2F0-foundations%2F0.1-the-gaussian%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
086802049cd74c83e2f43a0ba05119e4