luiarthur / TuringBnpBenchmarks

Benchmarks of Bayesian Nonparametric models in Turing and other PPLs
https://luiarthur.github.io/TuringBnpBenchmarks/
MIT License
29 stars 1 forks source link

Poor inference for CRP DP GMM via SMC, PG, IS #10

Open luiarthur opened 4 years ago

luiarthur commented 4 years ago

I simulated some data and fit the following model to the data. I get poor inference. i.e., the results don't really match up with the simulation truth. Particularly, I tend to learn 4 to 5 clusters when my data has 4 almost-equally-sized clusters, but there are usually only 1 or 2 big (dominating) clusters in the posterior inference.

So, my first question is am I abusing the API? In my DP gaussian mixtures of location and scale, I use a base measure (H) which is Normal x InverseGamma (two independent distributions) for the location (mu) and scale (sigma). Am I doing this correctly? (It runs, but I suspect I'm doing something outside the intended use.)

My second question is, if the model is implemented correctly, what might be the cause for poor inference? Admittedly, I'm not familiar with SMC/PC. But does increasing the number of particles generally lead to better inference?

# DP GMM model under CRP construction
@model dp_gmm_crp(y) = begin
    nobs = length(y)

    alpha ~ Gamma(1, 0.1)  # mean = a*b
    rpm = DirichletProcess(alpha)

    # Base measure.
    H = arraydist([Normal(0, 3), InverseGamma(2, 0.05)])  # is this OK?

    # Latent assignment.
    z = tzeros(Int, nobs)

    # Locations and scales of infinitely many clusters.
    mu_sigma = TArray(Vector{Float64}, 0)  # is this OK?

    for i in 1:nobs
        # Number of clusters.
        K = maximum(z)
        n = Vector{Int}([sum(z .== k) for k in 1:K])

        # Sample cluster label.
        z[i] ~ ChineseRestaurantProcess(rpm,  n)

        # Create a new cluster.
        if z[i] > K
            push!(mu_sigma, [0.0, 0.1])  # is this OK?
            mu_sigma[z[i]] ~ H  # is this OK?
        end

        # Sampling distribution.
        mu, sigma = mu_sigma[z[i]]  # is this OK?
        y[i] ~ Normal(mu, sigma)
    end
end
;
# Set random seed for reproducibility
Random.seed!(0);

# Sample from posterior
@time chain = begin
    burn = 2000  # NOTE: The burn in is also returned. Discard manually.
    n_samples = 1000
    iterations = burn + n_samples

    sample(dp_gmm_crp(y), SMC(), iterations)
    # sample(dp_gmm_crp(y), IS(), iterations)
    # sample(dp_gmm_crp(y), Gibbs(PG(5, :z), PG(5, :mu_sigma)), iterations)
end;

Here is the complete notebook.

trappmartin commented 4 years ago

You might want to do something like this instead.

@model dp_gmm_crp_delayed(y) = begin
    nobs = length(y)

    alpha ~ Gamma(1, 0.1)  # mean = a*b
    rpm = DirichletProcess(alpha)

    # Base measure.
    H_mu = Normal(0, 3)
    H_sigma = InverseGamma(2, 3)

    # Latent assignment.
    z = tzeros(Int, nobs)
    n = tzeros(Int, nobs)
    K = 0

    mu = tzeros(Float64, nobs)
    sigma = tzeros(Float64, nobs)

    for i in 1:nobs
        z[i] ~ ChineseRestaurantProcess(rpm,  n)
        n[z[i]] += 1

        if z[i] > K
            mu[z[i]] ~ H_mu
            sigma[z[i]] ~ H_sigma
            K += 1
        end

        y[i] ~ Normal(mu[z[i]], sqrt(sigma[z[i]]))
    end
end

This might contain a bug as I haven't tested it.

In general, it is beneficial to delay sampling of RVs for SMC and PG, as in this code. I wasn't aware of this myself, Hong told me a while ago.

xukai92 commented 4 years ago

I think Martin's version at least removes the possibility of mis-using TArray because the sizes of containers are fixed now.

In general, it is beneficial to delay sampling of RVs for SMC and PG, as in this code. I wasn't aware of this myself, Hong told a while ago.

Can you remind me of the example Hong gave? I thought as for delay sampling of RVs, we should indeed put them in the same loop, which is what Arthur is doing ATM. My understanding is that the model can stop early if it observes data in an online-fashion.

mohamed82008 commented 4 years ago

Also try running the inference in the https://github.com/mohamed82008/DynamicPPL_NeurIPS environment. I think I fixed some bugs there for dynamic programs.

trappmartin commented 4 years ago

You are completely right Kai. Stupid me. I’ll update the code example.

luiarthur commented 4 years ago

What is the output of chain = sample(model, SMC(), iterations).

I see that chians.name_map[1] has something called internals, and has a key named weight. In chain.value, I also see a field/column named weight.

My limited understanding is that in SMC, you end up with particles and weights for those particles, which together represent approximate posterior samples. So does sample(model, SMC(), iterations) produce particles and weights? Or does it return something else?

Sorry if this sounds like a mess...

luiarthur commented 4 years ago

Also try running the inference in the https://github.com/mohamed82008/DynamicPPL_NeurIPS environment. I think I fixed some bugs there for dynamic programs.

@mohamed82008 Do you mean use the libraries and versions listed in Project.toml?

mohamed82008 commented 4 years ago

Yes activate the environment and run your code there.

trappmartin commented 4 years ago

What is the output of chain = sample(model, SMC(), iterations).

I see that chians.name_map[1] has something called internals, and has a key named weight. In chain.value, I also see a field/column named weight.

My limited understanding is that in SMC, you end up with particles and weights for those particles, which together represent approximate posterior samples. So does sample(model, SMC(), iterations) produce particles and weights? Or does it return something else?

Sorry if this sounds like a mess...

Yes, it should return particles and their weights. I’ll need to have a look as I’m not 100% sure. I should be able to do so by tomorrow evening. Sorry for the late reply.

yebai commented 4 years ago

The model in the notebook (assuming it is the latest version) looks good to me. Also, it makes sense to set an upper bound on the total active clusters Kmax. This is because in most cases the actual number of clusters is smaller than the number of data points.

It is also a good idea to explore different sampler setups. Sampling everything using SMC (even with a large number of particles) would probably lead to poor inference results due to usual SMC dependency issues. Similar issues would also affect the PG sampler. It is generally a better idea to use PG + MH, PG + HMC, or PG + MH + HMC samplers, where PG is only used for discrete random variables (i.e. cluster assignments). A potentially good sampler I imagine: PG: z, HMC: alpha, HMC: mu, gamma. It is worth noting that a separate HMC sampler is used for the concentration parameter here.

luiarthur commented 4 years ago

@yebai Thanks for the comment! Awesome, I'll double check that I'm using the latest versions. You answered my other question. I've searched around and read that, in Turing, PG is more like a conditional SMC, correct? I'll try a few more sampler setups.

yebai commented 4 years ago

in Turing, PG is more like a conditional SMC, correct?

That's correct -- PG is conditional SMC in Turing.

luiarthur commented 4 years ago

I am currently on the master branch of Turing. The model is:

@model dp_gmm_crp_delayed(y, Kmax) = begin
    nobs = length(y)

    alpha ~ Gamma(1, 1/10)  # mean = a*b
    rpm = DirichletProcess(alpha)

    # Base measure.
    H_mu = Normal(0, 3)
    H_sigma = LogNormal(-1, 0.5)

    # Latent assignment.
    z = tzeros(Int, nobs)
    n = tzeros(Int, nobs)
    K = 0

    mu = tzeros(Float64, Kmax)
    sigma = tzeros(Float64, Kmax)

    for i in 1:nobs
        z[i] ~ ChineseRestaurantProcess(rpm,  n)
        n[z[i]] += 1

        if z[i] > K
            mu[z[i]] ~ H_mu
            sigma[z[i]] ~ H_sigma
            K += 1
        end

        y[i] ~ Normal(mu[z[i]], sigma[z[i]])
    end
end;

In this case, length(y) is 50 and Kmax is 30.

When I use a sampler like:

Kmax = 30
sample(dp_gmm_crp_delayed(y, Kmax), Gibbs(MH(0.1, :alpha), PG(10, :z), HMC(0.01, 100, :mu, :sigma)), iterations)

I get the following error (it seems like sampler doesn't play nicely with this model as the number of mixture components vary from iteration to iteration, yielding missing values in mu and sigma at times?):

TypeError: in typeassert, expected Float64, got ForwardDiff.Dual{Nothing,Float64,2}

Stacktrace:
 [1] setindex!(::Array{Float64,1}, ::ForwardDiff.Dual{ForwardDiff.Tag{Turing.Core.var"#f#7"{DynamicPPL.VarInfo{NamedTuple{(:alpha, :z, :mu, :sigma),Tuple{DynamicPPL.Metadata{Dict{DynamicPPL.VarName{:alpha,Tuple{}},Int64},Array{Gamma{Float64},1},Array{DynamicPPL.VarName{:alpha,Tuple{}},1},Array{Float64,1},Array{Set{DynamicPPL.Selector},1}},DynamicPPL.Metadata{Dict{DynamicPPL.VarName{:z,Tuple{Tuple{Int64}}},Int64},Array{ChineseRestaurantProcess{DirichletProcess{Float64},TArray{Int64,1}},1},Array{DynamicPPL.VarName{:z,Tuple{Tuple{Int64}}},1},Array{Int64,1},Array{Set{DynamicPPL.Selector},1}},DynamicPPL.Metadata{Dict{DynamicPPL.VarName{:mu,Tuple{Tuple{Int64}}},Int64},Array{Normal{Float64},1},Array{DynamicPPL.VarName{:mu,Tuple{Tuple{Int64}}},1},Array{Float64,1},Array{Set{DynamicPPL.Selector},1}},DynamicPPL.Metadata{Dict{DynamicPPL.VarName{:sigma,Tuple{Tuple{Int64}}},Int64},Array{Uniform{Float64},1},Array{DynamicPPL.VarName{:sigma,Tuple{Tuple{Int64}}},1},Array{Float64,1},Array{Set{DynamicPPL.Selector},1}}}},Float64},DynamicPPL.Model{var"###evaluator#311",(:y, :Kmax),Tuple{Array{Float64,1},Int64},(),DynamicPPL.ModelGen{var"###generator#312",(:y, :Kmax),(),Tuple{}}},DynamicPPL.Sampler{HMC{Turing.Core.ForwardDiffAD{40},(:mu, :sigma),AdvancedHMC.UnitEuclideanMetric},Turing.Inference.SamplerState{DynamicPPL.VarInfo{NamedTuple{(:alpha, :z, :mu, :sigma),Tuple{DynamicPPL.Metadata{Dict{DynamicPPL.VarName{:alpha,Tuple{}},Int64},Array{Gamma{Float64},1},Array{DynamicPPL.VarName{:alpha,Tuple{}},1},Array{Float64,1},Array{Set{DynamicPPL.Selector},1}},DynamicPPL.Metadata{Dict{DynamicPPL.VarName{:z,Tuple{Tuple{Int64}}},Int64},Array{ChineseRestaurantProcess{DirichletProcess{Float64},TArray{Int64,1}},1},Array{DynamicPPL.VarName{:z,Tuple{Tuple{Int64}}},1},Array{Int64,1},Array{Set{DynamicPPL.Selector},1}},DynamicPPL.Metadata{Dict{DynamicPPL.VarName{:mu,Tuple{Tuple{Int64}}},Int64},Array{Normal{Float64},1},Array{DynamicPPL.VarName{:mu,Tuple{Tuple{Int64}}},1},Array{Float64,1},Array{Set{DynamicPPL.Selector},1}},DynamicPPL.Metadata{Dict{DynamicPPL.VarName{:sigma,Tuple{Tuple{Int64}}},Int64},Array{Uniform{Float64},1},Array{DynamicPPL.VarName{:sigma,Tuple{Tuple{Int64}}},1},Array{Float64,1},Array{Set{DynamicPPL.Selector},1}}}},Float64}}}},Float64},Float64,2}, ::Int64) at ./array.jl:826
 [2] setindex!(::TArray{Float64,1}, ::ForwardDiff.Dual{ForwardDiff.Tag{Turing.Core.var"#f#7"{DynamicPPL.VarInfo{NamedTuple{(:alpha, :z, :mu, :sigma),Tuple{DynamicPPL.Metadata{Dict{DynamicPPL.VarName{:alpha,Tuple{}},Int64},Array{Gamma{Float64},1},Array{DynamicPPL.VarName{:alpha,Tuple{}},1},Array{Float64,1},Array{Set{DynamicPPL.Selector},1}},DynamicPPL.Metadata{Dict{DynamicPPL.VarName{:z,Tuple{Tuple{Int64}}},Int64},Array{ChineseRestaurantProcess{DirichletProcess{Float64},TArray{Int64,1}},1},Array{DynamicPPL.VarName{:z,Tuple{Tuple{Int64}}},1},Array{Int64,1},Array{Set{DynamicPPL.Selector},1}},DynamicPPL.Metadata{Dict{DynamicPPL.VarName{:mu,Tuple{Tuple{Int64}}},Int64},Array{Normal{Float64},1},Array{DynamicPPL.VarName{:mu,Tuple{Tuple{Int64}}},1},Array{Float64,1},Array{Set{DynamicPPL.Selector},1}},DynamicPPL.Metadata{Dict{DynamicPPL.VarName{:sigma,Tuple{Tuple{Int64}}},Int64},Array{Uniform{Float64},1},Array{DynamicPPL.VarName{:sigma,Tuple{Tuple{Int64}}},1},Array{Float64,1},Array{Set{DynamicPPL.Selector},1}}}},Float64},DynamicPPL.Model{var"###evaluator#311",(:y, :Kmax),Tuple{Array{Float64,1},Int64},(),DynamicPPL.ModelGen{var"###generator#312",(:y, :Kmax),(),Tuple{}}},DynamicPPL.Sampler{HMC{Turing.Core.ForwardDiffAD{40},(:mu, :sigma),AdvancedHMC.UnitEuclideanMetric},Turing.Inference.SamplerState{DynamicPPL.VarInfo{NamedTuple{(:alpha, :z, :mu, :sigma),Tuple{DynamicPPL.Metadata{Dict{DynamicPPL.VarName{:alpha,Tuple{}},Int64},Array{Gamma{Float64},1},Array{DynamicPPL.VarName{:alpha,Tuple{}},1},Array{Float64,1},Array{Set{DynamicPPL.Selector},1}},DynamicPPL.Metadata{Dict{DynamicPPL.VarName{:z,Tuple{Tuple{Int64}}},Int64},Array{ChineseRestaurantProcess{DirichletProcess{Float64},TArray{Int64,1}},1},Array{DynamicPPL.VarName{:z,Tuple{Tuple{Int64}}},1},Array{Int64,1},Array{Set{DynamicPPL.Selector},1}},DynamicPPL.Metadata{Dict{DynamicPPL.VarName{:mu,Tuple{Tuple{Int64}}},Int64},Array{Normal{Float64},1},Array{DynamicPPL.VarName{:mu,Tuple{Tuple{Int64}}},1},Array{Float64,1},Array{Set{DynamicPPL.Selector},1}},DynamicPPL.Metadata{Dict{DynamicPPL.VarName{:sigma,Tuple{Tuple{Int64}}},Int64},Array{Uniform{Float64},1},Array{DynamicPPL.VarName{:sigma,Tuple{Tuple{Int64}}},1},Array{Float64,1},Array{Set{DynamicPPL.Selector},1}}}},Float64}}}},Float64},Float64,2}, ::Int64) at /home/ubuntu/.julia/packages/Libtask/Zo6uM/src/tarray.jl:65
 [3] macro expansion at ./In[12]:26 [inlined]
 [4] ##evaluator#311(::Random._GLOBAL_RNG, ::DynamicPPL.Model{var"###evaluator#311",(:y, :Kmax),Tuple{Array{Float64,1},Int64},(),DynamicPPL.ModelGen{var"###generator#312",(:y, :Kmax),(),Tuple{}}}, ::DynamicPPL.VarInfo{NamedTuple{(:alpha, :z, :mu, :sigma),Tuple{DynamicPPL.Metadata{Dict{DynamicPPL.VarName{:alpha,Tuple{}},Int64},Array{Gamma{Float64},1},Array{DynamicPPL.VarName{:alpha,Tuple{}},1},Array{Float64,1},Array{Set{DynamicPPL.Selector},1}},DynamicPPL.Metadata{Dict{DynamicPPL.VarName{:z,Tuple{Tuple{Int64}}},Int64},Array{ChineseRestaurantProcess{DirichletProcess{Float64},TArray{Int64,1}},1},Array{DynamicPPL.VarName{:z,Tuple{Tuple{Int64}}},1},Array{Int64,1},Array{Set{DynamicPPL.Selector},1}},DynamicPPL.Metadata{Dict{DynamicPPL.VarName{:mu,Tuple{Tuple{Int64}}},Int64},Array{Normal{Float64},1},Array{DynamicPPL.VarName{:mu,Tuple{Tuple{Int64}}},1},Array{ForwardDiff.Dual{ForwardDiff.Tag{Turing.Core.var"#f#7"{DynamicPPL.VarInfo{NamedTuple{(:alpha, :z, :mu, :sigma),Tuple{DynamicPPL.Metadata{Dict{DynamicPPL.VarName{:alpha,Tuple{}},Int64},Array{Gamma{Float64},1},Array{DynamicPPL.VarName{:alpha,Tuple{}},1},Array{Float64,1},Array{Set{DynamicPPL.Selector},1}},DynamicPPL.Metadata{Dict{DynamicPPL.VarName{:z,Tuple{Tuple{Int64}}},Int64},Array{ChineseRestaurantProcess{DirichletProcess{Float64},TArray{Int64,1}},1},Array{DynamicPPL.VarName{:z,Tuple{Tuple{Int64}}},1},Array{Int64,1},Array{Set{DynamicPPL.Selector},1}},DynamicPPL.Metadata{Dict{DynamicPPL.VarName{:mu,Tuple{Tuple{Int64}}},Int64},Array{Normal{Float64},1},Array{DynamicPPL.VarName{:mu,Tuple{Tuple{Int64}}},1},Array{Float64,1},Array{Set{DynamicPPL.Selector},1}},DynamicPPL.Metadata{Dict{DynamicPPL.VarName{:sigma,Tuple{Tuple{Int64}}},Int64},Array{Uniform{Float64},1},Array{DynamicPPL.VarName{:sigma,Tuple{Tuple{Int64}}},1},Array{Float64,1},Array{Set{DynamicPPL.Selector},1}}}},Float64},DynamicPPL.Model{var"###evaluator#311",(:y, :Kmax),Tuple{Array{Float64,1},Int64},(),DynamicPPL.ModelGen{var"###generator#312",(:y, :Kmax),(),Tuple{}}},DynamicPPL.Sampler{HMC{Turing.Core.ForwardDiffAD{40},(:mu, :sigma),AdvancedHMC.UnitEuclideanMetric},Turing.Inference.SamplerState{DynamicPPL.VarInfo{NamedTuple{(:alpha, :z, :mu, :sigma),Tuple{DynamicPPL.Metadata{Dict{DynamicPPL.VarName{:alpha,Tuple{}},Int64},Array{Gamma{Float64},1},Array{DynamicPPL.VarName{:alpha,Tuple{}},1},Array{Float64,1},Array{Set{DynamicPPL.Selector},1}},DynamicPPL.Metadata{Dict{DynamicPPL.VarName{:z,Tuple{Tuple{Int64}}},Int64},Array{ChineseRestaurantProcess{DirichletProcess{Float64},TArray{Int64,1}},1},Array{DynamicPPL.VarName{:z,Tuple{Tuple{Int64}}},1},Array{Int64,1},Array{Set{DynamicPPL.Selector},1}},DynamicPPL.Metadata{Dict{DynamicPPL.VarName{:mu,Tuple{Tuple{Int64}}},Int64},Array{Normal{Float64},1},Array{DynamicPPL.VarName{:mu,Tuple{Tuple{Int64}}},1},Array{Float64,1},Array{Set{DynamicPPL.Selector},1}},DynamicPPL.Metadata{Dict{DynamicPPL.VarName{:sigma,Tuple{Tuple{Int64}}},Int64},Array{Uniform{Float64},1},Array{DynamicPPL.VarName{:sigma,Tuple{Tuple{Int64}}},1},Array{Float64,1},Array{Set{DynamicPPL.Selector},1}}}},Float64}}}},Float64},Float64,2},1},Array{Set{DynamicPPL.Selector},1}},DynamicPPL.Metadata{Dict{DynamicPPL.VarName{:sigma,Tuple{Tuple{Int64}}},Int64},Array{Uniform{Float64},1},Array{DynamicPPL.VarName{:sigma,Tuple{Tuple{Int64}}},1},Array{ForwardDiff.Dual{ForwardDiff.Tag{Turing.Core.var"#f#7"{DynamicPPL.VarInfo{NamedTuple{(:alpha, :z, :mu, :sigma),Tuple{DynamicPPL.Metadata{Dict{DynamicPPL.VarName{:alpha,Tuple{}},Int64},Array{Gamma{Float64},1},Array{DynamicPPL.VarName{:alpha,Tuple{}},1},Array{Float64,1},Array{Set{DynamicPPL.Selector},1}},DynamicPPL.Metadata{Dict{DynamicPPL.VarName{:z,Tuple{Tuple{Int64}}},Int64},Array{ChineseRestaurantProcess{DirichletProcess{Float64},TArray{Int64,1}},1},Array{DynamicPPL.VarName{:z,Tuple{Tuple{Int64}}},1},Array{Int64,1},Array{Set{DynamicPPL.Selector},1}},DynamicPPL.Metadata{Dict{DynamicPPL.VarName{:mu,Tuple{Tuple{Int64}}},Int64},Array{Normal{Float64},1},Array{DynamicPPL.VarName{:mu,Tuple{Tuple{Int64}}},1},Array{Float64,1},Array{Set{DynamicPPL.Selector},1}},DynamicPPL.Metadata{Dict{DynamicPPL.VarName{:sigma,Tuple{Tuple{Int64}}},Int64},Array{Uniform{Float64},1},Array{DynamicPPL.VarName{:sigma,Tuple{Tuple{Int64}}},1},Array{Float64,1},Array{Set{DynamicPPL.Selector},1}}}},Float64},DynamicPPL.Model{var"###evaluator#311",(:y, :Kmax),Tuple{Array{Float64,1},Int64},(),DynamicPPL.ModelGen{var"###generator#312",(:y, :Kmax),(),Tuple{}}},DynamicPPL.Sampler{HMC{Turing.Core.ForwardDiffAD{40},(:mu, :sigma),AdvancedHMC.UnitEuclideanMetric},Turing.Inference.SamplerState{DynamicPPL.VarInfo{NamedTuple{(:alpha, :z, :mu, :sigma),Tuple{DynamicPPL.Metadata{Dict{DynamicPPL.VarName{:alpha,Tuple{}},Int64},Array{Gamma{Float64},1},Array{DynamicPPL.VarName{:alpha,Tuple{}},1},Array{Float64,1},Array{Set{DynamicPPL.Selector},1}},DynamicPPL.Metadata{Dict{DynamicPPL.VarName{:z,Tuple{Tuple{Int64}}},Int64},Array{ChineseRestaurantProcess{DirichletProcess{Float64},TArray{Int64,1}},1},Array{DynamicPPL.VarName{:z,Tuple{Tuple{Int64}}},1},Array{Int64,1},Array{Set{DynamicPPL.Selector},1}},DynamicPPL.Metadata{Dict{DynamicPPL.VarName{:mu,Tuple{Tuple{Int64}}},Int64},Array{Normal{Float64},1},Array{DynamicPPL.VarName{:mu,Tuple{Tuple{Int64}}},1},Array{Float64,1},Array{Set{DynamicPPL.Selector},1}},DynamicPPL.Metadata{Dict{DynamicPPL.VarName{:sigma,Tuple{Tuple{Int64}}},Int64},Array{Uniform{Float64},1},Array{DynamicPPL.VarName{:sigma,Tuple{Tuple{Int64}}},1},Array{Float64,1},Array{Set{DynamicPPL.Selector},1}}}},Float64}}}},Float64},Float64,2},1},Array{Set{DynamicPPL.Selector},1}}}},ForwardDiff.Dual{ForwardDiff.Tag{Turing.Core.var"#f#7"{DynamicPPL.VarInfo{NamedTuple{(:alpha, :z, :mu, :sigma),Tuple{DynamicPPL.Metadata{Dict{DynamicPPL.VarName{:alpha,Tuple{}},Int64},Array{Gamma{Float64},1},Array{DynamicPPL.VarName{:alpha,Tuple{}},1},Array{Float64,1},Array{Set{DynamicPPL.Selector},1}},DynamicPPL.Metadata{Dict{DynamicPPL.VarName{:z,Tuple{Tuple{Int64}}},Int64},Array{ChineseRestaurantProcess{DirichletProcess{Float64},TArray{Int64,1}},1},Array{DynamicPPL.VarName{:z,Tuple{Tuple{Int64}}},1},Array{Int64,1},Array{Set{DynamicPPL.Selector},1}},DynamicPPL.Metadata{Dict{DynamicPPL.VarName{:mu,Tuple{Tuple{Int64}}},Int64},Array{Normal{Float64},1},Array{DynamicPPL.VarName{:mu,Tuple{Tuple{Int64}}},1},Array{Float64,1},Array{Set{DynamicPPL.Selector},1}},DynamicPPL.Metadata{Dict{DynamicPPL.VarName{:sigma,Tuple{Tuple{Int64}}},Int64},Array{Uniform{Float64},1},Array{DynamicPPL.VarName{:sigma,Tuple{Tuple{Int64}}},1},Array{Float64,1},Array{Set{DynamicPPL.Selector},1}}}},Float64},DynamicPPL.Model{var"###evaluator#311",(:y, :Kmax),Tuple{Array{Float64,1},Int64},(),DynamicPPL.ModelGen{var"###generator#312",(:y, :Kmax),(),Tuple{}}},DynamicPPL.Sampler{HMC{Turing.Core.ForwardDiffAD{40},(:mu, :sigma),AdvancedHMC.UnitEuclideanMetric},Turing.Inference.SamplerState{DynamicPPL.VarInfo{NamedTuple{(:alpha, :z, :mu, :sigma),Tuple{DynamicPPL.Metadata{Dict{DynamicPPL.VarName{:alpha,Tuple{}},Int64},Array{Gamma{Float64},1},Array{DynamicPPL.VarName{:alpha,Tuple{}},1},Array{Float64,1},Array{Set{DynamicPPL.Selector},1}},DynamicPPL.Metadata{Dict{DynamicPPL.VarName{:z,Tuple{Tuple{Int64}}},Int64},Array{ChineseRestaurantProcess{DirichletProcess{Float64},TArray{Int64,1}},1},Array{DynamicPPL.VarName{:z,Tuple{Tuple{Int64}}},1},Array{Int64,1},Array{Set{DynamicPPL.Selector},1}},DynamicPPL.Metadata{Dict{DynamicPPL.VarName{:mu,Tuple{Tuple{Int64}}},Int64},Array{Normal{Float64},1},Array{DynamicPPL.VarName{:mu,Tuple{Tuple{Int64}}},1},Array{Float64,1},Array{Set{DynamicPPL.Selector},1}},DynamicPPL.Metadata{Dict{DynamicPPL.VarName{:sigma,Tuple{Tuple{Int64}}},Int64},Array{Uniform{Float64},1},Array{DynamicPPL.VarName{:sigma,Tuple{Tuple{Int64}}},1},Array{Float64,1},Array{Set{DynamicPPL.Selector},1}}}},Float64}}}},Float64},Float64,2}}, ::DynamicPPL.Sampler{HMC{Turing.Core.ForwardDiffAD{40},(:mu, :sigma),AdvancedHMC.UnitEuclideanMetric},Turing.Inference.SamplerState{DynamicPPL.VarInfo{NamedTuple{(:alpha, :z, :mu, :sigma),Tuple{DynamicPPL.Metadata{Dict{DynamicPPL.VarName{:alpha,Tuple{}},Int64},Array{Gamma{Float64},1},Array{DynamicPPL.VarName{:alpha,Tuple{}},1},Array{Float64,1},Array{Set{DynamicPPL.Selector},1}},DynamicPPL.Metadata{Dict{DynamicPPL.VarName{:z,Tuple{Tuple{Int64}}},Int64},Array{ChineseRestaurantProcess{DirichletProcess{Float64},TArray{Int64,1}},1},Array{DynamicPPL.VarName{:z,Tuple{Tuple{Int64}}},1},Array{Int64,1},Array{Set{DynamicPPL.Selector},1}},DynamicPPL.Metadata{Dict{DynamicPPL.VarName{:mu,Tuple{Tuple{Int64}}},Int64},Array{Normal{Float64},1},Array{DynamicPPL.VarName{:mu,Tuple{Tuple{Int64}}},1},Array{Float64,1},Array{Set{DynamicPPL.Selector},1}},DynamicPPL.Metadata{Dict{DynamicPPL.VarName{:sigma,Tuple{Tuple{Int64}}},Int64},Array{Uniform{Float64},1},Array{DynamicPPL.VarName{:sigma,Tuple{Tuple{Int64}}},1},Array{Float64,1},Array{Set{DynamicPPL.Selector},1}}}},Float64}}}, ::DynamicPPL.DefaultContext) at /home/ubuntu/.julia/packages/DynamicPPL/MRwtL/src/compiler.jl:356
 [5] evaluate_threadunsafe at /home/ubuntu/.julia/packages/DynamicPPL/MRwtL/src/model.jl:157 [inlined]
 [6] (::DynamicPPL.Model{var"###evaluator#311",(:y, :Kmax),Tuple{Array{Float64,1},Int64},(),DynamicPPL.ModelGen{var"###generator#312",(:y, :Kmax),(),Tuple{}}})(::Random._GLOBAL_RNG, ::DynamicPPL.VarInfo{NamedTuple{(:alpha, :z, :mu, :sigma),Tuple{DynamicPPL.Metadata{Dict{DynamicPPL.VarName{:alpha,Tuple{}},Int64},Array{Gamma{Float64},1},Array{DynamicPPL.VarName{:alpha,Tuple{}},1},Array{Float64,1},Array{Set{DynamicPPL.Selector},1}},DynamicPPL.Metadata{Dict{DynamicPPL.VarName{:z,Tuple{Tuple{Int64}}},Int64},Array{ChineseRestaurantProcess{DirichletProcess{Float64},TArray{Int64,1}},1},Array{DynamicPPL.VarName{:z,Tuple{Tuple{Int64}}},1},Array{Int64,1},Array{Set{DynamicPPL.Selector},1}},DynamicPPL.Metadata{Dict{DynamicPPL.VarName{:mu,Tuple{Tuple{Int64}}},Int64},Array{Normal{Float64},1},Array{DynamicPPL.VarName{:mu,Tuple{Tuple{Int64}}},1},Array{ForwardDiff.Dual{ForwardDiff.Tag{Turing.Core.var"#f#7"{DynamicPPL.VarInfo{NamedTuple{(:alpha, :z, :mu, :sigma),Tuple{DynamicPPL.Metadata{Dict{DynamicPPL.VarName{:alpha,Tuple{}},Int64},Array{Gamma{Float64},1},Array{DynamicPPL.VarName{:alpha,Tuple{}},1},Array{Float64,1},Array{Set{DynamicPPL.Selector},1}},DynamicPPL.Metadata{Dict{DynamicPPL.VarName{:z,Tuple{Tuple{Int64}}},Int64},Array{ChineseRestaurantProcess{DirichletProcess{Float64},TArray{Int64,1}},1},Array{DynamicPPL.VarName{:z,Tuple{Tuple{Int64}}},1},Array{Int64,1},Array{Set{DynamicPPL.Selector},1}},DynamicPPL.Metadata{Dict{DynamicPPL.VarName{:mu,Tuple{Tuple{Int64}}},Int64},Array{Normal{Float64},1},Array{DynamicPPL.VarName{:mu,Tuple{Tuple{Int64}}},1},Array{Float64,1},Array{Set{DynamicPPL.Selector},1}},DynamicPPL.Metadata{Dict{DynamicPPL.VarName{:sigma,Tuple{Tuple{Int64}}},Int64},Array{Uniform{Float64},1},Array{DynamicPPL.VarName{:sigma,Tuple{Tuple{Int64}}},1},Array{Float64,1},Array{Set{DynamicPPL.Selector},1}}}},Float64},DynamicPPL.Model{var"###evaluator#311",(:y, :Kmax),Tuple{Array{Float64,1},Int64},(),DynamicPPL.ModelGen{var"###generator#312",(:y, :Kmax),(),Tuple{}}},DynamicPPL.Sampler{HMC{Turing.Core.ForwardDiffAD{40},(:mu, :sigma),AdvancedHMC.UnitEuclideanMetric},Turing.Inference.SamplerState{DynamicPPL.VarInfo{NamedTuple{(:alpha, :z, :mu, :sigma),Tuple{DynamicPPL.Metadata{Dict{DynamicPPL.VarName{:alpha,Tuple{}},Int64},Array{Gamma{Float64},1},Array{DynamicPPL.VarName{:alpha,Tuple{}},1},Array{Float64,1},Array{Set{DynamicPPL.Selector},1}},DynamicPPL.Metadata{Dict{DynamicPPL.VarName{:z,Tuple{Tuple{Int64}}},Int64},Array{ChineseRestaurantProcess{DirichletProcess{Float64},TArray{Int64,1}},1},Array{DynamicPPL.VarName{:z,Tuple{Tuple{Int64}}},1},Array{Int64,1},Array{Set{DynamicPPL.Selector},1}},DynamicPPL.Metadata{Dict{DynamicPPL.VarName{:mu,Tuple{Tuple{Int64}}},Int64},Array{Normal{Float64},1},Array{DynamicPPL.VarName{:mu,Tuple{Tuple{Int64}}},1},Array{Float64,1},Array{Set{DynamicPPL.Selector},1}},DynamicPPL.Metadata{Dict{DynamicPPL.VarName{:sigma,Tuple{Tuple{Int64}}},Int64},Array{Uniform{Float64},1},Array{DynamicPPL.VarName{:sigma,Tuple{Tuple{Int64}}},1},Array{Float64,1},Array{Set{DynamicPPL.Selector},1}}}},Float64}}}},Float64},Float64,2},1},Array{Set{DynamicPPL.Selector},1}},DynamicPPL.Metadata{Dict{DynamicPPL.VarName{:sigma,Tuple{Tuple{Int64}}},Int64},Array{Uniform{Float64},1},Array{DynamicPPL.VarName{:sigma,Tuple{Tuple{Int64}}},1},Array{ForwardDiff.Dual{ForwardDiff.Tag{Turing.Core.var"#f#7"{DynamicPPL.VarInfo{NamedTuple{(:alpha, :z, :mu, :sigma),Tuple{DynamicPPL.Metadata{Dict{DynamicPPL.VarName{:alpha,Tuple{}},Int64},Array{Gamma{Float64},1},Array{DynamicPPL.VarName{:alpha,Tuple{}},1},Array{Float64,1},Array{Set{DynamicPPL.Selector},1}},DynamicPPL.Metadata{Dict{DynamicPPL.VarName{:z,Tuple{Tuple{Int64}}},Int64},Array{ChineseRestaurantProcess{DirichletProcess{Float64},TArray{Int64,1}},1},Array{DynamicPPL.VarName{:z,Tuple{Tuple{Int64}}},1},Array{Int64,1},Array{Set{DynamicPPL.Selector},1}},DynamicPPL.Metadata{Dict{DynamicPPL.VarName{:mu,Tuple{Tuple{Int64}}},Int64},Array{Normal{Float64},1},Array{DynamicPPL.VarName{:mu,Tuple{Tuple{Int64}}},1},Array{Float64,1},Array{Set{DynamicPPL.Selector},1}},DynamicPPL.Metadata{Dict{DynamicPPL.VarName{:sigma,Tuple{Tuple{Int64}}},Int64},Array{Uniform{Float64},1},Array{DynamicPPL.VarName{:sigma,Tuple{Tuple{Int64}}},1},Array{Float64,1},Array{Set{DynamicPPL.Selector},1}}}},Float64},DynamicPPL.Model{var"###evaluator#311",(:y, :Kmax),Tuple{Array{Float64,1},Int64},(),DynamicPPL.ModelGen{var"###generator#312",(:y, :Kmax),(),Tuple{}}},DynamicPPL.Sampler{HMC{Turing.Core.ForwardDiffAD{40},(:mu, :sigma),AdvancedHMC.UnitEuclideanMetric},Turing.Inference.SamplerState{DynamicPPL.VarInfo{NamedTuple{(:alpha, :z, :mu, :sigma),Tuple{DynamicPPL.Metadata{Dict{DynamicPPL.VarName{:alpha,Tuple{}},Int64},Array{Gamma{Float64},1},Array{DynamicPPL.VarName{:alpha,Tuple{}},1},Array{Float64,1},Array{Set{DynamicPPL.Selector},1}},DynamicPPL.Metadata{Dict{DynamicPPL.VarName{:z,Tuple{Tuple{Int64}}},Int64},Array{ChineseRestaurantProcess{DirichletProcess{Float64},TArray{Int64,1}},1},Array{DynamicPPL.VarName{:z,Tuple{Tuple{Int64}}},1},Array{Int64,1},Array{Set{DynamicPPL.Selector},1}},DynamicPPL.Metadata{Dict{DynamicPPL.VarName{:mu,Tuple{Tuple{Int64}}},Int64},Array{Normal{Float64},1},Array{DynamicPPL.VarName{:mu,Tuple{Tuple{Int64}}},1},Array{Float64,1},Array{Set{DynamicPPL.Selector},1}},DynamicPPL.Metadata{Dict{DynamicPPL.VarName{:sigma,Tuple{Tuple{Int64}}},Int64},Array{Uniform{Float64},1},Array{DynamicPPL.VarName{:sigma,Tuple{Tuple{Int64}}},1},Array{Float64,1},Array{Set{DynamicPPL.Selector},1}}}},Float64}}}},Float64},Float64,2},1},Array{Set{DynamicPPL.Selector},1}}}},ForwardDiff.Dual{ForwardDiff.Tag{Turing.Core.var"#f#7"{DynamicPPL.VarInfo{NamedTuple{(:alpha, :z, :mu, :sigma),Tuple{DynamicPPL.Metadata{Dict{DynamicPPL.VarName{:alpha,Tuple{}},Int64},Array{Gamma{Float64},1},Array{DynamicPPL.VarName{:alpha,Tuple{}},1},Array{Float64,1},Array{Set{DynamicPPL.Selector},1}},DynamicPPL.Metadata{Dict{DynamicPPL.VarName{:z,Tuple{Tuple{Int64}}},Int64},Array{ChineseRestaurantProcess{DirichletProcess{Float64},TArray{Int64,1}},1},Array{DynamicPPL.VarName{:z,Tuple{Tuple{Int64}}},1},Array{Int64,1},Array{Set{DynamicPPL.Selector},1}},DynamicPPL.Metadata{Dict{DynamicPPL.VarName{:mu,Tuple{Tuple{Int64}}},Int64},Array{Normal{Float64},1},Array{DynamicPPL.VarName{:mu,Tuple{Tuple{Int64}}},1},Array{Float64,1},Array{Set{DynamicPPL.Selector},1}},DynamicPPL.Metadata{Dict{DynamicPPL.VarName{:sigma,Tuple{Tuple{Int64}}},Int64},Array{Uniform{Float64},1},Array{DynamicPPL.VarName{:sigma,Tuple{Tuple{Int64}}},1},Array{Float64,1},Array{Set{DynamicPPL.Selector},1}}}},Float64},DynamicPPL.Model{var"###evaluator#311",(:y, :Kmax),Tuple{Array{Float64,1},Int64},(),DynamicPPL.ModelGen{var"###generator#312",(:y, :Kmax),(),Tuple{}}},DynamicPPL.Sampler{HMC{Turing.Core.ForwardDiffAD{40},(:mu, :sigma),AdvancedHMC.UnitEuclideanMetric},Turing.Inference.SamplerState{DynamicPPL.VarInfo{NamedTuple{(:alpha, :z, :mu, :sigma),Tuple{DynamicPPL.Metadata{Dict{DynamicPPL.VarName{:alpha,Tuple{}},Int64},Array{Gamma{Float64},1},Array{DynamicPPL.VarName{:alpha,Tuple{}},1},Array{Float64,1},Array{Set{DynamicPPL.Selector},1}},DynamicPPL.Metadata{Dict{DynamicPPL.VarName{:z,Tuple{Tuple{Int64}}},Int64},Array{ChineseRestaurantProcess{DirichletProcess{Float64},TArray{Int64,1}},1},Array{DynamicPPL.VarName{:z,Tuple{Tuple{Int64}}},1},Array{Int64,1},Array{Set{DynamicPPL.Selector},1}},DynamicPPL.Metadata{Dict{DynamicPPL.VarName{:mu,Tuple{Tuple{Int64}}},Int64},Array{Normal{Float64},1},Array{DynamicPPL.VarName{:mu,Tuple{Tuple{Int64}}},1},Array{Float64,1},Array{Set{DynamicPPL.Selector},1}},DynamicPPL.Metadata{Dict{DynamicPPL.VarName{:sigma,Tuple{Tuple{Int64}}},Int64},Array{Uniform{Float64},1},Array{DynamicPPL.VarName{:sigma,Tuple{Tuple{Int64}}},1},Array{Float64,1},Array{Set{DynamicPPL.Selector},1}}}},Float64}}}},Float64},Float64,2}}, ::DynamicPPL.Sampler{HMC{Turing.Core.ForwardDiffAD{40},(:mu, :sigma),AdvancedHMC.UnitEuclideanMetric},Turing.Inference.SamplerState{DynamicPPL.VarInfo{NamedTuple{(:alpha, :z, :mu, :sigma),Tuple{DynamicPPL.Metadata{Dict{DynamicPPL.VarName{:alpha,Tuple{}},Int64},Array{Gamma{Float64},1},Array{DynamicPPL.VarName{:alpha,Tuple{}},1},Array{Float64,1},Array{Set{DynamicPPL.Selector},1}},DynamicPPL.Metadata{Dict{DynamicPPL.VarName{:z,Tuple{Tuple{Int64}}},Int64},Array{ChineseRestaurantProcess{DirichletProcess{Float64},TArray{Int64,1}},1},Array{DynamicPPL.VarName{:z,Tuple{Tuple{Int64}}},1},Array{Int64,1},Array{Set{DynamicPPL.Selector},1}},DynamicPPL.Metadata{Dict{DynamicPPL.VarName{:mu,Tuple{Tuple{Int64}}},Int64},Array{Normal{Float64},1},Array{DynamicPPL.VarName{:mu,Tuple{Tuple{Int64}}},1},Array{Float64,1},Array{Set{DynamicPPL.Selector},1}},DynamicPPL.Metadata{Dict{DynamicPPL.VarName{:sigma,Tuple{Tuple{Int64}}},Int64},Array{Uniform{Float64},1},Array{DynamicPPL.VarName{:sigma,Tuple{Tuple{Int64}}},1},Array{Float64,1},Array{Set{DynamicPPL.Selector},1}}}},Float64}}}, ::DynamicPPL.DefaultContext) at /home/ubuntu/.julia/packages/DynamicPPL/MRwtL/src/model.jl:136
 [7] Model at /home/ubuntu/.julia/packages/DynamicPPL/MRwtL/src/model.jl:135 [inlined]
 [8] Model at /home/ubuntu/.julia/packages/DynamicPPL/MRwtL/src/model.jl:126 [inlined]
 [9] f at /home/ubuntu/.julia/packages/Turing/NZ9IU/src/core/ad.jl:104 [inlined]
 [10] vector_mode_dual_eval(::Turing.Core.var"#f#7"{DynamicPPL.VarInfo{NamedTuple{(:alpha, :z, :mu, :sigma),Tuple{DynamicPPL.Metadata{Dict{DynamicPPL.VarName{:alpha,Tuple{}},Int64},Array{Gamma{Float64},1},Array{DynamicPPL.VarName{:alpha,Tuple{}},1},Array{Float64,1},Array{Set{DynamicPPL.Selector},1}},DynamicPPL.Metadata{Dict{DynamicPPL.VarName{:z,Tuple{Tuple{Int64}}},Int64},Array{ChineseRestaurantProcess{DirichletProcess{Float64},TArray{Int64,1}},1},Array{DynamicPPL.VarName{:z,Tuple{Tuple{Int64}}},1},Array{Int64,1},Array{Set{DynamicPPL.Selector},1}},DynamicPPL.Metadata{Dict{DynamicPPL.VarName{:mu,Tuple{Tuple{Int64}}},Int64},Array{Normal{Float64},1},Array{DynamicPPL.VarName{:mu,Tuple{Tuple{Int64}}},1},Array{Float64,1},Array{Set{DynamicPPL.Selector},1}},DynamicPPL.Metadata{Dict{DynamicPPL.VarName{:sigma,Tuple{Tuple{Int64}}},Int64},Array{Uniform{Float64},1},Array{DynamicPPL.VarName{:sigma,Tuple{Tuple{Int64}}},1},Array{Float64,1},Array{Set{DynamicPPL.Selector},1}}}},Float64},DynamicPPL.Model{var"###evaluator#311",(:y, :Kmax),Tuple{Array{Float64,1},Int64},(),DynamicPPL.ModelGen{var"###generator#312",(:y, :Kmax),(),Tuple{}}},DynamicPPL.Sampler{HMC{Turing.Core.ForwardDiffAD{40},(:mu, :sigma),AdvancedHMC.UnitEuclideanMetric},Turing.Inference.SamplerState{DynamicPPL.VarInfo{NamedTuple{(:alpha, :z, :mu, :sigma),Tuple{DynamicPPL.Metadata{Dict{DynamicPPL.VarName{:alpha,Tuple{}},Int64},Array{Gamma{Float64},1},Array{DynamicPPL.VarName{:alpha,Tuple{}},1},Array{Float64,1},Array{Set{DynamicPPL.Selector},1}},DynamicPPL.Metadata{Dict{DynamicPPL.VarName{:z,Tuple{Tuple{Int64}}},Int64},Array{ChineseRestaurantProcess{DirichletProcess{Float64},TArray{Int64,1}},1},Array{DynamicPPL.VarName{:z,Tuple{Tuple{Int64}}},1},Array{Int64,1},Array{Set{DynamicPPL.Selector},1}},DynamicPPL.Metadata{Dict{DynamicPPL.VarName{:mu,Tuple{Tuple{Int64}}},Int64},Array{Normal{Float64},1},Array{DynamicPPL.VarName{:mu,Tuple{Tuple{Int64}}},1},Array{Float64,1},Array{Set{DynamicPPL.Selector},1}},DynamicPPL.Metadata{Dict{DynamicPPL.VarName{:sigma,Tuple{Tuple{Int64}}},Int64},Array{Uniform{Float64},1},Array{DynamicPPL.VarName{:sigma,Tuple{Tuple{Int64}}},1},Array{Float64,1},Array{Set{DynamicPPL.Selector},1}}}},Float64}}}}, ::Array{Float64,1}, ::ForwardDiff.GradientConfig{ForwardDiff.Tag{Turing.Core.var"#f#7"{DynamicPPL.VarInfo{NamedTuple{(:alpha, :z, :mu, :sigma),Tuple{DynamicPPL.Metadata{Dict{DynamicPPL.VarName{:alpha,Tuple{}},Int64},Array{Gamma{Float64},1},Array{DynamicPPL.VarName{:alpha,Tuple{}},1},Array{Float64,1},Array{Set{DynamicPPL.Selector},1}},DynamicPPL.Metadata{Dict{DynamicPPL.VarName{:z,Tuple{Tuple{Int64}}},Int64},Array{ChineseRestaurantProcess{DirichletProcess{Float64},TArray{Int64,1}},1},Array{DynamicPPL.VarName{:z,Tuple{Tuple{Int64}}},1},Array{Int64,1},Array{Set{DynamicPPL.Selector},1}},DynamicPPL.Metadata{Dict{DynamicPPL.VarName{:mu,Tuple{Tuple{Int64}}},Int64},Array{Normal{Float64},1},Array{DynamicPPL.VarName{:mu,Tuple{Tuple{Int64}}},1},Array{Float64,1},Array{Set{DynamicPPL.Selector},1}},DynamicPPL.Metadata{Dict{DynamicPPL.VarName{:sigma,Tuple{Tuple{Int64}}},Int64},Array{Uniform{Float64},1},Array{DynamicPPL.VarName{:sigma,Tuple{Tuple{Int64}}},1},Array{Float64,1},Array{Set{DynamicPPL.Selector},1}}}},Float64},DynamicPPL.Model{var"###evaluator#311",(:y, :Kmax),Tuple{Array{Float64,1},Int64},(),DynamicPPL.ModelGen{var"###generator#312",(:y, :Kmax),(),Tuple{}}},DynamicPPL.Sampler{HMC{Turing.Core.ForwardDiffAD{40},(:mu, :sigma),AdvancedHMC.UnitEuclideanMetric},Turing.Inference.SamplerState{DynamicPPL.VarInfo{NamedTuple{(:alpha, :z, :mu, :sigma),Tuple{DynamicPPL.Metadata{Dict{DynamicPPL.VarName{:alpha,Tuple{}},Int64},Array{Gamma{Float64},1},Array{DynamicPPL.VarName{:alpha,Tuple{}},1},Array{Float64,1},Array{Set{DynamicPPL.Selector},1}},DynamicPPL.Metadata{Dict{DynamicPPL.VarName{:z,Tuple{Tuple{Int64}}},Int64},Array{ChineseRestaurantProcess{DirichletProcess{Float64},TArray{Int64,1}},1},Array{DynamicPPL.VarName{:z,Tuple{Tuple{Int64}}},1},Array{Int64,1},Array{Set{DynamicPPL.Selector},1}},DynamicPPL.Metadata{Dict{DynamicPPL.VarName{:mu,Tuple{Tuple{Int64}}},Int64},Array{Normal{Float64},1},Array{DynamicPPL.VarName{:mu,Tuple{Tuple{Int64}}},1},Array{Float64,1},Array{Set{DynamicPPL.Selector},1}},DynamicPPL.Metadata{Dict{DynamicPPL.VarName{:sigma,Tuple{Tuple{Int64}}},Int64},Array{Uniform{Float64},1},Array{DynamicPPL.VarName{:sigma,Tuple{Tuple{Int64}}},1},Array{Float64,1},Array{Set{DynamicPPL.Selector},1}}}},Float64}}}},Float64},Float64,2,Array{ForwardDiff.Dual{ForwardDiff.Tag{Turing.Core.var"#f#7"{DynamicPPL.VarInfo{NamedTuple{(:alpha, :z, :mu, :sigma),Tuple{DynamicPPL.Metadata{Dict{DynamicPPL.VarName{:alpha,Tuple{}},Int64},Array{Gamma{Float64},1},Array{DynamicPPL.VarName{:alpha,Tuple{}},1},Array{Float64,1},Array{Set{DynamicPPL.Selector},1}},DynamicPPL.Metadata{Dict{DynamicPPL.VarName{:z,Tuple{Tuple{Int64}}},Int64},Array{ChineseRestaurantProcess{DirichletProcess{Float64},TArray{Int64,1}},1},Array{DynamicPPL.VarName{:z,Tuple{Tuple{Int64}}},1},Array{Int64,1},Array{Set{DynamicPPL.Selector},1}},DynamicPPL.Metadata{Dict{DynamicPPL.VarName{:mu,Tuple{Tuple{Int64}}},Int64},Array{Normal{Float64},1},Array{DynamicPPL.VarName{:mu,Tuple{Tuple{Int64}}},1},Array{Float64,1},Array{Set{DynamicPPL.Selector},1}},DynamicPPL.Metadata{Dict{DynamicPPL.VarName{:sigma,Tuple{Tuple{Int64}}},Int64},Array{Uniform{Float64},1},Array{DynamicPPL.VarName{:sigma,Tuple{Tuple{Int64}}},1},Array{Float64,1},Array{Set{DynamicPPL.Selector},1}}}},Float64},DynamicPPL.Model{var"###evaluator#311",(:y, :Kmax),Tuple{Array{Float64,1},Int64},(),DynamicPPL.ModelGen{var"###generator#312",(:y, :Kmax),(),Tuple{}}},DynamicPPL.Sampler{HMC{Turing.Core.ForwardDiffAD{40},(:mu, :sigma),AdvancedHMC.UnitEuclideanMetric},Turing.Inference.SamplerState{DynamicPPL.VarInfo{NamedTuple{(:alpha, :z, :mu, :sigma),Tuple{DynamicPPL.Metadata{Dict{DynamicPPL.VarName{:alpha,Tuple{}},Int64},Array{Gamma{Float64},1},Array{DynamicPPL.VarName{:alpha,Tuple{}},1},Array{Float64,1},Array{Set{DynamicPPL.Selector},1}},DynamicPPL.Metadata{Dict{DynamicPPL.VarName{:z,Tuple{Tuple{Int64}}},Int64},Array{ChineseRestaurantProcess{DirichletProcess{Float64},TArray{Int64,1}},1},Array{DynamicPPL.VarName{:z,Tuple{Tuple{Int64}}},1},Array{Int64,1},Array{Set{DynamicPPL.Selector},1}},DynamicPPL.Metadata{Dict{DynamicPPL.VarName{:mu,Tuple{Tuple{Int64}}},Int64},Array{Normal{Float64},1},Array{DynamicPPL.VarName{:mu,Tuple{Tuple{Int64}}},1},Array{Float64,1},Array{Set{DynamicPPL.Selector},1}},DynamicPPL.Metadata{Dict{DynamicPPL.VarName{:sigma,Tuple{Tuple{Int64}}},Int64},Array{Uniform{Float64},1},Array{DynamicPPL.VarName{:sigma,Tuple{Tuple{Int64}}},1},Array{Float64,1},Array{Set{DynamicPPL.Selector},1}}}},Float64}}}},Float64},Float64,2},1}}) at /home/ubuntu/.julia/packages/ForwardDiff/sdToQ/src/apiutils.jl:37
 [11] vector_mode_gradient! at /home/ubuntu/.julia/packages/ForwardDiff/sdToQ/src/gradient.jl:103 [inlined]
 [12] gradient!(::Array{Float64,1}, ::Turing.Core.var"#f#7"{DynamicPPL.VarInfo{NamedTuple{(:alpha, :z, :mu, :sigma),Tuple{DynamicPPL.Metadata{Dict{DynamicPPL.VarName{:alpha,Tuple{}},Int64},Array{Gamma{Float64},1},Array{DynamicPPL.VarName{:alpha,Tuple{}},1},Array{Float64,1},Array{Set{DynamicPPL.Selector},1}},DynamicPPL.Metadata{Dict{DynamicPPL.VarName{:z,Tuple{Tuple{Int64}}},Int64},Array{ChineseRestaurantProcess{DirichletProcess{Float64},TArray{Int64,1}},1},Array{DynamicPPL.VarName{:z,Tuple{Tuple{Int64}}},1},Array{Int64,1},Array{Set{DynamicPPL.Selector},1}},DynamicPPL.Metadata{Dict{DynamicPPL.VarName{:mu,Tuple{Tuple{Int64}}},Int64},Array{Normal{Float64},1},Array{DynamicPPL.VarName{:mu,Tuple{Tuple{Int64}}},1},Array{Float64,1},Array{Set{DynamicPPL.Selector},1}},DynamicPPL.Metadata{Dict{DynamicPPL.VarName{:sigma,Tuple{Tuple{Int64}}},Int64},Array{Uniform{Float64},1},Array{DynamicPPL.VarName{:sigma,Tuple{Tuple{Int64}}},1},Array{Float64,1},Array{Set{DynamicPPL.Selector},1}}}},Float64},DynamicPPL.Model{var"###evaluator#311",(:y, :Kmax),Tuple{Array{Float64,1},Int64},(),DynamicPPL.ModelGen{var"###generator#312",(:y, :Kmax),(),Tuple{}}},DynamicPPL.Sampler{HMC{Turing.Core.ForwardDiffAD{40},(:mu, :sigma),AdvancedHMC.UnitEuclideanMetric},Turing.Inference.SamplerState{DynamicPPL.VarInfo{NamedTuple{(:alpha, :z, :mu, :sigma),Tuple{DynamicPPL.Metadata{Dict{DynamicPPL.VarName{:alpha,Tuple{}},Int64},Array{Gamma{Float64},1},Array{DynamicPPL.VarName{:alpha,Tuple{}},1},Array{Float64,1},Array{Set{DynamicPPL.Selector},1}},DynamicPPL.Metadata{Dict{DynamicPPL.VarName{:z,Tuple{Tuple{Int64}}},Int64},Array{ChineseRestaurantProcess{DirichletProcess{Float64},TArray{Int64,1}},1},Array{DynamicPPL.VarName{:z,Tuple{Tuple{Int64}}},1},Array{Int64,1},Array{Set{DynamicPPL.Selector},1}},DynamicPPL.Metadata{Dict{DynamicPPL.VarName{:mu,Tuple{Tuple{Int64}}},Int64},Array{Normal{Float64},1},Array{DynamicPPL.VarName{:mu,Tuple{Tuple{Int64}}},1},Array{Float64,1},Array{Set{DynamicPPL.Selector},1}},DynamicPPL.Metadata{Dict{DynamicPPL.VarName{:sigma,Tuple{Tuple{Int64}}},Int64},Array{Uniform{Float64},1},Array{DynamicPPL.VarName{:sigma,Tuple{Tuple{Int64}}},1},Array{Float64,1},Array{Set{DynamicPPL.Selector},1}}}},Float64}}}}, ::Array{Float64,1}, ::ForwardDiff.GradientConfig{ForwardDiff.Tag{Turing.Core.var"#f#7"{DynamicPPL.VarInfo{NamedTuple{(:alpha, :z, :mu, :sigma),Tuple{DynamicPPL.Metadata{Dict{DynamicPPL.VarName{:alpha,Tuple{}},Int64},Array{Gamma{Float64},1},Array{DynamicPPL.VarName{:alpha,Tuple{}},1},Array{Float64,1},Array{Set{DynamicPPL.Selector},1}},DynamicPPL.Metadata{Dict{DynamicPPL.VarName{:z,Tuple{Tuple{Int64}}},Int64},Array{ChineseRestaurantProcess{DirichletProcess{Float64},TArray{Int64,1}},1},Array{DynamicPPL.VarName{:z,Tuple{Tuple{Int64}}},1},Array{Int64,1},Array{Set{DynamicPPL.Selector},1}},DynamicPPL.Metadata{Dict{DynamicPPL.VarName{:mu,Tuple{Tuple{Int64}}},Int64},Array{Normal{Float64},1},Array{DynamicPPL.VarName{:mu,Tuple{Tuple{Int64}}},1},Array{Float64,1},Array{Set{DynamicPPL.Selector},1}},DynamicPPL.Metadata{Dict{DynamicPPL.VarName{:sigma,Tuple{Tuple{Int64}}},Int64},Array{Uniform{Float64},1},Array{DynamicPPL.VarName{:sigma,Tuple{Tuple{Int64}}},1},Array{Float64,1},Array{Set{DynamicPPL.Selector},1}}}},Float64},DynamicPPL.Model{var"###evaluator#311",(:y, :Kmax),Tuple{Array{Float64,1},Int64},(),DynamicPPL.ModelGen{var"###generator#312",(:y, :Kmax),(),Tuple{}}},DynamicPPL.Sampler{HMC{Turing.Core.ForwardDiffAD{40},(:mu, :sigma),AdvancedHMC.UnitEuclideanMetric},Turing.Inference.SamplerState{DynamicPPL.VarInfo{NamedTuple{(:alpha, :z, :mu, :sigma),Tuple{DynamicPPL.Metadata{Dict{DynamicPPL.VarName{:alpha,Tuple{}},Int64},Array{Gamma{Float64},1},Array{DynamicPPL.VarName{:alpha,Tuple{}},1},Array{Float64,1},Array{Set{DynamicPPL.Selector},1}},DynamicPPL.Metadata{Dict{DynamicPPL.VarName{:z,Tuple{Tuple{Int64}}},Int64},Array{ChineseRestaurantProcess{DirichletProcess{Float64},TArray{Int64,1}},1},Array{DynamicPPL.VarName{:z,Tuple{Tuple{Int64}}},1},Array{Int64,1},Array{Set{DynamicPPL.Selector},1}},DynamicPPL.Metadata{Dict{DynamicPPL.VarName{:mu,Tuple{Tuple{Int64}}},Int64},Array{Normal{Float64},1},Array{DynamicPPL.VarName{:mu,Tuple{Tuple{Int64}}},1},Array{Float64,1},Array{Set{DynamicPPL.Selector},1}},DynamicPPL.Metadata{Dict{DynamicPPL.VarName{:sigma,Tuple{Tuple{Int64}}},Int64},Array{Uniform{Float64},1},Array{DynamicPPL.VarName{:sigma,Tuple{Tuple{Int64}}},1},Array{Float64,1},Array{Set{DynamicPPL.Selector},1}}}},Float64}}}},Float64},Float64,2,Array{ForwardDiff.Dual{ForwardDiff.Tag{Turing.Core.var"#f#7"{DynamicPPL.VarInfo{NamedTuple{(:alpha, :z, :mu, :sigma),Tuple{DynamicPPL.Metadata{Dict{DynamicPPL.VarName{:alpha,Tuple{}},Int64},Array{Gamma{Float64},1},Array{DynamicPPL.VarName{:alpha,Tuple{}},1},Array{Float64,1},Array{Set{DynamicPPL.Selector},1}},DynamicPPL.Metadata{Dict{DynamicPPL.VarName{:z,Tuple{Tuple{Int64}}},Int64},Array{ChineseRestaurantProcess{DirichletProcess{Float64},TArray{Int64,1}},1},Array{DynamicPPL.VarName{:z,Tuple{Tuple{Int64}}},1},Array{Int64,1},Array{Set{DynamicPPL.Selector},1}},DynamicPPL.Metadata{Dict{DynamicPPL.VarName{:mu,Tuple{Tuple{Int64}}},Int64},Array{Normal{Float64},1},Array{DynamicPPL.VarName{:mu,Tuple{Tuple{Int64}}},1},Array{Float64,1},Array{Set{DynamicPPL.Selector},1}},DynamicPPL.Metadata{Dict{DynamicPPL.VarName{:sigma,Tuple{Tuple{Int64}}},Int64},Array{Uniform{Float64},1},Array{DynamicPPL.VarName{:sigma,Tuple{Tuple{Int64}}},1},Array{Float64,1},Array{Set{DynamicPPL.Selector},1}}}},Float64},DynamicPPL.Model{var"###evaluator#311",(:y, :Kmax),Tuple{Array{Float64,1},Int64},(),DynamicPPL.ModelGen{var"###generator#312",(:y, :Kmax),(),Tuple{}}},DynamicPPL.Sampler{HMC{Turing.Core.ForwardDiffAD{40},(:mu, :sigma),AdvancedHMC.UnitEuclideanMetric},Turing.Inference.SamplerState{DynamicPPL.VarInfo{NamedTuple{(:alpha, :z, :mu, :sigma),Tuple{DynamicPPL.Metadata{Dict{DynamicPPL.VarName{:alpha,Tuple{}},Int64},Array{Gamma{Float64},1},Array{DynamicPPL.VarName{:alpha,Tuple{}},1},Array{Float64,1},Array{Set{DynamicPPL.Selector},1}},DynamicPPL.Metadata{Dict{DynamicPPL.VarName{:z,Tuple{Tuple{Int64}}},Int64},Array{ChineseRestaurantProcess{DirichletProcess{Float64},TArray{Int64,1}},1},Array{DynamicPPL.VarName{:z,Tuple{Tuple{Int64}}},1},Array{Int64,1},Array{Set{DynamicPPL.Selector},1}},DynamicPPL.Metadata{Dict{DynamicPPL.VarName{:mu,Tuple{Tuple{Int64}}},Int64},Array{Normal{Float64},1},Array{DynamicPPL.VarName{:mu,Tuple{Tuple{Int64}}},1},Array{Float64,1},Array{Set{DynamicPPL.Selector},1}},DynamicPPL.Metadata{Dict{DynamicPPL.VarName{:sigma,Tuple{Tuple{Int64}}},Int64},Array{Uniform{Float64},1},Array{DynamicPPL.VarName{:sigma,Tuple{Tuple{Int64}}},1},Array{Float64,1},Array{Set{DynamicPPL.Selector},1}}}},Float64}}}},Float64},Float64,2},1}}, ::Val{true}) at /home/ubuntu/.julia/packages/ForwardDiff/sdToQ/src/gradient.jl:35
 [13] gradient!(::Array{Float64,1}, ::Turing.Core.var"#f#7"{DynamicPPL.VarInfo{NamedTuple{(:alpha, :z, :mu, :sigma),Tuple{DynamicPPL.Metadata{Dict{DynamicPPL.VarName{:alpha,Tuple{}},Int64},Array{Gamma{Float64},1},Array{DynamicPPL.VarName{:alpha,Tuple{}},1},Array{Float64,1},Array{Set{DynamicPPL.Selector},1}},DynamicPPL.Metadata{Dict{DynamicPPL.VarName{:z,Tuple{Tuple{Int64}}},Int64},Array{ChineseRestaurantProcess{DirichletProcess{Float64},TArray{Int64,1}},1},Array{DynamicPPL.VarName{:z,Tuple{Tuple{Int64}}},1},Array{Int64,1},Array{Set{DynamicPPL.Selector},1}},DynamicPPL.Metadata{Dict{DynamicPPL.VarName{:mu,Tuple{Tuple{Int64}}},Int64},Array{Normal{Float64},1},Array{DynamicPPL.VarName{:mu,Tuple{Tuple{Int64}}},1},Array{Float64,1},Array{Set{DynamicPPL.Selector},1}},DynamicPPL.Metadata{Dict{DynamicPPL.VarName{:sigma,Tuple{Tuple{Int64}}},Int64},Array{Uniform{Float64},1},Array{DynamicPPL.VarName{:sigma,Tuple{Tuple{Int64}}},1},Array{Float64,1},Array{Set{DynamicPPL.Selector},1}}}},Float64},DynamicPPL.Model{var"###evaluator#311",(:y, :Kmax),Tuple{Array{Float64,1},Int64},(),DynamicPPL.ModelGen{var"###generator#312",(:y, :Kmax),(),Tuple{}}},DynamicPPL.Sampler{HMC{Turing.Core.ForwardDiffAD{40},(:mu, :sigma),AdvancedHMC.UnitEuclideanMetric},Turing.Inference.SamplerState{DynamicPPL.VarInfo{NamedTuple{(:alpha, :z, :mu, :sigma),Tuple{DynamicPPL.Metadata{Dict{DynamicPPL.VarName{:alpha,Tuple{}},Int64},Array{Gamma{Float64},1},Array{DynamicPPL.VarName{:alpha,Tuple{}},1},Array{Float64,1},Array{Set{DynamicPPL.Selector},1}},DynamicPPL.Metadata{Dict{DynamicPPL.VarName{:z,Tuple{Tuple{Int64}}},Int64},Array{ChineseRestaurantProcess{DirichletProcess{Float64},TArray{Int64,1}},1},Array{DynamicPPL.VarName{:z,Tuple{Tuple{Int64}}},1},Array{Int64,1},Array{Set{DynamicPPL.Selector},1}},DynamicPPL.Metadata{Dict{DynamicPPL.VarName{:mu,Tuple{Tuple{Int64}}},Int64},Array{Normal{Float64},1},Array{DynamicPPL.VarName{:mu,Tuple{Tuple{Int64}}},1},Array{Float64,1},Array{Set{DynamicPPL.Selector},1}},DynamicPPL.Metadata{Dict{DynamicPPL.VarName{:sigma,Tuple{Tuple{Int64}}},Int64},Array{Uniform{Float64},1},Array{DynamicPPL.VarName{:sigma,Tuple{Tuple{Int64}}},1},Array{Float64,1},Array{Set{DynamicPPL.Selector},1}}}},Float64}}}}, ::Array{Float64,1}, ::ForwardDiff.GradientConfig{ForwardDiff.Tag{Turing.Core.var"#f#7"{DynamicPPL.VarInfo{NamedTuple{(:alpha, :z, :mu, :sigma),Tuple{DynamicPPL.Metadata{Dict{DynamicPPL.VarName{:alpha,Tuple{}},Int64},Array{Gamma{Float64},1},Array{DynamicPPL.VarName{:alpha,Tuple{}},1},Array{Float64,1},Array{Set{DynamicPPL.Selector},1}},DynamicPPL.Metadata{Dict{DynamicPPL.VarName{:z,Tuple{Tuple{Int64}}},Int64},Array{ChineseRestaurantProcess{DirichletProcess{Float64},TArray{Int64,1}},1},Array{DynamicPPL.VarName{:z,Tuple{Tuple{Int64}}},1},Array{Int64,1},Array{Set{DynamicPPL.Selector},1}},DynamicPPL.Metadata{Dict{DynamicPPL.VarName{:mu,Tuple{Tuple{Int64}}},Int64},Array{Normal{Float64},1},Array{DynamicPPL.VarName{:mu,Tuple{Tuple{Int64}}},1},Array{Float64,1},Array{Set{DynamicPPL.Selector},1}},DynamicPPL.Metadata{Dict{DynamicPPL.VarName{:sigma,Tuple{Tuple{Int64}}},Int64},Array{Uniform{Float64},1},Array{DynamicPPL.VarName{:sigma,Tuple{Tuple{Int64}}},1},Array{Float64,1},Array{Set{DynamicPPL.Selector},1}}}},Float64},DynamicPPL.Model{var"###evaluator#311",(:y, :Kmax),Tuple{Array{Float64,1},Int64},(),DynamicPPL.ModelGen{var"###generator#312",(:y, :Kmax),(),Tuple{}}},DynamicPPL.Sampler{HMC{Turing.Core.ForwardDiffAD{40},(:mu, :sigma),AdvancedHMC.UnitEuclideanMetric},Turing.Inference.SamplerState{DynamicPPL.VarInfo{NamedTuple{(:alpha, :z, :mu, :sigma),Tuple{DynamicPPL.Metadata{Dict{DynamicPPL.VarName{:alpha,Tuple{}},Int64},Array{Gamma{Float64},1},Array{DynamicPPL.VarName{:alpha,Tuple{}},1},Array{Float64,1},Array{Set{DynamicPPL.Selector},1}},DynamicPPL.Metadata{Dict{DynamicPPL.VarName{:z,Tuple{Tuple{Int64}}},Int64},Array{ChineseRestaurantProcess{DirichletProcess{Float64},TArray{Int64,1}},1},Array{DynamicPPL.VarName{:z,Tuple{Tuple{Int64}}},1},Array{Int64,1},Array{Set{DynamicPPL.Selector},1}},DynamicPPL.Metadata{Dict{DynamicPPL.VarName{:mu,Tuple{Tuple{Int64}}},Int64},Array{Normal{Float64},1},Array{DynamicPPL.VarName{:mu,Tuple{Tuple{Int64}}},1},Array{Float64,1},Array{Set{DynamicPPL.Selector},1}},DynamicPPL.Metadata{Dict{DynamicPPL.VarName{:sigma,Tuple{Tuple{Int64}}},Int64},Array{Uniform{Float64},1},Array{DynamicPPL.VarName{:sigma,Tuple{Tuple{Int64}}},1},Array{Float64,1},Array{Set{DynamicPPL.Selector},1}}}},Float64}}}},Float64},Float64,2,Array{ForwardDiff.Dual{ForwardDiff.Tag{Turing.Core.var"#f#7"{DynamicPPL.VarInfo{NamedTuple{(:alpha, :z, :mu, :sigma),Tuple{DynamicPPL.Metadata{Dict{DynamicPPL.VarName{:alpha,Tuple{}},Int64},Array{Gamma{Float64},1},Array{DynamicPPL.VarName{:alpha,Tuple{}},1},Array{Float64,1},Array{Set{DynamicPPL.Selector},1}},DynamicPPL.Metadata{Dict{DynamicPPL.VarName{:z,Tuple{Tuple{Int64}}},Int64},Array{ChineseRestaurantProcess{DirichletProcess{Float64},TArray{Int64,1}},1},Array{DynamicPPL.VarName{:z,Tuple{Tuple{Int64}}},1},Array{Int64,1},Array{Set{DynamicPPL.Selector},1}},DynamicPPL.Metadata{Dict{DynamicPPL.VarName{:mu,Tuple{Tuple{Int64}}},Int64},Array{Normal{Float64},1},Array{DynamicPPL.VarName{:mu,Tuple{Tuple{Int64}}},1},Array{Float64,1},Array{Set{DynamicPPL.Selector},1}},DynamicPPL.Metadata{Dict{DynamicPPL.VarName{:sigma,Tuple{Tuple{Int64}}},Int64},Array{Uniform{Float64},1},Array{DynamicPPL.VarName{:sigma,Tuple{Tuple{Int64}}},1},Array{Float64,1},Array{Set{DynamicPPL.Selector},1}}}},Float64},DynamicPPL.Model{var"###evaluator#311",(:y, :Kmax),Tuple{Array{Float64,1},Int64},(),DynamicPPL.ModelGen{var"###generator#312",(:y, :Kmax),(),Tuple{}}},DynamicPPL.Sampler{HMC{Turing.Core.ForwardDiffAD{40},(:mu, :sigma),AdvancedHMC.UnitEuclideanMetric},Turing.Inference.SamplerState{DynamicPPL.VarInfo{NamedTuple{(:alpha, :z, :mu, :sigma),Tuple{DynamicPPL.Metadata{Dict{DynamicPPL.VarName{:alpha,Tuple{}},Int64},Array{Gamma{Float64},1},Array{DynamicPPL.VarName{:alpha,Tuple{}},1},Array{Float64,1},Array{Set{DynamicPPL.Selector},1}},DynamicPPL.Metadata{Dict{DynamicPPL.VarName{:z,Tuple{Tuple{Int64}}},Int64},Array{ChineseRestaurantProcess{DirichletProcess{Float64},TArray{Int64,1}},1},Array{DynamicPPL.VarName{:z,Tuple{Tuple{Int64}}},1},Array{Int64,1},Array{Set{DynamicPPL.Selector},1}},DynamicPPL.Metadata{Dict{DynamicPPL.VarName{:mu,Tuple{Tuple{Int64}}},Int64},Array{Normal{Float64},1},Array{DynamicPPL.VarName{:mu,Tuple{Tuple{Int64}}},1},Array{Float64,1},Array{Set{DynamicPPL.Selector},1}},DynamicPPL.Metadata{Dict{DynamicPPL.VarName{:sigma,Tuple{Tuple{Int64}}},Int64},Array{Uniform{Float64},1},Array{DynamicPPL.VarName{:sigma,Tuple{Tuple{Int64}}},1},Array{Float64,1},Array{Set{DynamicPPL.Selector},1}}}},Float64}}}},Float64},Float64,2},1}}) at /home/ubuntu/.julia/packages/ForwardDiff/sdToQ/src/gradient.jl:33
 [14] gradient_logp(::Turing.Core.ForwardDiffAD{40}, ::Array{Float64,1}, ::DynamicPPL.VarInfo{NamedTuple{(:alpha, :z, :mu, :sigma),Tuple{DynamicPPL.Metadata{Dict{DynamicPPL.VarName{:alpha,Tuple{}},Int64},Array{Gamma{Float64},1},Array{DynamicPPL.VarName{:alpha,Tuple{}},1},Array{Float64,1},Array{Set{DynamicPPL.Selector},1}},DynamicPPL.Metadata{Dict{DynamicPPL.VarName{:z,Tuple{Tuple{Int64}}},Int64},Array{ChineseRestaurantProcess{DirichletProcess{Float64},TArray{Int64,1}},1},Array{DynamicPPL.VarName{:z,Tuple{Tuple{Int64}}},1},Array{Int64,1},Array{Set{DynamicPPL.Selector},1}},DynamicPPL.Metadata{Dict{DynamicPPL.VarName{:mu,Tuple{Tuple{Int64}}},Int64},Array{Normal{Float64},1},Array{DynamicPPL.VarName{:mu,Tuple{Tuple{Int64}}},1},Array{Float64,1},Array{Set{DynamicPPL.Selector},1}},DynamicPPL.Metadata{Dict{DynamicPPL.VarName{:sigma,Tuple{Tuple{Int64}}},Int64},Array{Uniform{Float64},1},Array{DynamicPPL.VarName{:sigma,Tuple{Tuple{Int64}}},1},Array{Float64,1},Array{Set{DynamicPPL.Selector},1}}}},Float64}, ::DynamicPPL.Model{var"###evaluator#311",(:y, :Kmax),Tuple{Array{Float64,1},Int64},(),DynamicPPL.ModelGen{var"###generator#312",(:y, :Kmax),(),Tuple{}}}, ::DynamicPPL.Sampler{HMC{Turing.Core.ForwardDiffAD{40},(:mu, :sigma),AdvancedHMC.UnitEuclideanMetric},Turing.Inference.SamplerState{DynamicPPL.VarInfo{NamedTuple{(:alpha, :z, :mu, :sigma),Tuple{DynamicPPL.Metadata{Dict{DynamicPPL.VarName{:alpha,Tuple{}},Int64},Array{Gamma{Float64},1},Array{DynamicPPL.VarName{:alpha,Tuple{}},1},Array{Float64,1},Array{Set{DynamicPPL.Selector},1}},DynamicPPL.Metadata{Dict{DynamicPPL.VarName{:z,Tuple{Tuple{Int64}}},Int64},Array{ChineseRestaurantProcess{DirichletProcess{Float64},TArray{Int64,1}},1},Array{DynamicPPL.VarName{:z,Tuple{Tuple{Int64}}},1},Array{Int64,1},Array{Set{DynamicPPL.Selector},1}},DynamicPPL.Metadata{Dict{DynamicPPL.VarName{:mu,Tuple{Tuple{Int64}}},Int64},Array{Normal{Float64},1},Array{DynamicPPL.VarName{:mu,Tuple{Tuple{Int64}}},1},Array{Float64,1},Array{Set{DynamicPPL.Selector},1}},DynamicPPL.Metadata{Dict{DynamicPPL.VarName{:sigma,Tuple{Tuple{Int64}}},Int64},Array{Uniform{Float64},1},Array{DynamicPPL.VarName{:sigma,Tuple{Tuple{Int64}}},1},Array{Float64,1},Array{Set{DynamicPPL.Selector},1}}}},Float64}}}) at /home/ubuntu/.julia/packages/Turing/NZ9IU/src/core/ad.jl:114
 [15] gradient_logp at /home/ubuntu/.julia/packages/Turing/NZ9IU/src/core/ad.jl:78 [inlined]
 [16] ∂logπ∂θ at /home/ubuntu/.julia/packages/Turing/NZ9IU/src/inference/hmc.jl:474 [inlined]
 [17] ∂H∂θ at /home/ubuntu/.julia/packages/AdvancedHMC/P9wqk/src/hamiltonian.jl:31 [inlined]
 [18] phasepoint(::AdvancedHMC.Hamiltonian{AdvancedHMC.UnitEuclideanMetric{Float64,Tuple{Int64}},Turing.Inference.var"#logπ#55"{DynamicPPL.VarInfo{NamedTuple{(:alpha, :z, :mu, :sigma),Tuple{DynamicPPL.Metadata{Dict{DynamicPPL.VarName{:alpha,Tuple{}},Int64},Array{Gamma{Float64},1},Array{DynamicPPL.VarName{:alpha,Tuple{}},1},Array{Float64,1},Array{Set{DynamicPPL.Selector},1}},DynamicPPL.Metadata{Dict{DynamicPPL.VarName{:z,Tuple{Tuple{Int64}}},Int64},Array{ChineseRestaurantProcess{DirichletProcess{Float64},TArray{Int64,1}},1},Array{DynamicPPL.VarName{:z,Tuple{Tuple{Int64}}},1},Array{Int64,1},Array{Set{DynamicPPL.Selector},1}},DynamicPPL.Metadata{Dict{DynamicPPL.VarName{:mu,Tuple{Tuple{Int64}}},Int64},Array{Normal{Float64},1},Array{DynamicPPL.VarName{:mu,Tuple{Tuple{Int64}}},1},Array{Float64,1},Array{Set{DynamicPPL.Selector},1}},DynamicPPL.Metadata{Dict{DynamicPPL.VarName{:sigma,Tuple{Tuple{Int64}}},Int64},Array{Uniform{Float64},1},Array{DynamicPPL.VarName{:sigma,Tuple{Tuple{Int64}}},1},Array{Float64,1},Array{Set{DynamicPPL.Selector},1}}}},Float64},DynamicPPL.Sampler{HMC{Turing.Core.ForwardDiffAD{40},(:mu, :sigma),AdvancedHMC.UnitEuclideanMetric},Turing.Inference.SamplerState{DynamicPPL.VarInfo{NamedTuple{(:alpha, :z, :mu, :sigma),Tuple{DynamicPPL.Metadata{Dict{DynamicPPL.VarName{:alpha,Tuple{}},Int64},Array{Gamma{Float64},1},Array{DynamicPPL.VarName{:alpha,Tuple{}},1},Array{Float64,1},Array{Set{DynamicPPL.Selector},1}},DynamicPPL.Metadata{Dict{DynamicPPL.VarName{:z,Tuple{Tuple{Int64}}},Int64},Array{ChineseRestaurantProcess{DirichletProcess{Float64},TArray{Int64,1}},1},Array{DynamicPPL.VarName{:z,Tuple{Tuple{Int64}}},1},Array{Int64,1},Array{Set{DynamicPPL.Selector},1}},DynamicPPL.Metadata{Dict{DynamicPPL.VarName{:mu,Tuple{Tuple{Int64}}},Int64},Array{Normal{Float64},1},Array{DynamicPPL.VarName{:mu,Tuple{Tuple{Int64}}},1},Array{Float64,1},Array{Set{DynamicPPL.Selector},1}},DynamicPPL.Metadata{Dict{DynamicPPL.VarName{:sigma,Tuple{Tuple{Int64}}},Int64},Array{Uniform{Float64},1},Array{DynamicPPL.VarName{:sigma,Tuple{Tuple{Int64}}},1},Array{Float64,1},Array{Set{DynamicPPL.Selector},1}}}},Float64}}},DynamicPPL.Model{var"###evaluator#311",(:y, :Kmax),Tuple{Array{Float64,1},Int64},(),DynamicPPL.ModelGen{var"###generator#312",(:y, :Kmax),(),Tuple{}}}},Turing.Inference.var"#∂logπ∂θ#54"{DynamicPPL.VarInfo{NamedTuple{(:alpha, :z, :mu, :sigma),Tuple{DynamicPPL.Metadata{Dict{DynamicPPL.VarName{:alpha,Tuple{}},Int64},Array{Gamma{Float64},1},Array{DynamicPPL.VarName{:alpha,Tuple{}},1},Array{Float64,1},Array{Set{DynamicPPL.Selector},1}},DynamicPPL.Metadata{Dict{DynamicPPL.VarName{:z,Tuple{Tuple{Int64}}},Int64},Array{ChineseRestaurantProcess{DirichletProcess{Float64},TArray{Int64,1}},1},Array{DynamicPPL.VarName{:z,Tuple{Tuple{Int64}}},1},Array{Int64,1},Array{Set{DynamicPPL.Selector},1}},DynamicPPL.Metadata{Dict{DynamicPPL.VarName{:mu,Tuple{Tuple{Int64}}},Int64},Array{Normal{Float64},1},Array{DynamicPPL.VarName{:mu,Tuple{Tuple{Int64}}},1},Array{Float64,1},Array{Set{DynamicPPL.Selector},1}},DynamicPPL.Metadata{Dict{DynamicPPL.VarName{:sigma,Tuple{Tuple{Int64}}},Int64},Array{Uniform{Float64},1},Array{DynamicPPL.VarName{:sigma,Tuple{Tuple{Int64}}},1},Array{Float64,1},Array{Set{DynamicPPL.Selector},1}}}},Float64},DynamicPPL.Sampler{HMC{Turing.Core.ForwardDiffAD{40},(:mu, :sigma),AdvancedHMC.UnitEuclideanMetric},Turing.Inference.SamplerState{DynamicPPL.VarInfo{NamedTuple{(:alpha, :z, :mu, :sigma),Tuple{DynamicPPL.Metadata{Dict{DynamicPPL.VarName{:alpha,Tuple{}},Int64},Array{Gamma{Float64},1},Array{DynamicPPL.VarName{:alpha,Tuple{}},1},Array{Float64,1},Array{Set{DynamicPPL.Selector},1}},DynamicPPL.Metadata{Dict{DynamicPPL.VarName{:z,Tuple{Tuple{Int64}}},Int64},Array{ChineseRestaurantProcess{DirichletProcess{Float64},TArray{Int64,1}},1},Array{DynamicPPL.VarName{:z,Tuple{Tuple{Int64}}},1},Array{Int64,1},Array{Set{DynamicPPL.Selector},1}},DynamicPPL.Metadata{Dict{DynamicPPL.VarName{:mu,Tuple{Tuple{Int64}}},Int64},Array{Normal{Float64},1},Array{DynamicPPL.VarName{:mu,Tuple{Tuple{Int64}}},1},Array{Float64,1},Array{Set{DynamicPPL.Selector},1}},DynamicPPL.Metadata{Dict{DynamicPPL.VarName{:sigma,Tuple{Tuple{Int64}}},Int64},Array{Uniform{Float64},1},Array{DynamicPPL.VarName{:sigma,Tuple{Tuple{Int64}}},1},Array{Float64,1},Array{Set{DynamicPPL.Selector},1}}}},Float64}}},DynamicPPL.Model{var"###evaluator#311",(:y, :Kmax),Tuple{Array{Float64,1},Int64},(),DynamicPPL.ModelGen{var"###generator#312",(:y, :Kmax),(),Tuple{}}}}}, ::Array{Float64,1}, ::Array{Float64,1}) at /home/ubuntu/.julia/packages/AdvancedHMC/P9wqk/src/hamiltonian.jl:69
 [19] phasepoint at /home/ubuntu/.julia/packages/AdvancedHMC/P9wqk/src/hamiltonian.jl:139 [inlined]
 [20] sample_init at /home/ubuntu/.julia/packages/AdvancedHMC/P9wqk/src/sampler.jl:42 [inlined]
 [21] Turing.Inference.HMCState(::DynamicPPL.Model{var"###evaluator#311",(:y, :Kmax),Tuple{Array{Float64,1},Int64},(),DynamicPPL.ModelGen{var"###generator#312",(:y, :Kmax),(),Tuple{}}}, ::DynamicPPL.Sampler{HMC{Turing.Core.ForwardDiffAD{40},(:mu, :sigma),AdvancedHMC.UnitEuclideanMetric},Turing.Inference.SamplerState{DynamicPPL.VarInfo{NamedTuple{(:alpha, :z, :mu, :sigma),Tuple{DynamicPPL.Metadata{Dict{DynamicPPL.VarName{:alpha,Tuple{}},Int64},Array{Gamma{Float64},1},Array{DynamicPPL.VarName{:alpha,Tuple{}},1},Array{Float64,1},Array{Set{DynamicPPL.Selector},1}},DynamicPPL.Metadata{Dict{DynamicPPL.VarName{:z,Tuple{Tuple{Int64}}},Int64},Array{ChineseRestaurantProcess{DirichletProcess{Float64},TArray{Int64,1}},1},Array{DynamicPPL.VarName{:z,Tuple{Tuple{Int64}}},1},Array{Int64,1},Array{Set{DynamicPPL.Selector},1}},DynamicPPL.Metadata{Dict{DynamicPPL.VarName{:mu,Tuple{Tuple{Int64}}},Int64},Array{Normal{Float64},1},Array{DynamicPPL.VarName{:mu,Tuple{Tuple{Int64}}},1},Array{Float64,1},Array{Set{DynamicPPL.Selector},1}},DynamicPPL.Metadata{Dict{DynamicPPL.VarName{:sigma,Tuple{Tuple{Int64}}},Int64},Array{Uniform{Float64},1},Array{DynamicPPL.VarName{:sigma,Tuple{Tuple{Int64}}},1},Array{Float64,1},Array{Set{DynamicPPL.Selector},1}}}},Float64}}}, ::Random._GLOBAL_RNG; kwargs::Base.Iterators.Pairs{Union{},Union{},Tuple{},NamedTuple{(),Tuple{}}}) at /home/ubuntu/.julia/packages/Turing/NZ9IU/src/inference/hmc.jl:638
 [22] Turing.Inference.HMCState(::DynamicPPL.Model{var"###evaluator#311",(:y, :Kmax),Tuple{Array{Float64,1},Int64},(),DynamicPPL.ModelGen{var"###generator#312",(:y, :Kmax),(),Tuple{}}}, ::DynamicPPL.Sampler{HMC{Turing.Core.ForwardDiffAD{40},(:mu, :sigma),AdvancedHMC.UnitEuclideanMetric},Turing.Inference.SamplerState{DynamicPPL.VarInfo{NamedTuple{(:alpha, :z, :mu, :sigma),Tuple{DynamicPPL.Metadata{Dict{DynamicPPL.VarName{:alpha,Tuple{}},Int64},Array{Gamma{Float64},1},Array{DynamicPPL.VarName{:alpha,Tuple{}},1},Array{Float64,1},Array{Set{DynamicPPL.Selector},1}},DynamicPPL.Metadata{Dict{DynamicPPL.VarName{:z,Tuple{Tuple{Int64}}},Int64},Array{ChineseRestaurantProcess{DirichletProcess{Float64},TArray{Int64,1}},1},Array{DynamicPPL.VarName{:z,Tuple{Tuple{Int64}}},1},Array{Int64,1},Array{Set{DynamicPPL.Selector},1}},DynamicPPL.Metadata{Dict{DynamicPPL.VarName{:mu,Tuple{Tuple{Int64}}},Int64},Array{Normal{Float64},1},Array{DynamicPPL.VarName{:mu,Tuple{Tuple{Int64}}},1},Array{Float64,1},Array{Set{DynamicPPL.Selector},1}},DynamicPPL.Metadata{Dict{DynamicPPL.VarName{:sigma,Tuple{Tuple{Int64}}},Int64},Array{Uniform{Float64},1},Array{DynamicPPL.VarName{:sigma,Tuple{Tuple{Int64}}},1},Array{Float64,1},Array{Set{DynamicPPL.Selector},1}}}},Float64}}}, ::Random._GLOBAL_RNG) at /home/ubuntu/.julia/packages/Turing/NZ9IU/src/inference/hmc.jl:609
 [23] DynamicPPL.Sampler(::HMC{Turing.Core.ForwardDiffAD{40},(:mu, :sigma),AdvancedHMC.UnitEuclideanMetric}, ::DynamicPPL.Model{var"###evaluator#311",(:y, :Kmax),Tuple{Array{Float64,1},Int64},(),DynamicPPL.ModelGen{var"###generator#312",(:y, :Kmax),(),Tuple{}}}, ::DynamicPPL.Selector) at /home/ubuntu/.julia/packages/Turing/NZ9IU/src/inference/hmc.jl:384
 [24] (::Turing.Inference.var"#80#81"{Gibbs{(:alpha, :z, :mu, :sigma),Tuple{MH{(:alpha,),NamedTuple{(),Tuple{}}},PG{(:z,),Turing.Core.ResampleWithESSThreshold{typeof(Turing.Inference.resample_systematic),Float64}},HMC{Turing.Core.ForwardDiffAD{40},(:mu, :sigma),AdvancedHMC.UnitEuclideanMetric}}},DynamicPPL.Model{var"###evaluator#311",(:y, :Kmax),Tuple{Array{Float64,1},Int64},(),DynamicPPL.ModelGen{var"###generator#312",(:y, :Kmax),(),Tuple{}}}})(::HMC{Turing.Core.ForwardDiffAD{40},(:mu, :sigma),AdvancedHMC.UnitEuclideanMetric}) at /home/ubuntu/.julia/packages/Turing/NZ9IU/src/inference/gibbs.jl:85
 [25] map at ./tuple.jl:159 [inlined]
 [26] DynamicPPL.Sampler(::Gibbs{(:alpha, :z, :mu, :sigma),Tuple{MH{(:alpha,),NamedTuple{(),Tuple{}}},PG{(:z,),Turing.Core.ResampleWithESSThreshold{typeof(Turing.Inference.resample_systematic),Float64}},HMC{Turing.Core.ForwardDiffAD{40},(:mu, :sigma),AdvancedHMC.UnitEuclideanMetric}}}, ::DynamicPPL.Model{var"###evaluator#311",(:y, :Kmax),Tuple{Array{Float64,1},Int64},(),DynamicPPL.ModelGen{var"###generator#312",(:y, :Kmax),(),Tuple{}}}, ::DynamicPPL.Selector) at /home/ubuntu/.julia/packages/Turing/NZ9IU/src/inference/gibbs.jl:76
 [27] Sampler at /home/ubuntu/.julia/packages/DynamicPPL/MRwtL/src/sampler.jl:55 [inlined]
 [28] sample(::Random._GLOBAL_RNG, ::DynamicPPL.Model{var"###evaluator#311",(:y, :Kmax),Tuple{Array{Float64,1},Int64},(),DynamicPPL.ModelGen{var"###generator#312",(:y, :Kmax),(),Tuple{}}}, ::Gibbs{(:alpha, :z, :mu, :sigma),Tuple{MH{(:alpha,),NamedTuple{(),Tuple{}}},PG{(:z,),Turing.Core.ResampleWithESSThreshold{typeof(Turing.Inference.resample_systematic),Float64}},HMC{Turing.Core.ForwardDiffAD{40},(:mu, :sigma),AdvancedHMC.UnitEuclideanMetric}}}, ::Int64; kwargs::Base.Iterators.Pairs{Union{},Union{},Tuple{},NamedTuple{(),Tuple{}}}) at /home/ubuntu/.julia/packages/Turing/NZ9IU/src/inference/Inference.jl:165
 [29] sample at /home/ubuntu/.julia/packages/Turing/NZ9IU/src/inference/Inference.jl:165 [inlined]
 [30] #sample#1 at /home/ubuntu/.julia/packages/Turing/NZ9IU/src/inference/Inference.jl:155 [inlined]
 [31] sample(::DynamicPPL.Model{var"###evaluator#311",(:y, :Kmax),Tuple{Array{Float64,1},Int64},(),DynamicPPL.ModelGen{var"###generator#312",(:y, :Kmax),(),Tuple{}}}, ::Gibbs{(:alpha, :z, :mu, :sigma),Tuple{MH{(:alpha,),NamedTuple{(),Tuple{}}},PG{(:z,),Turing.Core.ResampleWithESSThreshold{typeof(Turing.Inference.resample_systematic),Float64}},HMC{Turing.Core.ForwardDiffAD{40},(:mu, :sigma),AdvancedHMC.UnitEuclideanMetric}}}, ::Int64) at /home/ubuntu/.julia/packages/Turing/NZ9IU/src/inference/Inference.jl:155
 [32] macro expansion at ./In[95]:20 [inlined]
 [33] macro expansion at ./util.jl:175 [inlined]
 [34] top-level scope at ./In[95]:3
trappmartin commented 4 years ago

You need to slightly adjust the model definition so that it works with AD.

trappmartin commented 4 years ago
@model function dp_gmm_crp_delayed(y, Kmax, ::Type{T}=Vector{Float64}) where {T}
    nobs = length(y)

    alpha ~ Gamma(1, 1/10)  # mean = a*b
    rpm = DirichletProcess(alpha)

    # Base measure.
    H_mu = Normal(0, 3)
    H_sigma = LogNormal(-1, 0.5)

    # Latent assignment.
    z = tzeros(Int, nobs)
    n = tzeros(Int, nobs)
    K = 0

    mu = T(undef, Kmax)
    sigma = T(undef, Kmax)

    for i in 1:nobs
        z[i] ~ ChineseRestaurantProcess(rpm,  n)
        n[z[i]] += 1

        if z[i] > K
            mu[z[i]] ~ H_mu
            sigma[z[i]] ~ H_sigma
            K += 1
        end

        y[i] ~ Normal(mu[z[i]], sigma[z[i]])
    end
end;

That should work.

luiarthur commented 4 years ago

Thanks, I'll try that.

Also, what is the reason for tzeros instead of zeros for z?

trappmartin commented 4 years ago

tzeros is instantiating a copy-on-write array, which is necessary for particle-based samplers as we instantiate new particles by copying the Julia task (and local memory) associated with a particle. Whenever you see TArray or tzeros it's instantiating such an array. But this is only necessary for particle-based samplers and should not be used in general.

See this library: https://github.com/TuringLang/Libtask.jl

luiarthur commented 4 years ago

So if I were to use PG for mu, then I would need mu = tzeros(Float64, Kmax) again, correct?

trappmartin commented 4 years ago

Yes.