JuliaPhysics / Measurements.jl

Error propagation calculator and library for physical measurements. It supports real and complex numbers with uncertainty, arbitrary precision calculations, operations with arrays, and numerical integration.

Home Page:https://juliaphysics.github.io/Measurements.jl/stable/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Bizarre behavior with `Statistics.mean`.

bensetterholm opened this issue · comments

I have two identical arrays generated in different ways which produce different means. What is going on here?

julia> using Measurements, Statistics

julia> a = fill(1.0 ± 0.5, 2)
2-element Array{Measurement{Float64},1}:
 1.0 ± 0.5
 1.0 ± 0.5

julia> b = [1.0 ± 0.5, 1.0 ± 0.5]
2-element Array{Measurement{Float64},1}:
 1.0 ± 0.5
 1.0 ± 0.5

julia> a == b
true

julia> mean(a)
1.0 ± 0.5

julia> mean(b)
1.0 ± 0.35

julia> mean(a) == mean(b)
false

(@v1.5) pkg> st Measurements
Status `~/.julia/environments/v1.5/Project.toml`
  [eff96d63] Measurements v2.2.1

You're missing correlation between variables: https://juliaphysics.github.io/Measurements.jl/stable/usage/#Correlation-Between-Variables-1. a has the same measurement twice, b has two distinct measurements. See also #47

To elaborate a bit:

julia> using Measurements

julia> a = fill(1.0 ± 0.5, 2)
2-element Array{Measurement{Float64},1}:
 1.0 ± 0.5
 1.0 ± 0.5

julia> a[1] === a[2]
true

julia> sum(a)
2.0 ± 1.0

julia> b = [1.0 ± 0.5, 1.0 ± 0.5]
2-element Array{Measurement{Float64},1}:
 1.0 ± 0.5
 1.0 ± 0.5

julia> b[1] === b[2]
false

julia> sum(b)
2.0 ± 0.71

a[1] === a[2] shows that the two quantities are exactly the same measurements and their sum must take this into account, while the fact that b[1] !== b[2] means that the two quantities are distinct measurements, and the uncertainty of their sum is simply the quadrature sum of their uncertainties.

You can understand that the mean of a is the same as that of each element of the vector because they're just all the same measurement, so you don't get more insight by taking the mean of the same measurement repeated multiple times.

Thank you very much, this explains the tag property, of which I found the discussion of in the Appendix.