# Parameter inference with Turing with Observed Distribution

**URL:** <https://discourse.julialang.org/t/parameter-inference-with-turing-with-observed-distribution/123065>\
**Category:** Probabilistic Programming\
**Tags:** question\
**Created:** [November 25, 2024, 10:48pm UTC](https://discourse.julialang.org/t/parameter-inference-with-turing-with-observed-distribution/123065 "2024-11-25T22:48:37Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Fourier](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fourier/32/38176_2.png) [@Fourier](https://discourse.julialang.org/u/Fourier)\
**Post date:** [November 25, 2024, 10:48pm UTC](https://discourse.julialang.org/t/parameter-inference-with-turing-with-observed-distribution/123065/1 "2024-11-25T22:48:37Z")

</div>

Lets say I have a model

```julia
using Turing, StatsPlots

@model function model(a)
    p ~ Normal(1,1)
    a ~ Normal(p,1)
end

```

and I do an experiment and I observe `a` to be equal to 1.5:

Then I can update my prior believe regarding `a` using

```julia
obs = 1.5
chain = sample(model(obs), NUTS(), 1000)

```

which gives me the posterior distribution of `a`.

However in reality, we never observe single values, almost always there is some uncertainty assosicated with them. We observe Distributions.  
So instead of observing `a=1.5` I would observe `obs = Normal(1.5,0.1)`

How can I model this to find the parameters using turing or any other julia package?

I am new to turing, but this seems quite exiting.

---

<div class="post-metadata">

**Author:** ![p-gw](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/p-gw/32/210518_2.png) [@p-gw](https://discourse.julialang.org/u/p-gw)\
**Post date:** [November 26, 2024, 10:16am UTC](https://discourse.julialang.org/t/parameter-inference-with-turing-with-observed-distribution/123065/2 "2024-11-26T10:16:05Z")

</div>

Turing.jl doesn’t really do bayesian updating as you have described.  
If you observe new data you will have to re-fit the model using the entire dataset to get the posterior distributions of your parameters.

If you really need online updating there are a few options depending on your model. Maybe something simple like ConjugatePriors.jl could do the trick:

```julia
using Distributions
using ConjugatePriors

prior = Normal(1, 1)
sigma = 1.0

obs = [1.5] # has to be an array and can include multiple values
posterior((prior, sigma), Normal, obs)

# Normal{Float64}(μ=1.25, σ=0.7071067811865475)

```

where you can use the result from `posterior` as the prior when you observe new data.

For more complicated models, RxInfer.jl seems to be optimized for updating/streaming data so that might be worth to look into.  
It features a similar DSL to Turing.jl.

---

<div class="post-metadata">

**Author:** ![penelopeysm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/penelopeysm/32/213172_2.png) [@penelopeysm](https://discourse.julialang.org/u/penelopeysm)\
**Post date:** [December 9, 2024, 9:08pm UTC](https://discourse.julialang.org/t/parameter-inference-with-turing-with-observed-distribution/123065/3 "2024-12-09T21:08:57Z")

</div>

Hi @Fourier!

> So instead of observing `a=1.5` I would observe `obs = Normal(1.5,0.1)`

Could you do this then?

```julia
using Turing

@model function model(obs)
    p ~ Normal(1, 1)
    a ~ Normal(p, 1) # the 'true' underlying value
    obs ~ Normal(a, 0.1) # the physical measurement
end

```
