|
| 1 | +using LogDensityProblems |
| 2 | + |
| 3 | +abstract type AbstractGMM end |
| 4 | + |
| 5 | +struct GMM <: AbstractGMM |
| 6 | + data::NamedTuple |
| 7 | +end |
| 8 | + |
| 9 | +struct ConditionedGMM{conditioned_vars} <: AbstractGMM |
| 10 | + data::NamedTuple |
| 11 | + conditioned_values::NamedTuple{conditioned_vars} |
| 12 | +end |
| 13 | + |
| 14 | +function log_joint(; μ, w, z, x) |
| 15 | + # μ is mean of each component |
| 16 | + # w is weights of each component |
| 17 | + # z is assignment of each data point |
| 18 | + # x is data |
| 19 | + |
| 20 | + K = 2 # assume we know the number of components |
| 21 | + D = 2 # dimension of each data point |
| 22 | + N = size(x, 2) # number of data points |
| 23 | + logp = 0.0 |
| 24 | + |
| 25 | + μ_prior = MvNormal(zeros(K), I) |
| 26 | + logp += logpdf(μ_prior, μ) |
| 27 | + |
| 28 | + w_prior = Dirichlet(K, 1.0) |
| 29 | + logp += logpdf(w_prior, w) |
| 30 | + |
| 31 | + z_prior = Categorical(w) |
| 32 | + logp += sum([logpdf(z_prior, z[i]) for i in 1:N]) |
| 33 | + |
| 34 | + obs_priors = [MvNormal(fill(μₖ, D), I) for μₖ in μ] |
| 35 | + for i in 1:N |
| 36 | + logp += logpdf(obs_priors[z[i]], x[:, i]) |
| 37 | + end |
| 38 | + |
| 39 | + return logp |
| 40 | +end |
| 41 | + |
| 42 | +function condition(gmm::GMM, conditioned_values::NamedTuple) |
| 43 | + return ConditionedGMM(gmm.data, conditioned_values) |
| 44 | +end |
| 45 | + |
| 46 | +function _logdensity(gmm::ConditionedGMM{(:μ, :w)}, params) |
| 47 | + return log_joint(; |
| 48 | + μ=gmm.conditioned_values.μ, w=gmm.conditioned_values.w, z=params.z, x=gmm.data.x |
| 49 | + ) |
| 50 | +end |
| 51 | +function _logdensity(gmm::ConditionedGMM{(:z,)}, params) |
| 52 | + return log_joint(; μ=params.μ, w=params.w, z=gmm.conditioned_values.z, x=gmm.data.x) |
| 53 | +end |
| 54 | + |
| 55 | +function LogDensityProblems.logdensity( |
| 56 | + gmm::ConditionedGMM{(:μ, :w)}, params_vec::AbstractVector |
| 57 | +) |
| 58 | + return _logdensity(gmm, (; z=params_vec)) |
| 59 | +end |
| 60 | +function LogDensityProblems.logdensity( |
| 61 | + gmm::ConditionedGMM{(:z,)}, params_vec::AbstractVector |
| 62 | +) |
| 63 | + return _logdensity(gmm, (; μ=params_vec[1:2], w=params_vec[3:4])) |
| 64 | +end |
| 65 | + |
| 66 | +function LogDensityProblems.dimension(gmm::ConditionedGMM{(:μ, :w)}) |
| 67 | + return size(gmm.data.x, 1) |
| 68 | +end |
| 69 | +function LogDensityProblems.dimension(gmm::ConditionedGMM{(:z,)}) |
| 70 | + return size(gmm.data.x, 1) |
| 71 | +end |
| 72 | + |
| 73 | +## test using Turing |
| 74 | + |
| 75 | +# data generation |
| 76 | + |
| 77 | +using Distributions |
| 78 | +using FillArrays |
| 79 | +using LinearAlgebra |
| 80 | +using Random |
| 81 | + |
| 82 | +w = [0.5, 0.5] |
| 83 | +μ = [-3.5, 0.5] |
| 84 | +mixturemodel = Distributions.MixtureModel([MvNormal(Fill(μₖ, 2), I) for μₖ in μ], w) |
| 85 | + |
| 86 | +N = 60 |
| 87 | +x = rand(mixturemodel, N); |
| 88 | + |
| 89 | +# Turing model from https://turinglang.org/docs/tutorials/01-gaussian-mixture-model/ |
| 90 | +using Turing |
| 91 | + |
| 92 | +@model function gaussian_mixture_model(x) |
| 93 | + # Draw the parameters for each of the K=2 clusters from a standard normal distribution. |
| 94 | + K = 2 |
| 95 | + μ ~ MvNormal(Zeros(K), I) |
| 96 | + |
| 97 | + # Draw the weights for the K clusters from a Dirichlet distribution with parameters αₖ = 1. |
| 98 | + w ~ Dirichlet(K, 1.0) |
| 99 | + # Alternatively, one could use a fixed set of weights. |
| 100 | + # w = fill(1/K, K) |
| 101 | + |
| 102 | + # Construct categorical distribution of assignments. |
| 103 | + distribution_assignments = Categorical(w) |
| 104 | + |
| 105 | + # Construct multivariate normal distributions of each cluster. |
| 106 | + D, N = size(x) |
| 107 | + distribution_clusters = [MvNormal(Fill(μₖ, D), I) for μₖ in μ] |
| 108 | + |
| 109 | + # Draw assignments for each datum and generate it from the multivariate normal distribution. |
| 110 | + k = Vector{Int}(undef, N) |
| 111 | + for i in 1:N |
| 112 | + k[i] ~ distribution_assignments |
| 113 | + x[:, i] ~ distribution_clusters[k[i]] |
| 114 | + end |
| 115 | + |
| 116 | + return μ, w, k, __varinfo__ |
| 117 | +end |
| 118 | + |
| 119 | +model = gaussian_mixture_model(x); |
| 120 | + |
| 121 | +using Test |
| 122 | +# full model |
| 123 | +μ, w, k, vi = model() |
| 124 | +@test log_joint(; μ=μ, w=w, z=k, x=x) ≈ DynamicPPL.getlogp(vi) |
| 125 | + |
| 126 | +gmm = GMM((; x=x)) |
| 127 | + |
| 128 | +# cond model on μ, w |
| 129 | +μ, w, k, vi = (DynamicPPL.condition(model, (μ=μ, w=w)))() |
| 130 | +@test _logdensity(condition(gmm, (; μ=μ, w=w)), (; z=k)) ≈ DynamicPPL.getlogp(vi) |
| 131 | + |
| 132 | +# cond model on z |
| 133 | +μ, w, k, vi = (DynamicPPL.condition(model, (z = k)))() |
| 134 | +@test _logdensity(condition(gmm, (; z=k)), (; μ=μ, w=w)) ≈ DynamicPPL.getlogp(vi) |
0 commit comments