def log_change_ratio(old_node: DecisionNode, old_left_node: LeafNode, old_right_node: LeafNode, node: LeafNode, left_node: LeafNode, right_node: LeafNode, sigma: Sigma, sigma_mu: float): var = np.power(sigma.current_value(), 2) var_mu = np.power(sigma_mu, 2) n_1 = old_left_node.data.n_obsv n_1_star = left_node.data.n_obsv n_2 = old_right_node.data.n_obsv n_2_star = old_right_node.data.n_obsv combined_y_sum, left_y_sum, right_y_sum = left_and_right_sums( old_node, old_left_node, old_right_node) combined_y_sum_star, left_y_sum_star, right_y_sum_star = left_and_right_sums( node, left_node, right_node) first_term = (var / var_mu + n_1) / (var / var_mu + n_1_star) * ( var / var_mu + n_2) / (var / var_mu + n_2_star) first_term = np.log(np.sqrt(first_term)) left_contribution = np.square(left_y_sum) / (n_1 + var / var_mu) right_contribution = np.square(right_y_sum) / (n_2 + var / var_mu) left_contribution_star = np.square(left_y_sum_star) / (n_1_star + var / var_mu) right_contribution_star = np.square(right_y_sum_star) / (n_2_star + var / var_mu) contribution = left_contribution_star + right_contribution_star - left_contribution - right_contribution return first_term + contribution / (2 * var)
def log_grow_ratio(combined_node: LeafNode, left_node: LeafNode, right_node: LeafNode, sigma: Sigma, sigma_mu: float): var = np.power(sigma.current_value(), 2) var_mu = np.power(sigma_mu, 2) n = combined_node.data.n_obsv n_l = left_node.data.n_obsv n_r = right_node.data.n_obsv first_term = (var * (var + n * sigma_mu)) / ((var + n_l * var_mu) * (var + n_r * var_mu)) first_term = np.log(np.sqrt(first_term)) if combined_node.data.y_sum_cache_up_to_date and left_node.data.y_sum_cache_up_to_date: combined_y_sum = combined_node.data.summed_y() left_y_sum = left_node.data.summed_y() right_y_sum = right_node.data.summed_y() else: combined_y_sum = combined_node.data.summed_y() left_y_sum = left_node.data.summed_y() right_y_sum = combined_y_sum - left_y_sum left_resp_contribution = np.square(left_y_sum) / (var + n_l * sigma_mu) right_resp_contribution = np.square(right_y_sum) / (var + n_r * sigma_mu) combined_resp_contribution = np.square(combined_y_sum) / (var + n * sigma_mu) resp_contribution = left_resp_contribution + right_resp_contribution - combined_resp_contribution return first_term + ((var_mu / (2 * var)) * resp_contribution)
def log_grow_ratio(combined_node: LeafNode, left_node: LeafNode, right_node: LeafNode, sigma: Sigma, sigma_mu: float): var = np.power(sigma.current_value(), 2) var_mu = np.power(sigma_mu, 2) n = combined_node.data.n_obsv n_l = left_node.data.n_obsv n_r = right_node.data.n_obsv first_term = (var * (var + n * sigma_mu)) / ((var + n_l * var_mu) * (var + n_r * var_mu)) first_term = np.log(np.sqrt(first_term)) left_resp_contribution = np.square(np.sum(left_node.data.y)) / (var + n_l * sigma_mu) right_resp_contribution = np.square(np.sum(right_node.data.y)) / (var + n_r * sigma_mu) combined_resp_contribution = np.square(np.sum(combined_node.data.y)) / (var + n * sigma_mu) resp_contribution = left_resp_contribution + right_resp_contribution - combined_resp_contribution return first_term + ((var_mu / (2 * var)) * resp_contribution)