TuringLang / docs

Documentation and tutorials for the Turing language
https://turinglang.org/docs/
MIT License
229 stars 99 forks source link

Chinese restaurant processes #11

Open yebai opened 6 years ago

yebai commented 6 years ago

CRP Demo

trappmartin commented 5 years ago

I think we should wait with this issue until the BNP PR is merged. I integrated CRP representations into the PR and learning an IMM with a CRP prior will be straightforward with the PR.

trappmartin commented 5 years ago

Chinese restaurant process example.

# Some data points.
data = [-2,2,-1.5,1.5]

# Parameters of the base distribution of the Dirichlet process.
mu_0 = mean(data)
sigma_0 = 4

# Hyper-parameter of the Dirichlet process, i.e. concentration parameter.
alpha = 0.25

@model infiniteMixtureModel(y, rpm) = begin

    # Definition of the base distribution.
    H = Normal(mu_0, sigma_0)

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

    # Cluster counts.
    cluster_counts = tzeros(Int, N)

    # Cluster locations.
    x = tzeros(Float64, N)

    for i in 1:N
        # Draw assignments using a Chinese restaurant process.
        z[i] ~ ChineseRestaurantProcess(rpm, cluster_counts)

        if cluster_counts[z[i]] == 0
            # Cluster is new, therefore, draw new location.
            l ~ H
            x[z[i]] ~ H
        end

        cluster_counts[z[i]] += 1
        # Draw observation.
        y[i] ~ Normal(x[z[i]], sigma_1)
    end
end

# We use a Dirichlet process as the random probability measure.
rpm = DirichletProcess(alpha)

# Use a sequential Monte Carlo sampler.
sampler = SMC(5000)

# Inference.
mf = infiniteMixtureModel(data, rpm)
samples = sample(mf, sampler)