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.stats.diagnostic%20import%20acorr_ljungbox%0A%20%20%20%20from%20statsmodels.tsa.stattools%20import%20acf%2C%20pacf%0A%0A%20%20%20%20return%20acf%2C%20acorr_ljungbox%2C%20go%2C%20make_subplots%2C%20mo%2C%20np%2C%20pacf%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%2022%3A%20Autocorrelation%20(ACF)%2C%20Partial%20Autocorrelation%20(PACF)%2C%20and%20ARMA%20Process%20Identification%0A%0A%20%20%20%20%26larr%3B%20Previous%20Note%3A%20%5B21%20Kruskal-Wallis%5D(21_kruskal_wallis.py)%20%7C%20Next%20Note%3A%20%5B23%20EWA%20and%20Bias%20Correction%5D(23_ewa_and_bias_correction.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%20time%20series%20forecasting%2C%20sequential%20machine%20learning%2C%20financial%20econometrics%2C%20and%20systems%20telemetry%2C%20consecutive%20observations%20violate%20the%20standard%20statistical%20assumption%20of%20independent%20and%20identically%20distributed%20(i.i.d.)%20observations.%20Successive%20data%20points%20carry%20temporal%20memory.%0A%0A%20%20%20%20Understanding%20the%20**Autocorrelation%20Function%20(ACF)**%20and%20**Partial%20Autocorrelation%20Function%20(PACF)**%20is%20essential%20for%20four%20core%20reasons%3A%0A%20%20%20%201.%20**Box-Jenkins%20Model%20Identification**%3A%20The%20complementary%20visual%20patterns%20of%20ACF%20and%20PACF%20serve%20as%20the%20universal%20fingerprint%20for%20identifying%20the%20signature%20order%20of%20Autoregressive%20Moving%20Average%20models%20(%24%5Ctext%7BARMA%7D(p%2C%20q)%24).%0A%20%20%20%202.%20**Disentangling%20Direct%20vs.%20Indirect%20Lag%20Dependence**%3A%20If%20a%20temperature%20series%20today%20depends%20on%20yesterday's%20temperature%20(%24%5Crho_1%20%3D%200.8%24)%2C%20it%20will%20naturally%20correlate%20with%20the%20temperature%20two%20days%20ago%20(%24%5Crho_2%20%5Capprox%200.64%24)%20simply%20through%20transitivity.%20The%20PACF%20removes%20the%20mediating%20influence%20of%20lag%201%20to%20test%20whether%20lag%202%20exerts%20an%20autonomous%2C%20direct%20causal%20impact.%0A%20%20%20%203.%20**Stationarity%20and%20Unit%20Root%20Screening**%3A%20If%20the%20sample%20ACF%20decays%20extremely%20slowly%20(linearly%20rather%20than%20geometrically)%20and%20remains%20statistically%20significant%20across%20dozens%20of%20lags%2C%20the%20process%20is%20non-stationary%20and%20contains%20a%20unit%20root%20(requiring%20differencing%20%24d%20%5Cgeq%201%24).%0A%20%20%20%204.%20**Residual%20Whiteness%20Diagnostics**%3A%20After%20training%20any%20predictive%20time%20series%20model%20(ARIMA%2C%20Prophet%2C%20LSTM%2C%20Temporal%20Fusion%20Transformer%2C%20or%20State%20Space%20Model)%2C%20the%20model%20residuals%20must%20be%20uncorrelated%20white%20noise.%20Plotting%20residual%20ACF%2FPACF%20and%20running%20the%20Ljung-Box%20portmanteau%20test%20verifies%20whether%20uncaptured%20temporal%20structure%20remains.%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.%20Weak%20Stationarity%0A%0A%20%20%20%20A%20discrete-time%20univariate%20stochastic%20process%20%24%5C%7BX_t%5C%7D_%7Bt%20%5Cin%20%5Cmathbb%7BZ%7D%7D%24%20is%20**weakly%20(second-order)%20stationary**%20if%3A%0A%20%20%20%201.%20The%20mean%20is%20constant%20over%20all%20time%3A%20%24%5Cmathbb%7BE%7D%5BX_t%5D%20%3D%20%5Cmu%24%20for%20all%20%24t%24.%0A%20%20%20%202.%20The%20variance%20is%20finite%20and%20time-invariant%3A%20%24%5Coperatorname%7BVar%7D(X_t)%20%3D%20%5Cgamma_0%20%3C%20%5Cinfty%24.%0A%20%20%20%203.%20The%20autocovariance%20depends%20exclusively%20on%20the%20temporal%20lag%20%24k%24%2C%20not%20on%20the%20absolute%20time%20index%20%24t%24%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20%5Cgamma_k%20%3D%20%5Coperatorname%7BCov%7D(X_t%2C%20X_%7Bt-k%7D)%20%3D%20%5Cmathbb%7BE%7D%5B(X_t%20-%20%5Cmu)(X_%7Bt-k%7D%20-%20%5Cmu)%5D%0A%20%20%20%20%24%24%0A%0A%20%20%20%20---%0A%0A%20%20%20%20%23%23%23%202.%20The%20Autocorrelation%20Function%20(ACF)%0A%0A%20%20%20%20The%20population%20autocorrelation%20at%20lag%20%24k%24%20is%20the%20normalized%20autocovariance%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20%5Crho_k%20%3D%20%5Cfrac%7B%5Cgamma_k%7D%7B%5Cgamma_0%7D%20%3D%20%5Cfrac%7B%5Coperatorname%7BCov%7D(X_t%2C%20X_%7Bt-k%7D)%7D%7B%5Coperatorname%7BVar%7D(X_t)%7D%2C%20%5Cquad%20k%20%3D%200%2C%201%2C%202%2C%20%5Cdots%0A%20%20%20%20%24%24%0A%0A%20%20%20%20By%20symmetry%2C%20%24%5Crho_0%20%3D%201%24%20and%20%24%5Crho_%7B-k%7D%20%3D%20%5Crho_k%24.%20For%20a%20sample%20of%20length%20%24n%24%2C%20the%20plug-in%20sample%20autocorrelation%20%24r_k%24%20is%20computed%20as%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20r_k%20%3D%20%5Cfrac%7B%5Csum_%7Bt%3Dk%2B1%7D%5En%20(X_t%20-%20%5Cbar%7BX%7D)(X_%7Bt-k%7D%20-%20%5Cbar%7BX%7D)%7D%7B%5Csum_%7Bt%3D1%7D%5En%20(X_t%20-%20%5Cbar%7BX%7D)%5E2%7D%0A%20%20%20%20%24%24%0A%0A%20%20%20%20%23%23%23%23%20Bartlett's%20Asymptotic%20Confidence%20Bands%0A%20%20%20%20Under%20the%20null%20hypothesis%20that%20the%20series%20is%20independent%20white%20noise%20(%24X_t%20%5Csim%20%5Ctext%7BWN%7D(0%2C%20%5Csigma%5E2)%24)%2C%20Bartlett%20proved%20that%20the%20sample%20autocorrelations%20are%20asymptotically%20independent%20and%20normally%20distributed%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20r_k%20%5Cxrightarrow%7Bd%7D%20%5Cmathcal%7BN%7D%5Cleft(0%2C%20%5C%2C%20%5Cfrac%7B1%7D%7Bn%7D%5Cright)%20%5Cquad%20%5Cforall%20k%20%5Cgeq%201%0A%20%20%20%20%24%24%0A%0A%20%20%20%20Thus%2C%20the%20standard%2095%25%20confidence%20bounds%20are%20drawn%20at%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20%5Ctext%7BCI%7D_%7B0.95%7D%20%3D%20%5Cpm%20%5Cfrac%7B1.96%7D%7B%5Csqrt%7Bn%7D%7D%0A%20%20%20%20%24%24%0A%0A%20%20%20%20Any%20lag%20coefficient%20falling%20outside%20this%20band%20indicates%20statistically%20significant%20serial%20dependence.%0A%0A%20%20%20%20---%0A%0A%20%20%20%20%23%23%23%203.%20The%20Partial%20Autocorrelation%20Function%20(PACF)%0A%0A%20%20%20%20The%20partial%20autocorrelation%20at%20lag%20%24k%24%2C%20denoted%20%24%5Cphi_%7Bkk%7D%24%2C%20measures%20the%20correlation%20between%20%24X_t%24%20and%20%24X_%7Bt-k%7D%24%20after%20algebraically%20conditioning%20out%20the%20linear%20projection%20onto%20all%20intermediate%20observations%20%24%5C%7BX_%7Bt-1%7D%2C%20X_%7Bt-2%7D%2C%20%5Cdots%2C%20X_%7Bt-k%2B1%7D%5C%7D%24%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20%5Cphi_%7Bkk%7D%20%3D%20%5Coperatorname%7Bcorr%7D%5Cleft(X_t%20-%20%5Chat%7BX%7D_t%2C%20%5C%2C%20X_%7Bt-k%7D%20-%20%5Chat%7BX%7D_%7Bt-k%7D%5Cright)%0A%20%20%20%20%24%24%0A%0A%20%20%20%20where%20%24%5Chat%7BX%7D_t%24%20and%20%24%5Chat%7BX%7D_%7Bt-k%7D%24%20are%20the%20best%20linear%20predictions%20based%20on%20the%20intervening%20variables.%0A%0A%20%20%20%20Equivalently%2C%20%24%5Cphi_%7Bkk%7D%24%20is%20the%20last%20coefficient%20of%20an%20Autoregressive%20model%20of%20order%20%24k%24%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20X_t%20%3D%20%5Cphi_%7Bk1%7D%20X_%7Bt-1%7D%20%2B%20%5Cphi_%7Bk2%7D%20X_%7Bt-2%7D%20%2B%20%5Cdots%20%2B%20%5Cphi_%7Bkk%7D%20X_%7Bt-k%7D%20%2B%20%5Cepsilon_t%0A%20%20%20%20%24%24%0A%0A%20%20%20%20%23%23%23%23%20The%20Durbin-Levinson%20Recursion%0A%20%20%20%20Rather%20than%20inverting%20an%20increasingly%20large%20%24k%20%5Ctimes%20k%24%20Toeplitz%20matrix%20at%20every%20lag%2C%20the%20Durbin-Levinson%20algorithm%20solves%20for%20%24%5Cphi_%7Bkk%7D%24%20recursively%20in%20%24%5Cmathcal%7BO%7D(k%5E2)%24%20operations%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20%5Cphi_%7B11%7D%20%3D%20%5Crho_1%0A%20%20%20%20%24%24%0A%0A%20%20%20%20%24%24%0A%20%20%20%20%5Cphi_%7Bkk%7D%20%3D%20%5Cfrac%7B%5Crho_k%20-%20%5Csum_%7Bj%3D1%7D%5E%7Bk-1%7D%20%5Cphi_%7Bk-1%2C%20j%7D%20%5Crho_%7Bk-j%7D%7D%7B1%20-%20%5Csum_%7Bj%3D1%7D%5E%7Bk-1%7D%20%5Cphi_%7Bk-1%2C%20j%7D%20%5Crho_j%7D%2C%20%5Cquad%20k%20%5Cgeq%202%0A%20%20%20%20%24%24%0A%0A%20%20%20%20%24%24%0A%20%20%20%20%5Cphi_%7Bkj%7D%20%3D%20%5Cphi_%7Bk-1%2C%20j%7D%20-%20%5Cphi_%7Bkk%7D%20%5Cphi_%7Bk-1%2C%20k-j%7D%2C%20%5Cquad%201%20%5Cleq%20j%20%3C%20k%0A%20%20%20%20%24%24%0A%0A%20%20%20%20Under%20a%20white%20noise%20null%20hypothesis%2C%20Quenouille's%20theorem%20shows%20that%20%24%5Cphi_%7Bkk%7D%24%20shares%20the%20same%20asymptotic%20distribution%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20%5Cphi_%7Bkk%7D%20%5Cxrightarrow%7Bd%7D%20%5Cmathcal%7BN%7D%5Cleft(0%2C%20%5C%2C%20%5Cfrac%7B1%7D%7Bn%7D%5Cright)%0A%20%20%20%20%24%24%0A%0A%20%20%20%20---%0A%0A%20%20%20%20%23%23%23%204.%20Canonical%20Identification%20Rules%20(Box%20%26%20Jenkins)%0A%0A%20%20%20%20%7C%20Stochastic%20Process%20%7C%20Theoretical%20ACF%20Signature%20%7C%20Theoretical%20PACF%20Signature%20%7C%20Mathematical%20Formula%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**White%20Noise**%20%7C%20All%20%24r_k%20%3D%200%24%20for%20%24k%20%5Cge%201%24%20%7C%20All%20%24%5Cphi_%7Bkk%7D%20%3D%200%24%20for%20%24k%20%5Cge%201%24%20%7C%20%24X_t%20%3D%20%5Cepsilon_t%24%20%7C%0A%20%20%20%20%7C%20**AR(%24p%24)**%20%7C%20Tails%20off%20(exponential%20or%20dampened%20sinusoidal%20decay)%20%7C%20**Cuts%20off%20sharply%20after%20lag%20%24p%24**%20(%24%5Cphi_%7Bkk%7D%20%3D%200%20%5C%20%5Cforall%20k%20%3E%20p%24)%20%7C%20%24X_t%20%3D%20%5Csum_%7Bj%3D1%7D%5Ep%20%5Cphi_j%20X_%7Bt-j%7D%20%2B%20%5Cepsilon_t%24%20%7C%0A%20%20%20%20%7C%20**MA(%24q%24)**%20%7C%20**Cuts%20off%20sharply%20after%20lag%20%24q%24**%20(%24%5Crho_k%20%3D%200%20%5C%20%5Cforall%20k%20%3E%20q%24)%20%7C%20Tails%20off%20(exponential%20or%20dampened%20sinusoidal%20decay)%20%7C%20%24X_t%20%3D%20%5Cepsilon_t%20%2B%20%5Csum_%7Bm%3D1%7D%5Eq%20%5Ctheta_m%20%5Cepsilon_%7Bt-m%7D%24%20%7C%0A%20%20%20%20%7C%20**ARMA(%24p%2C%20q%24)**%20%7C%20Tails%20off%20after%20lag%20%24q%24%20%7C%20Tails%20off%20after%20lag%20%24p%24%20%7C%20%24X_t%20%3D%20%5Csum%20%5Cphi_j%20X_%7Bt-j%7D%20%2B%20%5Csum%20%5Ctheta_m%20%5Cepsilon_%7Bt-m%7D%20%2B%20%5Cepsilon_t%24%20%7C%0A%0A%20%20%20%20%23%23%23%23%20Closed-Form%20Archetypes%0A%20%20%20%20-%20**AR(1)%20with%20parameter%20%24%5Cphi%24**%3A%0A%20%20%20%20%20%20%24%24%5Crho_k%20%3D%20%5Cphi%5Ek%2C%20%5Cquad%20%5Cphi_%7B11%7D%20%3D%20%5Cphi%2C%20%5Cquad%20%5Cphi_%7Bkk%7D%20%3D%200%20%5C%20%5Cforall%20k%20%5Cgeq%202%24%24%0A%20%20%20%20-%20**MA(1)%20with%20parameter%20%24%5Ctheta%24**%3A%0A%20%20%20%20%20%20%24%24%5Crho_1%20%3D%20%5Cfrac%7B%5Ctheta%7D%7B1%20%2B%20%5Ctheta%5E2%7D%2C%20%5Cquad%20%5Crho_k%20%3D%200%20%5C%20%5Cforall%20k%20%5Cgeq%202%2C%20%5Cquad%20%5Cphi_%7Bkk%7D%20%3D%20-%5Cfrac%7B(-%5Ctheta)%5Ek(1%20-%20%5Ctheta%5E2)%7D%7B1%20-%20%5Ctheta%5E%7B2(k%2B1)%7D%7D%24%24%0A%0A%20%20%20%20---%0A%0A%20%20%20%20%23%23%23%205.%20Ljung-Box%20Portmanteau%20Whiteness%20Test%0A%0A%20%20%20%20To%20formally%20evaluate%20whether%20a%20set%20of%20%24h%24%20autocorrelation%20coefficients%20are%20jointly%20zero%20(testing%20for%20residual%20whiteness)%2C%20the%20**Ljung-Box%20%24Q%24-statistic**%20applies%20a%20finite-sample%20adjustment%20to%20the%20Box-Pierce%20test%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20Q(h)%20%3D%20n(n%20%2B%202)%20%5Csum_%7Bk%3D1%7D%5Eh%20%5Cfrac%7Br_k%5E2%7D%7Bn%20-%20k%7D%20%5Cxrightarrow%7Bd%7D%20%5Cchi%5E2(h)%0A%20%20%20%20%24%24%0A%0A%20%20%20%20If%20%24p%20%3C%200.05%24%2C%20we%20reject%20the%20null%20hypothesis%20of%20independence%2C%20indicating%20significant%20uncaptured%20autocorrelation.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(np)%3A%0A%20%20%20%20%23%20Simulation%20Cell%3A%20AR(1)%20and%20MA(1)%20Synthetic%20Realizations%0A%20%20%20%20np.random.seed(47)%0A%20%20%20%20_n%20%3D%20500%0A%20%20%20%20_burn%20%3D%20200%0A%0A%20%20%20%20_phi%20%3D%200.70%0A%20%20%20%20_theta%20%3D%200.70%0A%0A%20%20%20%20%23%20Simulate%20AR(1)%3A%20X_t%20%3D%20phi%20*%20X_%7Bt-1%7D%20%2B%20eps_t%0A%20%20%20%20_eps_ar%20%3D%20np.random.normal(0.0%2C%201.0%2C%20_n%20%2B%20_burn)%0A%20%20%20%20_x_ar%20%3D%20np.zeros(_n%20%2B%20_burn)%0A%20%20%20%20for%20_t%20in%20range(1%2C%20_n%20%2B%20_burn)%3A%0A%20%20%20%20%20%20%20%20_x_ar%5B_t%5D%20%3D%20_phi%20*%20_x_ar%5B_t%20-%201%5D%20%2B%20_eps_ar%5B_t%5D%0A%20%20%20%20ts_ar1%20%3D%20_x_ar%5B_burn%3A%5D%0A%0A%20%20%20%20%23%20Simulate%20MA(1)%3A%20X_t%20%3D%20eps_t%20%2B%20theta%20*%20eps_%7Bt-1%7D%0A%20%20%20%20_eps_ma%20%3D%20np.random.normal(0.0%2C%201.0%2C%20_n%20%2B%201)%0A%20%20%20%20ts_ma1%20%3D%20_eps_ma%5B1%3A%5D%20%2B%20_theta%20*%20_eps_ma%5B%3A-1%5D%0A%20%20%20%20return%20ts_ar1%2C%20ts_ma1%0A%0A%0A%40app.cell%0Adef%20_(acf%2C%20go%2C%20make_subplots%2C%20np%2C%20pacf%2C%20ts_ar1%2C%20ts_ma1)%3A%0A%20%20%20%20%23%20Interactive%20Visualizations%20Cell%3A%0A%20%20%20%20%23%202x3%20Grid%3A%0A%20%20%20%20%23%20Row%201%3A%20AR(1)%20Time%20Series%2C%20ACF%20(Decay)%2C%20PACF%20(Cutoff%20at%20Lag%201)%0A%20%20%20%20%23%20Row%202%3A%20MA(1)%20Time%20Series%2C%20ACF%20(Cutoff%20at%20Lag%201)%2C%20PACF%20(Decay)%0A%0A%20%20%20%20_fig%20%3D%20make_subplots(%0A%20%20%20%20%20%20%20%20rows%3D2%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%22AR(1)%20Trajectory%20(phi%20%3D%200.70)%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22AR(1)%20ACF%3A%20Exponential%20Decay%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22AR(1)%20PACF%3A%20Sharp%20Cutoff%20at%20Lag%201%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22MA(1)%20Trajectory%20(theta%20%3D%200.70)%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22MA(1)%20ACF%3A%20Sharp%20Cutoff%20at%20Lag%201%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22MA(1)%20PACF%3A%20Geometric%20Decay%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%20%20%20%20vertical_spacing%3D0.18%2C%0A%20%20%20%20)%0A%0A%20%20%20%20_nlags%20%3D%2020%0A%20%20%20%20_n%20%3D%20len(ts_ar1)%0A%20%20%20%20_ci_band%20%3D%201.96%20%2F%20np.sqrt(_n)%0A%20%20%20%20_lags%20%3D%20np.arange(1%2C%20_nlags%20%2B%201)%0A%0A%20%20%20%20%23%20Compute%20ACF%20and%20PACF%0A%20%20%20%20_ar_acf%20%3D%20acf(ts_ar1%2C%20nlags%3D_nlags)%5B1%3A%5D%0A%20%20%20%20_ar_pacf%20%3D%20pacf(ts_ar1%2C%20nlags%3D_nlags%2C%20method%3D%22ywm%22)%5B1%3A%5D%0A%0A%20%20%20%20_ma_acf%20%3D%20acf(ts_ma1%2C%20nlags%3D_nlags)%5B1%3A%5D%0A%20%20%20%20_ma_pacf%20%3D%20pacf(ts_ma1%2C%20nlags%3D_nlags%2C%20method%3D%22ywm%22)%5B1%3A%5D%0A%0A%20%20%20%20%23%20Row%201%3A%20AR(1)%0A%20%20%20%20%23%201.%20Trajectory%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(120)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20y%3Dts_ar1%5B%3A120%5D%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%231f4e79%22%2C%20width%3D1.5)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20name%3D%22AR(1)%20Path%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%3D1%2C%0A%20%20%20%20)%0A%0A%20%20%20%20%23%202.%20AR%20ACF%20(Lollipop)%0A%20%20%20%20for%20_l%2C%20_val%20in%20zip(_lags%2C%20_ar_acf)%3A%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%5B_l%2C%20_l%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20y%3D%5B0%2C%20_val%5D%2C%0A%20%20%20%20%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%20%20%20%20%20line%3Ddict(color%3D%22%231f4e79%22%2C%20width%3D1.5)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20showlegend%3DFalse%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20hoverinfo%3D%22skip%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%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_lags%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20y%3D_ar_acf%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%3D7%2C%20color%3D%22%231f4e79%22)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20name%3D%22AR(1)%20ACF%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%20%20%20%20hovertemplate%3D%22Lag%20%25%7Bx%7D%3A%20ACF%20%3D%20%25%7By%3A.3f%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%3D2%2C%0A%20%20%20%20)%0A%0A%20%20%20%20%23%203.%20AR%20PACF%20(Lollipop)%0A%20%20%20%20for%20_l%2C%20_val%20in%20zip(_lags%2C%20_ar_pacf)%3A%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%5B_l%2C%20_l%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20y%3D%5B0%2C%20_val%5D%2C%0A%20%20%20%20%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%20%20%20%20%20line%3Ddict(color%3D%22%232563eb%22%2C%20width%3D1.5)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20showlegend%3DFalse%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20hoverinfo%3D%22skip%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%3D3%2C%0A%20%20%20%20%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_lags%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20y%3D_ar_pacf%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%3D7%2C%20color%3D%22%232563eb%22)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20name%3D%22AR(1)%20PACF%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%20%20%20%20hovertemplate%3D%22Lag%20%25%7Bx%7D%3A%20PACF%20%3D%20%25%7By%3A.3f%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%3D3%2C%0A%20%20%20%20)%0A%0A%20%20%20%20%23%20Row%202%3A%20MA(1)%0A%20%20%20%20%23%204.%20Trajectory%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(120)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20y%3Dts_ma1%5B%3A120%5D%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%23047857%22%2C%20width%3D1.5)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20name%3D%22MA(1)%20Path%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%3D2%2C%0A%20%20%20%20%20%20%20%20col%3D1%2C%0A%20%20%20%20)%0A%0A%20%20%20%20%23%205.%20MA%20ACF%20(Lollipop)%0A%20%20%20%20for%20_l%2C%20_val%20in%20zip(_lags%2C%20_ma_acf)%3A%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%5B_l%2C%20_l%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20y%3D%5B0%2C%20_val%5D%2C%0A%20%20%20%20%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%20%20%20%20%20line%3Ddict(color%3D%22%23047857%22%2C%20width%3D1.5)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20showlegend%3DFalse%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20hoverinfo%3D%22skip%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%3D2%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%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_lags%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20y%3D_ma_acf%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%3D7%2C%20color%3D%22%23047857%22)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20name%3D%22MA(1)%20ACF%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%20%20%20%20hovertemplate%3D%22Lag%20%25%7Bx%7D%3A%20ACF%20%3D%20%25%7By%3A.3f%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%3D2%2C%0A%20%20%20%20%20%20%20%20col%3D2%2C%0A%20%20%20%20)%0A%0A%20%20%20%20%23%206.%20MA%20PACF%20(Lollipop)%0A%20%20%20%20for%20_l%2C%20_val%20in%20zip(_lags%2C%20_ma_pacf)%3A%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%5B_l%2C%20_l%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20y%3D%5B0%2C%20_val%5D%2C%0A%20%20%20%20%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%20%20%20%20%20line%3Ddict(color%3D%22%23059669%22%2C%20width%3D1.5)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20showlegend%3DFalse%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20hoverinfo%3D%22skip%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%3D2%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20col%3D3%2C%0A%20%20%20%20%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_lags%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20y%3D_ma_pacf%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%3D7%2C%20color%3D%22%23059669%22)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20name%3D%22MA(1)%20PACF%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%20%20%20%20hovertemplate%3D%22Lag%20%25%7Bx%7D%3A%20PACF%20%3D%20%25%7By%3A.3f%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%3D2%2C%0A%20%20%20%20%20%20%20%20col%3D3%2C%0A%20%20%20%20)%0A%0A%20%20%20%20%23%20Add%20Bartlett%20Confidence%20Bands%20to%20all%20ACF%20%26%20PACF%20subplots%0A%20%20%20%20for%20_r%20in%20%5B1%2C%202%5D%3A%0A%20%20%20%20%20%20%20%20for%20_c%20in%20%5B2%2C%203%5D%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20_fig.add_trace(%0A%20%20%20%20%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%20%20%20%20%20x%3D%5B1%2C%20_nlags%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20y%3D%5B_ci_band%2C%20_ci_band%5D%2C%0A%20%20%20%20%20%20%20%20%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%20%20%20%20%20%20%20%20%20line%3Ddict(color%3D%22%23ef4444%22%2C%20dash%3D%22dash%22%2C%20width%3D1.5)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20showlegend%3DFalse%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20hoverinfo%3D%22skip%22%2C%0A%20%20%20%20%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%20%20%20%20%20row%3D_r%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20col%3D_c%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%20_fig.add_trace(%0A%20%20%20%20%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%20%20%20%20%20x%3D%5B1%2C%20_nlags%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20y%3D%5B-_ci_band%2C%20-_ci_band%5D%2C%0A%20%20%20%20%20%20%20%20%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%20%20%20%20%20%20%20%20%20line%3Ddict(color%3D%22%23ef4444%22%2C%20dash%3D%22dash%22%2C%20width%3D1.5)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20showlegend%3DFalse%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20hoverinfo%3D%22skip%22%2C%0A%20%20%20%20%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%20%20%20%20%20row%3D_r%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20col%3D_c%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%20_fig.add_trace(%0A%20%20%20%20%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%20%20%20%20%20x%3D%5B1%2C%20_nlags%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20y%3D%5B0%2C%200%5D%2C%0A%20%20%20%20%20%20%20%20%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%20%20%20%20%20%20%20%20%20line%3Ddict(color%3D%22%239ca3af%22%2C%20width%3D1)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20showlegend%3DFalse%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20hoverinfo%3D%22skip%22%2C%0A%20%20%20%20%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%20%20%20%20%20row%3D_r%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20col%3D_c%2C%0A%20%20%20%20%20%20%20%20%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%3D620%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%22Box-Jenkins%20Identification%3A%20AR(1)%20vs.%20MA(1)%20Diagnostic%20Fingerprints%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%20margin%3Ddict(l%3D45%2C%20r%3D45%2C%20t%3D75%2C%20b%3D50)%2C%0A%20%20%20%20)%0A%0A%20%20%20%20_fig.update_xaxes(title_text%3D%22Time%20Index%20(t)%22%2C%20row%3D1%2C%20col%3D1)%0A%20%20%20%20_fig.update_xaxes(title_text%3D%22Lag%20k%22%2C%20row%3D1%2C%20col%3D2)%0A%20%20%20%20_fig.update_xaxes(title_text%3D%22Lag%20k%22%2C%20row%3D1%2C%20col%3D3)%0A%0A%20%20%20%20_fig.update_xaxes(title_text%3D%22Time%20Index%20(t)%22%2C%20row%3D2%2C%20col%3D1)%0A%20%20%20%20_fig.update_xaxes(title_text%3D%22Lag%20k%22%2C%20row%3D2%2C%20col%3D2)%0A%20%20%20%20_fig.update_xaxes(title_text%3D%22Lag%20k%22%2C%20row%3D2%2C%20col%3D3)%0A%0A%20%20%20%20_fig.update_yaxes(title_text%3D%22X_t%22%2C%20row%3D1%2C%20col%3D1)%0A%20%20%20%20_fig.update_yaxes(title_text%3D%22Autocorrelation%22%2C%20range%3D%5B-0.4%2C%201.0%5D%2C%20row%3D1%2C%20col%3D2)%0A%20%20%20%20_fig.update_yaxes(title_text%3D%22Partial%20Autocorr%22%2C%20range%3D%5B-0.4%2C%201.0%5D%2C%20row%3D1%2C%20col%3D3)%0A%0A%20%20%20%20_fig.update_yaxes(title_text%3D%22X_t%22%2C%20row%3D2%2C%20col%3D1)%0A%20%20%20%20_fig.update_yaxes(title_text%3D%22Autocorrelation%22%2C%20range%3D%5B-0.4%2C%201.0%5D%2C%20row%3D2%2C%20col%3D2)%0A%20%20%20%20_fig.update_yaxes(title_text%3D%22Partial%20Autocorr%22%2C%20range%3D%5B-0.4%2C%201.0%5D%2C%20row%3D2%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%20demonstrations%3A%0A%20%20%20%201.%20**Durbin-Levinson%20Algorithm%20from%20Scratch**%3A%20Computing%20the%20sample%20ACF%20and%20recursively%20deriving%20the%20PACF%20without%20matrix%20inversion%2C%20comparing%20exact%20numerical%20output%20against%20%60statsmodels.tsa.stattools.pacf(method%3D'ywm')%60.%0A%20%20%20%202.%20**Model%20Residual%20Whiteness%20and%20Ljung-Box%20Portmanteau%20Test**%3A%20Evaluating%20an%20AR(1)%20model's%20residuals%20vs.%20an%20unmodelled%20series%20across%20multiple%20lag%20horizons%20to%20verify%20whether%20the%20residual%20sequence%20is%20genuine%20white%20noise.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(np%2C%20pacf%2C%20pd%2C%20ts_ar1)%3A%0A%20%20%20%20%23%20Example%201%3A%20Durbin-Levinson%20Algorithm%20in%20Pure%20NumPy%0A%20%20%20%20_series%20%3D%20ts_ar1%0A%20%20%20%20_n%20%3D%20len(_series)%0A%20%20%20%20_max_lag%20%3D%2010%0A%20%20%20%20_x_centered%20%3D%20_series%20-%20np.mean(_series)%0A%20%20%20%20_var_0%20%3D%20np.sum(_x_centered**2)%0A%0A%20%20%20%20%23%201.%20Sample%20ACF%0A%20%20%20%20_r%20%3D%20np.zeros(_max_lag%20%2B%201)%0A%20%20%20%20_r%5B0%5D%20%3D%201.0%0A%20%20%20%20for%20_k%20in%20range(1%2C%20_max_lag%20%2B%201)%3A%0A%20%20%20%20%20%20%20%20_r%5B_k%5D%20%3D%20np.sum(_x_centered%5B_k%3A%5D%20*%20_x_centered%5B%3A-_k%5D)%20%2F%20_var_0%0A%0A%20%20%20%20%23%202.%20Recursive%20Durbin-Levinson%20for%20PACF%0A%20%20%20%20_phi_matrix%20%3D%20np.zeros((_max_lag%20%2B%201%2C%20_max_lag%20%2B%201))%0A%20%20%20%20_pacf_scratch%20%3D%20np.zeros(_max_lag%20%2B%201)%0A%0A%20%20%20%20_phi_matrix%5B1%2C%201%5D%20%3D%20_r%5B1%5D%0A%20%20%20%20_pacf_scratch%5B1%5D%20%3D%20_r%5B1%5D%0A%0A%20%20%20%20for%20_k%20in%20range(2%2C%20_max_lag%20%2B%201)%3A%0A%20%20%20%20%20%20%20%20_num%20%3D%20_r%5B_k%5D%20-%20np.sum(_phi_matrix%5B_k%20-%201%2C%201%3A_k%5D%20*%20_r%5B1%3A_k%5D%5B%3A%3A-1%5D)%0A%20%20%20%20%20%20%20%20_den%20%3D%201.0%20-%20np.sum(_phi_matrix%5B_k%20-%201%2C%201%3A_k%5D%20*%20_r%5B1%3A_k%5D)%0A%20%20%20%20%20%20%20%20_phi_kk%20%3D%20_num%20%2F%20_den%0A%20%20%20%20%20%20%20%20_phi_matrix%5B_k%2C%20_k%5D%20%3D%20_phi_kk%0A%20%20%20%20%20%20%20%20_pacf_scratch%5B_k%5D%20%3D%20_phi_kk%0A%0A%20%20%20%20%20%20%20%20for%20_j%20in%20range(1%2C%20_k)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20_phi_matrix%5B_k%2C%20_j%5D%20%3D%20_phi_matrix%5B_k%20-%201%2C%20_j%5D%20-%20_phi_kk%20*%20_phi_matrix%5B_k%20-%201%2C%20_k%20-%20_j%5D%0A%0A%20%20%20%20%23%20Reference%20PACF%20from%20statsmodels%0A%20%20%20%20_statsmodels_pacf%20%3D%20pacf(_series%2C%20nlags%3D_max_lag%2C%20method%3D%22ywm%22)%0A%0A%20%20%20%20%23%20Format%20Comparison%20DataFrame%0A%20%20%20%20_comparison_records%20%3D%20%5B%5D%0A%20%20%20%20_ci%20%3D%201.96%20%2F%20np.sqrt(_n)%0A%20%20%20%20for%20_k%20in%20range(1%2C%20_max_lag%20%2B%201)%3A%0A%20%20%20%20%20%20%20%20_comparison_records.append(%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%22Lag%20k%22%3A%20_k%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22Sample%20ACF%20r_k%22%3A%20f%22%7B_r%5B_k%5D%3A.4f%7D%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22PACF%20(Durbin-Levinson%20Scratch)%22%3A%20f%22%7B_pacf_scratch%5B_k%5D%3A.4f%7D%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22PACF%20(Statsmodels%20Yule-Walker)%22%3A%20f%22%7B_statsmodels_pacf%5B_k%5D%3A.4f%7D%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%2295%25%20Bartlett%20CI%20Threshold%22%3A%20f%22%2B%2F-%20%7B_ci%3A.4f%7D%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22Identification%20Role%22%3A%20%22Statistically%20Significant%20Direct%20Lag%22%20if%20np.abs(_pacf_scratch%5B_k%5D)%20%3E%20_ci%20else%20%22Inside%20Null%20Bands%20(Zero%20Direct%20Effect)%22%2C%0A%20%20%20%20%20%20%20%20%7D)%0A%0A%20%20%20%20_df_dl_comparison%20%3D%20pd.DataFrame(_comparison_records)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(acorr_ljungbox%2C%20np%2C%20pd%2C%20ts_ar1)%3A%0A%20%20%20%20%23%20Example%202%3A%20Ljung-Box%20Residual%20Whiteness%20Diagnostic%0A%20%20%20%20%23%20Model%201%3A%20Raw%20Unmodelled%20Series%20(Hypothesis%3A%20Significant%20Autocorrelation%20Remains)%0A%20%20%20%20%23%20Model%202%3A%20Properly%20Filtered%20AR(1)%20Residuals%20e_t%20%3D%20X_t%20-%20phi_hat%20*%20X_%7Bt-1%7D%20(Hypothesis%3A%20White%20Noise)%0A%20%20%20%20_phi_hat%20%3D%20np.sum(ts_ar1%5B1%3A%5D%20*%20ts_ar1%5B%3A-1%5D)%20%2F%20np.sum(ts_ar1%5B%3A-1%5D%20**%202)%0A%20%20%20%20_residuals_ar1%20%3D%20ts_ar1%5B1%3A%5D%20-%20_phi_hat%20*%20ts_ar1%5B%3A-1%5D%0A%0A%20%20%20%20_lb_raw%20%3D%20acorr_ljungbox(ts_ar1%2C%20lags%3D%5B5%2C%2010%2C%2015%2C%2020%5D%2C%20return_df%3DTrue)%0A%20%20%20%20_lb_res%20%3D%20acorr_ljungbox(_residuals_ar1%2C%20lags%3D%5B5%2C%2010%2C%2015%2C%2020%5D%2C%20return_df%3DTrue)%0A%0A%20%20%20%20_diagnostic_rows%20%3D%20%5B%5D%0A%20%20%20%20for%20_lag%20in%20%5B5%2C%2010%2C%2015%2C%2020%5D%3A%0A%20%20%20%20%20%20%20%20_diagnostic_rows.append(%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%22Test%20Lag%20Horizon%20(h)%22%3A%20_lag%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22Raw%20Series%20Q-Stat%22%3A%20f%22%7B_lb_raw.loc%5B_lag%2C%20'lb_stat'%5D%3A.2f%7D%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22Raw%20Series%20p-Value%22%3A%20f%22%7B_lb_raw.loc%5B_lag%2C%20'lb_pvalue'%5D%3A.2e%7D%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22Raw%20Series%20Whiteness%22%3A%20%22Rejected%20(Serial%20Correlation%20Present)%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22AR(1)%20Residual%20Q-Stat%22%3A%20f%22%7B_lb_res.loc%5B_lag%2C%20'lb_stat'%5D%3A.2f%7D%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22AR(1)%20Residual%20p-Value%22%3A%20f%22%7B_lb_res.loc%5B_lag%2C%20'lb_pvalue'%5D%3A.4f%7D%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22Residual%20Whiteness%20Status%22%3A%20%22Confirmed%20White%20Noise%20(p%20%3E%200.05)%22%2C%0A%20%20%20%20%20%20%20%20%7D)%0A%0A%20%20%20%20_df_whiteness%20%3D%20pd.DataFrame(_diagnostic_rows)%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
4386b89721801cc11522a413a8c7f6ee