# Does Julia have anything like Measurements.jl for multivariate error propagation?

**URL:** https://discourse.julialang.org/t/does-julia-have-anything-like-measurements-jl-for-multivariate-error-propagation/103879
**Category:** General Usage
**Tags:** uncertainty-quantifi
**Created:** [September 15, 2023, 6:18am UTC](https://discourse.julialang.org/t/does-julia-have-anything-like-measurements-jl-for-multivariate-error-propagation/103879 "2023-09-15T06:18:02Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![marius311](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/marius311/32/3953_2.png) [@marius311](https://discourse.julialang.org/u/marius311)
#### Post date: [September 15, 2023, 6:18am UTC](https://discourse.julialang.org/t/does-julia-have-anything-like-measurements-jl-for-multivariate-error-propagation/103879/1 "2023-09-15T06:18:02Z")

</div>

Say I have two variables `a` and `b` and I know the full 2x2 covariance between them. Is there a Julia package to conveniently propagate Gaussian uncertainy through arbitrary functions of them in the same way that Measurements.jl works for single variables? Like I want:

```julia
(a,b) = multivariate_measurement([10, 12], [1 0.5; 0.5 1]) # mean, covariance
b^2 / a # 14.4 ± 2.1 

```

Or is there some way to do this in Measurements already?

---

<div class="post-metadata">

### Author: ![gdalle](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gdalle/32/27854_2.png) [@gdalle](https://discourse.julialang.org/u/gdalle)
#### Post date: [September 15, 2023, 6:19am UTC](https://discourse.julialang.org/t/does-julia-have-anything-like-measurements-jl-for-multivariate-error-propagation/103879/2 "2023-09-15T06:19:27Z")

</div>

Maybe MonteCarloMeasurements.jl?

> **[GitHub - baggepinnen/MonteCarloMeasurements.jl: Propagation of distributions...](https://github.com/baggepinnen/MonteCarloMeasurements.jl)**
>
> Propagation of distributions by Monte-Carlo sampling: Real number types with uncertainty represented by samples. - GitHub - baggepinnen/MonteCarloMeasurements.jl: Propagation of distributions by Mo...

---

<div class="post-metadata">

### Author: ![marius311](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/marius311/32/3953_2.png) [@marius311](https://discourse.julialang.org/u/marius311)
#### Post date: [September 15, 2023, 6:25am UTC](https://discourse.julialang.org/t/does-julia-have-anything-like-measurements-jl-for-multivariate-error-propagation/103879/3 "2023-09-15T06:25:06Z")

</div>

That was fast 😆 🙏

```julia
julia> a, b = Particles(10000, MvNormal([10,12], [1 0.5; 0.5 1]))
2-element Vector{Particles{Float64, 10000}}:
 10.0 ± 1.0
 12.0 ± 1.0

julia> b^2 / a
14.4971 ± 2.11 Particles{Float64, 10000}

```

---

<div class="post-metadata">

### Author: ![gdalle](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gdalle/32/27854_2.png) [@gdalle](https://discourse.julialang.org/u/gdalle)
#### Post date: [September 15, 2023, 6:25am UTC](https://discourse.julialang.org/t/does-julia-have-anything-like-measurements-jl-for-multivariate-error-propagation/103879/4 "2023-09-15T06:25:43Z")

</div>

Beware though, it’s particle-based and not formula-based (at least I think so)

---

<div class="post-metadata">

### Author: ![marius311](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/marius311/32/3953_2.png) [@marius311](https://discourse.julialang.org/u/marius311)
#### Post date: [September 15, 2023, 6:27am UTC](https://discourse.julialang.org/t/does-julia-have-anything-like-measurements-jl-for-multivariate-error-propagation/103879/5 "2023-09-15T06:27:27Z")

</div>

Will keep in mind, I think I’m in an ok regime though. I guess I would be interested still if there’s some AD-like formula-based thing.

---

<div class="post-metadata">

### Author: ![gdalle](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gdalle/32/27854_2.png) [@gdalle](https://discourse.julialang.org/u/gdalle)
#### Post date: [September 15, 2023, 6:30am UTC](https://discourse.julialang.org/t/does-julia-have-anything-like-measurements-jl-for-multivariate-error-propagation/103879/6 "2023-09-15T06:30:17Z")

</div>

If you ever see something like that, let me know. If not, I’d be interested in coding it, in case you wanna help?

---

<div class="post-metadata">

### Author: ![DrPapa](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/drpapa/32/6835_2.png) [@DrPapa](https://discourse.julialang.org/u/DrPapa)
#### Post date: [September 15, 2023, 1:28pm UTC](https://discourse.julialang.org/t/does-julia-have-anything-like-measurements-jl-for-multivariate-error-propagation/103879/7 "2023-09-15T13:28:14Z")

</div>

Not exactly what you are asking for, but the Reachability Analysis community has some tools for “imprecise probabilities” that may be of value. They are guaranteed to bound the probabilities, however in some cases the over approximation can be too conservative to be useful.

> **[GitHub - AnderGray/ProbabilityBoundsAnalysis.jl: Probability bounds analysis...](https://github.com/AnderGray/ProbabilityBoundsAnalysis.jl)**
>
> Probability bounds analysis in Julia. Contribute to AnderGray/ProbabilityBoundsAnalysis.jl development by creating an account on GitHub.

> **[GitHub - AnderGray/MomentArithmetic.jl: Rigorous moment propagation with...](https://github.com/AnderGray/MomentArithmetic.jl)**
>
> Rigorous moment propagation with partial information about moments and dependencies in Julia - GitHub - AnderGray/MomentArithmetic.jl: Rigorous moment propagation with partial information about mom...

Another thought is that if the function is invertible, computing the push-forward density is simple

Pf(\boldsymbol{x})=f(S^{-1}(\boldsymbol{x}))|\frac{d S^{-1}}{d\boldsymbol{x}}(\boldsymbol{x})|

where S is the function and f is the pdf.

Even if the function is invertible, knowing it is another issue. I don’t know if a multi-variate version of [Series Reversion](https://mathworld.wolfram.com/SeriesReversion.html) exists (I think I’ve seen it before), but if so, adding that to TaylorSeries.jl would make a pretty general solution, albeit approximate.

---

<div class="post-metadata">

### Author: ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)
#### Post date: [September 15, 2023, 2:43pm UTC](https://discourse.julialang.org/t/does-julia-have-anything-like-measurements-jl-for-multivariate-error-propagation/103879/8 "2023-09-15T14:43:20Z")

</div>

You are right, MCM.jl is particle-based, the constructor is even called `Particles` 🙂

Since you are asking about AD-based solutions, I’ll note that you get _exact_ propagation of multivariate normal distributions through linear functions using the [unscented transform](https://en.wikipedia.org/wiki/Unscented_transform). These situations are also the only situations in which AD-based solutions are exact (the ones that use first-order approximations). The unscented transform selects a small number of samples carefully, in a way that allows for exact propagation of Gaussians. In any other case than linear and Gaussian, simple linearization for uncertainty propagation is fraught with a lot of [pitfalls](https://baggepinnen.github.io/MonteCarloMeasurements.jl/stable/comparison/), while the unscented transform typically fails slightly less catastrophically.

You can perform the unscented transform using MCM.jl by constructing [`sigmapoints`](https://baggepinnen.github.io/MonteCarloMeasurements.jl/stable/#Sigma-points-1).

---

<div class="post-metadata">

### Author: ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)
#### Post date: [September 16, 2023, 12:09pm UTC](https://discourse.julialang.org/t/does-julia-have-anything-like-measurements-jl-for-multivariate-error-propagation/103879/9 "2023-09-16T12:09:54Z")

</div>

> [@marius311](#):
>
> Say I have two variables `a` and `b` and I know the full 2x2 covariance between them. Is there a Julia package to conveniently propagate Gaussian uncertainy through arbitrary functions of them

Fyi, the package [Covar.jl](https://discourse.julialang.org/t/ann-covar-jl-propagation-of-covariant-uncertainties/19006) might be of interest.
