# Can traces be used as priors?

**URL:** <https://discourse.pymc.io/t/can-traces-be-used-as-priors/3438>\
**Category:** Questions\
**Created:** [June 15, 2019, 1:21pm UTC](https://discourse.pymc.io/t/can-traces-be-used-as-priors/3438 "2019-06-15T13:21:17Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Edderic](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/edderic/32/1422_2.png) [@Edderic](https://discourse.pymc.io/u/Edderic)\
**Post date:** [June 15, 2019, 1:21pm UTC](https://discourse.pymc.io/t/can-traces-be-used-as-priors/3438/1 "2019-06-15T13:21:17Z")

</div>

Hi!

Is it possible to do [online learning](https://en.wikipedia.org/wiki/Online_machine_learning) with PyMC3? One scenario I’m thinking about is when we get one new observation, we’d like to get the posterior. After getting that posterior, I’d like to use that as a prior for the next time I get an observation. This is in contrast to getting all the data at once, fitting a model, then getting a posterior distribution.

Thanks,  
Edderic

---

<div class="post-metadata">

**Author:** ![chartl](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/chartl/32/1515_2.png) [@chartl](https://discourse.pymc.io/u/chartl)\
**Post date:** [June 15, 2019, 2:35pm UTC](https://discourse.pymc.io/t/can-traces-be-used-as-priors/3438/2 "2019-06-15T14:35:42Z")

</div>

So there’s a notebook on updating priors:

[https://docs.pymc.io/notebooks/updating\_priors.html](https://docs.pymc.io/notebooks/updating_priors.html)

and a couple discussions on the forums for how to do this in a multivariate way either approximately:

> [@Updating multivariate priors](https://discourse.pymc.io/t/updating-multivariate-priors/2804):
>
> Dear all, There’s a very helpful notebook on [Updating Priors](https://docs.pymc.io/notebooks/updating_priors.html) in which the prior distributions of three variables are updated as new data becomes available. Importantly, these prior distributions are assumed independent. In practice however, priors are often correlated. How would one consider such a case? From what I’ve seen, the “Interpolated” distribution is only applicable to univariate problems, and I have not seen any examples of multivariate distributions being updated as in the above not…

or using copulas

> [@Prior propagation by nonparametric copulas](https://discourse.pymc.io/t/prior-propagation-by-nonparametric-copulas/3158):
>
> Having participated in a number of conversations regarding updating priors, I thought about the best one could do when you observe only a sample \hat \theta\_k \sim \theta | D\_1, \dots, D\_k; and seek to compute P[\theta|D\_1, \dots, D\_{k+1}]. In effect, the question is how to extend [https://docs.pymc.io/notebooks/updating\_priors.html](https://docs.pymc.io/notebooks/updating_priors.html) in some reasonable way for multivariate parameters. Previous topics have suggested using multiple pm.Interpolated distributions: or parametric approximations: …

---

<div class="post-metadata">

**Author:** ![Edderic](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/edderic/32/1422_2.png) [@Edderic](https://discourse.pymc.io/u/Edderic)\
**Post date:** [June 16, 2019, 9:21pm UTC](https://discourse.pymc.io/t/can-traces-be-used-as-priors/3438/3 "2019-06-16T21:21:16Z")

</div>

Thank you! Exactly what I was looking for. 🙂

---

<div class="post-metadata">

**Author:** ![Edderic](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/edderic/32/1422_2.png) [@Edderic](https://discourse.pymc.io/u/Edderic)\
**Post date:** [July 15, 2019, 6:43pm UTC](https://discourse.pymc.io/t/can-traces-be-used-as-priors/3438/4 "2019-07-15T18:43:10Z")

</div>

@chartl (or anyone else) do you know if the method in [updating priors notebook](https://docs.pymc.io/notebooks/updating_priors.html) can be used to approximate a beta distribution? I see the line:

```auto
x = np.concatenate([[x[0] - 3 * width], x, [x[-1] + 3 * width]])

```

Does it make sense to modify it to something like:

```auto
    min_explore_x = x[0] - 3 * width
    max_explore_x = x[-1] + 3 * width
    
    x = np.concatenate([
        [max(min_explore_x, 0.0001)],
        x,
        [min(max_explore_x, 0.9999)]
    ])

```

so that support is always between 0 and 1?

---

<div class="post-metadata">

**Author:** ![chartl](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/chartl/32/1515_2.png) [@chartl](https://discourse.pymc.io/u/chartl)\
**Post date:** [July 15, 2019, 7:30pm UTC](https://discourse.pymc.io/t/can-traces-be-used-as-priors/3438/5 "2019-07-15T19:30:56Z")

</div>

Yes - although since this is using an interpolated distribution anyway, my recommendation is to keep everything on an unconstrained space (say, `logit(x)`), and set the extrapolation limits quite wide. This is consistent with the `Automatic Transformation of Constrained Variables` part of ADVI ([https://arxiv.org/abs/1506.03431](https://arxiv.org/abs/1506.03431)).

Also, since you’ve brought up a parametric distribution (Beta), why not put the interpolated distribution over hyperparameters \alpha and \beta? You could probably get away with treating \log \alpha, \log \beta as multivariate normal, too.

---

<div class="post-metadata">

**Author:** ![Edderic](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/edderic/32/1422_2.png) [@Edderic](https://discourse.pymc.io/u/Edderic)\
**Post date:** [September 12, 2019, 1:00pm UTC](https://discourse.pymc.io/t/can-traces-be-used-as-priors/3438/6 "2019-09-12T13:00:30Z")

</div>

> [@chartl](#):
>
> You could probably get away with treating logα,logβ\log \alpha, \log \beta as multivariate normal, too.

That’s a good point! Thanks. I modified my code to keep the original `from_posterior` method in place. However, I started running into some issues. With the project that I’m working on, a bunch of the variables are unobserved. Since the `from_posterior` function adds 3 times the width of the old posterior to the left and the right of the old posterior, the new priors of these unobserved variables become much wider than the original prior. So I’m adding some small constant instead. This prevents the blowup of the prior, while still giving the possibility of exploring values it hasn’t sampled in the past. Do you think this makes sense?

Also another question: Would the usual model comparison work? Let’s say I have two models that made use of `from_posterior`. Would `pm.compare` give sensible WAIC values (as if I ran the two models on the whole data set instead of updating my priors one by one)?
