-
Notifications
You must be signed in to change notification settings - Fork 12
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
Add Kling-Gupta Efficiency #172
Changes from 3 commits
309d5da
f12eb10
1edff52
db45795
a01283e
790de75
5a777b3
e9a141d
9e69e98
a89c779
070cf53
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
__version__ = "1.1.3" | ||
__version__ = "1.2.3" | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,6 +22,7 @@ | |
- equitable_threat_score | ||
- mean_squared_error | ||
- nash_sutcliffe_efficiency | ||
- kling_gupta_efficiency | ||
|
||
""" | ||
|
||
|
@@ -66,7 +67,7 @@ def nash_sutcliffe_efficiency( | |
log: bool = False, | ||
normalized: bool = False | ||
) -> float: | ||
"""Compute the Nash–Sutcliffe model efficiency coefficient (NSE), also called the | ||
"""Compute the Nash-Sutcliffe model efficiency coefficient (NSE), also called the | ||
mean squared error skill score or the R^2 (coefficient of determination) regression score. | ||
|
||
Parameters | ||
|
@@ -112,6 +113,62 @@ def nash_sutcliffe_efficiency( | |
return 1.0 / (1.0 + numerator/denominator) | ||
return 1.0 - numerator/denominator | ||
|
||
def kling_gupta_efficiency( | ||
y_true: npt.ArrayLike, | ||
y_pred: npt.ArrayLike, | ||
r_scale: float = 1.0, | ||
a_scale: float = 1.0, | ||
b_scale: float = 1.0 | ||
) -> float: | ||
"""Compute the Kling-Gupta model efficiency coefficient (KGE). | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should any software assumptions be stated here? Something like: "This function assumes two sorted arrays of equal length." I would also want to add something that conveys that There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It might be worth mentioning what the shape of the array's are assumed to be. i.e. this method should only be applied to 1 dimensional arrays. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This made this realize, the docstring description for these doesn't make any sense. I copied these from There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Updated docstrings and added new input array validation methods for NSE and KGE. These could certainly be way more rigorous and exhaustive, but it's a start. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note: the parameters descriptions explicitly state the expected shape ( |
||
|
||
Parameters | ||
---------- | ||
y_true: array-like of shape (n_samples,) or (n_samples, n_outputs) | ||
Ground truth (correct) target values, also called observations, measurements, or observed values. | ||
y_pred: pandas.Series, required | ||
Estimated target values, also called simulations or modeled values. | ||
r_scale: float, optional, default 1.0 | ||
Linear correlation (r) scaling factor. Used to re-scale the Euclidean space by | ||
emphasizing different KGE components. | ||
a_scale: float, optional, default 1.0 | ||
Relative variability (alpha) scaling factor. Used to re-scale the Euclidean space by | ||
emphasizing different KGE components. | ||
b_scale: float, optional, default 1.0 | ||
Relative mean (beta) scaling factor. Used to re-scale the Euclidean space by | ||
emphasizing different KGE components. | ||
|
||
Returns | ||
------- | ||
score: float | ||
Kling-Gupta efficiency. | ||
|
||
References | ||
---------- | ||
Gupta, H. V., Kling, H., Yilmaz, K. K., & Martinez, G. F. (2009). Decomposition of | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. on a side note, it would be "cool" if you could get the citation for the paper in bibtex or some other format. I am thinking a decorator that wraps each metric function and contains the bibtex formatted citation. You could access it like: from hydrotools.metrics import metrics
print(metrics.kling_gupta_efficiency.citation(format="bibtex"))
@article{Gupta2009,
doi = {10.1016/j.jhydrol.2009.08.003},
url = {https://doi.org/10.1016/j.jhydrol.2009.08.003},
year = {2009},
month = oct,
publisher = {Elsevier {BV}},
volume = {377},
number = {1-2},
pages = {80--91},
author = {Hoshin V. Gupta and Harald Kling and Koray K. Yilmaz and Guillermo F. Martinez},
title = {Decomposition of the mean squared error and {NSE} performance criteria: Implications for improving hydrological modelling},
journal = {Journal of Hydrology}
} There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. added There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That would be cool. Let's discuss that at next hydrotools meeting! |
||
the mean squared error and NSE performance criteria: Implications for improving | ||
hydrological modelling. Journal of hydrology, 377(1-2), 80-91. | ||
|
||
""" | ||
# Pearson correlation coefficient | ||
r = np.corrcoef(y_pred, y_true)[0,1] | ||
|
||
# Relative variability | ||
a = np.std(y_pred) / np.std(y_true) | ||
|
||
# Relative mean | ||
b = np.mean(y_pred) / np.mean(y_true) | ||
|
||
# Scaled Euclidean distance | ||
EDs = np.sqrt( | ||
(r_scale * (r - 1.0)) ** 2.0 + | ||
(a_scale * (a - 1.0)) ** 2.0 + | ||
(b_scale * (b - 1.0)) ** 2.0 | ||
) | ||
|
||
# Return KGE | ||
return 1.0 - EDs | ||
|
||
def compute_contingency_table( | ||
observed: pd.Series, | ||
simulated: pd.Series, | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
shouldnt the version bump be to
1.2.0
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes. Thanks!