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. try: L = linalg.cholesky(R.T @ R) R = R @ linalg.solve(L, np.eye(data_kwds["n_latent_factors"])) except: R = utils.find_rotation_matrix(A_true, A_est, full_output=False) 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")] xs = [A_true.flatten(), truth["scores"].flatten(), truth["psi"].flatten()] ys = [ A_est.flatten(),
Js, Ks, gJs, gKs, converged, meta, X_H, label_names = contents # 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)
A_astrophysical[idx, i] = 1.0 / count A_astrophysical /= np.clip(np.sqrt(np.sum(A_astrophysical, axis=0)), 1, np.inf) # Un-assigned columns 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))