API
ErrorMetrics.metric Function
metric(m::ErrorMetric, ŷ::AbstractArray, y::AbstractArray, yσ::AbstractArray)
metric(m::ErrorMetric, ŷ::AbstractArray, y::AbstractArray)
metric(m::ErrorMetric, ŷ::AbstractArray, y::AbstractArray, yσ::AbstractArray)
metric(m::ErrorMetric, ŷ::AbstractArray, y::AbstractArray)calculate the performance/loss metric for given observation and model simulation data stream
Returns:
metric: The calculated metric value
Arguments:
m: ErrorMetric type specifying which metric to calculateŷ: model simulation datay: observation datayσ: observational uncertainty data (optional; when omitted it behaves likeones(size(y))without allocating)
Examples
julia> using ErrorMetrics
julia> y = [1.0, 2.0, 3.0, 4.0, 5.0]
5-element Vector{Float64}:
1.0
2.0
3.0
4.0
5.0
julia> ŷ = [1.1, 2.2, 2.9, 4.1, 4.8]
5-element Vector{Float64}:
1.1
2.2
2.9
4.1
4.8
julia> metric(MSE(), ŷ, y)
0.02200000000000002
julia> yσ = [0.1, 0.1, 0.1, 0.1, 0.1]
5-element Vector{Float64}:
0.1
0.1
0.1
0.1
0.1
julia> metric(NSEσ(), ŷ, y, yσ)
0.989
julia> metric(Pcor(), ŷ, y)
0.9966065527770355
julia> metric(NSE(), ŷ, y)
0.989Metrics
Error-based Metrics
MSE
Mean Squared Error: Measures the average squared difference between predicted and observed values
Code
function metric(::MSE, ŷ::AbstractArray, y::AbstractArray, yσ::AbstractArray)
return mean(abs2.(y .- ŷ))
endNAME1R
Normalized Absolute Mean Error with 1/R scaling: Measures the absolute difference between means normalized by the range of observations
Code
function metric(::NAME1R, ŷ::AbstractArray, y::AbstractArray, yσ::AbstractArray)
μ_y = mean(y)
μ_ŷ = mean(ŷ)
NMAE1R = abs(μ_ŷ - μ_y) / (one(eltype(ŷ)) + μ_y)
return NMAE1R
endNMAE1R
Normalized Mean Absolute Error with 1/R scaling: Measures the average absolute error normalized by the range of observations
Code
function metric(::NMAE1R, ŷ::AbstractArray, y::AbstractArray, yσ::AbstractArray)
μ_y = mean(y)
NMAE1R = mean(abs.(ŷ - y)) / (one(eltype(ŷ)) + μ_y)
return NMAE1R
endNash-Sutcliffe Efficiency Metrics
NNSE
Normalized Nash-Sutcliffe Efficiency: Measures model performance relative to the mean of observations, normalized to [0,1] range
Code
function metric(::NNSE, ŷ::AbstractArray, y::AbstractArray, yσ::AbstractArray)
NSE_v = metric(y, yσ, ŷ, NSE())
NNSE = one(eltype(ŷ)) / (one(eltype(ŷ)) + one(eltype(ŷ)) - NSE_v)
return NNSE
endNNSEInv
Inverse Normalized Nash-Sutcliffe Efficiency: Inverse of NNSE for minimization problems, normalized to [0,1] range
Code
function metric(::NNSEInv, ŷ::AbstractArray, y::AbstractArray, yσ::AbstractArray)
NNSEInv = one(eltype(ŷ)) - metric(y, yσ, ŷ, NNSE())
return NNSEInv
endNNSEσ
Normalized Nash-Sutcliffe Efficiency with uncertainty: Incorporates observation uncertainty in the normalized performance measure
Code
function metric(::NNSEσ, ŷ::AbstractArray, y::AbstractArray, yσ::AbstractArray)
NSE_v = metric(y, yσ, ŷ, NSEσ())
NNSE = one(eltype(ŷ)) / (one(eltype(ŷ)) + one(eltype(ŷ)) - NSE_v)
return NNSE
endNNSEσInv
Inverse Normalized Nash-Sutcliffe Efficiency with uncertainty: Inverse of NNSEσ for minimization problems
Code
function metric(::NNSEσInv, ŷ::AbstractArray, y::AbstractArray, yσ::AbstractArray)
NNSEInv = one(eltype(ŷ)) - metric(y, yσ, ŷ, NNSEσ())
return NNSEInv
endNSE
Nash-Sutcliffe Efficiency: Measures model performance relative to the mean of observations
Code
function metric(::NSE, ŷ::AbstractArray, y::AbstractArray, yσ::AbstractArray)
NSE = one(eltype(ŷ)) .- sum(abs2.((y .- ŷ))) / sum(abs2.((y .- mean(y))))
return NSE
endNSEInv
Inverse Nash-Sutcliffe Efficiency: Inverse of NSE for minimization problems
Code
function metric(::NSEInv, ŷ::AbstractArray, y::AbstractArray, yσ::AbstractArray)
NSEInv = one(eltype(ŷ)) - metric(y, yσ, ŷ, NSE())
return NSEInv
endNSEσ
Nash-Sutcliffe Efficiency with uncertainty: Incorporates observation uncertainty in the performance measure
Code
function metric(::NSEσ, ŷ::AbstractArray, y::AbstractArray, yσ::AbstractArray)
NSE =
one(eltype(ŷ)) .-
sum(abs2.((y .- ŷ) ./ yσ)) /
sum(abs2.((y .- mean(y)) ./ yσ))
return NSE
endNSEσInv
Inverse Nash-Sutcliffe Efficiency with uncertainty: Inverse of NSEσ for minimization problems
Code
function metric(::NSEσInv, ŷ::AbstractArray, y::AbstractArray, yσ::AbstractArray)
NSEInv = one(eltype(ŷ)) - metric(y, yσ, ŷ, NSEσ())
return NSEInv
endCorrelation-based Metrics
NPcor
Normalized Pearson Correlation: Measures linear correlation between predictions and observations, normalized to [0,1] range
Code
function metric(::NPcor, ŷ::AbstractArray, y::AbstractArray, yσ::AbstractArray)
r = cor(y, ŷ)
one_r = one(r)
n_r = one_r / (one_r + one_r -r)
return n_r
endNPcorInv
Inverse Normalized Pearson Correlation: Inverse of NPcor for minimization problems
Code
function metric(::NPcorInv, ŷ::AbstractArray, y::AbstractArray, yσ::AbstractArray)
n_r = metric(y, yσ, ŷ, NPcor())
return one(n_r) - n_r
endPcor
Pearson Correlation: Measures linear correlation between predictions and observations
Code
function metric(::Pcor, ŷ::AbstractArray, y::AbstractArray, yσ::AbstractArray)
return cor(y[:], ŷ[:])
endPcor2
Squared Pearson Correlation: Measures the strength of linear relationship between predictions and observations
Code
function metric(::Pcor2, ŷ::AbstractArray, y::AbstractArray, yσ::AbstractArray)
r = metric(y, yσ, ŷ, Pcor())
return r * r
endPcor2Inv
Inverse Squared Pearson Correlation: Inverse of Pcor2 for minimization problems
Code
function metric(::Pcor2Inv, ŷ::AbstractArray, y::AbstractArray, yσ::AbstractArray)
r2Inv = one(eltype(ŷ)) - metric(y, yσ, ŷ, Pcor2())
return r2Inv
endPcorInv
Inverse Pearson Correlation: Inverse of Pcor for minimization problems
Code
function metric(::PcorInv, ŷ::AbstractArray, y::AbstractArray, yσ::AbstractArray)
rInv = one(eltype(ŷ)) - metric(y, yσ, ŷ, Pcor())
return rInv
endRank Correlation Metrics
NScor
Normalized Spearman Correlation: Measures monotonic relationship between predictions and observations, normalized to [0,1] range
Code
function metric(::NScor, ŷ::AbstractArray, y::AbstractArray, yσ::AbstractArray)
ρ = corspearman(y, ŷ)
one_ρ = one(ρ)
n_ρ = one_ρ / (one_ρ + one_ρ -ρ)
return n_ρ
endNScorInv
Inverse Normalized Spearman Correlation: Inverse of NScor for minimization problems
Code
function metric(::NScorInv, ŷ::AbstractArray, y::AbstractArray, yσ::AbstractArray)
n_ρ = metric(y, yσ, ŷ, NScor())
return one(n_ρ) - n_ρ
endScor
Spearman Correlation: Measures monotonic relationship between predictions and observations
Code
function metric(::Scor, ŷ::AbstractArray, y::AbstractArray, yσ::AbstractArray)
return corspearman(y[:], ŷ[:])
endScor2
Squared Spearman Correlation: Measures the strength of monotonic relationship between predictions and observations
Code
function metric(::Scor2, ŷ::AbstractArray, y::AbstractArray, yσ::AbstractArray)
ρ = metric(y, yσ, ŷ, Scor())
return ρ * ρ
endScor2Inv
Inverse Squared Spearman Correlation: Inverse of Scor2 for minimization problems
Code
function metric(::Scor2Inv, ŷ::AbstractArray, y::AbstractArray, yσ::AbstractArray)
ρ2Inv = one(eltype(ŷ)) - metric(y, yσ, ŷ, Scor2())
return ρ2Inv
endScorInv
Inverse Spearman Correlation: Inverse of Scor for minimization problems
Code
function metric(::ScorInv, ŷ::AbstractArray, y::AbstractArray, yσ::AbstractArray)
ρInv = one(eltype(ŷ)) - metric(y, yσ, ŷ, Scor())
return ρInv
end