# Usage of Measurements.jl

I want to use measurements.jl for my data analysis, because it can propagate uncertainites.
However for example this input:

julia> 1 ± 1 * 1 ± 0.5
1.0 ± 1.0 ± 0.5 ± 0.0


gives me an answer I did not expect, I thought that measurements.jl would do the error propagation for me, as explained in the documentation.

Also when I have a dataset

julia> x = measurement.(randn(10))
10-element Vector{Measurement{Float64}}:
-0.33523444126034807 ± 0.0
1.0859473014454655 ± 0.0
0.1321432397700558 ± 0.0
-0.7874950926044291 ± 0.0
-0.6331084102037436 ± 0.0
1.082620131744912 ± 0.0
-0.7920111784770187 ± 0.0
-0.5497645412145613 ± 0.0
0.10080953078817759 ± 0.0
0.03496576346443183 ± 0.0
julia> using Statistics

julia> mean(x)
-0.06611276965470582 ± 0.0


it doesnt seem to propagate the uncerainty for the mean.
What am I doing wrong here?

Not sure about how arithmetic for Measurements works, but it looks like the first one is just operator precedence:

In [9]: (1 ± 1) * (1 ± 0.5)
1.0 ± 1.1


the second one is propagating uncertainty correctly as far as I can tell - the error for each measurement is zero, so why would you expect there to be an error in the mean? If the measurements actually do have uncertainty then that flows through when calculating the mean:

In [15]: x = measurement.(rand(5), rand(5))
5-element Vector{Measurement{Float64}}:
0.61 ± 0.63
0.77 ± 0.44
0.5 ± 0.43
0.8 ± 0.82
0.32 ± 0.11

In [16]: mean(x)
0.6 ± 0.24

1 Like

I thought, that when I take the mean of a population x of n unrelated variables, I would get \langle x \rangle \pm \frac{\sigma(x)}{\sqrt n}.

But now I am looking at the derivation on wikipedia and they are saying that when I take the sum T = \sum_{i} x_{i} then \operatorname{Var}(T) \approx\left(\operatorname{Var}\left(x_1\right)+\operatorname{Var}\left(x_2\right)+\cdots+\operatorname{Var}\left(x_n\right)\right)=n \sigma^2, which means that I would need to first take the standard deviation of my values and apply it to all of the measurements as an uncertainty and then I get the SEM, when I take their mean.
Even though \sigma is the standard deviation of x, so I had assumed that it would work without that, but looking at the derivation, I guess thats wrong.

Ok, you are thinking about the standard error of the sample mean as an estimator of a population mean, but that’s not what Measurements.jl is about:

Measurements.jl relieves you from the hassle of propagating uncertainties coming from physical measurements, when performing mathematical operations involving them. The linear error propagation theory is employed to propagate the errors.

You might be interested in the sem (standard error of mean) function in StatsBase:

In [26]: using StatsBase

In [27]: mean(sem(rand(10)) for _ in 1:10_000)
0.09006180977433016

# Increasing sample size by 4x should decrease se by factor of √4 = 2
In [28]: mean(sem(rand(40)) for _ in 1:10_000)
0.045502169617763155

1 Like