Metrics
modelskill.metrics
The metrics
module contains different skill metrics for evaluating the
difference between a model and an observation.
- bias
- max_error
- root_mean_squared_error (rmse)
- urmse
- mean_absolute_error (mae)
- mean_absolute_percentage_error (mape)
- kling_gupta_efficiency (kge)
- nash_sutcliffe_efficiency (nse)
- r2 (r2=nse)
- model_efficiency_factor (mef)
- wilmott
- scatter_index (si)
- scatter_index2
- corrcoef (cc)
- spearmanr (rho)
- lin_slope
- hit_ratio
- explained_variance (ev)
- peak_ratio (pr)
Circular metrics (for directional data with units in degrees):
- c_bias
- c_max_error
- c_mean_absolute_error (c_mae)
- c_root_mean_squared_error (c_rmse)
- c_unbiased_root_mean_squared_error (c_urmse)
The names in parentheses are shorthand aliases for the different metrics.
Examples:
>>> obs = np.array([0.3, 2.1, -1.0])
>>> mod = np.array([0.0, 2.3, 1.0])
>>> bias(obs, mod)
np.float64(0.6333333333333332)
>>> max_error(obs, mod)
np.float64(2.0)
>>> rmse(obs, mod)
np.float64(1.173314393786536)
>>> urmse(obs, mod)
np.float64(0.9877021593352702)
>>> mae(obs, mod)
np.float64(0.8333333333333331)
>>> mape(obs, mod)
np.float64(103.17460317460316)
>>> nse(obs, mod)
np.float64(0.14786795048143053)
>>> r2(obs, mod)
np.float64(0.14786795048143053)
>>> mef(obs, mod)
np.float64(0.9231099877688299)
>>> si(obs, mod)
np.float64(0.8715019052958266)
>>> spearmanr(obs, mod)
np.float64(0.5)
>>> willmott(obs, mod)
np.float64(0.7484604452865941)
>>> hit_ratio(obs, mod, a=0.5)
np.float64(0.6666666666666666)
>>> ev(obs, mod)
np.float64(0.39614855570839064)
add_metric
Adds a metric to the metric list. Useful for custom metrics.
Some metrics are dimensionless, others have the same dimension as the observations.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
metric |
str or callable
|
Metric name or function |
required |
has_units |
bool
|
True if metric has a dimension, False otherwise. Default:False |
False
|
Returns:
Type | Description |
---|---|
None
|
|
Examples:
Source code in modelskill/metrics.py
bias
Bias (mean error)
Range: \((-\infty, \infty)\); Best: 0
c_bias
Circular bias (mean error)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
obs |
ndarray
|
Observation in degrees (0, 360) |
required |
model |
ndarray
|
Model in degrees (0, 360) |
required |
Range |
|
required |
Returns:
Type | Description |
---|---|
float
|
Circular bias |
Examples:
>>> obs = np.array([10., 355., 170.])
>>> mod = np.array([20., 5., -180.])
>>> c_bias(obs, mod)
np.float64(10.0)
Source code in modelskill/metrics.py
c_mae
c_max_error
Circular max error
Parameters:
Name | Type | Description | Default |
---|---|---|---|
obs |
ndarray
|
Observation in degrees (0, 360) |
required |
model |
ndarray
|
Model in degrees (0, 360) |
required |
Range |
|
required |
Returns:
Type | Description |
---|---|
float
|
Circular max error |
Examples:
>>> obs = np.array([10., 350., 10.])
>>> mod = np.array([20., 10., 350.])
>>> c_max_error(obs, mod)
np.float64(20.0)
Source code in modelskill/metrics.py
c_mean_absolute_error
Circular mean absolute error
Parameters:
Name | Type | Description | Default |
---|---|---|---|
obs |
ndarray
|
Observation in degrees (0, 360) |
required |
model |
ndarray
|
Model in degrees (0, 360) |
required |
weights |
ndarray
|
Weights, by default None |
None
|
Range |
|
required |
Returns:
Type | Description |
---|---|
float
|
Circular mean absolute error |
Source code in modelskill/metrics.py
c_rmse
alias for circular root mean squared error
c_root_mean_squared_error
Circular root mean squared error
Parameters:
Name | Type | Description | Default |
---|---|---|---|
obs |
ndarray
|
Observation in degrees (0, 360) |
required |
model |
ndarray
|
Model in degrees (0, 360) |
required |
weights |
ndarray
|
Weights, by default None |
None
|
Range |
|
required |
Returns:
Type | Description |
---|---|
float
|
Circular root mean squared error |
Source code in modelskill/metrics.py
c_unbiased_root_mean_squared_error
Circular unbiased root mean squared error
Parameters:
Name | Type | Description | Default |
---|---|---|---|
obs |
ndarray
|
Observation in degrees (0, 360) |
required |
model |
ndarray
|
Model in degrees (0, 360) |
required |
weights |
ndarray
|
Weights, by default None |
None
|
Range |
|
required |
Returns:
Type | Description |
---|---|
float
|
Circular unbiased root mean squared error |
Source code in modelskill/metrics.py
c_urmse
alias for circular unbiased root mean squared error
cc
corrcoef
Pearson’s Correlation coefficient (CC)
Range: [-1, 1]; Best: 1
See Also
spearmanr np.corrcoef
Source code in modelskill/metrics.py
ev
explained_variance
EV: Explained variance
EV is the explained variance and measures the proportion [0 - 1] to which the model accounts for the variation (dispersion) of the observations.
In cases with no bias, EV is equal to r2
Range: [0, 1]; Best: 1
See Also
r2
Source code in modelskill/metrics.py
hit_ratio
Fraction within obs ± acceptable deviation
Range: [0, 1]; Best: 1
Examples:
>>> obs = np.array([1.0, 1.1, 1.2, 1.3, 1.4, 1.4, 1.3])
>>> model = np.array([1.02, 1.16, 1.3, 1.38, 1.49, 1.45, 1.32])
>>> hit_ratio(obs, model, a=0.05)
np.float64(0.2857142857142857)
>>> hit_ratio(obs, model, a=0.1)
np.float64(0.8571428571428571)
>>> hit_ratio(obs, model, a=0.15)
np.float64(1.0)
Source code in modelskill/metrics.py
kge
kling_gupta_efficiency
Kling-Gupta Efficiency (KGE)
where \(r\) is the pearson correlation coefficient, \(\mu_{obs},\mu_{mod}\) and \(\sigma_{obs},\sigma_{mod}\) is the mean and standard deviation of observations and model.
Range: \((-\infty, 1]\); Best: 1
References
Gupta, H. V., Kling, H., Yilmaz, K. K. and Martinez, G. F., (2009), Decomposition of the mean squared error and NSE performance criteria: Implications for improving hydrological modelling, J. Hydrol., 377(1-2), 80-91 https://doi.org/10.1016/j.jhydrol.2009.08.003
Knoben, W. J. M., Freer, J. E., and Woods, R. A. (2019) Technical note: Inherent benchmark or not? Comparing Nash–Sutcliffe and Kling–Gupta efficiency scores, Hydrol. Earth Syst. Sci., 23, 4323-4331 https://doi.org/10.5194/hess-23-4323-2019
Source code in modelskill/metrics.py
lin_slope
Slope of the regression line.
Range: \((-\infty, \infty )\); Best: 1
Source code in modelskill/metrics.py
mae
mape
max_error
Max (absolute) error
Range: \([0, \infty)\); Best: 0
mean_absolute_error
Mean Absolute Error (MAE)
Range: \([0, \infty)\); Best: 0
Source code in modelskill/metrics.py
mean_absolute_percentage_error
Mean Absolute Percentage Error (MAPE)
Range: \([0, \infty)\); Best: 0
Source code in modelskill/metrics.py
mef
metric_has_units
Check if a metric has units (dimension).
Some metrics are dimensionless, others have the same dimension as the observations.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
metric |
str or callable
|
Metric name or function |
required |
Returns:
Type | Description |
---|---|
bool
|
True if metric has a dimension, False otherwise |
Examples:
Source code in modelskill/metrics.py
model_efficiency_factor
Model Efficiency Factor (MEF)
Scale independent RMSE, standardized by Stdev of observations
Range: \([0, \infty)\); Best: 0
See Also
nash_sutcliffe_efficiency root_mean_squared_error
Source code in modelskill/metrics.py
nash_sutcliffe_efficiency
Nash-Sutcliffe Efficiency (NSE)
Range: \((-\infty, 1]\); Best: 1
Note
r2 = nash_sutcliffe_efficiency(nse)
References
Nash, J. E.; Sutcliffe, J. V. (1970). "River flow forecasting through conceptual models part I — A discussion of principles". Journal of Hydrology. 10 (3): 282–290. https://doi.org/10.1016/0022-1694(70)90255-6
Source code in modelskill/metrics.py
nse
peak_ratio
Peak Ratio
PR is the mean of the largest-N individual ratios of identified peaks in the model / identified peaks in the measurements (N number of events defined by AAP). PR is calculated only for the joint-events, ie, events that ocurr simulateneously within a window +/- 0.5*inter_event_time.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
inter_event_level |
float
|
Inter-event level threshold (default: 0.7). |
0.7
|
AAP |
Union[int, float]
|
Average Annual Peaks (ie, Number of peaks per year, on average). (default: 2) |
2
|
inter_event_time |
str
|
|
'36h'
|
Range |
|
required |
Source code in modelskill/metrics.py
543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 |
|
pr
alias for peak_ratio
Source code in modelskill/metrics.py
r2
Coefficient of determination (R2)
Pronounced 'R-squared'; the proportion of the variation in the dependent variable that is predictable from the independent variable(s), i.e. the proportion of explained variance.
Range: \((-\infty, 1]\); Best: 1
Note
r2 = nash_sutcliffe_efficiency(nse)
Examples:
>>> obs = np.array([1.0,1.1,1.2,1.3,1.4])
>>> model = np.array([1.09, 1.16, 1.3 , 1.38, 1.49])
>>> r2(obs,model)
np.float64(0.6379999999999998)
Source code in modelskill/metrics.py
rho
rmse
alias for root_mean_squared_error
root_mean_squared_error
Root Mean Squared Error (RMSE)
Unbiased version:
Range: \([0, \infty)\); Best: 0
Source code in modelskill/metrics.py
scatter_index
Scatter index (SI)
Which is the same as the unbiased-RMSE normalized by the absolute mean of the observations.
Range: \([0, \infty)\); Best: 0
Source code in modelskill/metrics.py
scatter_index2
Alternative formulation of the scatter index (SI)
Range: [0, 100]; Best: 0
Source code in modelskill/metrics.py
si
spearmanr
Spearman rank correlation coefficient
The rank correlation coefficient is similar to the Pearson correlation coefficient but applied to ranked quantities and is useful to quantify a monotonous relationship
Range: [-1, 1]; Best: 1
Examples:
>>> obs = np.linspace(-20, 20, 100)
>>> mod = np.tanh(obs)
>>> rho(obs, mod)
np.float64(0.9999759973116955)
>>> spearmanr(obs, mod)
np.float64(0.9999759973116955)
See Also
corrcoef
Source code in modelskill/metrics.py
urmse
Unbiased Root Mean Squared Error (uRMSE)
Range: \([0, \infty)\); Best: 0
See Also
root_mean_squared_error
Source code in modelskill/metrics.py
willmott
Willmott's Index of Agreement
A scaled representation of the predictive accuracy of the model against observations. A value of 1 indicates a perfect match, and 0 indicates no agreement at all.
Range: [0, 1]; Best: 1
Examples:
>>> obs = np.array([1.0, 1.1, 1.2, 1.3, 1.4, 1.4, 1.3])
>>> model = np.array([1.02, 1.16, 1.3, 1.38, 1.49, 1.45, 1.32])
>>> willmott(obs, model)
np.float64(0.9501403174479723)
References
Willmott, C. J. 1981. "On the validation of models". Physical Geography, 2, 184–194.