def _polar_rotate_shrink(X, gamma=0.1): # Algorithm 1 from the paper U, _, _ = selectSVD(X, n_components=X.shape[1], algorithm="full") # U = _polar(X) # R, _ = orthogonal_procrustes(U_old, U) # print(np.linalg.norm(U_old @ R - U)) U_rot = _varimax(U) U_thresh = soft_threshold(U_rot, gamma) return U_thresh
def _initialize(self, X): """[summary] Parameters ---------- X : [type] [description] Returns ------- [type] [description] """ U, D, Vt = selectSVD(X, n_components=self.n_components) score = np.linalg.norm(D) return U, Vt.T, score
def sparse_component_analysis(X, n_components=2, gamma=None, max_iter=10, reorder_components=True): X = X.copy() U, D, Vt = selectSVD(X, n_components=n_components) if gamma is None: gamma = np.sqrt(U.shape[1] * X.shape[1]) Z_hat = U Y_hat = Vt.T i = 0 while i < max_iter: Y_hat = _polar_rotate_shrink(X.T @ Z_hat, gamma=gamma) Z_hat = _polar(X @ Y_hat) i += 1 if reorder_components: Z_hat, Y_hat = _reorder_components(X, Z_hat, Y_hat) return Z_hat, Y_hat
def _polar(X): # REF: https://en.wikipedia.org/wiki/Polar_decomposition#Relation_to_the_SVD U, D, Vt = selectSVD(X, n_components=X.shape[1], algorithm="full") return U @ Vt
def _polar_rotate_shrink(X, gamma=0.1): U, D, Vt = selectSVD(X, n_components=X.shape[1], algorithm="full") U_rot = _varimax(U) U_thresh = _soft_threshold(U_rot, gamma) return U_thresh
def _initialize(X): U, D, Vt = selectSVD(X, n_components=n_components) return U, Vt.T
va="center", fontsize="xx-small", ) ax.axis("off") # ax.axis("off") ax.set(xticks=[], yticks=[], ylabel="", xlabel="") n_iter = 1 n_show = 5 fig, axs = plt.subplots(n_iter * n_show, 3 * n_show, figsize=(15, 5)) colors = sns.color_palette("deep", 3) for i in range(n_iter): A = X.T @ Z_hat U, _, _ = selectSVD(A, n_components=A.shape[1], algorithm="full") plot_components(U, axs[i * n_show : (i + 1) * n_show, :n_show], color=colors[0]) # pairplot(U, alpha=0.2) U_rot = _varimax(U) plot_components( U_rot, axs[i * n_show : (i + 1) * n_show, n_show : 2 * n_show], color=colors[1] ) # pairplot(U_rot, alpha=0.2) U_thresh = soft_threshold(U_rot, gamma=5 * n_components) plot_components( U_thresh, axs[i * n_show : (i + 1) * n_show, 2 * n_show : 3 * n_show], color=colors[2], )
#%% sns.histplot(lr_score) #%% adj = mg.sum.adj.copy() adj = remove_loops(adj) H = adj - adj.T # is it indeed skew symmetric? print((H == -H.T).all()) from graspologic.embed import selectSVD U, S, V = selectSVD(H, n_components=2) #%% n = len(adj) e = np.full(n, 1 / np.sqrt(n)) P = U @ U.T # project ones vector onto span of the first two components e_proj = P @ e e_proj = e_proj / np.linalg.norm(e_proj) # need to find the intersection of the orthogonal complement of e_proj and the span of U P_e_proj = np.outer(e_proj, e_proj) # get random vector in the span of U u_rand = P @ np.random.normal(size=n) # get vector orthogonal to e_proj svd_score = (np.eye(n) - P_e_proj) @ u_rand
X = ase.fit_transform(A) Q = ortho_group.rvs(X.shape[1]) X = X @ Q Xs.append(X) sns.scatterplot(x=X[:, 0], y=X[:, 1], hue=labels, ax=axs[i], legend=False) # ax.set(xticks=[], yticks=[]) plt.tight_layout() Z = np.concatenate(Xs, axis=1) # ase = AdjacencySpectralEmbed(n_components=2) # V = ase.fit_transform(Z) V, D, W = selectSVD(Z, n_components=2) Vs.append(V) #%% from graspologic.align import OrthogonalProcrustes fig, axs = plt.subplots(2, 5, figsize=(20, 8)) axs = axs.flat for i, V in enumerate(Vs): V_rot = OrthogonalProcrustes().fit_transform(V, Vs[0]) # V_rot = V ax = axs[i] sns.scatterplot(x=V_rot[:, 0], y=V_rot[:, 1], hue=labels, ax=ax,