import%20marimo%0A%0A__generated_with%20%3D%20%220.24.0%22%0Aapp%20%3D%20marimo.App()%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20import%20marimo%20as%20mo%0A%20%20%20%20import%20numpy%20as%20np%0A%20%20%20%20import%20pandas%20as%20pd%0A%20%20%20%20import%20plotly.graph_objects%20as%20go%0A%20%20%20%20from%20plotly.subplots%20import%20make_subplots%0A%20%20%20%20from%20statsmodels.tsa.holtwinters%20import%20ExponentialSmoothing%0A%0A%20%20%20%20return%20ExponentialSmoothing%2C%20go%2C%20make_subplots%2C%20mo%2C%20np%2C%20pd%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%20Note%2023%3A%20Exponentially%20Weighted%20Averages%2C%20Bias%20Correction%2C%20and%20the%20Adam%20Optimizer%20Engine%0A%0A%20%20%20%20%26larr%3B%20Previous%20Note%3A%20%5B22%20ACF%20and%20PACF%5D(22_acf_and_pacf.py)%20%7C%20Next%20Note%3A%20%5B24%20Adjusted%20R-Squared%5D(24_adjusted_r_squared.py)%20%26rarr%3B%0A%0A%20%20%20%20---%0A%0A%20%20%20%20%23%23%20%5Ba%5D%20Why%20do%20you%20need%20to%20know%20these%20concepts%3F%0A%0A%20%20%20%20In%20machine%20learning%2C%20deep%20neural%20network%20optimization%2C%20streaming%20data%20pipelines%2C%20and%20quantitative%20forecasting%2C%20tracking%20running%20estimates%20of%20means%20and%20variances%20is%20ubiquitous.%20While%20an%20ordinary%20moving%20average%20over%20window%20size%20%24W%24%20requires%20buffering%20%24W%24%20historical%20observations%20in%20memory%2C%20the%20**Exponentially%20Weighted%20Average%20(EWA)**%20(also%20known%20as%20Exponential%20Moving%20Average%2C%20EMA)%20tracks%20central%20tendency%20using%20a%20single%20scalar%20state%20variable%20in%20%24%5Cmathcal%7BO%7D(1)%24%20space%20and%20time.%0A%0A%20%20%20%20However%2C%20practical%20implementations%20face%20key%20challenges%20that%20make%20mathematical%20mastery%20essential%3A%0A%20%20%20%201.%20**The%20Cold-Start%20Zero-Initialization%20Bias**%3A%20When%20initializing%20an%20EWA%20at%20%24v_0%20%3D%200%24%2C%20early%20iterations%20(%24t%20%3D%201%2C%202%2C%20%5Cdots%24)%20suffer%20from%20severe%20downward%20bias%20because%20the%20recursive%20formula%20multiplies%20past%20states%20by%20the%20decay%20factor%20%24%5Cbeta%24.%20If%20%24%5Cbeta%20%3D%200.98%24%2C%20the%20first%20update%20only%20incorporates%20%242%5C%25%24%20of%20the%20first%20observation%20(%24v_1%20%3D%200.02%20%5Ctheta_1%24)%2C%20dragging%20early%20values%20toward%20zero.%0A%20%20%20%202.%20**Universal%20Bias%20Correction**%3A%20Dividing%20by%20the%20scaling%20factor%20%241%20-%20%5Cbeta%5Et%24%20removes%20this%20cold-start%20artifact%20entirely%2C%20producing%20an%20unbiased%20estimator%20that%20automatically%20transitions%20to%20standard%20EWA%20as%20%24t%20%5Cto%20%5Cinfty%24%20(%24%5Cbeta%5Et%20%5Cto%200%24).%0A%20%20%20%203.%20**Core%20Engine%20of%20Modern%20Optimizers%20(Adam%2C%20AdamW%2C%20RMSProp)**%3A%20The%20Adam%20optimizer%20relies%20fundamentally%20on%20bias-corrected%20first%20moments%20(%24%5Chat%7Bm%7D_t%20%3D%20%5Cfrac%7Bm_t%7D%7B1%20-%20%5Cbeta_1%5Et%7D%24)%20and%20second%20moments%20(%24%5Chat%7Bv%7D_t%20%3D%20%5Cfrac%7Bv_t%7D%7B1%20-%20%5Cbeta_2%5Et%7D%24).%20Without%20bias%20correction%2C%20initial%20gradient%20steps%20are%20severely%20suppressed%2C%20impeding%20pre-training%20convergence%20in%20Large%20Language%20Models%20and%20Vision%20Transformers.%0A%20%20%20%204.%20**Holt-Winters%20Multi-Component%20Forecasting**%3A%20In%20demand%20planning%20and%20latency%20forecasting%2C%20extending%20single%20exponential%20smoothing%20to%20Double%20(Holt's%20linear%20trend)%20and%20Triple%20(Holt-Winters%20seasonality)%20provides%20lightweight%2C%20highly%20accurate%20operational%20forecasts.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%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---%0A%0A%20%20%20%20%23%23%20%5Bb%5D%20Concept%20explanation%20with%20their%20role%20in%20ML%2FAI%2FStats%3F%0A%0A%20%20%20%20%23%23%23%201.%20The%20Recursive%20EWA%20Formulation%0A%0A%20%20%20%20Given%20a%20sequential%20stream%20of%20observations%20%24%5Ctheta_1%2C%20%5Ctheta_2%2C%20%5Cdots%2C%20%5Ctheta_t%24%2C%20the%20exponentially%20weighted%20average%20%24v_t%24%20with%20momentum%20parameter%20%24%5Cbeta%20%5Cin%20%5B0%2C%201)%24%20is%20defined%20by%20the%20recurrence%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20v_t%20%3D%20%5Cbeta%20v_%7Bt-1%7D%20%2B%20(1%20-%20%5Cbeta)%20%5Ctheta_t%2C%20%5Cquad%20v_0%20%3D%200%0A%20%20%20%20%24%24%0A%0A%20%20%20%20Unrolling%20the%20recursion%20backwards%20to%20time%20%24t%20%3D%200%24%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20%5Cbegin%7Baligned%7D%0A%20%20%20%20v_1%20%26%3D%20(1%20-%20%5Cbeta)%20%5Ctheta_1%20%5C%5C%0A%20%20%20%20v_2%20%26%3D%20%5Cbeta%20v_1%20%2B%20(1%20-%20%5Cbeta)%20%5Ctheta_2%20%3D%20%5Cbeta%20(1%20-%20%5Cbeta)%20%5Ctheta_1%20%2B%20(1%20-%20%5Cbeta)%20%5Ctheta_2%20%5C%5C%0A%20%20%20%20v_t%20%26%3D%20(1%20-%20%5Cbeta)%20%5Csum_%7Bi%3D1%7D%5Et%20%5Cbeta%5E%7Bt%20-%20i%7D%20%5Ctheta_i%0A%20%20%20%20%5Cend%7Baligned%7D%0A%20%20%20%20%24%24%0A%0A%20%20%20%20---%0A%0A%20%20%20%20%23%23%23%202.%20Derivation%20of%20the%20Cold-Start%20Bias%0A%0A%20%20%20%20Suppose%20the%20observations%20%24%5Ctheta_i%24%20are%20drawn%20from%20a%20distribution%20with%20constant%20mean%20%24%5Cmathbb%7BE%7D%5B%5Ctheta_i%5D%20%3D%20%5Cmu%24.%20Taking%20expectations%20of%20both%20sides%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20%5Cmathbb%7BE%7D%5Bv_t%5D%20%3D%20(1%20-%20%5Cbeta)%20%5Csum_%7Bi%3D1%7D%5Et%20%5Cbeta%5E%7Bt%20-%20i%7D%20%5Cmathbb%7BE%7D%5B%5Ctheta_i%5D%20%3D%20%5Cmu%20(1%20-%20%5Cbeta)%20%5Csum_%7Bi%3D1%7D%5Et%20%5Cbeta%5E%7Bt%20-%20i%7D%0A%20%20%20%20%24%24%0A%0A%20%20%20%20The%20finite%20geometric%20series%20sum%20is%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20%5Csum_%7Bi%3D1%7D%5Et%20%5Cbeta%5E%7Bt%20-%20i%7D%20%3D%20%5Csum_%7Bk%3D0%7D%5E%7Bt-1%7D%20%5Cbeta%5Ek%20%3D%20%5Cfrac%7B1%20-%20%5Cbeta%5Et%7D%7B1%20-%20%5Cbeta%7D%0A%20%20%20%20%24%24%0A%0A%20%20%20%20Substituting%20this%20back%20into%20the%20expectation%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20%5Cmathbb%7BE%7D%5Bv_t%5D%20%3D%20%5Cmu%20(1%20-%20%5Cbeta)%20%5Cleft%5B%5Cfrac%7B1%20-%20%5Cbeta%5Et%7D%7B1%20-%20%5Cbeta%7D%5Cright%5D%20%3D%20%5Cmu%20(1%20-%20%5Cbeta%5Et)%0A%20%20%20%20%24%24%0A%0A%20%20%20%20Because%20%24%5Cbeta%20%5Cin%20(0%2C%201)%24%2C%20the%20factor%20%24(1%20-%20%5Cbeta%5Et)%20%3C%201%24.%20Thus%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20%5Cmathbb%7BE%7D%5Bv_t%5D%20%5Cneq%20%5Cmu%20%5Cquad%20%5Ctext%7Bfor%20finite%20%7D%20t%0A%20%20%20%20%24%24%0A%0A%20%20%20%20For%20example%2C%20when%20%24%5Cbeta%20%3D%200.98%24%20and%20%24t%20%3D%2010%24%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%201%20-%20%5Cbeta%5E%7B10%7D%20%3D%201%20-%200.98%5E%7B10%7D%20%5Capprox%201%20-%200.817%20%3D%200.183%0A%20%20%20%20%24%24%0A%0A%20%20%20%20The%20raw%20estimate%20%24v_%7B10%7D%24%20captures%20only%20%2418.3%5C%25%24%20of%20the%20true%20expectation.%0A%0A%20%20%20%20---%0A%0A%20%20%20%20%23%23%23%203.%20The%20Exact%20Bias%20Correction%20Formula%0A%0A%20%20%20%20To%20construct%20an%20estimator%20%24%5Chat%7Bv%7D_t%24%20whose%20expectation%20equals%20%24%5Cmu%24%20for%20all%20%24t%20%5Cgeq%201%24%2C%20we%20divide%20by%20the%20accumulated%20geometric%20weight%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20%5Chat%7Bv%7D_t%20%3D%20%5Cfrac%7Bv_t%7D%7B1%20-%20%5Cbeta%5Et%7D%0A%20%20%20%20%24%24%0A%0A%20%20%20%20Taking%20expectations%20confirms%20unbiasedness%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20%5Cmathbb%7BE%7D%5B%5Chat%7Bv%7D_t%5D%20%3D%20%5Cfrac%7B%5Cmathbb%7BE%7D%5Bv_t%5D%7D%7B1%20-%20%5Cbeta%5Et%7D%20%3D%20%5Cfrac%7B%5Cmu%20(1%20-%20%5Cbeta%5Et)%7D%7B1%20-%20%5Cbeta%5Et%7D%20%3D%20%5Cmu%20%5Cquad%20%5Cforall%20t%20%5Cgeq%201%0A%20%20%20%20%24%24%0A%0A%20%20%20%20As%20%24t%20%5Cto%20%5Cinfty%24%2C%20%24%5Cbeta%5Et%20%5Cto%200%24%2C%20so%20%241%20-%20%5Cbeta%5Et%20%5Cto%201%24%2C%20meaning%20%24%5Chat%7Bv%7D_t%20%5Cto%20v_t%24.%20The%20correction%20automatically%20decays%20as%20historical%20data%20accumulates.%0A%0A%20%20%20%20---%0A%0A%20%20%20%20%23%23%23%204.%20Effective%20Window%20Size%0A%0A%20%20%20%20Since%20%24%5Clim_%7B%5Cepsilon%20%5Cto%200%7D%20(1%20-%20%5Cepsilon)%5E%7B1%2F%5Cepsilon%7D%20%3D%20%5Cfrac%7B1%7D%7Be%7D%20%5Capprox%200.368%24%2C%20after%20%24k%20%3D%20%5Cfrac%7B1%7D%7B1%20-%20%5Cbeta%7D%24%20steps%2C%20the%20weight%20assigned%20to%20an%20observation%20decays%20to%20approximately%20%24%5Cfrac%7B1%7D%7Be%7D%24%20of%20its%20initial%20contribution.%0A%0A%20%20%20%20Therefore%2C%20an%20EWA%20with%20parameter%20%24%5Cbeta%24%20effectively%20aggregates%20over%20the%20preceding%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20W_%7B%5Ctext%7Beff%7D%7D%20%5Capprox%20%5Cfrac%7B1%7D%7B1%20-%20%5Cbeta%7D%20%5Cquad%20%5Ctext%7Bobservations%7D%0A%20%20%20%20%24%24%0A%0A%20%20%20%20-%20%24%5Cbeta%20%3D%200.90%20%5Cimplies%20W_%7B%5Ctext%7Beff%7D%7D%20%5Capprox%2010%24%20steps%0A%20%20%20%20-%20%24%5Cbeta%20%3D%200.98%20%5Cimplies%20W_%7B%5Ctext%7Beff%7D%7D%20%5Capprox%2050%24%20steps%0A%20%20%20%20-%20%24%5Cbeta%20%3D%200.999%20%5Cimplies%20W_%7B%5Ctext%7Beff%7D%7D%20%5Capprox%201000%24%20steps%20(standard%20in%20Adam%20%24%5Cbeta_2%24)%0A%0A%20%20%20%20---%0A%0A%20%20%20%20%23%23%23%205.%20Application%20in%20Deep%20Learning%3A%20The%20Adam%20Optimizer%0A%0A%20%20%20%20The%20Adam%20(Adaptive%20Moment%20Estimation)%20optimizer%20maintains%20exponentially%20decaying%20averages%20of%20past%20gradients%20(%24m_t%24%2C%20first%20moment)%20and%20squared%20gradients%20(%24v_t%24%2C%20second%20uncentered%20moment)%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20g_t%20%3D%20%5Cnabla_%5Ctheta%20%5Cmathcal%7BL%7D_t(%5Ctheta_t)%0A%20%20%20%20%24%24%0A%0A%20%20%20%20%24%24%0A%20%20%20%20m_t%20%3D%20%5Cbeta_1%20m_%7Bt-1%7D%20%2B%20(1%20-%20%5Cbeta_1)%20g_t%2C%20%5Cquad%20%5Chat%7Bm%7D_t%20%3D%20%5Cfrac%7Bm_t%7D%7B1%20-%20%5Cbeta_1%5Et%7D%0A%20%20%20%20%24%24%0A%0A%20%20%20%20%24%24%0A%20%20%20%20v_t%20%3D%20%5Cbeta_2%20v_%7Bt-1%7D%20%2B%20(1%20-%20%5Cbeta_2)%20g_t%5E2%2C%20%5Cquad%20%5Chat%7Bv%7D_t%20%3D%20%5Cfrac%7Bv_t%7D%7B1%20-%20%5Cbeta_2%5Et%7D%0A%20%20%20%20%24%24%0A%0A%20%20%20%20%24%24%0A%20%20%20%20%5Ctheta_%7Bt%2B1%7D%20%3D%20%5Ctheta_t%20-%20%5Cfrac%7B%5Ceta%7D%7B%5Csqrt%7B%5Chat%7Bv%7D_t%7D%20%2B%20%5Cepsilon%7D%20%5Chat%7Bm%7D_t%0A%20%20%20%20%24%24%0A%0A%20%20%20%20Without%20bias%20correction%2C%20because%20%24%5Cbeta_2%20%3D%200.999%24%20initially%2C%20%24v_1%20%3D%200.001%20g_1%5E2%24%2C%20which%20would%20make%20%24%5Csqrt%7Bv_1%7D%24%20tiny%20and%20cause%20the%20initial%20parameter%20update%20step%20size%20to%20explode.%20Bias%20correction%20prevents%20this%20instability.%0A%0A%20%20%20%20---%0A%0A%20%20%20%20%23%23%23%206.%20Holt-Winters%20Exponential%20Smoothing%20Hierarchy%0A%0A%20%20%20%20%7C%20Model%20%7C%20Components%20Captured%20%7C%20Recurrence%20Equations%20%7C%20Forecast%20%24%5Chat%7BY%7D_%7Bt%2Bh%7D%24%20%7C%0A%20%20%20%20%7C%20%3A---%20%7C%20%3A---%20%7C%20%3A---%20%7C%20%3A---%20%7C%0A%20%20%20%20%7C%20**Single%20(SES)**%20%7C%20Level%20(%24L_t%24)%20%7C%20%24L_t%20%3D%20%5Calpha%20Y_t%20%2B%20(1%20-%20%5Calpha)%20L_%7Bt-1%7D%24%20%7C%20%24%5Chat%7BY%7D_%7Bt%2Bh%7D%20%3D%20L_t%24%20%7C%0A%20%20%20%20%7C%20**Double%20(DES%20%2F%20Holt)**%20%7C%20Level%20%2B%20Trend%20(%24T_t%24)%20%7C%20%24L_t%20%3D%20%5Calpha%20Y_t%20%2B%20(1%20-%20%5Calpha)(L_%7Bt-1%7D%20%2B%20T_%7Bt-1%7D)%24%3Cbr%3E%24T_t%20%3D%20%5Cbeta%20(L_t%20-%20L_%7Bt-1%7D)%20%2B%20(1%20-%20%5Cbeta)%20T_%7Bt-1%7D%24%20%7C%20%24%5Chat%7BY%7D_%7Bt%2Bh%7D%20%3D%20L_t%20%2B%20h%20T_t%24%20%7C%0A%20%20%20%20%7C%20**Triple%20(TES%20%2F%20Holt-Winters)**%20%7C%20Level%20%2B%20Trend%20%2B%20Seasonality%20(%24S_t%24)%20%7C%20%24L_t%20%3D%20%5Calpha%20(Y_t%20-%20S_%7Bt-m%7D)%20%2B%20(1%20-%20%5Calpha)(L_%7Bt-1%7D%20%2B%20T_%7Bt-1%7D)%24%3Cbr%3E%24T_t%20%3D%20%5Cbeta%20(L_t%20-%20L_%7Bt-1%7D)%20%2B%20(1%20-%20%5Cbeta)%20T_%7Bt-1%7D%24%3Cbr%3E%24S_t%20%3D%20%5Cgamma%20(Y_t%20-%20L_t)%20%2B%20(1%20-%20%5Cgamma)%20S_%7Bt-m%7D%24%20%7C%20%24%5Chat%7BY%7D_%7Bt%2Bh%7D%20%3D%20L_t%20%2B%20h%20T_t%20%2B%20S_%7Bt-m%2Bh%7D%24%20%7C%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(np%2C%20pd)%3A%0A%20%20%20%20%23%20Simulation%20Data%3A%20Cold-start%20step%20function%20%2B%20Seasonal%20time%20series%0A%20%20%20%20np.random.seed(47)%0A%20%20%20%20_n%20%3D%2080%0A%0A%20%20%20%20%23%20Constant%20signal%20with%20additive%20Gaussian%20noise%20to%20demonstrate%20cold-start%20bias%0A%20%20%20%20_true_mean%20%3D%2050.0%0A%20%20%20%20signal_coldstart%20%3D%20_true_mean%20%2B%20np.random.normal(0.0%2C%203.0%2C%20_n)%0A%0A%20%20%20%20%23%20Seasonal%20series%20for%20Holt-Winters%20(Daily%20data%20with%20weekly%20period%20m%3D7%2C%20linear%20trend)%0A%20%20%20%20_n_seasonal%20%3D%2045%0A%20%20%20%20_periods%20%3D%207%0A%20%20%20%20_t%20%3D%20np.arange(_n_seasonal)%0A%20%20%20%20_trend%20%3D%200.6%20*%20_t%20%2B%2020.0%0A%20%20%20%20_season%20%3D%205.0%20*%20np.sin(2%20*%20np.pi%20*%20_t%20%2F%20_periods)%0A%20%20%20%20_noise%20%3D%20np.random.normal(0.0%2C%201.2%2C%20_n_seasonal)%0A%20%20%20%20_series_seasonal%20%3D%20_trend%20%2B%20_season%20%2B%20_noise%0A%0A%20%20%20%20dates_seasonal%20%3D%20pd.date_range(%222025-01-01%22%2C%20periods%3D_n_seasonal%2C%20freq%3D%22D%22)%0A%20%20%20%20df_seasonal%20%3D%20pd.DataFrame(%7B%22Date%22%3A%20dates_seasonal%2C%20%22Value%22%3A%20np.round(_series_seasonal%2C%202)%7D)%0A%20%20%20%20return%20df_seasonal%2C%20signal_coldstart%0A%0A%0A%40app.cell%0Adef%20_(%0A%20%20%20%20ExponentialSmoothing%2C%0A%20%20%20%20df_seasonal%2C%0A%20%20%20%20go%2C%0A%20%20%20%20make_subplots%2C%0A%20%20%20%20np%2C%0A%20%20%20%20pd%2C%0A%20%20%20%20signal_coldstart%2C%0A)%3A%0A%20%20%20%20%23%20Interactive%20Visualizations%20Cell%3A%0A%20%20%20%20%23%20Subplot%201%3A%20Cold-Start%20Bias%3A%20Raw%20EWA%20vs.%20Bias-Corrected%20EWA%20(beta%3D0.98)%0A%20%20%20%20%23%20Subplot%202%3A%20Exponential%20Memory%20Decay%20Kernels%20across%20beta%20values%0A%20%20%20%20%23%20Subplot%203%3A%20Holt-Winters%20(SES%2C%20DES%2C%20TES)%20Decomposition%20%26%20Forecast%0A%0A%20%20%20%20_fig%20%3D%20make_subplots(%0A%20%20%20%20%20%20%20%20rows%3D1%2C%0A%20%20%20%20%20%20%20%20cols%3D3%2C%0A%20%20%20%20%20%20%20%20subplot_titles%3D(%0A%20%20%20%20%20%20%20%20%20%20%20%20%221.%20Cold-Start%20Bias%3A%20Raw%20vs.%20Corrected%20(beta%3D0.98)%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%222.%20Effective%20Memory%20Window%20Weight%20Kernels%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%223.%20Holt-Winters%20Hierarchy%20%26%20Seasonal%20Forecast%22%2C%0A%20%20%20%20%20%20%20%20)%2C%0A%20%20%20%20%20%20%20%20horizontal_spacing%3D0.08%2C%0A%20%20%20%20)%0A%0A%20%20%20%20%23%20Subplot%201%3A%20Cold%20Start%20EWA%0A%20%20%20%20_beta%20%3D%200.98%0A%20%20%20%20_n_steps%20%3D%20len(signal_coldstart)%0A%20%20%20%20_raw_ewa%20%3D%20np.zeros(_n_steps)%0A%20%20%20%20_corrected_ewa%20%3D%20np.zeros(_n_steps)%0A%0A%20%20%20%20_running_v%20%3D%200.0%0A%20%20%20%20for%20_i%2C%20_obs%20in%20enumerate(signal_coldstart)%3A%0A%20%20%20%20%20%20%20%20_running_v%20%3D%20_beta%20*%20_running_v%20%2B%20(1.0%20-%20_beta)%20*%20_obs%0A%20%20%20%20%20%20%20%20_raw_ewa%5B_i%5D%20%3D%20_running_v%0A%20%20%20%20%20%20%20%20_corrected_ewa%5B_i%5D%20%3D%20_running_v%20%2F%20(1.0%20-%20_beta%20**%20(_i%20%2B%201))%0A%0A%20%20%20%20_fig.add_trace(%0A%20%20%20%20%20%20%20%20go.Scatter(%0A%20%20%20%20%20%20%20%20%20%20%20%20x%3Dnp.arange(1%2C%20_n_steps%20%2B%201)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20y%3Dsignal_coldstart%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20mode%3D%22markers%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20marker%3Ddict(size%3D4%2C%20color%3D%22%239ca3af%22)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20name%3D%22Noisy%20Observations%20(True%20Mean%3D50)%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20hovertemplate%3D%22t%3D%25%7Bx%7D%3A%20Obs%3D%25%7By%3A.1f%7D%3Cextra%3E%3C%2Fextra%3E%22%2C%0A%20%20%20%20%20%20%20%20)%2C%0A%20%20%20%20%20%20%20%20row%3D1%2C%0A%20%20%20%20%20%20%20%20col%3D1%2C%0A%20%20%20%20)%0A%20%20%20%20_fig.add_trace(%0A%20%20%20%20%20%20%20%20go.Scatter(%0A%20%20%20%20%20%20%20%20%20%20%20%20x%3Dnp.arange(1%2C%20_n_steps%20%2B%201)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20y%3D_raw_ewa%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20mode%3D%22lines%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20line%3Ddict(color%3D%22%23ef4444%22%2C%20width%3D2.5)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20name%3D%22Raw%20EWA%20(v_t%2C%20Zero-Biased)%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20hovertemplate%3D%22t%3D%25%7Bx%7D%3A%20Raw%3D%25%7By%3A.2f%7D%3Cextra%3E%3C%2Fextra%3E%22%2C%0A%20%20%20%20%20%20%20%20)%2C%0A%20%20%20%20%20%20%20%20row%3D1%2C%0A%20%20%20%20%20%20%20%20col%3D1%2C%0A%20%20%20%20)%0A%20%20%20%20_fig.add_trace(%0A%20%20%20%20%20%20%20%20go.Scatter(%0A%20%20%20%20%20%20%20%20%20%20%20%20x%3Dnp.arange(1%2C%20_n_steps%20%2B%201)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20y%3D_corrected_ewa%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20mode%3D%22lines%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20line%3Ddict(color%3D%22%2310b981%22%2C%20width%3D2.5)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20name%3D%22Bias-Corrected%20EWA%20(v_hat_t)%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20hovertemplate%3D%22t%3D%25%7Bx%7D%3A%20Corrected%3D%25%7By%3A.2f%7D%3Cextra%3E%3C%2Fextra%3E%22%2C%0A%20%20%20%20%20%20%20%20)%2C%0A%20%20%20%20%20%20%20%20row%3D1%2C%0A%20%20%20%20%20%20%20%20col%3D1%2C%0A%20%20%20%20)%0A%0A%20%20%20%20%23%20Subplot%202%3A%20Exponential%20Memory%20Kernels%0A%20%20%20%20_lags%20%3D%20np.arange(0%2C%2040)%0A%20%20%20%20for%20_b%2C%20_col%20in%20%5B(0.80%2C%20%22%233b82f6%22)%2C%20(0.90%2C%20%22%23f59e0b%22)%2C%20(0.98%2C%20%22%238b5cf6%22)%5D%3A%0A%20%20%20%20%20%20%20%20_weights%20%3D%20(1.0%20-%20_b)%20*%20(_b**_lags)%0A%20%20%20%20%20%20%20%20_fig.add_trace(%0A%20%20%20%20%20%20%20%20%20%20%20%20go.Scatter(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20x%3D_lags%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20y%3D_weights%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20mode%3D%22lines%2Bmarkers%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20marker%3Ddict(size%3D5)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20line%3Ddict(color%3D_col%2C%20width%3D2)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20name%3Df%22beta%20%3D%20%7B_b%3A.2f%7D%20(W_eff%20~%20%7Bint(1%20%2F%20(1%20-%20_b))%7D)%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20row%3D1%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20col%3D2%2C%0A%20%20%20%20%20%20%20%20)%0A%0A%20%20%20%20%23%20Subplot%203%3A%20Holt-Winters%20Fits%20%26%20Forecast%0A%20%20%20%20_val_series%20%3D%20df_seasonal%5B%22Value%22%5D%0A%20%20%20%20_ses_mod%20%3D%20ExponentialSmoothing(_val_series%2C%20trend%3DNone%2C%20seasonal%3DNone).fit(smoothing_level%3D0.3)%0A%20%20%20%20_des_mod%20%3D%20ExponentialSmoothing(_val_series%2C%20trend%3D%22add%22%2C%20seasonal%3DNone).fit()%0A%20%20%20%20_tes_mod%20%3D%20ExponentialSmoothing(_val_series%2C%20trend%3D%22add%22%2C%20seasonal%3D%22add%22%2C%20seasonal_periods%3D7).fit()%0A%0A%20%20%20%20_h%20%3D%207%0A%20%20%20%20_future_dates%20%3D%20pd.date_range(df_seasonal%5B%22Date%22%5D.iloc%5B-1%5D%20%2B%20pd.Timedelta(days%3D1)%2C%20periods%3D_h%2C%20freq%3D%22D%22)%0A%20%20%20%20_tes_forecast%20%3D%20_tes_mod.forecast(_h)%0A%0A%20%20%20%20_fig.add_trace(%0A%20%20%20%20%20%20%20%20go.Scatter(%0A%20%20%20%20%20%20%20%20%20%20%20%20x%3Ddf_seasonal%5B%22Date%22%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20y%3D_val_series%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20mode%3D%22markers%2Blines%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20marker%3Ddict(size%3D4)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20line%3Ddict(color%3D%22%236b7280%22%2C%20width%3D1)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20name%3D%22Seasonal%20Observed%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20showlegend%3DFalse%2C%0A%20%20%20%20%20%20%20%20)%2C%0A%20%20%20%20%20%20%20%20row%3D1%2C%0A%20%20%20%20%20%20%20%20col%3D3%2C%0A%20%20%20%20)%0A%20%20%20%20_fig.add_trace(%0A%20%20%20%20%20%20%20%20go.Scatter(%0A%20%20%20%20%20%20%20%20%20%20%20%20x%3Ddf_seasonal%5B%22Date%22%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20y%3D_ses_mod.fittedvalues%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20mode%3D%22lines%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20line%3Ddict(color%3D%22%23f97316%22%2C%20width%3D1.5%2C%20dash%3D%22dot%22)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20name%3D%22Single%20(SES)%22%2C%0A%20%20%20%20%20%20%20%20)%2C%0A%20%20%20%20%20%20%20%20row%3D1%2C%0A%20%20%20%20%20%20%20%20col%3D3%2C%0A%20%20%20%20)%0A%20%20%20%20_fig.add_trace(%0A%20%20%20%20%20%20%20%20go.Scatter(%0A%20%20%20%20%20%20%20%20%20%20%20%20x%3Ddf_seasonal%5B%22Date%22%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20y%3D_des_mod.fittedvalues%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20mode%3D%22lines%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20line%3Ddict(color%3D%22%233b82f6%22%2C%20width%3D1.5%2C%20dash%3D%22dash%22)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20name%3D%22Double%20(DES%20%2F%20Holt)%22%2C%0A%20%20%20%20%20%20%20%20)%2C%0A%20%20%20%20%20%20%20%20row%3D1%2C%0A%20%20%20%20%20%20%20%20col%3D3%2C%0A%20%20%20%20)%0A%20%20%20%20_fig.add_trace(%0A%20%20%20%20%20%20%20%20go.Scatter(%0A%20%20%20%20%20%20%20%20%20%20%20%20x%3Ddf_seasonal%5B%22Date%22%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20y%3D_tes_mod.fittedvalues%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20mode%3D%22lines%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20line%3Ddict(color%3D%22%2310b981%22%2C%20width%3D2)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20name%3D%22Triple%20(TES%20%2F%20Holt-Winters)%22%2C%0A%20%20%20%20%20%20%20%20)%2C%0A%20%20%20%20%20%20%20%20row%3D1%2C%0A%20%20%20%20%20%20%20%20col%3D3%2C%0A%20%20%20%20)%0A%20%20%20%20_fig.add_trace(%0A%20%20%20%20%20%20%20%20go.Scatter(%0A%20%20%20%20%20%20%20%20%20%20%20%20x%3D_future_dates%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20y%3D_tes_forecast%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20mode%3D%22lines%2Bmarkers%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20marker%3Ddict(size%3D6%2C%20symbol%3D%22star%22)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20line%3Ddict(color%3D%22%23047857%22%2C%20width%3D2.5%2C%20dash%3D%22dash%22)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20name%3D%22TES%207-Day%20Forecast%22%2C%0A%20%20%20%20%20%20%20%20)%2C%0A%20%20%20%20%20%20%20%20row%3D1%2C%0A%20%20%20%20%20%20%20%20col%3D3%2C%0A%20%20%20%20)%0A%0A%20%20%20%20_fig.update_layout(%0A%20%20%20%20%20%20%20%20template%3D%22plotly_white%22%2C%0A%20%20%20%20%20%20%20%20height%3D480%2C%0A%20%20%20%20%20%20%20%20title%3Ddict(%0A%20%20%20%20%20%20%20%20%20%20%20%20text%3D%22Exponential%20Moving%20Averages%3A%20Cold-Start%20Bias%20Correction%2C%20Decay%20Kernels%2C%20and%20Holt-Winters%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20x%3D0.5%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20xanchor%3D%22center%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20font%3Ddict(size%3D16%2C%20family%3D%22Inter%2C%20system-ui%2C%20sans-serif%22)%2C%0A%20%20%20%20%20%20%20%20)%2C%0A%20%20%20%20%20%20%20%20legend%3Ddict(orientation%3D%22h%22%2C%20yanchor%3D%22bottom%22%2C%20y%3D-0.25%2C%20xanchor%3D%22center%22%2C%20x%3D0.5)%2C%0A%20%20%20%20%20%20%20%20margin%3Ddict(l%3D40%2C%20r%3D40%2C%20t%3D75%2C%20b%3D80)%2C%0A%20%20%20%20)%0A%0A%20%20%20%20_fig.update_xaxes(title_text%3D%22Iteration%20Step%20(t)%22%2C%20row%3D1%2C%20col%3D1)%0A%20%20%20%20_fig.update_yaxes(title_text%3D%22Value%22%2C%20row%3D1%2C%20col%3D1)%0A%0A%20%20%20%20_fig.update_xaxes(title_text%3D%22Past%20Lag%20(k)%22%2C%20row%3D1%2C%20col%3D2)%0A%20%20%20%20_fig.update_yaxes(title_text%3D%22Weight%20Contribution%22%2C%20row%3D1%2C%20col%3D2)%0A%0A%20%20%20%20_fig.update_xaxes(title_text%3D%22Date%22%2C%20row%3D1%2C%20col%3D3)%0A%20%20%20%20_fig.update_yaxes(title_text%3D%22Observed%20%2F%20Fitted%20Value%22%2C%20row%3D1%2C%20col%3D3)%0A%20%20%20%20return%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---%0A%0A%20%20%20%20%23%23%20%5Bd%5D%20Code%20Examples%0A%0A%20%20%20%20Below%20we%20implement%20two%20end-to-end%20production%20algorithmic%20workflows%3A%0A%20%20%20%201.%20**Bias-Corrected%20EWA%20vs.%20Raw%20EWA%20Step-by-Step%20Diagnostic**%3A%20Step-by-step%20numerical%20verification%20of%20how%20the%20factor%20%241%20-%20%5Cbeta%5Et%24%20eliminates%20the%20cold-start%20deficit%20across%20early%20iterations%20(%24t%20%3D%201%2C%20%5Cdots%2C%2020%24).%0A%20%20%20%202.%20**Adam%20Optimizer%20Engine%20Simulation**%3A%20A%20vectorized%20simulation%20comparing%20naive%20SGD%2C%20Adam%20without%20bias%20correction%2C%20and%20Adam%20with%20full%20bias%20correction%20on%20a%20noisy%20gradient%20optimization%20task.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(np%2C%20pd%2C%20signal_coldstart)%3A%0A%20%20%20%20%23%20Example%201%3A%20Pure%20NumPy%20Step-by-Step%20Cold-Start%20Verification%0A%20%20%20%20_beta%20%3D%200.98%0A%20%20%20%20_steps%20%3D%2015%0A%20%20%20%20_true_mean%20%3D%2050.0%0A%0A%20%20%20%20_records%20%3D%20%5B%5D%0A%20%20%20%20_v_raw%20%3D%200.0%0A%0A%20%20%20%20for%20_t%20in%20range(1%2C%20_steps%20%2B%201)%3A%0A%20%20%20%20%20%20%20%20_obs%20%3D%20signal_coldstart%5B_t%20-%201%5D%0A%20%20%20%20%20%20%20%20_v_raw%20%3D%20_beta%20*%20_v_raw%20%2B%20(1.0%20-%20_beta)%20*%20_obs%0A%20%20%20%20%20%20%20%20_correction_factor%20%3D%201.0%20-%20_beta**_t%0A%20%20%20%20%20%20%20%20_v_hat%20%3D%20_v_raw%20%2F%20_correction_factor%0A%20%20%20%20%20%20%20%20_pct_recovered%20%3D%20_correction_factor%20*%20100.0%0A%0A%20%20%20%20%20%20%20%20_records.append(%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%22Step%20t%22%3A%20_t%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22Observation%20theta_t%22%3A%20f%22%7B_obs%3A.2f%7D%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22Raw%20EWA%20(v_t)%22%3A%20f%22%7B_v_raw%3A.2f%7D%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22Weight%20Sum%20(1%20-%20beta%5Et)%22%3A%20f%22%7B_correction_factor%3A.4f%7D%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22Bias-Corrected%20EWA%22%3A%20f%22%7B_v_hat%3A.2f%7D%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22Unbiased%20Recovery%22%3A%20f%22%7B_pct_recovered%3A.1f%7D%25%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22Error%20vs%20Mean%20(50.0)%22%3A%20f%22%7Bnp.abs(_v_hat%20-%20_true_mean)%3A.2f%7D%22%2C%0A%20%20%20%20%20%20%20%20%7D)%0A%0A%20%20%20%20_df_step_eval%20%3D%20pd.DataFrame(_records)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(np%2C%20pd)%3A%0A%20%20%20%20%23%20Example%202%3A%20Adam%20Optimizer%20Simulation%20(Raw%20vs.%20Bias-Corrected%20Moments)%0A%20%20%20%20%23%20Objective%3A%20Minimize%200.5%20*%20a%20*%20theta%5E2%20with%20noisy%20gradients%20g_t%20%3D%20a%20*%20theta%20%2B%20N(0%2C%20sigma%5E2)%0A%20%20%20%20_a%20%3D%202.0%0A%20%20%20%20_lr%20%3D%200.1%0A%20%20%20%20_beta1%20%3D%200.90%0A%20%20%20%20_beta2%20%3D%200.999%0A%20%20%20%20_eps%20%3D%201e-8%0A%20%20%20%20_iterations%20%3D%2040%0A%0A%20%20%20%20np.random.seed(42)%0A%20%20%20%20_theta_sgd%20%3D%2010.0%0A%20%20%20%20_theta_uncorrected%20%3D%2010.0%0A%20%20%20%20_theta_adam%20%3D%2010.0%0A%0A%20%20%20%20_m_unc%20%3D%200.0%0A%20%20%20%20_v_unc%20%3D%200.0%0A%0A%20%20%20%20_m_cor%20%3D%200.0%0A%20%20%20%20_v_cor%20%3D%200.0%0A%0A%20%20%20%20_history%20%3D%20%5B%5D%0A%0A%20%20%20%20for%20_t%20in%20range(1%2C%20_iterations%20%2B%201)%3A%0A%20%20%20%20%20%20%20%20_noise%20%3D%20np.random.normal(0.0%2C%201.0)%0A%20%20%20%20%20%20%20%20_grad_sgd%20%3D%20_a%20*%20_theta_sgd%20%2B%20_noise%0A%20%20%20%20%20%20%20%20_grad_unc%20%3D%20_a%20*%20_theta_uncorrected%20%2B%20_noise%0A%20%20%20%20%20%20%20%20_grad_cor%20%3D%20_a%20*%20_theta_adam%20%2B%20_noise%0A%0A%20%20%20%20%20%20%20%20%23%201.%20Pure%20SGD%0A%20%20%20%20%20%20%20%20_theta_sgd%20-%3D%20_lr%20*%20_grad_sgd%0A%0A%20%20%20%20%20%20%20%20%23%202.%20Adam%20without%20Bias%20Correction%0A%20%20%20%20%20%20%20%20_m_unc%20%3D%20_beta1%20*%20_m_unc%20%2B%20(1.0%20-%20_beta1)%20*%20_grad_unc%0A%20%20%20%20%20%20%20%20_v_unc%20%3D%20_beta2%20*%20_v_unc%20%2B%20(1.0%20-%20_beta2)%20*%20(_grad_unc**2)%0A%20%20%20%20%20%20%20%20_theta_uncorrected%20-%3D%20_lr%20*%20(_m_unc%20%2F%20(np.sqrt(_v_unc)%20%2B%20_eps))%0A%0A%20%20%20%20%20%20%20%20%23%203.%20Adam%20with%20Full%20Bias%20Correction%0A%20%20%20%20%20%20%20%20_m_cor%20%3D%20_beta1%20*%20_m_cor%20%2B%20(1.0%20-%20_beta1)%20*%20_grad_cor%0A%20%20%20%20%20%20%20%20_v_cor%20%3D%20_beta2%20*%20_v_cor%20%2B%20(1.0%20-%20_beta2)%20*%20(_grad_cor**2)%0A%20%20%20%20%20%20%20%20_m_hat%20%3D%20_m_cor%20%2F%20(1.0%20-%20_beta1**_t)%0A%20%20%20%20%20%20%20%20_v_hat%20%3D%20_v_cor%20%2F%20(1.0%20-%20_beta2**_t)%0A%20%20%20%20%20%20%20%20_theta_adam%20-%3D%20_lr%20*%20(_m_hat%20%2F%20(np.sqrt(_v_hat)%20%2B%20_eps))%0A%0A%20%20%20%20%20%20%20%20if%20_t%20in%20%5B1%2C%202%2C%205%2C%2010%2C%2020%2C%2030%2C%2040%5D%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20_history.append(%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22Iteration%20t%22%3A%20_t%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22SGD%20Parameter%22%3A%20f%22%7B_theta_sgd%3A.4f%7D%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22Adam%20(Uncorrected)%22%3A%20f%22%7B_theta_uncorrected%3A.4f%7D%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22Adam%20(Bias-Corrected)%22%3A%20f%22%7B_theta_adam%3A.4f%7D%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22Loss%20Ratio%20(Corrected%20vs%20Uncorrected)%22%3A%20f%22%7B(_theta_adam**2)%20%2F%20(_theta_uncorrected**2%20%2B%201e-9)%3A.4f%7D%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D)%0A%0A%20%20%20%20_df_adam_eval%20%3D%20pd.DataFrame(_history)%0A%20%20%20%20return%0A%0A%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20app.run()%0A
81eb42d01c8e70ec5d0ed024536458d4