Skip to content

API ​

ErrorMetrics.metric Function
julia
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 data

  • y: observation data

  • yσ: observational uncertainty data (optional; when omitted it behaves like ones(size(y)) without allocating)

Examples

julia
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.989

Metrics ​

Error-based Metrics ​

LogNMSE ​

Log Normalized Mean Squared Error: log(1 + (1 - NSE)), zero for a perfect fit, with a gradient that fades more slowly for bad fits than that of NNSEInv

LogNMSE=log⁡(1+(1−NSE))
Code
julia
function metric(::LogNMSE, ŷ::AbstractArray, y::AbstractArray, yσ::AbstractArray)
    return log1p(metric(y, yσ, ŷ, NSEInv()))
end

MSE ​

Mean Squared Error: Measures the average squared difference between predicted and observed values

MSE=1n∑i=1n(yi−y^i)2
Code
julia
function metric(::MSE, ŷ::AbstractArray, y::AbstractArray, yσ::AbstractArray)
    return mean(abs2.(y .- ŷ))
end

NAME1R ​

Normalized Absolute Mean Error with 1/R scaling: Measures the absolute difference between means normalized by the range of observations

NAME1R=|y^¯−y¯|1+y¯
Code
julia
function metric(::NAME1R, ŷ::AbstractArray, y::AbstractArray, yσ::AbstractArray)
    μ_y = mean(y)
        μ_ŷ = mean(ŷ)
        NMAE1R = abs(μ_ŷ - μ_y) / (one(eltype(ŷ)) + μ_y)
        return NMAE1R
end

NMAE1R ​

Normalized Mean Absolute Error with 1/R scaling: Measures the average absolute error normalized by the range of observations

NMAE1R=1n∑i=1n|yi−y^i|1+y¯
Code
julia
function metric(::NMAE1R, ŷ::AbstractArray, y::AbstractArray, yσ::AbstractArray)
    μ_y = mean(y)
        NMAE1R = sumLazy((a, b) -> abs(a - b), ŷ, y) / length(y) / (one(eltype(ŷ)) + μ_y)
        return NMAE1R
end

NMAE1RBounded ​

Bounded Normalized Mean Absolute Error: NMAE1R mapped to [0, 1) as x / (1 + x), so that a very large error cannot dominate a sum of metrics

NMAE1RBounded=NMAE1R1+NMAE1R
Code
julia
function metric(::NMAE1RBounded, ŷ::AbstractArray, y::AbstractArray, yσ::AbstractArray)
    x = metric(y, yσ, ŷ, NMAE1R())
        return x / (one(x) + x)
end

Nash-Sutcliffe Efficiency Metrics ​

NNSE ​

Normalized Nash-Sutcliffe Efficiency: Measures model performance relative to the mean of observations, normalized to [0,1] range

NNSE=11+(1−NSE)=12−NSE
Code
julia
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
end

NNSEInv ​

Inverse Normalized Nash-Sutcliffe Efficiency: Inverse of NNSE for minimization problems, normalized to [0,1] range

NNSEInv=1−NNSE
Code
julia
function metric(::NNSEInv, ŷ::AbstractArray, y::AbstractArray, yσ::AbstractArray)
    NNSEInv = one(eltype(ŷ)) - metric(y, yσ, ŷ, NNSE())
        return NNSEInv
end

NNSEσ ​

Normalized Nash-Sutcliffe Efficiency with uncertainty: Incorporates observation uncertainty in the normalized performance measure

NNSEσ=11+(1−NSEσ)=12−NSEσ
Code
julia
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
end

NNSEσInv ​

Inverse Normalized Nash-Sutcliffe Efficiency with uncertainty: Inverse of NNSEσ for minimization problems

NNSEσInv=1−NNSEσ
Code
julia
function metric(::NNSEσInv, ŷ::AbstractArray, y::AbstractArray, yσ::AbstractArray)
    NNSEInv = one(eltype(ŷ)) - metric(y, yσ, ŷ, NNSEσ())
        return NNSEInv
end

NSE ​

Nash-Sutcliffe Efficiency: Measures model performance relative to the mean of observations

NSE=1−∑i=1n(yi−y^i)2∑i=1n(yi−y¯)2
Code
julia
function metric(::NSE, ŷ::AbstractArray, y::AbstractArray, yσ::AbstractArray)
    μ_y = mean(y)
        NSE = one(eltype(ŷ)) - sumLazy((a, b) -> abs2(a - b), y, ŷ) / sumLazy(a -> abs2(a - μ_y), y)
        return NSE
end

NSEInv ​

Inverse Nash-Sutcliffe Efficiency: Inverse of NSE for minimization problems

NSEInv=1−NSE
Code
julia
function metric(::NSEInv, ŷ::AbstractArray, y::AbstractArray, yσ::AbstractArray)
    NSEInv = one(eltype(ŷ)) - metric(y, yσ, ŷ, NSE())
        return NSEInv
end

NSEσ ​

Nash-Sutcliffe Efficiency with uncertainty: Incorporates observation uncertainty in the performance measure

NSEσ=1−∑i=1n(yi−y^iσi)2∑i=1n(yi−y¯σi)2
Code
julia
function metric(::NSEσ, ŷ::AbstractArray, y::AbstractArray, yσ::AbstractArray)
    μ_y = mean(y)
        NSE =
            one(eltype(ŷ)) -
            sumLazy((a, b, σ) -> abs2((a - b) / σ), y, ŷ, yσ) /
            sumLazy((a, σ) -> abs2((a - μ_y) / σ), y, yσ)
        return NSE
end

NSEσInv ​

Inverse Nash-Sutcliffe Efficiency with uncertainty: Inverse of NSEσ for minimization problems

NSEσInv=1−NSEσ
Code
julia
function metric(::NSEσInv, ŷ::AbstractArray, y::AbstractArray, yσ::AbstractArray)
    NSEInv = one(eltype(ŷ)) - metric(y, yσ, ŷ, NSEσ())
        return NSEInv
end

Correlation-based Metrics ​

NPcor ​

Normalized Pearson Correlation: Measures linear correlation between predictions and observations, normalized to [0,1] range

NPcor=11+(1−r)=12−r
Code
julia
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
end

NPcorInv ​

Inverse Normalized Pearson Correlation: Inverse of NPcor for minimization problems

NPcorInv=1−NPcor
Code
julia
function metric(::NPcorInv, ŷ::AbstractArray, y::AbstractArray, yσ::AbstractArray)
    n_r = metric(y, yσ, ŷ, NPcor())
        return one(n_r) - n_r
end

Pcor ​

Pearson Correlation: Measures linear correlation between predictions and observations

r=∑i=1n(yi−y¯)(y^i−y^¯)∑i=1n(yi−y¯)2∑i=1n(y^i−y^¯)2
Code
julia
function metric(::Pcor, ŷ::AbstractArray, y::AbstractArray, yσ::AbstractArray)
    return cor(y[:], ŷ[:])
end

Pcor2 ​

Squared Pearson Correlation: Measures the strength of linear relationship between predictions and observations

r2=(∑i=1n(yi−y¯)(y^i−y^¯)∑i=1n(yi−y¯)2∑i=1n(y^i−y^¯)2)2
Code
julia
function metric(::Pcor2, ŷ::AbstractArray, y::AbstractArray, yσ::AbstractArray)
    r = metric(y, yσ, ŷ, Pcor())
        return r * r
end

Pcor2Inv ​

Inverse Squared Pearson Correlation: Inverse of Pcor2 for minimization problems

rInv2=1−r2
Code
julia
function metric(::Pcor2Inv, ŷ::AbstractArray, y::AbstractArray, yσ::AbstractArray)
    r2Inv = one(eltype(ŷ)) - metric(y, yσ, ŷ, Pcor2())
        return r2Inv
end

PcorInv ​

Inverse Pearson Correlation: Inverse of Pcor for minimization problems

rInv=1−r
Code
julia
function metric(::PcorInv, ŷ::AbstractArray, y::AbstractArray, yσ::AbstractArray)
    rInv = one(eltype(ŷ)) - metric(y, yσ, ŷ, Pcor())
        return rInv
end

Rank Correlation Metrics ​

NScor ​

Normalized Spearman Correlation: Measures monotonic relationship between predictions and observations, normalized to [0,1] range

NScor=11+(1−ρ)=12−ρ
Code
julia
function metric(::NScor, ŷ::AbstractArray, y::AbstractArray, yσ::AbstractArray)
    ρ = corspearman(y, ŷ)
        one_ρ = one(ρ)
        n_ρ = one_ρ / (one_ρ + one_ρ -ρ)
        return n_ρ
end

NScorInv ​

Inverse Normalized Spearman Correlation: Inverse of NScor for minimization problems

NScorInv=1−NScor
Code
julia
function metric(::NScorInv, ŷ::AbstractArray, y::AbstractArray, yσ::AbstractArray)
    n_ρ = metric(y, yσ, ŷ, NScor())
        return one(n_ρ) - n_ρ
end

Scor ​

Spearman Correlation: Measures monotonic relationship between predictions and observations

ρ=Spearman(y,y^)
Code
julia
function metric(::Scor, ŷ::AbstractArray, y::AbstractArray, yσ::AbstractArray)
    return corspearman(y[:], ŷ[:])
end

Scor2 ​

Squared Spearman Correlation: Measures the strength of monotonic relationship between predictions and observations

ρ2=(Spearman(y,y^))2
Code
julia
function metric(::Scor2, ŷ::AbstractArray, y::AbstractArray, yσ::AbstractArray)
    ρ = metric(y, yσ, ŷ, Scor())
        return ρ * ρ
end

Scor2Inv ​

Inverse Squared Spearman Correlation: Inverse of Scor2 for minimization problems

ρInv2=1−ρ2
Code
julia
function metric(::Scor2Inv, ŷ::AbstractArray, y::AbstractArray, yσ::AbstractArray)
    ρ2Inv = one(eltype(ŷ)) - metric(y, yσ, ŷ, Scor2())
        return ρ2Inv
end

ScorInv ​

Inverse Spearman Correlation: Inverse of Scor for minimization problems

ρInv=1−ρ
Code
julia
function metric(::ScorInv, ŷ::AbstractArray, y::AbstractArray, yσ::AbstractArray)
    ρInv = one(eltype(ŷ)) - metric(y, yσ, ŷ, Scor())
        return ρInv
end

ErrorMetrics.jl - Error and performance metrics for model evaluation