def test_multiplication_with_both_empty_dataframe(): df1 = pd.DataFrame( [], columns=["event_value", "belief_horizon"], ) # set correct types df1["event_value"] = pd.to_numeric(df1["event_value"]) df1["belief_horizon"] = pd.to_timedelta(df1["belief_horizon"]) df2 = pd.DataFrame( [], columns=["event_value", "belief_horizon"], ) # set correct types df2["event_value"] = pd.to_numeric(df2["event_value"]) df2["belief_horizon"] = pd.to_timedelta(df2["belief_horizon"]) df_compare = pd.DataFrame( [], columns=["event_value", "belief_horizon"], ) # set correct types df_compare["event_value"] = pd.to_numeric(df_compare["event_value"]) df_compare["belief_horizon"] = pd.to_timedelta( df_compare["belief_horizon"]) df = multiply_dataframe_with_deterministic_beliefs(df1, df2) pd.testing.assert_frame_equal(df, df_compare)
def test_multiplication_with_one_empty_dataframe(): df1 = pd.DataFrame( [], columns=["event_value", "belief_horizon"], ) # set correct types df1["event_value"] = pd.to_numeric(df1["event_value"]) df1["belief_horizon"] = pd.to_timedelta(df1["belief_horizon"]) df2 = pd.DataFrame( [[10.0, timedelta(hours=1)]], index=pd.date_range("2000-01-01 13:00", "2000-01-01 18:00", freq="1h", closed="left"), columns=["event_value", "belief_horizon"], ) df_compare = pd.DataFrame( [[np.nan, timedelta(hours=1)]], index=pd.date_range("2000-01-01 13:00", "2000-01-01 18:00", freq="1h", closed="left"), columns=["event_value", "belief_horizon"], ) # set correct types df_compare["event_value"] = pd.to_numeric(df_compare["event_value"]) df_compare["belief_horizon"] = pd.to_timedelta( df_compare["belief_horizon"]) df = multiply_dataframe_with_deterministic_beliefs(df1, df2) pd.testing.assert_frame_equal(df, df_compare)
def test_multiplication(): df1 = pd.DataFrame( [[30.0, timedelta(hours=3)]], index=pd.date_range("2000-01-01 10:00", "2000-01-01 15:00", freq="1h", closed="left"), columns=["event_value", "belief_horizon"], ) df2 = pd.DataFrame( [[10.0, timedelta(hours=1)]], index=pd.date_range("2000-01-01 13:00", "2000-01-01 18:00", freq="1h", closed="left"), columns=["event_value", "belief_horizon"], ) df = multiply_dataframe_with_deterministic_beliefs(df1, df2) df_compare = pd.concat( [ pd.DataFrame( [[np.nan, timedelta(hours=3)]], index=pd.date_range("2000-01-01 10:00", "2000-01-01 13:00", freq="1h", closed="left"), columns=["event_value", "belief_horizon"], ), pd.DataFrame( [[300.0, timedelta(hours=1)]], index=pd.date_range("2000-01-01 13:00", "2000-01-01 15:00", freq="1h", closed="left"), columns=["event_value", "belief_horizon"], ), pd.DataFrame( [[np.nan, timedelta(hours=1)]], index=pd.date_range("2000-01-01 15:00", "2000-01-01 18:00", freq="1h", closed="left"), columns=["event_value", "belief_horizon"], ), ], axis=0, ) pd.testing.assert_frame_equal(df, df_compare)
def get_revenues_costs_data( power_data: pd.DataFrame, prices_data: pd.DataFrame, power_forecast_data: pd.DataFrame, prices_forecast_data: pd.DataFrame, metrics: Dict[str, float], unit_factor: float, resolution: str, showing_individual_traces: bool, ) -> Tuple[pd.DataFrame, pd.DataFrame, dict]: """Compute revenues/costs data. These data are purely derivative from power and prices. For forecasts we use the WAPE metrics. Then we calculate metrics on this construct. The unit factor is used when multiplying quantities and prices, e.g. when multiplying quantities in kWh with prices in EUR/MWh, use a unit factor of 0.001. Return revenue/cost observations, revenue/cost forecasts (either might be an empty DataFrame) and a dict with the following metrics: - expected value - mean absolute error - mean absolute percentage error - weighted absolute percentage error """ power_hour_factor = time_utils.resolution_to_hour_factor(resolution) rev_cost_data = multiply_dataframe_with_deterministic_beliefs( power_data, prices_data, result_source=None if showing_individual_traces else "Calculated from power and price data", multiplication_factor=power_hour_factor * unit_factor, ) if power_data.empty or prices_data.empty: metrics["realised_revenues_costs"] = np.NaN else: metrics["realised_revenues_costs"] = np.nansum( rev_cost_data["event_value"].values) rev_cost_forecasts = multiply_dataframe_with_deterministic_beliefs( power_forecast_data, prices_forecast_data, result_source="Calculated from power and price data", multiplication_factor=power_hour_factor * unit_factor, ) if power_forecast_data.empty or prices_forecast_data.empty: metrics["expected_revenues_costs"] = np.NaN metrics["mae_revenues_costs"] = np.NaN metrics["mape_revenues_costs"] = np.NaN metrics["wape_revenues_costs"] = np.NaN else: metrics["expected_revenues_costs"] = np.nansum( rev_cost_forecasts["event_value"]) metrics["mae_revenues_costs"] = calculations.mean_absolute_error( rev_cost_data["event_value"], rev_cost_forecasts["event_value"]) metrics[ "mape_revenues_costs"] = calculations.mean_absolute_percentage_error( rev_cost_data["event_value"], rev_cost_forecasts["event_value"]) metrics[ "wape_revenues_costs"] = calculations.weighted_absolute_percentage_error( rev_cost_data["event_value"], rev_cost_forecasts["event_value"]) # Todo: compute confidence interval properly - this is just a simple heuristic rev_cost_forecasts["yhat_upper"] = rev_cost_forecasts[ "event_value"] * (1 + metrics["wape_revenues_costs"]) rev_cost_forecasts["yhat_lower"] = rev_cost_forecasts[ "event_value"] * (1 - metrics["wape_revenues_costs"]) return rev_cost_data, rev_cost_forecasts, metrics