model = mcfa.MCFA(n_components=data_kwds["n_components"], n_latent_factors=data_kwds["n_latent_factors"], **mcfa_kwds) tick = time() model.fit(X) tock = time() model.message_length(X) print(f"Model took {tock - tick:.1f} seconds") A_true = truth["A"] A_est = model.theta_[model.parameter_names.index("A")] # Get exact transformation. R = utils.exact_rotation_matrix(A_true, A_est) # Now make it a valid rotation matrix. L = linalg.cholesky(R.T @ R) R = R @ linalg.solve(L, np.eye(n_latent_factors)) model.rotate(R) scatter_kwds = dict(s=1, rasterized=True, c="#000000") fig = plt.figure(figsize=(7.5, 3.09)) gs = gridspec.GridSpec(2, 3, height_ratios=[1, 4], width_ratios=[1, 1, 1]) A_est = model.theta_[model.parameter_names.index("A")]
# Get best model. J_best_mml, K_best_mml = grid_search.best(Js, Ks, meta["message_length"]) comparison_model = meta["best_models"]["mml"] # Perform rotation. A_est = comparison_model.theta_[comparison_model.parameter_names.index("A")] if A_est.shape == A_target.shape: for rotation in range(max_rotations): A_est = comparison_model.theta_[comparison_model.parameter_names.index("A")] R, p_opt, cov, *_ = utils.find_rotation_matrix(A_target, A_est, full_output=True) R_opt = utils.exact_rotation_matrix(A_target, A_est, p0=np.random.uniform(-np.pi, np.pi, comparison_model.n_latent_factors**2)) AL = linalg.cholesky(R_opt.T @ R_opt) R_opt2 = R_opt @ linalg.solve(AL, np.eye(comparison_model.n_latent_factors)) chi1 = np.sum(np.abs(A_est @ R - A_target)) chi2 = np.sum(np.abs(A_est @ R_opt2 - A_target)) R = R_opt2 if chi2 < chi1 else R # Now make it a valid rotation matrix. comparison_model.rotate(R) else: # Now it gets Hard(tm) if comparison_model_path == comparison_model_paths[0]:
for column_index in np.where(np.all(A_astrophysical == 0, axis=0))[0]: print(f"Warning: unassigned column index: {column_index}") A_astrophysical[:, column_index] = np.random.normal(0, 1e-2, size=D) if config["correct_A_astrophysical"]: AL = linalg.cholesky(A_astrophysical.T @ A_astrophysical) A_astrophysical = A_astrophysical @ linalg.solve( AL, np.eye(model.n_latent_factors)) R, p_opt, cov, *_ = utils.find_rotation_matrix(A_astrophysical, A_est, full_output=True) R_opt = utils.exact_rotation_matrix(A_astrophysical, A_est, p0=np.random.uniform( -np.pi, np.pi, model.n_latent_factors**2)) # WTF check R_opt. AL = linalg.cholesky(R_opt.T @ R_opt) R_opt2 = R_opt @ linalg.solve(AL, np.eye(model.n_latent_factors)) chi1 = np.sum(np.abs(A_est @ R - A_astrophysical)) chi2 = np.sum(np.abs(A_est @ R_opt2 - A_astrophysical)) R = R_opt2 if chi2 < chi1 else R # Now make it a valid rotation matrix. model.rotate(R, X=X, ensure_valid_rotation=True) """