-
Notifications
You must be signed in to change notification settings - Fork 1
Feat/extended evaluation #22
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| --- | ||
| title: rMAEEvaluator | ||
| description: Relative Mean Absolute Error metric | ||
| --- | ||
|
|
||
| # rMAEEvaluator | ||
|
|
||
| Calculates relative Mean Absolute Error (rMAE) — the ratio of a model's MAE to a base model's MAE. This is a standard metric in the electricity price forecasting literature (Lago et al., 2021) for benchmarking model performance. | ||
|
|
||
| ## Formula | ||
|
|
||
| rMAE = MAE(model) / MAE(base_model) | ||
|
|
||
| Where both MAEs are computed on the same data slice (same time period, same grouping). | ||
|
|
||
| **Interpretation:** | ||
| - rMAE < 1 — model **outperforms** the base model | ||
| - rMAE = 1 — model performs **equally** to the base model | ||
| - rMAE > 1 — model **underperforms** the base model | ||
|
|
||
| ## Parameters | ||
|
|
||
| | Parameter | Type | Default | Description | | ||
| |-----------|------|---------|-------------| | ||
| | `base_model` | str | Required | Name of the model to use as the benchmark | | ||
|
|
||
| ## Basic Usage | ||
|
|
||
| ```python | ||
| from epftoolbox2.evaluators import rMAEEvaluator | ||
|
|
||
| evaluator = rMAEEvaluator(base_model="OLS") | ||
| ``` | ||
|
|
||
| ## In Pipeline | ||
|
|
||
| ```python | ||
| from epftoolbox2.pipelines import ModelPipeline | ||
| from epftoolbox2.models import OLSModel, LassoCVModel | ||
| from epftoolbox2.evaluators import MAEEvaluator, rMAEEvaluator | ||
| from epftoolbox2.exporters import TerminalExporter | ||
|
|
||
| pipeline = ( | ||
| ModelPipeline() | ||
| .add_model(OLSModel(predictors=predictors, name="OLS")) | ||
| .add_model(LassoCVModel(predictors=predictors, cv=7, name="LassoCV")) | ||
| .add_evaluator(MAEEvaluator()) | ||
| .add_evaluator(rMAEEvaluator(base_model="OLS")) | ||
| .add_exporter(TerminalExporter()) | ||
| ) | ||
|
|
||
| report = pipeline.run(...) | ||
| print(report.summary()) | ||
| # model MAE rMAE | ||
| # 0 OLS 26.0199 1.0000 | ||
| # 1 LassoCV 24.8100 0.9535 | ||
| ``` | ||
|
|
||
| The rMAE is computed per data slice, so grouped views (`by_hour`, `by_horizon`, etc.) each show the relative performance for that specific group. | ||
|
|
||
| ## Notes | ||
|
|
||
| - The `base_model` name must match the `name` parameter of one of the models in the pipeline. | ||
| - If the base model's MAE is zero for a given group, rMAE returns `inf`. | ||
| - rMAE is serializable to YAML and works with `ModelPipeline.save()`/`load()`. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| --- | ||
| title: RMSEEvaluator | ||
| description: Root Mean Squared Error metric | ||
| --- | ||
|
|
||
| # RMSEEvaluator | ||
|
|
||
| Calculates Root Mean Squared Error (RMSE) between predictions and actual values. RMSE penalizes larger errors more heavily than MAE. | ||
|
|
||
| ## Formula | ||
|
|
||
| RMSE = √((1/n) × Σ(yᵢ - ŷᵢ)²) | ||
|
|
||
| Where: | ||
| - yᵢ = actual value | ||
| - ŷᵢ = predicted value | ||
| - n = number of observations | ||
|
|
||
| ## Basic Usage | ||
|
|
||
| ```python | ||
| from epftoolbox2.evaluators import RMSEEvaluator | ||
|
|
||
| evaluator = RMSEEvaluator() | ||
| ``` | ||
|
|
||
| ## In Pipeline | ||
|
|
||
| ```python | ||
| from epftoolbox2.pipelines import ModelPipeline | ||
| from epftoolbox2.models import OLSModel | ||
| from epftoolbox2.evaluators import MAEEvaluator, RMSEEvaluator | ||
| from epftoolbox2.exporters import TerminalExporter | ||
|
|
||
| pipeline = ( | ||
| ModelPipeline() | ||
| .add_model(OLSModel(predictors=predictors, name="OLS")) | ||
| .add_evaluator(MAEEvaluator()) | ||
| .add_evaluator(RMSEEvaluator()) | ||
| .add_exporter(TerminalExporter()) | ||
| ) | ||
|
|
||
| report = pipeline.run(...) | ||
| print(report.summary()) | ||
| # model MAE RMSE | ||
| # 0 OLS 26.0199 32.4512 | ||
| ``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| --- | ||
| title: CsvExporter | ||
| description: Export results to a wide-format CSV | ||
| --- | ||
|
|
||
| # CsvExporter | ||
|
|
||
| Exports detailed prediction results to a wide-format CSV file. Each row represents a single forecast point, with per-model prediction and error columns. | ||
|
|
||
| ## Parameters | ||
|
|
||
| | Parameter | Type | Default | Description | | ||
| |-----------|------|---------|-------------| | ||
| | `path` | str | Required | Output CSV file path | | ||
| | `extra_columns` | List[str] | `[]` | Columns from the source dataset to include | | ||
|
|
||
| ## Basic Usage | ||
|
|
||
| ```python | ||
| from epftoolbox2.exporters import CsvExporter | ||
|
|
||
| exporter = CsvExporter("results.csv") | ||
| ``` | ||
|
|
||
| ## Output Format | ||
|
|
||
| The CSV contains the following columns: | ||
|
|
||
| **Base columns:** | ||
| - `run_date` — date the forecast was made | ||
| - `target_date` — date being forecasted | ||
| - `hour` — hour of day (0-23) | ||
| - `horizon` — forecast horizon (1 to max) | ||
| - `day_in_test` — day index in the test period | ||
| - `actual` — actual observed value | ||
|
|
||
| **Per-model columns:** | ||
| - `{model}_prediction` — model's prediction | ||
| - `{model}_error` — residual (prediction - actual) | ||
|
|
||
| **Extra columns** (optional): | ||
| - Any columns from the source dataset, joined by `target_date` + `hour` | ||
|
|
||
| ### Example Output | ||
|
|
||
| For a pipeline with models `OLS` and `LassoCV`, and `extra_columns=["is_holiday"]`: | ||
|
|
||
| | run_date | target_date | hour | horizon | actual | OLS_prediction | OLS_error | LassoCV_prediction | LassoCV_error | is_holiday | | ||
| |----------|-------------|------|---------|--------|---------------|-----------|-------------------|---------------|------------| | ||
| | 2024-02-01 | 2024-02-02 | 0 | 1 | 48.50 | 45.23 | -3.27 | 46.10 | -2.40 | 0 | | ||
|
|
||
| ## Extra Columns | ||
|
|
||
| Use `extra_columns` to include columns from the source dataset (e.g., calendar features, weather data). Columns are joined by matching `target_date` and `hour` from the results to the source dataset's DatetimeIndex. | ||
|
|
||
| ```python | ||
| exporter = CsvExporter( | ||
| "results.csv", | ||
| extra_columns=["is_holiday", "load_forecast", "warsaw_temperature_2m"], | ||
| ) | ||
| ``` | ||
|
|
||
| If a requested column does not exist in the source dataset, it is silently skipped. | ||
|
|
||
| ## In Pipeline | ||
|
|
||
| ```python | ||
| from epftoolbox2.pipelines import ModelPipeline | ||
| from epftoolbox2.models import OLSModel, LassoCVModel | ||
| from epftoolbox2.evaluators import MAEEvaluator, RMSEEvaluator | ||
| from epftoolbox2.exporters import CsvExporter | ||
|
|
||
| pipeline = ( | ||
| ModelPipeline() | ||
| .add_model(OLSModel(predictors=predictors, name="OLS")) | ||
| .add_model(LassoCVModel(predictors=predictors, cv=7, name="LassoCV")) | ||
| .add_evaluator(MAEEvaluator()) | ||
| .add_evaluator(RMSEEvaluator()) | ||
| .add_exporter(CsvExporter( | ||
| "results.csv", | ||
| extra_columns=["is_holiday", "load_forecast"], | ||
| )) | ||
| ) | ||
|
|
||
| report = pipeline.run(data=df, test_start="2024-02-01", test_end="2024-03-01", target="price", horizon=7) | ||
| # Results saved to results.csv | ||
| ``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,6 @@ | ||
| from .base import Evaluator | ||
| from .mae import MAEEvaluator | ||
| from .rmae import rMAEEvaluator | ||
| from .rmse import RMSEEvaluator | ||
|
|
||
| __all__ = ["Evaluator", "MAEEvaluator"] | ||
| __all__ = ["Evaluator", "MAEEvaluator", "RMSEEvaluator", "rMAEEvaluator"] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| from typing import Dict | ||
|
|
||
| import pandas as pd | ||
| from .base import Evaluator | ||
|
|
||
|
|
||
| class rMAEEvaluator(Evaluator): | ||
| name = "rMAE" | ||
|
|
||
| def __init__(self, base_model: str): | ||
| self.base_model = base_model | ||
|
|
||
| def compute(self, df: pd.DataFrame, **kwargs) -> float: | ||
| model_dfs: Dict[str, pd.DataFrame] = kwargs.get("model_dfs", {}) | ||
| if self.base_model not in model_dfs: | ||
| raise ValueError( | ||
| f"rMAE base model '{self.base_model}' not found in pipeline models. " | ||
| f"Available: {list(model_dfs)}" | ||
| ) | ||
| base_df = model_dfs[self.base_model] | ||
| base_mae = (base_df["prediction"] - base_df["actual"]).abs().mean() | ||
| if base_mae == 0: | ||
| return float("inf") | ||
| model_mae = (df["prediction"] - df["actual"]).abs().mean() | ||
| return model_mae / base_mae | ||
|
dawidlinek marked this conversation as resolved.
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| import numpy as np | ||
| import pandas as pd | ||
| from .base import Evaluator | ||
|
|
||
|
|
||
| class RMSEEvaluator(Evaluator): | ||
| name = "RMSE" | ||
|
|
||
| def compute(self, df: pd.DataFrame, **kwargs) -> float: | ||
| return float(np.sqrt(((df["prediction"] - df["actual"]) ** 2).mean())) | ||
|
dawidlinek marked this conversation as resolved.
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,6 @@ | ||
| from .base import Exporter | ||
| from .csv import CsvExporter | ||
| from .terminal import TerminalExporter | ||
| from .excel import ExcelExporter | ||
|
|
||
| __all__ = ["Exporter", "TerminalExporter", "ExcelExporter"] | ||
| __all__ = ["Exporter", "CsvExporter", "TerminalExporter", "ExcelExporter"] |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.