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

[BUG] MeanAbsoluteError does not leverage horizon_weight #6412

Closed
kdekker-private opened this issue May 13, 2024 · 4 comments · Fixed by #6495
Closed

[BUG] MeanAbsoluteError does not leverage horizon_weight #6412

kdekker-private opened this issue May 13, 2024 · 4 comments · Fixed by #6495
Labels
bug Something isn't working module:metrics&benchmarking metrics and benchmarking modules
Projects

Comments

@kdekker-private
Copy link

kdekker-private commented May 13, 2024

Describe the bug
The performance metric MeanAbsoluteError does not take into account horizon_weight when calling the class instance.
All other scikit-learn based metrics do.

To Reproduce

import numpy as np
from sktime.performance_metrics.forecasting import MeanAbsoluteError
y_true = np.array([3, -0.5, 2, 7, 2])
y_pred = np.array([2.5, 0.0, 2, 8, 1.25])
mae = MeanAbsoluteError()
mae(y_true, y_pred) != mae(y_true, y_pred, horizon_weight=np.array([0.1,0.2,0.1,0.3,0.4]))

Expected behavior
The boolean statement at the end should be true.

Additional context

Versions
0.28.0

@kdekker-private kdekker-private added the bug Something isn't working label May 13, 2024
@fkiraly
Copy link
Collaborator

fkiraly commented May 13, 2024

All other scikit-learn based metrics do.

Can you give an example of a class that does?

I was not aware of the argument being used by the classes, and wonder why it is present in the functions and not the classes.

@fkiraly fkiraly added the module:metrics&benchmarking metrics and benchmarking modules label May 13, 2024
@fkiraly fkiraly added this to Needs triage & validation in Bugfixing via automation May 13, 2024
@kdekker-private
Copy link
Author

Metrics class MeanSquaredError does.

Example:

import numpy as np
from sktime.performance_metrics.forecasting import MeanSquaredError
y_true = np.array([3, -0.5, 2, 7, 2])
y_pred = np.array([2.5, 0.0, 2, 8, 1.25])
mse = MeanSquaredError()
mse(y_true, y_pred) != mse(y_true, y_pred, horizon_weight=np.array([0.1,0.2,0.1,0.3,0.4]))

@fkiraly
Copy link
Collaborator

fkiraly commented May 27, 2024

Ok, this has puzzled me quite a bit but I now understand what is going on.

The horizon_weight parameter is an undocumented and untested yet public interface point of metrics classes. It is accessible because evaluate forwards kwargs, and some metrics relying on functions forwarded the weight param to the function (not sklearn but sktime functions).

Now, we have recently began to improve runtime performance of metrics gradually, and that refactor inadvertently removed this undocumented interface point, namely MeanAbsoluteError in 0.28.0, and MeanSquaredError in 0.29.0. Funnily, it was @marrov's request for performance improvements which caused this indirectly. Tests also did not discover this degradation of public behaviour, because it was neither known to devs nor tested.

FYI @sktime/core-developers, this is a textbook example of Hyrum's law 😁
https://www.hyrumslaw.com/

Either way, I think we should be "nice" about this, proposed resolution:

weights are actually a nice feature to have for metrics, and it does not cost much to add it to all metrics, and to add it back to the refactored metrics. For compatibility with sklearn, we should name the arg sample_weight, but redirect horizon_weight to it if passed, with a user warning.

@fkiraly
Copy link
Collaborator

fkiraly commented May 27, 2024

Question out of interest, @kdekker-private, how did you get the idea to use the horizon_weight arg in a class? Is there a tutorial or other code that does that?

fkiraly added a commit that referenced this issue May 29, 2024
…mple_weight` parameter (#6495)

Fixes #6412.

This adds a `sample_weight` argument to all `sktime` metrics, and
restores the ability to pass `horizon_weight` for downwards
compatibility.

For compatibility with `sklearn`, the publicly documented argument is
called `sample_weight`.
Bugfixing automation moved this from Needs triage & validation to Fixed/resolved May 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working module:metrics&benchmarking metrics and benchmarking modules
Projects
Bugfixing
Fixed/resolved
2 participants