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_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 _construct_model(self, X: np.ndarray, y: np.ndarray) -> Model: if len(X) == 0 or X.shape[1] == 0: raise ValueError("Empty covariate matrix passed") self.data = self._convert_covariates_to_data(X, y) self.sigma = Sigma(self.sigma_a, self.sigma_b, self.data.normalizing_scale) self.model = Model(self.data, self.sigma, n_trees=self.n_trees, alpha=self.alpha, beta=self.beta) return self.model
def setUp(self): self.X = format_covariate_matrix(pd.DataFrame({"a": [1, 2, 3, 4, 5]})) self.raw_y = np.array([1.0, 2.0, 3.0, 4.0, 5.0]) self.data = Data(format_covariate_matrix(self.X), self.raw_y, normalize=True) normalizing_scale = self.data.y.normalizing_scale self.model = Model(self.data, Sigma(0.001, 0.001, scaling_factor=normalizing_scale), n_trees=2, initializer=None) self.model.initialize_trees()
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)
def step(self, model: Model, sigma: Sigma) -> float: sample_value = self.sample(model, sigma) sigma.set_value(sample_value) return sample_value
def step(self, model: Model, sigma: Sigma) -> None: sigma.set_value(self.sample(model, sigma))
def _construct_model(self, X: Union[np.ndarray, pd.DataFrame], y: np.ndarray) -> Model: self.data = self._convert_covariates_to_data(X, y) self.sigma = Sigma(self.sigma_a, self.sigma_b, self.data.normalizing_scale) self.model = Model(self.data, self.sigma, n_trees=self.n_trees, alpha=self.alpha, beta=self.beta) return self.model