# Model updating: Using previously computed posterior as prior after new data acquisition

**URL:** <https://discourse.julialang.org/t/model-updating-using-previously-computed-posterior-as-prior-after-new-data-acquisition/46869>\
**Category:** Probabilistic Programming\
**Tags:** question\
**Created:** [September 18, 2020, 8:01pm UTC](https://discourse.julialang.org/t/model-updating-using-previously-computed-posterior-as-prior-after-new-data-acquisition/46869 "2020-09-18T20:01:55Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Camilo1704](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/camilo1704/32/14601_2.png) [@Camilo1704](https://discourse.julialang.org/u/Camilo1704)\
**Post date:** [September 18, 2020, 8:01pm UTC](https://discourse.julialang.org/t/model-updating-using-previously-computed-posterior-as-prior-after-new-data-acquisition/46869/1 "2020-09-18T20:01:55Z")

</div>

Hi guys! I’m working with some models using a bayesian framework (Soss and Turing). I was wondering, once I have already contrasted my prior with the data and obtained the posterior distribution, how to add more data and use the already obtained posterior as prior. In other words, I would like to update my model when I have new data.

---

<div class="post-metadata">

**Author:** ![cscherrer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cscherrer/32/7631_2.png) [@cscherrer](https://discourse.julialang.org/u/cscherrer)\
**Post date:** [September 18, 2020, 8:21pm UTC](https://discourse.julialang.org/t/model-updating-using-previously-computed-posterior-as-prior-after-new-data-acquisition/46869/2 "2020-09-18T20:21:16Z")

</div>

Hi @Camilo1704, welcome to the Discourse! 🙂

Abstractly this is a very standard thing to do. But in practice it often gets tricky, because the posterior is no longer an abstract function, but (typically) a sample from the distribution represented by this function.

Still, there are things we can do. Let’s look at a really simple example in Soss:

```julia
using Soss

m = @model prior,n begin
    p ~ prior
    x ~ Bernoulli(p) |> iid(n)
end

```

with some fake data

```julia
x = rand(Bernoulli(0.2),10);
n = length(x)

```

Here I’ve made `prior` an argument to the model. In principle we can mess with the source code however we want, but this is simpler for starting out.

Say we fit some data from a uniform prior,

```julia
prior = Uniform()
post1 = dynamicHMC(m(prior=prior, n=length(x)), (x=x,));

```

To summarize,

```julia
julia> typeof(post1)
Array{NamedTuple{(:p,),Tuple{Float64}},1}

julia> particles(post1)
(p = 0.0882 ± 0.081,)

```

So now the idea is we want to feed this information back into the model as the new `prior`. In principle we could treat the prior as particles and sample from the empirical distribution. But we haven’t yet connected many samplers in Soss, and the discreteness will throw us off here.

A simple thing we can still do is _assumed density filtering_. The idea is, we fit a distribution to the posterior and use that as the new prior. For a unit interval, a Beta distribution is (often) reasonable, so

```julia
julia> ps = particles(post1).p.particles;

julia> newprior = fit(Beta, ps)
Beta{Float64}(α=0.9935509466234749, β=10.272982073369201)

```

Now with some new data,

```julia
julia> newx= rand(Bernoulli(0.2),10);

julia> post2 = dynamicHMC(m(prior=newprior, n=length(newx)), (x=newx,));

```

and comparing

```julia
julia> particles(post1)
(p = 0.0882 ± 0.081,)

julia> particles(post2)
(p = 0.14 ± 0.075,)

```

We can also compare the assumed densities:

```julia
julia> newprior
Beta{Float64}(α=0.9935509466234749, β=10.272982073369201)

julia> fit(Beta, particles(post2).p.particles)
Beta{Float64}(α=2.8409226999574213, β=17.4435665618913)

```

There’s a lot more we could do for all of this. One route I think is interesting is fitting the _transformed_ (to ℝⁿ) posterior to a `MvNormal`, then transforming back. This would allow representation of correlations in the posterior. Or, we could even fit a normalizing flow from Bijectors.jl! Lots of possibilities, but we’d need to extend things a bit to be able to do this.

---

<div class="post-metadata">

**Author:** ![Camilo1704](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/camilo1704/32/14601_2.png) [@Camilo1704](https://discourse.julialang.org/u/Camilo1704)\
**Post date:** [September 18, 2020, 8:37pm UTC](https://discourse.julialang.org/t/model-updating-using-previously-computed-posterior-as-prior-after-new-data-acquisition/46869/3 "2020-09-18T20:37:51Z")

</div>

Thank you very much for the response, it was very clear 🙂

---

<div class="post-metadata">

**Author:** ![cscherrer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cscherrer/32/7631_2.png) [@cscherrer](https://discourse.julialang.org/u/cscherrer)\
**Post date:** [September 18, 2020, 8:38pm UTC](https://discourse.julialang.org/t/model-updating-using-previously-computed-posterior-as-prior-after-new-data-acquisition/46869/4 "2020-09-18T20:38:51Z")

</div>

> [@Camilo1704](#):
>
> it was very clear 🙂

Thank you! Happy to help 🙂
