TuringLang / Turing.jl

Bayesian inference with probabilistic programming.
https://turinglang.org
MIT License
2.04k stars 219 forks source link

StickBreakingProcess logpdf is ambiguous #893

Closed Red-Portal closed 5 years ago

Red-Portal commented 5 years ago
ERROR: MethodError: Distributions.logpdf(::StickBreakingProcess{DirichletProcess{Float64}}, ::Float64) is ambiguous. Candidates:
  logpdf(d::StickBreakingProcess, x) in Turing.RandomMeasures at /home/msca8h/.julia/packages/Turing/eIa7P/src/stdlib/RandomMeasures.jl:41
  logpdf(d::Distribution{Univariate,S} where S<:ValueSupport, x::Real) in Distributions at /home/msca8h/.julia/packages/Distributions/tfkz4/src/univariates.jl:333
Possible fix, define
  logpdf(::StickBreakingProcess, ::Real)

This is the Turing compiler error,

using Plots, Random
using Turing
using Turing.RandomMeasures
    @model infiniteGMM(x, K_max) = begin
        # Base distribution.
        α   = 1.0
        μ0  = 0.0
        σ0  = 1.0
        αg  = 2.0
        βg  = 2.0
        rpm = DirichletProcess(α)

        # Latent assignments.
        N = length(x)
        z = tzeros(Int, N)

        # Infinite collection of stick pieces and weights.
        v = tzeros(Float64, K_max)
        w = tzeros(Float64, K_max)
        K = 0

        # Cluster locations.
        μ = tzeros(Float64, K_max)
        σ = tzeros(Float64, K_max)
        u = tzeros(Float64, K_max)

        for i in 1:N

            # Draw a slice ∈ [0,1].
            u[i] ~ Beta(1, 1)

            # Instantiate new cluster.
            while (sum(w) < u[i]) && (K < K_max)
                K += 1
                v[K] ~ StickBreakingProcess(rpm)
                σ[K] ~ Gamma(αg, βg)
                μ[K] ~ Normal(μ0, σ0*σ[K])
                w[K] = v[K] * prod(1 .- v[1:(K-1)])
            end

            # Find truncation point
            K_ = findfirst(u[i] .< cumsum(w))

            # Sample assignments.
            w_ = w[1:K_] / sum(w[1:K_])
            z[i] ~ Categorical(w_)

            # Draw observation.
            x[i] ~ Normal(x[z[i]], σ[z[i]])
        end
    end

Here is the code.

trappmartin commented 5 years ago

Should be fixed once the PR is merged.

You might want to look into using the size-biased representation instead of the stick-breaking as this should be more efficient for inference. See: https://github.com/TuringLang/Turing.jl/blob/f2192c71c763f678b653e80e84363197835e2355/test/stdlib/RandomMeasures.jl#L274-L300

I'll add a tutorial on this and on the stick-breaking representation once I find the time.

For the paper see: https://pdfs.semanticscholar.org/e68a/9e22f650bcb8ffe2e6b0a8b13096a6595b31.pdf

Red-Portal commented 5 years ago

@trappmartin Thanks!