Skip to content

Rotation Matrices

import sys, os
cwd = os.getcwd()
sys.path.insert(0, f'{cwd}/../../src')
sys.path.insert(0, f'{cwd}/../../src/itetoolbox')

import numpy as np
import ite
from sklearn.utils import check_random_state
from data.toy import entropy_marginal
from scipy import stats

%matplotlib inline
%load_ext autoreload
%autoreload 2
The autoreload extension is already loaded. To reload it, use:
  %reload_ext autoreload

Random Rotation

d_dimensions = 2
seed = 123

A_rand = stats.special_ortho_group.rvs(dim=d_dimensions, random_state=seed)

print(A_rand.shape)

# Calculate the log determinant
np.linalg.slogdet(A_rand)[1]
(2, 2)
1.6653345369377348e-16

Random Orthogonal Matrix

d_dimensions = 2
seed = 123

A_ortho = stats.ortho_group.rvs(dim=d_dimensions, random_state=seed)

A_ortho.shape

# Calculate the log determinant
np.linalg.slogdet(A_ortho)[1]
4.996003610813204e-16

Random Unitary Matrix

d_dimensions = 2
seed = 123

A_unitary = stats.unitary_group.rvs(dim=d_dimensions, random_state=seed)

A_unitary.shape

# Calculate the log determinant
np.linalg.slogdet(A_unitary)[1]
-4.163336342344337e-16

Random Correlation Matrix

d_dimensions = 2
seed = 123
eigs = np.array([1.2, 0.8])

A_corr = stats.random_correlation.rvs(eigs=eigs, random_state=seed)

A_corr.shape

# Calculate the log determinant
np.linalg.slogdet(A_corr)[1]
-0.04082199452025481

PCA Transformation

# generate complete random matrix
n_samples = 100
d_dimensions = 2
X = np.random.rand(n_samples, d_dimensions)
from sklearn.decomposition import PCA


pca_model = PCA().fit(X)

# get components, V
V = pca_model.components_

# find log determinant transform of components
np.linalg.slogdet(V)[1]
1.1102230246251565e-16

ICA Transformation

# generate complete random matrix
n_samples = 100
d_dimensions = 2
X = np.random.rand(n_samples, d_dimensions)
from sklearn.decomposition import FastICA

ica_model = FastICA(whiten=True, random_state=seed).fit(X)

# get components, V
V = ica_model.components_

# find log determinant transform of components
np.linalg.slogdet(V)[1]
-2.094808661664451

Orthogonal Constraint

So we need to ensure that the ICA performs under the orthogonal constraint. So for this we can use the Picard-O algorithm. The software can be found here (docs).

#!pip install picard
from picard import picard

ortho = True
seed = 123

K, W, Y = picard(X, ortho=True, random_state=123, whiten=True)

components = W @ K
print(components.shape)
X_trans = X @ components.T
# find log determinant transform of components
# np.linalg.slogdet(W @ K)[1]
(2, 100)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-17-2206cfc7d55f> in <module>
      8 components = W @ K
      9 print(components.shape)
---> 10 X_trans = X @ components.T
     11 # find log determinant transform of components
     12 # np.linalg.slogdet(W @ K)[1]

ValueError: matmul: Input operand 1 has a mismatch in its core dimension 0, with gufunc signature (n?,k),(k,m?)->(n?,m?) (size 100 is different from 2)
X_trans.shape, components.shape, W.shape
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-18-deeb8cb2cac6> in <module>
----> 1 X_trans.shape, components.shape, W.shape

NameError: name 'X_trans' is not defined
np.linalg.slogdet(W)[1]
9.020562075079397e-16

Can we go back??

# can we go back?
X_ori = (W @ X_trans).T
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-21-9e4215dfa4ae> in <module>
      1 # can we go back?
----> 2 X_ori = (W @ X_trans).T

NameError: name 'X_trans' is not defined
X_ori.shape
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-22-36430365f080> in <module>
----> 1 X_ori.shape

NameError: name 'X_ori' is not defined