# Usage of Measurements.jl

**URL:** <https://discourse.julialang.org/t/usage-of-measurements-jl/98726>\
**Category:** Data\
**Tags:** question, uncertainty-quantifi\
**Created:** [May 12, 2023, 8:40am UTC](https://discourse.julialang.org/t/usage-of-measurements-jl/98726 "2023-05-12T08:40:44Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Fourier](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fourier/32/38176_2.png) [@Fourier](https://discourse.julialang.org/u/Fourier)\
**Post date:** [May 12, 2023, 8:40am UTC](https://discourse.julialang.org/t/usage-of-measurements-jl/98726/1 "2023-05-12T08:40:44Z")

</div>

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

```julia
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](https://juliaphysics.github.io/Measurements.jl/stable/examples/).

Also when I have a dataset

```julia
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?

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [May 12, 2023, 8:52am UTC](https://discourse.julialang.org/t/usage-of-measurements-jl/98726/2 "2023-05-12T08:52:41Z")

</div>

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

```julia
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:

```julia
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

```

---

<div class="post-metadata">

**Author:** ![Fourier](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fourier/32/38176_2.png) [@Fourier](https://discourse.julialang.org/u/Fourier)\
**Post date:** [May 12, 2023, 9:15am UTC](https://discourse.julialang.org/t/usage-of-measurements-jl/98726/3 "2023-05-12T09:15:10Z")

</div>

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](https://en.wikipedia.org/wiki/Standard_error#Derivation) 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.

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [May 12, 2023, 9:36am UTC](https://discourse.julialang.org/t/usage-of-measurements-jl/98726/4 "2023-05-12T09:36:19Z")

</div>

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`:

```julia
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

```
