def test_X_dtype_prediction(standardize): """ tests whether providing an int or float X returns the same prediction """ training_data = pd.DataFrame( {"x": np.linspace(0, 500, num=500), "y": np.linspace(0, 500, num=500)} ) reg = DAGRegressor( threshold=0.0, alpha=0.0001, beta=0.5, fit_intercept=True, hidden_layer_units=[10], standardize=standardize, ) X = training_data.loc[:, ["x"]] y = training_data["y"] reg.fit(X, y) test_data_int = pd.DataFrame({"x": [0, 250, 500]}) test_data_float = pd.DataFrame({"x": [0.0, 250.0, 500.0]}) pred_int = reg.predict(test_data_int) pred_float = reg.predict(test_data_float) assert np.all(pred_float == pred_int)
def test_independent_predictions(hidden_layer_units): x = np.linspace(0.0, 100, 100) X = pd.DataFrame({"x": x}) Y = pd.Series(x**2, name="y") reg = DAGRegressor( threshold=0.0, alpha=0.0, beta=0.5, fit_intercept=True, hidden_layer_units=hidden_layer_units, standardize=False, ) reg.fit(X, Y) pred_alone = reg.predict(pd.DataFrame({"x": [10.0]})) pred_joint0 = reg.predict(pd.DataFrame({"x": [10.0, 0.0]})) pred_joint1 = reg.predict(pd.DataFrame({"x": [10.0] + x.tolist()})) assert np.isclose(pred_alone[0], pred_joint0[0]) assert np.isclose(pred_alone[0], pred_joint1[0]) assert np.isclose(pred_joint0[0], pred_joint1[0])
def test_glm(self, target_dist_type, y): reg = DAGRegressor(target_dist_type=target_dist_type) X = np.random.normal(size=(100, 2)) reg.fit(X, y) reg.predict(X)
class Forecaster: def __init__(self, tabu_child_nodes=['hour', 'dayofweak', 'dayofyear'], tabu_edges=[('demand', 'demand')]): self.generic_tabu_child_nodes = tabu_child_nodes self.generic_tabu_edges = tabu_edges def _expand_tabu_edges(self, data_columns): """Prepare the tabu_edges input for the DAGregressor Examples -------- >>> f = Forecaster() >>> f._expand_tabu_edges(data_columns=['demand-1', 'demand-2', 'constraint-1', ... 'availability-1', 'availability-2']) Parameters ---------- data_columns Returns ------- """ expanded_edges = [] for generic_edge in self.generic_tabu_edges: first_generic_node = generic_edge[0] second_generic_node = generic_edge[1] specific_first_nodes = [col for col in data_columns if first_generic_node in col] specific_second_nodes = [col for col in data_columns if second_generic_node in col] specific_edges = product(specific_first_nodes, specific_second_nodes) specific_edges = [edge for edge in specific_edges if edge[0] != edge[1]] expanded_edges += specific_edges return expanded_edges def train(self, data, train_sample_fraction, target_col): self.target_col = target_col self.features = [col for col in data.columns if col not in [target_col, 'interval'] and 'fleet-dispatch' not in col] tabu_child_nodes = [col for col in self.generic_tabu_edges if col in self.features] self.regressor = DAGRegressor(threshold=0.0, alpha=0.0001, beta=0.5, fit_intercept=True, hidden_layer_units=[10], standardize=True, tabu_child_nodes=tabu_child_nodes, tabu_edges=self._expand_tabu_edges(self.features) ) n_rows = len(data.index) sample_size = int(n_rows * train_sample_fraction) train = data.sample(sample_size, random_state=1) train = train.reset_index(drop=True) X, y = train.loc[:, self.features], np.asarray(train[target_col]) self.regressor.fit(X, y) def price_forecast_with_generation_sensitivities(self, forward_data, region, market, min_delta, max_delta, steps): prediction = forward_data.loc[:, ['interval']] if market + '-fleet-dispatch' in forward_data.columns: forward_data['old_demand'] = forward_data[region + '-demand'] + forward_data[market + '-fleet-dispatch'] else: forward_data['old_demand'] = forward_data[region + '-demand'] delta_step_size = max(int((max_delta - min_delta) / steps), 1) for delta in range(int(min_delta), int(max_delta) + delta_step_size, delta_step_size): forward_data[region + '-demand'] = forward_data['old_demand'] - delta X = forward_data.loc[:, self.features] Y = self.regressor.predict(X) prediction[delta] = Y return prediction def single_trace_forecast(self, forward_data): prediction = forward_data.loc[:, ['interval']] X = forward_data.loc[:, self.features] Y = self.regressor.predict(X) prediction[self.target_col] = Y return prediction