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

  • : 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>= [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

MSE

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

MSE=1ni=1n(yiy^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=1ni=1n|yiy^i|1+y¯
Code
julia
function metric(::NMAE1R, ŷ::AbstractArray, y::AbstractArray, yσ::AbstractArray)
    μ_y = mean(y)
        NMAE1R = mean(abs.(ŷ - y)) / (one(eltype(ŷ)) + μ_y)
        return NMAE1R
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+(1NSE)=12NSE
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=1NNSE
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+(1NSEσ)=12NSEσ
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=1NNSEσ
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=1i=1n(yiy^i)2i=1n(yiy¯)2
Code
julia
function metric(::NSE, ŷ::AbstractArray, y::AbstractArray, yσ::AbstractArray)
    NSE = one(eltype(ŷ)) .- sum(abs2.((y .- ŷ))) / sum(abs2.((y .- mean(y))))
        return NSE
end

NSEInv

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

NSEInv=1NSE
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σ=1i=1n(yiy^iσi)2i=1n(yiy¯σi)2
Code
julia
function metric(::NSEσ, ŷ::AbstractArray, y::AbstractArray, yσ::AbstractArray)
    NSE =
            one(eltype(ŷ)) .-
            sum(abs2.((y .- ŷ) ./ yσ)) /
            sum(abs2.((y .- mean(y)) ./ yσ))
        return NSE
end

NSEσInv

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

NSEσInv=1NSEσ
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+(1r)=12r
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=1NPcor
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(yiy¯)(y^iy^¯)i=1n(yiy¯)2i=1n(y^iy^¯)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(yiy¯)(y^iy^¯)i=1n(yiy¯)2i=1n(y^iy^¯)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=1r2
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=1r
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=1NScor
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