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%20scipy%20import%20stats%0A%20%20%20%20from%20sklearn.cross_decomposition%20import%20CCA%0A%0A%20%20%20%20return%20CCA%2C%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%2029%3A%20Canonical%20Correlation%20Analysis%2C%20Multimodal%20Alignment%2C%20and%20Generalized%20Eigendecompositions%0A%0A%20%20%20%20%26larr%3B%20Previous%20Note%3A%20%5B28%20Factor%20Analysis%5D(28_factor_analysis.py)%20%7C%20Next%20Note%3A%20%5B30%20Correspondence%20Analysis%5D(30_correspondence_analysis.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%20modern%20data%20science%20and%20deep%20learning%2C%20datasets%20often%20consist%20of%20multiple%20distinct%20views%20or%20modalities%20capturing%20the%20same%20underlying%20phenomenon%20(e.g.%20text%20descriptions%20paired%20with%20images%20in%20CLIP%2C%20audio%20signals%20paired%20with%20video%20frames%2C%20patient%20genomics%20paired%20with%20clinical%20outcomes).%0A%0A%20%20%20%20While%20standard%20multiple%20regression%20models%20a%20single%20scalar%20target%20from%20multiple%20predictors%2C%20and%20PCA%20finds%20directions%20of%20maximum%20variance%20within%20a%20single%20dataset%2C%20**Canonical%20Correlation%20Analysis%20(CCA)**%20solves%20the%20symmetric%20multiview%20alignment%20problem%3A%0A%20%20%20%201.%20**Maximizing%20Cross-Modal%20Linear%20Association**%3A%20Given%20two%20multivariate%20sets%20%24%5Cmathbf%7BX%7D%20%5Cin%20%5Cmathbb%7BR%7D%5E%7Bn%20%5Ctimes%20p%7D%24%20and%20%24%5Cmathbf%7BY%7D%20%5Cin%20%5Cmathbb%7BR%7D%5E%7Bn%20%5Ctimes%20q%7D%24%2C%20CCA%20seeks%20linear%20combinations%20%24%5Cmathbf%7Bu%7D_1%20%3D%20%5Cmathbf%7BX%7D%5Cmathbf%7Ba%7D_1%24%20and%20%24%5Cmathbf%7Bv%7D_1%20%3D%20%5Cmathbf%7BY%7D%5Cmathbf%7Bb%7D_1%24%20that%20achieve%20maximum%20possible%20Pearson%20correlation%20%24%5Crho_1%20%3D%20%5Coperatorname%7Bcorr%7D(%5Cmathbf%7Bu%7D_1%2C%20%5Cmathbf%7Bv%7D_1)%24.%0A%20%20%20%202.%20**Invariance%20to%20Affine%20Coordinate%20Transformations**%3A%20Unlike%20PCA%20(which%20is%20sensitive%20to%20coordinate%20scaling%20and%20rotations)%2C%20canonical%20correlations%20are%20invariant%20to%20any%20non-singular%20linear%20transformations%20of%20either%20%24%5Cmathbf%7BX%7D%24%20or%20%24%5Cmathbf%7BY%7D%24%20(%24%5Cmathbf%7BX%7D%20%5Cto%20%5Cmathbf%7BX%7D%5Cmathbf%7BM%7D_1%2C%20%5Cmathbf%7BY%7D%20%5Cto%20%5Cmathbf%7BY%7D%5Cmathbf%7BM%7D_2%24).%0A%20%20%20%203.%20**Grand%20Unification%20of%20Classical%20Multivariate%20Statistics**%3A%20Harold%20Hotelling%20(1936)%20demonstrated%20that%20ordinary%20linear%20regression%2C%20multivariate%20regression%2C%20MANOVA%2C%20and%20Linear%20Discriminant%20Analysis%20(LDA)%20are%20all%20special%20cases%20of%20CCA.%0A%20%20%20%204.%20**Modern%20Self-Supervised%20and%20Contrastive%20Learning**%3A%20Algorithms%20like%20Deep%20CCA%20(DCCA)%2C%20Barlow%20Twins%2C%20and%20cross-modal%20embedding%20projectors%20directly%20implement%20generalized%20canonical%20correlation%20objectives%20to%20align%20latent%20representations%20across%20neural%20network%20branches.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20---%0A%0A%20%20%20%20%23%23%20%5Bb%5D%20Concept%20explanation%20with%20their%20role%20in%20ML%2FAI%2FStats%3F%0A%0A%20%20%20%20%23%23%23%201.%20The%20Optimization%20Problem%0A%0A%20%20%20%20Let%20%24%5Cmathbf%7BX%7D%20%5Cin%20%5Cmathbb%7BR%7D%5E%7Bn%20%5Ctimes%20p%7D%24%20and%20%24%5Cmathbf%7BY%7D%20%5Cin%20%5Cmathbb%7BR%7D%5E%7Bn%20%5Ctimes%20q%7D%24%20be%20mean-centered%20observation%20matrices.%20The%20sample%20covariance%20blocks%20are%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20%5Cboldsymbol%7B%5CSigma%7D_%7BXX%7D%20%3D%20%5Cfrac%7B1%7D%7Bn%20-%201%7D%20%5Cmathbf%7BX%7D%5E%5Ctop%20%5Cmathbf%7BX%7D%2C%20%5Cquad%20%5Cboldsymbol%7B%5CSigma%7D_%7BYY%7D%20%3D%20%5Cfrac%7B1%7D%7Bn%20-%201%7D%20%5Cmathbf%7BY%7D%5E%5Ctop%20%5Cmathbf%7BY%7D%2C%20%5Cquad%20%5Cboldsymbol%7B%5CSigma%7D_%7BXY%7D%20%3D%20%5Cfrac%7B1%7D%7Bn%20-%201%7D%20%5Cmathbf%7BX%7D%5E%5Ctop%20%5Cmathbf%7BY%7D%0A%20%20%20%20%24%24%0A%0A%20%20%20%20We%20seek%20projection%20vectors%20%24%5Cmathbf%7Ba%7D%20%5Cin%20%5Cmathbb%7BR%7D%5Ep%24%20and%20%24%5Cmathbf%7Bb%7D%20%5Cin%20%5Cmathbb%7BR%7D%5Eq%24%20to%20form%20canonical%20variates%20%24u%20%3D%20%5Cmathbf%7BX%7D%5Cmathbf%7Ba%7D%24%20and%20%24v%20%3D%20%5Cmathbf%7BY%7D%5Cmathbf%7Bb%7D%24%20that%20maximize%20the%20correlation%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20%5Crho%20%3D%20%5Coperatorname%7Bcorr%7D(u%2C%20v)%20%3D%20%5Cfrac%7B%5Cmathbf%7Ba%7D%5E%5Ctop%20%5Cboldsymbol%7B%5CSigma%7D_%7BXY%7D%20%5Cmathbf%7Bb%7D%7D%7B%5Csqrt%7B%5Cmathbf%7Ba%7D%5E%5Ctop%20%5Cboldsymbol%7B%5CSigma%7D_%7BXX%7D%20%5Cmathbf%7Ba%7D%7D%20%5Csqrt%7B%5Cmathbf%7Bb%7D%5E%5Ctop%20%5Cboldsymbol%7B%5CSigma%7D_%7BYY%7D%20%5Cmathbf%7Bb%7D%7D%7D%0A%20%20%20%20%24%24%0A%0A%20%20%20%20Since%20correlation%20is%20scale-invariant%2C%20we%20fix%20the%20variances%20to%20unity%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20%5Cmax_%7B%5Cmathbf%7Ba%7D%2C%20%5Cmathbf%7Bb%7D%7D%20%5Cmathbf%7Ba%7D%5E%5Ctop%20%5Cboldsymbol%7B%5CSigma%7D_%7BXY%7D%20%5Cmathbf%7Bb%7D%20%5Cquad%20%5Ctext%7Bsubject%20to%7D%20%5Cquad%20%5Cmathbf%7Ba%7D%5E%5Ctop%20%5Cboldsymbol%7B%5CSigma%7D_%7BXX%7D%20%5Cmathbf%7Ba%7D%20%3D%201%2C%20%5Cquad%20%5Cmathbf%7Bb%7D%5E%5Ctop%20%5Cboldsymbol%7B%5CSigma%7D_%7BYY%7D%20%5Cmathbf%7Bb%7D%20%3D%201%0A%20%20%20%20%24%24%0A%0A%20%20%20%20---%0A%0A%20%20%20%20%23%23%23%202.%20Lagrangian%20Formulation%20and%20the%20Generalized%20Eigenvalue%20Problem%0A%0A%20%20%20%20Formulating%20the%20Lagrangian%20with%20multipliers%20%24%5Cfrac%7B%5Clambda_1%7D%7B2%7D%24%20and%20%24%5Cfrac%7B%5Clambda_2%7D%7B2%7D%24%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20%5Cmathcal%7BL%7D(%5Cmathbf%7Ba%7D%2C%20%5Cmathbf%7Bb%7D%2C%20%5Clambda_1%2C%20%5Clambda_2)%20%3D%20%5Cmathbf%7Ba%7D%5E%5Ctop%20%5Cboldsymbol%7B%5CSigma%7D_%7BXY%7D%20%5Cmathbf%7Bb%7D%20-%20%5Cfrac%7B%5Clambda_1%7D%7B2%7D(%5Cmathbf%7Ba%7D%5E%5Ctop%20%5Cboldsymbol%7B%5CSigma%7D_%7BXX%7D%20%5Cmathbf%7Ba%7D%20-%201)%20-%20%5Cfrac%7B%5Clambda_2%7D%7B2%7D(%5Cmathbf%7Bb%7D%5E%5Ctop%20%5Cboldsymbol%7B%5CSigma%7D_%7BYY%7D%20%5Cmathbf%7Bb%7D%20-%201)%0A%20%20%20%20%24%24%0A%0A%20%20%20%20Computing%20partial%20derivatives%20and%20setting%20them%20to%20zero%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20%5Cnabla_%7B%5Cmathbf%7Ba%7D%7D%20%5Cmathcal%7BL%7D%20%3D%20%5Cboldsymbol%7B%5CSigma%7D_%7BXY%7D%20%5Cmathbf%7Bb%7D%20-%20%5Clambda_1%20%5Cboldsymbol%7B%5CSigma%7D_%7BXX%7D%20%5Cmathbf%7Ba%7D%20%3D%20%5Cmathbf%7B0%7D%20%5Cimplies%20%5Cboldsymbol%7B%5CSigma%7D_%7BXY%7D%20%5Cmathbf%7Bb%7D%20%3D%20%5Clambda_1%20%5Cboldsymbol%7B%5CSigma%7D_%7BXX%7D%20%5Cmathbf%7Ba%7D%0A%20%20%20%20%24%24%0A%0A%20%20%20%20%24%24%0A%20%20%20%20%5Cnabla_%7B%5Cmathbf%7Bb%7D%7D%20%5Cmathcal%7BL%7D%20%3D%20%5Cboldsymbol%7B%5CSigma%7D_%7BYX%7D%20%5Cmathbf%7Ba%7D%20-%20%5Clambda_2%20%5Cboldsymbol%7B%5CSigma%7D_%7BYY%7D%20%5Cmathbf%7Bb%7D%20%3D%20%5Cmathbf%7B0%7D%20%5Cimplies%20%5Cboldsymbol%7B%5CSigma%7D_%7BYX%7D%20%5Cmathbf%7Ba%7D%20%3D%20%5Clambda_2%20%5Cboldsymbol%7B%5CSigma%7D_%7BYY%7D%20%5Cmathbf%7Bb%7D%0A%20%20%20%20%24%24%0A%0A%20%20%20%20Multiplying%20the%20first%20equation%20by%20%24%5Cmathbf%7Ba%7D%5E%5Ctop%24%20and%20the%20second%20by%20%24%5Cmathbf%7Bb%7D%5E%5Ctop%24%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20%5Clambda_1%20%3D%20%5Cmathbf%7Ba%7D%5E%5Ctop%20%5Cboldsymbol%7B%5CSigma%7D_%7BXY%7D%20%5Cmathbf%7Bb%7D%20%3D%20%5Clambda_2%20%3D%20%5Crho%0A%20%20%20%20%24%24%0A%0A%20%20%20%20Solving%20for%20%24%5Cmathbf%7Bb%7D%20%3D%20%5Cfrac%7B1%7D%7B%5Crho%7D%20%5Cboldsymbol%7B%5CSigma%7D_%7BYY%7D%5E%7B-1%7D%20%5Cboldsymbol%7B%5CSigma%7D_%7BYX%7D%20%5Cmathbf%7Ba%7D%24%20and%20substituting%20into%20the%20first%20equation%20yields%20the%20decoupled%20generalized%20eigenvalue%20problem%20for%20%24%5Cmathbf%7Ba%7D%24%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20%5Cleft(%5Cboldsymbol%7B%5CSigma%7D_%7BXX%7D%5E%7B-1%7D%20%5Cboldsymbol%7B%5CSigma%7D_%7BXY%7D%20%5Cboldsymbol%7B%5CSigma%7D_%7BYY%7D%5E%7B-1%7D%20%5Cboldsymbol%7B%5CSigma%7D_%7BYX%7D%5Cright)%20%5Cmathbf%7Ba%7D%20%3D%20%5Crho%5E2%20%5Cmathbf%7Ba%7D%0A%20%20%20%20%24%24%0A%0A%20%20%20%20Symmetrically%2C%20solving%20for%20%24%5Cmathbf%7Bb%7D%24%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20%5Cleft(%5Cboldsymbol%7B%5CSigma%7D_%7BYY%7D%5E%7B-1%7D%20%5Cboldsymbol%7B%5CSigma%7D_%7BYX%7D%20%5Cboldsymbol%7B%5CSigma%7D_%7BXX%7D%5E%7B-1%7D%20%5Cboldsymbol%7B%5CSigma%7D_%7BXY%7D%5Cright)%20%5Cmathbf%7Bb%7D%20%3D%20%5Crho%5E2%20%5Cmathbf%7Bb%7D%0A%20%20%20%20%24%24%0A%0A%20%20%20%20The%20squared%20canonical%20correlations%20%24%5Crho_1%5E2%20%5Cgeq%20%5Crho_2%5E2%20%5Cgeq%20%5Cdots%20%5Cgeq%20%5Crho_m%5E2%24%20(%24m%20%3D%20%5Cmin(p%2C%20q)%24)%20are%20the%20eigenvalues%20of%20these%20operator%20matrices.%0A%0A%20%20%20%20---%0A%0A%20%20%20%20%23%23%23%203.%20SVD%20of%20the%20Normalized%20Coherence%20Matrix%0A%0A%20%20%20%20In%20practice%2C%20to%20ensure%20numerical%20stability%20and%20avoid%20explicit%20inversion%20of%20covariance%20matrices%2C%20CCA%20is%20solved%20via%20the%20Singular%20Value%20Decomposition%20of%20the%20**coherence%20matrix**%20%24%5Cmathbf%7BK%7D%24%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20%5Cmathbf%7BK%7D%20%3D%20%5Cboldsymbol%7B%5CSigma%7D_%7BXX%7D%5E%7B-1%2F2%7D%20%5Cboldsymbol%7B%5CSigma%7D_%7BXY%7D%20%5Cboldsymbol%7B%5CSigma%7D_%7BYY%7D%5E%7B-1%2F2%7D%0A%20%20%20%20%24%24%0A%0A%20%20%20%20Computing%20the%20thin%20SVD%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20%5Cmathbf%7BK%7D%20%3D%20%5Cmathbf%7BU%7D%20%5Cmathbf%7BD%7D%20%5Cmathbf%7BV%7D%5E%5Ctop%0A%20%20%20%20%24%24%0A%0A%20%20%20%20The%20canonical%20correlations%20are%20the%20diagonal%20entries%20%24%5Crho_k%20%3D%20D_%7Bkk%7D%24%2C%20and%20the%20canonical%20weight%20vectors%20are%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20%5Cmathbf%7Ba%7D_k%20%3D%20%5Cboldsymbol%7B%5CSigma%7D_%7BXX%7D%5E%7B-1%2F2%7D%20%5Cmathbf%7Bu%7D_k%2C%20%5Cquad%20%5Cmathbf%7Bb%7D_k%20%3D%20%5Cboldsymbol%7B%5CSigma%7D_%7BYY%7D%5E%7B-1%2F2%7D%20%5Cmathbf%7Bv%7D_k%0A%20%20%20%20%24%24%0A%0A%20%20%20%20---%0A%0A%20%20%20%20%23%23%23%204.%20Hypothesis%20Testing%3A%20Wilks'%20Lambda%20(%24%5CLambda%5E*%24)%0A%0A%20%20%20%20To%20test%20the%20global%20null%20hypothesis%20that%20%24%5Cmathbf%7BX%7D%24%20and%20%24%5Cmathbf%7BY%7D%24%20are%20completely%20independent%20(%24H_0%3A%20%5Cboldsymbol%7B%5CSigma%7D_%7BXY%7D%20%3D%20%5Cmathbf%7B0%7D%20%5Ciff%20%5Crho_1%20%3D%20%5Crho_2%20%3D%20%5Cdots%20%3D%20%5Crho_m%20%3D%200%24)%2C%20**Wilks'%20Lambda**%20computes%20the%20product%20of%20unexplained%20variances%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20%5CLambda%5E*%20%3D%20%5Cprod_%7Bk%3D1%7D%5Em%20%5Cleft(1%20-%20%5Crho_k%5E2%5Cright)%0A%20%20%20%20%24%24%0A%0A%20%20%20%20Bartlett's%20asymptotic%20Chi-Square%20approximation%20is%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20%5Cchi%5E2%20%3D%20-%5Cleft(n%20-%201%20-%20%5Cfrac%7Bp%20%2B%20q%20%2B%201%7D%7B2%7D%5Cright)%20%5Cln%20%5CLambda%5E*%20%5Csim%20%5Cchi%5E2(p%20%5Ccdot%20q)%0A%20%20%20%20%24%24%0A%0A%20%20%20%20If%20%24p%5Ctext%7B-value%7D%20%3C%200.05%24%2C%20we%20reject%20%24H_0%24%20and%20conclude%20that%20significant%20cross-modal%20linear%20association%20exists.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(np%2C%20pd)%3A%0A%20%20%20%20%23%20Simulation%20Data%3A%20Multimodal%20Biometric%20%26%20Physical%20Performance%20Dataset%0A%20%20%20%20%23%20Set%20X%20(Physiological%20Variables)%3A%20Weight%2C%20Waist%2C%20Pulse%20(p%20%3D%203)%0A%20%20%20%20%23%20Set%20Y%20(Athletic%20Performance)%3A%20Pullups%2C%20Situps%2C%20Jumps%20(q%20%3D%203)%0A%20%20%20%20np.random.seed(42)%0A%20%20%20%20_n%20%3D%20150%0A%0A%20%20%20%20%23%20Common%20latent%20fitness%20factor%20%2B%20latent%20body%20scale%20factor%0A%20%20%20%20_latent_fitness%20%3D%20np.random.normal(0.0%2C%201.0%2C%20_n)%0A%20%20%20%20_latent_mass%20%3D%20np.random.normal(0.0%2C%201.0%2C%20_n)%0A%0A%20%20%20%20%23%20Set%20X%3A%20Physical%20Attributes%0A%20%20%20%20_weight%20%3D%2075.0%20%2B%2010.0%20*%20_latent_mass%20-%202.0%20*%20_latent_fitness%20%2B%20np.random.normal(0.0%2C%202.0%2C%20_n)%0A%20%20%20%20_waist%20%3D%2085.0%20%2B%208.0%20*%20_latent_mass%20-%203.0%20*%20_latent_fitness%20%2B%20np.random.normal(0.0%2C%201.5%2C%20_n)%0A%20%20%20%20_pulse%20%3D%2070.0%20%2B%202.0%20*%20_latent_mass%20-%206.0%20*%20_latent_fitness%20%2B%20np.random.normal(0.0%2C%203.0%2C%20_n)%0A%0A%20%20%20%20%23%20Set%20Y%3A%20Athletic%20Tests%0A%20%20%20%20_pullups%20%3D%2010.0%20-%203.0%20*%20_latent_mass%20%2B%205.0%20*%20_latent_fitness%20%2B%20np.random.normal(0.0%2C%201.5%2C%20_n)%0A%20%20%20%20_situps%20%3D%20180.0%20-%2015.0%20*%20_latent_mass%20%2B%2025.0%20*%20_latent_fitness%20%2B%20np.random.normal(0.0%2C%208.0%2C%20_n)%0A%20%20%20%20_jumps%20%3D%2050.0%20-%205.0%20*%20_latent_mass%20%2B%2010.0%20*%20_latent_fitness%20%2B%20np.random.normal(0.0%2C%203.0%2C%20_n)%0A%0A%20%20%20%20df_X%20%3D%20pd.DataFrame(%7B%22Weight%22%3A%20_weight%2C%20%22Waist%22%3A%20_waist%2C%20%22Pulse%22%3A%20_pulse%7D)%0A%20%20%20%20df_Y%20%3D%20pd.DataFrame(%7B%22Pullups%22%3A%20_pullups%2C%20%22Situps%22%3A%20_situps%2C%20%22Jumps%22%3A%20_jumps%7D)%0A%20%20%20%20return%20df_X%2C%20df_Y%0A%0A%0A%40app.cell%0Adef%20_(CCA%2C%20df_X%2C%20df_Y%2C%20go%2C%20make_subplots%2C%20np)%3A%0A%20%20%20%20%23%20Interactive%20Visualizations%20Cell%3A%0A%20%20%20%20%23%20Subplot%201%3A%20Scatter%20plot%20of%201st%20Canonical%20Variates%20(u1%20vs%20v1)%20with%20correlation%20fit%0A%20%20%20%20%23%20Subplot%202%3A%20Canonical%20Correlation%20Spectrum%20(rho_1%2C%20rho_2%2C%20rho_3)%0A%20%20%20%20%23%20Subplot%203%3A%20Canonical%20Loadings%20Bar%20Chart%20for%20Set%20X%20and%20Set%20Y%20on%20Variate%201%0A%0A%20%20%20%20_fig%20%3D%20make_subplots(%0A%20%20%20%20%20%20%20%20rows%3D1%2C%0A%20%20%20%20%20%20%20%20cols%3D3%2C%0A%20%20%20%20%20%20%20%20subplot_titles%3D(%0A%20%20%20%20%20%20%20%20%20%20%20%20%221.%20Alignment%3A%20Variate%20u1%20vs.%20Variate%20v1%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%222.%20Canonical%20Correlation%20Spectrum%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%223.%20Feature%20Loadings%20on%201st%20Canonical%20Variate%22%2C%0A%20%20%20%20%20%20%20%20)%2C%0A%20%20%20%20%20%20%20%20horizontal_spacing%3D0.09%2C%0A%20%20%20%20)%0A%0A%20%20%20%20%23%20Fit%20Scikit-Learn%20CCA%0A%20%20%20%20_cca%20%3D%20CCA(n_components%3D3%2C%20scale%3DTrue)%0A%20%20%20%20_cca.fit(df_X%2C%20df_Y)%0A%20%20%20%20_u%2C%20_v%20%3D%20_cca.transform(df_X%2C%20df_Y)%0A%0A%20%20%20%20%23%20Compute%20exact%20canonical%20correlation%20coefficients%0A%20%20%20%20_corrs%20%3D%20%5Bnp.corrcoef(_u%5B%3A%2C%20i%5D%2C%20_v%5B%3A%2C%20i%5D)%5B0%2C%201%5D%20for%20i%20in%20range(3)%5D%0A%0A%20%20%20%20%23%20Subplot%201%3A%20Variates%20Scatter%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_u%5B%3A%2C%200%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20y%3D_v%5B%3A%2C%200%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%3D6%2C%20color%3D%22%236366f1%22%2C%20opacity%3D0.75)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20name%3D%22Aligned%20Pairs%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20hovertemplate%3D%22u1%20(Physio)%3A%20%25%7Bx%3A.2f%7D%3Cbr%3Ev1%20(Athletic)%3A%20%25%7By%3A.2f%7D%3Cextra%3E%3C%2Fextra%3E%22%2C%0A%20%20%20%20%20%20%20%20)%2C%0A%20%20%20%20%20%20%20%20row%3D1%2C%0A%20%20%20%20%20%20%20%20col%3D1%2C%0A%20%20%20%20)%0A%0A%20%20%20%20%23%20Trend%20line%0A%20%20%20%20_line_x%20%3D%20np.linspace(_u%5B%3A%2C%200%5D.min()%2C%20_u%5B%3A%2C%200%5D.max()%2C%2050)%0A%20%20%20%20_slope%2C%20_inter%20%3D%20np.polyfit(_u%5B%3A%2C%200%5D%2C%20_v%5B%3A%2C%200%5D%2C%201)%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_line_x%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20y%3D_slope%20*%20_line_x%20%2B%20_inter%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20mode%3D%22lines%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20line%3Ddict(color%3D%22%23ef4444%22%2C%20width%3D2.5%2C%20dash%3D%22dash%22)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20name%3Df%22Fit%20(rho_1%20%3D%20%7B_corrs%5B0%5D%3A.3f%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%20Subplot%202%3A%20Spectrum%20Bar%20Chart%0A%20%20%20%20_pair_names%20%3D%20%5B%22Pair%201%22%2C%20%22Pair%202%22%2C%20%22Pair%203%22%5D%0A%20%20%20%20_fig.add_trace(%0A%20%20%20%20%20%20%20%20go.Bar(%0A%20%20%20%20%20%20%20%20%20%20%20%20x%3D_pair_names%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20y%3D_corrs%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20marker_color%3D%5B%22%2310b981%22%2C%20%22%233b82f6%22%2C%20%22%2394a3b8%22%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20name%3D%22Canonical%20Correlation%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20text%3D%5Bf%22%7Bc%3A.3f%7D%22%20for%20c%20in%20_corrs%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20textposition%3D%22auto%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%20Subplot%203%3A%20Structural%20Loadings%20on%20Variate%201%0A%20%20%20%20%23%20Correlation%20between%20original%20variables%20and%20first%20canonical%20variates%0A%20%20%20%20_loadings_x%20%3D%20%5Bnp.corrcoef(df_X%5Bcol%5D%2C%20_u%5B%3A%2C%200%5D)%5B0%2C%201%5D%20for%20col%20in%20df_X.columns%5D%0A%20%20%20%20_loadings_y%20%3D%20%5Bnp.corrcoef(df_Y%5Bcol%5D%2C%20_v%5B%3A%2C%200%5D)%5B0%2C%201%5D%20for%20col%20in%20df_Y.columns%5D%0A%20%20%20%20_feat_names%20%3D%20list(df_X.columns)%20%2B%20list(df_Y.columns)%0A%20%20%20%20_all_loadings%20%3D%20_loadings_x%20%2B%20_loadings_y%0A%20%20%20%20_colors%20%3D%20%5B%22%233b82f6%22%5D%20*%203%20%2B%20%5B%22%2310b981%22%5D%20*%203%0A%0A%20%20%20%20_fig.add_trace(%0A%20%20%20%20%20%20%20%20go.Bar(%0A%20%20%20%20%20%20%20%20%20%20%20%20x%3D_feat_names%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20y%3D_all_loadings%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20marker_color%3D_colors%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20name%3D%22Canonical%20Loadings%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20text%3D%5Bf%22%7Bl%3A.2f%7D%22%20for%20l%20in%20_all_loadings%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20textposition%3D%22auto%22%2C%0A%20%20%20%20%20%20%20%20)%2C%0A%20%20%20%20%20%20%20%20row%3D1%2C%0A%20%20%20%20%20%20%20%20col%3D3%2C%0A%20%20%20%20)%0A%0A%20%20%20%20_fig.update_layout(%0A%20%20%20%20%20%20%20%20template%3D%22plotly_white%22%2C%0A%20%20%20%20%20%20%20%20height%3D480%2C%0A%20%20%20%20%20%20%20%20title%3Ddict(%0A%20%20%20%20%20%20%20%20%20%20%20%20text%3D%22Canonical%20Correlation%20Analysis%3A%20Cross-Modal%20Alignment%20%26%20Structural%20Loadings%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20x%3D0.5%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20xanchor%3D%22center%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20font%3Ddict(size%3D16%2C%20family%3D%22Inter%2C%20system-ui%2C%20sans-serif%22)%2C%0A%20%20%20%20%20%20%20%20)%2C%0A%20%20%20%20%20%20%20%20legend%3Ddict(orientation%3D%22h%22%2C%20yanchor%3D%22bottom%22%2C%20y%3D-0.25%2C%20xanchor%3D%22center%22%2C%20x%3D0.5)%2C%0A%20%20%20%20%20%20%20%20margin%3Ddict(l%3D40%2C%20r%3D40%2C%20t%3D75%2C%20b%3D80)%2C%0A%20%20%20%20)%0A%0A%20%20%20%20_fig.update_xaxes(title_text%3D%22Canonical%20Variate%20u1%20(Physiology)%22%2C%20row%3D1%2C%20col%3D1)%0A%20%20%20%20_fig.update_yaxes(title_text%3D%22Canonical%20Variate%20v1%20(Athletics)%22%2C%20row%3D1%2C%20col%3D1)%0A%0A%20%20%20%20_fig.update_xaxes(title_text%3D%22Canonical%20Variate%20Pair%22%2C%20row%3D1%2C%20col%3D2)%0A%20%20%20%20_fig.update_yaxes(title_text%3D%22Canonical%20Correlation%20(rho)%22%2C%20range%3D%5B0%2C%201.05%5D%2C%20row%3D1%2C%20col%3D2)%0A%0A%20%20%20%20_fig.update_xaxes(title_text%3D%22Original%20Indicator%20Feature%22%2C%20tickangle%3D45%2C%20row%3D1%2C%20col%3D3)%0A%20%20%20%20_fig.update_yaxes(title_text%3D%22Correlation%20with%20Variate%201%22%2C%20range%3D%5B-1.05%2C%201.05%5D%2C%20row%3D1%2C%20col%3D3)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20---%0A%0A%20%20%20%20%23%23%20%5Bd%5D%20Code%20Examples%0A%0A%20%20%20%20Below%20we%20implement%20two%20rigorous%2C%20production-grade%20demonstrations%3A%0A%20%20%20%201.%20**Full%20Step-by-Step%20CCA%20from%20Scratch%20via%20SVD%20of%20the%20Coherence%20Matrix**%3A%20Computing%20covariance%20blocks%20%24%5Cboldsymbol%7B%5CSigma%7D_%7BXX%7D%2C%20%5Cboldsymbol%7B%5CSigma%7D_%7BYY%7D%2C%20%5Cboldsymbol%7B%5CSigma%7D_%7BXY%7D%24%2C%20matrix%20square%20roots%20via%20eigendecomposition%2C%20SVD%20of%20%24%5Cmathbf%7BK%7D%24%2C%20and%20extracting%20canonical%20weights%20%24%5Cmathbf%7Ba%7D_1%2C%20%5Cmathbf%7Bb%7D_1%24%20and%20correlation%20%24%5Crho_1%24%2C%20verified%20against%20Scikit-Learn.%0A%20%20%20%202.%20**Multivariate%20Independence%20Hypothesis%20Test%20(Wilks'%20Lambda)**%3A%20Computing%20Wilks'%20%24%5CLambda%5E*%24%2C%20degrees%20of%20freedom%20%24p%20%5Ccdot%20q%24%2C%20Bartlett's%20%24%5Cchi%5E2%24-statistic%2C%20and%20two-tailed%20%24p%24-value%20to%20verify%20statistical%20significance%20of%20cross-modal%20coupling.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(CCA%2C%20df_X%2C%20df_Y%2C%20np%2C%20pd)%3A%0A%20%20%20%20%23%20Example%201%3A%20Pure%20NumPy%20Step-by-Step%20CCA%20from%20Scratch%0A%20%20%20%20_n%20%3D%20len(df_X)%0A%20%20%20%20_X%20%3D%20df_X.to_numpy()%0A%20%20%20%20_Y%20%3D%20df_Y.to_numpy()%0A%0A%20%20%20%20%23%20Center%20variables%0A%20%20%20%20_Xc%20%3D%20_X%20-%20np.mean(_X%2C%20axis%3D0)%0A%20%20%20%20_Yc%20%3D%20_Y%20-%20np.mean(_Y%2C%20axis%3D0)%0A%0A%20%20%20%20%23%20Covariance%20blocks%0A%20%20%20%20_Sxx%20%3D%20(_Xc.T%20%40%20_Xc)%20%2F%20(_n%20-%201)%0A%20%20%20%20_Syy%20%3D%20(_Yc.T%20%40%20_Yc)%20%2F%20(_n%20-%201)%0A%20%20%20%20_Sxy%20%3D%20(_Xc.T%20%40%20_Yc)%20%2F%20(_n%20-%201)%0A%0A%20%20%20%20%23%20Matrix%20square%20roots%20via%20symmetric%20eigendecomposition%0A%20%20%20%20def%20_inv_sqrt_matrix(M)%3A%0A%20%20%20%20%20%20%20%20_evals%2C%20_evecs%20%3D%20np.linalg.eigh(M)%0A%20%20%20%20%20%20%20%20_inv_sqrt_d%20%3D%20np.diag(1.0%20%2F%20np.sqrt(np.maximum(_evals%2C%201e-12)))%0A%20%20%20%20%20%20%20%20return%20_evecs%20%40%20_inv_sqrt_d%20%40%20_evecs.T%0A%0A%20%20%20%20_Sxx_inv_sqrt%20%3D%20_inv_sqrt_matrix(_Sxx)%0A%20%20%20%20_Syy_inv_sqrt%20%3D%20_inv_sqrt_matrix(_Syy)%0A%0A%20%20%20%20%23%20Normalized%20coherence%20matrix%20K%0A%20%20%20%20_K%20%3D%20_Sxx_inv_sqrt%20%40%20_Sxy%20%40%20_Syy_inv_sqrt%0A%20%20%20%20_U%2C%20_D%2C%20_Vt%20%3D%20np.linalg.svd(_K)%0A%0A%20%20%20%20%23%20Canonical%20correlations%20from%20scratch%0A%20%20%20%20_rho_scratch%20%3D%20_D%0A%0A%20%20%20%20%23%20Canonical%20weights%0A%20%20%20%20_A_weights%20%3D%20_Sxx_inv_sqrt%20%40%20_U%0A%20%20%20%20_B_weights%20%3D%20_Syy_inv_sqrt%20%40%20_Vt.T%0A%0A%20%20%20%20%23%20Scikit-Learn%20Reference%0A%20%20%20%20_cca%20%3D%20CCA(n_components%3D3%2C%20scale%3DFalse)%0A%20%20%20%20_cca.fit(_Xc%2C%20_Yc)%0A%20%20%20%20_u_sk%2C%20_v_sk%20%3D%20_cca.transform(_Xc%2C%20_Yc)%0A%20%20%20%20_rho_sklearn%20%3D%20%5Bnp.corrcoef(_u_sk%5B%3A%2C%20i%5D%2C%20_v_sk%5B%3A%2C%20i%5D)%5B0%2C%201%5D%20for%20i%20in%20range(3)%5D%0A%0A%20%20%20%20_comparison_rows%20%3D%20%5B%5D%0A%20%20%20%20for%20_i%20in%20range(len(_rho_scratch))%3A%0A%20%20%20%20%20%20%20%20_comparison_rows.append(%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%22Canonical%20Pair%22%3A%20f%22Pair%20%7B_i%20%2B%201%7D%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22Scratch%20SVD%20Correlation%20rho%22%3A%20f%22%7B_rho_scratch%5B_i%5D%3A.6f%7D%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22Scikit-Learn%20Correlation%20rho%22%3A%20f%22%7B_rho_sklearn%5B_i%5D%3A.6f%7D%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22Discrepancy%22%3A%20f%22%7Bnp.abs(_rho_scratch%5B_i%5D%20-%20_rho_sklearn%5B_i%5D)%3A.2e%7D%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22Interpretation%22%3A%20%22Primary%20Shared%20Alignment%20Axis%22%20if%20_i%20%3D%3D%200%20else%20%22Orthogonal%20Secondary%20Mode%22%2C%0A%20%20%20%20%20%20%20%20%7D)%0A%0A%20%20%20%20_df_cca_benchmark%20%3D%20pd.DataFrame(_comparison_rows)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(df_X%2C%20df_Y%2C%20np%2C%20pd%2C%20stats)%3A%0A%20%20%20%20%23%20Example%202%3A%20Wilks'%20Lambda%20Multivariate%20Independence%20Test%0A%20%20%20%20_n%20%3D%20len(df_X)%0A%20%20%20%20_p%20%3D%20df_X.shape%5B1%5D%0A%20%20%20%20_q%20%3D%20df_Y.shape%5B1%5D%0A%20%20%20%20_m%20%3D%20min(_p%2C%20_q)%0A%0A%20%20%20%20%23%20Compute%20canonical%20correlations%0A%20%20%20%20_Xc%20%3D%20df_X.to_numpy()%20-%20np.mean(df_X.to_numpy()%2C%20axis%3D0)%0A%20%20%20%20_Yc%20%3D%20df_Y.to_numpy()%20-%20np.mean(df_Y.to_numpy()%2C%20axis%3D0)%0A%0A%20%20%20%20_Sxx%20%3D%20(_Xc.T%20%40%20_Xc)%20%2F%20(_n%20-%201)%0A%20%20%20%20_Syy%20%3D%20(_Yc.T%20%40%20_Yc)%20%2F%20(_n%20-%201)%0A%20%20%20%20_Sxy%20%3D%20(_Xc.T%20%40%20_Yc)%20%2F%20(_n%20-%201)%0A%0A%20%20%20%20def%20_inv_sqrt(M)%3A%0A%20%20%20%20%20%20%20%20_evals%2C%20_evecs%20%3D%20np.linalg.eigh(M)%0A%20%20%20%20%20%20%20%20return%20_evecs%20%40%20np.diag(1.0%20%2F%20np.sqrt(np.maximum(_evals%2C%201e-12)))%20%40%20_evecs.T%0A%0A%20%20%20%20_K%20%3D%20_inv_sqrt(_Sxx)%20%40%20_Sxy%20%40%20_inv_sqrt(_Syy)%0A%20%20%20%20_%2C%20_rhos%2C%20_%20%3D%20np.linalg.svd(_K)%0A%0A%20%20%20%20%23%20Wilks'%20Lambda%0A%20%20%20%20_wilks_lambda%20%3D%20np.prod(1.0%20-%20_rhos**2)%0A%0A%20%20%20%20%23%20Bartlett's%20Chi-Square%20approximation%0A%20%20%20%20_bartlett_factor%20%3D%20_n%20-%201.0%20-%20(_p%20%2B%20_q%20%2B%201.0)%20%2F%202.0%0A%20%20%20%20_chi2_stat%20%3D%20-_bartlett_factor%20*%20np.log(_wilks_lambda)%0A%20%20%20%20_df_chi2%20%3D%20_p%20*%20_q%0A%20%20%20%20_p_value%20%3D%201.0%20-%20stats.chi2.cdf(_chi2_stat%2C%20df%3D_df_chi2)%0A%0A%20%20%20%20_df_wilks%20%3D%20pd.DataFrame(%0A%20%20%20%20%20%20%20%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20%7B%22Statistical%20Metric%22%3A%20%22Sample%20Size%20(n)%22%2C%20%22Value%22%3A%20str(_n)%2C%20%22Description%22%3A%20%22Observation%20count%22%7D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%7B%22Statistical%20Metric%22%3A%20%22Dimensions%20(p%2C%20q)%22%2C%20%22Value%22%3A%20f%22p%20%3D%20%7B_p%7D%2C%20q%20%3D%20%7B_q%7D%22%2C%20%22Description%22%3A%20%22Feature%20set%20cardinalities%22%7D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%7B%22Statistical%20Metric%22%3A%20%22Wilks'%20Lambda%20(Lambda*)%22%2C%20%22Value%22%3A%20f%22%7B_wilks_lambda%3A.6f%7D%22%2C%20%22Description%22%3A%20%22Product%20of%20unexplained%20variance%20ratios%22%7D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%7B%22Statistical%20Metric%22%3A%20%22Bartlett's%20Chi-Square%20Statistic%22%2C%20%22Value%22%3A%20f%22%7B_chi2_stat%3A.2f%7D%22%2C%20%22Description%22%3A%20%22Log-likelihood%20ratio%20test%20statistic%22%7D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%7B%22Statistical%20Metric%22%3A%20%22Degrees%20of%20Freedom%20(p%20*%20q)%22%2C%20%22Value%22%3A%20str(_df_chi2)%2C%20%22Description%22%3A%20%22Joint%20dimensionality%20df%22%7D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%7B%22Statistical%20Metric%22%3A%20%22Independence%20p-Value%22%2C%20%22Value%22%3A%20f%22%7B_p_value%3A.4e%7D%22%2C%20%22Description%22%3A%20%22Reject%20H0%3A%20strong%20cross-modal%20dependence%22%7D%2C%0A%20%20%20%20%20%20%20%20%5D%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
bd1aca76b254ca5bdc37f19943c5ce8d