%23%20%2F%2F%2F%20script%0A%23%20requires-python%20%3D%20%22%3E%3D3.11%22%0A%23%20dependencies%20%3D%20%5B%0A%23%20%20%20%20%20%22marimo%3D%3D0.20.4%22%2C%0A%23%20%20%20%20%20%22basanos%22%2C%0A%23%20%20%20%20%20%22numpy%3E%3D2.0.0%22%2C%0A%23%20%20%20%20%20%22polars%3E%3D1.0.0%22%2C%0A%23%20%20%20%20%20%22plotly%3E%3D6.0.0%22%2C%0A%23%20%5D%0A%23%20%5Btool.uv.sources%5D%0A%23%20basanos%20%3D%20%7B%20path%20%3D%20%22..%2F..%2F..%22%2C%20editable%20%3D%20true%20%7D%0A%23%20%2F%2F%2F%0A%0Aimport%20marimo%0A%0A__generated_with%20%3D%20%220.20.4%22%0Aapp%20%3D%20marimo.App(width%3D%22medium%22)%0A%0Awith%20app.setup%3A%0A%20%20%20%20import%20marimo%20as%20mo%0A%20%20%20%20import%20numpy%20as%20np%0A%20%20%20%20import%20plotly.graph_objects%20as%20go%0A%20%20%20%20import%20polars%20as%20pl%0A%20%20%20%20from%20plotly.subplots%20import%20make_subplots%0A%0A%20%20%20%20from%20basanos.math%20import%20BasanosConfig%2C%20BasanosEngine%2C%20SlidingWindowConfig%0A%0A%0A%40app.cell%0Adef%20cell_01()%3A%0A%20%20%20%20%22%22%22Render%20the%20diagnostics%20notebook%20introduction.%22%22%22%0A%20%20%20%20mo.md(%0A%20%20%20%20%20%20%20%20r%22%22%22%0A%20%20%20%20%20%20%20%20%23%20%F0%9F%A9%BA%20Basanos%20%E2%80%94%20Engine%20Diagnostics%0A%0A%20%20%20%20%20%20%20%20**Basanos**%20exposes%20rich%20per-timestamp%20diagnostic%20properties%20that%20help%20you%0A%20%20%20%20%20%20%20%20understand%20*why*%20the%20optimizer%20produced%20a%20particular%20set%20of%20positions%20%E2%80%94%0A%20%20%20%20%20%20%20%20and%20flag%20when%20something%20is%20going%20wrong.%0A%0A%20%20%20%20%20%20%20%20This%20notebook%20demonstrates%20all%20five%20diagnostic%20properties%20of%0A%20%20%20%20%20%20%20%20%3Aclass%3A%60~basanos.math.BasanosEngine%60%20on%20a%20synthetic%20dataset%20that%20contains%0A%20%20%20%20%20%20%20%20deliberately%20engineered%20edge%20cases%3A%0A%0A%20%20%20%20%20%20%20%20%7C%20Property%20%7C%20Description%20%7C%0A%20%20%20%20%20%20%20%20%7C---%7C---%7C%0A%20%20%20%20%20%20%20%20%7C%20%60position_status%60%20%7C%20Per-row%20label%3A%20%60warmup%60%20%2F%20%60zero_signal%60%20%2F%20%60degenerate%60%20%2F%20%60valid%60%20%7C%0A%20%20%20%20%20%20%20%20%7C%20%60condition_number%60%20%7C%20Condition%20number%20%CE%BA%20of%20the%20correlation%20matrix%20%7C%0A%20%20%20%20%20%20%20%20%7C%20%60effective_rank%60%20%7C%20Entropy-based%20effective%20rank%20of%20the%20correlation%20matrix%20%7C%0A%20%20%20%20%20%20%20%20%7C%20%60solver_residual%60%20%7C%20Euclidean%20residual%20%E2%80%96C%C2%B7x%20%E2%88%92%20%CE%BC%E2%80%96%E2%82%82%20of%20the%20linear%20solve%20%7C%0A%20%20%20%20%20%20%20%20%7C%20%60signal_utilisation%60%20%7C%20Fraction%20of%20%CE%BC%E1%B5%A2%20surviving%20the%20correlation%20filter%20per%20asset%20%7C%0A%0A%20%20%20%20%20%20%20%20%23%23%20What%20this%20notebook%20covers%0A%0A%20%20%20%20%20%20%20%201.%20%F0%9F%A7%AA%20**Synthetic%20dataset**%20%E2%80%94%20prices%20and%20signals%20with%20warm-up%2C%20zero-signal%2C%20and%20low-shrinkage%20periods%0A%20%20%20%20%20%20%20%202.%20%F0%9F%93%8A%20**Position-status%20breakdown**%20%E2%80%94%20stacked%20bar%20chart%20showing%20when%20each%20status%20occurs%0A%20%20%20%20%20%20%20%203.%20%F0%9F%93%88%20**Condition%20number%20%26%20effective%20rank**%20%E2%80%94%20time%20series%20with%20a%20configurable%20ill-conditioning%20threshold%0A%20%20%20%20%20%20%20%204.%20%F0%9F%94%A2%20**Solver%20residual**%20%E2%80%94%20residual%20norm%20over%20time%2C%20showing%20when%20the%20linear%20solve%20degrades%0A%20%20%20%20%20%20%20%205.%20%F0%9F%8E%9B%EF%B8%8F%20**Signal%20utilisation**%20%E2%80%94%20per-asset%20attenuation%2Famplification%20from%20correlation%20structure%0A%20%20%20%20%20%20%20%20%22%22%22%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20cell_02()%3A%0A%20%20%20%20%22%22%22Render%20separator.%22%22%22%0A%20%20%20%20mo.md(r%22%22%22---%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20cell_03()%3A%0A%20%20%20%20%22%22%22Introduce%20the%20synthetic%20data%20section.%22%22%22%0A%20%20%20%20mo.md(%0A%20%20%20%20%20%20%20%20r%22%22%22%0A%20%20%20%20%20%20%20%20%23%23%20%F0%9F%A7%AA%20Synthetic%20Dataset%20with%20Edge%20Cases%0A%0A%20%20%20%20%20%20%20%20We%20generate%20**six%20synthetic%20equity-like%20price%20series**%20over%20500%20trading%20days.%0A%20%20%20%20%20%20%20%20The%20dataset%20is%20crafted%20to%20contain%20three%20distinct%20edge%20cases%20that%20exercise%20every%0A%20%20%20%20%20%20%20%20diagnostic%20code%20path%3A%0A%0A%20%20%20%20%20%20%20%20*%20**Early%20EWMA%20period**%20(rows%200%E2%80%93~60)%3A%20the%20EWMA%20correlation%20estimator%20has%20not%0A%20%20%20%20%20%20%20%20%20%20yet%20accumulated%20enough%20history%20so%20the%20correlation%20matrix%20is%20near-zero%20%2F%20NaN.%0A%20%20%20%20%20%20%20%20%20%20With%20low%20shrinkage%20(%CE%BB%20%3D%200.99)%2C%20the%20normalisation%20denominator%20is%20degenerate%2C%0A%20%20%20%20%20%20%20%20%20%20producing%20%60degenerate%60%20rows.%20%20Once%20the%20EWMA%20converges%2C%20rows%20become%20%60valid%60.%0A%20%20%20%20%20%20%20%20%20%20%3E%20**Note%3A**%20%60'warmup'%60%20is%20a%20*Sliding%20Window*-only%20status%20code.%20%20EWMA%20mode%0A%20%20%20%20%20%20%20%20%20%20%3E%20emits%20%60'degenerate'%60%20during%20its%20convergence%20period%2C%20not%20%60'warmup'%60.%0A%20%20%20%20%20%20%20%20*%20**Zero-signal%20period**%20(rows%20100%E2%80%93149)%3A%20the%20expected-return%20vector%20%CE%BC%20is%20forced%0A%20%20%20%20%20%20%20%20%20%20to%20zero%20for%20all%20assets%20%E2%80%94%20the%20optimizer%20short-circuits%20without%20solving%2C%20producing%0A%20%20%20%20%20%20%20%20%20%20%60zero_signal%60%20rows.%0A%20%20%20%20%20%20%20%20*%20**Low-shrinkage%20period**%20(rows%20200%E2%80%93299)%3A%20the%20engine%20is%20configured%20with%20%CE%BB%20%3D%200.99%0A%20%20%20%20%20%20%20%20%20%20(near-zero%20shrinkage)%2C%20which%20can%20drive%20large%20condition%20numbers%20especially%20when%0A%20%20%20%20%20%20%20%20%20%20the%20correlation%20matrix%20is%20near-singular%20%E2%80%94%20diagnostic%20rows%20may%20show%20%60degenerate%60.%0A%0A%20%20%20%20%20%20%20%20Outside%20these%20windows%20the%20engine%20runs%20normally%2C%20producing%20%60valid%60%20positions.%0A%20%20%20%20%20%20%20%20%22%22%22%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20cell_04()%3A%0A%20%20%20%20%22%22%22Generate%20synthetic%20prices%2C%20signals%2C%20and%20config.%22%22%22%0A%20%20%20%20_seed%20%3D%207%20%20%23%20fixed%20seed%20for%20reproducibility%0A%20%20%20%20_rng%20%3D%20np.random.default_rng(_seed)%0A%20%20%20%20_assets%20%3D%20%5B%22AAPL%22%2C%20%22GOOGL%22%2C%20%22MSFT%22%2C%20%22AMZN%22%2C%20%22META%22%2C%20%22NVDA%22%5D%0A%20%20%20%20_n%20%3D%20500%0A%20%20%20%20_start%20%3D%20pl.date(2022%2C%201%2C%201)%0A%20%20%20%20_dates%20%3D%20pl.date_range(_start%2C%20_start%20%2B%20pl.duration(days%3D_n%20-%201)%2C%20interval%3D%221d%22%2C%20eager%3DTrue)%0A%0A%20%20%20%20%23%20Geometric%20Brownian%20motion%20with%20moderate%20correlations%0A%20%20%20%20_drift%20%3D%20%5B0.0003%2C%200.0002%2C%200.0004%2C%200.0001%2C%200.0003%2C%200.0005%5D%0A%20%20%20%20_sigma%20%3D%20%5B0.018%2C%200.022%2C%200.020%2C%200.025%2C%200.030%2C%200.028%5D%0A%20%20%20%20_s0%20%3D%20%5B150.0%2C%202800.0%2C%20300.0%2C%203300.0%2C%20330.0%2C%20500.0%5D%0A%0A%20%20%20%20%23%20Introduce%20mild%20correlation%20via%20a%20common%20factor%0A%20%20%20%20_common%20%3D%20_rng.normal(0%2C%200.01%2C%20_n)%0A%20%20%20%20_prices_cols%3A%20dict%5Bstr%2C%20object%5D%20%3D%20%7B%22date%22%3A%20_dates%7D%0A%20%20%20%20for%20_i%2C%20_a%20in%20enumerate(_assets)%3A%0A%20%20%20%20%20%20%20%20_log_ret%20%3D%20_rng.normal(_drift%5B_i%5D%2C%20_sigma%5B_i%5D%2C%20_n)%20%2B%200.4%20*%20_common%0A%20%20%20%20%20%20%20%20_prices_cols%5B_a%5D%20%3D%20_s0%5B_i%5D%20*%20np.exp(np.cumsum(_log_ret))%0A%0A%20%20%20%20prices%20%3D%20pl.DataFrame(_prices_cols)%0A%0A%20%20%20%20%23%20Momentum%20signal%3A%20tanh%20of%20(MA5%20-%20MA20)%20%2F%20price%2C%20clipped%20to%20%5B-1%2C%201%5D%0A%20%20%20%20_mu_cols%3A%20dict%5Bstr%2C%20object%5D%20%3D%20%7B%22date%22%3A%20_dates%7D%0A%20%20%20%20for%20_a%20in%20_assets%3A%0A%20%20%20%20%20%20%20%20_p%20%3D%20prices%5B_a%5D.to_numpy()%0A%20%20%20%20%20%20%20%20_ma5%20%3D%20np.convolve(_p%2C%20np.ones(5)%20%2F%205%2C%20mode%3D%22full%22)%5B%3A_n%5D%0A%20%20%20%20%20%20%20%20_ma20%20%3D%20np.convolve(_p%2C%20np.ones(20)%20%2F%2020%2C%20mode%3D%22full%22)%5B%3A_n%5D%0A%20%20%20%20%20%20%20%20_raw%20%3D%20np.tanh(50.0%20*%20(_ma5%20-%20_ma20)%20%2F%20_p)%0A%20%20%20%20%20%20%20%20%23%20Zero-signal%20window%3A%20rows%20100-149%0A%20%20%20%20%20%20%20%20_raw%5B100%3A150%5D%20%3D%200.0%0A%20%20%20%20%20%20%20%20_mu_cols%5B_a%5D%20%3D%20_raw%0A%0A%20%20%20%20mu%20%3D%20pl.DataFrame(_mu_cols)%0A%0A%20%20%20%20mo.callout(%0A%20%20%20%20%20%20%20%20mo.md(%0A%20%20%20%20%20%20%20%20%20%20%20%20f%22%22%22%0A%20%20%20%20%20%20%20%20%20%20%20%20**Dataset%20summary**%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20%7C%20%7C%20Value%20%7C%0A%20%20%20%20%20%20%20%20%20%20%20%20%7C---%7C---%7C%0A%20%20%20%20%20%20%20%20%20%20%20%20%7C%20Assets%20%7C%20%7B%22%2C%20%22.join(_assets)%7D%20%7C%0A%20%20%20%20%20%20%20%20%20%20%20%20%7C%20Rows%20%7C%20%7B_n%7D%20%7C%0A%20%20%20%20%20%20%20%20%20%20%20%20%7C%20Date%20range%20%7C%20%7B_dates%5B0%5D%7D%20%E2%86%92%20%7B_dates%5B-1%5D%7D%20%7C%0A%20%20%20%20%20%20%20%20%20%20%20%20%7C%20Zero-signal%20window%20%7C%20rows%20100-149%20(50%20rows)%20%7C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22%22%22%0A%20%20%20%20%20%20%20%20)%2C%0A%20%20%20%20%20%20%20%20kind%3D%22info%22%2C%0A%20%20%20%20)%0A%20%20%20%20return%20mu%2C%20prices%0A%0A%0A%40app.cell%0Adef%20cell_05()%3A%0A%20%20%20%20%22%22%22Introduce%20the%20configuration%20section.%22%22%22%0A%20%20%20%20mo.md(%0A%20%20%20%20%20%20%20%20r%22%22%22%0A%20%20%20%20%20%20%20%20%23%23%20%E2%9A%99%EF%B8%8F%20Engine%20Configuration%0A%0A%20%20%20%20%20%20%20%20We%20use%20the%20default%20EWMA-shrinkage%20mode%20with%20**low%20shrinkage**%20(%60%CE%BB%20%3D%200.99%60)%0A%20%20%20%20%20%20%20%20to%20deliberately%20surface%20ill-conditioning%20in%20the%20diagnostic%20charts.%0A%20%20%20%20%20%20%20%20You%20can%20adjust%20the%20parameters%20below%20using%20the%20sliders.%0A%20%20%20%20%20%20%20%20%22%22%22%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20cell_06()%3A%0A%20%20%20%20%22%22%22Create%20interactive%20sliders%20for%20BasanosConfig%20parameters.%22%22%22%0A%20%20%20%20shrink_slider%20%3D%20mo.ui.slider(%0A%20%20%20%20%20%20%20%20start%3D0.1%2C%0A%20%20%20%20%20%20%20%20stop%3D1.0%2C%0A%20%20%20%20%20%20%20%20step%3D0.05%2C%0A%20%20%20%20%20%20%20%20value%3D0.99%2C%0A%20%20%20%20%20%20%20%20label%3D%22Shrinkage%20retention%20weight%20%CE%BB%20(higher%20%E2%86%92%20less%20regularisation)%22%2C%0A%20%20%20%20)%0A%20%20%20%20corr_slider%20%3D%20mo.ui.slider(%0A%20%20%20%20%20%20%20%20start%3D20%2C%0A%20%20%20%20%20%20%20%20stop%3D120%2C%0A%20%20%20%20%20%20%20%20step%3D10%2C%0A%20%20%20%20%20%20%20%20value%3D64%2C%0A%20%20%20%20%20%20%20%20label%3D%22Correlation%20EWMA%20half-life%20(days)%22%2C%0A%20%20%20%20)%0A%20%20%20%20kappa_threshold%20%3D%20mo.ui.slider(%0A%20%20%20%20%20%20%20%20start%3D50%2C%0A%20%20%20%20%20%20%20%20stop%3D5000%2C%0A%20%20%20%20%20%20%20%20step%3D50%2C%0A%20%20%20%20%20%20%20%20value%3D500%2C%0A%20%20%20%20%20%20%20%20label%3D%22Condition-number%20threshold%20%CE%BA*%20(dashed%20line%20on%20chart)%22%2C%0A%20%20%20%20)%0A%20%20%20%20mo.vstack(%5Bshrink_slider%2C%20corr_slider%2C%20kappa_threshold%5D)%0A%20%20%20%20return%20corr_slider%2C%20kappa_threshold%2C%20shrink_slider%0A%0A%0A%40app.cell%0Adef%20cell_07(corr_slider%2C%20mu%2C%20prices%2C%20shrink_slider)%3A%0A%20%20%20%20%22%22%22Build%20BasanosEngine%20from%20slider%20values%20and%20extract%20all%20diagnostic%20properties.%22%22%22%0A%20%20%20%20cfg%20%3D%20BasanosConfig(%0A%20%20%20%20%20%20%20%20vola%3D16%2C%0A%20%20%20%20%20%20%20%20corr%3Dcorr_slider.value%2C%0A%20%20%20%20%20%20%20%20clip%3D3.5%2C%0A%20%20%20%20%20%20%20%20shrink%3Dshrink_slider.value%2C%0A%20%20%20%20%20%20%20%20aum%3D1_000_000%2C%0A%20%20%20%20)%0A%20%20%20%20engine%20%3D%20BasanosEngine(prices%3Dprices%2C%20mu%3Dmu%2C%20cfg%3Dcfg)%0A%0A%20%20%20%20%23%20Extract%20all%20five%20diagnostic%20properties%0A%20%20%20%20pos_status%20%3D%20engine.position_status%0A%20%20%20%20cond_num%20%3D%20engine.condition_number%0A%20%20%20%20eff_rank%20%3D%20engine.effective_rank%0A%20%20%20%20solv_res%20%3D%20engine.solver_residual%0A%20%20%20%20sig_util%20%3D%20engine.signal_utilisation%0A%0A%20%20%20%20%23%20Count%20status%20codes%20for%20callout%0A%20%20%20%20_counts%20%3D%20pos_status.group_by(%22status%22).len().sort(%22status%22)%0A%20%20%20%20_rows%20%3D%20%5Bf%22%7C%20%60%7Br%5B'status'%5D%7D%60%20%7C%20%7Br%5B'len'%5D%7D%20%7C%22%20for%20r%20in%20_counts.iter_rows(named%3DTrue)%5D%0A%0A%20%20%20%20mo.callout(%0A%20%20%20%20%20%20%20%20mo.md(%0A%20%20%20%20%20%20%20%20%20%20%20%20f%22%22%22%0A%20%20%20%20%20%20%20%20%20%20%20%20**Active%20configuration**%3A%20%CE%BB%20%3D%20%7Bshrink_slider.value%7D%2C%20corr%20half-life%20%3D%20%7Bcorr_slider.value%7D%20days%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20**Position%20status%20breakdown**%20(%7Bprices.height%7D%20rows%20total)%3A%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20%7C%20Status%20%7C%20Count%20%7C%0A%20%20%20%20%20%20%20%20%20%20%20%20%7C---%7C---%7C%0A%20%20%20%20%20%20%20%20%20%20%20%20%7B%22%22.join(_rows)%7D%0A%20%20%20%20%20%20%20%20%20%20%20%20%22%22%22%0A%20%20%20%20%20%20%20%20)%2C%0A%20%20%20%20%20%20%20%20kind%3D%22success%22%2C%0A%20%20%20%20)%0A%20%20%20%20return%20cond_num%2C%20eff_rank%2C%20pos_status%2C%20sig_util%2C%20solv_res%0A%0A%0A%40app.cell%0Adef%20cell_08()%3A%0A%20%20%20%20%22%22%22Render%20separator.%22%22%22%0A%20%20%20%20mo.md(r%22%22%22---%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20cell_09()%3A%0A%20%20%20%20%22%22%22Introduce%20position-status%20chart.%22%22%22%0A%20%20%20%20mo.md(%0A%20%20%20%20%20%20%20%20r%22%22%22%0A%20%20%20%20%20%20%20%20%23%23%20%F0%9F%93%8A%20Position%20Status%20Breakdown%0A%0A%20%20%20%20%20%20%20%20The%20stacked%20bar%20chart%20below%20shows%20how%20the%20four%20status%20codes%20are%0A%20%20%20%20%20%20%20%20distributed%20over%20time.%20Each%20bar%20covers%20a%2010-day%20bucket.%0A%0A%20%20%20%20%20%20%20%20*%20**warmup**%20(blue)%20%E2%80%94%20appears%20at%20the%20start%20when%20the%20EWMA%20estimator%0A%20%20%20%20%20%20%20%20%20%20has%20insufficient%20history.%0A%20%20%20%20%20%20%20%20*%20**zero_signal**%20(orange)%20%E2%80%94%20rows%20100%E2%80%93149%2C%20where%20%CE%BC%20was%20forced%20to%20zero.%0A%20%20%20%20%20%20%20%20*%20**degenerate**%20(red)%20%E2%80%94%20timestamps%20where%20the%20solve%20failed%20numerically.%0A%20%20%20%20%20%20%20%20*%20**valid**%20(green)%20%E2%80%94%20normal%20rows%20where%20positions%20were%20computed.%0A%20%20%20%20%20%20%20%20%22%22%22%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20cell_10(pos_status)%3A%0A%20%20%20%20%22%22%22Render%20the%20position-status%20stacked%20bar%20chart.%22%22%22%0A%20%20%20%20_bucket_size%20%3D%2010%20%20%23%20days%20per%20bar%0A%20%20%20%20_df%20%3D%20pos_status.with_columns((pl.arange(0%2C%20pos_status.height)%20%2F%2F%20_bucket_size).alias(%22bucket%22))%0A%20%20%20%20_buckets%20%3D%20sorted(_df%5B%22bucket%22%5D.unique().to_list())%0A%20%20%20%20_statuses%20%3D%20%5B%22warmup%22%2C%20%22zero_signal%22%2C%20%22degenerate%22%2C%20%22valid%22%5D%0A%20%20%20%20_colors%20%3D%20%7B%0A%20%20%20%20%20%20%20%20%22warmup%22%3A%20%22%233498db%22%2C%0A%20%20%20%20%20%20%20%20%22zero_signal%22%3A%20%22%23e67e22%22%2C%0A%20%20%20%20%20%20%20%20%22degenerate%22%3A%20%22%23e74c3c%22%2C%0A%20%20%20%20%20%20%20%20%22valid%22%3A%20%22%2327ae60%22%2C%0A%20%20%20%20%7D%0A%0A%20%20%20%20%23%20Build%20a%20count%20matrix%3A%20rows%20%3D%20buckets%2C%20columns%20%3D%20statuses%0A%20%20%20%20_counts%3A%20dict%5Bstr%2C%20list%5Bint%5D%5D%20%3D%20%7Bs%3A%20%5B%5D%20for%20s%20in%20_statuses%7D%0A%20%20%20%20_bucket_labels%3A%20list%5Bstr%5D%20%3D%20%5B%5D%0A%20%20%20%20for%20_b%20in%20_buckets%3A%0A%20%20%20%20%20%20%20%20_slice%20%3D%20_df.filter(pl.col(%22bucket%22)%20%3D%3D%20_b)%0A%20%20%20%20%20%20%20%20_start_row%20%3D%20int(_b%20*%20_bucket_size)%0A%20%20%20%20%20%20%20%20_bucket_labels.append(f%22Row%20%7B_start_row%7D%22)%0A%20%20%20%20%20%20%20%20_vc%20%3D%20%7Br%5B0%5D%3A%20r%5B1%5D%20for%20r%20in%20_slice.group_by(%22status%22).len().iter_rows()%7D%0A%20%20%20%20%20%20%20%20for%20_s%20in%20_statuses%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20_counts%5B_s%5D.append(_vc.get(_s%2C%200))%0A%0A%20%20%20%20_fig1%20%3D%20go.Figure()%0A%20%20%20%20for%20_s%20in%20_statuses%3A%0A%20%20%20%20%20%20%20%20_fig1.add_trace(%0A%20%20%20%20%20%20%20%20%20%20%20%20go.Bar(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20x%3D_bucket_labels%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20y%3D_counts%5B_s%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20name%3D_s%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20marker_color%3D_colors%5B_s%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20_fig1.update_layout(%0A%20%20%20%20%20%20%20%20barmode%3D%22stack%22%2C%0A%20%20%20%20%20%20%20%20title%3D%22Position%20Status%20Breakdown%20(10-row%20buckets)%22%2C%0A%20%20%20%20%20%20%20%20xaxis_title%3D%22Bucket%20start%20row%22%2C%0A%20%20%20%20%20%20%20%20yaxis_title%3D%22Number%20of%20rows%22%2C%0A%20%20%20%20%20%20%20%20legend_title%3D%22Status%22%2C%0A%20%20%20%20%20%20%20%20height%3D380%2C%0A%20%20%20%20%20%20%20%20xaxis%3D%7B%22tickangle%22%3A%20-45%7D%2C%0A%20%20%20%20)%0A%20%20%20%20_fig1%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20cell_11()%3A%0A%20%20%20%20%22%22%22Render%20separator.%22%22%22%0A%20%20%20%20mo.md(r%22%22%22---%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20cell_12()%3A%0A%20%20%20%20%22%22%22Introduce%20condition%20number%20and%20effective%20rank%20section.%22%22%22%0A%20%20%20%20mo.md(%0A%20%20%20%20%20%20%20%20r%22%22%22%0A%20%20%20%20%20%20%20%20%23%23%20%F0%9F%93%88%20Condition%20Number%20%26%20Effective%20Rank%0A%0A%20%20%20%20%20%20%20%20*%20**Condition%20number%20%CE%BA**%20%E2%80%94%20the%20ratio%20of%20the%20largest%20to%20smallest%20eigenvalue%20of%0A%20%20%20%20%20%20%20%20%20%20the%20correlation%20matrix.%20%20%CE%BA%20%E2%89%AB%201%20means%20the%20matrix%20is%20nearly%20singular%20and%20the%0A%20%20%20%20%20%20%20%20%20%20linear%20solve%20may%20be%20inaccurate.%20%20A%20threshold%20of%20%CE%BA*%20%3D%20500%20(adjustable%20above)%0A%20%20%20%20%20%20%20%20%20%20is%20a%20common%20rule%20of%20thumb%20for%20flagging%20ill-conditioned%20matrices.%0A%20%20%20%20%20%20%20%20*%20**Effective%20rank**%20%E2%80%94%20an%20entropy-based%20measure%20of%20the%20number%20of%20*effective*%0A%20%20%20%20%20%20%20%20%20%20dimensions%20in%20the%20correlation%20matrix.%20%20A%20value%20close%20to%20the%20number%20of%20assets%0A%20%20%20%20%20%20%20%20%20%20indicates%20a%20well-spread%20spectrum%3B%20a%20value%20of%201%20indicates%20a%20rank-1%20matrix.%0A%0A%20%20%20%20%20%20%20%20With%20%CE%BB%20close%20to%201%20(low%20shrinkage)%2C%20the%20raw%20EWMA%20matrix%20dominates%20and%20condition%0A%20%20%20%20%20%20%20%20numbers%20can%20spike%20dramatically%2C%20especially%20during%20short%20lookback%20windows.%0A%20%20%20%20%20%20%20%20%22%22%22%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20cell_13(cond_num%2C%20eff_rank%2C%20kappa_threshold)%3A%0A%20%20%20%20%22%22%22Render%20the%20condition%20number%20and%20effective%20rank%20charts.%22%22%22%0A%20%20%20%20_fig2%20%3D%20make_subplots(%0A%20%20%20%20%20%20%20%20rows%3D2%2C%0A%20%20%20%20%20%20%20%20cols%3D1%2C%0A%20%20%20%20%20%20%20%20shared_xaxes%3DTrue%2C%0A%20%20%20%20%20%20%20%20subplot_titles%3D(%22Condition%20Number%20%CE%BA%20(log%20scale)%22%2C%20%22Effective%20Rank%22)%2C%0A%20%20%20%20%20%20%20%20vertical_spacing%3D0.12%2C%0A%20%20%20%20)%0A%0A%20%20%20%20%23%20Condition%20number%20(row%201)%0A%20%20%20%20_fig2.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%3Dlist(range(cond_num.height))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20y%3Dcond_num%5B%22condition_number%22%5D.to_list()%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%20name%3D%22%CE%BA%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20line%3D%7B%22color%22%3A%20%22%238e44ad%22%2C%20%22width%22%3A%201.5%7D%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_fig2.add_hline(%0A%20%20%20%20%20%20%20%20y%3Dkappa_threshold.value%2C%0A%20%20%20%20%20%20%20%20line_dash%3D%22dash%22%2C%0A%20%20%20%20%20%20%20%20line_color%3D%22red%22%2C%0A%20%20%20%20%20%20%20%20annotation_text%3Df%22%CE%BA*%20%3D%20%7Bkappa_threshold.value%7D%20(threshold)%22%2C%0A%20%20%20%20%20%20%20%20annotation_position%3D%22top%20left%22%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%20Effective%20rank%20(row%202)%0A%20%20%20%20_fig2.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%3Dlist(range(eff_rank.height))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20y%3Deff_rank%5B%22effective_rank%22%5D.to_list()%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%20name%3D%22Effective%20rank%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20line%3D%7B%22color%22%3A%20%22%232980b9%22%2C%20%22width%22%3A%201.5%7D%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_fig2.update_layout(%0A%20%20%20%20%20%20%20%20title%3D%22Matrix%20Quality%20Diagnostics%22%2C%0A%20%20%20%20%20%20%20%20height%3D550%2C%0A%20%20%20%20%20%20%20%20showlegend%3DTrue%2C%0A%20%20%20%20)%0A%20%20%20%20_fig2.update_yaxes(type%3D%22log%22%2C%20row%3D1%2C%20col%3D1)%0A%20%20%20%20_fig2.update_xaxes(title_text%3D%22Row%20index%22%2C%20row%3D2%2C%20col%3D1)%0A%20%20%20%20_fig2%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20cell_14()%3A%0A%20%20%20%20%22%22%22Render%20separator.%22%22%22%0A%20%20%20%20mo.md(r%22%22%22---%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20cell_15()%3A%0A%20%20%20%20%22%22%22Introduce%20solver%20residual%20section.%22%22%22%0A%20%20%20%20mo.md(%0A%20%20%20%20%20%20%20%20r%22%22%22%0A%20%20%20%20%20%20%20%20%23%23%20%F0%9F%94%A2%20Solver%20Residual%20%E2%80%96C%C2%B7x%20%E2%88%92%20%CE%BC%E2%80%96%E2%82%82%0A%0A%20%20%20%20%20%20%20%20After%20solving%20**C%20%C2%B7%20x%20%3D%20%CE%BC**%20the%20engine%20computes%20the%20Euclidean%20residual%0A%20%20%20%20%20%20%20%20%E2%80%96C%C2%B7x%20%E2%88%92%20%CE%BC%E2%80%96%E2%82%82.%20%20For%20a%20well-conditioned%20system%20on%20modern%20hardware%20this%20value%0A%20%20%20%20%20%20%20%20sits%20near%20machine%20epsilon%20(~10%E2%81%BB%C2%B9%E2%81%B5).%0A%0A%20%20%20%20%20%20%20%20Large%20residuals%20(%3E%2010%E2%81%BB%E2%81%B6)%20indicate%3A%0A%0A%20%20%20%20%20%20%20%20*%20Near-singular%20correlation%20matrices%20(high%20%CE%BA)%2C%20causing%20the%20Cholesky%20solve%0A%20%20%20%20%20%20%20%20%20%20to%20accumulate%20rounding%20errors.%0A%20%20%20%20%20%20%20%20*%20Fallback%20to%20LU%20decomposition%20when%20Cholesky%20fails%2C%20which%20can%20still%20leave%0A%20%20%20%20%20%20%20%20%20%20a%20non-trivial%20residual.%0A%20%20%20%20%20%20%20%20*%20%60NaN%60%20residuals%20appear%20during%20%60warmup%60%20or%20%60degenerate%60%20rows.%0A%0A%20%20%20%20%20%20%20%20The%20residual%20is%20reported%20as%20**zero**%20during%20%60zero_signal%60%20rows%20(no%20solve%20is%0A%20%20%20%20%20%20%20%20performed%20when%20%CE%BC%20%3D%200).%0A%20%20%20%20%20%20%20%20%22%22%22%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20cell_16(solv_res)%3A%0A%20%20%20%20%22%22%22Render%20the%20solver%20residual%20time%20series%20chart.%22%22%22%0A%20%20%20%20_fig3%20%3D%20go.Figure()%0A%20%20%20%20_fig3.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%3Dlist(range(solv_res.height))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20y%3Dsolv_res%5B%22residual%22%5D.to_list()%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%20name%3D%22%E2%80%96C%C2%B7x%20%E2%88%92%20%CE%BC%E2%80%96%E2%82%82%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20line%3D%7B%22color%22%3A%20%22%23c0392b%22%2C%20%22width%22%3A%201.5%7D%2C%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20)%0A%20%20%20%20_fig3.update_layout(%0A%20%20%20%20%20%20%20%20title%3D%22Solver%20Residual%20%E2%80%96C%C2%B7x%20%E2%88%92%20%CE%BC%E2%80%96%E2%82%82%20over%20Time%22%2C%0A%20%20%20%20%20%20%20%20xaxis_title%3D%22Row%20index%22%2C%0A%20%20%20%20%20%20%20%20yaxis_title%3D%22Residual%20norm%20(log%20scale)%22%2C%0A%20%20%20%20%20%20%20%20yaxis_type%3D%22log%22%2C%0A%20%20%20%20%20%20%20%20height%3D380%2C%0A%20%20%20%20)%0A%20%20%20%20_fig3%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20cell_17()%3A%0A%20%20%20%20%22%22%22Render%20separator.%22%22%22%0A%20%20%20%20mo.md(r%22%22%22---%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20cell_18()%3A%0A%20%20%20%20%22%22%22Introduce%20signal%20utilisation%20section.%22%22%22%0A%20%20%20%20mo.md(%0A%20%20%20%20%20%20%20%20r%22%22%22%0A%20%20%20%20%20%20%20%20%23%23%20%F0%9F%8E%9B%EF%B8%8F%20Signal%20Utilisation%20per%20Asset%0A%0A%20%20%20%20%20%20%20%20Signal%20utilisation%20**u%E1%B5%A2**%20measures%20what%20fraction%20of%20the%20raw%20signal%20%CE%BC%E1%B5%A2%0A%20%20%20%20%20%20%20%20survives%20after%20the%20correlation%20filter%20is%20applied%3A%0A%0A%20%20%20%20%20%20%20%20%24%24u_i%20%3D%20%5Cfrac%7B(C%5E%7B-1%7D%5C%2C%5Cmu)_i%7D%7B%5Cmu_i%7D%24%24%0A%0A%20%20%20%20%20%20%20%20*%20**u%E1%B5%A2%20%3D%201**%20%E2%80%94%20asset%20*i*%20is%20uncorrelated%20with%20all%20others%3B%20the%20correlation%0A%20%20%20%20%20%20%20%20%20%20filter%20leaves%20its%20signal%20unchanged.%0A%20%20%20%20%20%20%20%20*%20**u%E1%B5%A2%20%3C%201**%20%E2%80%94%20the%20asset's%20signal%20is%20attenuated%20because%20it%20is%20correlated%0A%20%20%20%20%20%20%20%20%20%20with%20other%20assets%20that%20already%20carry%20similar%20information.%0A%20%20%20%20%20%20%20%20*%20**u%E1%B5%A2%20%3E%201**%20%E2%80%94%20the%20asset's%20signal%20is%20amplified%20because%20it%20is%20*negatively*%0A%20%20%20%20%20%20%20%20%20%20correlated%20with%20others%20(diversification%20benefit).%0A%0A%20%20%20%20%20%20%20%20NaN%20values%20appear%20during%20%60warmup%60%20and%20%60degenerate%60%20rows%2C%20or%20when%20%7C%CE%BC%E1%B5%A2%7C%20is%0A%20%20%20%20%20%20%20%20below%20machine%20precision.%0A%20%20%20%20%20%20%20%20%22%22%22%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20cell_19(sig_util)%3A%0A%20%20%20%20%22%22%22Render%20the%20signal%20utilisation%20chart.%22%22%22%0A%20%20%20%20_assets%20%3D%20%5Bc%20for%20c%20in%20sig_util.columns%20if%20c%20!%3D%20%22date%22%5D%0A%20%20%20%20_palette%20%3D%20%5B%0A%20%20%20%20%20%20%20%20%22%23e74c3c%22%2C%0A%20%20%20%20%20%20%20%20%22%233498db%22%2C%0A%20%20%20%20%20%20%20%20%22%232ecc71%22%2C%0A%20%20%20%20%20%20%20%20%22%23f39c12%22%2C%0A%20%20%20%20%20%20%20%20%22%239b59b6%22%2C%0A%20%20%20%20%20%20%20%20%22%231abc9c%22%2C%0A%20%20%20%20%5D%0A%0A%20%20%20%20_fig4%20%3D%20go.Figure()%0A%20%20%20%20for%20_j%2C%20_a%20in%20enumerate(_assets)%3A%0A%20%20%20%20%20%20%20%20_fig4.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%3Dlist(range(sig_util.height))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20y%3Dsig_util%5B_a%5D.to_list()%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%20name%3D_a%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20line%3D%7B%22color%22%3A%20_palette%5B_j%20%25%20len(_palette)%5D%2C%20%22width%22%3A%201.2%7D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20opacity%3D0.85%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20)%0A%0A%20%20%20%20_fig4.add_hline(%0A%20%20%20%20%20%20%20%20y%3D1.0%2C%0A%20%20%20%20%20%20%20%20line_dash%3D%22dash%22%2C%0A%20%20%20%20%20%20%20%20line_color%3D%22black%22%2C%0A%20%20%20%20%20%20%20%20annotation_text%3D%22u%E1%B5%A2%20%3D%201%20(no%20attenuation)%22%2C%0A%20%20%20%20%20%20%20%20annotation_position%3D%22top%20right%22%2C%0A%20%20%20%20)%0A%20%20%20%20_fig4.update_layout(%0A%20%20%20%20%20%20%20%20title%3D%22Signal%20Utilisation%20u%E1%B5%A2%20per%20Asset%22%2C%0A%20%20%20%20%20%20%20%20xaxis_title%3D%22Row%20index%22%2C%0A%20%20%20%20%20%20%20%20yaxis_title%3D%22Utilisation%20u%E1%B5%A2%22%2C%0A%20%20%20%20%20%20%20%20height%3D420%2C%0A%20%20%20%20%20%20%20%20legend_title%3D%22Asset%22%2C%0A%20%20%20%20)%0A%20%20%20%20_fig4%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20cell_20()%3A%0A%20%20%20%20%22%22%22Render%20separator.%22%22%22%0A%20%20%20%20mo.md(r%22%22%22---%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20cell_sw_01()%3A%0A%20%20%20%20%22%22%22Introduce%20the%20Sliding%20Window%20mode%20section.%22%22%22%0A%20%20%20%20mo.md(%0A%20%20%20%20%20%20%20%20r%22%22%22%0A%20%20%20%20%20%20%20%20%23%23%20%F0%9F%AA%9F%20Sliding%20Window%20Mode%20Diagnostics%0A%0A%20%20%20%20%20%20%20%20The%20same%20five%20diagnostic%20properties%20are%20available%20when%20the%20engine%20is%20configured%0A%20%20%20%20%20%20%20%20with%20%3Aclass%3A%60~basanos.math.SlidingWindowConfig%60%20(%60%60covariance_mode%20%3D%20'sliding_window'%60%60).%0A%20%20%20%20%20%20%20%20This%20mode%20fits%20a%20low-rank%20factor%20model%20over%20a%20rolling%20window%20of%20returns%20instead%20of%0A%20%20%20%20%20%20%20%20maintaining%20an%20EWMA%20correlation%20matrix.%0A%0A%20%20%20%20%20%20%20%20**Key%20behavioural%20difference**%3A%20in%20sliding%20window%20mode%20the%20%60%60'warmup'%60%60%20status%20code%0A%20%20%20%20%20%20%20%20is%20emitted%20explicitly%20for%20the%20first%20%60%60window%20-%201%60%60%20rows%20%E2%80%94%20there%20is%20simply%20not%20enough%0A%20%20%20%20%20%20%20%20history%20to%20fill%20the%20window%20yet.%20%20EWMA%20mode%20never%20emits%20%60%60'warmup'%60%60%3B%20its%20convergence%0A%20%20%20%20%20%20%20%20period%20appears%20as%20%60%60'degenerate'%60%60%20rows%20instead.%0A%0A%20%20%20%20%20%20%20%20Below%20we%20run%20the%20same%20dataset%20through%20a%20sliding-window%20engine%0A%20%20%20%20%20%20%20%20(%60%60window%20%3D%2080%60%60%2C%20%60%60n_factors%20%3D%203%60%60)%20so%20you%20can%20compare%20the%20diagnostic%20output%0A%20%20%20%20%20%20%20%20side-by-side%20with%20the%20EWMA%20results%20above.%0A%20%20%20%20%20%20%20%20%22%22%22%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20cell_sw_02(mu%2C%20prices)%3A%0A%20%20%20%20%22%22%22Build%20a%20sliding-window%20BasanosEngine%20and%20extract%20its%20diagnostic%20properties.%22%22%22%0A%20%20%20%20_sw_cfg%20%3D%20BasanosConfig(%0A%20%20%20%20%20%20%20%20vola%3D16%2C%0A%20%20%20%20%20%20%20%20corr%3D64%2C%0A%20%20%20%20%20%20%20%20clip%3D3.5%2C%0A%20%20%20%20%20%20%20%20shrink%3D0.5%2C%0A%20%20%20%20%20%20%20%20aum%3D1_000_000%2C%0A%20%20%20%20%20%20%20%20covariance_config%3DSlidingWindowConfig(window%3D80%2C%20n_factors%3D3)%2C%0A%20%20%20%20)%0A%20%20%20%20sw_engine%20%3D%20BasanosEngine(prices%3Dprices%2C%20mu%3Dmu%2C%20cfg%3D_sw_cfg)%0A%0A%20%20%20%20sw_pos_status%20%3D%20sw_engine.position_status%0A%20%20%20%20sw_cond_num%20%3D%20sw_engine.condition_number%0A%20%20%20%20sw_eff_rank%20%3D%20sw_engine.effective_rank%0A%20%20%20%20sw_solv_res%20%3D%20sw_engine.solver_residual%0A%20%20%20%20sw_sig_util%20%3D%20sw_engine.signal_utilisation%0A%0A%20%20%20%20_sw_counts%20%3D%20sw_pos_status.group_by(%22status%22).len().sort(%22status%22)%0A%20%20%20%20_sw_rows%20%3D%20%5Bf%22%7C%20%60%7Br%5B'status'%5D%7D%60%20%7C%20%7Br%5B'len'%5D%7D%20%7C%22%20for%20r%20in%20_sw_counts.iter_rows(named%3DTrue)%5D%0A%0A%20%20%20%20mo.callout(%0A%20%20%20%20%20%20%20%20mo.md(%0A%20%20%20%20%20%20%20%20%20%20%20%20f%22%22%22%0A%20%20%20%20%20%20%20%20%20%20%20%20**Sliding%20Window%20config**%3A%20window%20%3D%2080%2C%20n_factors%20%3D%203%2C%20shrink%20%3D%200.5%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20**Position%20status%20breakdown**%20(%7Bprices.height%7D%20rows%20total)%3A%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20%7C%20Status%20%7C%20Count%20%7C%0A%20%20%20%20%20%20%20%20%20%20%20%20%7C---%7C---%7C%0A%20%20%20%20%20%20%20%20%20%20%20%20%7B%22%22.join(_sw_rows)%7D%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20%3E%20The%20first%20**79%20rows**%20carry%20%60'warmup'%60%20status%20%E2%80%94%20the%20window%20cannot%0A%20%20%20%20%20%20%20%20%20%20%20%20%3E%20be%20filled%20until%20row%2080.%20%20Compare%20with%20EWMA%20mode%20above%2C%20where%20the%0A%20%20%20%20%20%20%20%20%20%20%20%20%3E%20early%20rows%20appear%20as%20%60'degenerate'%60%20rather%20than%20%60'warmup'%60.%0A%20%20%20%20%20%20%20%20%20%20%20%20%22%22%22%0A%20%20%20%20%20%20%20%20)%2C%0A%20%20%20%20%20%20%20%20kind%3D%22neutral%22%2C%0A%20%20%20%20)%0A%20%20%20%20return%20sw_cond_num%2C%20sw_pos_status%0A%0A%0A%40app.cell%0Adef%20cell_sw_03(sw_cond_num%2C%20sw_pos_status)%3A%0A%20%20%20%20%22%22%22Render%20SW%20position-status%20and%20condition-number%20charts%20side%20by%20side.%22%22%22%0A%20%20%20%20from%20plotly.subplots%20import%20make_subplots%20as%20_make_subplots%0A%0A%20%20%20%20%23%20%E2%94%80%E2%94%80%20Position-status%20stacked%20bar%20%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%0A%20%20%20%20_bucket_size%20%3D%2010%0A%20%20%20%20_df_sw%20%3D%20sw_pos_status.with_columns((pl.arange(0%2C%20sw_pos_status.height)%20%2F%2F%20_bucket_size).alias(%22bucket%22))%0A%20%20%20%20_buckets_sw%20%3D%20sorted(_df_sw%5B%22bucket%22%5D.unique().to_list())%0A%20%20%20%20_statuses_sw%20%3D%20%5B%22warmup%22%2C%20%22zero_signal%22%2C%20%22degenerate%22%2C%20%22valid%22%5D%0A%20%20%20%20_colors_sw%20%3D%20%7B%0A%20%20%20%20%20%20%20%20%22warmup%22%3A%20%22%233498db%22%2C%0A%20%20%20%20%20%20%20%20%22zero_signal%22%3A%20%22%23e67e22%22%2C%0A%20%20%20%20%20%20%20%20%22degenerate%22%3A%20%22%23e74c3c%22%2C%0A%20%20%20%20%20%20%20%20%22valid%22%3A%20%22%2327ae60%22%2C%0A%20%20%20%20%7D%0A%20%20%20%20_counts_sw%3A%20dict%5Bstr%2C%20list%5Bint%5D%5D%20%3D%20%7Bs%3A%20%5B%5D%20for%20s%20in%20_statuses_sw%7D%0A%20%20%20%20_labels_sw%3A%20list%5Bstr%5D%20%3D%20%5B%5D%0A%20%20%20%20for%20_b%20in%20_buckets_sw%3A%0A%20%20%20%20%20%20%20%20_slice%20%3D%20_df_sw.filter(pl.col(%22bucket%22)%20%3D%3D%20_b)%0A%20%20%20%20%20%20%20%20_start_row%20%3D%20int(_b%20*%20_bucket_size)%0A%20%20%20%20%20%20%20%20_labels_sw.append(f%22Row%20%7B_start_row%7D%22)%0A%20%20%20%20%20%20%20%20_vc%20%3D%20%7Br%5B0%5D%3A%20r%5B1%5D%20for%20r%20in%20_slice.group_by(%22status%22).len().iter_rows()%7D%0A%20%20%20%20%20%20%20%20for%20_s%20in%20_statuses_sw%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20_counts_sw%5B_s%5D.append(_vc.get(_s%2C%200))%0A%0A%20%20%20%20_fig_sw%20%3D%20_make_subplots(%0A%20%20%20%20%20%20%20%20rows%3D1%2C%0A%20%20%20%20%20%20%20%20cols%3D2%2C%0A%20%20%20%20%20%20%20%20subplot_titles%3D(%22Position%20Status%20(SW%2C%2010-row%20buckets)%22%2C%20%22Condition%20Number%20%CE%BA%20(SW%2C%20log)%22)%2C%0A%20%20%20%20%20%20%20%20horizontal_spacing%3D0.12%2C%0A%20%20%20%20)%0A%0A%20%20%20%20for%20_s%20in%20_statuses_sw%3A%0A%20%20%20%20%20%20%20%20_fig_sw.add_trace(%0A%20%20%20%20%20%20%20%20%20%20%20%20go.Bar(x%3D_labels_sw%2C%20y%3D_counts_sw%5B_s%5D%2C%20name%3D_s%2C%20marker_color%3D_colors_sw%5B_s%5D)%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%3D1%2C%0A%20%20%20%20%20%20%20%20)%0A%0A%20%20%20%20%23%20%E2%94%80%E2%94%80%20Condition%20number%20%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%E2%94%80%0A%20%20%20%20_fig_sw.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%3Dlist(range(sw_cond_num.height))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20y%3Dsw_cond_num%5B%22condition_number%22%5D.to_list()%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%20name%3D%22%CE%BA%20(SW)%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20line%3D%7B%22color%22%3A%20%22%238e44ad%22%2C%20%22width%22%3A%201.5%7D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20showlegend%3DTrue%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_fig_sw.update_layout(%0A%20%20%20%20%20%20%20%20barmode%3D%22stack%22%2C%0A%20%20%20%20%20%20%20%20height%3D400%2C%0A%20%20%20%20%20%20%20%20title_text%3D%22Sliding%20Window%20Mode%20Diagnostics%22%2C%0A%20%20%20%20%20%20%20%20legend_title%3D%22Status%20%2F%20Series%22%2C%0A%20%20%20%20)%0A%20%20%20%20_fig_sw.update_yaxes(type%3D%22log%22%2C%20row%3D1%2C%20col%3D2)%0A%20%20%20%20_fig_sw.update_xaxes(tickangle%3D-45%2C%20row%3D1%2C%20col%3D1)%0A%20%20%20%20_fig_sw.update_xaxes(title_text%3D%22Row%20index%22%2C%20row%3D1%2C%20col%3D2)%0A%20%20%20%20_fig_sw%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20cell_21()%3A%0A%20%20%20%20%22%22%22Render%20closing%20remarks.%22%22%22%0A%20%20%20%20mo.md(%0A%20%20%20%20%20%20%20%20r%22%22%22%0A%20%20%20%20%20%20%20%20%23%23%20%F0%9F%92%A1%20Summary%20%26%20Practical%20Guidelines%0A%0A%20%20%20%20%20%20%20%20%7C%20Diagnostic%20%7C%20What%20to%20watch%20for%20%7C%20Action%20%7C%0A%20%20%20%20%20%20%20%20%7C---%7C---%7C---%7C%0A%20%20%20%20%20%20%20%20%7C%20%60position_status%60%20%7C%20%60degenerate%60%20(EWMA)%20or%20%60warmup%60%20(SW)%20persist%20%7C%20Increase%20shrinkage%20%2F%20window%20%7C%0A%20%20%20%20%20%20%20%20%7C%20%60condition_number%60%20%7C%20%CE%BA%20%3E%20500%20in%20sustained%20periods%20%7C%20Reduce%20%CE%BB%20or%20switch%20to%20%60sliding_window%60%20mode%20%7C%0A%20%20%20%20%20%20%20%20%7C%20%60effective_rank%60%20%7C%20Effective%20rank%20%E2%89%AA%20n_assets%20%7C%20Correlation%20is%20concentrated%3B%20few%20factors%20dominate%20%7C%0A%20%20%20%20%20%20%20%20%7C%20%60solver_residual%60%20%7C%20Residuals%20%3E%2010%E2%81%BB%E2%81%B6%20%7C%20Numerical%20concern%20%E2%80%94%20inspect%20%CE%BA%20at%20those%20timestamps%20%7C%0A%20%20%20%20%20%20%20%20%7C%20%60signal_utilisation%60%20%7C%20u%E1%B5%A2%20%3C%200.2%20consistently%20%7C%20Signal%20is%20heavily%20attenuated%3B%20consider%20excluding%20%7C%0A%0A%20%20%20%20%20%20%20%20All%20five%20properties%20are%20available%20on%20every%20%3Aclass%3A%60~basanos.math.BasanosEngine%60%0A%20%20%20%20%20%20%20%20instance%20and%20are%20computed%20lazily%20(each%20property%20runs%20its%20own%20pass%20through%0A%20%20%20%20%20%20%20%20the%20stored%20price%20and%20signal%20data).%0A%0A%20%20%20%20%20%20%20%20Both%20**EWMA-shrink**%20and%20**Sliding%20Window**%20covariance%20modes%20expose%20the%20same%0A%20%20%20%20%20%20%20%20diagnostic%20API%20%E2%80%94%20but%20their%20%60%60position_status%60%60%20codes%20differ%20during%20the%0A%20%20%20%20%20%20%20%20convergence%20%2F%20warmup%20period%3A%0A%0A%20%20%20%20%20%20%20%20*%20**EWMA%20mode**%3A%20early%20rows%20appear%20as%20%60%60'degenerate'%60%60%20(NaN%20matrix%20%E2%86%92%20solve%20fails).%0A%20%20%20%20%20%20%20%20*%20**SW%20mode**%3A%20first%20%60%60window%20-%201%60%60%20rows%20appear%20as%20%60%60'warmup'%60%60%20(explicit%20guard).%0A%20%20%20%20%20%20%20%20%22%22%22%0A%20%20%20%20)%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
707c79f039381f7989ab9ac05810e25f