def _build_first_tree(self): """Build first level tree.""" tau_sorted = self._sort_tau_by_y(0) for itr in range(self.n_nodes - 1): ind = int(tau_sorted[itr, 0]) name, theta = Bivariate.select_copula(self.u_matrix[:, (0, ind)]) new_edge = Edge(itr, 0, ind, name, theta) new_edge.tau = self.tau_matrix[0, ind] self.edges.append(new_edge)
def get_child_edge(cls, left_parent, right_parent): """Construct a child edge from two parent edges.""" [ed1, ed2, depend_set] = cls._identify_eds_ing(left_parent, right_parent) left_u, right_u = cls.get_conditional_uni(left_parent, right_parent) X = np.array([[x, y] for x, y in zip(left_u, right_u)]) name, theta = Bivariate.select_copula(X) new_edge = Edge(ed1, ed2, name, theta) new_edge.D = depend_set new_edge.parents = [left_parent, right_parent] return new_edge
def test_copula_selection_negative_tau(self): """If tau is negative, should choose frank copula.""" # Setup X = np.array([[0.1, 0.6], [0.2, 0.5], [0.3, 0.4], [0.4, 0.3]]) assert stats.kendalltau(X[:, 0], X[:, 1])[0] < 0 # Run name, param = Bivariate.select_copula(X) expected = CopulaTypes.FRANK # Check assert name == expected
def _build_first_tree(self): """Build the first tree with n-1 variable.""" # Prim's algorithm neg_tau = -1.0 * abs(self.tau_matrix) X = {0} while len(X) != self.n_nodes: adj_set = set() for x in X: for k in range(self.n_nodes): if k not in X and k != x: adj_set.add((x, k)) # find edge with maximum edge = sorted(adj_set, key=lambda e: neg_tau[e[0]][e[1]])[0] name, theta = Bivariate.select_copula(self.u_matrix[:, (edge[0], edge[1])]) left, right = sorted([edge[0], edge[1]]) new_edge = Edge(left, right, name, theta) new_edge.tau = self.tau_matrix[edge[0], edge[1]] self.edges.append(new_edge) X.add(edge[1])
def _build_first_tree(self): # find the pair of maximum tau tau_matrix = self.tau_matrix tau_sorted = self._sort_tau_by_y(0) left_ind = tau_sorted[0, 0] right_ind = tau_sorted[1, 0] T1 = np.array([left_ind, 0, right_ind]).astype(int) tau_T1 = tau_sorted[:2, 1] # replace tau matrix of the selected variables as a negative number tau_matrix[:, [T1]] = -10 for k in range(2, self.n_nodes - 1): left = np.argmax(tau_matrix[T1[0], :]) right = np.argmax(tau_matrix[T1[-1], :]) valL = np.max(tau_matrix[T1[0], :]) valR = np.max(tau_matrix[T1[-1], :]) if valL > valR: # add nodes to the left T1 = np.append(int(left), T1) tau_T1 = np.append(valL, tau_T1) tau_matrix[:, left] = -10 else: # add node to the right T1 = np.append(T1, int(right)) tau_T1 = np.append(tau_T1, valR) tau_matrix[:, right] = -10 for k in range(self.n_nodes - 1): name, theta = Bivariate.select_copula(self.u_matrix[:, (T1[k], T1[k + 1])]) left, right = sorted([T1[k], T1[k + 1]]) new_edge = Edge(k, left, right, name, theta) new_edge.tau = tau_T1[k] self.edges.append(new_edge)
def get_child_edge(cls, index, left_parent, right_parent): """Construct a child edge from two parent edges. Args: index (int): Index of the new Edge. left_parent (Edge): Left parent right_parent (Edge): Right parent Returns: Edge: The new child edge. """ [ed1, ed2, depend_set] = cls._identify_eds_ing(left_parent, right_parent) left_u, right_u = cls.get_conditional_uni(left_parent, right_parent) X = np.array([[x, y] for x, y in zip(left_u, right_u)]) copula = Bivariate.select_copula(X) name, theta = copula.copula_type, copula.theta new_edge = Edge(index, ed1, ed2, name, theta) new_edge.D = depend_set new_edge.parents = [left_parent, right_parent] return new_edge