Is there a turing alternative to pm.Deterministic from pymc3?

Hi,
You can do something like this:

using Turing, Random

# Define a deterministic distribution
struct Determin{T<:Real} <: ContinuousUnivariateDistribution
  val::T
end

Distributions.rand(rng::AbstractRNG, d::Determin) = d.val
Distributions.logpdf(d::Determin, x::T) where T<:Real = zero(x)

@model function testmodel(x)
  y ~ Normal()
  m ~ Determin(y * 2)
  for i in eachindex(x)
    x[i] ~ Normal(m)
  end
end

# instantiate a model
model = testmodel(randn(10))

# inference
chain = sample(model, SMC(), 1000)

And if I would recall how to properly define the bijection for a custom distribution, you could also use HMC or NUTS. @torfjelde could you jump in?

3 Likes