diff --git a/src/regression.jl b/src/regression.jl index 0cbc26c..69f8c41 100644 --- a/src/regression.jl +++ b/src/regression.jl @@ -198,27 +198,16 @@ function mswd(μ::Collection{T}, σ::Collection; chauvenet=false) where {T} return χ² / (length(μ)-1) end -function mswd(x::Collection{Measurement{T}}; chauvenet=false) where {T} +function mswd(x::AbstractVector{Measurement{T}}; chauvenet=false) where {T} if chauvenet not_outliers = chauvenet_func(val.(x), err.(x)) x = x[not_outliers] end - sum_of_values = sum_of_weights = χ² = zero(float(T)) - - @inbounds for i in eachindex(x) - w = 1 / err(x[i])^2 - sum_of_values += w * val(x[i]) - sum_of_weights += w - end - wx = sum_of_values / sum_of_weights - - @inbounds for i in eachindex(x) - χ² += (val(x[i]) - wx)^2 / err(x[i])^2 - end + wμ, wσ, mswd = wmean(val.(x), Measurements.cov(x)) - return χ² / (length(x)-1) + return mswd end ## --- Simple linear regression diff --git a/test/runtests.jl b/test/runtests.jl index b7a85a6..382a533 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -310,6 +310,9 @@ module BaseTests @test wm.val ≈ 752.2453179272093 @test wm.err ≈ 1.4781473739306696 @test m ≈ 13.15644886325888 + + m = mswd(age68.(analyses[1:10])) + @test m ≈ 13.15644886325888 end @testset "Concordia Metropolis" begin