# PG fails to sample properly with acclogp!

**URL:** https://discourse.julialang.org/t/pg-fails-to-sample-properly-with-acclogp/67924
**Category:** Probabilistic Programming
**Created:** [September 9, 2021, 10:40am UTC](https://discourse.julialang.org/t/pg-fails-to-sample-properly-with-acclogp/67924 "2021-09-09T10:40:21Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![sami1](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sami1/32/23555_2.png) [@sami1](https://discourse.julialang.org/u/sami1)
#### Post date: [September 9, 2021, 10:40am UTC](https://discourse.julialang.org/t/pg-fails-to-sample-properly-with-acclogp/67924/1 "2021-09-09T10:40:21Z")

</div>

Hello,

I’m currently trying to sample from a posterior with a mixture of differentiable and discrete parameters, some of them I can’t marginalize. I tried to marginalize the one I could using acclogp!, and sample the rest of the discrete parameters with PG.  
However I realized that doing this, PG fails to sample from the posterior, and samples from the prior.  
Here is an MWE :

```nohighlight
@model minimodel(Y) = begin
    μ ~ Normal(0, 1)
    σ ~ InverseGamma(2, 1)
    Turing.acclogp!( __varinfo__ , logpdf(filldist(Normal(μ, σ), length(Y)), Y))
end

Y = 2 .+ 0.3*randn(1000)
chn = sample(minimodel(Y), PG(20), 1000)

```

returns :

```julia
Summary Statistics
  parameters mean std naive_se mcse ess rhat ess_per_sec 
      Symbol Float64 Float64 Float64 Float64 Float64 Float64 Float64 

           μ -0.0106 0.9972 0.0315 0.0313 996.6409 0.9990 231.9928
           σ 1.0773 2.1186 0.0670 0.0670 970.7092 0.9991 225.9565

```

By plotting the result, you can easily check that it just sampled the prior. I can go around this issue with trick in the flavour of

```nohighlight
L = logpdf(filldist(Normal(μ, σ), length(Y)), Y)
1 ~ Bernoulli(exp(L/length(Y)))

```

But I’d like to know if it’s a intended behavior, and if so what am I missing ?

Thanks

---

<div class="post-metadata">

### Author: ![torfjelde](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/torfjelde/32/206542_2.png) [@torfjelde](https://discourse.julialang.org/u/torfjelde)
#### Post date: [September 9, 2021, 2:33pm UTC](https://discourse.julialang.org/t/pg-fails-to-sample-properly-with-acclogp/67924/2 "2021-09-09T14:33:20Z")

</div>

`PG` requires knowledge about what is considered an observation and what is not, so unfortunately it won’t be compatible with usage of `@acclogprob!` as a replacement for the observe `~` 😕

EDIT: We have ideas on introducing traits for samplers, etc., so that we can warn/error in these cases.

---

<div class="post-metadata">

### Author: ![sami1](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sami1/32/23555_2.png) [@sami1](https://discourse.julialang.org/u/sami1)
#### Post date: [September 9, 2021, 3:33pm UTC](https://discourse.julialang.org/t/pg-fails-to-sample-properly-with-acclogp/67924/3 "2021-09-09T15:33:49Z")

</div>

Got it ! Thanks for the quick answer
