Skip to content
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

added trip metrics #31

Merged
merged 2 commits into from
Jan 29, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions nrel/routee/powertrain/validation/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@
"link_negative_log_likelihood": "Link NLL",
"link_continuous_ranked_probability_score": "Link CRPS",
"link_prediction_interval_coverage_probability": "Link PICP",
"trip_negative_log_likelihood": "Trip NLL",
"trip_continuous_ranked_probability_score": "Trip CRPS",
"trip_prediction_interval_coverage_probability": "Trip PICP",
}


Expand Down Expand Up @@ -107,6 +110,15 @@ def calculate_picp(target, lower_bound, upper_bound) -> float:
return picp


def calculate_combined_sd(target_std) -> float:
"""
Calculate the combined standard deviation of the target.
"""
combined_variance = (target_std**2).sum()
combined_sd = np.sqrt(combined_variance)
return combined_sd


def errors_to_html_lines(errors: Errors) -> List[str]:
html_lines = []
for error_name, error_value in errors.to_dict().items():
Expand Down Expand Up @@ -138,6 +150,10 @@ class Errors:
link_continuous_ranked_probability_score: Optional[float] = None
link_prediction_interval_coverage_probability: Optional[float] = None

trip_negative_log_likelihood: Optional[float] = None
trip_continuous_ranked_probability_score: Optional[float] = None
trip_prediction_interval_coverage_probability: Optional[float] = None

@classmethod
def from_dict(self, d: dict) -> Errors:
return Errors(**d)
Expand Down Expand Up @@ -344,6 +360,29 @@ def compute_errors(
calculate_picp(target, lower_bound, upper_bound), 2
)

if trip_column in test_df.columns:
test_df["energy_pred"] = target_pred
test_df["energy_pred_std"] = target_std
gb = test_df.groupby(trip_column).agg(
{
energy_name: "sum",
"energy_pred": "sum",
"energy_pred_std": calculate_combined_sd,
}
)
lower_bound = gb["energy_pred"] - z * gb["energy_pred_std"]
upper_bound = gb["energy_pred"] + z * gb["energy_pred_std"]

errors["trip_negative_log_likelihood"] = calculate_nll(
gb[energy_name], gb["energy_pred"], gb["energy_pred_std"]
)
errors["trip_continuous_ranked_probability_score"] = calculate_crps(
gb[energy_name], gb["energy_pred"], gb["energy_pred_std"]
)
errors["trip_prediction_interval_coverage_probability"] = round(
calculate_picp(gb[energy_name], lower_bound, upper_bound), 2
)

errors["net_error"] = net_energy_error(target, target_pred)

total_dist = test_df[distance.name].sum()
Expand Down