# Sampling from power posterior

**URL:** <https://discourse.julialang.org/t/sampling-from-power-posterior/54924>\
**Category:** Probabilistic Programming\
**Tags:** turing\
**Created:** [February 9, 2021, 1:09pm UTC](https://discourse.julialang.org/t/sampling-from-power-posterior/54924 "2021-02-09T13:09:04Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![theogf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/theogf/32/1987_2.png) [@theogf](https://discourse.julialang.org/u/theogf)\
**Post date:** [February 9, 2021, 1:09pm UTC](https://discourse.julialang.org/t/sampling-from-power-posterior/54924/1 "2021-02-09T13:09:04Z")

</div>

Hey,  
I was wondering if someone could guide me (or direct me to the relevant code) on how to sample from the power posterior given a Turing model. By power posterior, I mean the posterior to modified joint : p(x|y; b) \propto p(y|x)^b p(x), where b \in [0,1].

---

<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:** [February 9, 2021, 1:42pm UTC](https://discourse.julialang.org/t/sampling-from-power-posterior/54924/2 "2021-02-09T13:42:45Z")

</div>

I don’t know about Turing, but it should be relatively straightforward to build a logdensity function like this for Soss.

I like the way the power posterior connects the prior and posterior, but I don’t know any applications for sampling from it. Is this something that comes up a lot in some area?

---

<div class="post-metadata">

**Author:** ![theogf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/theogf/32/1987_2.png) [@theogf](https://discourse.julialang.org/u/theogf)\
**Post date:** [February 9, 2021, 2:10pm UTC](https://discourse.julialang.org/t/sampling-from-power-posterior/54924/3 "2021-02-09T14:10:06Z")

</div>

It appears in particular for Thermodynamic Integration for which I created a small package : [GitHub - theogf/ThermodynamicIntegration.jl: Thermodynamic Integration for Turing models and more](https://github.com/theogf/ThermodynamicIntegration.jl).  
I wanted to create some nice wrapper where the user gives a model and it automatically spits out the evidence. I would be happy to create a wrapper for Soss models as well!

---

<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:** [February 9, 2021, 2:18pm UTC](https://discourse.julialang.org/t/sampling-from-power-posterior/54924/4 "2021-02-09T14:18:19Z")

</div>

Nice! In the log-density this just comes in as a multiplicative constant, so I don’t think there will be any measurable overhead (the rest of the computation is so much more). So I may just add a switch for this to `logdensity`.

What’s a good descriptive name for the exponent `b` as a keyword argument?

---

<div class="post-metadata">

**Author:** ![theogf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/theogf/32/1987_2.png) [@theogf](https://discourse.julialang.org/u/theogf)\
**Post date:** [February 9, 2021, 2:21pm UTC](https://discourse.julialang.org/t/sampling-from-power-posterior/54924/5 "2021-02-09T14:21:31Z")

</div>

In physics they call it the coupling parameter cf : [Thermodynamic integration - Wikipedia](https://en.wikipedia.org/wiki/Thermodynamic_integration#Derivation)

---

<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:** [February 9, 2021, 2:35pm UTC](https://discourse.julialang.org/t/sampling-from-power-posterior/54924/6 "2021-02-09T14:35:48Z")

</div>

I’m seeing it called the temperature in some places:

> **[Estimating the evidence -- a review](https://arxiv.org/abs/1111.1957)**
>
> The model evidence is a vital quantity in the comparison of statistical models under the Bayesian paradigm. This paper presents a review of commonly used methods. We outline some guidelines and offer some practical advice. The reviewed methods are...

Is that consistent with other uses of “temperature”?

---

<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:** [February 9, 2021, 2:38pm UTC](https://discourse.julialang.org/t/sampling-from-power-posterior/54924/7 "2021-02-09T14:38:48Z")

</div>

Also

> **[Probabilistic Integration: A Role in Statistical Computation?](https://arxiv.org/abs/1512.00933)**
>
> A research frontier has emerged in scientific computation, wherein numerical error is regarded as a source of epistemic uncertainty that can be modelled. This raises several statistical challenges, including the design of statistical methods that...

---

<div class="post-metadata">

**Author:** ![theogf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/theogf/32/1987_2.png) [@theogf](https://discourse.julialang.org/u/theogf)\
**Post date:** [February 9, 2021, 2:39pm UTC](https://discourse.julialang.org/t/sampling-from-power-posterior/54924/8 "2021-02-09T14:39:24Z")

</div>

I think the issue with temperature it that it could be confused with tempered posteriors, which are quite different: for : p(x|y) \propto \exp(-V(x,y)),  
the tempered posterior is given by p(x|y;T)\propto \exp(-\frac{V(x,y)}{T}) where T is the temperature

---

<div class="post-metadata">

**Author:** ![mohamed82008](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mohamed82008/32/18171_2.png) [@mohamed82008](https://discourse.julialang.org/u/mohamed82008)\
**Post date:** [February 9, 2021, 2:39pm UTC](https://discourse.julialang.org/t/sampling-from-power-posterior/54924/9 "2021-02-09T14:39:25Z")

</div>

The `MiniBatchContext` in DynamicPPL and Turing can help you with that. The `loglike_scalar` in [DynamicPPL.jl/contexts.jl at master · TuringLang/DynamicPPL.jl · GitHub](https://github.com/TuringLang/DynamicPPL.jl/blob/master/src/contexts.jl#L48) is `b`.

---

<div class="post-metadata">

**Author:** ![theogf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/theogf/32/1987_2.png) [@theogf](https://discourse.julialang.org/u/theogf)\
**Post date:** [February 9, 2021, 2:42pm UTC](https://discourse.julialang.org/t/sampling-from-power-posterior/54924/10 "2021-02-09T14:42:18Z")

</div>

Oh that does sound perfect, it obviously has a completely different target but it should work!

---

<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:** [February 9, 2021, 2:44pm UTC](https://discourse.julialang.org/t/sampling-from-power-posterior/54924/11 "2021-02-09T14:44:01Z")

</div>

The “Probabilistic Integration” paper calls it the inverse temperature. Both b and 1/T appear as an exponent, so maybe the same?

---

<div class="post-metadata">

**Author:** ![trappmartin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/trappmartin/32/1165_2.png) [@trappmartin](https://discourse.julialang.org/u/trappmartin)\
**Post date:** [February 9, 2021, 2:48pm UTC](https://discourse.julialang.org/t/sampling-from-power-posterior/54924/12 "2021-02-09T14:48:08Z")

</div>

Yeah, there are various names for the same, e.g. power posterior or cold posterior. I have seen the term power posterior mostly been used by statisticians.

---

<div class="post-metadata">

**Author:** ![theogf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/theogf/32/1987_2.png) [@theogf](https://discourse.julialang.org/u/theogf)\
**Post date:** [February 9, 2021, 2:48pm UTC](https://discourse.julialang.org/t/sampling-from-power-posterior/54924/13 "2021-02-09T14:48:28Z")

</div>

Fair enough, I would personally disagree with the use of the term (a temperature is not restricted to [0,1] 🙂 ) and there is still my argument of how confusing it can be with tempered posterior. But I think as long everything is properly described all terms are fine.

---

<div class="post-metadata">

**Author:** ![theogf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/theogf/32/1987_2.png) [@theogf](https://discourse.julialang.org/u/theogf)\
**Post date:** [February 9, 2021, 2:49pm UTC](https://discourse.julialang.org/t/sampling-from-power-posterior/54924/14 "2021-02-09T14:49:35Z")

</div>

But these two are different 😃 For power posterior you only act on the likelihood, while for cold posteriors you act on the whole joint.  
Cold posteriors are used in simulated annealing for instance.

---

<div class="post-metadata">

**Author:** ![trappmartin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/trappmartin/32/1165_2.png) [@trappmartin](https://discourse.julialang.org/u/trappmartin)\
**Post date:** [February 9, 2021, 2:51pm UTC](https://discourse.julialang.org/t/sampling-from-power-posterior/54924/15 "2021-02-09T14:51:47Z")

</div>

Hm, I think there is a paper on BNNs which defines a cold posterior just as a power posterior.

---

<div class="post-metadata">

**Author:** ![trappmartin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/trappmartin/32/1165_2.png) [@trappmartin](https://discourse.julialang.org/u/trappmartin)\
**Post date:** [February 9, 2021, 2:53pm UTC](https://discourse.julialang.org/t/sampling-from-power-posterior/54924/16 "2021-02-09T14:53:31Z")

</div>

But maybe I remember it wrong. It’s a while ago that I look at this paper.

---

<div class="post-metadata">

**Author:** ![theogf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/theogf/32/1987_2.png) [@theogf](https://discourse.julialang.org/u/theogf)\
**Post date:** [February 9, 2021, 2:55pm UTC](https://discourse.julialang.org/t/sampling-from-power-posterior/54924/17 "2021-02-09T14:55:34Z")

</div>

If you mean this one : [How Good is the Bayes Posterior in Deep Neural Networks Really? | Florian Wenzel](http://florianwenzel.com/publication/2020_icml/) they use the same definition that I gave you ![2021-02-09_15-54](https://global.discourse-cdn.com/julialang/original/3X/6/a/6a48cc6d7d6b57d5afdfb4aa3f5577b3cf9850f1.png)

I got pretty confused at first as well, that’s why I am so confident now 😆

Cold posteriors are tempered posteriors where T\<1

---

<div class="post-metadata">

**Author:** ![trappmartin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/trappmartin/32/1165_2.png) [@trappmartin](https://discourse.julialang.org/u/trappmartin)\
**Post date:** [February 9, 2021, 2:59pm UTC](https://discourse.julialang.org/t/sampling-from-power-posterior/54924/18 "2021-02-09T14:59:05Z")

</div>

Ah, yes. See I remembered it wrong. 😅  
But at least I remembered correctly that it was the same as some existing concept. Haha.

---

<div class="post-metadata">

**Author:** ![theogf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/theogf/32/1987_2.png) [@theogf](https://discourse.julialang.org/u/theogf)\
**Post date:** [February 10, 2021, 3:42pm UTC](https://discourse.julialang.org/t/sampling-from-power-posterior/54924/19 "2021-02-10T15:42:57Z")

</div>

So I tried the following :

```julia
function power_logjoint(model, β)
    ctx = DynamicPPL.MiniBatchContext(DynamicPPL.DefaultContext(), β)
    spl = DynamicPPL.SampleFromPrior()
    return function f(z)
        vi = DynamicPPL.VarInfo(model)
        varinfo = DynamicPPL.VarInfo(vi, ctx, z)
        model(varinfo, spl, ctx)
        return DynamicPPL.getlogp(varinfo)
    end
end

```

But I get the error

```julia
ERROR: LoadError: MethodError: no method matching getspace(::DynamicPPL.MiniBatchContext{DynamicPPL.DefaultContext,Float64})
Closest candidates are:
  getspace(::Union{DynamicPPL.SampleFromPrior, DynamicPPL.SampleFromUniform}) at /home/theo/.julia/packages/DynamicPPL/wf0dU/src/sampler.jl:9
  getspace(::GibbsConditional{S,C} where C) where S at /home/theo/.julia/packages/Turing/a9ANC/src/inference/gibbs_conditional.jl:60
  getspace(::SMC{space,R} where R) where space at /home/theo/.julia/packages/Turing/a9ANC/src/inference/Inference.jl:425
  ...
Stacktrace:
 [1] DynamicPPL.VarInfo(::DynamicPPL.VarInfo{NamedTuple{(:x,),Tuple{DynamicPPL.Metadata{Dict{DynamicPPL.VarName{:x,Tuple{}},Int64},Array{MvNormal{Float64,PDMats.PDiagMat{Float64,Array{Float64,1}},FillArrays.Zeros{Float64,1,Tuple{Base.OneTo{Int64}}}},1},Array{DynamicPPL.VarName{:x,Tuple{}},1},Array{Float64,1},Array{Set{DynamicPPL.Selector},1}}}},Float64}, ::DynamicPPL.MiniBatchContext{DynamicPPL.DefaultContext,Float64}, ::Array{ForwardDiff.Dual{ForwardDiff.Tag{ThermodynamicIntegration.var"#f#33"{DynamicPPL.Model{var"#7#8",(:y,),(),(),Tuple{Array{Float64,1}},Tuple{}},DynamicPPL.MiniBatchContext{DynamicPPL.DefaultContext,Float64},DynamicPPL.SampleFromPrior},Float64},Float64,5},1}) at /home/theo/.julia/packages/DynamicPPL/wf0dU/src/varinfo.jl:115

```

```julia

```

---

<div class="post-metadata">

**Author:** ![mohamed82008](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mohamed82008/32/18171_2.png) [@mohamed82008](https://discourse.julialang.org/u/mohamed82008)\
**Post date:** [February 10, 2021, 3:55pm UTC](https://discourse.julialang.org/t/sampling-from-power-posterior/54924/20 "2021-02-10T15:55:13Z")

</div>

Full stacktrace please.

[Next page](https://discourse.julialang.org/t/sampling-from-power-posterior/54924.md?page=2)
