Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modeling discrete variables #315

Open
jariji opened this issue Dec 29, 2021 · 0 comments
Open

Modeling discrete variables #315

jariji opened this issue Dec 29, 2021 · 0 comments

Comments

@jariji
Copy link

jariji commented Dec 29, 2021

Hi @cscherrer,

I tried writing Stan's estimating parameters of a mixture in Soss but I must be missing something. I'm not sure how to translate the loop

for (n in 1:N) {
    vector[K] lps = log_theta;
    for (k in 1:K) {
      lps[k] += normal_lpdf(y[n] | mu[k], sigma[k]);
    }
    target += log_sum_exp(lps);
}
# https://mc-stan.org/docs/2_28/stan-users-guide/summing-out-the-responsibility-parameter.html

using TransformVariables, LogDensityProblems, DynamicHMC,
    DynamicHMC.Diagnostics, Parameters, Statistics, Random, Distributions, Soss


model = @model (N,K) begin
    # Replaced LogNormal with HalfCauchy.
    σ .~ fill(HalfCauchy(2.), K)
    μ .~ fill(Normal(0., 10.), K)
    y ~ For(1:N) do n
        # What about `lps`?
        For(1:K) do k
            Normal(μ[k], σ[k])
        end
        # What about `target?`
    end
end
sourceLogdensity(model)

Manually writing

For(1:N) do n
        lps = log.(θ)
        For(1:K) do k
            lps[k] += logpdf(Normal(μ[k], σ[k]), y[n])
        end
        _ℓ += log(sum(exp.(lps)))
    end

doesn't seem right, since I thought _ℓ gets defined in sourceLogdensity, not in the model.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant