# Misspecified Turing.jl model?

**URL:** https://discourse.julialang.org/t/misspecified-turing-jl-model/118451
**Category:** Statistics
**Tags:** turing
**Created:** [August 21, 2024, 12:57pm UTC](https://discourse.julialang.org/t/misspecified-turing-jl-model/118451 "2024-08-21T12:57:37Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Domenic\_Di\_Francesco](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/domenic_di_francesco/32/25260_2.png) [@Domenic\_Di\_Francesco](https://discourse.julialang.org/u/Domenic_Di_Francesco)
#### Post date: [August 21, 2024, 12:57pm UTC](https://discourse.julialang.org/t/misspecified-turing-jl-model/118451/1 "2024-08-21T12:57:37Z")

</div>

hi everyone - I’m thinking I may have miss-specified a Turing model 🤔

I want to combine measurements of X, from 2 tests:  
test A, has a known precision (`test_A_error`)  
test B has an unknown precision (`test_B_error`) and bias (`test_B_bias_pr`)

I’m expecting that increasing the amount of data from test A and from test B should reduce uncertainty in all my parameters: `mean_X`, `sd_X`, `X`, `test_B_bias` and `test_B_error`

…however, this is not the case. Specifically, `mean_X`,` sd_X`, `X` , do not appear to be shifting from their (weakly informative) prior.

I will continue to investigate, but would like to knof if anyone sees anything fundamentally wrong with the model:

```julia
@model function test_model(test_A_data, test_A_error, test_B_data)  

    # priors
    mean_X ~ mean_X_pr 
    sd_X ~ sd_X_pr 

    test_B_bias ~ test_B_bias_pr
    test_B_error ~ test_B_error_pr

    # Likelihood for the test data
    X ~ Normal(mean_X, sd_X)

    for test_A_id in eachindex(test_A_data)
        test_A_data[test_id] ~ Normal(X, test_A_error)
    end
    
    for test_B_id in eachindex(test_B_data)
        test_B_data[test_B_id] ~ Normal(X + test_B_bias, test_B_error)
    end
        
end

```

---

<div class="post-metadata">

### Author: ![Domenic\_Di\_Francesco](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/domenic_di_francesco/32/25260_2.png) [@Domenic\_Di\_Francesco](https://discourse.julialang.org/u/Domenic_Di_Francesco)
#### Post date: [August 28, 2024, 10:28am UTC](https://discourse.julialang.org/t/misspecified-turing-jl-model/118451/2 "2024-08-28T10:28:44Z")

</div>

I thought I would include the solution here, just incase anyone would benefit:

The issue was that X wasn’t vectorising as I had assumed in the model code. I needed to explicitly create an X for each measurement:

```julia
X = Vector{Real}(undef, Nx)
for i in 1:Nx
    X[i] ~ Normal(mean_X, sd_X)
end

```

and then reference that particular X in the likelihoods:

```julia
for test_A_id in eachindex(test_A_data)
    test_A_data[test_A_id] ~ Normal(X[test_A_id], test_A_error)
end

```

…where

```julia
Nx = max(length(test_A_data), length_test_B_data))

```

---

<div class="post-metadata">

### Author: ![dlakelan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dlakelan/32/8491_2.png) [@dlakelan](https://discourse.julialang.org/u/dlakelan)
#### Post date: [August 28, 2024, 3:22pm UTC](https://discourse.julialang.org/t/misspecified-turing-jl-model/118451/3 "2024-08-28T15:22:16Z")

</div>

> [@Domenic\_Di\_Francesco](#):
>
> `X ~ Normal(mean_X, sd_X)`

I think what you wanted was

```julia
X ~ MvNormal(...)

```

Which will also be faster than the looped version.
