import%20marimo%0A%0A__generated_with%20%3D%20%220.24.0%22%0Aapp%20%3D%20marimo.App(width%3D%22medium%22)%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%20import%20scipy.stats%20as%20stats%0A%0A%20%20%20%20return%20go%2C%20make_subplots%2C%20mo%2C%20np%2C%20pd%2C%20stats%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%2012%3A%20Multivariate%20Normal%20Distribution%20and%20Covariance%20Geometry%0A%0A%20%20%20%20%26larr%3B%20Previous%20Note%3A%20%5B11%20Empirical%20CDF%5D(11_ecdf.py)%20%7C%20Next%20Note%3A%20%5B13%20Unbiased%20vs%20Consistent%5D(13_unbiased_vs_consistent.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%20The%20Multivariate%20Normal%20(Gaussian)%20distribution%20is%20the%20cornerstone%20of%20modern%20probabilistic%20modeling%2C%20machine%20learning%2C%20and%20statistical%20signal%20processing.%20Whenever%20real-valued%20random%20vectors%20arise%20from%20multiple%20aggregated%20micro-effects%2C%20the%20Multivariate%20Central%20Limit%20Theorem%20dictates%20that%20their%20joint%20distribution%20approaches%20a%20Gaussian.%0A%0A%20%20%20%20Core%20applications%20across%20AI%2C%20statistics%2C%20and%20data%20science%3A%0A%20%20%20%201.%20**The%20Maximum%20Entropy%20Principle**%3A%20Among%20all%20continuous%20multivariate%20distributions%20with%20a%20specified%20mean%20%24%5Cboldsymbol%7B%5Cmu%7D%24%20and%20covariance%20matrix%20%24%5Cboldsymbol%7B%5CSigma%7D%24%2C%20the%20Multivariate%20Normal%20uniquely%20maximizes%20differential%20entropy.%20It%20is%20the%20most%20conservative%20and%20honest%20probability%20model%20when%20only%20first%20and%20second%20moments%20are%20known.%0A%20%20%20%202.%20**Generative%20Modeling%20and%20VAEs**%3A%20Variational%20Autoencoders%20(VAEs)%20and%20Diffusion%20Models%20rely%20on%20isotropic%20Gaussian%20latent%20priors%20%24p(%5Cmathbf%7Bz%7D)%20%3D%20%5Cmathcal%7BN%7D(%5Cmathbf%7B0%7D%2C%20%5Cmathbf%7BI%7D)%24%20and%20Gaussian%20posterior%20approximations%20%24q_%5Cphi(%5Cmathbf%7Bz%7D%7C%5Cmathbf%7Bx%7D)%20%3D%20%5Cmathcal%7BN%7D(%5Cboldsymbol%7B%5Cmu%7D_%5Cphi(%5Cmathbf%7Bx%7D)%2C%20%5Ctext%7Bdiag%7D(%5Cboldsymbol%7B%5Csigma%7D_%5Cphi%5E2(%5Cmathbf%7Bx%7D)))%24%2C%20utilizing%20the%20reparameterization%20trick%20%24%5Cmathbf%7Bz%7D%20%3D%20%5Cboldsymbol%7B%5Cmu%7D%20%2B%20%5Cboldsymbol%7B%5Csigma%7D%20%5Codot%20%5Cboldsymbol%7B%5Cepsilon%7D%24.%0A%20%20%20%203.%20**Clustering%20and%20Classification**%3A%20Gaussian%20Mixture%20Models%20(GMMs)%20model%20complex%20multi-modal%20density%20landscapes%20via%20Expectation-Maximization%20(EM).%20Linear%20Discriminant%20Analysis%20(LDA)%20and%20Quadratic%20Discriminant%20Analysis%20(QDA)%20construct%20Bayes-optimal%20decision%20boundaries%20using%20Gaussian%20assumptions.%0A%20%20%20%204.%20**Kalman%20Filtering%20and%20State%20Estimation**%3A%20Tracking%20algorithms%20in%20robotics%2C%20autonomous%20driving%2C%20and%20aerospace%20represent%20dynamic%20uncertainty%20as%20propagating%20Gaussian%20belief%20states%20through%20linear-Gaussian%20state%20space%20updates.%0A%20%20%20%205.%20**Mahalanobis%20Distance%20and%20Outlier%20Detection**%3A%20In%20high%20dimensions%20with%20correlated%20features%2C%20standard%20Euclidean%20distance%20%24%5C%7C%5Cmathbf%7Bx%7D%20-%20%5Cboldsymbol%7B%5Cmu%7D%5C%7C_2%24%20is%20misleading.%20The%20covariance-weighted%20Mahalanobis%20distance%20accounts%20for%20variance%20and%20correlation%2C%20following%20an%20exact%20Chi-Square%20distribution%20for%20principled%20anomaly%20detection.%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%20Mathematical%20Definition%20of%20the%20Density%20Function%0A%0A%20%20%20%20A%20random%20vector%20%24%5Cmathbf%7BX%7D%20%5Cin%20%5Cmathbb%7BR%7D%5Ed%24%20follows%20a%20Multivariate%20Normal%20distribution%20%24%5Cmathcal%7BN%7D_d(%5Cboldsymbol%7B%5Cmu%7D%2C%20%5Cboldsymbol%7B%5CSigma%7D)%24%20if%20its%20probability%20density%20function%20(PDF)%20is%20given%20by%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20f(%5Cmathbf%7Bx%7D)%20%3D%20%5Cfrac%7B1%7D%7B(2%5Cpi)%5E%7Bd%2F2%7D%20%5Cdet(%5Cboldsymbol%7B%5CSigma%7D)%5E%7B1%2F2%7D%7D%20%5Cexp%5Cleft(-%5Cfrac%7B1%7D%7B2%7D%20(%5Cmathbf%7Bx%7D%20-%20%5Cboldsymbol%7B%5Cmu%7D)%5ET%20%5Cboldsymbol%7B%5CSigma%7D%5E%7B-1%7D%20(%5Cmathbf%7Bx%7D%20-%20%5Cboldsymbol%7B%5Cmu%7D)%5Cright)%0A%20%20%20%20%24%24%0A%0A%20%20%20%20where%3A%0A%20%20%20%20*%20%24%5Cboldsymbol%7B%5Cmu%7D%20%3D%20%5Cmathbb%7BE%7D%5B%5Cmathbf%7BX%7D%5D%20%5Cin%20%5Cmathbb%7BR%7D%5Ed%24%20is%20the%20population%20mean%20vector.%0A%20%20%20%20*%20%24%5Cboldsymbol%7B%5CSigma%7D%20%3D%20%5Cmathbb%7BE%7D%5B(%5Cmathbf%7BX%7D%20-%20%5Cboldsymbol%7B%5Cmu%7D)(%5Cmathbf%7BX%7D%20-%20%5Cboldsymbol%7B%5Cmu%7D)%5ET%5D%20%5Cin%20%5Cmathbb%7BR%7D%5E%7Bd%20%5Ctimes%20d%7D%24%20is%20the%20symmetric%20positive%20definite%20covariance%20matrix%20(%24%5Cboldsymbol%7B%5CSigma%7D%20%3D%20%5Cboldsymbol%7B%5CSigma%7D%5ET%24%2C%20%24%5Cmathbf%7Bv%7D%5ET%20%5Cboldsymbol%7B%5CSigma%7D%20%5Cmathbf%7Bv%7D%20%3E%200%24%20for%20all%20%24%5Cmathbf%7Bv%7D%20%5Cneq%20%5Cmathbf%7B0%7D%24).%0A%20%20%20%20*%20%24%5Cboldsymbol%7B%5CSigma%7D%5E%7B-1%7D%24%20is%20the%20**precision%20matrix**%20(or%20concentration%20matrix)%2C%20whose%20off-diagonal%20entries%20directly%20encode%20conditional%20independence%20structures%20in%20Gaussian%20Graphical%20Models.%0A%0A%20%20%20%20---%0A%0A%20%20%20%20%23%23%23%20The%20Mahalanobis%20Distance%20and%20Chi-Square%20Level%20Sets%0A%0A%20%20%20%20The%20exponent%20of%20the%20Gaussian%20density%20defines%20the%20**squared%20Mahalanobis%20distance**%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20D_M%5E2(%5Cmathbf%7Bx%7D)%20%3D%20(%5Cmathbf%7Bx%7D%20-%20%5Cboldsymbol%7B%5Cmu%7D)%5ET%20%5Cboldsymbol%7B%5CSigma%7D%5E%7B-1%7D%20(%5Cmathbf%7Bx%7D%20-%20%5Cboldsymbol%7B%5Cmu%7D)%0A%20%20%20%20%24%24%0A%0A%20%20%20%20Level%20sets%20of%20constant%20probability%20density%20%24f(%5Cmathbf%7Bx%7D)%20%3D%20c%24%20are%20hyper-ellipsoids%20defined%20by%20%24D_M%5E2(%5Cmathbf%7Bx%7D)%20%3D%20%5Ctext%7Bconstant%7D%24.%0A%0A%20%20%20%20%23%23%23%23%20Distribution%20of%20the%20Mahalanobis%20Distance%0A%20%20%20%20If%20%24%5Cmathbf%7BX%7D%20%5Csim%20%5Cmathcal%7BN%7D_d(%5Cboldsymbol%7B%5Cmu%7D%2C%20%5Cboldsymbol%7B%5CSigma%7D)%24%2C%20the%20squared%20Mahalanobis%20distance%20follows%20an%20exact%20Chi-Square%20distribution%20with%20%24d%24%20degrees%20of%20freedom%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20D_M%5E2(%5Cmathbf%7BX%7D)%20%5Csim%20%5Cchi%5E2(d)%0A%20%20%20%20%24%24%0A%0A%20%20%20%20In%20two%20dimensions%20(%24d%20%3D%202%24)%2C%20the%20cumulative%20distribution%20function%20of%20%24%5Cchi%5E2(2)%24%20is%20exponential%3A%20%24P(D_M%5E2%20%5Cleq%20c)%20%3D%201%20-%20e%5E%7B-c%2F2%7D%24.%0A%20%20%20%20*%20%24c%20%3D%201.0%24%20(%241%5Csigma%24%20contour)%3A%20%241%20-%20e%5E%7B-0.5%7D%20%5Capprox%2039.3%5C%25%24%20coverage.%0A%20%20%20%20*%20%24c%20%3D%204.0%24%20(%242%5Csigma%24%20contour)%3A%20%241%20-%20e%5E%7B-2.0%7D%20%5Capprox%2086.5%5C%25%24%20coverage.%0A%20%20%20%20*%20%24c%20%3D%205.991%24%20(%2495%5C%25%24%20contour)%3A%20%24%5Cchi%5E2_%7B0.05%2C%202%7D%20%5Capprox%205.991%24.%0A%20%20%20%20*%20%24c%20%3D%209.0%24%20(%243%5Csigma%24%20contour)%3A%20%241%20-%20e%5E%7B-4.5%7D%20%5Capprox%2098.9%5C%25%24%20coverage.%0A%0A%20%20%20%20---%0A%0A%20%20%20%20%23%23%23%20Covariance%20Geometry%20and%20Mahalanobis%20Whitening%0A%0A%20%20%20%20By%20the%20Spectral%20Theorem%20(Note%2007)%2C%20the%20real%20symmetric%20positive%20definite%20matrix%20%24%5Cboldsymbol%7B%5CSigma%7D%24%20has%20orthogonal%20eigendecomposition%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20%5Cboldsymbol%7B%5CSigma%7D%20%3D%20%5Cmathbf%7BQ%7D%20%5Cmathbf%7B%5CLambda%7D%20%5Cmathbf%7BQ%7D%5ET%0A%20%20%20%20%24%24%0A%0A%20%20%20%20where%20%24%5Cmathbf%7BQ%7D%20%3D%20%5B%5Cmathbf%7Bq%7D_1%2C%20%5Cdots%2C%20%5Cmathbf%7Bq%7D_d%5D%24%20contains%20orthonormal%20column%20eigenvectors%2C%20and%20%24%5Cmathbf%7B%5CLambda%7D%20%3D%20%5Ctext%7Bdiag%7D(%5Clambda_1%2C%20%5Cdots%2C%20%5Clambda_d)%24%20contains%20strictly%20positive%20eigenvalues%20%24%5Clambda_i%20%3E%200%24.%0A%0A%20%20%20%20*%20**Eigenvectors%20%24%5Cmathbf%7Bq%7D_i%24**%3A%20Point%20along%20the%20principal%20axes%20of%20rotation%20of%20the%20probability%20density%20ellipsoids.%0A%20%20%20%20*%20**Eigenvalues%20%24%5Clambda_i%24**%3A%20Measure%20the%20variance%20along%20the%20%24i%24-th%20principal%20axis.%20The%20half-axis%20lengths%20of%20the%20%24k%24-sigma%20ellipsoid%20are%20given%20by%20%24k%20%5Csqrt%7B%5Clambda_i%7D%24.%0A%0A%20%20%20%20%23%23%23%23%20Mahalanobis%20Whitening%20Transformation%0A%20%20%20%20Given%20correlated%20data%20%24%5Cmathbf%7BX%7D%20%5Csim%20%5Cmathcal%7BN%7D_d(%5Cboldsymbol%7B%5Cmu%7D%2C%20%5Cboldsymbol%7B%5CSigma%7D)%24%2C%20we%20define%20the%20whitening%20matrix%20%24%5Cmathbf%7BW%7D%20%3D%20%5Cmathbf%7B%5CSigma%7D%5E%7B-1%2F2%7D%20%3D%20%5Cmathbf%7BQ%7D%20%5Cmathbf%7B%5CLambda%7D%5E%7B-1%2F2%7D%20%5Cmathbf%7BQ%7D%5ET%24.%20Transforming%20the%20data%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20%5Cmathbf%7BZ%7D%20%3D%20%5Cmathbf%7B%5CSigma%7D%5E%7B-1%2F2%7D%20(%5Cmathbf%7BX%7D%20-%20%5Cboldsymbol%7B%5Cmu%7D)%0A%20%20%20%20%24%24%0A%0A%20%20%20%20Evaluating%20the%20covariance%20of%20%24%5Cmathbf%7BZ%7D%24%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20%5Ctext%7BCov%7D(%5Cmathbf%7BZ%7D)%20%3D%20%5Cmathbf%7B%5CSigma%7D%5E%7B-1%2F2%7D%20%5Cboldsymbol%7B%5CSigma%7D%20%5Cmathbf%7B%5CSigma%7D%5E%7B-1%2F2%7D%20%3D%20%5Cmathbf%7B%5CSigma%7D%5E%7B-1%2F2%7D%20%5Cmathbf%7B%5CSigma%7D%5E%7B1%2F2%7D%20%5Cmathbf%7B%5CSigma%7D%5E%7B1%2F2%7D%20%5Cmathbf%7B%5CSigma%7D%5E%7B-1%2F2%7D%20%3D%20%5Cmathbf%7BI%7D_d%0A%20%20%20%20%24%24%0A%0A%20%20%20%20The%20whitened%20random%20vector%20%24%5Cmathbf%7BZ%7D%24%20follows%20a%20standard%20spherical%20isotropic%20Gaussian%20%24%5Cmathcal%7BN%7D_d(%5Cmathbf%7B0%7D%2C%20%5Cmathbf%7BI%7D_d)%24%2C%20completely%20removing%20all%20feature%20correlations%20and%20equalizing%20variances.%0A%0A%20%20%20%20---%0A%0A%20%20%20%20%23%23%23%20Maximum%20Likelihood%20Estimation%20(MLE)%0A%0A%20%20%20%20Given%20an%20i.i.d.%20sample%20%24%5Cmathbf%7Bx%7D_1%2C%20%5Cdots%2C%20%5Cmathbf%7Bx%7D_N%24%20from%20%24%5Cmathcal%7BN%7D_d(%5Cboldsymbol%7B%5Cmu%7D%2C%20%5Cboldsymbol%7B%5CSigma%7D)%24%2C%20the%20log-likelihood%20function%20is%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20%5Cln%20%5Cmathcal%7BL%7D(%5Cboldsymbol%7B%5Cmu%7D%2C%20%5Cboldsymbol%7B%5CSigma%7D)%20%3D%20-%5Cfrac%7BN%20d%7D%7B2%7D%20%5Cln(2%5Cpi)%20-%20%5Cfrac%7BN%7D%7B2%7D%20%5Cln%20%5Cdet(%5Cboldsymbol%7B%5CSigma%7D)%20-%20%5Cfrac%7B1%7D%7B2%7D%20%5Csum_%7Bi%3D1%7D%5EN%20(%5Cmathbf%7Bx%7D_i%20-%20%5Cboldsymbol%7B%5Cmu%7D)%5ET%20%5Cboldsymbol%7B%5CSigma%7D%5E%7B-1%7D%20(%5Cmathbf%7Bx%7D_i%20-%20%5Cboldsymbol%7B%5Cmu%7D)%0A%20%20%20%20%24%24%0A%0A%20%20%20%20Using%20matrix%20calculus%20(Note%2008)%2C%20maximizing%20with%20respect%20to%20%24%5Cboldsymbol%7B%5Cmu%7D%24%20and%20%24%5Cboldsymbol%7B%5CSigma%7D%24%20yields%20the%20closed-form%20MLE%20estimators%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20%5Chat%7B%5Cboldsymbol%7B%5Cmu%7D%7D_%7B%5Ctext%7BMLE%7D%7D%20%3D%20%5Cfrac%7B1%7D%7BN%7D%20%5Csum_%7Bi%3D1%7D%5EN%20%5Cmathbf%7Bx%7D_i%0A%20%20%20%20%24%24%0A%0A%20%20%20%20%24%24%0A%20%20%20%20%5Chat%7B%5Cboldsymbol%7B%5CSigma%7D%7D_%7B%5Ctext%7BMLE%7D%7D%20%3D%20%5Cfrac%7B1%7D%7BN%7D%20%5Csum_%7Bi%3D1%7D%5EN%20(%5Cmathbf%7Bx%7D_i%20-%20%5Chat%7B%5Cboldsymbol%7B%5Cmu%7D%7D)(%5Cmathbf%7Bx%7D_i%20-%20%5Chat%7B%5Cboldsymbol%7B%5Cmu%7D%7D)%5ET%0A%20%20%20%20%24%24%0A%0A%20%20%20%20While%20%24%5Chat%7B%5Cboldsymbol%7B%5Cmu%7D%7D_%7B%5Ctext%7BMLE%7D%7D%24%20is%20unconditionally%20unbiased%20(%24%5Cmathbb%7BE%7D%5B%5Chat%7B%5Cboldsymbol%7B%5Cmu%7D%7D%5D%20%3D%20%5Cboldsymbol%7B%5Cmu%7D%24)%2C%20the%20covariance%20MLE%20is%20slightly%20biased%3A%20%24%5Cmathbb%7BE%7D%5B%5Chat%7B%5Cboldsymbol%7B%5CSigma%7D%7D_%7B%5Ctext%7BMLE%7D%7D%5D%20%3D%20%5Cfrac%7BN-1%7D%7BN%7D%20%5Cboldsymbol%7B%5CSigma%7D%24.%20The%20unbiased%20sample%20covariance%20divides%20by%20%24N%20-%201%24%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20%5Cmathbf%7BS%7D%20%3D%20%5Cfrac%7B1%7D%7BN%20-%201%7D%20%5Csum_%7Bi%3D1%7D%5EN%20(%5Cmathbf%7Bx%7D_i%20-%20%5Chat%7B%5Cboldsymbol%7B%5Cmu%7D%7D)(%5Cmathbf%7Bx%7D_i%20-%20%5Chat%7B%5Cboldsymbol%7B%5Cmu%7D%7D)%5ET%0A%20%20%20%20%24%24%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%5Bc%5D%20Interactive%20Visualizations%3A%20Covariance%20Geometry%2C%20Density%20Contours%2C%20and%203D%20Surface%0A%0A%20%20%20%20The%20interactive%20subplots%20below%20display%20the%20structural%20geometry%20of%20the%20Bivariate%20Normal%20distribution%3A%0A%20%20%20%20*%20**Left%20Panel**%3A%202D%20level%20contours%20of%20probability%20density%20alongside%20%24N%20%3D%20500%24%20drawn%20samples.%20The%20orthogonal%20principal%20axes%20%24%5Cmathbf%7Bq%7D_1%2C%20%5Cmathbf%7Bq%7D_2%24%20(scaled%20by%20%242%5Csqrt%7B%5Clambda_i%7D%24)%20radiate%20from%20the%20mean%20vector%20%24%5Cboldsymbol%7B%5Cmu%7D%20%3D%20%5B2.0%2C%203.0%5D%5ET%24%2C%20demonstrating%20that%20the%20eigenvectors%20dictate%20the%20orientation%20and%20stretching%20of%20the%20covariance%20ellipse.%0A%20%20%20%20*%20**Right%20Panel**%3A%203D%20probability%20density%20surface%20%24f(x_1%2C%20x_2)%24%2C%20illustrating%20the%20mode%20at%20%24(%5Cboldsymbol%7B%5Cmu%7D%2C%20f(%5Cboldsymbol%7B%5Cmu%7D))%24%20and%20the%20exponential%20drop-off%20dictated%20by%20the%20Mahalanobis%20distance.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(go%2C%20make_subplots%2C%20np%2C%20stats)%3A%0A%20%20%20%20rng_mvn%20%3D%20np.random.default_rng(42)%0A%0A%20%20%20%20%23%20Population%20parameters%0A%20%20%20%20mu_true%20%3D%20np.array(%5B2.0%2C%203.0%5D)%0A%20%20%20%20sigma_true%20%3D%20np.array(%5B%5B3.0%2C%201.8%5D%2C%20%5B1.8%2C%202.0%5D%5D)%0A%0A%20%20%20%20%23%20Spectral%20decomposition%20of%20Sigma%0A%20%20%20%20eigvals%2C%20eigvecs%20%3D%20np.linalg.eigh(sigma_true)%0A%20%20%20%20%23%20Sort%20descending%0A%20%20%20%20sort_indices%20%3D%20np.argsort(eigvals)%5B%3A%3A-1%5D%0A%20%20%20%20eigvals%20%3D%20eigvals%5Bsort_indices%5D%0A%20%20%20%20eigvecs%20%3D%20eigvecs%5B%3A%2C%20sort_indices%5D%0A%0A%20%20%20%20%23%20Sample%20points%0A%20%20%20%20n_pts_vis%20%3D%20500%0A%20%20%20%20sample_points%20%3D%20rng_mvn.multivariate_normal(mu_true%2C%20sigma_true%2C%20size%3Dn_pts_vis)%0A%0A%20%20%20%20%23%202D%20Grid%0A%20%20%20%20x1_axis%20%3D%20np.linspace(-3.0%2C%207.0%2C%2080)%0A%20%20%20%20x2_axis%20%3D%20np.linspace(-2.0%2C%208.0%2C%2080)%0A%20%20%20%20x1_grid%2C%20x2_grid%20%3D%20np.meshgrid(x1_axis%2C%20x2_axis)%0A%20%20%20%20coords_stacked%20%3D%20np.dstack((x1_grid%2C%20x2_grid))%0A%0A%20%20%20%20mvn_distribution%20%3D%20stats.multivariate_normal(mu_true%2C%20sigma_true)%0A%20%20%20%20density_grid%20%3D%20mvn_distribution.pdf(coords_stacked)%0A%0A%20%20%20%20%23%20Principal%20axes%20vectors%20(scaled%20by%202%20*%20sqrt(lambda))%0A%20%20%20%20axis1_vec%20%3D%20eigvecs%5B%3A%2C%200%5D%20*%202.0%20*%20np.sqrt(eigvals%5B0%5D)%0A%20%20%20%20axis2_vec%20%3D%20eigvecs%5B%3A%2C%201%5D%20*%202.0%20*%20np.sqrt(eigvals%5B1%5D)%0A%0A%20%20%20%20fig%20%3D%20make_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%20specs%3D%5B%5B%7B%22type%22%3A%20%22xy%22%7D%2C%20%7B%22type%22%3A%20%22scene%22%7D%5D%5D%2C%0A%20%20%20%20%20%20%20%20subplot_titles%3D%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20%222D%20Density%20Contours%20%26%20Covariance%20Principal%20Axes%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%223D%20Bivariate%20Gaussian%20Density%20Surface%22%2C%0A%20%20%20%20%20%20%20%20%5D%2C%0A%20%20%20%20)%0A%0A%20%20%20%20%23%20Left%3A%20Contours%0A%20%20%20%20fig.add_trace(%0A%20%20%20%20%20%20%20%20go.Contour(%0A%20%20%20%20%20%20%20%20%20%20%20%20z%3Ddensity_grid%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20x%3Dx1_axis%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20y%3Dx2_axis%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20colorscale%3D%22Viridis%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20opacity%3D0.75%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20showscale%3DFalse%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20contours%3Ddict(showlines%3DTrue)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20name%3D%22Density%20Contours%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%20Left%3A%20Sample%20Scatter%0A%20%20%20%20fig.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%3Dsample_points%5B%3A%2C%200%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20y%3Dsample_points%5B%3A%2C%201%5D%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%2364748b%22%2C%20opacity%3D0.45)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20name%3D%22Sampled%20Points%20(N%3D500)%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%20Left%3A%20Mean%20marker%0A%20%20%20%20fig.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%5Bmu_true%5B0%5D%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20y%3D%5Bmu_true%5B1%5D%5D%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%3D12%2C%20color%3D%22%23dc2626%22%2C%20symbol%3D%22cross%22)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20name%3D%22Mean%20%CE%BC%20%3D%20%5B2%2C%203%5D%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%20Left%3A%20Principal%20Axis%201%20(Largest%20Variance)%0A%20%20%20%20fig.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%5Bmu_true%5B0%5D%2C%20mu_true%5B0%5D%20%2B%20axis1_vec%5B0%5D%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20y%3D%5Bmu_true%5B1%5D%2C%20mu_true%5B1%5D%20%2B%20axis1_vec%5B1%5D%5D%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%20line%3Ddict(color%3D%22%23dc2626%22%2C%20width%3D3.5)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20marker%3Ddict(size%3D7%2C%20color%3D%22%23dc2626%22)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20name%3Df%22Principal%20Axis%201%20(2%E2%88%9A%CE%BB%E2%82%81%3D%7B2.0%20*%20np.sqrt(eigvals%5B0%5D)%3A.2f%7D)%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%20Left%3A%20Principal%20Axis%202%20(Smallest%20Variance)%0A%20%20%20%20fig.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%5Bmu_true%5B0%5D%2C%20mu_true%5B0%5D%20%2B%20axis2_vec%5B0%5D%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20y%3D%5Bmu_true%5B1%5D%2C%20mu_true%5B1%5D%20%2B%20axis2_vec%5B1%5D%5D%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%20line%3Ddict(color%3D%22%2316a34a%22%2C%20width%3D3.5)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20marker%3Ddict(size%3D7%2C%20color%3D%22%2316a34a%22)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20name%3Df%22Principal%20Axis%202%20(2%E2%88%9A%CE%BB%E2%82%82%3D%7B2.0%20*%20np.sqrt(eigvals%5B1%5D)%3A.2f%7D)%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%20Right%3A%203D%20Surface%0A%20%20%20%20fig.add_trace(%0A%20%20%20%20%20%20%20%20go.Surface(%0A%20%20%20%20%20%20%20%20%20%20%20%20z%3Ddensity_grid%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20x%3Dx1_axis%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20y%3Dx2_axis%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20colorscale%3D%22Viridis%22%2C%0A%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%20showscale%3DFalse%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20name%3D%22PDF%20Surface%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%20Right%3A%20Peak%20mode%20marker%0A%20%20%20%20fig.add_trace(%0A%20%20%20%20%20%20%20%20go.Scatter3d(%0A%20%20%20%20%20%20%20%20%20%20%20%20x%3D%5Bmu_true%5B0%5D%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20y%3D%5Bmu_true%5B1%5D%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20z%3D%5Bfloat(mvn_distribution.pdf(mu_true))%5D%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%3D8%2C%20color%3D%22%23dc2626%22%2C%20symbol%3D%22diamond%22)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20name%3D%22Peak%20Mode%20f(%CE%BC)%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%20fig.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%3D540%2C%0A%20%20%20%20%20%20%20%20margin%3Ddict(l%3D30%2C%20r%3D30%2C%20t%3D50%2C%20b%3D30)%2C%0A%20%20%20%20%20%20%20%20xaxis%3Ddict(%0A%20%20%20%20%20%20%20%20%20%20%20%20title%3D%22X%E2%82%81%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20range%3D%5B-3.0%2C%207.0%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20zeroline%3DTrue%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20zerolinecolor%3D%22%23cbd5e1%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20gridcolor%3D%22%23f1f5f9%22%2C%0A%20%20%20%20%20%20%20%20)%2C%0A%20%20%20%20%20%20%20%20yaxis%3Ddict(%0A%20%20%20%20%20%20%20%20%20%20%20%20title%3D%22X%E2%82%82%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20range%3D%5B-2.0%2C%208.0%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20zeroline%3DTrue%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20zerolinecolor%3D%22%23cbd5e1%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20gridcolor%3D%22%23f1f5f9%22%2C%0A%20%20%20%20%20%20%20%20)%2C%0A%20%20%20%20%20%20%20%20scene%3Ddict(%0A%20%20%20%20%20%20%20%20%20%20%20%20xaxis%3Ddict(title%3D%22X%E2%82%81%22%2C%20gridcolor%3D%22%23f1f5f9%22)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20yaxis%3Ddict(title%3D%22X%E2%82%82%22%2C%20gridcolor%3D%22%23f1f5f9%22)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20zaxis%3Ddict(title%3D%22Density%20f(X)%22%2C%20gridcolor%3D%22%23f1f5f9%22)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20camera%3Ddict(eye%3Ddict(x%3D-1.5%2C%20y%3D-1.5%2C%20z%3D1.3))%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.22%2C%20xanchor%3D%22center%22%2C%20x%3D0.5)%2C%0A%20%20%20%20)%0A%20%20%20%20return%20(fig%2C)%0A%0A%0A%40app.cell%0Adef%20_(fig%2C%20mo)%3A%0A%20%20%20%20mo.ui.plotly(fig)%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%20%23%23%23%20Example%201%3A%20Maximum%20Likelihood%20Estimation%20and%20Mahalanobis%20Whitening%0A%0A%20%20%20%20In%20this%20example%2C%20we%20generate%20%24N%20%3D%201%2C000%24%20points%20from%20a%20correlated%20bivariate%20normal%20distribution%20%24%5Cmathcal%7BN%7D(%5Cboldsymbol%7B%5Cmu%7D%2C%20%5Cboldsymbol%7B%5CSigma%7D)%24.%20We%3A%0A%20%20%20%201.%20Estimate%20%24%5Chat%7B%5Cboldsymbol%7B%5Cmu%7D%7D_%7B%5Ctext%7BMLE%7D%7D%24%20and%20unbiased%20sample%20covariance%20%24%5Cmathbf%7BS%7D%24.%0A%20%20%20%202.%20Apply%20the%20Mahalanobis%20whitening%20transformation%3A%0A%0A%20%20%20%20%24%24%5Cmathbf%7BZ%7D%20%3D%20%5Cboldsymbol%7B%5CSigma%7D%5E%7B-1%2F2%7D%20(%5Cmathbf%7BX%7D%20-%20%5Cboldsymbol%7B%5Cmu%7D)%24%24%0A%0A%20%20%20%203.%20Verify%20that%20the%20transformed%20covariance%20matrix%20equals%20the%20identity%20matrix%20%24%5Cmathbf%7BI%7D_2%24.%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%20rng_ex1%20%3D%20np.random.default_rng(101)%0A%20%20%20%20sample_size_mle%20%3D%201000%0A%0A%20%20%20%20mu_pop%20%3D%20np.array(%5B2.5%2C%20-1.5%5D)%0A%20%20%20%20sigma_pop%20%3D%20np.array(%5B%5B4.0%2C%202.2%5D%2C%20%5B2.2%2C%203.0%5D%5D)%0A%0A%20%20%20%20%23%20Generate%20samples%0A%20%20%20%20raw_data%20%3D%20rng_ex1.multivariate_normal(mu_pop%2C%20sigma_pop%2C%20size%3Dsample_size_mle)%0A%0A%20%20%20%20%23%20MLE%20estimates%0A%20%20%20%20mu_hat_mle%20%3D%20np.mean(raw_data%2C%20axis%3D0)%0A%20%20%20%20centered_data%20%3D%20raw_data%20-%20mu_hat_mle%0A%20%20%20%20sigma_hat_mle%20%3D%20(centered_data.T%20%40%20centered_data)%20%2F%20sample_size_mle%0A%20%20%20%20sigma_unbiased_s%20%3D%20(centered_data.T%20%40%20centered_data)%20%2F%20(sample_size_mle%20-%201)%0A%0A%20%20%20%20%23%20Mahalanobis%20Whitening%0A%20%20%20%20eigenvalues_pop%2C%20eigenvectors_pop%20%3D%20np.linalg.eigh(sigma_pop)%0A%20%20%20%20whitening_op%20%3D%20eigenvectors_pop%20%40%20np.diag(1.0%20%2F%20np.sqrt(eigenvalues_pop))%20%40%20eigenvectors_pop.T%0A%20%20%20%20whitened_samples%20%3D%20(raw_data%20-%20mu_pop)%20%40%20whitening_op%0A%20%20%20%20cov_whitened_samples%20%3D%20np.cov(whitened_samples%2C%20rowvar%3DFalse)%0A%0A%20%20%20%20max_whitening_error%20%3D%20float(np.max(np.abs(cov_whitened_samples%20-%20np.eye(2))))%0A%0A%20%20%20%20mle_whitening_summary%20%3D%20%7B%0A%20%20%20%20%20%20%20%20%22Metric%20%2F%20Parameter%22%3A%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20%22Mean%20Vector%20%CE%BC%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22Variance%20Var(X%E2%82%81)%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22Variance%20Var(X%E2%82%82)%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22Covariance%20Cov(X%E2%82%81%2C%20X%E2%82%82)%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22Correlation%20Corr(X%E2%82%81%2C%20X%E2%82%82)%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22Whitened%20Covariance%20Max%20%7CCov(Z)%20-%20I%7C%22%2C%0A%20%20%20%20%20%20%20%20%5D%2C%0A%20%20%20%20%20%20%20%20%22True%20Population%20Value%22%3A%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20f%22%5B%7Bmu_pop%5B0%5D%3A.2f%7D%2C%20%7Bmu_pop%5B1%5D%3A.2f%7D%5D%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20f%22%7Bsigma_pop%5B0%2C%200%5D%3A.4f%7D%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20f%22%7Bsigma_pop%5B1%2C%201%5D%3A.4f%7D%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20f%22%7Bsigma_pop%5B0%2C%201%5D%3A.4f%7D%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20f%22%7Bsigma_pop%5B0%2C%201%5D%20%2F%20np.sqrt(sigma_pop%5B0%2C%200%5D%20*%20sigma_pop%5B1%2C%201%5D)%3A.4f%7D%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%220.0000%20(Exact%20Identity)%22%2C%0A%20%20%20%20%20%20%20%20%5D%2C%0A%20%20%20%20%20%20%20%20%22Sample%20Estimate%22%3A%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20f%22%5B%7Bmu_hat_mle%5B0%5D%3A.2f%7D%2C%20%7Bmu_hat_mle%5B1%5D%3A.2f%7D%5D%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20f%22%7Bsigma_unbiased_s%5B0%2C%200%5D%3A.4f%7D%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20f%22%7Bsigma_unbiased_s%5B1%2C%201%5D%3A.4f%7D%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20f%22%7Bsigma_unbiased_s%5B0%2C%201%5D%3A.4f%7D%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20f%22%7Bsigma_unbiased_s%5B0%2C%201%5D%20%2F%20np.sqrt(sigma_unbiased_s%5B0%2C%200%5D%20*%20sigma_unbiased_s%5B1%2C%201%5D)%3A.4f%7D%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20f%22%7Bmax_whitening_error%3A.4f%7D%20(Identity%20restored)%22%2C%0A%20%20%20%20%20%20%20%20%5D%2C%0A%20%20%20%20%7D%0A%20%20%20%20return%20(mle_whitening_summary%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mle_whitening_summary%2C%20mo%2C%20pd)%3A%0A%20%20%20%20df_mle%20%3D%20pd.DataFrame(mle_whitening_summary)%0A%20%20%20%20mo.ui.table(df_mle)%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%23%20Example%202%3A%20Mahalanobis%20Distance%20vs%20Euclidean%20Distance%20for%20Anomaly%20Detection%0A%0A%20%20%20%20In%20correlated%20multivariate%20spaces%2C%20Euclidean%20distance%20%24%5C%7C%5Cmathbf%7Bx%7D%20-%20%5Cboldsymbol%7B%5Cmu%7D%5C%7C_2%24%20produces%20severe%20false%20alarms%20and%20missed%20anomalies%20because%20it%20treats%20all%20directions%20as%20equally%20probable.%0A%0A%20%20%20%20Below%2C%20we%20evaluate%20four%20probe%20points%20against%20%24%5Cmathcal%7BN%7D(%5Cboldsymbol%7B%5Cmu%7D%2C%20%5Cboldsymbol%7B%5CSigma%7D)%24%20with%20%24%5Cboldsymbol%7B%5Cmu%7D%20%3D%20%5B2.0%2C%203.0%5D%5ET%24%20and%20%24%5Cboldsymbol%7B%5CSigma%7D%20%3D%20%5Cbegin%7Bbmatrix%7D%203.0%20%26%201.8%20%5C%5C%201.8%20%26%202.0%20%5Cend%7Bbmatrix%7D%24%3A%0A%20%20%20%20*%20**Probe%20A**%3A%20A%20point%20lying%20along%20the%20principal%20correlation%20axis.%0A%20%20%20%20*%20**Probe%20B**%3A%20A%20point%20lying%20strictly%20orthogonal%20to%20the%20correlation%20axis%20with%20a%20smaller%20Euclidean%20distance.%0A%20%20%20%20*%20**Probe%20C**%3A%20A%20severe%20outlier%20far%20off%20the%20axis.%0A%20%20%20%20*%20**Probe%20D**%3A%20A%20point%20near%20the%20center.%0A%0A%20%20%20%20Using%20the%20Chi-Square%20distribution%20%24D_M%5E2%20%5Csim%20%5Cchi%5E2(2)%24%2C%20we%20compute%20the%20exact%20%24p%24-value%20for%20each%20point%20and%20flag%20anomalies%20at%20%24%5Calpha%20%3D%200.01%24%20(%24D_M%5E2%20%3E%209.21%24).%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(np%2C%20stats)%3A%0A%20%20%20%20mu_ref%20%3D%20np.array(%5B2.0%2C%203.0%5D)%0A%20%20%20%20sigma_ref%20%3D%20np.array(%5B%5B3.0%2C%201.8%5D%2C%20%5B1.8%2C%202.0%5D%5D)%0A%20%20%20%20sigma_inv_ref%20%3D%20np.linalg.inv(sigma_ref)%0A%0A%20%20%20%20probe_points_dict%20%3D%20%7B%0A%20%20%20%20%20%20%20%20%22Probe%20A%20(Along%20Correlation%20Axis)%22%3A%20np.array(%5B5.5%2C%205.8%5D)%2C%0A%20%20%20%20%20%20%20%20%22Probe%20B%20(Orthogonal%20to%20Correlation)%22%3A%20np.array(%5B0.0%2C%205.0%5D)%2C%0A%20%20%20%20%20%20%20%20%22Probe%20C%20(Severe%20Outlier)%22%3A%20np.array(%5B-1.5%2C%207.5%5D)%2C%0A%20%20%20%20%20%20%20%20%22Probe%20D%20(Near%20Mean%20Mode)%22%3A%20np.array(%5B2.2%2C%203.1%5D)%2C%0A%20%20%20%20%7D%0A%0A%20%20%20%20anomaly_evaluation_records%20%3D%20%5B%5D%0A%0A%20%20%20%20for%20label%2C%20probe_coords%20in%20probe_points_dict.items()%3A%0A%20%20%20%20%20%20%20%20diff_vec%20%3D%20probe_coords%20-%20mu_ref%0A%20%20%20%20%20%20%20%20euclidean_dist%20%3D%20float(np.linalg.norm(diff_vec))%0A%20%20%20%20%20%20%20%20mahalanobis_dist_sq%20%3D%20float(diff_vec.T%20%40%20sigma_inv_ref%20%40%20diff_vec)%0A%20%20%20%20%20%20%20%20mahalanobis_dist%20%3D%20float(np.sqrt(mahalanobis_dist_sq))%0A%0A%20%20%20%20%20%20%20%20%23%20p-value%20under%20Chi%5E2(df%3D2)%0A%20%20%20%20%20%20%20%20chi2_pval%20%3D%20float(1.0%20-%20stats.chi2.cdf(mahalanobis_dist_sq%2C%20df%3D2))%0A%20%20%20%20%20%20%20%20is_anomaly%20%3D%20chi2_pval%20%3C%200.01%0A%0A%20%20%20%20%20%20%20%20anomaly_evaluation_records.append(%0A%20%20%20%20%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22Candidate%20Point%22%3A%20label%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22Coordinates%20%5Bx%E2%82%81%2C%20x%E2%82%82%5D%22%3A%20f%22%5B%7Bprobe_coords%5B0%5D%3A.1f%7D%2C%20%7Bprobe_coords%5B1%5D%3A.1f%7D%5D%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22Euclidean%20Distance%22%3A%20f%22%7Beuclidean_dist%3A.2f%7D%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22Mahalanobis%20Distance%20D_M%22%3A%20f%22%7Bmahalanobis_dist%3A.2f%7D%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22Chi-Square%20p-value%22%3A%20f%22%7Bchi2_pval%3A.4f%7D%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22Detection%20(%CE%B1%3D0.01)%22%3A%20%22ANOMALY%20DETECTED%22%20if%20is_anomaly%20else%20%22NORMAL%20IN-BOUNDS%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20return%20(anomaly_evaluation_records%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(anomaly_evaluation_records%2C%20mo%2C%20pd)%3A%0A%20%20%20%20df_anomaly%20%3D%20pd.DataFrame(anomaly_evaluation_records)%0A%20%20%20%20mo.ui.table(df_anomaly)%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
6408816dae28d1e37810daf30cd7cf88