def init_row_clustering(data_matrix, isotropic, num_iter=200): m, n = data_matrix.m, data_matrix.n state = crp.fit_model(data_matrix, isotropic_w=isotropic, isotropic_b=isotropic, num_iter=num_iter) U = np.zeros((m, state.assignments.max() + 1), dtype=int) U[np.arange(m), state.assignments] = 1 left = recursive.MultinomialNode(U) if isotropic: right = recursive.GaussianNode(state.centers, 'scalar', state.sigma_sq_b) else: right = recursive.GaussianNode(state.centers, 'col', state.sigma_sq_b) pred = state.centers[state.assignments, :] X = data_matrix.sample_latent_values(pred, state.sigma_sq_w * np.ones((m, n))) if isotropic: noise = recursive.GaussianNode(X - pred, 'scalar', state.sigma_sq_w) else: noise = recursive.GaussianNode(X - pred, 'col', state.sigma_sq_w) return recursive.SumNode([recursive.ProductNode([left, right]), noise])
def init_row_binary(data_matrix, num_iter=200): state = ibp.fit_model(data_matrix, num_iter=num_iter) left = recursive.BernoulliNode(state.Z) right = recursive.GaussianNode(state.A, 'scalar', state.sigma_sq_f) pred = np.dot(state.Z, state.A) X = data_matrix.sample_latent_values(pred, state.sigma_sq_n) noise = recursive.GaussianNode(X - pred, 'scalar', state.sigma_sq_n) return recursive.SumNode([recursive.ProductNode([left, right]), noise])
def init_row_chain(data_matrix, num_iter=200): states, sigma_sq_D, sigma_sq_N = chains.fit_model(data_matrix, num_iter=num_iter) integ = chains.integration_matrix(data_matrix.m_orig)[data_matrix.row_ids, :] left = recursive.IntegrationNode(integ) temp = np.vstack([states[0, :][nax, :], states[1:, :] - states[:-1, :]]) right = recursive.GaussianNode(temp, 'scalar', sigma_sq_D) pred = states[data_matrix.row_ids, :] X = data_matrix.sample_latent_values(pred, sigma_sq_N) noise = recursive.GaussianNode(X - pred, 'scalar', sigma_sq_N) return recursive.SumNode([recursive.ProductNode([left, right]), noise])
def init_low_rank(data_matrix, num_iter=200): m, n = data_matrix.m, data_matrix.n state, X = low_rank_poisson.fit_model(data_matrix, 2, num_iter=num_iter) U, V, ssq_U, ssq_N = state.U, state.V, state.ssq_U, state.ssq_N U /= ssq_U[nax, :] ** 0.25 V *= ssq_U[:, nax] ** 0.25 left = recursive.GaussianNode(U, 'col', np.sqrt(ssq_U)) right = recursive.GaussianNode(V, 'row', np.sqrt(ssq_U)) pred = np.dot(U, V) X = data_matrix.sample_latent_values(pred, ssq_N) noise = recursive.GaussianNode(X - pred, 'scalar', ssq_N) return recursive.SumNode([recursive.ProductNode([left, right]), noise])
def dummy(self): return recursive.SumNode([c.dummy() for c in self.children])