# Propagation of uncertainty

**URL:** <https://discourse.pymc.io/t/propagation-of-uncertainty/11222>\
**Category:** v5\
**Tags:** modeling\
**Created:** [January 19, 2023, 2:17pm UTC](https://discourse.pymc.io/t/propagation-of-uncertainty/11222 "2023-01-19T14:17:39Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![scipio](https://avatars.discourse-cdn.com/v4/letter/s/3ec8ea/32.png) [@scipio](https://discourse.pymc.io/u/scipio)\
**Post date:** [January 19, 2023, 2:17pm UTC](https://discourse.pymc.io/t/propagation-of-uncertainty/11222/1 "2023-01-19T14:17:39Z")

</div>

Hello Pymc community

I have a doubt about the propagation of uncertainties.

Currently, I have estimated some parameters through a data regression, and thanks to bayes and pymc I have their mean, sd, and pdf

Now, I would like to combine these parameters with others through a different function than the one used in the data regression to see how well they might follow an experimental data set.

So, in this case, I don’t need to optimize the parameters, just to see how well these parameters follow the expected trend.

My question is about the best way to associate an uncertainty bar with the mean trend.

Instead of propagating the uncertainties in the classical form, what I think is more correct to do, since I have pdfs of the variables, is to randomly extract values from the various pdfs and average the results.

Is this correct? Is there any example to guide me?

---

<div class="post-metadata">

**Author:** ![cluhmann](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/cluhmann/32/3083_2.png) [@cluhmann](https://discourse.pymc.io/u/cluhmann)\
**Post date:** [January 19, 2023, 3:22pm UTC](https://discourse.pymc.io/t/propagation-of-uncertainty/11222/2 "2023-01-19T15:22:44Z")

</div>

Can you provide a bit more details of your scenario? I am unclear what you mean by “associate an uncertainty bar with the mean trend”. Is it just a matter of combining estimated parameter values in a new way (the “different function” you mention) to generate predictions about another data set?

---

<div class="post-metadata">

**Author:** ![scipio](https://avatars.discourse-cdn.com/v4/letter/s/3ec8ea/32.png) [@scipio](https://discourse.pymc.io/u/scipio)\
**Post date:** [January 19, 2023, 7:11pm UTC](https://discourse.pymc.io/t/propagation-of-uncertainty/11222/3 "2023-01-19T19:11:42Z")

</div>

A simplified example to clarify  
I have a set of points (x,y) and a linear model, whose parameters I can estimate with a fit y=ax+b in pymc

In this way I find the pdfs for a and b

Next, I want to use these parameters, in another function  
y’=a+cx’+e^bx’  
to see the predictions with respect to a different dataset  
Is there an example that can guide me?

---

<div class="post-metadata">

**Author:** ![bwengals](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/bwengals/32/1237_2.png) [@bwengals](https://discourse.pymc.io/u/bwengals)\
**Post date:** [January 19, 2023, 8:00pm UTC](https://discourse.pymc.io/t/propagation-of-uncertainty/11222/4 "2023-01-19T20:00:10Z")

</div>

Just use samples from the posterior of the parameters!

So after you fit your model in PyMC, write a loop that takes one sample from the trace for `a` and `b`, drops those samples into your next function for `y'`, and calculate to get a single output. Repeat that process to get samples from the pdf of `y'`.

You can get the same result by setting `y'` as a “deterministic” in your model, something like

```auto
pm.Deterministic("y_prime", a + c * x_prime + exp(b*x_prime))

```

and then you’ll find `y_prime` in your trace or `idata` object with the other parameters you fit.

---

<div class="post-metadata">

**Author:** ![scipio](https://avatars.discourse-cdn.com/v4/letter/s/3ec8ea/32.png) [@scipio](https://discourse.pymc.io/u/scipio)\
**Post date:** [January 31, 2023, 9:48pm UTC](https://discourse.pymc.io/t/propagation-of-uncertainty/11222/6 "2023-01-31T21:48:04Z")

</div>

Sorry if it’s dumb, but I’m stuck  
This is my code (more or less)

I’m not able to sample from the trace for a and b

```auto
with pm.Model() as a_model:

		a = pm.Normal('a', mu=0.4, sigma=0.04)
		b = pm.Normal('b', mu=0.9, sigma=0.2)
		c = pm.Normal('c', mu=0.03, sigma=0.01)
		e = pm.HalfCauchy('e',0.1)
		u = func ( x, a, b, c)

		y_pre = pm.Normal(‘y_pre',mu=u, sigma=e, observed=y_obs)	
		trace_a=pm.sample(2000, tune=500, random_seed=42)

		b_m = float(trace_a.posterior["b"].mean())

with pm.Model() as b_model:

		e2 = pm.HalfCauchy('e2',0.1)
		f = pm.Normal('f', mu=0.3, sigma=0.1)
		g = pm.Normal('g', mu=0.6, sigma=0.02)
				
		u2 = func2 ( x2, b_m, f, g)

		y_pre2 = pm.Normal(‘y_pre2',mu=u2, sigma=e2, observed=y_obs2)
		trace_b=pm.sample(2000, tune=500, random_seed=42)

	t_post_a=trace_a.posterior
	t_post_b=trace_b.posterior

	final = [] 
	final = np.append(final,func3(x3,t_post_a["b"],t_post_a["a"],t_post_a["c"],t_post_b["f"],t_post_b["g"]))

```

---

<div class="post-metadata">

**Author:** ![cluhmann](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/cluhmann/32/3083_2.png) [@cluhmann](https://discourse.pymc.io/u/cluhmann)\
**Post date:** [February 1, 2023, 7:27pm UTC](https://discourse.pymc.io/t/propagation-of-uncertainty/11222/7 "2023-02-01T19:27:49Z")

</div>

I think the easiest way is to use arviz’s [`extract()`](https://python.arviz.org/en/latest/api/generated/arviz.extract.html) function:

```python
samples = az.extract(idata, group="posterior", num_samples=10)

```

---

<div class="post-metadata">

**Author:** ![jessegrabowski](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/jessegrabowski/32/5010_2.png) [@jessegrabowski](https://discourse.pymc.io/u/jessegrabowski)\
**Post date:** [February 1, 2023, 7:32pm UTC](https://discourse.pymc.io/t/propagation-of-uncertainty/11222/8 "2023-02-01T19:32:14Z")

</div>

Wouldn’t it make sense to run both of these models on the same MCMC chain, and pass the random variable `b` into `func2` directly? Just taking the mean discards the uncertainty about it.

---

<div class="post-metadata">

**Author:** ![cluhmann](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/cluhmann/32/3083_2.png) [@cluhmann](https://discourse.pymc.io/u/cluhmann)\
**Post date:** [February 1, 2023, 9:35pm UTC](https://discourse.pymc.io/t/propagation-of-uncertainty/11222/9 "2023-02-01T21:35:16Z")

</div>

Yeah, I wasn’t sure what’s going on with the multiple models and didn’t really take a close look honestly. Yes, taking means of (marginal) posteriors is a bad idea.
