# Uncertainty propagation

**URL:** https://discourse.julialang.org/t/uncertainty-propagation/63024
**Category:** Statistics
**Created:** [June 16, 2021, 2:59pm UTC](https://discourse.julialang.org/t/uncertainty-propagation/63024 "2021-06-16T14:59:47Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![lrnv](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lrnv/32/19373_2.png) [@lrnv](https://discourse.julialang.org/u/lrnv)
#### Post date: [June 16, 2021, 2:59pm UTC](https://discourse.julialang.org/t/uncertainty-propagation/63024/1 "2021-06-16T14:59:47Z")

</div>

Hey,

Consider a random dataset:

```julia
using Distributions
using Random
using Statistics
using StatsBase
X = rand(LogNormal(0,1),10000)

```

Consider a first function that ‘summarises’ the dataset into a simple vector of `k` statistics (here k=16 for this simple example):

```julia
function moms(x)
    return mean(x .^ (0:15)' .* exp.(-x),dims=1)
end
m = moms(X)

```

and a second function that does a lot of heavy computations on these statistics, which append to also output a `k`-sized vector :

```julia

function heavy_computations(m)
    # do a lot a stuff with m 
    result = log.(1 .+abs.(m)) .+ 1
    return result 
end
r = heavy_computations(m)

```

Now i want to asses the empirical covariance of the final `k` statistics. I could bootstrap:

```julia
function boot(M,data,f)
    n_obs = length(data)
    r = zeros((M,length(f(data))))
    for i in 1:M
        r[i,:] = f(data[sample(1:n_obs,n_obs,replace=true)])
    end
    return r
end
M = 1000
cov_r = cov(boot(M,X,heavy_computations ∘ moms))

```

but it computes the `heavy_computations` function M times ! It’s on the other hand way faster to only compute the covariance of m:

```julia
cov_m = cov(boot(1000,X,moms))

```

Could I leverage the fact that the `heavy_computations` function is written in Julia, a little like `Measurement.jl` but multivariate ?

---

<div class="post-metadata">

### Author: ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)
#### Post date: [June 16, 2021, 3:27pm UTC](https://discourse.julialang.org/t/uncertainty-propagation/63024/2 "2021-06-16T15:27:26Z")

</div>

I saw this package the other day for moment propagation, it may be related to your goals: [https://github.com/AnderGray/MomentArithmetic.jl](https://github.com/AnderGray/MomentArithmetic.jl)

---

<div class="post-metadata">

### Author: ![lrnv](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lrnv/32/19373_2.png) [@lrnv](https://discourse.julialang.org/u/lrnv)
#### Post date: [June 23, 2021, 2:22pm UTC](https://discourse.julialang.org/t/uncertainty-propagation/63024/3 "2021-06-23T14:22:35Z")

</div>

Unfortunately, this is not exactly what I want, but thanks for the catch, really interesting stuff.

What I want is a mechanisme to propagate a second order information through a piece of code. I’m not even sure it’s possible…

---

<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: [June 23, 2021, 2:54pm UTC](https://discourse.julialang.org/t/uncertainty-propagation/63024/4 "2021-06-23T14:54:58Z")

</div>

[GitHub - baggepinnen/MonteCarloMeasurements.jl: Propagation of distributions by Monte-Carlo sampling: Real number types with uncertainty represented by samples.](https://github.com/baggepinnen/MonteCarloMeasurements.jl) does this, it uses samples, but it’s not exactly the same as running `heavy_comoutation` M times due to the way its implemented. There are some details on this in the paper linked in the Readme.
