# How to correct high autocorrelation?

**URL:** <https://discourse.pymc.io/t/how-to-correct-high-autocorrelation/5636>\
**Category:** Questions\
**Created:** [August 17, 2020, 1:52pm UTC](https://discourse.pymc.io/t/how-to-correct-high-autocorrelation/5636 "2020-08-17T13:52:26Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![bridgeland](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/bridgeland/32/2773_2.png) [@bridgeland](https://discourse.pymc.io/u/bridgeland)\
**Post date:** [August 17, 2020, 1:52pm UTC](https://discourse.pymc.io/t/how-to-correct-high-autocorrelation/5636/1 "2020-08-17T13:52:26Z")

</div>

In @_eigenfoo’s [excellent cookbook](https://eigenfoo.xyz/bayesian-modelling-cookbook/), he advises checking “if your chains are impaired by high autocorrelation”. I have a trace that seems to be impaired by high autocorrelation. For example, here is an autocorrelation plot for one variable, that has a mean of 2.0 effective samples in a trace of 1200 samples (NUTS):

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/pymc3/original/2X/3/3a057183e5f70d159fafd554ce6bf353f89a2ec2.png)

Now what? I know I need to reparameterize the model, but I am not sure how to start. Certainly this RV is super-simple:

`potential_sale_mu = pm.Normal('potential_sale_mu', mu=0, sigma=2)`

So the problem must be downstream, among variables that use `potential_sale_mu`.

How can I figure out what change to make?

---

<div class="post-metadata">

**Author:** ![AlexAndorra](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/alexandorra/32/9142_2.png) [@AlexAndorra](https://discourse.pymc.io/u/AlexAndorra)\
**Post date:** [August 19, 2020, 6:52am UTC](https://discourse.pymc.io/t/how-to-correct-high-autocorrelation/5636/2 "2020-08-19T06:52:45Z")

</div>

Hi David,  
This looks quite correlated indeed! I would check which variables are highly correlated with each other: an `az.plot_pair` is usually useful here. This should give you hints about _which_ variables are problematic for the sampler, and _at which range_.  
Hope this helps 🖖

---

<div class="post-metadata">

**Author:** ![bridgeland](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/bridgeland/32/2773_2.png) [@bridgeland](https://discourse.pymc.io/u/bridgeland)\
**Post date:** [August 19, 2020, 2:16pm UTC](https://discourse.pymc.io/t/how-to-correct-high-autocorrelation/5636/3 "2020-08-19T14:16:26Z")

</div>

Update: I refactored the model (for other reasons), and the autocorrelation largely disappeared. Here is the plot for the same RV now:

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/pymc3/original/2X/e/ec9b41a9eed413ae6d81910ba7d05a5efa5b920c.png)

---

<div class="post-metadata">

**Author:** ![AlexAndorra](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/alexandorra/32/9142_2.png) [@AlexAndorra](https://discourse.pymc.io/u/AlexAndorra)\
**Post date:** [August 21, 2020, 8:45am UTC](https://discourse.pymc.io/t/how-to-correct-high-autocorrelation/5636/4 "2020-08-21T08:45:57Z")

</div>

Well done @bridgeland! I’m curious: what and how did you refactor?

---

<div class="post-metadata">

**Author:** ![bridgeland](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/bridgeland/32/2773_2.png) [@bridgeland](https://discourse.pymc.io/u/bridgeland)\
**Post date:** [August 21, 2020, 3:00pm UTC](https://discourse.pymc.io/t/how-to-correct-high-autocorrelation/5636/5 "2020-08-21T15:00:03Z")

</div>

There were several variables that model situations that are conceptually binomials. But hierarchical models of binomials do not simulate well, so the binomials were approximated as truncated normals. I refactored; each conceptual binomial is now a product of n and a beta. Problem solved.

---

<div class="post-metadata">

**Author:** ![AlexAndorra](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/alexandorra/32/9142_2.png) [@AlexAndorra](https://discourse.pymc.io/u/AlexAndorra)\
**Post date:** [August 24, 2020, 9:19am UTC](https://discourse.pymc.io/t/how-to-correct-high-autocorrelation/5636/6 "2020-08-24T09:19:15Z")

</div>

Nice! Let us know if you publish that model somewhere 😉
